1
0

Autogeneration of settings.ini and webadmin.ini

Fixes issue #75
This commit is contained in:
Tiger Wang 2013-11-04 21:51:24 +00:00
parent d47a8ea008
commit e832736e0b
11 changed files with 80 additions and 65 deletions

View File

@ -46,28 +46,9 @@ cAuthenticator::~cAuthenticator()
/// Read custom values from INI /// Read custom values from INI
void cAuthenticator::ReadINI(cIniFile & IniFile) void cAuthenticator::ReadINI(cIniFile & IniFile)
{ {
m_Server = IniFile.GetValue("Authentication", "Server"); m_Server = IniFile.GetValueSet("Authentication", "Server", DEFAULT_AUTH_SERVER);
m_Address = IniFile.GetValue("Authentication", "Address"); m_Address = IniFile.GetValueSet("Authentication", "Address", DEFAULT_AUTH_ADDRESS);
m_ShouldAuthenticate = IniFile.GetValueB("Authentication", "Authenticate", true); m_ShouldAuthenticate = IniFile.GetValueSetB("Authentication", "Authenticate", true);
bool bSave = false;
if (m_Server.length() == 0)
{
m_Server = DEFAULT_AUTH_SERVER;
IniFile.SetValue("Authentication", "Server", m_Server);
bSave = true;
}
if (m_Address.length() == 0)
{
m_Address = DEFAULT_AUTH_ADDRESS;
IniFile.SetValue("Authentication", "Address", m_Address);
bSave = true;
}
if (bSave)
{
IniFile.SetValueB("Authentication", "Authenticate", m_ShouldAuthenticate);
}
} }
@ -199,7 +180,7 @@ bool cAuthenticator::AuthFromAddress(const AString & a_Server, const AString & a
} }
else if (code == 200) else if (code == 200)
{ {
LOGINFO("Got 200 OK :D"); LOGD("cAuthenticator: Received status 200 OK! :D");
bOK = true; bOK = true;
} }
} }
@ -268,17 +249,16 @@ bool cAuthenticator::AuthFromAddress(const AString & a_Server, const AString & a
std::string Result; std::string Result;
ss >> Result; ss >> Result;
LOGINFO("Got result: %s", Result.c_str()); LOGD("cAuthenticator: Authentication result was %s", Result.c_str());
//if (Result.compare("3") == 0) // FIXME: Quick and dirty hack to support auth
//Lapayo: Wtf 3?
if (Result.compare("YES") == 0) //Works well if (Result.compare("YES") == 0) //Works well
{ {
LOGINFO("Result was \"YES\", so player is authenticated!"); LOGINFO("Authentication result \"YES\", player authentication success!");
return true; return true;
} }
LOGINFO("Result was \"%s\", so player is NOT authenticated!", Result.c_str()); LOGINFO("Authentication result was \"%s\", player authentication failure!", Result.c_str());
return false; return false;
} }

View File

@ -205,7 +205,7 @@ void cClientHandle::Kick(const AString & a_Reason)
{ {
if (m_State >= csAuthenticating) // Don't log pings if (m_State >= csAuthenticating) // Don't log pings
{ {
LOG("Kicking user \"%s\" for \"%s\"", m_Username.c_str(), a_Reason.c_str()); LOG("Kicking user \"%s\" for \"%s\"", m_Username.c_str(), StripColorCodes(a_Reason).c_str());
} }
SendDisconnect(a_Reason); SendDisconnect(a_Reason);
} }
@ -2196,7 +2196,7 @@ void cClientHandle::SocketClosed(void)
{ {
// The socket has been closed for any reason // The socket has been closed for any reason
LOG("Client \"%s\" @ %s disconnected", m_Username.c_str(), m_IPString.c_str()); LOGD("Client \"%s\" @ %s disconnected", m_Username.c_str(), m_IPString.c_str());
Destroy(); Destroy();
} }

View File

@ -108,8 +108,8 @@
#include "LeakFinder.h" #include "LeakFinder.h"
// Currently only tested with MS VC++ 5 to 10 // Currently only tested with MS VC++ 5 to 10
#if (_MSC_VER < 1100) || (_MSC_VER > 1700) #if (_MSC_VER < 1100) || (_MSC_VER > 1800)
#error Only MS VC++ 5/6/7/7.1/8/9 supported. Check if the '_CrtMemBlockHeader' has not changed with this compiler! #error Only MS VC++ 5/6/7/7.1/8/9/10/11/12 supported. Check if the '_CrtMemBlockHeader' has not changed with this compiler!
#endif #endif

View File

