初始
This commit is contained in:
7
sospanel/config.cfg
Normal file
7
sospanel/config.cfg
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
rednet_ports = {
|
||||
response = 102,
|
||||
control = 101,
|
||||
},
|
||||
name = "区域1",
|
||||
}
|
||||
122
sospanel/startup.lua
Normal file
122
sospanel/startup.lua
Normal file
@@ -0,0 +1,122 @@
|
||||
printUtf8 = load(http.get("https://git.liulikeji.cn/xingluo/ComputerCraft-Utf8/raw/branch/main/utf8ptrint.lua").readAll())()
|
||||
|
||||
f = fs.open("config","rb")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
-- 简易空间控制端
|
||||
-- 功能: 通过配置文件读取参数,检测按键并发送切换命令
|
||||
|
||||
-- 配置文件名称
|
||||
local CONFIG_FILE = "config.cfg"
|
||||
|
||||
-- 读取配置文件
|
||||
local function readConfig()
|
||||
if not fs.exists(CONFIG_FILE) then
|
||||
-- 创建默认配置文件
|
||||
local defaultConfig = {
|
||||
rednet_ports = {
|
||||
control = 101, -- 控制信道
|
||||
response = 102 -- 响应信道
|
||||
},
|
||||
name = "默认空间" -- 空间名称
|
||||
}
|
||||
|
||||
local file = fs.open(CONFIG_FILE, "w")
|
||||
file.write(textutils.serialize(defaultConfig))
|
||||
file.close()
|
||||
|
||||
print("已创建默认配置文件,请编辑 " .. CONFIG_FILE)
|
||||
return defaultConfig
|
||||
end
|
||||
|
||||
local file = fs.open(CONFIG_FILE, "r")
|
||||
local content = file.readAll()
|
||||
file.close()
|
||||
|
||||
local config = textutils.unserialize(content)
|
||||
if not config then
|
||||
error("配置文件格式错误!")
|
||||
end
|
||||
|
||||
-- 验证必要字段
|
||||
if not config.name then
|
||||
error("配置文件中缺少 'name' 字段")
|
||||
end
|
||||
|
||||
if not config.rednet_ports then
|
||||
config.rednet_ports = {
|
||||
control = 101,
|
||||
response = 102
|
||||
}
|
||||
else
|
||||
config.rednet_ports.control = config.rednet_ports.control or 101
|
||||
config.rednet_ports.response = config.rednet_ports.response or 102
|
||||
end
|
||||
|
||||
return config
|
||||
end
|
||||
|
||||
-- 初始化红网
|
||||
local function initRednet()
|
||||
local sides = {"left", "right", "front", "back", "top", "bottom"}
|
||||
local modem = nil
|
||||
|
||||
for _, side in ipairs(sides) do
|
||||
if peripheral.getType(side) == "modem" then
|
||||
modem = peripheral.wrap(side)
|
||||
print("找到网卡在: " .. side)
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if not modem then
|
||||
error("未找到可用的网卡!")
|
||||
end
|
||||
|
||||
return modem
|
||||
end
|
||||
|
||||
-- 发送切换命令
|
||||
local function sendSwitchCommand(diskName, modem, controlPort)
|
||||
local message = {
|
||||
type = "switch_disk",
|
||||
disk_name = diskName
|
||||
}
|
||||
|
||||
modem.transmit(controlPort, controlPort, message)
|
||||
end
|
||||
|
||||
-- 主程序
|
||||
local function main()
|
||||
-- 读取配置
|
||||
local config = readConfig()
|
||||
|
||||
-- 初始化红网
|
||||
local modem = initRednet()
|
||||
|
||||
-- 显示提示信息
|
||||
term.clear()
|
||||
term.setCursorPos(1, 1)
|
||||
|
||||
printUtf8("当前空间: "..config.name,colors.white,colors.black)
|
||||
printUtf8("如果你被困在此空间中,请按下任意键",colors.white,colors.black)
|
||||
printUtf8("我将放出此空间",colors.white,colors.black)
|
||||
|
||||
|
||||
-- 检测按键
|
||||
while true do
|
||||
local event, key = os.pullEvent("key")
|
||||
|
||||
-- 发送切换命令
|
||||
sendSwitchCommand(config.name, modem, config.rednet_ports.control)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
-- 运行程序
|
||||
main()
|
||||
Reference in New Issue
Block a user