mirror of
https://github.com/CCLeonOS/LeonOS.git
synced 2026-03-03 15:01:12 +00:00
feat: 初始提交 LeonOS 实现
添加 LeonOS 的基本实现,包括: - 核心 API 模块(colors, disk, gps, keys, multishell, parallel, rednet, redstone, settings, vector) - 命令行程序(about, alias, bg, clear, copy, delete, edit, fg, help, list, lua, mkdir, move, paint, peripherals, programs, reboot, set, shutdown, threads) - 系统启动脚本和包管理 - 文档(README.md, LICENSE) - 开发工具(devbin)和更新程序 实现功能: - 完整的线程管理系统 - 兼容 ComputerCraft 的 API 设计 - 改进的 shell 和命令补全系统 - 多标签终端支持 - 设置管理系统
This commit is contained in:
131
data/computercraft/lua/rom/startup/10_package.lua
Normal file
131
data/computercraft/lua/rom/startup/10_package.lua
Normal file
@@ -0,0 +1,131 @@
|
||||
-- package library --
|
||||
|
||||
local rc = ...
|
||||
|
||||
_G.package = {}
|
||||
|
||||
package.config = "/\n;\n?\n!\n-"
|
||||
package.cpath = ""
|
||||
package.path = "/rc/apis/?.lua;/rc/modules/main/?.lua;./lib/?.lua;./lib/?/init.lua;./?.lua;./?/init.lua"
|
||||
|
||||
local function rm(api)
|
||||
local tab = _G[api]
|
||||
_G[api] = nil
|
||||
return tab
|
||||
end
|
||||
|
||||
package.loaded = {
|
||||
_G = _G,
|
||||
os = os,
|
||||
rc = rc,
|
||||
math = math,
|
||||
utf8 = utf8,
|
||||
table = table,
|
||||
debug = debug,
|
||||
bit32 = rawget(_G, "bit32"),
|
||||
string = string,
|
||||
package = package,
|
||||
coroutine = coroutine,
|
||||
|
||||
-- CC-specific ones
|
||||
peripheral = rm("peripheral"),
|
||||
redstone = rm("redstone"),
|
||||
commands = rm("commands"),
|
||||
pocket = rm("pocket"),
|
||||
turtle = rm("turtle"),
|
||||
http = rm("http"),
|
||||
term = rm("term"),
|
||||
fs = rm("fs"),
|
||||
rs = rm("rs"),
|
||||
|
||||
-- LeonOS-PC APIs
|
||||
periphemu = rm("periphemu"),
|
||||
mounter = rm("mounter"),
|
||||
config = rm("config"),
|
||||
|
||||
-- CCEmuX API
|
||||
ccemux = rm("ccemux")
|
||||
}
|
||||
|
||||
package.preload = {}
|
||||
|
||||
package.searchers = {
|
||||
-- check package.preload
|
||||
function(mod)
|
||||
if package.preload[mod] then
|
||||
return package.preload[mod]
|
||||
else
|
||||
return nil, "no field package.preload['" .. mod .. "']"
|
||||
end
|
||||
end,
|
||||
|
||||
-- check for lua library
|
||||
function(mod)
|
||||
local ok, err = package.searchpath(mod, package.path, ".", "/")
|
||||
if not ok then
|
||||
return nil, err
|
||||
end
|
||||
|
||||
local func, loaderr = loadfile(ok)
|
||||
if not func then
|
||||
return nil, loaderr
|
||||
end
|
||||
return func()
|
||||
end,
|
||||
}
|
||||
|
||||
local fs = package.loaded.fs
|
||||
-- require isn't here yet
|
||||
local expect = loadfile("/rc/modules/main/cc/expect.lua")()
|
||||
package.loaded["cc.expect"] = expect
|
||||
|
||||
function package.searchpath(name, path, sep, rep)
|
||||
expect(1, name, "string")
|
||||
expect(2, path, "string")
|
||||
expect(3, sep, "string", "nil")
|
||||
expect(4, rep, "string", "nil")
|
||||
|
||||
sep = "%" .. (sep or ".")
|
||||
rep = rep or "/"
|
||||
|
||||
name = name:gsub(sep, rep)
|
||||
local serr = ""
|
||||
|
||||
for search in path:gmatch("[^;]+") do
|
||||
search = search:gsub("%?", name)
|
||||
|
||||
if fs.exists(search) then
|
||||
return search
|
||||
|
||||
else
|
||||
if #serr > 0 then
|
||||
serr = serr .. "\n "
|
||||
end
|
||||
|
||||
serr = serr .. "no file '" .. search .. "'"
|
||||
end
|
||||
end
|
||||
|
||||
return nil, serr
|
||||
end
|
||||
|
||||
function _G.require(mod)
|
||||
expect(1, mod, "string")
|
||||
|
||||
if package.loaded[mod] then
|
||||
return package.loaded[mod]
|
||||
end
|
||||
|
||||
local serr = "module '" .. mod .. "' not found:"
|
||||
for _, searcher in ipairs(package.searchers) do
|
||||
local result, err = searcher(mod)
|
||||
if result then
|
||||
package.loaded[mod] = result
|
||||
return result
|
||||
else
|
||||
serr = serr .. "\n " .. err
|
||||
end
|
||||
end
|
||||
|
||||
error(serr, 2)
|
||||
end
|
||||
Reference in New Issue
Block a user