@ -71,10 +71,10 @@ cMonster::eType cMobSpawner::ChooseMobType(EMCSBiome a_Biome)
addIfAllowed(cMonster::mtZombiePigman, allowedMobs); addIfAllowed(cMonster::mtZombiePigman, allowedMobs);
addIfAllowed(cMonster::mtMagmaCube, allowedMobs); addIfAllowed(cMonster::mtMagmaCube, allowedMobs);
} }
/*else if (a_Biome == biEnder) MG TODO : figure out what are the biomes of the ender else if (a_Biome == biEnd)
{ {
addIfAllowed(cMonster::mtEnderman, allowedMobs); addIfAllowed(cMonster::mtEnderman, allowedMobs);
}*/ }
else else
{ {
addIfAllowed(cMonster::mtBat, allowedMobs); addIfAllowed(cMonster::mtBat, allowedMobs);
@ -210,7 +210,7 @@ bool cMobSpawner::CanSpawnHere(cChunk * a_Chunk, int a_RelX, int a_RelY, int a_R
return (TargetBlock == E_BLOCK_AIR) && (BlockAbove == E_BLOCK_AIR) && (!g_BlockTransparent[BlockBelow]) && return (TargetBlock == E_BLOCK_AIR) && (BlockAbove == E_BLOCK_AIR) && (!g_BlockTransparent[BlockBelow]) &&
(m_Random.NextInt(20,a_Biome) == 0); (m_Random.NextInt(20,a_Biome) == 0);
default: default:
LOGD("MG TODO : check I've got a Rule to write for type %d",a_MobType); LOGD("MG TODO: Write spawning rule for mob type %d", a_MobType);
return false; return false;
} }
} }

View File

