Files
FSE-Ticket.sys/install_ticketmachine.lua
T
Henry_Du 446a8b0d09 feat(自动更新): 添加持久化版本追踪避免重复更新
新增读写本地版本记录文件的工具函数,调整自动更新触发逻辑仅在版本不一致时触发,更新成功后写入版本号;修复更新失败版本号的赋值问题,同时为安装脚本添加标准文件头注释
2026-07-11 00:09:43 +08:00

99 lines
2.9 KiB
Lua

--[[
Author: HenryDu8133 813367384@qq.com
Date: 2026-06-28 12:58:42
LastEditors: HenryDu8133 813367384@qq.com
LastEditTime: 2026-07-11 00:02:33
FilePath: \TicketMachine\install_ticketmachine.lua
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
--]]
local URL_MACHINE_HTTP = "http://gitea.fse-media.group/Henry_Du/FSE-Ticket.sys/raw/branch/main/ticketmachine.lua"
local URL_UPDATE_MACHINE = "http://gitea.fse-media.group/Henry_Du/FSE-Ticket.sys/raw/branch/main/update_machine.lua"
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 atomicWrite(path, content, binary)
local tmp = path .. ".new"
if fs.exists(tmp) then fs.delete(tmp) end
if not writeFile(tmp, content, binary) then return false end
if fs.exists(path) then fs.delete(path) end
fs.move(tmp, path)
return true
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
term.clear()
term.setCursorPos(1, 1)
print("Ticket Machine Installer")
print("")
print("Downloading ticket machine program...")
local ok, code = httpGet(URL_MACHINE_HTTP)
if not ok or type(code) ~= "string" or #code == 0 then
print("Download failed: " .. tostring(code or ""))
return
end
local okUpdate, updateCode = httpGet(URL_UPDATE_MACHINE)
if not okUpdate or type(updateCode) ~= "string" or #updateCode == 0 then
print("Download failed: " .. tostring(updateCode or ""))
return
end
if not atomicWrite("startup", code, false) then
print("Write failed: startup")
return
end
atomicWrite("startup.lua", code, false)
if fs.exists("ticketmachine.lua") then atomicWrite("ticketmachine.lua", code, false) end
if not atomicWrite("update_machine.lua", updateCode, false) then
print("Write failed: update_machine.lua")
return
end
print("")
print("Done.")
print("Reboot the computer to start the ticket machine.")