From 4441be36cd217002c0e7954fa9e618a6e1fe7279 Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Mon, 1 Sep 2025 19:31:08 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E8=87=AA?= =?UTF-8?q?=E5=8A=A8require=E5=8A=9F=E8=83=BD=E5=8F=8A=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 移除auto_require模块及其启动脚本和测试文件 该功能通过修改全局环境实现自动加载库,可能导致不可预期行为 --- .../lua/rom/modules/auto_require.lua | 69 ------------------- .../lua/rom/programs/test_auto_require.lua | 21 ------ .../lua/rom/startup/05_auto_require.lua | 5 -- 3 files changed, 95 deletions(-) delete mode 100644 data/computercraft/lua/rom/modules/auto_require.lua delete mode 100644 data/computercraft/lua/rom/programs/test_auto_require.lua delete mode 100644 data/computercraft/lua/rom/startup/05_auto_require.lua diff --git a/data/computercraft/lua/rom/modules/auto_require.lua b/data/computercraft/lua/rom/modules/auto_require.lua deleted file mode 100644 index 318def4..0000000 --- a/data/computercraft/lua/rom/modules/auto_require.lua +++ /dev/null @@ -1,69 +0,0 @@ --- auto_require.lua: Automatically require libraries when accessed - --- Save the original require function -local original_require = require - --- List of common libraries to auto-require -local common_libs = { - "rc", "fs", "term", "colors", "textutils", "window", - "keys", "peripheral", "redstone", "rs", "monitor", - "modem", "http", "json", "image", "paint", "sound" -} - --- Pre-load common libraries into a cache -local lib_cache = {} -for _, lib_name in ipairs(common_libs) do - local success, lib = pcall(original_require, lib_name) - if success then - lib_cache[lib_name] = lib - end -end - --- Create a new environment with auto-require functionality -local auto_env = setmetatable({}, { - __index = function(_, key) - -- Check if the key exists in the global environment - local val = rawget(_G, key) - if val ~= nil then - return val - end - - -- Check if the key is in our pre-loaded cache - if lib_cache[key] then - return lib_cache[key] - end - - -- 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 - end - - -- If all else fails, return nil - return nil - end -}) - --- 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/programs/test_auto_require.lua b/data/computercraft/lua/rom/programs/test_auto_require.lua deleted file mode 100644 index f981520..0000000 --- a/data/computercraft/lua/rom/programs/test_auto_require.lua +++ /dev/null @@ -1,21 +0,0 @@ --- Test auto_require functionality --- This program doesn't explicitly require 'textutils' - --- Try to use textutils without requiring it first -textutils.coloredPrint(colors.green, "Auto-require test successful!", colors.white) -textutils.printTable({test = "This should work without require"}) - --- Try another library -term.setTextColor(colors.yellow) -print("Using term library without require:") -term.at(1, 3).write("Cursor moved using term library") - --- Test a library that might not be pre-loaded -game = fs.open("/test.txt", "w") -if game then - game.write("Testing fs library") - game.close() - print("File written successfully") -else - print("Failed to open file") -end \ 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 deleted file mode 100644 index 95ce4fd..0000000 --- a/data/computercraft/lua/rom/startup/05_auto_require.lua +++ /dev/null @@ -1,5 +0,0 @@ --- Load auto_require module to automatically require libraries -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