Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let core.get_mod_storage be called multiple times #12572

Merged
merged 5 commits into from
Jul 23, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions builtin/client/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
19 changes: 19 additions & 0 deletions builtin/common/mod_storage.lua
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions builtin/game/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 2 additions & 6 deletions src/script/lua_api/l_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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/[...].lua
TurkeyMcMac marked this conversation as resolved.
Show resolved Hide resolved
std::string mod_name = readParam<std::string>(L, 1);
TurkeyMcMac marked this conversation as resolved.
Show resolved Hide resolved

ModMetadata *store = nullptr;

Expand Down