Use the native embedded browser and pass the game language through auto_local. Share web assets and extract vehicle icons, flags, and localized names from the installed client. Load and validate per-domain JSONC tuning and merge local overrides by field. Use official store USD prices for GJN products, keep GJN balances in integer cents, and split the native and web store handlers by protocol. Multi-vehicle packs are still listed as single vehicles at this point. Type checking, patch checks, and configuration loading passed. In-game acceptance is pending.
52 lines
2.1 KiB
TypeScript
52 lines
2.1 KiB
TypeScript
/**
|
|
* Load one tuning file: the defaults shipped in
|
|
* packages/local-service/tuning/<name>.jsonc, with <data folder>/tuning/<name>.jsonc laid
|
|
* over them field by field when the player or a developer put one there. Objects merge key
|
|
* by key; any other value (numbers, texts, lists) in the override replaces the default.
|
|
*/
|
|
import { existsSync, readFileSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { parseJsonc } from './jsonc.ts';
|
|
import { TuningError, TuningReader } from './tuning-reader.ts';
|
|
|
|
const DEFAULTS_FOLDER = fileURLToPath(new URL('../../tuning/', import.meta.url));
|
|
|
|
export interface TuningFiles {
|
|
/** The merged content of <name>.jsonc, ready for typed reads. */
|
|
read(name: string): TuningReader;
|
|
}
|
|
|
|
export function tuningFiles(dataFolder: string, report: (line: string) => void): TuningFiles {
|
|
const overridesFolder = join(dataFolder, 'tuning');
|
|
return {
|
|
read(name: string): TuningReader {
|
|
const fileName = `${name}.jsonc`;
|
|
const defaults = parseFile(join(DEFAULTS_FOLDER, fileName));
|
|
const overridePath = join(overridesFolder, fileName);
|
|
if (!existsSync(overridePath)) return new TuningReader(fileName, defaults);
|
|
report(`tuning ${fileName}: using overrides from ${overridePath}`);
|
|
return new TuningReader(fileName, mergeOverride(defaults, parseFile(overridePath)));
|
|
},
|
|
};
|
|
}
|
|
|
|
function parseFile(path: string): unknown {
|
|
try {
|
|
return parseJsonc(readFileSync(path, 'utf8'));
|
|
} catch (error) {
|
|
throw new TuningError(`tuning file ${path} cannot be read: ${error instanceof Error ? error.message : String(error)}`);
|
|
}
|
|
}
|
|
|
|
function mergeOverride(base: unknown, override: unknown): unknown {
|
|
if (!isObject(base) || !isObject(override)) return override;
|
|
const merged: Record<string, unknown> = { ...base };
|
|
for (const [key, value] of Object.entries(override)) merged[key] = key in base ? mergeOverride(base[key], value) : value;
|
|
return merged;
|
|
}
|
|
|
|
function isObject(value: unknown): value is Record<string, unknown> {
|
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
}
|