Cuberite
A lightweight, fast and extensible game server for Minecraft
Root.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "Defines.h"
5 #include "FunctionRef.h"
6 #include "HTTP/HTTPServer.h"
8 #include "Protocol/MojangAPI.h"
9 #include "RankManager.h"
10 #include "ChunkDef.h"
11 
12 
13 
14 
15 // fwd:
16 class cItem;
17 class cMonsterConfig;
18 class cBrewingRecipes;
19 class cCraftingRecipes;
20 class cRecipeMapper;
21 class cFurnaceRecipe;
22 class cWebAdmin;
23 class cPluginManager;
24 class cServer;
25 class cWorld;
26 class cPlayer;
28 class cCompositeChat;
30 class cDeadlockDetect;
31 class cUUID;
32 class BlockTypePalette;
33 class ProtocolPalettes;
34 
37 
38 namespace Json
39 {
40  class Value;
41 }
42 
43 
44 
45 
46 
48 // tolua_begin
49 class cRoot
50 {
51 public:
52  static cRoot * Get() { return s_Root; }
53  // tolua_end
54 
57 
58  cRoot(void);
59  ~cRoot();
60 
62  bool Run(cSettingsRepositoryInterface & a_OverridesRepo);
63 
65  static void Stop();
66 
68  static void Restart();
69 
70  // tolua_begin
71  cServer * GetServer(void) { return m_Server; }
72  cWorld * GetDefaultWorld(void);
73 
75  cWorld * GetWorld(const AString & a_WorldName);
76 
78  int GetServerUpTime(void)
79  {
80  return static_cast<int>(std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - m_StartTime).count());
81  }
82  // tolua_end
83 
85  bool ForEachWorld(cWorldListCallback a_Callback); // >> Exported in ManualBindings <<
86 
88  void LogChunkStats(cCommandOutputCallback & a_Output);
89 
91 
92  cCraftingRecipes * GetCraftingRecipes(void) { return m_CraftingRecipes; } // tolua_export
93  cRecipeMapper * GetRecipeMapper(void) { return m_RecipeMapper.get(); }
94  cFurnaceRecipe * GetFurnaceRecipe (void) { return m_FurnaceRecipe; } // Exported in ManualBindings.cpp with quite a different signature
95  cBrewingRecipes * GetBrewingRecipes (void) { return m_BrewingRecipes.get(); } // Exported in ManualBindings.cpp
96 
98  // BlockTypeRegistry & GetBlockTypeRegistry() { return m_BlockTypeRegistry; }
99 
101  static int GetFurnaceFuelBurnTime(const cItem & a_Fuel); // tolua_export
102 
106 
108  std::chrono::steady_clock::time_point m_StartTime;
109 
110  cWebAdmin * GetWebAdmin (void) { return m_WebAdmin; } // tolua_export
111  cPluginManager * GetPluginManager (void) { return m_PluginManager; } // tolua_export
113  cMojangAPI & GetMojangAPI (void) { return *m_MojangAPI; }
114  cRankManager * GetRankManager (void) { return m_RankManager.get(); }
115 
121  void QueueExecuteConsoleCommand(const AString & a_Cmd, cCommandOutputCallback & a_Output);
122 
128  void QueueExecuteConsoleCommand(const AString & a_Cmd); // tolua_export
129 
131  void KickUser(int a_ClientID, const AString & a_Reason);
132 
134  size_t GetTotalChunkCount(void); // tolua_export
135 
137  void SaveAllChunks(void); // tolua_export
138 
140  void SaveAllChunksNow(void);
141 
143  void SetSavingEnabled(bool a_SavingEnabled); // tolua_export
144 
146  bool ForEachPlayer(cPlayerListCallback a_Callback); // >> EXPORTED IN MANUALBINDINGS <<
147 
149  bool FindAndDoWithPlayer(const AString & a_PlayerName, cPlayerListCallback a_Callback); // >> EXPORTED IN MANUALBINDINGS <<
150 
152  bool DoWithPlayerByUUID(const cUUID & a_PlayerUUID, cPlayerListCallback a_Callback); // >> EXPORTED IN MANUALBINDINGS <<
153 
155  bool DoWithPlayer(const AString & a_PlayerName, cPlayerListCallback a_Callback);
156 
158  void SendPlayerLists(cPlayer * a_DestPlayer);
159 
161  void BroadcastPlayerListsAddPlayer(const cPlayer & a_Player, const cClientHandle * a_Exclude = nullptr);
162 
164  void BroadcastPlayerListsRemovePlayer(const cPlayer & a_Player, const cClientHandle * a_Exclude = nullptr);
165 
167  void BroadcastPlayerListsHeaderFooter(const cCompositeChat & a_Header, const cCompositeChat & a_Footer); // tolua_export
168 
169  // tolua_begin
170 
172  void BroadcastChat (const AString & a_Message, eMessageType a_ChatPrefix = mtCustom);
173  void BroadcastChat (const cCompositeChat & a_Message);
174  void BroadcastChatDeath (const AString & a_Message) { BroadcastChat(a_Message, mtDeath); }
175  void BroadcastChatFailure(const AString & a_Message) { BroadcastChat(a_Message, mtFailure); }
176  void BroadcastChatFatal (const AString & a_Message) { BroadcastChat(a_Message, mtFailure); }
177  void BroadcastChatInfo (const AString & a_Message) { BroadcastChat(a_Message, mtInformation); }
178  void BroadcastChatJoin (const AString & a_Message) { BroadcastChat(a_Message, mtJoin); }
179  void BroadcastChatLeave (const AString & a_Message) { BroadcastChat(a_Message, mtLeave); }
180  void BroadcastChatSuccess(const AString & a_Message) { BroadcastChat(a_Message, mtSuccess); }
181  void BroadcastChatWarning(const AString & a_Message) { BroadcastChat(a_Message, mtWarning); }
182 
184  static AString GetProtocolVersionTextFromInt(int a_ProtocolVersionNum);
185 
187  static int GetVirtualRAMUsage(void);
188 
190  static int GetPhysicalRAMUsage(void);
191 
192  // tolua_end
193 
194 private:
195 
198  enum class NextState
199  {
200  Run,
201  Restart,
202  Stop
203  };
204 
205  typedef std::map<AString, cWorld> WorldMap;
206 
208  void HandleInput();
209 
211  static void TransitionNextState(NextState a_NextState);
212 
215 
217 
220 
222  std::unique_ptr<cRecipeMapper> m_RecipeMapper;
224  std::unique_ptr<cBrewingRecipes> m_BrewingRecipes;
229 
230  std::unique_ptr<cRankManager> m_RankManager;
231 
233 
235  // BlockTypeRegistry m_BlockTypeRegistry;
236 
237 
238  void LoadGlobalSettings();
239 
241  void LoadWorlds(cDeadlockDetect & a_dd, cSettingsRepositoryInterface & a_Settings, bool a_IsNewIniFile);
242 
244  void StartWorlds(cDeadlockDetect & a_DeadlockDetect);
245 
247  void StopWorlds(cDeadlockDetect & a_DeadlockDetect);
248 
249  static cRoot * s_Root;
250 
252  static std::atomic<NextState> s_NextState;
253 }; // tolua_export
eMessageType
Definition: Defines.h:352
@ mtWarning
Definition: Defines.h:360
@ mtJoin
Definition: Defines.h:364
@ mtSuccess
Definition: Defines.h:359
@ mtCustom
Definition: Defines.h:356
@ mtInformation
Definition: Defines.h:358
@ mtDeath
Definition: Defines.h:362
@ mtLeave
Definition: Defines.h:365
@ mtFailure
Definition: Defines.h:357
std::vector< AString > AStringVector
Definition: StringUtils.h:12
std::string AString
Definition: StringUtils.h:11
Definition: Inventory.h:11
Holds a palette that maps between block type + state and numbers.
Interface for a callback that receives command output The Out() function is called for any output the...
Definition: CommandOutput.h:16
Container for a single chat message composed of multiple functional parts.
Definition: CompositeChat.h:34
The crafting recipes are the configurations to build a result item out of a set of ingredient items.
Definition: Player.h:29
Definition: Item.h:37
Definition: Event.h:18
The RecipeMapper handles the translation of crafting recipes into protocol specific recipe Ids.
Definition: RecipeMapper.h:14
The root of the object hierarchy.
Definition: Root.h:50
cMonsterConfig * m_MonsterConfig
Definition: Root.h:219
void BroadcastChatFatal(const AString &a_Message)
Definition: Root.h:176
AString m_SettingsFilename
which ini file to load settings from, default is settings.ini
Definition: Root.h:56
std::unique_ptr< cRecipeMapper > m_RecipeMapper
Definition: Root.h:222
cServer * GetServer(void)
Definition: Root.h:71
void BroadcastChatDeath(const AString &a_Message)
Definition: Root.h:174
cServer * m_Server
Definition: Root.h:218
void SetSavingEnabled(bool a_SavingEnabled)
Sets whether saving chunks is enabled in all worlds (overrides however the worlds were already set)
Definition: Root.cpp:597
void BroadcastPlayerListsHeaderFooter(const cCompositeChat &a_Header, const cCompositeChat &a_Footer)
Broadcast playerlist header and footer through all worlds.
Definition: Root.cpp:645
static cRoot * Get()
Definition: Root.h:52
cWorld * m_pDefaultWorld
Definition: Root.h:213
static int GetPhysicalRAMUsage(void)
Returns the amount of virtual RAM used, in KiB.
Definition: Root.cpp:840
cRoot(void)
Definition: Root.cpp:75
void BroadcastChat(const AString &a_Message, eMessageType a_ChatPrefix=mtCustom)
Sends a chat message to all connected clients (in all worlds)
Definition: Root.cpp:657
void BroadcastChatWarning(const AString &a_Message)
Definition: Root.h:181
bool ForEachWorld(cWorldListCallback a_Callback)
Calls the callback for each world; returns true if the callback didn't abort (return true)
Definition: Root.cpp:480
bool FindAndDoWithPlayer(const AString &a_PlayerName, cPlayerListCallback a_Callback)
Finds a player from a partial or complete player name and calls the callback - case-insensitive.
Definition: Root.cpp:697
cPluginManager * m_PluginManager
Definition: Root.h:226
void SaveAllChunksNow(void)
Saves all chunks in all worlds synchronously (waits until dirty chunks have been sent to the ChunkSto...
Definition: Root.cpp:585
cFurnaceRecipe * GetFurnaceRecipe(void)
Definition: Root.h:94
void KickUser(int a_ClientID, const AString &a_Reason)
Kicks the user, no matter in what world they are.
Definition: Root.cpp:550
AStringVector GetPlayerTabCompletionMultiWorld(const AString &a_Text)
Returns the completions for a player name across all worlds.
Definition: Root.cpp:983
int GetServerUpTime(void)
Returns the up time of the server in seconds.
Definition: Root.h:78
void SendPlayerLists(cPlayer *a_DestPlayer)
Send playerlist of all worlds to player.
Definition: Root.cpp:609
void StartWorlds(cDeadlockDetect &a_DeadlockDetect)
Starts each world's life.
Definition: Root.cpp:428
cBrewingRecipes * GetBrewingRecipes(void)
Definition: Root.h:95
cCraftingRecipes * m_CraftingRecipes
Definition: Root.h:221
bool DoWithPlayerByUUID(const cUUID &a_PlayerUUID, cPlayerListCallback a_Callback)
Finds the player over his uuid and calls the callback.
Definition: Root.cpp:747
std::chrono::steady_clock::time_point m_StartTime
The current time where the startup of the server has been completed.
Definition: Root.h:108
cAuthenticator & GetAuthenticator(void)
Definition: Root.h:112
cMojangAPI * m_MojangAPI
Definition: Root.h:228
void HandleInput()
Blocking reads and processes console input.
Definition: Root.cpp:999
void SaveAllChunks(void)
Saves all chunks in all worlds.
Definition: Root.cpp:573
cMojangAPI & GetMojangAPI(void)
Definition: Root.h:113
cWorld * GetDefaultWorld(void)
Definition: Root.cpp:455
cRecipeMapper * GetRecipeMapper(void)
Definition: Root.h:93
static cRoot * s_Root
Definition: Root.h:249
NextState
States that the global cRoot can be in.
Definition: Root.h:199
void BroadcastChatLeave(const AString &a_Message)
Definition: Root.h:179
cCraftingRecipes * GetCraftingRecipes(void)
Definition: Root.h:92
cWebAdmin * m_WebAdmin
Definition: Root.h:225
static void Restart()
Interrupts the server and restarts it, as if "/restart" was typed in the console.
Definition: Root.cpp:268
static void Stop()
Interrupts the server and stops it, as if "/stop" typed in the console.
Definition: Root.cpp:259
static int GetVirtualRAMUsage(void)
Returns the amount of virtual RAM used, in KiB.
Definition: Root.cpp:788
cHTTPServer m_HTTPServer
Definition: Root.h:232
void BroadcastChatInfo(const AString &a_Message)
Definition: Root.h:177
static int GetFurnaceFuelBurnTime(const cItem &a_Fuel)
Returns the (read-write) storage for registered block types.
Definition: Root.cpp:973
cPluginManager * GetPluginManager(void)
Definition: Root.h:111
std::unique_ptr< cBrewingRecipes > m_BrewingRecipes
Definition: Root.h:224
static cEvent s_StopEvent
Definition: Root.h:216
void BroadcastChatJoin(const AString &a_Message)
Definition: Root.h:178
bool DoWithPlayer(const AString &a_PlayerName, cPlayerListCallback a_Callback)
Finds the player using it's complete username and calls the callback.
Definition: Root.cpp:763
void QueueExecuteConsoleCommand(const AString &a_Cmd, cCommandOutputCallback &a_Output)
Queues a console command for execution through the cServer class.
Definition: Root.cpp:496
size_t GetTotalChunkCount(void)
Returns the number of chunks loaded.
Definition: Root.cpp:559
static AString GetProtocolVersionTextFromInt(int a_ProtocolVersionNum)
Returns the textual description of the protocol version: 49 -> "1.4.4".
Definition: Root.cpp:779
~cRoot()
Definition: Root.cpp:94
void StopWorlds(cDeadlockDetect &a_DeadlockDetect)
Stops each world's threads, so that it's safe to unload them.
Definition: Root.cpp:443
void BroadcastChatFailure(const AString &a_Message)
Definition: Root.h:175
cWorld * GetWorld(const AString &a_WorldName)
Returns a pointer to the world specified.
Definition: Root.cpp:465
std::map< AString, cWorld > WorldMap
Definition: Root.h:205
WorldMap m_WorldsByName
Definition: Root.h:214
static void TransitionNextState(NextState a_NextState)
Performs run state transition, enforcing guarantees about state transitions.
Definition: Root.cpp:1051
void BroadcastPlayerListsAddPlayer(const cPlayer &a_Player, const cClientHandle *a_Exclude=nullptr)
Broadcast playerlist addition through all worlds.
Definition: Root.cpp:621
void LoadGlobalSettings()
The storage for all registered block types.
Definition: Root.cpp:277
cAuthenticator m_Authenticator
Definition: Root.h:227
void BroadcastPlayerListsRemovePlayer(const cPlayer &a_Player, const cClientHandle *a_Exclude=nullptr)
Broadcast playerlist removal through all worlds.
Definition: Root.cpp:633
void BroadcastChatSuccess(const AString &a_Message)
Definition: Root.h:180
cWebAdmin * GetWebAdmin(void)
Definition: Root.h:110
void LogChunkStats(cCommandOutputCallback &a_Output)
Writes chunkstats, for each world and totals, to the output callback.
Definition: Root.cpp:923
cRankManager * GetRankManager(void)
Definition: Root.h:114
cFurnaceRecipe * m_FurnaceRecipe
Definition: Root.h:223
bool ForEachPlayer(cPlayerListCallback a_Callback)
Calls the callback for each player in all worlds.
Definition: Root.cpp:681
static std::atomic< NextState > s_NextState
Indicates the next action of cRoot, whether to run, stop or restart.
Definition: Root.h:252
void LoadWorlds(cDeadlockDetect &a_dd, cSettingsRepositoryInterface &a_Settings, bool a_IsNewIniFile)
Loads the worlds from settings.ini, creates the worldmap.
Definition: Root.cpp:286
std::unique_ptr< cRankManager > m_RankManager
Definition: Root.h:230
bool Run(cSettingsRepositoryInterface &a_OverridesRepo)
Run the server.
Definition: Root.cpp:103
cMonsterConfig * GetMonsterConfig(void)
Definition: Root.h:90
Definition: Server.h:56
Definition: UUID.h:11
Definition: World.h:53