aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJude Melton-Houghton <jwmhjwmh@gmail.com>2022-07-23 16:27:07 -0400
committerGitHub <noreply@github.com>2022-07-23 22:27:07 +0200
commitd631f21024bac626babe2e8eac4505210d368ffa (patch)
treee6f929193cf2cb7751543fe2a6950a13d4798d98
parent2351c9561265d4136f78ce3dd73c0c77acfed711 (diff)
downloadhax-minetest-server-d631f21024bac626babe2e8eac4505210d368ffa.tar.gz
hax-minetest-server-d631f21024bac626babe2e8eac4505210d368ffa.zip
Let core.get_mod_storage be called multiple times (#12572)
-rw-r--r--builtin/client/init.lua1
-rw-r--r--builtin/common/mod_storage.lua19
-rw-r--r--builtin/game/init.lua1
-rw-r--r--src/script/lua_api/l_storage.cpp8
4 files changed, 23 insertions, 6 deletions
diff --git a/builtin/client/init.lua b/builtin/client/init.lua
index 589fe8f24..0133fc16d 100644
--- a/builtin/client/init.lua
+++ b/builtin/client/init.lua
@@ -5,6 +5,7 @@ local commonpath = scriptpath.."common"..DIR_DELIM
dofile(clientpath .. "register.lua")
dofile(commonpath .. "after.lua")
+dofile(commonpath .. "mod_storage.lua")
dofile(commonpath .. "chatcommands.lua")
dofile(clientpath .. "chatcommands.lua")
dofile(clientpath .. "death_formspec.lua")
diff --git a/builtin/common/mod_storage.lua b/builtin/common/mod_storage.lua
new file mode 100644
index 000000000..7ccf62900
--- /dev/null
+++ b/builtin/common/mod_storage.lua
@@ -0,0 +1,19 @@
+-- Modify core.get_mod_storage to return the storage for the current mod.
+
+local get_current_modname = core.get_current_modname
+
+local old_get_mod_storage = core.get_mod_storage
+
+local storages = setmetatable({}, {
+ __mode = "v", -- values are weak references (can be garbage-collected)
+ __index = function(self, modname)
+ local storage = old_get_mod_storage(modname)
+ self[modname] = storage
+ return storage
+ end,
+})
+
+function core.get_mod_storage()
+ local modname = get_current_modname()
+ return modname and storages[modname]
+end
diff --git a/builtin/game/init.lua b/builtin/game/init.lua
index 68d6a10f8..d7606f357 100644
--- a/builtin/game/init.lua
+++ b/builtin/game/init.lua
@@ -17,6 +17,7 @@ if core.settings:get_bool("profiler.load") then
end
dofile(commonpath .. "after.lua")
+dofile(commonpath .. "mod_storage.lua")
dofile(gamepath .. "item_entity.lua")
dofile(gamepath .. "deprecated.lua")
dofile(gamepath .. "misc_s.lua")
diff --git a/src/script/lua_api/l_storage.cpp b/src/script/lua_api/l_storage.cpp
index b8f4347a8..b6c53e353 100644
--- a/src/script/lua_api/l_storage.cpp
+++ b/src/script/lua_api/l_storage.cpp
@@ -25,12 +25,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
int ModApiStorage::l_get_mod_storage(lua_State *L)
{
- lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
- if (!lua_isstring(L, -1)) {
- return 0;
- }
-
- std::string mod_name = readParam<std::string>(L, -1);
+ // 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;