diff --git a/CloudServer/server.js b/CloudServer/server.js index 2cf440f..efc4936 100644 --- a/CloudServer/server.js +++ b/CloudServer/server.js @@ -259,11 +259,20 @@ function maybeSendStationUpdate(ws, stationName, stationVersion) { } } -async function fetchGiteaVersion() { +async function fetchGiteaScript() { try { const response = await fetch('https://gitea.fse-media.group/Henry_Du/CC-StationBroadcast/raw/branch/main/station_broadcast.lua'); if (!response.ok) return null; - const text = await response.text(); + return await response.text(); + } catch (e) { + return null; + } +} + +async function fetchGiteaVersion() { + try { + const text = await fetchGiteaScript(); + if (!text) return null; const match = text.match(/local\s+APP_VERSION_TEXT\s*=\s*["']([^"']+)["']/); return match ? String(match[1]).trim() : null; } catch (e) { @@ -272,10 +281,22 @@ async function fetchGiteaVersion() { } async function publishStationUpdate(req) { - let version = await fetchGiteaVersion(); + let version = null; + let giteaText = await fetchGiteaScript(); + if (giteaText) { + const match = giteaText.match(/local\s+APP_VERSION_TEXT\s*=\s*["']([^"']+)["']/); + version = match ? String(match[1]).trim() : null; + } if (!version) { version = getLocalStationScriptVersion() || `build-${Date.now()}`; log('WARN', `Failed to fetch Gitea version, falling back to local version: ${version}`); + } else if (giteaText) { + try { + fs.writeFileSync(stationScriptPath, giteaText, 'utf8'); + log('INFO', `Synced station_broadcast.lua from Gitea to local (version ${version})`); + } catch (e) { + log('ERROR', `Failed to sync station_broadcast.lua to local: ${e.message}`); + } } const releaseId = String(Date.now()); diff --git a/station_broadcast.lua b/station_broadcast.lua index c9bcb76..d52a197 100644 --- a/station_broadcast.lua +++ b/station_broadcast.lua @@ -2231,6 +2231,18 @@ function AutoUpdate.apply(info, reason) return false, err end + local downloadedVersion = nil + do + local m = string.match(body, 'local%s+APP_VERSION_TEXT%s*=%s*["\']([^"\']+)["\']') + if m then downloadedVersion = Utils.trim(m) end + end + + if downloadedVersion and downloadedVersion ~= targetVersion then + State.updateInProgress = false + Utils.log("ERROR", "Downloaded version mismatch: target=" .. tostring(targetVersion) .. " got=" .. tostring(downloadedVersion) .. " - aborting update to prevent reboot loop") + return false, "downloaded version mismatch (" .. tostring(downloadedVersion) .. " ~= " .. tostring(targetVersion) .. ")" + end + local ok, writeErr = AutoUpdate.writeFile(startupPath, body) if not ok then State.updateInProgress = false