/** * Per-player cloud settings persistence inside `/saves//cloud-settings/` *. Keeping cloud settings inside each player's save directory means * copying `saves//` to another machine or fresh client folder carries all game * options, keybinds, HUD settings, presets, and map preferences along automatically. */ import { existsSync, readFileSync } from 'node:fs'; import { join } from 'node:path'; import { decodeBlk, emptyBlock, encodeBlk, ensureBlock, setParam, type BlkBlock } from '@wto/blk'; import { writeFileAtomic } from '../storage/atomic-file.ts'; import { GENERAL_BLK_TYPES, SUPPORTED_CLOUD_COMPRESSIONS, type ParsedGblkUpload, } from './cloud-settings-codec.ts'; interface StoredBlockMeta { version: number; contentHash: string; updatedAt: string; } interface CloudSettingsMetaFile { formatVersion: 1; blocks: Record; } export class CloudSettingsStore { readonly #savesRoot: string; constructor(dataFolder: string) { this.#savesRoot = join(dataFolder, 'saves'); } /** * Build the `generalBlkMeta` / `cln_get_meta_blk` DataBlock (aces.exe 0x4665cf4..0x4666900). * Every registered `blkType` is marked `perMachine:b=no` in `__schema` so settings travel * with the player's save across machines without hardware-ID partitioning. */ buildMetaBlk(userId?: string, blockName?: string): BlkBlock { const meta = blockName ? emptyBlock(blockName) : emptyBlock(); setParam(meta, 'machineHash', { type: 'string', value: `hhh${userId ?? '0'}` }); setParam(meta, 'compressionSupported', { type: 'string', value: SUPPORTED_CLOUD_COMPRESSIONS }); const schema = ensureBlock(meta, '__schema'); const saved = userId && isValidUserId(userId) ? this.#loadMeta(userId).blocks : {}; const allTypes = new Set([...GENERAL_BLK_TYPES, ...Object.keys(saved)]); for (const blkType of allTypes) { const schemaEntry = ensureBlock(schema, blkType); setParam(schemaEntry, 'perMachine', { type: 'bool', value: false }); const entry = saved[blkType]; if (entry && entry.version > 0) { const typeBlock = ensureBlock(meta, blkType); setParam(typeBlock, 'version', { type: 'int', value: entry.version }); setParam(typeBlock, 'contentHash', { type: 'string', value: entry.contentHash }); } } return meta; } /** Load a stored `ContainerBlk` (`version:i` + `content { ... }`) for `cln_get_general_blk`. */ loadContainer(userId: string, blkType: string): BlkBlock | undefined { if (!isValidUserId(userId) || !isValidBlkType(blkType)) return undefined; const blkPath = this.#blkPath(userId, blkType); if (!existsSync(blkPath)) return undefined; try { return decodeBlk(readFileSync(blkPath)); } catch { return undefined; } } /** Persist a single uploaded `ContainerBlk` and update `meta.json` atomically. */ saveUpload(userId: string, upload: ParsedGblkUpload): boolean { if (!isValidUserId(userId) || !isValidBlkType(upload.blkType)) return false; const meta = this.#loadMeta(userId); const nextVersion = Math.max(1, Math.trunc(upload.version)); setParam(upload.container, 'version', { type: 'int', value: nextVersion }); writeFileAtomic(this.#blkPath(userId, upload.blkType), encodeBlk(upload.container)); meta.blocks[upload.blkType] = { version: nextVersion, contentHash: upload.contentHash, updatedAt: new Date().toISOString(), }; writeFileAtomic(this.#metaPath(userId), JSON.stringify(meta, null, 2)); return true; } /** Persist a batch of uploads from `cln_bulk_set_gblk`. */ saveBulkUploads(userId: string, uploads: ReadonlyArray): number { let savedCount = 0; for (const upload of uploads) { if (this.saveUpload(userId, upload)) savedCount += 1; } return savedCount; } #loadMeta(userId: string): CloudSettingsMetaFile { const metaPath = this.#metaPath(userId); if (!existsSync(metaPath)) return { formatVersion: 1, blocks: {} }; try { const parsed = JSON.parse(readFileSync(metaPath, 'utf8')) as Partial; return { formatVersion: 1, blocks: parsed.blocks && typeof parsed.blocks === 'object' ? { ...parsed.blocks } : {}, }; } catch { return { formatVersion: 1, blocks: {} }; } } #folderOf(userId: string): string { return join(this.#savesRoot, userId, 'cloud-settings'); } #metaPath(userId: string): string { return join(this.#folderOf(userId), 'meta.json'); } #blkPath(userId: string, blkType: string): string { return join(this.#folderOf(userId), `${blkType}.blk`); } } function isValidUserId(userId: string): boolean { return /^\d+$/.test(userId); } function isValidBlkType(blkType: string): boolean { return /^[A-Za-z0-9_.-]{1,80}$/.test(blkType); }