feat: simplify OTA updates with timestamp
This commit is contained in:
@@ -127,17 +127,49 @@ local function setLastUpdateVersion(v)
|
||||
if f then f.write(tostring(v)); f.close() end
|
||||
end
|
||||
|
||||
local function runSilentGateUpdate()
|
||||
if not fs.exists("update_gate.lua") then
|
||||
return false, "update_gate.lua not found"
|
||||
local function checkAndApplyUpdate()
|
||||
if type(CFG) ~= 'table' then return end
|
||||
local remoteTs = tonumber(CFG.update_timestamp)
|
||||
|
||||
if not remoteTs or remoteTs <= 0 then return end
|
||||
|
||||
-- Prevent infinite reboot loops: only update once per timestamp
|
||||
local attemptFile = ".last_update_ts"
|
||||
if fs.exists(attemptFile) then
|
||||
local f = fs.open(attemptFile, "r")
|
||||
local tried = tonumber(f and f.readAll() or "0") or 0
|
||||
if f then f.close() end
|
||||
if tried >= remoteTs then
|
||||
return
|
||||
end
|
||||
end
|
||||
local baseEnv = (getfenv and getfenv()) or _ENV or _G
|
||||
local env = setmetatable({ AUTO_UPDATE_SILENT = true }, { __index = baseEnv })
|
||||
|
||||
-- We have a NEW remote timestamp. Trigger update!
|
||||
isUpdating = true
|
||||
pcall(drawReadyScreen)
|
||||
sleep(0.5)
|
||||
|
||||
-- Record that we are attempting this timestamp
|
||||
local f = fs.open(attemptFile, "w")
|
||||
if f then f.write(tostring(remoteTs)); f.close() end
|
||||
|
||||
-- Load and run update script
|
||||
local env = setmetatable({ AUTO_UPDATE_SILENT = true }, { __index = _G or getfenv() or _ENV })
|
||||
local fn, err = loadfile("update_gate.lua", env)
|
||||
if not fn then return false, err end
|
||||
local ok, res = pcall(fn, "--silent")
|
||||
if not ok then return false, tostring(res) end
|
||||
return true, true
|
||||
if fn then
|
||||
local ok, res = pcall(fn, "--silent")
|
||||
if not ok then
|
||||
lastUpdateError = string.sub(tostring(res), 1, 30)
|
||||
end
|
||||
else
|
||||
lastUpdateError = "loadfile failed: " .. tostring(err)
|
||||
end
|
||||
|
||||
isUpdating = false
|
||||
pcall(drawReadyScreen)
|
||||
|
||||
-- Reboot
|
||||
os.reboot()
|
||||
end
|
||||
|
||||
local function setServerConnected(ok)
|
||||
@@ -422,9 +454,10 @@ end
|
||||
local function refreshRemoteLuaVersion(serverBase)
|
||||
serverBase = trim(serverBase or "")
|
||||
if #serverBase == 0 then return false end
|
||||
local url = serverBase:gsub("/+$", "") .. "/api/public/config"
|
||||
local url = serverBase:gsub("/+$", "") .. "/api/config?_t=" .. tostring(nowMs() + math.random(1, 999999))
|
||||
local ok, parsed = getJSON(url)
|
||||
if not ok or type(parsed) ~= "table" then return false end
|
||||
_G.CFG = parsed
|
||||
local remote = normalizeVersionTag(type(parsed.lua_versions) == "table" and parsed.lua_versions.gate or nil)
|
||||
remoteForceUpdate = (parsed.force_update == true)
|
||||
if #remote == 0 then
|
||||
|
||||
Reference in New Issue
Block a user