diff options
Diffstat (limited to 'builtin')
-rw-r--r-- | builtin/async/game.lua | 7 | ||||
-rw-r--r-- | builtin/common/item_s.lua (renamed from builtin/game/item_s.lua) | 46 | ||||
-rw-r--r-- | builtin/game/init.lua | 4 |
3 files changed, 55 insertions, 2 deletions
diff --git a/builtin/async/game.lua b/builtin/async/game.lua index 6512f0706..0b7a7ef0e 100644 --- a/builtin/async/game.lua +++ b/builtin/async/game.lua @@ -13,9 +13,12 @@ end -- Import a bunch of individual files from builtin/game/ local gamepath = core.get_builtin_path() .. "game" .. DIR_DELIM +local commonpath = core.get_builtin_path() .. "common" .. DIR_DELIM + +local builtin_shared = {} dofile(gamepath .. "constants.lua") -dofile(gamepath .. "item_s.lua") +assert(loadfile(commonpath .. "item_s.lua"))(builtin_shared) dofile(gamepath .. "misc_s.lua") dofile(gamepath .. "features.lua") dofile(gamepath .. "voxelarea.lua") @@ -57,3 +60,5 @@ setmetatable(core.registered_items, alias_metatable) setmetatable(core.registered_nodes, alias_metatable) setmetatable(core.registered_craftitems, alias_metatable) setmetatable(core.registered_tools, alias_metatable) + +builtin_shared.cache_content_ids() diff --git a/builtin/game/item_s.lua b/builtin/common/item_s.lua index ac1090d63..6e4269b7d 100644 --- a/builtin/game/item_s.lua +++ b/builtin/common/item_s.lua @@ -4,6 +4,8 @@ -- Server or writable access to IGameDef on the engine side. -- (The '_s' stands for standalone.) +local builtin_shared = ... + -- -- Item definition helpers -- @@ -177,3 +179,47 @@ function core.strip_param2_color(param2, paramtype2) -- paramtype2 == "color" requires no modification. return param2 end + +-- Content ID caching + +local old_get_content_id = core.get_content_id +local old_get_name_from_content_id = core.get_name_from_content_id + +local name2content = setmetatable({}, { + __index = function(self, name) + return old_get_content_id(name) + end, +}) + +local content2name = setmetatable({}, { + __index = function(self, id) + return old_get_name_from_content_id(id) + end, +}) + +function core.get_content_id(name) + return name2content[name] +end + +function core.get_name_from_content_id(id) + return content2name[id] +end + +-- Cache content IDs after they have stopped changing. +function builtin_shared.cache_content_ids() + for name in pairs(core.registered_nodes) do + local id = old_get_content_id(name) + name2content[name] = id + content2name[id] = name + end + -- unknown is not in the registered node list. + local unknown_name = old_get_name_from_content_id(core.CONTENT_UNKNOWN) + name2content[unknown_name] = core.CONTENT_UNKNOWN + content2name[core.CONTENT_UNKNOWN] = unknown_name + + for name in pairs(core.registered_aliases) do + if core.registered_nodes[name] then + name2content[name] = old_get_content_id(name) + end + end +end diff --git a/builtin/game/init.lua b/builtin/game/init.lua index d7606f357..b9d2c0baf 100644 --- a/builtin/game/init.lua +++ b/builtin/game/init.lua @@ -8,7 +8,7 @@ local gamepath = scriptpath .. "game".. DIR_DELIM local builtin_shared = {} dofile(gamepath .. "constants.lua") -dofile(gamepath .. "item_s.lua") +assert(loadfile(commonpath .. "item_s.lua"))(builtin_shared) assert(loadfile(gamepath .. "item.lua"))(builtin_shared) dofile(gamepath .. "register.lua") @@ -37,4 +37,6 @@ dofile(gamepath .. "statbars.lua") dofile(gamepath .. "knockback.lua") dofile(gamepath .. "async.lua") +core.after(0, builtin_shared.cache_content_ids) + profiler = nil |