Skip to content

Commit

Permalink
Remove (un)registerModStorage
Browse files Browse the repository at this point in the history
It is no longer necessary to keep just one object per mod, since
data is no longer stored in these objects. Now one can obtain
multiple distinct references with core.get_mod_storage, but this
will be changed by PR minetest#12572.

One can test like so:

    local a = core.get_mod_storage()
    local b = core.get_mod_storage()
    a:set_string("!", "?")
    assert(b:get("!") == "?")
  • Loading branch information
TurkeyMcMac committed Jul 23, 2022
1 parent 5daf5d1 commit 2b0adae
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 69 deletions.
20 changes: 0 additions & 20 deletions src/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1989,26 +1989,6 @@ const std::string* Client::getModFile(std::string filename)
return &it->second;
}

bool Client::registerModStorage(ModMetadata *storage)
{
if (m_mod_storages.find(storage->getModName()) != m_mod_storages.end()) {
errorstream << "Unable to register same mod storage twice. Storage name: "
<< storage->getModName() << std::endl;
return false;
}

m_mod_storages[storage->getModName()] = storage;
return true;
}

void Client::unregisterModStorage(const std::string &name)
{
std::unordered_map<std::string, ModMetadata *>::const_iterator it =
m_mod_storages.find(name);
if (it != m_mod_storages.end())
m_mod_storages.erase(name);
}

/*
* Mod channels
*/
Expand Down
4 changes: 0 additions & 4 deletions src/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,6 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
const std::string* getModFile(std::string filename);
ModMetadataDatabase *getModStorageDatabase() override { return m_mod_storage_database; }

bool registerModStorage(ModMetadata *meta) override;
void unregisterModStorage(const std::string &name) override;

// Migrates away old files-based mod storage if necessary
void migrateModStorage();

Expand Down Expand Up @@ -593,7 +590,6 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef

// Client modding
ClientScripting *m_script = nullptr;
std::unordered_map<std::string, ModMetadata *> m_mod_storages;
ModMetadataDatabase *m_mod_storage_database = nullptr;
float m_mod_storage_save_timer = 10.0f;
std::vector<ModSpec> m_mods;
Expand Down
2 changes: 0 additions & 2 deletions src/gamedef.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ class IGameDef
virtual const std::vector<ModSpec> &getMods() const = 0;
virtual const ModSpec* getModSpec(const std::string &modname) const = 0;
virtual std::string getWorldPath() const { return ""; }
virtual bool registerModStorage(ModMetadata *storage) = 0;
virtual void unregisterModStorage(const std::string &name) = 0;
virtual ModMetadataDatabase *getModStorageDatabase() = 0;

virtual bool joinModChannel(const std::string &channel) = 0;
Expand Down
22 changes: 4 additions & 18 deletions src/script/lua_api/l_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,12 @@ int ModApiStorage::l_get_mod_storage(lua_State *L)
// Note that this is wrapped in Lua, see builtin/common/mod_storage.lua
std::string mod_name = readParam<std::string>(L, 1);

ModMetadata *store = nullptr;

if (IGameDef *gamedef = getGameDef(L)) {
store = new ModMetadata(mod_name, gamedef->getModStorageDatabase());
if (gamedef->registerModStorage(store)) {
StorageRef::create(L, store);
int object = lua_gettop(L);
lua_pushvalue(L, object);
return 1;
}
} else {
if (IGameDef *gamedef = getGameDef(L))
StorageRef::create(L, new ModMetadata(mod_name, gamedef->getModStorageDatabase()));
else {
assert(false); // this should not happen
lua_pushnil(L);
}

delete store;

lua_pushnil(L);
return 1;
}

Expand Down Expand Up @@ -74,9 +63,6 @@ void StorageRef::create(lua_State *L, ModMetadata *object)
int StorageRef::gc_object(lua_State *L)
{
StorageRef *o = *(StorageRef **)(lua_touserdata(L, 1));
// Server side
if (IGameDef *gamedef = getGameDef(L))
gamedef->unregisterModStorage(getobject(o)->getModName());
delete o;
return 0;
}
Expand Down
19 changes: 0 additions & 19 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3888,25 +3888,6 @@ PlayerSAO* Server::emergePlayer(const char *name, session_t peer_id, u16 proto_v
return playersao;
}

bool Server::registerModStorage(ModMetadata *storage)
{
if (m_mod_storages.find(storage->getModName()) != m_mod_storages.end()) {
errorstream << "Unable to register same mod storage twice. Storage name: "
<< storage->getModName() << std::endl;
return false;
}

m_mod_storages[storage->getModName()] = storage;
return true;
}

void Server::unregisterModStorage(const std::string &name)
{
std::unordered_map<std::string, ModMetadata *>::const_iterator it = m_mod_storages.find(name);
if (it != m_mod_storages.end())
m_mod_storages.erase(name);
}

void dedicated_server_loop(Server &server, bool &kill)
{
verbosestream<<"dedicated_server_loop()"<<std::endl;
Expand Down
4 changes: 0 additions & 4 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,6 @@ class Server : public con::PeerHandler, public MapEventReceiver,

void sendDetachedInventories(session_t peer_id, bool incremental);

virtual bool registerModStorage(ModMetadata *storage);
virtual void unregisterModStorage(const std::string &name);

bool joinModChannel(const std::string &channel);
bool leaveModChannel(const std::string &channel);
bool sendModChannelMessage(const std::string &channel, const std::string &message);
Expand Down Expand Up @@ -695,7 +692,6 @@ class Server : public con::PeerHandler, public MapEventReceiver,
s32 m_next_sound_id = 0; // positive values only
s32 nextSoundId();

std::unordered_map<std::string, ModMetadata *> m_mod_storages;
ModMetadataDatabase *m_mod_storage_database = nullptr;
float m_mod_storage_save_timer = 10.0f;

Expand Down
2 changes: 0 additions & 2 deletions src/unittest/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ class TestGameDef : public IGameDef {
return testmodspec;
}
virtual const ModSpec* getModSpec(const std::string &modname) const { return NULL; }
virtual bool registerModStorage(ModMetadata *meta) { return true; }
virtual void unregisterModStorage(const std::string &name) {}
bool joinModChannel(const std::string &channel);
bool leaveModChannel(const std::string &channel);
bool sendModChannelMessage(const std::string &channel, const std::string &message);
Expand Down

0 comments on commit 2b0adae

Please sign in to comment.