117 lines
4.7 KiB
TypeScript
117 lines
4.7 KiB
TypeScript
import { readFileSync, rmSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import { parseJsonc } from '@wto/jsonc';
|
|
import { readBattleEconomyTuning } from '../battle/battle-economy-tuning.ts';
|
|
import { readBattleTuning } from '../battle/battle-tuning.ts';
|
|
import { readNewProfileTuning } from '../profile/new-profile-tuning.ts';
|
|
import { readResearchTuning } from '../research/research-tuning.ts';
|
|
import { writeFileAtomic } from '../storage/atomic-file.ts';
|
|
import { tuningFiles, type TuningFiles } from './tuning-loader.ts';
|
|
import { TuningError } from './tuning-reader.ts';
|
|
|
|
function values(files: TuningFiles) {
|
|
const battle = readBattleTuning(files.read('battle'));
|
|
const economy = readBattleEconomyTuning(files.read('battle-economy'));
|
|
const newProfile = readNewProfileTuning(files.read('new-profile')).startingBalance;
|
|
const research = readResearchTuning(files.read('research'));
|
|
return {
|
|
rewardMultiplierSilverLions: battle.rewardMultiplierSilverLions,
|
|
rewardMultiplierResearchPoints: battle.rewardMultiplierResearchPoints,
|
|
friendlyKillPenaltyMul: economy.friendlyKillPenaltyMul,
|
|
maxPlayers: battle.maxPlayers,
|
|
smallTeamsMaxPlayers: battle.smallTeamsMaxPlayers,
|
|
brSpreadMrank: battle.brSpreadMrank,
|
|
subModeChance: battle.subModeChance,
|
|
tankBotRespawns: battle.tankBotRespawns,
|
|
airBotRespawns: battle.airBotRespawns,
|
|
strikeUcavSpawnScore: battle.strikeUcavSpawnScore,
|
|
silverLions: newProfile.silverLions,
|
|
goldenEagles: newProfile.goldenEagles,
|
|
preferLineSuccessor: research.preferLineSuccessor,
|
|
autoTargetPick: research.autoTargetPick,
|
|
};
|
|
}
|
|
|
|
type Editable = ReturnType<typeof values>;
|
|
export type LauncherTuningPatch = { [K in keyof Editable]?: Editable[K] | undefined };
|
|
|
|
const fileOf = {
|
|
rewardMultiplierSilverLions: 'battle',
|
|
rewardMultiplierResearchPoints: 'battle',
|
|
friendlyKillPenaltyMul: 'battle-economy',
|
|
maxPlayers: 'battle',
|
|
smallTeamsMaxPlayers: 'battle',
|
|
brSpreadMrank: 'battle',
|
|
subModeChance: 'battle',
|
|
tankBotRespawns: 'battle',
|
|
airBotRespawns: 'battle',
|
|
strikeUcavSpawnScore: 'battle',
|
|
silverLions: 'new-profile',
|
|
goldenEagles: 'new-profile',
|
|
preferLineSuccessor: 'research',
|
|
autoTargetPick: 'research',
|
|
} as const satisfies Record<keyof Editable, string>;
|
|
|
|
interface OverrideFile {
|
|
readonly path: string;
|
|
readonly previous: string | undefined;
|
|
readonly fields: Record<string, unknown>;
|
|
}
|
|
|
|
export function readLauncherTuning(dataFolder: string): { defaults: Editable; effective: Editable } {
|
|
return { defaults: values(tuningFiles(null, () => {})), effective: values(tuningFiles(dataFolder, () => {})) };
|
|
}
|
|
|
|
export function writeLauncherTuning(dataFolder: string, patch: LauncherTuningPatch) {
|
|
const { defaults, effective } = readLauncherTuning(dataFolder);
|
|
const changes = new Map<string, OverrideFile>();
|
|
for (const key of Object.keys(patch) as Array<keyof Editable>) {
|
|
if (!Object.hasOwn(fileOf, key)) throw new TuningError(`Unknown launcher tuning field ${key}`);
|
|
const value = patch[key];
|
|
if (value === undefined || value === effective[key]) continue;
|
|
const name = fileOf[key];
|
|
const file = changes.get(name) ?? readOverride(join(dataFolder, 'tuning', `${name}.jsonc`));
|
|
changes.set(name, file);
|
|
if (value === defaults[key]) delete file.fields[key];
|
|
else file.fields[key] = value;
|
|
}
|
|
|
|
// Validate the complete proposed configuration before writing any file.
|
|
const proposed = new Map([...changes].map(([name, file]) => [name, file.fields]));
|
|
const updated = values(tuningFiles(dataFolder, () => {}, proposed));
|
|
const written: OverrideFile[] = [];
|
|
try {
|
|
for (const file of changes.values()) {
|
|
if (Object.keys(file.fields).length > 0) writeFileAtomic(file.path, JSON.stringify(file.fields, null, 2) + '\n');
|
|
else rmSync(file.path, { force: true });
|
|
written.push(file);
|
|
}
|
|
} catch (error) {
|
|
for (const file of written.reverse()) {
|
|
try {
|
|
if (file.previous === undefined) rmSync(file.path, { force: true });
|
|
else writeFileAtomic(file.path, file.previous);
|
|
} catch (failure) {
|
|
console.error('[tuning] could not restore the previous override:', file.path, failure);
|
|
}
|
|
}
|
|
throw error;
|
|
}
|
|
return { defaults, effective: updated };
|
|
}
|
|
|
|
function readOverride(path: string): OverrideFile {
|
|
let previous: string;
|
|
try {
|
|
previous = readFileSync(path, 'utf8');
|
|
} catch (error) {
|
|
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error;
|
|
return { path, previous: undefined, fields: {} };
|
|
}
|
|
const parsed: unknown = parseJsonc(previous);
|
|
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
throw new TuningError(`Expected an object in ${path}`);
|
|
}
|
|
return { path, previous, fields: { ...parsed } };
|
|
}
|