Skip to content

Commit 454dbf8

Browse files
authoredMay 7, 2020
Server class code cleanups (#9769)
* Server::overrideDayNightRatio doesn't require to return bool There is no sense to sending null player, the caller should send a valid object * Server::init: make private & cleanup This function is always called before start() and loads some variables which can be loaded in constructor directly. Make it private and call it directly in start * Split Server inventory responsibility to a dedicated object This splits permit to found various historical issues: * duplicate lookups on player connection * sending inventory to non related player when a player connects * non friendly lookups on detached inventories ownership This reduce the detached inventory complexity and also increased the lookup performance in a quite interesting way for servers with thousands of inventories.
1 parent 650168c commit 454dbf8

12 files changed

+312
-207
lines changed
 

Diff for: ‎src/client/game.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,6 @@ bool Game::createSingleplayerServer(const std::string &map_dir,
13021302
}
13031303

13041304
server = new Server(map_dir, gamespec, simple_singleplayer_mode, bind_addr, false);
1305-
server->init();
13061305
server->start();
13071306

13081307
return true;

Diff for: ‎src/main.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,6 @@ static bool run_dedicated_server(const GameParams &game_params, const Settings &
887887
// Create server
888888
Server server(game_params.world_path, game_params.game_spec,
889889
false, bind_addr, true, &iface);
890-
server.init();
891890

892891
g_term_console.setup(&iface, &kill, admin_nick);
893892

@@ -922,7 +921,6 @@ static bool run_dedicated_server(const GameParams &game_params, const Settings &
922921
// Create server
923922
Server server(game_params.world_path, game_params.game_spec, false,
924923
bind_addr, true);
925-
server.init();
926924
server.start();
927925

928926
// Run server

Diff for: ‎src/network/serverpackethandler.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3434
#include "network/networkprotocol.h"
3535
#include "network/serveropcodes.h"
3636
#include "server/player_sao.h"
37+
#include "server/serverinventorymgr.h"
3738
#include "util/auth.h"
3839
#include "util/base64.h"
3940
#include "util/pointedthing.h"
@@ -620,9 +621,9 @@ void Server::handleCommand_InventoryAction(NetworkPacket* pkt)
620621
ma->from_inv.applyCurrentPlayer(player->getName());
621622
ma->to_inv.applyCurrentPlayer(player->getName());
622623

623-
setInventoryModified(ma->from_inv);
624+
m_inventory_mgr->setInventoryModified(ma->from_inv);
624625
if (ma->from_inv != ma->to_inv)
625-
setInventoryModified(ma->to_inv);
626+
m_inventory_mgr->setInventoryModified(ma->to_inv);
626627

627628
bool from_inv_is_current_player =
628629
(ma->from_inv.type == InventoryLocation::PLAYER) &&
@@ -687,7 +688,7 @@ void Server::handleCommand_InventoryAction(NetworkPacket* pkt)
687688

688689
da->from_inv.applyCurrentPlayer(player->getName());
689690

690-
setInventoryModified(da->from_inv);
691+
m_inventory_mgr->setInventoryModified(da->from_inv);
691692

692693
/*
693694
Disable dropping items out of craftpreview
@@ -723,7 +724,7 @@ void Server::handleCommand_InventoryAction(NetworkPacket* pkt)
723724

724725
ca->craft_inv.applyCurrentPlayer(player->getName());
725726

726-
setInventoryModified(ca->craft_inv);
727+
m_inventory_mgr->setInventoryModified(ca->craft_inv);
727728

728729
//bool craft_inv_is_current_player =
729730
// (ca->craft_inv.type == InventoryLocation::PLAYER) &&
@@ -739,7 +740,7 @@ void Server::handleCommand_InventoryAction(NetworkPacket* pkt)
739740
}
740741

741742
// Do the action
742-
a->apply(this, playersao, this);
743+
a->apply(m_inventory_mgr.get(), playersao, this);
743744
// Eat the action
744745
delete a;
745746
}

Diff for: ‎src/script/lua_api/l_base.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ Server *ModApiBase::getServer(lua_State *L)
4545
return getScriptApiBase(L)->getServer();
4646
}
4747

48+
ServerInventoryManager *ModApiBase::getServerInventoryMgr(lua_State *L)
49+
{
50+
return getScriptApiBase(L)->getServer()->getInventoryMgr();
51+
}
52+
4853
#ifndef SERVER
4954
Client *ModApiBase::getClient(lua_State *L)
5055
{

Diff for: ‎src/script/lua_api/l_base.h

+2
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ class GUIEngine;
3838
class ScriptApiBase;
3939
class Server;
4040
class Environment;
41+
class ServerInventoryManager;
4142

4243
class ModApiBase : protected LuaHelper {
4344

4445
public:
4546
static ScriptApiBase* getScriptApiBase(lua_State *L);
4647
static Server* getServer(lua_State *L);
48+
static ServerInventoryManager *getServerInventoryMgr(lua_State *L);
4749
#ifndef SERVER
4850
static Client* getClient(lua_State *L);
4951
static GUIEngine* getGuiEngine(lua_State *L);

Diff for: ‎src/script/lua_api/l_inventory.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2323
#include "common/c_converter.h"
2424
#include "common/c_content.h"
2525
#include "server.h"
26+
#include "server/serverinventorymgr.h"
2627
#include "remoteplayer.h"
2728

2829
/*
@@ -38,7 +39,7 @@ InvRef* InvRef::checkobject(lua_State *L, int narg)
3839

3940
Inventory* InvRef::getinv(lua_State *L, InvRef *ref)
4041
{
41-
return getServer(L)->getInventory(ref->m_loc);
42+
return getServerInventoryMgr(L)->getInventory(ref->m_loc);
4243
}
4344

4445
InventoryList* InvRef::getlist(lua_State *L, InvRef *ref,
@@ -54,7 +55,7 @@ InventoryList* InvRef::getlist(lua_State *L, InvRef *ref,
5455
void InvRef::reportInventoryChange(lua_State *L, InvRef *ref)
5556
{
5657
// Inform other things that the inventory has changed
57-
getServer(L)->setInventoryModified(ref->m_loc);
58+
getServerInventoryMgr(L)->setInventoryModified(ref->m_loc);
5859
}
5960

6061
// Exported functions
@@ -497,7 +498,7 @@ int ModApiInventory::l_get_inventory(lua_State *L)
497498
v3s16 pos = check_v3s16(L, -1);
498499
loc.setNodeMeta(pos);
499500

500-
if (getServer(L)->getInventory(loc) != NULL)
501+
if (getServerInventoryMgr(L)->getInventory(loc) != NULL)
501502
InvRef::create(L, loc);
502503
else
503504
lua_pushnil(L);
@@ -515,7 +516,7 @@ int ModApiInventory::l_get_inventory(lua_State *L)
515516
lua_pop(L, 1);
516517
}
517518

518-
if (getServer(L)->getInventory(loc) != NULL)
519+
if (getServerInventoryMgr(L)->getInventory(loc) != NULL)
519520
InvRef::create(L, loc);
520521
else
521522
lua_pushnil(L);
@@ -530,7 +531,7 @@ int ModApiInventory::l_create_detached_inventory_raw(lua_State *L)
530531
NO_MAP_LOCK_REQUIRED;
531532
const char *name = luaL_checkstring(L, 1);
532533
std::string player = readParam<std::string>(L, 2, "");
533-
if (getServer(L)->createDetachedInventory(name, player) != NULL) {
534+
if (getServerInventoryMgr(L)->createDetachedInventory(name, getServer(L)->idef(), player) != NULL) {
534535
InventoryLocation loc;
535536
loc.setDetached(name);
536537
InvRef::create(L, loc);
@@ -545,7 +546,7 @@ int ModApiInventory::l_remove_detached_inventory_raw(lua_State *L)
545546
{
546547
NO_MAP_LOCK_REQUIRED;
547548
const std::string &name = luaL_checkstring(L, 1);
548-
lua_pushboolean(L, getServer(L)->removeDetachedInventory(name));
549+
lua_pushboolean(L, getServerInventoryMgr(L)->removeDetachedInventory(name));
549550
return 1;
550551
}
551552

Diff for: ‎src/script/lua_api/l_object.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3333
#include "scripting_server.h"
3434
#include "server/luaentity_sao.h"
3535
#include "server/player_sao.h"
36+
#include "server/serverinventorymgr.h"
3637

3738
/*
3839
ObjectRef
@@ -289,7 +290,7 @@ int ObjectRef::l_get_inventory(lua_State *L)
289290
if (co == NULL) return 0;
290291
// Do it
291292
InventoryLocation loc = co->getInventoryLocation();
292-
if (getServer(L)->getInventory(loc) != NULL)
293+
if (getServerInventoryMgr(L)->getInventory(loc) != NULL)
293294
InvRef::create(L, loc);
294295
else
295296
lua_pushnil(L); // An object may have no inventory (nil)
@@ -2172,9 +2173,7 @@ int ObjectRef::l_override_day_night_ratio(lua_State *L)
21722173
ratio = readParam<float>(L, 2);
21732174
}
21742175

2175-
if (!getServer(L)->overrideDayNightRatio(player, do_override, ratio))
2176-
return 0;
2177-
2176+
getServer(L)->overrideDayNightRatio(player, do_override, ratio);
21782177
lua_pushboolean(L, true);
21792178
return 1;
21802179
}

0 commit comments

Comments
 (0)
Please sign in to comment.