@ -94,6 +94,17 @@ void cPluginManager::FindPlugins(void)
void cPluginManager::ReloadPluginsNow(void) void cPluginManager::ReloadPluginsNow(void)
{
cIniFile a_SettingsIni;
a_SettingsIni.ReadFile("settings.ini");
ReloadPluginsNow(a_SettingsIni);
}
void cPluginManager::ReloadPluginsNow(cIniFile & a_SettingsIni)
{ {
LOG("-- Loading Plugins --"); LOG("-- Loading Plugins --");
m_bReloadPlugins = false; m_bReloadPlugins = false;
@ -103,25 +114,21 @@ void cPluginManager::ReloadPluginsNow(void)
cServer::BindBuiltInConsoleCommands(); cServer::BindBuiltInConsoleCommands();
cIniFile IniFile; unsigned int KeyNum = a_SettingsIni.FindKey("Plugins");
if (!IniFile.ReadFile("settings.ini")) unsigned int NumPlugins = ((KeyNum != -1) ? (a_SettingsIni.GetNumValues(KeyNum)) : 0);
if (KeyNum == -1)
{ {
LOGWARNING("cPluginManager: Can't find settings.ini, so can't load any plugins."); a_SettingsIni.AddKeyName("Plugins");
a_SettingsIni.AddKeyComment("Plugins", " Plugin=Core");
} }
else if (NumPlugins > 0)
unsigned int KeyNum = IniFile.FindKey("Plugins");
unsigned int NumPlugins = IniFile.GetNumValues(KeyNum);
if (NumPlugins > 0)
{ {
for(unsigned int i = 0; i < NumPlugins; i++) for(unsigned int i = 0; i < NumPlugins; i++)
{ {
AString ValueName = IniFile.GetValueName(KeyNum, i ); AString ValueName = a_SettingsIni.GetValueName(KeyNum, i);
if ( if (ValueName.compare("Plugin") == 0)
(ValueName.compare("NewPlugin") == 0) ||
(ValueName.compare("Plugin") == 0)
)
{ {
AString PluginFile = IniFile.GetValue(KeyNum, i); AString PluginFile = a_SettingsIni.GetValue(KeyNum, i);
if (!PluginFile.empty()) if (!PluginFile.empty())
{ {
if (m_Plugins.find(PluginFile) != m_Plugins.end()) if (m_Plugins.find(PluginFile) != m_Plugins.end())
@ -137,7 +144,7 @@ void cPluginManager::ReloadPluginsNow(void)
{ {
LOG("-- No Plugins Loaded --"); LOG("-- No Plugins Loaded --");
} }
else if ((GetNumPlugins() > 1) || (GetNumPlugins() == 0)) else if (GetNumPlugins() > 1)
{ {
LOG("-- Loaded %i Plugins --", GetNumPlugins()); LOG("-- Loaded %i Plugins --", GetNumPlugins());
} }

View File

@ -271,7 +271,14 @@ private:
cPluginManager(); cPluginManager();
~cPluginManager(); ~cPluginManager();
/// Reloads all plugins, defaulting to settings.ini for settings location
void ReloadPluginsNow(void); void ReloadPluginsNow(void);
/// Reloads all plugins with a cIniFile object expected to be initialised to settings.ini
/// Used because cRoot otherwise overwrites any configuration generation here if cRoot's IniFile is not used
void ReloadPluginsNow(cIniFile & a_SettingsIni);
/// Unloads all plugins
void UnloadPluginsNow(void); void UnloadPluginsNow(void);
/// Adds the plugin into the internal list of plugins and initializes it. If initialization fails, the plugin is removed again. /// Adds the plugin into the internal list of plugins and initializes it. If initialization fails, the plugin is removed again.

View File

@ -119,8 +119,12 @@ void cRoot::Start(void)
cIniFile IniFile; cIniFile IniFile;
if (!IniFile.ReadFile("settings.ini")) if (!IniFile.ReadFile("settings.ini"))
{ {
LOGWARNING("settings.ini inaccessible, all settings are reset to default values"); LOGWARN("Regenerating settings.ini, all settings will be reset");
IniFile.AddHeaderComment(" This is the main server configuration");
IniFile.AddHeaderComment(" Most of the settings here can be configured using the webadmin interface, if enabled in webadmin.ini");
IniFile.AddHeaderComment(" See: http://www.mc-server.org/wiki/doku.php?id=configure:settings.ini for further configuration help");
} }
m_PrimaryServerVersion = IniFile.GetValueI("Server", "PrimaryServerVersion", 0); m_PrimaryServerVersion = IniFile.GetValueI("Server", "PrimaryServerVersion", 0);
if (m_PrimaryServerVersion == 0) if (m_PrimaryServerVersion == 0)
{ {
@ -129,7 +133,7 @@ void cRoot::Start(void)
else else
{ {
// Make a note in the log that the primary server version is explicitly set in the ini file // Make a note in the log that the primary server version is explicitly set in the ini file
LOGINFO("settings.ini: [Server].PrimaryServerVersion set to %d.", m_PrimaryServerVersion); LOGINFO("Primary server version set explicitly to %d.", m_PrimaryServerVersion);
} }
LOG("Starting server..."); LOG("Starting server...");
@ -152,7 +156,7 @@ void cRoot::Start(void)
LOGD("Loading plugin manager..."); LOGD("Loading plugin manager...");
m_PluginManager = new cPluginManager(); m_PluginManager = new cPluginManager();
m_PluginManager->ReloadPluginsNow(); m_PluginManager->ReloadPluginsNow(IniFile);
LOGD("Loading MonsterConfig..."); LOGD("Loading MonsterConfig...");
m_MonsterConfig = new cMonsterConfig; m_MonsterConfig = new cMonsterConfig;
@ -261,6 +265,7 @@ void cRoot::LoadWorlds(cIniFile & IniFile)
return; return;
} }
bool FoundAdditionalWorlds = false;
for (unsigned int i = 0; i < NumWorlds; i++) for (unsigned int i = 0; i < NumWorlds; i++)
{ {
AString ValueName = IniFile.GetValueName(KeyNum, i ); AString ValueName = IniFile.GetValueName(KeyNum, i );
@ -273,9 +278,15 @@ void cRoot::LoadWorlds(cIniFile & IniFile)
{ {
continue; continue;
} }
FoundAdditionalWorlds = true;
cWorld* NewWorld = new cWorld( WorldName.c_str() ); cWorld* NewWorld = new cWorld( WorldName.c_str() );
m_WorldsByName[ WorldName ] = NewWorld; m_WorldsByName[ WorldName ] = NewWorld;
} // for i - Worlds } // for i - Worlds
if (!FoundAdditionalWorlds)
{
IniFile.AddKeyComment("Worlds", " World=secondworld");
}
} }

View File

@ -195,8 +195,9 @@ void cServer::PlayerDestroying(const cPlayer * a_Player)
bool cServer::InitServer(cIniFile & a_SettingsIni) bool cServer::InitServer(cIniFile & a_SettingsIni)
{ {
m_Description = a_SettingsIni.GetValue ("Server", "Description", "MCServer! - In C++!").c_str(); m_Description = a_SettingsIni.GetValueSet("Server", "Description", "MCServer - The Minecraft server in C++!").c_str();
m_MaxPlayers = a_SettingsIni.GetValueI("Server", "MaxPlayers", 100); m_MaxPlayers = a_SettingsIni.GetValueSetI("Server", "MaxPlayers", 100);
m_bIsHardcore = a_SettingsIni.GetValueSetB("Server", "HardcoreEnabled");
m_PlayerCount = 0; m_PlayerCount = 0;
m_PlayerCountDiff = 0; m_PlayerCountDiff = 0;
@ -320,7 +321,7 @@ void cServer::OnConnectionAccepted(cSocket & a_Socket)
return; return;
} }
LOG("Client \"%s\" connected!", ClientIP.c_str()); LOGD("Client \"%s\" connected!", ClientIP.c_str());
cClientHandle * NewHandle = new cClientHandle(&a_Socket, m_ClientViewDistance); cClientHandle * NewHandle = new cClientHandle(&a_Socket, m_ClientViewDistance);
if (!m_SocketThreads.AddClient(a_Socket, NewHandle)) if (!m_SocketThreads.AddClient(a_Socket, NewHandle))

View File

@ -46,6 +46,9 @@ public: // tolua_export
int GetNumPlayers(void); int GetNumPlayers(void);
void SetMaxPlayers(int a_MaxPlayers) { m_MaxPlayers = a_MaxPlayers; } void SetMaxPlayers(int a_MaxPlayers) { m_MaxPlayers = a_MaxPlayers; }
// Hardcore mode or not:
bool IsHardcore(void) const {return m_bIsHardcore; }
// tolua_end // tolua_end
bool Start(void); bool Start(void);
@ -161,6 +164,7 @@ private:
AString m_Description; AString m_Description;
int m_MaxPlayers; int m_MaxPlayers;
bool m_bIsHardcore;
cTickThread m_TickThread; cTickThread m_TickThread;
cEvent m_RestartEvent; cEvent m_RestartEvent;

View File

@ -56,7 +56,7 @@ cWebAdmin::~cWebAdmin()
{ {
if (m_IsInitialized) if (m_IsInitialized)
{ {
LOG("Stopping WebAdmin..."); LOGD("Stopping WebAdmin...");
} }
} }
@ -87,7 +87,11 @@ bool cWebAdmin::Init(void)
{ {
if (!m_IniFile.ReadFile("webadmin.ini")) if (!m_IniFile.ReadFile("webadmin.ini"))
{ {
return false; LOGWARN("Regenerating webadmin.ini, all settings will be reset");
m_IniFile.AddHeaderComment(" This file controls the webadmin feature of MCServer");
m_IniFile.AddHeaderComment(" Username format: [User:*username*] | Password format: Password=*password*; for example:");
m_IniFile.AddHeaderComment(" [User:admin]");
m_IniFile.AddHeaderComment(" Password=admin");
} }
if (!m_IniFile.GetValueSetB("WebAdmin", "Enabled", true)) if (!m_IniFile.GetValueSetB("WebAdmin", "Enabled", true))
@ -96,16 +100,17 @@ bool cWebAdmin::Init(void)
return true; return true;
} }
LOG("Initialising WebAdmin..."); LOGD("Initialising WebAdmin...");
AString PortsIPv4 = m_IniFile.GetValueSet("WebAdmin", "Port", "8080"); AString PortsIPv4 = m_IniFile.GetValueSet("WebAdmin", "Port", "8080");
AString PortsIPv6 = m_IniFile.GetValueSet("WebAdmin", "PortsIPv6", ""); AString PortsIPv6 = m_IniFile.GetValueSet("WebAdmin", "Port-IPv6", "");
if (!m_HTTPServer.Initialize(PortsIPv4, PortsIPv6)) if (!m_HTTPServer.Initialize(PortsIPv4, PortsIPv6))
{ {
return false; return false;
} }
m_IsInitialized = true; m_IsInitialized = true;
m_IniFile.WriteFile("webadmin.ini");
return true; return true;
} }
@ -121,7 +126,7 @@ bool cWebAdmin::Start(void)
return false; return false;
} }
LOG("Starting WebAdmin..."); LOGD("Starting WebAdmin...");
// Initialize the WebAdmin template script and load the file // Initialize the WebAdmin template script and load the file
m_TemplateScript.Create(); m_TemplateScript.Create();

View File

@ -509,7 +509,7 @@ void cWorld::Start(void)
break; break;
} }
} }
m_bAnimals = IniFile.GetValueB("Monsters", "AnimalsOn", true); m_bAnimals = IniFile.GetValueSetB("Monsters", "AnimalsOn", true);
AString AllMonsters = IniFile.GetValueSet("Monsters", "Types", DefaultMonsters); AString AllMonsters = IniFile.GetValueSet("Monsters", "Types", DefaultMonsters);
AStringVector SplitList = StringSplitAndTrim(AllMonsters, ","); AStringVector SplitList = StringSplitAndTrim(AllMonsters, ",");
for (AStringVector::const_iterator itr = SplitList.begin(), end = SplitList.end(); itr != end; ++itr) for (AStringVector::const_iterator itr = SplitList.begin(), end = SplitList.end(); itr != end; ++itr)