82 lines
2.3 KiB
Lua
82 lines
2.3 KiB
Lua
local URL_MACHINE_HTTPS = "https://cloud.fse-media.group/d/API/TicketMachine/refillmachine.lua?sign=msZNBgD27qgGTwdeHlUxB25Q58386ZjDqorYurXiKqI=:0"
|
|
local URL_MACHINE_HTTP = "http://cloud.fse-media.group/d/API/TicketMachine/refillmachine.lua?sign=msZNBgD27qgGTwdeHlUxB25Q58386ZjDqorYurXiKqI=:0"
|
|
|
|
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 errMsg = p2
|
|
local res = p3
|
|
if type(p2) == "table" and type(p2.readAll) == "function" then
|
|
res = p2
|
|
errMsg = 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(errMsg or "http_failure")
|
|
end
|
|
end
|
|
end
|
|
|
|
term.clear()
|
|
term.setCursorPos(1, 1)
|
|
print("Refill Machine Updater")
|
|
print("")
|
|
print("Downloading refill machine program...")
|
|
|
|
local ok, code = httpGet(URL_MACHINE_HTTPS)
|
|
if not ok then
|
|
ok, code = httpGet(URL_MACHINE_HTTP)
|
|
end
|
|
if not ok or type(code) ~= "string" or #code == 0 then
|
|
print("Download failed: " .. tostring(code or ""))
|
|
return
|
|
end
|
|
|
|
if not atomicWrite("startup.lua", code, false) then
|
|
print("Write failed: startup.lua")
|
|
return
|
|
end
|
|
atomicWrite("startup", code, false)
|
|
atomicWrite("refillmachine.lua", code, false)
|
|
|
|
print("")
|
|
print("Done.")
|
|
print("Reboot the computer to apply the update.")
|