Skip to content

Commit

Permalink
Autogeneration of settings.ini and webadmin.ini
Browse files Browse the repository at this point in the history
Fixes issue #75
  • Loading branch information
tigerw committed Nov 4, 2013
1 parent d47a8ea commit e832736
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 65 deletions.
36 changes: 8 additions & 28 deletions source/Authenticator.cpp
Expand Up @@ -46,28 +46,9 @@ cAuthenticator::~cAuthenticator()
/// Read custom values from INI
void cAuthenticator::ReadINI(cIniFile & IniFile)
{
m_Server = IniFile.GetValue("Authentication", "Server");
m_Address = IniFile.GetValue("Authentication", "Address");
m_ShouldAuthenticate = IniFile.GetValueB("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);
}
m_Server = IniFile.GetValueSet("Authentication", "Server", DEFAULT_AUTH_SERVER);
m_Address = IniFile.GetValueSet("Authentication", "Address", DEFAULT_AUTH_ADDRESS);
m_ShouldAuthenticate = IniFile.GetValueSetB("Authentication", "Authenticate", true);
}


Expand Down Expand Up @@ -199,7 +180,7 @@ bool cAuthenticator::AuthFromAddress(const AString & a_Server, const AString & a
}
else if (code == 200)
{
LOGINFO("Got 200 OK :D");
LOGD("cAuthenticator: Received status 200 OK! :D");
bOK = true;
}
}
Expand Down Expand Up @@ -268,17 +249,16 @@ bool cAuthenticator::AuthFromAddress(const AString & a_Server, const AString & a

std::string Result;
ss >> Result;
LOGINFO("Got result: %s", Result.c_str());
//if (Result.compare("3") == 0) // FIXME: Quick and dirty hack to support auth
//Lapayo: Wtf 3?
LOGD("cAuthenticator: Authentication result was %s", Result.c_str());

if (Result.compare("YES") == 0) //Works well
{
LOGINFO("Result was \"YES\", so player is authenticated!");
LOGINFO("Authentication result \"YES\", player authentication success!");
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;
}

Expand Down
4 changes: 2 additions & 2 deletions source/ClientHandle.cpp
Expand Up @@ -205,7 +205,7 @@ void cClientHandle::Kick(const AString & a_Reason)
{
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);
}
Expand Down Expand Up @@ -2196,7 +2196,7 @@ void cClientHandle::SocketClosed(void)
{
// 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();
}

Expand Down
4 changes: 2 additions & 2 deletions source/LeakFinder.cpp
Expand Up @@ -108,8 +108,8 @@
#include "LeakFinder.h"

// Currently only tested with MS VC++ 5 to 10
#if (_MSC_VER < 1100) || (_MSC_VER > 1700)
#error Only MS VC++ 5/6/7/7.1/8/9 supported. Check if the '_CrtMemBlockHeader' has not changed with this compiler!
#if (_MSC_VER < 1100) || (_MSC_VER > 1800)
#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


Expand Down
6 changes: 3 additions & 3 deletions source/MobSpawner.cpp
Expand Up @@ -71,10 +71,10 @@ cMonster::eType cMobSpawner::ChooseMobType(EMCSBiome a_Biome)
addIfAllowed(cMonster::mtZombiePigman, 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);
}*/
}
else
{
addIfAllowed(cMonster::mtBat, allowedMobs);
Expand Down Expand Up @@ -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]) &&
(m_Random.NextInt(20,a_Biome) == 0);
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;
}
}
Expand Down
37 changes: 22 additions & 15 deletions source/PluginManager.cpp
Expand Up @@ -94,6 +94,17 @@ void cPluginManager::FindPlugins(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 --");
m_bReloadPlugins = false;
Expand All @@ -102,26 +113,22 @@ void cPluginManager::ReloadPluginsNow(void)
FindPlugins();

cServer::BindBuiltInConsoleCommands();

cIniFile IniFile;
if (!IniFile.ReadFile("settings.ini"))

unsigned int KeyNum = a_SettingsIni.FindKey("Plugins");
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");
}

