feat(tuning): configure tuning for each game copy

This commit is contained in:
N0VAN0M4LY committed 2026-11-29 16:24:21 +08:00
1 parent 61255f5473
commit 893d60854f
15 files changed
+440 -9

No files matched your search

+2 -4
View File
@@ -29,10 +29,8 @@ export interface BattleTuning {
export function readBattleTuning(reader: TuningReader): BattleTuning {
const maxPlayers = reader.int('maxPlayers', { min: 2, max: 64 });
const maxBots = reader.int('maxBots', { min: 1, max: 63 });
if (maxBots >= maxPlayers) {
throw new TuningError(`tuning file battle.jsonc: maxBots (${maxBots}) must be less than maxPlayers (${maxPlayers})`);
}
// Keep one slot for the player when the launcher lowers the session capacity.
const maxBots = Math.min(reader.int('maxBots', { min: 1, max: 63 }), maxPlayers - 1);
const smallTeamsMaxPlayers = reader.int('smallTeamsMaxPlayers', { min: 2, max: 64 });
if (smallTeamsMaxPlayers > maxPlayers) {
throw new TuningError(
+1
View File
@@ -10,4 +10,5 @@ export { editSaveResearch, readSaveResearch } from './saves/save-research.ts';
export { editSaveCurrencies, listSaveSummaries, readSaveDetails } from './saves/save-editor.ts';
export { importOfficialBackup, previewOfficialBackup } from './saves/import/official-backup.ts';
export { prepareTransferredProfile } from './saves/transferred-profile.ts';
export { readLauncherTuning, writeLauncherTuning, type LauncherTuningPatch } from './tuning/launcher-tuning.ts';
export { multiVehiclePackIds } from './store/store-pack-ids.ts';
+116
View File
@@ -0,0 +1,116 @@
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 } };
}
+9 -2
View File
@@ -17,12 +17,19 @@ export interface TuningFiles {
read(name: string): TuningReader;
}
export function tuningFiles(dataFolder: string, report: (line: string) => void): TuningFiles {
const overridesFolder = join(dataFolder, 'tuning');
export function tuningFiles(
dataFolder: string | null,
report: (line: string) => void,
proposed?: ReadonlyMap<string, Record<string, unknown>>,
): TuningFiles {
const overridesFolder = dataFolder === null ? null : join(dataFolder, 'tuning');
return {
read(name: string): TuningReader {
const fileName = `${name}.jsonc`;
const defaults = parseFile(join(DEFAULTS_FOLDER, fileName));
const candidate = proposed?.get(name);
if (candidate) return new TuningReader(fileName, mergeOverride(defaults, candidate));
if (overridesFolder === null) return new TuningReader(fileName, defaults);
const overridePath = join(overridesFolder, fileName);
if (!existsSync(overridePath)) return new TuningReader(fileName, defaults);
report(`tuning ${fileName}: using overrides from ${overridePath}`);