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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "Root.h"
#include "Server.h"
#include "World.h"
#include "WebAdmin.h"
#include "FurnaceRecipe.h"
#include "GroupManager.h"
#include "CraftingRecipes.h"
#include "Bindings/PluginManager.h"
#include "MonsterConfig.h"
#include "Entities/Player.h"
#include "Blocks/BlockHandler.h"
#include "Items/ItemHandler.h"
#include "Chunk.h"
#include "Protocol/ProtocolRecognizer.h" // for protocol version constants
#include "CommandOutput.h"
#include "DeadlockDetect.h"
#include "OSSupport/Timer.h"
#include "inifile/iniFile.h"
#ifdef _WIN32
#include <psapi.h>
#elif defined(__linux__)
#include <fstream>
#elif defined(__APPLE__)
#include <mach/mach.h>
#endif
cRoot* cRoot::s_Root = NULL;
cRoot::cRoot()
: m_Server( NULL )
, m_MonsterConfig( NULL )
, m_GroupManager( NULL )
, m_CraftingRecipes(NULL)
, m_FurnaceRecipe( NULL )
, m_WebAdmin( NULL )
, m_PluginManager( NULL )
, m_Log( NULL )
, m_bStop( false )
, m_bRestart( false )
, m_InputThread( NULL )
, m_pDefaultWorld( NULL )
{
s_Root = this;
}
cRoot::~cRoot()
{
s_Root = 0;
}
void cRoot::InputThread(void * a_Params)
{
cRoot & self = *(cRoot*)a_Params;
cLogCommandOutputCallback Output;
while (!(self.m_bStop || self.m_bRestart) && std::cin.good())
{
AString Command;
std::getline(std::cin, Command);
if (!Command.empty())
{
self.ExecuteConsoleCommand(TrimString(Command), Output);
}
}
if (!(self.m_bStop || self.m_bRestart))
{
// We have come here because the std::cin has received an EOF and the server is still running; stop the server:
self.m_bStop = true;
}
}
void cRoot::Start(void)
{
cDeadlockDetect dd;
delete m_Log;
m_Log = new cMCLogger();
m_bStop = false;
while (!m_bStop)
{
cTimer Time;
long long mseconds = Time.GetNowTime();
m_bRestart = false;
LoadGlobalSettings();
LOG("Creating new server instance...");
m_Server = new cServer();
LOG("Reading server config...");
cIniFile IniFile;
if (!IniFile.ReadFile("settings.ini"))
{
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)
{
m_PrimaryServerVersion = cProtocolRecognizer::PROTO_VERSION_LATEST;
}
else
{
// Make a note in the log that the primary server version is explicitly set in the ini file
LOGINFO("Primary server version set explicitly to %d.", m_PrimaryServerVersion);
}
LOG("Starting server...");
if (!m_Server->InitServer(IniFile))
{
LOGERROR("Failure starting server, aborting...");
return;
}
m_WebAdmin = new cWebAdmin();
m_WebAdmin->Init();
LOGD("Loading settings...");
m_GroupManager = new cGroupManager();
m_CraftingRecipes = new cCraftingRecipes;
m_FurnaceRecipe = new cFurnaceRecipe();
LOGD("Loading worlds...");
LoadWorlds(IniFile);
LOGD("Loading plugin manager...");
m_PluginManager = new cPluginManager();
m_PluginManager->ReloadPluginsNow(IniFile);
LOGD("Loading MonsterConfig...");
m_MonsterConfig = new cMonsterConfig;
// This sets stuff in motion
LOGD("Starting Authenticator...");
m_Authenticator.Start(IniFile);
LOGD("Starting worlds...");
StartWorlds();
if (IniFile.GetValueSetB("DeadlockDetect", "Enabled", true))
{
LOGD("Starting deadlock detector...");
dd.Start(IniFile.GetValueSetI("DeadlockDetect", "IntervalSec", 20));
}
IniFile.WriteFile("settings.ini");
LOGD("Finalising startup...");
m_Server->Start();
m_WebAdmin->Start();
#if !defined(ANDROID_NDK)
LOGD("Starting InputThread...");
m_InputThread = new cThread( InputThread, this, "cRoot::InputThread" );
m_InputThread->Start( false ); // We should NOT wait? Otherwise we can´t stop the server from other threads than the input thread
#endif
long long finishmseconds = Time.GetNowTime();
finishmseconds -= mseconds;
LOG("Startup complete, took %i ms!", finishmseconds);
while (!m_bStop && !m_bRestart) // These are modified by external threads
{
cSleep::MilliSleep(1000);
}
#if !defined(ANDROID_NDK)
delete m_InputThread; m_InputThread = NULL;
#endif
// Deallocate stuffs
LOG("Shutting down server...");
m_Server->Shutdown();
LOGD("Shutting down deadlock detector...");
dd.Stop();
LOGD("Stopping world threads...");
StopWorlds();
LOGD("Stopping authenticator...");
m_Authenticator.Stop();
LOGD("Freeing MonsterConfig...");
delete m_MonsterConfig; m_MonsterConfig = NULL;
delete m_WebAdmin; m_WebAdmin = NULL;
LOGD("Unloading recipes...");
delete m_FurnaceRecipe; m_FurnaceRecipe = NULL;
delete m_CraftingRecipes; m_CraftingRecipes = NULL;
LOGD("Forgetting groups...");
delete m_GroupManager; m_GroupManager = 0;
LOGD("Unloading worlds...");
UnloadWorlds();
LOGD("Stopping plugin manager...");
delete m_PluginManager; m_PluginManager = NULL;
cItemHandler::Deinit();
cBlockHandler::Deinit();
LOG("Cleaning up...");
//delete HeartBeat; HeartBeat = 0;
delete m_Server; m_Server = 0;
LOG("Shutdown successful!");
}
delete m_Log; m_Log = 0;
}
void cRoot::LoadGlobalSettings()
{
// Nothing needed yet
}
void cRoot::LoadWorlds(cIniFile & IniFile)
{
// First get the default world
AString DefaultWorldName = IniFile.GetValueSet("Worlds", "DefaultWorld", "world");
m_pDefaultWorld = new cWorld( DefaultWorldName.c_str() );
m_WorldsByName[ DefaultWorldName ] = m_pDefaultWorld;
// Then load the other worlds
unsigned int KeyNum = IniFile.FindKey("Worlds");
unsigned int NumWorlds = IniFile.GetNumValues( KeyNum );
if (NumWorlds <= 0)
{
return;
}
bool FoundAdditionalWorlds = false;
for (unsigned int i = 0; i < NumWorlds; i++)
{
AString ValueName = IniFile.GetValueName(KeyNum, i );
if (ValueName.compare("World") != 0)
{
continue;
}
AString WorldName = IniFile.GetValue(KeyNum, i );
if (WorldName.empty())
{
continue;
}
FoundAdditionalWorlds = true;
cWorld* NewWorld = new cWorld( WorldName.c_str() );
m_WorldsByName[ WorldName ] = NewWorld;
} // for i - Worlds
if (!FoundAdditionalWorlds)
{
if (IniFile.GetKeyComment("Worlds", 0) != " World=secondworld")
{
IniFile.AddKeyComment("Worlds", " World=secondworld");
}
}
}
cWorld * cRoot::CreateAndInitializeWorld(const AString & a_WorldName)
{
if (m_WorldsByName[a_WorldName] != NULL)
{
return NULL;
}
cWorld* NewWorld = new cWorld(a_WorldName.c_str());
m_WorldsByName[a_WorldName] = NewWorld;
NewWorld->Start();
NewWorld->InitializeSpawn();
return NewWorld;
}
void cRoot::StartWorlds(void)
{
for (WorldMap::iterator itr = m_WorldsByName.begin(); itr != m_WorldsByName.end(); ++itr)
{
itr->second->Start();
itr->second->InitializeSpawn();
m_PluginManager->CallHookWorldStarted(*itr->second);
}
}
void cRoot::StopWorlds(void)
{
for (WorldMap::iterator itr = m_WorldsByName.begin(); itr != m_WorldsByName.end(); ++itr)
{
itr->second->Stop();
}
}
void cRoot::UnloadWorlds(void)
{
m_pDefaultWorld = NULL;
for( WorldMap::iterator itr = m_WorldsByName.begin(); itr != m_WorldsByName.end(); ++itr )
{
delete itr->second;
}
m_WorldsByName.clear();
}
cWorld* cRoot::GetDefaultWorld()
{
return m_pDefaultWorld;
}
cWorld* cRoot::GetWorld( const AString & a_WorldName )
{
WorldMap::iterator itr = m_WorldsByName.find( a_WorldName );
if( itr != m_WorldsByName.end() )
return itr->second;
return 0;
}
bool cRoot::ForEachWorld(cWorldListCallback & a_Callback)
{
for (WorldMap::iterator itr = m_WorldsByName.begin(), itr2 = itr; itr != m_WorldsByName.end(); itr = itr2)
{
++itr2;
if (a_Callback.Item(itr->second))
{
return false;
}
}
return true;
}
void cRoot::TickCommands(void)
{
// Execute any pending commands:
cCommandQueue PendingCommands;
{
cCSLock Lock(m_CSPendingCommands);
std::swap(PendingCommands, m_PendingCommands);
}
for (cCommandQueue::iterator itr = PendingCommands.begin(), end = PendingCommands.end(); itr != end; ++itr)
{
ExecuteConsoleCommand(itr->m_Command, *(itr->m_Output));
}
}
void cRoot::QueueExecuteConsoleCommand(const AString & a_Cmd, cCommandOutputCallback & a_Output)
{
// Some commands are built-in:
if (a_Cmd == "stop")
{
m_bStop = true;
}
else if (a_Cmd == "restart")
{
m_bRestart = true;
}
// Put the command into a queue (Alleviates FS #363):
cCSLock Lock(m_CSPendingCommands);
m_PendingCommands.push_back(cCommand(a_Cmd, &a_Output));
}
void cRoot::QueueExecuteConsoleCommand(const AString & a_Cmd)
{
// Some commands are built-in:
if (a_Cmd == "stop")
{
m_bStop = true;
}
else if (a_Cmd == "restart")
{
m_bRestart = true;
}
// Put the command into a queue (Alleviates FS #363):
cCSLock Lock(m_CSPendingCommands);
m_PendingCommands.push_back(cCommand(a_Cmd, new cLogCommandDeleteSelfOutputCallback));
}
void cRoot::ExecuteConsoleCommand(const AString & a_Cmd, cCommandOutputCallback & a_Output)
{
// Some commands are built-in:
if (a_Cmd == "stop")
{
m_bStop = true;
}
else if (a_Cmd == "restart")
{
m_bRestart = true;
}
LOG("Executing console command: \"%s\"", a_Cmd.c_str());
m_Server->ExecuteConsoleCommand(a_Cmd, a_Output);
}
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_WorldsByName.begin(); itr != m_WorldsByName.end(); ++itr )
{
res += itr->second->GetNumChunks();
}
return res;
}
void cRoot::SaveAllChunks(void)
{
for (WorldMap::iterator itr = m_WorldsByName.begin(); itr != m_WorldsByName.end(); ++itr)
{
itr->second->QueueSaveAllChunks();
}
}
void cRoot::BroadcastChat(const AString & a_Message)
{
for (WorldMap::iterator itr = m_WorldsByName.begin(), end = m_WorldsByName.end(); itr != end; ++itr)
{
itr->second->BroadcastChat(a_Message);
} // for itr - m_WorldsByName[]
}
bool cRoot::ForEachPlayer(cPlayerListCallback & a_Callback)
{
for (WorldMap::iterator itr = m_WorldsByName.begin(), itr2 = itr; itr != m_WorldsByName.end(); itr = itr2)
{
++itr2;
if (!itr->second->ForEachPlayer(a_Callback))
{
return false;
}
}
return true;
}
bool cRoot::FindAndDoWithPlayer(const AString & a_PlayerName, cPlayerListCallback & a_Callback)
{
class cCallback : public cPlayerListCallback
{
unsigned int BestRating;
unsigned int NameLength;
const AString PlayerName;
cPlayerListCallback & m_Callback;
virtual bool Item (cPlayer * a_pPlayer)
{
unsigned int Rating = RateCompareString (PlayerName, a_pPlayer->GetName());
if (Rating > 0 && Rating >= BestRating)
{
BestMatch = a_pPlayer;
if( Rating > BestRating ) NumMatches = 0;
BestRating = Rating;
++NumMatches;
}
if (Rating == NameLength) // Perfect match
{
return true;
}
return false;
}
public:
cCallback (const AString & a_PlayerName, cPlayerListCallback & a_Callback)
: m_Callback( a_Callback )
, BestMatch( NULL )
, BestRating( 0 )
, NumMatches( 0 )
, NameLength( a_PlayerName.length() )
, PlayerName( a_PlayerName )
{}
cPlayer * BestMatch;
unsigned int NumMatches;
} Callback (a_PlayerName, a_Callback);
ForEachPlayer( Callback );
if (Callback.NumMatches == 1)
{
return a_Callback.Item (Callback.BestMatch);
}
return false;
}
AString cRoot::GetProtocolVersionTextFromInt(int a_ProtocolVersion)
{
return cProtocolRecognizer::GetVersionTextFromInt(a_ProtocolVersion);
}
int cRoot::GetVirtualRAMUsage(void)
{
#ifdef _WIN32
PROCESS_MEMORY_COUNTERS_EX pmc;
if (GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS *)&pmc, sizeof(pmc)))
{
return (int)(pmc.PrivateUsage / 1024);
}
return -1;
#elif defined(__linux__)
// Code adapted from http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process
std::ifstream StatFile("/proc/self/status");
if (!StatFile.good())
{
return -1;
}
while (StatFile.good())
{
AString Line;
std::getline(StatFile, Line);
if (strncmp(Line.c_str(), "VmSize:", 7) == 0)
{
int res = atoi(Line.c_str() + 8);
return (res == 0) ? -1 : res; // If parsing failed, return -1
}
}
return -1;
#elif defined (__APPLE__)
// Code adapted from http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process
struct task_basic_info t_info;
mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
if (KERN_SUCCESS == task_info(
mach_task_self(),
TASK_BASIC_INFO,
(task_info_t)&t_info,
&t_info_count
))
{
return (int)(t_info.virtual_size / 1024);
}
return -1;
#else
LOGINFO("%s: Unknown platform, cannot query memory usage", __FUNCTION__);
return -1;
#endif
}
int cRoot::GetPhysicalRAMUsage(void)
{
#ifdef _WIN32
PROCESS_MEMORY_COUNTERS pmc;
if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
{
return (int)(pmc.WorkingSetSize / 1024);
}
return -1;
#elif defined(__linux__)
// Code adapted from http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process
std::ifstream StatFile("/proc/self/status");
if (!StatFile.good())
{
return -1;
}
while (StatFile.good())
{
AString Line;
std::getline(StatFile, Line);
if (strncmp(Line.c_str(), "VmRSS:", 7) == 0)
{
int res = atoi(Line.c_str() + 8);
return (res == 0) ? -1 : res; // If parsing failed, return -1
}
}
return -1;
#elif defined (__APPLE__)
// Code adapted from http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process
struct task_basic_info t_info;
mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
if (KERN_SUCCESS == task_info(
mach_task_self(),
TASK_BASIC_INFO,
(task_info_t)&t_info,
&t_info_count
))
{
return (int)(t_info.resident_size / 1024);
}
return -1;
#else
LOGINFO("%s: Unknown platform, cannot query memory usage", __FUNCTION__);
return -1;
#endif
}
void cRoot::LogChunkStats(cCommandOutputCallback & a_Output)
{
int SumNumValid = 0;
int SumNumDirty = 0;
int SumNumInLighting = 0;
int SumNumInGenerator = 0;
int SumMem = 0;
for (WorldMap::iterator itr = m_WorldsByName.begin(), end = m_WorldsByName.end(); itr != end; ++itr)
{
cWorld * World = itr->second;
int NumInGenerator = World->GetGeneratorQueueLength();
int NumInSaveQueue = World->GetStorageSaveQueueLength();
int NumInLoadQueue = World->GetStorageLoadQueueLength();
int NumValid = 0;
int NumDirty = 0;
int NumInLighting = 0;
World->GetChunkStats(NumValid, NumDirty, NumInLighting);
a_Output.Out("World %s:", World->GetName().c_str());
a_Output.Out(" Num loaded chunks: %d", NumValid);
a_Output.Out(" Num dirty chunks: %d", NumDirty);
a_Output.Out(" Num chunks in lighting queue: %d", NumInLighting);
a_Output.Out(" Num chunks in generator queue: %d", NumInGenerator);
a_Output.Out(" Num chunks in storage load queue: %d", NumInLoadQueue);
a_Output.Out(" Num chunks in storage save queue: %d", NumInSaveQueue);
int Mem = NumValid * sizeof(cChunk);
a_Output.Out(" Memory used by chunks: %d KiB (%d MiB)", (Mem + 1023) / 1024, (Mem + 1024 * 1024 - 1) / (1024 * 1024));
a_Output.Out(" Per-chunk memory size breakdown:");
a_Output.Out(" block types: %6d bytes (%3d KiB)", sizeof(cChunkDef::BlockTypes), (sizeof(cChunkDef::BlockTypes) + 1023) / 1024);
a_Output.Out(" block metadata: %6d bytes (%3d KiB)", sizeof(cChunkDef::BlockNibbles), (sizeof(cChunkDef::BlockNibbles) + 1023) / 1024);
a_Output.Out(" block lighting: %6d bytes (%3d KiB)", 2 * sizeof(cChunkDef::BlockNibbles), (2 * sizeof(cChunkDef::BlockNibbles) + 1023) / 1024);
a_Output.Out(" heightmap: %6d bytes (%3d KiB)", sizeof(cChunkDef::HeightMap), (sizeof(cChunkDef::HeightMap) + 1023) / 1024);
a_Output.Out(" biomemap: %6d bytes (%3d KiB)", sizeof(cChunkDef::BiomeMap), (sizeof(cChunkDef::BiomeMap) + 1023) / 1024);
int Rest = sizeof(cChunk) - sizeof(cChunkDef::BlockTypes) - 3 * sizeof(cChunkDef::BlockNibbles) - sizeof(cChunkDef::HeightMap) - sizeof(cChunkDef::BiomeMap);
a_Output.Out(" other: %6d bytes (%3d KiB)", Rest, (Rest + 1023) / 1024);
SumNumValid += NumValid;
SumNumDirty += NumDirty;
SumNumInLighting += NumInLighting;
SumNumInGenerator += NumInGenerator;
SumMem += Mem;
}
a_Output.Out("Totals:");
a_Output.Out(" Num loaded chunks: %d", SumNumValid);
a_Output.Out(" Num dirty chunks: %d", SumNumDirty);
a_Output.Out(" Num chunks in lighting queue: %d", SumNumInLighting);
a_Output.Out(" Num chunks in generator queue: %d", SumNumInGenerator);
a_Output.Out(" Memory used by chunks: %d KiB (%d MiB)", (SumMem + 1023) / 1024, (SumMem + 1024 * 1024 - 1) / (1024 * 1024));
}
int cRoot::GetFurnaceFuelBurnTime(const cItem & a_Fuel)
{
cFurnaceRecipe * FR = Get()->GetFurnaceRecipe();
return FR->GetBurnTime(a_Fuel);
}
|