From 98a2116c19117684c3e282a10c1b2086d053fc64 Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Mon, 1 Sep 2025 18:10:19 +0800 Subject: [PATCH] =?UTF-8?q?fix(auto=5Frequire):=20=E6=94=B9=E8=BF=9B?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E5=8A=A0=E8=BD=BD=E7=9A=84=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=92=8C=E5=85=A8=E5=B1=80=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改自动加载模块的错误处理逻辑,增加对加载失败情况的打印输出 重构全局环境管理,使用原始require函数并优化metatable处理 --- .../lua/rom/modules/auto_require.lua | 39 +++++++++++++------ .../lua/rom/startup/05_auto_require.lua | 5 ++- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/data/computercraft/lua/rom/modules/auto_require.lua b/data/computercraft/lua/rom/modules/auto_require.lua index 57dfd8c..318def4 100644 --- a/data/computercraft/lua/rom/modules/auto_require.lua +++ b/data/computercraft/lua/rom/modules/auto_require.lua @@ -1,6 +1,7 @@ -- auto_require.lua: Automatically require libraries when accessed -local original_ENV = _ENV +-- Save the original require function +local original_require = require -- List of common libraries to auto-require local common_libs = { @@ -12,7 +13,7 @@ local common_libs = { -- Pre-load common libraries into a cache local lib_cache = {} for _, lib_name in ipairs(common_libs) do - local success, lib = pcall(require, lib_name) + local success, lib = pcall(original_require, lib_name) if success then lib_cache[lib_name] = lib end @@ -20,9 +21,9 @@ end -- Create a new environment with auto-require functionality local auto_env = setmetatable({}, { - __index = function(_, key) - -- Check if the key exists in the original environment - local val = original_ENV[key] + __index = function(_, key) + -- Check if the key exists in the global environment + local val = rawget(_G, key) if val ~= nil then return val end @@ -32,8 +33,8 @@ local auto_env = setmetatable({}, { return lib_cache[key] end - -- Try to require the module - local success, lib = pcall(require, key) + -- Try to require the module using the original require function + local success, lib = pcall(original_require, key) if success then lib_cache[key] = lib return lib @@ -44,9 +45,25 @@ local auto_env = setmetatable({}, { end }) --- Replace the global environment with our auto-require environment -setmetatable(_G, { - __index = auto_env -}) +-- Create a proxy function for require that uses the original require +_G.require = function(modname) + return original_require(modname) +end + +-- Add the auto-require environment to the global metatable +local old_mt = getmetatable(_G) or {} +local old_index = old_mt.__index +old_mt.__index = function(t, key) + -- Try the original index first + if old_index then + local val = old_index(t, key) + if val ~= nil then + return val + end + end + -- Then try our auto-require environment + return auto_env[key] +end +setmetatable(_G, old_mt) return {} \ No newline at end of file diff --git a/data/computercraft/lua/rom/startup/05_auto_require.lua b/data/computercraft/lua/rom/startup/05_auto_require.lua index 7e6c2d8..95ce4fd 100644 --- a/data/computercraft/lua/rom/startup/05_auto_require.lua +++ b/data/computercraft/lua/rom/startup/05_auto_require.lua @@ -1,2 +1,5 @@ -- Load auto_require module to automatically require libraries -require("auto_require") \ No newline at end of file +local success, err = pcall(require, "auto_require") +if not success then + print("Failed to load auto_require module: " .. tostring(err)) +end \ No newline at end of file