2025-09-01 18:04:19 +08:00
|
|
|
-- auto_require.lua: Automatically require libraries when accessed
|
|
|
|
|
|
2025-09-01 18:10:19 +08:00
|
|
|
-- Save the original require function
|
|
|
|
|
local original_require = require
|
2025-09-01 18:04:19 +08:00
|
|
|
|
|
|
|
|
-- 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
|
2025-09-01 18:10:19 +08:00
|
|
|
local success, lib = pcall(original_require, lib_name)
|
2025-09-01 18:04:19 +08:00
|
|
|
if success then
|
|
|
|
|
lib_cache[lib_name] = lib
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Create a new environment with auto-require functionality
|
|
|
|
|
local auto_env = setmetatable({}, {
|
2025-09-01 18:10:19 +08:00
|
|
|
__index = function(_, key)
|
|
|
|
|
-- Check if the key exists in the global environment
|
|
|
|
|
local val = rawget(_G, key)
|
2025-09-01 18:04:19 +08:00
|
|
|
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
|
|
|
|
|
|
2025-09-01 18:10:19 +08:00
|
|
|
-- Try to require the module using the original require function
|
|
|
|
|
local success, lib = pcall(original_require, key)
|
2025-09-01 18:04:19 +08:00
|
|
|
if success then
|
|
|
|
|
lib_cache[key] = lib
|
|
|
|
|
return lib
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- If all else fails, return nil
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
})
|
|
|
|
|
|
2025-09-01 18:10:19 +08:00
|
|
|
-- 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)
|
2025-09-01 18:04:19 +08:00
|
|
|
|
|
|
|
|
return {}
|