From d631f21024bac626babe2e8eac4505210d368ffa Mon Sep 17 00:00:00 2001 From: Jude Melton-Houghton Date: Sat, 23 Jul 2022 16:27:07 -0400 Subject: Let core.get_mod_storage be called multiple times (#12572) --- builtin/common/mod_storage.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 builtin/common/mod_storage.lua (limited to 'builtin/common') 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 -- cgit v1.2.3