// Khaos: the game data folder the launcher hands to the client's main (launcher/main.cjs, docs/LAUNCH.md section 1). // // The client reads its folder from /game-data-root.json (GameDataLocation, recovered/src/electron/khaos). // - Development runs (no KHAOS_GAME_DATA_SETUP): the folder they were given is created and saved, as always. // - An installed client (KHAOS_GAME_DATA_SETUP=1, launcher/player.cjs): the client decides at every start whether the // player gets the folder step (no folder chosen yet, a first download that never finished, a drive too small for // what is left; never a complete install), so the request (globalThis.__khaosGameDataSetup) is always passed. A // saved folder is kept as player.cjs read it (it drops a stale folder of an older build); nothing is created here, // so nothing lands in the default folder before the player's choice. const fs = require("node:fs"); const path = require("node:path"); const write = (files, file, root) => files.writeFileSync(file, JSON.stringify({ root }, null, 2)); /** * Prepares game-data-root.json and returns the client's folder step request ({ defaultRoot, folders }) or null. * userData the profile folder * root the folder player.cjs resolved (saved choice, else the default) or the development folder * setup KHAOS_GAME_DATA_SETUP=1 * defaultRoot the installed client's default folder (KHAOS_GAME_DATA_DEFAULT), else `root` * folders test folders replacing the drive scan (development test runs of the step), or null */ function prepareGameDataRoot({ userData, root, setup, defaultRoot = null, folders = null, files = fs }) { const rootFile = path.join(userData, "game-data-root.json"); root = path.resolve(root); if (!setup) { files.mkdirSync(root, { recursive: true }); write(files, rootFile, root); return null; } let saved = null; try { saved = JSON.parse(files.readFileSync(rootFile, "utf8"))?.root ?? null; } catch { /* first start (or an unreadable file): the client's step decides */ } if (saved !== null && (typeof saved !== "string" || !path.isAbsolute(saved) || path.resolve(saved) !== root)) { try { write(files, rootFile, root); } catch { /* a read-only profile: the client reads the file as it is */ } } return { defaultRoot: path.resolve(defaultRoot || root), folders: Array.isArray(folders) ? folders : null }; } module.exports = { prepareGameDataRoot };