]> git.lizzy.rs Git - minetest.git/blob - src/server.h
Add minetest.get_player_window_information() (#12367)
[minetest.git] / src / server.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include "irr_v3d.h"
23 #include "map.h"
24 #include "hud.h"
25 #include "gamedef.h"
26 #include "serialization.h" // For SER_FMT_VER_INVALID
27 #include "content/mods.h"
28 #include "inventorymanager.h"
29 #include "content/subgames.h"
30 #include "tileanimation.h" // TileAnimationParams
31 #include "particles.h" // ParticleParams
32 #include "network/peerhandler.h"
33 #include "network/address.h"
34 #include "util/numeric.h"
35 #include "util/thread.h"
36 #include "util/basic_macros.h"
37 #include "util/metricsbackend.h"
38 #include "serverenvironment.h"
39 #include "clientiface.h"
40 #include "chatmessage.h"
41 #include "translation.h"
42 #include <string>
43 #include <list>
44 #include <map>
45 #include <vector>
46 #include <unordered_set>
47
48 class ChatEvent;
49 struct ChatEventChat;
50 struct ChatInterface;
51 class IWritableItemDefManager;
52 class NodeDefManager;
53 class IWritableCraftDefManager;
54 class BanManager;
55 class EventManager;
56 class Inventory;
57 class ModChannelMgr;
58 class RemotePlayer;
59 class PlayerSAO;
60 struct PlayerHPChangeReason;
61 class IRollbackManager;
62 struct RollbackAction;
63 class EmergeManager;
64 class ServerScripting;
65 class ServerEnvironment;
66 struct SimpleSoundSpec;
67 struct CloudParams;
68 struct SkyboxParams;
69 struct SunParams;
70 struct MoonParams;
71 struct StarParams;
72 struct Lighting;
73 class ServerThread;
74 class ServerModManager;
75 class ServerInventoryManager;
76 struct PackedValue;
77
78 enum ClientDeletionReason {
79         CDR_LEAVE,
80         CDR_TIMEOUT,
81         CDR_DENY
82 };
83
84 struct MediaInfo
85 {
86         std::string path;
87         std::string sha1_digest; // base64-encoded
88         bool no_announce; // true: not announced in TOCLIENT_ANNOUNCE_MEDIA (at player join)
89
90         MediaInfo(const std::string &path_="",
91                   const std::string &sha1_digest_=""):
92                 path(path_),
93                 sha1_digest(sha1_digest_),
94                 no_announce(false)
95         {
96         }
97 };
98
99 // Combines the pure sound (SimpleSoundSpec) with positional information
100 struct ServerPlayingSound
101 {
102         SoundLocation type = SoundLocation::Local;
103
104         float gain = 1.0f; // for amplification of the base sound
105         float max_hear_distance = 32 * BS;
106         v3f pos;
107         u16 object = 0;
108         std::string to_player;
109         std::string exclude_player;
110
111         v3f getPos(ServerEnvironment *env, bool *pos_exists) const;
112
113         SimpleSoundSpec spec;
114
115         std::unordered_set<session_t> clients; // peer ids
116 };
117
118 struct MinimapMode {
119         MinimapType type = MINIMAP_TYPE_OFF;
120         std::string label;
121         u16 size = 0;
122         std::string texture;
123         u16 scale = 1;
124 };
125
126 // structure for everything getClientInfo returns, for convenience
127 struct ClientInfo {
128         ClientState state;
129         Address addr;
130         u32 uptime;
131         u8 ser_vers;
132         u16 prot_vers;
133         u8 major, minor, patch;
134         std::string vers_string, lang_code;
135 };
136
137 class Server : public con::PeerHandler, public MapEventReceiver,
138                 public IGameDef
139 {
140 public:
141         /*
142                 NOTE: Every public method should be thread-safe
143         */
144
145         Server(
146                 const std::string &path_world,
147                 const SubgameSpec &gamespec,
148                 bool simple_singleplayer_mode,
149                 Address bind_addr,
150                 bool dedicated,
151                 ChatInterface *iface = nullptr,
152                 std::string *on_shutdown_errmsg = nullptr
153         );
154         ~Server();
155         DISABLE_CLASS_COPY(Server);
156
157         void start();
158         void stop();
159         // This is mainly a way to pass the time to the server.
160         // Actual processing is done in another thread.
161         void step(float dtime);
162         // This is run by ServerThread and does the actual processing
163         void AsyncRunStep(bool initial_step=false);
164         void Receive();
165         PlayerSAO* StageTwoClientInit(session_t peer_id);
166
167         /*
168          * Command Handlers
169          */
170
171         void handleCommand(NetworkPacket* pkt);
172
173         void handleCommand_Null(NetworkPacket* pkt) {};
174         void handleCommand_Deprecated(NetworkPacket* pkt);
175         void handleCommand_Init(NetworkPacket* pkt);
176         void handleCommand_Init2(NetworkPacket* pkt);
177         void handleCommand_ModChannelJoin(NetworkPacket *pkt);
178         void handleCommand_ModChannelLeave(NetworkPacket *pkt);
179         void handleCommand_ModChannelMsg(NetworkPacket *pkt);
180         void handleCommand_RequestMedia(NetworkPacket* pkt);
181         void handleCommand_ClientReady(NetworkPacket* pkt);
182         void handleCommand_GotBlocks(NetworkPacket* pkt);
183         void handleCommand_PlayerPos(NetworkPacket* pkt);
184         void handleCommand_DeletedBlocks(NetworkPacket* pkt);
185         void handleCommand_InventoryAction(NetworkPacket* pkt);
186         void handleCommand_ChatMessage(NetworkPacket* pkt);
187         void handleCommand_Damage(NetworkPacket* pkt);
188         void handleCommand_PlayerItem(NetworkPacket* pkt);
189         void handleCommand_Respawn(NetworkPacket* pkt);
190         void handleCommand_Interact(NetworkPacket* pkt);
191         void handleCommand_RemovedSounds(NetworkPacket* pkt);
192         void handleCommand_NodeMetaFields(NetworkPacket* pkt);
193         void handleCommand_InventoryFields(NetworkPacket* pkt);
194         void handleCommand_FirstSrp(NetworkPacket* pkt);
195         void handleCommand_SrpBytesA(NetworkPacket* pkt);
196         void handleCommand_SrpBytesM(NetworkPacket* pkt);
197         void handleCommand_HaveMedia(NetworkPacket *pkt);
198         void handleCommand_UpdateClientInfo(NetworkPacket *pkt);
199
200         void ProcessData(NetworkPacket *pkt);
201
202         void Send(NetworkPacket *pkt);
203         void Send(session_t peer_id, NetworkPacket *pkt);
204
205         // Helper for handleCommand_PlayerPos and handleCommand_Interact
206         void process_PlayerPos(RemotePlayer *player, PlayerSAO *playersao,
207                 NetworkPacket *pkt);
208
209         // Both setter and getter need no envlock,
210         // can be called freely from threads
211         void setTimeOfDay(u32 time);
212
213         /*
214                 Shall be called with the environment locked.
215                 This is accessed by the map, which is inside the environment,
216                 so it shouldn't be a problem.
217         */
218         void onMapEditEvent(const MapEditEvent &event);
219
220         // Connection must be locked when called
221         std::string getStatusString();
222         inline double getUptime() const { return m_uptime_counter->get(); }
223
224         // read shutdown state
225         inline bool isShutdownRequested() const { return m_shutdown_state.is_requested; }
226
227         // request server to shutdown
228         void requestShutdown(const std::string &msg, bool reconnect, float delay = 0.0f);
229
230         // Returns -1 if failed, sound handle on success
231         // Envlock
232         s32 playSound(ServerPlayingSound &params, bool ephemeral=false);
233         void stopSound(s32 handle);
234         void fadeSound(s32 handle, float step, float gain);
235
236         // Envlock
237         std::set<std::string> getPlayerEffectivePrivs(const std::string &name);
238         bool checkPriv(const std::string &name, const std::string &priv);
239         void reportPrivsModified(const std::string &name=""); // ""=all
240         void reportInventoryFormspecModified(const std::string &name);
241         void reportFormspecPrependModified(const std::string &name);
242
243         void setIpBanned(const std::string &ip, const std::string &name);
244         void unsetIpBanned(const std::string &ip_or_name);
245         std::string getBanDescription(const std::string &ip_or_name);
246
247         void notifyPlayer(const char *name, const std::wstring &msg);
248         void notifyPlayers(const std::wstring &msg);
249
250         void spawnParticle(const std::string &playername,
251                 const ParticleParameters &p);
252
253         u32 addParticleSpawner(const ParticleSpawnerParameters &p,
254                 ServerActiveObject *attached, const std::string &playername);
255
256         void deleteParticleSpawner(const std::string &playername, u32 id);
257
258         bool dynamicAddMedia(std::string filepath, u32 token,
259                 const std::string &to_player, bool ephemeral);
260
261         ServerInventoryManager *getInventoryMgr() const { return m_inventory_mgr.get(); }
262         void sendDetachedInventory(Inventory *inventory, const std::string &name, session_t peer_id);
263
264         // Envlock and conlock should be locked when using scriptapi
265         ServerScripting *getScriptIface(){ return m_script; }
266
267         // actions: time-reversed list
268         // Return value: success/failure
269         bool rollbackRevertActions(const std::list<RollbackAction> &actions,
270                         std::list<std::string> *log);
271
272         // IGameDef interface
273         // Under envlock
274         virtual IItemDefManager* getItemDefManager();
275         virtual const NodeDefManager* getNodeDefManager();
276         virtual ICraftDefManager* getCraftDefManager();
277         virtual u16 allocateUnknownNodeId(const std::string &name);
278         IRollbackManager *getRollbackManager() { return m_rollback; }
279         virtual EmergeManager *getEmergeManager() { return m_emerge; }
280         virtual ModStorageDatabase *getModStorageDatabase() { return m_mod_storage_database; }
281
282         IWritableItemDefManager* getWritableItemDefManager();
283         NodeDefManager* getWritableNodeDefManager();
284         IWritableCraftDefManager* getWritableCraftDefManager();
285
286         virtual const std::vector<ModSpec> &getMods() const;
287         virtual const ModSpec* getModSpec(const std::string &modname) const;
288         virtual const SubgameSpec* getGameSpec() const { return &m_gamespec; }
289         static std::string getBuiltinLuaPath();
290         virtual std::string getWorldPath() const { return m_path_world; }
291
292         inline bool isSingleplayer() const
293                         { return m_simple_singleplayer_mode; }
294
295         inline void setAsyncFatalError(const std::string &error)
296                         { m_async_fatal_error.set(error); }
297         inline void setAsyncFatalError(const LuaError &e)
298         {
299                 setAsyncFatalError(std::string("Lua: ") + e.what());
300         }
301
302         bool showFormspec(const char *name, const std::string &formspec, const std::string &formname);
303         Map & getMap() { return m_env->getMap(); }
304         ServerEnvironment & getEnv() { return *m_env; }
305         v3f findSpawnPos();
306
307         u32 hudAdd(RemotePlayer *player, HudElement *element);
308         bool hudRemove(RemotePlayer *player, u32 id);
309         bool hudChange(RemotePlayer *player, u32 id, HudElementStat stat, void *value);
310         bool hudSetFlags(RemotePlayer *player, u32 flags, u32 mask);
311         bool hudSetHotbarItemcount(RemotePlayer *player, s32 hotbar_itemcount);
312         void hudSetHotbarImage(RemotePlayer *player, const std::string &name);
313         void hudSetHotbarSelectedImage(RemotePlayer *player, const std::string &name);
314
315         Address getPeerAddress(session_t peer_id);
316
317         void setLocalPlayerAnimations(RemotePlayer *player, v2s32 animation_frames[4],
318                         f32 frame_speed);
319         void setPlayerEyeOffset(RemotePlayer *player, const v3f &first, const v3f &third);
320
321         void setSky(RemotePlayer *player, const SkyboxParams &params);
322         void setSun(RemotePlayer *player, const SunParams &params);
323         void setMoon(RemotePlayer *player, const MoonParams &params);
324         void setStars(RemotePlayer *player, const StarParams &params);
325
326         void setClouds(RemotePlayer *player, const CloudParams &params);
327
328         void overrideDayNightRatio(RemotePlayer *player, bool do_override, float brightness);
329
330         void setLighting(RemotePlayer *player, const Lighting &lighting);
331
332         void RespawnPlayer(session_t peer_id);
333
334         /* con::PeerHandler implementation. */
335         void peerAdded(con::Peer *peer);
336         void deletingPeer(con::Peer *peer, bool timeout);
337
338         void DenySudoAccess(session_t peer_id);
339         void DenyAccess(session_t peer_id, AccessDeniedCode reason,
340                 const std::string &custom_reason = "", bool reconnect = false);
341         void acceptAuth(session_t peer_id, bool forSudoMode);
342         void DisconnectPeer(session_t peer_id);
343         bool getClientConInfo(session_t peer_id, con::rtt_stat_type type, float *retval);
344         bool getClientInfo(session_t peer_id, ClientInfo &ret);
345         const ClientDynamicInfo *getClientDynamicInfo(session_t peer_id);
346
347         void printToConsoleOnly(const std::string &text);
348
349         void HandlePlayerHPChange(PlayerSAO *sao, const PlayerHPChangeReason &reason);
350         void SendPlayerHP(PlayerSAO *sao, bool effect);
351         void SendPlayerBreath(PlayerSAO *sao);
352         void SendInventory(PlayerSAO *playerSAO, bool incremental);
353         void SendMovePlayer(session_t peer_id);
354         void SendPlayerSpeed(session_t peer_id, const v3f &added_vel);
355         void SendPlayerFov(session_t peer_id);
356
357         void SendMinimapModes(session_t peer_id,
358                         std::vector<MinimapMode> &modes,
359                         size_t wanted_mode);
360
361         void sendDetachedInventories(session_t peer_id, bool incremental);
362
363         bool joinModChannel(const std::string &channel);
364         bool leaveModChannel(const std::string &channel);
365         bool sendModChannelMessage(const std::string &channel, const std::string &message);
366         ModChannel *getModChannel(const std::string &channel);
367
368         // Send block to specific player only
369         bool SendBlock(session_t peer_id, const v3s16 &blockpos);
370
371         // Get or load translations for a language
372         Translations *getTranslationLanguage(const std::string &lang_code);
373
374         static ModStorageDatabase *openModStorageDatabase(const std::string &world_path);
375
376         static ModStorageDatabase *openModStorageDatabase(const std::string &backend,
377                         const std::string &world_path, const Settings &world_mt);
378
379         static bool migrateModStorageDatabase(const GameParams &game_params,
380                         const Settings &cmd_args);
381
382         // Lua files registered for init of async env, pair of modname + path
383         std::vector<std::pair<std::string, std::string>> m_async_init_files;
384
385         // Data transferred into async envs at init time
386         std::unique_ptr<PackedValue> m_async_globals_data;
387
388         // Bind address
389         Address m_bind_addr;
390
391         // Environment mutex (envlock)
392         std::mutex m_env_mutex;
393
394 private:
395         friend class EmergeThread;
396         friend class RemoteClient;
397         friend class TestServerShutdownState;
398
399         struct ShutdownState {
400                 friend class TestServerShutdownState;
401                 public:
402                         bool is_requested = false;
403                         bool should_reconnect = false;
404                         std::string message;
405
406                         void reset();
407                         void trigger(float delay, const std::string &msg, bool reconnect);
408                         void tick(float dtime, Server *server);
409                         std::wstring getShutdownTimerMessage() const;
410                         bool isTimerRunning() const { return m_timer > 0.0f; }
411                 private:
412                         float m_timer = 0.0f;
413         };
414
415         struct PendingDynamicMediaCallback {
416                 std::string filename; // only set if media entry and file is to be deleted
417                 float expiry_timer;
418                 std::unordered_set<session_t> waiting_players;
419         };
420
421         // The standard library does not implement std::hash for pairs so we have this:
422         struct SBCHash {
423                 size_t operator() (const std::pair<v3s16, u16> &p) const {
424                         return std::hash<v3s16>()(p.first) ^ p.second;
425                 }
426         };
427
428         typedef std::unordered_map<std::pair<v3s16, u16>, std::string, SBCHash> SerializedBlockCache;
429
430         void init();
431
432         void SendMovement(session_t peer_id);
433         void SendHP(session_t peer_id, u16 hp, bool effect);
434         void SendBreath(session_t peer_id, u16 breath);
435         void SendAccessDenied(session_t peer_id, AccessDeniedCode reason,
436                 const std::string &custom_reason, bool reconnect = false);
437         void SendAccessDenied_Legacy(session_t peer_id, const std::wstring &reason);
438         void SendDeathscreen(session_t peer_id, bool set_camera_point_target,
439                 v3f camera_point_target);
440         void SendItemDef(session_t peer_id, IItemDefManager *itemdef, u16 protocol_version);
441         void SendNodeDef(session_t peer_id, const NodeDefManager *nodedef,
442                 u16 protocol_version);
443
444
445         virtual void SendChatMessage(session_t peer_id, const ChatMessage &message);
446         void SendTimeOfDay(session_t peer_id, u16 time, f32 time_speed);
447
448         void SendLocalPlayerAnimations(session_t peer_id, v2s32 animation_frames[4],
449                 f32 animation_speed);
450         void SendEyeOffset(session_t peer_id, v3f first, v3f third);
451         void SendPlayerPrivileges(session_t peer_id);
452         void SendPlayerInventoryFormspec(session_t peer_id);
453         void SendPlayerFormspecPrepend(session_t peer_id);
454         void SendShowFormspecMessage(session_t peer_id, const std::string &formspec,
455                 const std::string &formname);
456         void SendHUDAdd(session_t peer_id, u32 id, HudElement *form);
457         void SendHUDRemove(session_t peer_id, u32 id);
458         void SendHUDChange(session_t peer_id, u32 id, HudElementStat stat, void *value);
459         void SendHUDSetFlags(session_t peer_id, u32 flags, u32 mask);
460         void SendHUDSetParam(session_t peer_id, u16 param, const std::string &value);
461         void SendSetSky(session_t peer_id, const SkyboxParams &params);
462         void SendSetSun(session_t peer_id, const SunParams &params);
463         void SendSetMoon(session_t peer_id, const MoonParams &params);
464         void SendSetStars(session_t peer_id, const StarParams &params);
465         void SendCloudParams(session_t peer_id, const CloudParams &params);
466         void SendOverrideDayNightRatio(session_t peer_id, bool do_override, float ratio);
467         void SendSetLighting(session_t peer_id, const Lighting &lighting);
468         void broadcastModChannelMessage(const std::string &channel,
469                         const std::string &message, session_t from_peer);
470
471         /*
472                 Send a node removal/addition event to all clients except ignore_id.
473                 Additionally, if far_players!=NULL, players further away than
474                 far_d_nodes are ignored and their peer_ids are added to far_players
475         */
476         // Envlock and conlock should be locked when calling these
477         void sendRemoveNode(v3s16 p, std::unordered_set<u16> *far_players = nullptr,
478                         float far_d_nodes = 100);
479         void sendAddNode(v3s16 p, MapNode n,
480                         std::unordered_set<u16> *far_players = nullptr,
481                         float far_d_nodes = 100, bool remove_metadata = true);
482         void sendNodeChangePkt(NetworkPacket &pkt, v3s16 block_pos,
483                         v3f p, float far_d_nodes, std::unordered_set<u16> *far_players);
484
485         void sendMetadataChanged(const std::unordered_set<v3s16> &positions,
486                         float far_d_nodes = 100);
487
488         // Environment and Connection must be locked when called
489         // `cache` may only be very short lived! (invalidation not handeled)
490         void SendBlockNoLock(session_t peer_id, MapBlock *block, u8 ver,
491                 u16 net_proto_version, SerializedBlockCache *cache = nullptr);
492
493         // Sends blocks to clients (locks env and con on its own)
494         void SendBlocks(float dtime);
495
496         bool addMediaFile(const std::string &filename, const std::string &filepath,
497                         std::string *filedata = nullptr, std::string *digest = nullptr);
498         void fillMediaCache();
499         void sendMediaAnnouncement(session_t peer_id, const std::string &lang_code);
500         void sendRequestedMedia(session_t peer_id,
501                         const std::vector<std::string> &tosend);
502         void stepPendingDynMediaCallbacks(float dtime);
503
504         // Adds a ParticleSpawner on peer with peer_id (PEER_ID_INEXISTENT == all)
505         void SendAddParticleSpawner(session_t peer_id, u16 protocol_version,
506                 const ParticleSpawnerParameters &p, u16 attached_id, u32 id);
507
508         void SendDeleteParticleSpawner(session_t peer_id, u32 id);
509
510         // Spawns particle on peer with peer_id (PEER_ID_INEXISTENT == all)
511         void SendSpawnParticle(session_t peer_id, u16 protocol_version,
512                 const ParticleParameters &p);
513
514         void SendActiveObjectRemoveAdd(RemoteClient *client, PlayerSAO *playersao);
515         void SendActiveObjectMessages(session_t peer_id, const std::string &datas,
516                 bool reliable = true);
517         void SendCSMRestrictionFlags(session_t peer_id);
518
519         /*
520                 Something random
521         */
522
523         void HandlePlayerDeath(PlayerSAO* sao, const PlayerHPChangeReason &reason);
524         void DeleteClient(session_t peer_id, ClientDeletionReason reason);
525         void UpdateCrafting(RemotePlayer *player);
526         bool checkInteractDistance(RemotePlayer *player, const f32 d, const std::string &what);
527
528         void handleChatInterfaceEvent(ChatEvent *evt);
529
530         // This returns the answer to the sender of wmessage, or "" if there is none
531         std::wstring handleChat(const std::string &name, std::wstring wmessage_input,
532                 bool check_shout_priv = false, RemotePlayer *player = nullptr);
533         void handleAdminChat(const ChatEventChat *evt);
534
535         // When called, connection mutex should be locked
536         RemoteClient* getClient(session_t peer_id, ClientState state_min = CS_Active);
537         RemoteClient* getClientNoEx(session_t peer_id, ClientState state_min = CS_Active);
538
539         // When called, environment mutex should be locked
540         std::string getPlayerName(session_t peer_id);
541         PlayerSAO *getPlayerSAO(session_t peer_id);
542
543         /*
544                 Get a player from memory or creates one.
545                 If player is already connected, return NULL
546                 Does not verify/modify auth info and password.
547
548                 Call with env and con locked.
549         */
550         PlayerSAO *emergePlayer(const char *name, session_t peer_id, u16 proto_version);
551
552         void handlePeerChanges();
553
554         /*
555                 Variables
556         */
557         // World directory
558         std::string m_path_world;
559         // Subgame specification
560         SubgameSpec m_gamespec;
561         // If true, do not allow multiple players and hide some multiplayer
562         // functionality
563         bool m_simple_singleplayer_mode;
564         u16 m_max_chatmessage_length;
565         // For "dedicated" server list flag
566         bool m_dedicated;
567         Settings *m_game_settings = nullptr;
568
569         // Thread can set; step() will throw as ServerError
570         MutexedVariable<std::string> m_async_fatal_error;
571
572         // Some timers
573         float m_liquid_transform_timer = 0.0f;
574         float m_liquid_transform_every = 1.0f;
575         float m_masterserver_timer = 0.0f;
576         float m_emergethread_trigger_timer = 0.0f;
577         float m_savemap_timer = 0.0f;
578         IntervalLimiter m_map_timer_and_unload_interval;
579
580         // Environment
581         ServerEnvironment *m_env = nullptr;
582
583         // Reference to the server map until ServerEnvironment is initialized
584         // after that this variable must be a nullptr
585         ServerMap *m_startup_server_map = nullptr;
586
587         // server connection
588         std::shared_ptr<con::Connection> m_con;
589
590         // Ban checking
591         BanManager *m_banmanager = nullptr;
592
593         // Rollback manager (behind m_env_mutex)
594         IRollbackManager *m_rollback = nullptr;
595
596         // Emerge manager
597         EmergeManager *m_emerge = nullptr;
598
599         // Scripting
600         // Envlock and conlock should be locked when using Lua
601         ServerScripting *m_script = nullptr;
602
603         // Item definition manager
604         IWritableItemDefManager *m_itemdef;
605
606         // Node definition manager
607         NodeDefManager *m_nodedef;
608
609         // Craft definition manager
610         IWritableCraftDefManager *m_craftdef;
611
612         // Mods
613         std::unique_ptr<ServerModManager> m_modmgr;
614
615         std::unordered_map<std::string, Translations> server_translations;
616
617         /*
618                 Threads
619         */
620         // A buffer for time steps
621         // step() increments and AsyncRunStep() run by m_thread reads it.
622         float m_step_dtime = 0.0f;
623         std::mutex m_step_dtime_mutex;
624
625         // The server mainly operates in this thread
626         ServerThread *m_thread = nullptr;
627
628         /*
629                 Time related stuff
630         */
631         // Timer for sending time of day over network
632         float m_time_of_day_send_timer = 0.0f;
633
634         /*
635                 Client interface
636         */
637         ClientInterface m_clients;
638
639         /*
640                 Peer change queue.
641                 Queues stuff from peerAdded() and deletingPeer() to
642                 handlePeerChanges()
643         */
644         std::queue<con::PeerChange> m_peer_change_queue;
645
646         std::unordered_map<session_t, std::string> m_formspec_state_data;
647
648         /*
649                 Random stuff
650         */
651
652         ShutdownState m_shutdown_state;
653
654         ChatInterface *m_admin_chat;
655         std::string m_admin_nick;
656
657         // if a mod-error occurs in the on_shutdown callback, the error message will
658         // be written into this
659         std::string *const m_on_shutdown_errmsg;
660
661         /*
662                 Map edit event queue. Automatically receives all map edits.
663                 The constructor of this class registers us to receive them through
664                 onMapEditEvent
665
666                 NOTE: Should these be moved to actually be members of
667                 ServerEnvironment?
668         */
669
670         /*
671                 Queue of map edits from the environment for sending to the clients
672                 This is behind m_env_mutex
673         */
674         std::queue<MapEditEvent*> m_unsent_map_edit_queue;
675         /*
676                 If a non-empty area, map edit events contained within are left
677                 unsent. Done at map generation time to speed up editing of the
678                 generated area, as it will be sent anyway.
679                 This is behind m_env_mutex
680         */
681         VoxelArea m_ignore_map_edit_events_area;
682
683         // media files known to server
684         std::unordered_map<std::string, MediaInfo> m_media;
685
686         // pending dynamic media callbacks, clients inform the server when they have a file fetched
687         std::unordered_map<u32, PendingDynamicMediaCallback> m_pending_dyn_media;
688         float m_step_pending_dyn_media_timer = 0.0f;
689
690         /*
691                 Sounds
692         */
693         std::unordered_map<s32, ServerPlayingSound> m_playing_sounds;
694         s32 m_next_sound_id = 0; // positive values only
695         s32 nextSoundId();
696
697         ModStorageDatabase *m_mod_storage_database = nullptr;
698         float m_mod_storage_save_timer = 10.0f;
699
700         // CSM restrictions byteflag
701         u64 m_csm_restriction_flags = CSMRestrictionFlags::CSM_RF_NONE;
702         u32 m_csm_restriction_noderange = 8;
703
704         // ModChannel manager
705         std::unique_ptr<ModChannelMgr> m_modchannel_mgr;
706
707         // Inventory manager
708         std::unique_ptr<ServerInventoryManager> m_inventory_mgr;
709
710         // Global server metrics backend
711         std::unique_ptr<MetricsBackend> m_metrics_backend;
712
713         // Server metrics
714         MetricCounterPtr m_uptime_counter;
715         MetricGaugePtr m_player_gauge;
716         MetricGaugePtr m_timeofday_gauge;
717         MetricGaugePtr m_lag_gauge;
718         MetricCounterPtr m_aom_buffer_counter[2]; // [0] = rel, [1] = unrel
719         MetricCounterPtr m_packet_recv_counter;
720         MetricCounterPtr m_packet_recv_processed_counter;
721         MetricCounterPtr m_map_edit_event_counter;
722 };
723
724 /*
725         Runs a simple dedicated server loop.
726
727         Shuts down when kill is set to true.
728 */
729 void dedicated_server_loop(Server &server, bool &kill);