summaryrefslogtreecommitdiffstats
path: root/source/cRoot.cpp
blob: 17471d6d9b0fce7030d0664c9ed32579d7f8a14e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#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"
#include "cThread.h"
#include "cFileFormatUpdater.h"
#include "cGenSettings.h"
#include "cRedstone.h"

#include "../iniFile/iniFile.h"

#include <iostream>





cRoot* cRoot::s_Root = 0;

typedef std::map< AString, cWorld* > WorldMap;
struct cRoot::sRootState
{
	cWorld* pDefaultWorld;
	WorldMap WorldsByName;
};





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 )
	, m_InputThread( 0 )
	, m_pState( new sRootState )
{
	s_Root = this;
}





cRoot::~cRoot()
{
	s_Root = 0;
	delete m_pState;
}





void cRoot::InputThread(void* a_Params)
{
	cRoot& self = *(cRoot*)a_Params;

	while( !(self.m_bStop || self.m_bRestart) )
	{
		std::string Command;
		std::getline(std::cin, Command);
		self.ServerCommand( Command.c_str() );
	}
}





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;

		LoadGlobalSettings();

		cFileFormatUpdater::UpdateFileFormat();

		LOG("Creating new server instance...");
		m_Server = new cServer();

		LOG("Starting server...");
		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 )
			{
				LOG("Creating WebAdmin...");
				m_WebAdmin = new cWebAdmin(8080);
			}
		}

		LOG("Loading settings...");
		m_GroupManager	= new cGroupManager();
		m_RecipeChecker = new cRecipeChecker();
		m_FurnaceRecipe = new cFurnaceRecipe();
		
		LOG("Loading worlds...");
		LoadWorlds();

		LOG("Loading plugin manager...");
		m_PluginManager = new cPluginManager(); // This should be last
		m_PluginManager->ReloadPluginsNow();
		
		LOG("Loading MonsterConfig...");
		m_MonsterConfig = new cMonsterConfig;

		// This sets stuff in motion
		LOG("Starting Authenticator...");
		m_Authenticator.Start();
		
		LOG("Starting server...");
		m_Server->StartListenThread();
		//cHeartBeat* HeartBeat = new cHeartBeat();

		LOG("Starting InputThread...");
		m_InputThread = new cThread( InputThread, this, "cRoot::InputThread" );
		m_InputThread->Start( true );

		LOG("Initialization done, server running now.");
		while( !m_bStop && !m_bRestart ) // These are modified by external threads
		{
			cSleep::MilliSleep( 1000 );
		}

		delete m_InputThread; m_InputThread = 0;

		// Deallocate stuffs
		LOG("Shutting down server...");
		m_Server->Shutdown(); // This waits for threads to stop and d/c clients
		LOG("Stopping authenticator...");
		m_Authenticator.Stop();
		LOG("Stopping plugin manager...");
		delete m_PluginManager; m_PluginManager = 0;  // This should be first
		LOG("Freeing MonsterConfig...");
		delete m_MonsterConfig; m_MonsterConfig = 0;
		LOG("Stopping WebAdmin...");
		delete m_WebAdmin; m_WebAdmin = 0;
		LOG("Unloading recipes...");
		delete m_FurnaceRecipe; m_FurnaceRecipe = 0;
		delete m_RecipeChecker; m_RecipeChecker = 0;
		LOG("Forgetting groups...");
		delete m_GroupManager; m_GroupManager = 0;
		LOG("Unloading worlds...");
		UnloadWorlds();
		LOG("Destroying server...");
		//delete HeartBeat; HeartBeat = 0;
		delete m_Server; m_Server = 0;
		LOG("Shutdown done.");
	}

	delete m_Log; m_Log = 0;
}





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();
	}
}





void cRoot::LoadWorlds()
{
	cIniFile IniFile("settings.ini"); IniFile.ReadFile();

	// First get the default world
	AString DefaultWorldName = IniFile.GetValue("Worlds", "DefaultWorld", "world");
	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;
				}
			}
		}
	}
}





void cRoot::UnloadWorlds()
{
	for( WorldMap::iterator itr = m_pState->WorldsByName.begin(); itr != m_pState->WorldsByName.end(); ++itr )
	{
		delete itr->second;
	}
	m_pState->WorldsByName.clear();
}





cWorld* cRoot::GetWorld()
{
	return GetDefaultWorld();
}





cWorld* cRoot::GetDefaultWorld()
{
	return m_pState->pDefaultWorld;
}





cWorld* cRoot::GetWorld( const AString & a_WorldName )
{
	WorldMap::iterator itr = m_pState->WorldsByName.find( a_WorldName );
	if( itr != m_pState->WorldsByName.end() )
		return itr->second;
	return 0;
}





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 );
	}
}





void cRoot::ServerCommand( const char * a_Cmd )
{
	LOG("Server console command: \"%s\"", a_Cmd );
	m_Server->ServerCommand( a_Cmd );
	if( strcmp(a_Cmd, "stop") == 0 )
	{
		m_bStop = true;
	}
	else if( strcmp( a_Cmd, "restart") == 0 )
	{
		m_bRestart = true;
	}
}





void cRoot::KickUser(int a_ClientID, const AString & a_Reason)
{
	m_Server->KickUser(a_ClientID, a_Reason);
}





void cRoot::AuthenticateUser(int a_ClientID)
{
	m_Server->AuthenticateUser(a_ClientID);
}





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;
}