refactor(repo)!: move apps, shared packages, and patch definitions
Everything sat in packages/: two programs next to the small libraries
they use, and the patch data loose at the root. The programs now live in
apps/ and the shared libraries in packages/:
apps/server the local server (was packages/local-service),
package @wto/server
packages/patcher builds the game copy (was packages/build),
package @wto/patcher
patches/exe exe patch definitions (was exe-patches)
patches/gui GUI script patches (was gui-patches)
Inside the server, the network layer folder becomes transport/ so it is
not server/server/, the entry module is server.ts, and startLocalService
and its types become startServer, ServerOptions and RunningServer. The
health answer names the app wto-server, and the TLS files in the data
folder are server.key and server.crt (made again on the next start).
Comments, logs and tool texts say "local server".
The patcher no longer reaches into the server's folder. It read the
default tuning/store.jsonc by a relative path to register bundle GUIDs
for the multi-vehicle packs, which also ignored the player's overrides
in <data>/tuning/. It now takes the pack ids from its caller; the
server exports multiVehiclePackIds(dataFolder), which reads them the way
the running server does. rebuildGuiPack takes the patch folder from its
caller as well.
The development build moves out of the patcher to
scripts/build-game-copy.ts, since it uses both the server and the
patcher. It needs --data (the folder the server will run with) and
defaults the patch folders and the hook library to this repository.
Checked with the root typecheck, check-patches, check-gui,
check-offsets, the native build, a run of build-game-copy.ts into a
scratch folder (12 exe patches, 35 GUI patches, 496 bundle GUIDs as
before) and a server start from apps/server answering wto-server.
BREAKING CHANGE: Use @wto/server and @wto/patcher from the new workspace paths. Run the development build with scripts/build-game-copy.ts and pass --data.
This commit is contained in:
1 parent
9c813ccf7d
commit
930ea07630
476 files changed
+275
-251
No files matched your search
@@ -0,0 +1,186 @@
|
||||
/**
|
||||
* Per-player cloud settings persistence inside `<data>/saves/<userId>/cloud-settings/`
|
||||
*. Keeping cloud settings inside each player's save directory means copying `saves/<userId>/`
|
||||
* to another machine or fresh client folder carries all game options, keybinds, HUD settings,
|
||||
* presets, and map preferences along automatically.
|
||||
*
|
||||
* When an account has no saved `GBT_GENERAL_SETTINGS.blk` yet, version 1 is seeded once with
|
||||
* `hudShowFuel = 2` and `hudShowAmmo = 2` ("Always", `aces.exe` 0xf4cc1f..0xf4cc55) so the
|
||||
* top-left ground/air weapon and fuel HUD indicators are visible out of the box without
|
||||
* overriding any subsequent change the player makes in the in-game Options menu.
|
||||
*/
|
||||
import { createHash } from 'node:crypto';
|
||||
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';
|
||||
|
||||
/**
|
||||
* The three GeneralBlk types accepted by `PlayerProfile::applyGeneralBlk` (0xf5afb0) and
|
||||
* `PlayerProfile::storeGeneralBlk` (0xf5b080).
|
||||
*/
|
||||
export const GENERAL_BLK_TYPES = [
|
||||
'GBT_GENERAL_STORAGE',
|
||||
'GBT_GENERAL_SETTINGS',
|
||||
'GBT_GENERAL_MACHINE_SETTINGS',
|
||||
] as const;
|
||||
|
||||
/**
|
||||
* Compression methods advertised in `cln_get_meta_blk`'s `compressionSupported`. Put `zstd`
|
||||
* first so `online_storage::Context::selectCompression` (0x4665de1) selects pure Zstandard
|
||||
* frames (0x3372ca0), which Node's built-in `node:zlib` unpacks directly.
|
||||
*/
|
||||
export const SUPPORTED_CLOUD_COMPRESSIONS = 'zstd;zlib;gzip';
|
||||
|
||||
const SUPPORTED_BLK_TYPE_SET: ReadonlySet<string> = new Set(GENERAL_BLK_TYPES);
|
||||
const HUD_SHOW_ALWAYS = 2;
|
||||
|
||||
export interface ParsedGblkUpload {
|
||||
readonly blkType: string;
|
||||
readonly version: number;
|
||||
readonly contentHash: string;
|
||||
readonly container: BlkBlock;
|
||||
}
|
||||
|
||||
interface StoredBlockMeta {
|
||||
version: number;
|
||||
contentHash: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
interface CloudSettingsMetaFile {
|
||||
formatVersion: 1;
|
||||
blocks: Record<string, StoredBlockMeta>;
|
||||
}
|
||||
|
||||
export class CloudSettingsStore {
|
||||
readonly #savesRoot: string;
|
||||
|
||||
constructor(dataFolder: string) {
|
||||
this.#savesRoot = join(dataFolder, 'saves');
|
||||
}
|
||||
|
||||
buildMetaBlk(userId?: string, blockName?: string): BlkBlock {
|
||||
if (userId && isValidUserId(userId)) this.#ensureDefaultGeneralSettings(userId);
|
||||
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<string>([...GENERAL_BLK_TYPES, ...Object.keys(saved).filter(isSupportedGeneralBlkType)]);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
loadContainer(userId: string, blkType: string): BlkBlock | undefined {
|
||||
if (!isValidUserId(userId) || !isSupportedGeneralBlkType(blkType)) return undefined;
|
||||
if (blkType === 'GBT_GENERAL_SETTINGS') this.#ensureDefaultGeneralSettings(userId);
|
||||
const blkPath = this.#blkPath(userId, blkType);
|
||||
if (!existsSync(blkPath)) return undefined;
|
||||
try {
|
||||
return decodeBlk(readFileSync(blkPath));
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
saveUpload(userId: string, upload: ParsedGblkUpload): boolean {
|
||||
if (!isValidUserId(userId) || !isSupportedGeneralBlkType(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;
|
||||
}
|
||||
|
||||
saveBulkUploads(userId: string, uploads: ReadonlyArray<ParsedGblkUpload>): number {
|
||||
let savedCount = 0;
|
||||
for (const upload of uploads) {
|
||||
if (this.saveUpload(userId, upload)) savedCount += 1;
|
||||
}
|
||||
return savedCount;
|
||||
}
|
||||
|
||||
#ensureDefaultGeneralSettings(userId: string): void {
|
||||
const blkPath = this.#blkPath(userId, 'GBT_GENERAL_SETTINGS');
|
||||
if (existsSync(blkPath)) return;
|
||||
const { container, contentHash } = buildDefaultGeneralSettings();
|
||||
this.saveUpload(userId, {
|
||||
blkType: 'GBT_GENERAL_SETTINGS',
|
||||
version: 1,
|
||||
contentHash,
|
||||
container,
|
||||
});
|
||||
}
|
||||
|
||||
#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<CloudSettingsMetaFile>;
|
||||
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 buildDefaultGeneralSettings(): { readonly container: BlkBlock; readonly contentHash: string } {
|
||||
const content = emptyBlock('content');
|
||||
const profile = ensureBlock(content, 'profile');
|
||||
const settings = ensureBlock(profile, 'settings');
|
||||
setParam(settings, 'hudShowFuel', { type: 'int', value: HUD_SHOW_ALWAYS });
|
||||
setParam(settings, 'hudShowAmmo', { type: 'int', value: HUD_SHOW_ALWAYS });
|
||||
|
||||
const hashRoot = emptyBlock();
|
||||
hashRoot.params.push(...content.params);
|
||||
hashRoot.blocks.push(...content.blocks);
|
||||
const contentHash = createHash('md5').update(encodeBlk(hashRoot)).digest('hex');
|
||||
|
||||
const container = emptyBlock();
|
||||
setParam(container, 'version', { type: 'int', value: 1 });
|
||||
container.blocks.push(content);
|
||||
return { container, contentHash };
|
||||
}
|
||||
|
||||
function isValidUserId(userId: string): boolean {
|
||||
return /^\d+$/.test(userId);
|
||||
}
|
||||
|
||||
export function isSupportedGeneralBlkType(blkType: string): boolean {
|
||||
return SUPPORTED_BLK_TYPE_SET.has(blkType);
|
||||
}
|
||||
Reference in new issue
Block a user