aboutsummaryrefslogtreecommitdiff
path: root/builtin/common
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 /builtin/common
parent2351c9561265d4136f78ce3dd73c0c77acfed711 (diff)
downloadminetest-d631f21024bac626babe2e8eac4505210d368ffa.tar.gz
minetest-d631f21024bac626babe2e8eac4505210d368ffa.tar.bz2
minetest-d631f21024bac626babe2e8eac4505210d368ffa.zip
Let core.get_mod_storage be called multiple times (#12572)
Diffstat (limited to 'builtin/common')
-rw-r--r--builtin/common/mod_storage.lua19
1 files changed, 19 insertions, 0 deletions
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