unsigned int KeyNum = IniFile.FindKey("Plugins");
unsigned int NumPlugins = IniFile.GetNumValues(KeyNum);
if (NumPlugins > 0)
else if (NumPlugins > 0)
{
for(unsigned int i = 0; i < NumPlugins; i++)
{
AString ValueName = IniFile.GetValueName(KeyNum, i );
if (
(ValueName.compare("NewPlugin") == 0) ||
(ValueName.compare("Plugin") == 0)
)
AString ValueName = a_SettingsIni.GetValueName(KeyNum, i);
if (ValueName.compare("Plugin") == 0)
{
AString PluginFile = IniFile.GetValue(KeyNum, i);
AString PluginFile = a_SettingsIni.GetValue(KeyNum, i);
if (!PluginFile.empty())
{
if (m_Plugins.find(PluginFile) != m_Plugins.end())
Expand All @@ -137,7 +144,7 @@ void cPluginManager::ReloadPluginsNow(void)
{
LOG("-- No Plugins Loaded --");
}
else if ((GetNumPlugins() > 1) || (GetNumPlugins() == 0))
else if (GetNumPlugins() > 1)
{
LOG("-- Loaded %i Plugins --", GetNumPlugins());
}
Expand Down
7 changes: 7 additions & 0 deletions source/PluginManager.h
Expand Up @@ -271,7 +271,14 @@ class cPluginManager // tolua_export
cPluginManager();
~cPluginManager();

/// Reloads all plugins, defaulting to settings.ini for settings location
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);

/// Adds the plugin into the internal list of plugins and initializes it. If initialization fails, the plugin is removed again.
Expand Down
17 changes: 14 additions & 3 deletions source/Root.cpp
Expand Up @@ -119,8 +119,12 @@ void cRoot::Start(void)
cIniFile IniFile;
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);
if (m_PrimaryServerVersion == 0)
{
Expand All @@ -129,7 +133,7 @@ void cRoot::Start(void)
else
{
// 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...");
Expand All @@ -152,7 +156,7 @@ void cRoot::Start(void)

LOGD("Loading plugin manager...");
m_PluginManager = new cPluginManager();
m_PluginManager->ReloadPluginsNow();
m_PluginManager->ReloadPluginsNow(IniFile);

LOGD("Loading MonsterConfig...");
m_MonsterConfig = new cMonsterConfig;
Expand Down Expand Up @@ -261,6 +265,7 @@ void cRoot::LoadWorlds(cIniFile & IniFile)
return;
}

bool FoundAdditionalWorlds = false;
for (unsigned int i = 0; i < NumWorlds; i++)
{
AString ValueName = IniFile.GetValueName(KeyNum, i );
Expand All @@ -273,9 +278,15 @@ void cRoot::LoadWorlds(cIniFile & IniFile)
{
continue;
}
FoundAdditionalWorlds = true;
cWorld* NewWorld = new cWorld( WorldName.c_str() );
m_WorldsByName[ WorldName ] = NewWorld;
} // for i - Worlds

if (!FoundAdditionalWorlds)
{
IniFile.AddKeyComment("Worlds", " World=secondworld");
}
}


Expand Down
9 changes: 5 additions & 4 deletions source/Server.cpp
Expand Up @@ -195,8 +195,9 @@ void cServer::PlayerDestroying(const cPlayer * a_Player)

bool cServer::InitServer(cIniFile & a_SettingsIni)
{
m_Description = a_SettingsIni.GetValue ("Server", "Description", "MCServer! - In C++!").c_str();
m_MaxPlayers = a_SettingsIni.GetValueI("Server", "MaxPlayers", 100);
m_Description = a_SettingsIni.GetValueSet("Server", "Description", "MCServer - The Minecraft server in C++!").c_str();
m_MaxPlayers = a_SettingsIni.GetValueSetI("Server", "MaxPlayers", 100);
m_bIsHardcore = a_SettingsIni.GetValueSetB("Server", "HardcoreEnabled");
m_PlayerCount = 0;
m_PlayerCountDiff = 0;

Expand Down Expand Up @@ -320,7 +321,7 @@ void cServer::OnConnectionAccepted(cSocket & a_Socket)
return;
}

LOG("Client \"%s\" connected!", ClientIP.c_str());
LOGD("Client \"%s\" connected!", ClientIP.c_str());

cClientHandle * NewHandle = new cClientHandle(&a_Socket, m_ClientViewDistance);
if (!m_SocketThreads.AddClient(a_Socket, NewHandle))
Expand Down Expand Up @@ -507,7 +508,7 @@ void cServer::BindBuiltInConsoleCommands(void)
PlgMgr->BindConsoleCommand("chunkstats", NULL, " - Displays detailed chunk memory statistics");
#if defined(_MSC_VER) && defined(_DEBUG) && defined(ENABLE_LEAK_FINDER)
PlgMgr->BindConsoleCommand("dumpmem", NULL, " - Dumps all used memory blocks together with their callstacks into memdump.xml");
#endif
#endif
}


Expand Down
4 changes: 4 additions & 0 deletions source/Server.h
Expand Up @@ -46,6 +46,9 @@ class cServer // tolua_export
int GetNumPlayers(void);
void SetMaxPlayers(int a_MaxPlayers) { m_MaxPlayers = a_MaxPlayers; }

// Hardcore mode or not:
bool IsHardcore(void) const {return m_bIsHardcore; }

// tolua_end

bool Start(void);
Expand Down Expand Up @@ -161,6 +164,7 @@ class cServer // tolua_export

AString m_Description;
int m_MaxPlayers;
bool m_bIsHardcore;

cTickThread m_TickThread;
cEvent m_RestartEvent;
Expand Down
15 changes: 10 additions & 5 deletions source/WebAdmin.cpp
Expand Up @@ -56,7 +56,7 @@ cWebAdmin::~cWebAdmin()
{
if (m_IsInitialized)
{
LOG("Stopping WebAdmin...");
LOGD("Stopping WebAdmin...");
}
}

Expand Down Expand Up @@ -87,7 +87,11 @@ bool cWebAdmin::Init(void)
{
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))
Expand All @@ -96,16 +100,17 @@ bool cWebAdmin::Init(void)
return true;
}

