"use strict"; // The installed client's update state, shared by the launcher's own "update ready" dialog (updater.cjs) and the game's // "update required" dialog (renderer ClientUpdateRequired.jsx -> IPC desktop:updater:request-required-update -> // recovered/src/electron/khaos/updateAdapter.js -> globalThis.__khaosUpdater.requestInstall()). // // Phases: idle (no check yet), checking, none (the feed has nothing newer), downloading, downloaded, installing (final: // the installer is running and the app is quitting), failed (check, download or verification failed). // // requestInstall() is what "update now" and the countdown call. It never answers "unavailable" for an update that is // downloaded or on its way: downloaded -> install at once; checking / downloading -> wait, then install; none / failed // / idle -> one fresh check (Auth may know a release this process has not seen: published after it started), then // install what it found. Only a fresh check that finds nothing answers "unavailable"; a failure throws (the dialog then // says to close and relaunch Khaos). Once installing, every later call answers "restarting". const BUSY = new Set(["checking", "downloading"]); const PHASES = new Set(["idle", "checking", "none", "downloading", "downloaded", "installing", "failed"]); const RESTARTING = Object.freeze({ action: "restarting" }); // Khaos (owner 2026-11-04, ticket "Imposible de faire la maj"): a player on 2026.11.3.1 sat on "Redémarrage du jeu pour // installer la mise à jour…" for good. "installing" was final: when the install did not take (electron-updater refused // it, or the app never quit), every later click answered "restarting" and nothing ran again. Now install() throwing // fails the attempt, and an install that has not closed the app after stallMs fails too (onStall lets the updater // forget it), so the next click checks, downloads and installs again. function createUpdateFlow({ install, recheck = null, waitMs = 180000, stallMs = 30000, onStall = () => {}, log = () => {}, setTimer = setTimeout, clearTimer = clearTimeout } = {}) { if (typeof install !== "function") throw new TypeError("createUpdateFlow needs install()"); let phase = "idle"; let detail = null; let installWanted = false; let waiters = []; const settle = () => { const ready = waiters; waiters = []; for (const resolve of ready) resolve(true); }; /** Resolves true when the phase leaves checking / downloading, false after waitMs. */ const settled = () => new Promise((resolve) => { if (!BUSY.has(phase)) return resolve(true); const timer = setTimer(() => { waiters = waiters.filter((w) => w !== done); resolve(false); }, waitMs); const done = (value) => { clearTimer(timer); resolve(value); }; waiters.push(done); }); function startInstall() { if (phase === "installing") return; phase = "installing"; detail = null; settle(); log("updates: installing now (quitAndInstall, the game restarts by itself)"); try { install(); } catch (e) { phase = "failed"; installWanted = false; detail = (e && e.message) || String(e); log(`updates: install failed (${detail})`); throw e; } const attempt = {}; current = attempt; const timer = setTimer(() => { if (current !== attempt || phase !== "installing") return; phase = "failed"; installWanted = false; detail = `the app was still running ${Math.round(stallMs / 1000)} s after the install started`; log(`updates: install stalled (${detail})`); try { onStall(); } catch (e) { log(`updates: ${e && e.message}`); } }, stallMs); timer?.unref?.(); } let current = null; function set(next, info = null) { if (!PHASES.has(next) || next === "installing") throw new Error(`unknown update phase ${next}`); // Installing is final (the app is quitting), unless electron-updater reports the install itself failed. if (phase === "installing" && next !== "failed") return; phase = next; detail = info; if (!BUSY.has(next)) settle(); if (next === "downloaded" && installWanted) startInstall(); } async function runCheck() { if (!recheck) return; try { await recheck(); } catch (e) { set("failed", (e && e.message) || String(e)); } // A check that returned without saying anything is no update. if (phase === "idle" || phase === "checking") set("none"); } async function requestInstall() { installWanted = true; let checked = false; for (;;) { if (phase === "installing") return RESTARTING; if (phase === "downloaded") { startInstall(); return RESTARTING; } if (BUSY.has(phase)) { if (!(await settled())) { installWanted = false; throw new Error(`the update is still ${phase} after ${Math.round(waitMs / 1000)} s`); } continue; } if (!checked && recheck) { checked = true; await runCheck(); continue; } installWanted = false; if (phase === "failed") throw new Error(detail || "the update failed"); return { action: "unavailable" }; } } return { set, requestInstall, /** The launcher's own dialog skips itself when the game already asked for the install. */ get installWanted() { return installWanted; }, getState: () => ({ phase, detail }), }; } // ---------------------------------------------------------------- install marker (Windows) // The silent NSIS installer replaces the app's files after the client quits. A client launched during those seconds // starts on half-replaced files and crashes (2026-10-16 19:26:16Z: GPU, network and renderer processes gone, "Cannot // find module ... builder-util-runtime"). Before the installer runs, the client writes update-installing.json // ({ version, at }) next to its logs folder; the next start (player.cjs, first thing) reads it with installGuard. const fs = require("fs"); const path = require("path"); /** An install younger than this is assumed to be still running (a silent install takes well under a minute). */ const INSTALL_WINDOW_MS = 3 * 60 * 1000; /** %LOCALAPPDATA%\\update-installing.json (the logs folder is %LOCALAPPDATA%\\logs). */ function installMarkerPath(logDir) { return path.join(path.dirname(logDir), "update-installing.json"); } function writeInstallMarker(file, version, log = () => {}) { if (!file) return; try { fs.writeFileSync(file, JSON.stringify({ version, at: Date.now() })); } catch (e) { log(`updates: could not write the install marker (${e.message})`); } } /** * What a starting client does about the marker: "installing" (an install of another version started less than * INSTALL_WINDOW_MS ago: tell the player to wait, do not start) or "start" (no marker; the install finished, so this * is the new version; or it is too old to trust). Every "start" removes the marker. */ function installGuard(file, runningVersion, now = Date.now()) { let marker = null; try { marker = JSON.parse(fs.readFileSync(file, "utf8")); } catch { return { action: "start" }; } const age = now - Number(marker && marker.at); if (marker && typeof marker.version === "string" && marker.version !== runningVersion && age >= 0 && age < INSTALL_WINDOW_MS) { return { action: "installing", version: marker.version, waitMs: INSTALL_WINDOW_MS - age }; } try { fs.rmSync(file, { force: true }); } catch { /* read-only profile: the window above still expires it */ } return { action: "start" }; } /** An install that did not start or did not take: the next start must not wait for it (installGuard). */ function dropInstallMarker(file) { if (!file) return; try { fs.rmSync(file, { force: true }); } catch { /* the next start's guard only waits INSTALL_WINDOW_MS anyway */ } } // ---------------------------------------------------------------- visible install (Windows) // electron-updater ran the NSIS installer with /S: no window at all while it removes the installed version and then // extracts the new one, 20 to 100 s on players' PCs (production Auth tables, 2026-11-11). Players who saw the game // close and nothing else clicked the shortcut (its target is gone during that gap: Windows offers to delete it), // shut the PC down in the middle or ran an old setup file: "le dernier patch delete le launcher". Without /S the // one-click installer shows its progress window ("installing, please wait") and tells Windows not to shut down // meanwhile, and its failures become message boxes instead of an installer that dies unseen. // // A visible one-click install starts the app when it is done. That is wanted after "update now" (--force-run), not // after an update installed because the player quit the game: NO_RUN_FLAG tells the installer to end without starting // it (client/release/installer.nsh customInstall; an installer built before 2026-11-11 ignores the flag and starts the // game once more). /** Installer flag: finish without starting the game (release/installer.nsh reads it by this name without the dashes). */ const NO_RUN_FLAG = "--khaos-no-run"; /** electron-updater's installer arguments, made visible: no /S, and NO_RUN_FLAG unless the game must restart. */ function visibleInstallArgs(args) { const out = (Array.isArray(args) ? args : []).filter((arg) => arg !== "/S"); if (!out.includes("--force-run") && !out.includes(NO_RUN_FLAG)) out.push(NO_RUN_FLAG); return out; } module.exports = { createUpdateFlow, installMarkerPath, writeInstallMarker, dropInstallMarker, installGuard, INSTALL_WINDOW_MS, NO_RUN_FLAG, visibleInstallArgs };