From 90614006ba34a771abfdf47304e830f8a2521242 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Mon, 16 Aug 2021 18:15:29 +0200 Subject: [PATCH] Minetest module: Remove _G index, add lazy loading --- minetest.lua | 69 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 12 deletions(-) diff --git a/minetest.lua b/minetest.lua index b35951d..fdb9ae2 100644 --- a/minetest.lua +++ b/minetest.lua @@ -1,14 +1,59 @@ -local _ENV = setmetatable({}, {__index = _G}) -local function load(filename) - assert(loadfile(modlib.mod.get_resource(modlib.modname, "minetest", filename .. ".lua")))(_ENV) +local _ENV = {} + +local components = {} +for _, value in pairs{ + "luon", + "raycast", + "schematic", + "colorspec" +} do + components[value] = value end --- minetest/gametime.lua is forceloaded in init.lua -load"misc" -load"collisionboxes" -load"liquid" -load"raycast" -load"wielditem_change" -load"colorspec" -load"schematic" -load"luon" +for filename, comps in pairs{ + misc = { + "max_wear", + "override", + "register_globalstep", + "form_listeners", + "register_form_listener", + "texture_modifier_inventorycube", + "get_node_inventory_image", + "check_player_privs", + "decode_base64", + "objects_inside_radius", + "objects_inside_area", + "nodename_matcher" + }, + liquid = { + "liquid_level_max", + "get_liquid_corner_levels", + "flowing_downwards", + "get_liquid_flow_direction" + }, + wielditem_change = { + "players", + "registered_on_wielditem_changes", + "register_on_wielditem_change" + }, + colorspec = { + "named_colors", + "colorspec_to_colorstring" + }, + collisionboxes = { + "get_node_collisionboxes" + } +} do + for _, component in pairs(comps) do + components[component] = filename + end +end + +setmetatable(_ENV, {__index = function(_ENV, name) + local filename = components[name] + if filename then + assert(loadfile(modlib.mod.get_resource(modlib.modname, "minetest", filename .. ".lua")))(_ENV) + return rawget(_ENV, name) + end +end}) + return _ENV \ No newline at end of file