From eec11a284562e8046a1da4e110afc1c5821312c8 Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Mon, 1 Sep 2025 18:04:19 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E8=87=AA=E5=8A=A8=E5=8A=A0=E8=BD=BD):=20?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=87=AA=E5=8A=A8require=E5=BA=93=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加auto_require模块,当访问未加载的库时自动require 包含测试程序验证自动加载功能 预加载常用库到缓存以提高性能 --- .../lua/rom/modules/auto_require.lua | 52 +++++++++++++++++++ .../lua/rom/programs/test_auto_require.lua | 21 ++++++++ .../lua/rom/startup/05_auto_require.lua | 2 + 3 files changed, 75 insertions(+) create mode 100644 data/computercraft/lua/rom/modules/auto_require.lua create mode 100644 data/computercraft/lua/rom/programs/test_auto_require.lua create 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 new file mode 100644 index 0000000..57dfd8c --- /dev/null +++ b/data/computercraft/lua/rom/modules/auto_require.lua @@ -0,0 +1,52 @@ +-- auto_require.lua: Automatically require libraries when accessed + +local original_ENV = _ENV + +-- 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(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 original environment + local val = original_ENV[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 + local success, lib = pcall(require, key) + if success then + lib_cache[key] = lib + return lib + end + + -- If all else fails, return nil + return nil + end +}) + +-- Replace the global environment with our auto-require environment +setmetatable(_G, { + __index = auto_env +}) + +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 new file mode 100644 index 0000000..f981520 --- /dev/null +++ b/data/computercraft/lua/rom/programs/test_auto_require.lua @@ -0,0 +1,21 @@ +-- 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 new file mode 100644 index 0000000..7e6c2d8 --- /dev/null +++ b/data/computercraft/lua/rom/startup/05_auto_require.lua @@ -0,0 +1,2 @@ +-- Load auto_require module to automatically require libraries +require("auto_require") \ No newline at end of file