移除所有脚本文件的头部注释模板 删除installer.lua中未使用的splitCsv函数 重构静默更新逻辑,改用loadfile替代os.run以正确捕获执行错误 添加更新错误追踪机制,在界面版本指示器处显示失败提示 修复更新成功的判断条件
120 lines
3.2 KiB
Lua
120 lines
3.2 KiB
Lua
|
|
local URL_ERROR = "http://gitea.fse-media.group/Henry_Du/FSE-Ticket.sys/raw/branch/main/error.dfpwm"
|
|
local URL_PASS = "http://gitea.fse-media.group/Henry_Du/FSE-Ticket.sys/raw/branch/main/pass.dfpwm"
|
|
local URL_GATE = "http://gitea.fse-media.group/Henry_Du/FSE-Ticket.sys/raw/branch/main/gate.lua"
|
|
local URL_UPDATE_GATE = "http://gitea.fse-media.group/Henry_Du/FSE-Ticket.sys/raw/branch/main/update_gate.lua"
|
|
|
|
local CONFIG_PATH = "gate_config.json"
|
|
|
|
local function trim(s)
|
|
return (tostring(s or ""):gsub("^%s+", ""):gsub("%s+$", ""))
|
|
end
|
|
|
|
|
|
|
|
|
|
local function writeFile(path, content, binary)
|
|
local mode = binary and "wb" or "w"
|
|
local f = fs.open(path, mode)
|
|
if not f then return false end
|
|
f.write(content)
|
|
f.close()
|
|
return true
|
|
end
|
|
|
|
local function prompt(label)
|
|
term.write(label)
|
|
return trim(read() or "")
|
|
end
|
|
|
|
local function httpGet(url)
|
|
if not http then return false, "HTTP API disabled" end
|
|
local okReq, err = pcall(function()
|
|
http.request({ url = url, method = "GET" })
|
|
end)
|
|
if not okReq then return false, tostring(err) end
|
|
|
|
while true do
|
|
local ev, p1, p2, p3 = os.pullEvent()
|
|
if ev == "http_success" and p1 == url then
|
|
local res = p2
|
|
if type(res) == "table" and type(res.readAll) == "function" then
|
|
local data = res.readAll()
|
|
res.close()
|
|
return true, data
|
|
end
|
|
return false, "invalid http response"
|
|
end
|
|
if ev == "http_failure" and p1 == url then
|
|
local err = p2
|
|
local res = p3
|
|
if type(p2) == "table" and type(p2.readAll) == "function" then
|
|
res = p2
|
|
err = p3
|
|
end
|
|
if type(res) == "table" and type(res.readAll) == "function" then
|
|
local data = res.readAll()
|
|
res.close()
|
|
return false, data
|
|
end
|
|
return false, tostring(err or "http_failure")
|
|
end
|
|
end
|
|
end
|
|
|
|
local function download(url, path, binary)
|
|
print("Downloading: " .. path)
|
|
local ok, data = httpGet(url)
|
|
if not ok then
|
|
print("Download failed: " .. tostring(data or ""))
|
|
return false
|
|
end
|
|
if not writeFile(path, data, binary) then
|
|
print("Write failed: " .. path)
|
|
return false
|
|
end
|
|
return true
|
|
end
|
|
|
|
term.clear()
|
|
term.setCursorPos(1, 1)
|
|
print("Ticket Gate Installer")
|
|
print("")
|
|
|
|
local stationCode = trim(prompt("Station code: "))
|
|
if #stationCode == 0 then
|
|
print("No station code provided.")
|
|
return
|
|
end
|
|
|
|
local modeRaw = prompt("Gate mode (entry/exit): ")
|
|
local mode = (trim(modeRaw):lower() == "exit") and "exit" or "entry"
|
|
local cfg = { mode = mode, station_codes = { stationCode }, station_code = stationCode }
|
|
local okCfg, cfgJson = pcall(textutils.serializeJSON, cfg)
|
|
if not okCfg then
|
|
print("Config serialize failed.")
|
|
return
|
|
end
|
|
if not writeFile(CONFIG_PATH, cfgJson, false) then
|
|
print("Failed to write config.")
|
|
return
|
|
end
|
|
|
|
if not download(URL_PASS, "pass.dfpwm", true) then return end
|
|
if not download(URL_ERROR, "error.dfpwm", true) then return end
|
|
|
|
local okGate, gateCode = httpGet(URL_GATE)
|
|
if not okGate then
|
|
print("Download failed: startup")
|
|
return
|
|
end
|
|
|
|
writeFile("startup.lua", gateCode, false)
|
|
writeFile("startup", gateCode, false)
|
|
if not download(URL_UPDATE_GATE, "update_gate.lua", false) then return end
|
|
|
|
print("")
|
|
print("Done.")
|
|
print("This gate now supports tickets and IC cards.")
|
|
print("Reboot the computer to start the gate.")
|