mod_storage.lua 535 B

12345678910111213141516171819
  1. -- Modify core.get_mod_storage to return the storage for the current mod.
  2. local get_current_modname = core.get_current_modname
  3. local old_get_mod_storage = core.get_mod_storage
  4. local storages = setmetatable({}, {
  5. __mode = "v", -- values are weak references (can be garbage-collected)
  6. __index = function(self, modname)
  7. local storage = old_get_mod_storage(modname)
  8. self[modname] = storage
  9. return storage
  10. end,
  11. })
  12. function core.get_mod_storage()
  13. local modname = get_current_modname()
  14. return modname and storages[modname]
  15. end