2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
#include "cRoot.h"
|
|
|
|
#include "cServer.h"
|
|
|
|
#include "cWorld.h"
|
|
|
|
#include "cWebAdmin.h"
|
|
|
|
#include "cFurnaceRecipe.h"
|
|
|
|
#include "cGroupManager.h"
|
|
|
|
#include "cRecipeChecker.h"
|
|
|
|
#include "cPluginManager.h"
|
|
|
|
#include "cMonsterConfig.h"
|
|
|
|
#include "cSleep.h"
|
2011-10-26 09:07:39 -04:00
|
|
|
#include "cThread.h"
|
2011-10-31 17:30:14 -04:00
|
|
|
#include "cFileFormatUpdater.h"
|
2012-02-22 10:35:10 -05:00
|
|
|
#include "cGenSettings.h"
|
|
|
|
#include "cRedstone.h"
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
#include "../iniFile/iniFile.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2012-02-01 07:46:44 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
cRoot* cRoot::s_Root = 0;
|
|
|
|
|
2012-03-07 08:36:30 -05:00
|
|
|
typedef std::map< AString, cWorld* > WorldMap;
|
2011-11-01 17:57:08 -04:00
|
|
|
struct cRoot::sRootState
|
|
|
|
{
|
|
|
|
cWorld* pDefaultWorld;
|
|
|
|
WorldMap WorldsByName;
|
|
|
|
};
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cRoot::cRoot()
|
|
|
|
: m_Server( 0 )
|
|
|
|
, m_MonsterConfig( 0 )
|
|
|
|
, m_GroupManager( 0 )
|
|
|
|
, m_RecipeChecker( 0 )
|
|
|
|
, m_FurnaceRecipe( 0 )
|
|
|
|
, m_WebAdmin( 0 )
|
|
|
|
, m_PluginManager( 0 )
|
|
|
|
, m_Log( 0 )
|
|
|
|
, m_bStop( false )
|
|
|
|
, m_bRestart( false )
|
2011-10-26 09:07:39 -04:00
|
|
|
, m_InputThread( 0 )
|
2011-11-01 17:57:08 -04:00
|
|
|
, m_pState( new sRootState )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
s_Root = this;
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cRoot::~cRoot()
|
|
|
|
{
|
|
|
|
s_Root = 0;
|
2011-11-01 17:57:08 -04:00
|
|
|
delete m_pState;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-26 09:07:39 -04:00
|
|
|
void cRoot::InputThread(void* a_Params)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2011-10-26 09:07:39 -04:00
|
|
|
cRoot& self = *(cRoot*)a_Params;
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2011-10-26 09:07:39 -04:00
|
|
|
while( !(self.m_bStop || self.m_bRestart) )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
std::string Command;
|
|
|
|
std::getline(std::cin, Command);
|
2011-10-26 09:07:39 -04:00
|
|
|
self.ServerCommand( Command.c_str() );
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cRoot::Start()
|
|
|
|
{
|
|
|
|
if( m_Log ) delete m_Log, m_Log = 0;
|
|
|
|
m_Log = new cMCLogger();
|
|
|
|
|
|
|
|
m_bStop = false;
|
|
|
|
while(!m_bStop)
|
|
|
|
{
|
|
|
|
m_bRestart = false;
|
|
|
|
|
2012-02-22 10:35:10 -05:00
|
|
|
LoadGlobalSettings();
|
|
|
|
|
2011-10-31 17:30:14 -04:00
|
|
|
cFileFormatUpdater::UpdateFileFormat();
|
|
|
|
|
2012-02-17 06:40:14 -05:00
|
|
|
LOG("Creating new server instance...");
|
2011-10-03 14:41:19 -04:00
|
|
|
m_Server = new cServer();
|
|
|
|
|
2012-02-17 06:40:14 -05:00
|
|
|
LOG("Starting server...");
|
2011-10-03 14:41:19 -04:00
|
|
|
cIniFile IniFile("settings.ini"); IniFile.ReadFile();
|
|
|
|
int Port = IniFile.GetValueI("Server", "Port", 25565 );
|
|
|
|
if(!m_Server->InitServer( Port ))
|
|
|
|
{
|
|
|
|
LOG("Failed to start server, shutting down.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cIniFile WebIniFile("webadmin.ini");
|
|
|
|
if( WebIniFile.ReadFile() )
|
|
|
|
{
|
|
|
|
if( WebIniFile.GetValueB("WebAdmin", "Enabled", false ) == true )
|
|
|
|
{
|
2012-02-17 06:40:14 -05:00
|
|
|
LOG("Creating WebAdmin...");
|
2011-10-03 14:41:19 -04:00
|
|
|
m_WebAdmin = new cWebAdmin(8080);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:40:14 -05:00
|
|
|
LOG("Loading settings...");
|
2011-10-03 14:41:19 -04:00
|
|
|
m_GroupManager = new cGroupManager();
|
|
|
|
m_RecipeChecker = new cRecipeChecker();
|
|
|
|
m_FurnaceRecipe = new cFurnaceRecipe();
|
2012-02-17 06:40:14 -05:00
|
|
|
|
|
|
|
LOG("Loading worlds...");
|
2011-11-01 17:57:08 -04:00
|
|
|
LoadWorlds();
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2012-02-17 06:40:14 -05:00
|
|
|
LOG("Loading plugin manager...");
|
2011-10-03 14:41:19 -04:00
|
|
|
m_PluginManager = new cPluginManager(); // This should be last
|
|
|
|
m_PluginManager->ReloadPluginsNow();
|
2012-02-17 06:40:14 -05:00
|
|
|
|
|
|
|
LOG("Loading MonsterConfig...");
|
|
|
|
m_MonsterConfig = new cMonsterConfig;
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
// This sets stuff in motion
|
2012-02-17 06:40:14 -05:00
|
|
|
LOG("Starting Authenticator...");
|
2012-02-01 17:38:03 -05:00
|
|
|
m_Authenticator.Start();
|
2012-02-17 06:40:14 -05:00
|
|
|
|
|
|
|
LOG("Starting server...");
|
2011-10-03 14:41:19 -04:00
|
|
|
m_Server->StartListenThread();
|
|
|
|
//cHeartBeat* HeartBeat = new cHeartBeat();
|
|
|
|
|
2012-02-17 06:40:14 -05:00
|
|
|
LOG("Starting InputThread...");
|
2011-12-23 09:26:29 -05:00
|
|
|
m_InputThread = new cThread( InputThread, this, "cRoot::InputThread" );
|
2011-10-26 09:07:39 -04:00
|
|
|
m_InputThread->Start( true );
|
|
|
|
|
2012-02-17 06:40:14 -05:00
|
|
|
LOG("Initialization done, server running now.");
|
2011-10-03 14:41:19 -04:00
|
|
|
while( !m_bStop && !m_bRestart ) // These are modified by external threads
|
|
|
|
{
|
|
|
|
cSleep::MilliSleep( 1000 );
|
|
|
|
}
|
|
|
|
|
2011-10-26 09:07:39 -04:00
|
|
|
delete m_InputThread; m_InputThread = 0;
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
// Deallocate stuffs
|
2012-02-17 06:40:14 -05:00
|
|
|
LOG("Shutting down server...");
|
2011-10-03 14:41:19 -04:00
|
|
|
m_Server->Shutdown(); // This waits for threads to stop and d/c clients
|
2012-02-17 06:40:14 -05:00
|
|
|
LOG("Stopping authenticator...");
|
2012-02-15 16:47:21 -05:00
|
|
|
m_Authenticator.Stop();
|
2012-02-17 06:40:14 -05:00
|
|
|
LOG("Stopping plugin manager...");
|
2011-10-03 14:41:19 -04:00
|
|
|
delete m_PluginManager; m_PluginManager = 0; // This should be first
|
2012-02-17 06:40:14 -05:00
|
|
|
LOG("Freeing MonsterConfig...");
|
2011-10-03 14:41:19 -04:00
|
|
|
delete m_MonsterConfig; m_MonsterConfig = 0;
|
2012-02-17 06:40:14 -05:00
|
|
|
LOG("Stopping WebAdmin...");
|
|
|
|
delete m_WebAdmin; m_WebAdmin = 0;
|
|
|
|
LOG("Unloading recipes...");
|
2011-10-03 14:41:19 -04:00
|
|
|
delete m_FurnaceRecipe; m_FurnaceRecipe = 0;
|
|
|
|
delete m_RecipeChecker; m_RecipeChecker = 0;
|
2012-02-17 06:40:14 -05:00
|
|
|
LOG("Forgetting groups...");
|
2011-10-03 14:41:19 -04:00
|
|
|
delete m_GroupManager; m_GroupManager = 0;
|
2012-02-17 06:40:14 -05:00
|
|
|
LOG("Unloading worlds...");
|
2011-11-01 17:57:08 -04:00
|
|
|
UnloadWorlds();
|
2012-02-17 06:40:14 -05:00
|
|
|
LOG("Destroying server...");
|
2011-10-03 14:41:19 -04:00
|
|
|
//delete HeartBeat; HeartBeat = 0;
|
|
|
|
delete m_Server; m_Server = 0;
|
2012-02-17 06:40:14 -05:00
|
|
|
LOG("Shutdown done.");
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
delete m_Log; m_Log = 0;
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-22 10:35:10 -05:00
|
|
|
void cRoot::LoadGlobalSettings()
|
|
|
|
{
|
|
|
|
cIniFile IniFile("settings.ini");
|
|
|
|
if( IniFile.ReadFile() )
|
|
|
|
{
|
|
|
|
cRedstone::s_UseRedstone = IniFile.GetValueB("Redstone", "SimulateRedstone", true );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// I think this should be removed? I can't believe anybody is using it anyway
|
|
|
|
cIniFile GenSettings("terrain.ini");
|
|
|
|
if( GenSettings.ReadFile() )
|
|
|
|
{
|
|
|
|
#define READ_INI_TERRAIN_VAL( var, type ) cGenSettings::var = (type)GenSettings.GetValueF("Terrain", #var, cGenSettings::var )
|
|
|
|
READ_INI_TERRAIN_VAL( HeightFreq1, float );
|
|
|
|
READ_INI_TERRAIN_VAL( HeightFreq2, float );
|
|
|
|
READ_INI_TERRAIN_VAL( HeightFreq3, float );
|
|
|
|
READ_INI_TERRAIN_VAL( HeightAmp1, float );
|
|
|
|
READ_INI_TERRAIN_VAL( HeightAmp2, float );
|
|
|
|
READ_INI_TERRAIN_VAL( HeightAmp3, float );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#define SET_INI_TERRAIN_VAL( var ) GenSettings.SetValueF("Terrain", #var, cGenSettings::var )
|
|
|
|
SET_INI_TERRAIN_VAL( HeightFreq1 );
|
|
|
|
SET_INI_TERRAIN_VAL( HeightFreq2 );
|
|
|
|
SET_INI_TERRAIN_VAL( HeightFreq3 );
|
|
|
|
SET_INI_TERRAIN_VAL( HeightAmp1 );
|
|
|
|
SET_INI_TERRAIN_VAL( HeightAmp2 );
|
|
|
|
SET_INI_TERRAIN_VAL( HeightAmp3 );
|
|
|
|
GenSettings.WriteFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-11-01 17:57:08 -04:00
|
|
|
void cRoot::LoadWorlds()
|
|
|
|
{
|
|
|
|
cIniFile IniFile("settings.ini"); IniFile.ReadFile();
|
|
|
|
|
|
|
|
// First get the default world
|
2012-02-08 07:36:54 -05:00
|
|
|
AString DefaultWorldName = IniFile.GetValue("Worlds", "DefaultWorld", "world");
|
2011-11-01 17:57:08 -04:00
|
|
|
m_pState->pDefaultWorld = new cWorld( DefaultWorldName.c_str() );
|
|
|
|
m_pState->pDefaultWorld->InitializeSpawn();
|
|
|
|
m_pState->WorldsByName[ DefaultWorldName ] = m_pState->pDefaultWorld;
|
|
|
|
|
|
|
|
// Then load the other worlds
|
|
|
|
unsigned int KeyNum = IniFile.FindKey("Worlds");
|
|
|
|
unsigned int NumWorlds = IniFile.GetNumValues( KeyNum );
|
|
|
|
if( NumWorlds > 0 )
|
|
|
|
{
|
|
|
|
for(unsigned int i = 0; i < NumWorlds; i++)
|
|
|
|
{
|
|
|
|
std::string ValueName = IniFile.GetValueName(KeyNum, i );
|
|
|
|
if( ValueName.compare("World") == 0 )
|
|
|
|
{
|
|
|
|
std::string WorldName = IniFile.GetValue(KeyNum, i );
|
|
|
|
if( WorldName.size() > 0 )
|
|
|
|
{
|
|
|
|
cWorld* NewWorld = new cWorld( WorldName.c_str() );
|
|
|
|
NewWorld->InitializeSpawn();
|
|
|
|
m_pState->WorldsByName[ WorldName ] = NewWorld;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-11-01 17:57:08 -04:00
|
|
|
void cRoot::UnloadWorlds()
|
|
|
|
{
|
|
|
|
for( WorldMap::iterator itr = m_pState->WorldsByName.begin(); itr != m_pState->WorldsByName.end(); ++itr )
|
|
|
|
{
|
|
|
|
delete itr->second;
|
|
|
|
}
|
|
|
|
m_pState->WorldsByName.clear();
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-11-01 17:57:08 -04:00
|
|
|
cWorld* cRoot::GetWorld()
|
|
|
|
{
|
|
|
|
return GetDefaultWorld();
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-11-01 17:57:08 -04:00
|
|
|
cWorld* cRoot::GetDefaultWorld()
|
|
|
|
{
|
|
|
|
return m_pState->pDefaultWorld;
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-07 08:36:30 -05:00
|
|
|
cWorld* cRoot::GetWorld( const AString & a_WorldName )
|
2011-11-01 17:57:08 -04:00
|
|
|
{
|
|
|
|
WorldMap::iterator itr = m_pState->WorldsByName.find( a_WorldName );
|
|
|
|
if( itr != m_pState->WorldsByName.end() )
|
|
|
|
return itr->second;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-11-01 17:57:08 -04:00
|
|
|
void cRoot::TickWorlds( float a_Dt )
|
|
|
|
{
|
|
|
|
for( WorldMap::iterator itr = m_pState->WorldsByName.begin(); itr != m_pState->WorldsByName.end(); ++itr )
|
|
|
|
{
|
|
|
|
itr->second->Tick( a_Dt );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-17 06:40:14 -05:00
|
|
|
void cRoot::ServerCommand( const char * a_Cmd )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-17 06:40:14 -05:00
|
|
|
LOG("Server console command: \"%s\"", a_Cmd );
|
2011-10-03 14:41:19 -04:00
|
|
|
m_Server->ServerCommand( a_Cmd );
|
|
|
|
if( strcmp(a_Cmd, "stop") == 0 )
|
|
|
|
{
|
|
|
|
m_bStop = true;
|
|
|
|
}
|
|
|
|
else if( strcmp( a_Cmd, "restart") == 0 )
|
|
|
|
{
|
|
|
|
m_bRestart = true;
|
|
|
|
}
|
|
|
|
}
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-09 08:42:28 -05:00
|
|
|
void cRoot::KickUser(int a_ClientID, const AString & a_Reason)
|
2012-02-01 17:38:03 -05:00
|
|
|
{
|
2012-03-09 08:42:28 -05:00
|
|
|
m_Server->KickUser(a_ClientID, a_Reason);
|
2012-02-01 17:38:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-09 08:42:28 -05:00
|
|
|
void cRoot::AuthenticateUser(int a_ClientID)
|
2012-02-01 17:38:03 -05:00
|
|
|
{
|
2012-03-09 08:42:28 -05:00
|
|
|
m_Server->AuthenticateUser(a_ClientID);
|
2012-02-01 17:38:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-08 13:57:04 -05:00
|
|
|
|
|
|
|
int cRoot::GetTotalChunkCount(void)
|
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
for ( WorldMap::iterator itr = m_pState->WorldsByName.begin(); itr != m_pState->WorldsByName.end(); ++itr )
|
|
|
|
{
|
|
|
|
res += itr->second->GetNumChunks();
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-10 17:27:24 -05:00
|
|
|
|
|
|
|
void cRoot::SaveAllChunks(void)
|
|
|
|
{
|
|
|
|
for (WorldMap::iterator itr = m_pState->WorldsByName.begin(); itr != m_pState->WorldsByName.end(); ++itr)
|
|
|
|
{
|
|
|
|
itr->second->SaveAllChunks();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cRoot::ForEachPlayer(cPlayerListCallback & a_Callback)
|
|
|
|
{
|
|
|
|
for (WorldMap::iterator itr = m_pState->WorldsByName.begin(); itr != m_pState->WorldsByName.end(); ++itr)
|
|
|
|
{
|
|
|
|
if (!itr->second->ForEachPlayer(a_Callback))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|