LOG("Initialising WebAdmin...");
LOGD("Initialising WebAdmin...");

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))
{
return false;
}
m_IsInitialized = true;
m_IniFile.WriteFile("webadmin.ini");
return true;
}

Expand All @@ -121,7 +126,7 @@ bool cWebAdmin::Start(void)
return false;
}

LOG("Starting WebAdmin...");
LOGD("Starting WebAdmin...");

// Initialize the WebAdmin template script and load the file
m_TemplateScript.Create();
Expand Down
6 changes: 3 additions & 3 deletions source/World.cpp
Expand Up @@ -509,7 +509,7 @@ void cWorld::Start(void)
break;
}
}
m_bAnimals = IniFile.GetValueB("Monsters", "AnimalsOn", true);
m_bAnimals = IniFile.GetValueSetB("Monsters", "AnimalsOn", true);
AString AllMonsters = IniFile.GetValueSet("Monsters", "Types", DefaultMonsters);
AStringVector SplitList = StringSplitAndTrim(AllMonsters, ",");
for (AStringVector::const_iterator itr = SplitList.begin(), end = SplitList.end(); itr != end; ++itr)
Expand Down Expand Up @@ -784,8 +784,8 @@ void cWorld::TickMobs(float a_Dt)
SpawnMobFinalize(*itr2);
}
}
} // for i - AllFamilies[]
} // if (Spawning enabled)
} // for i - AllFamilies[]
} // if (Spawning enabled)

// move close mobs
cMobProximityCounter::sIterablePair allCloseEnoughToMoveMobs = MobCensus.GetProximityCounter().getMobWithinThosesDistances(-1, 64 * 16);// MG TODO : deal with this magic number (the 16 is the size of a block)
Expand Down

0 comments on commit e832736

Please sign in to comment.