diff --git a/packages/build/src/char-pack.ts b/packages/build/src/char-pack.ts index 4662bb8..092890c 100644 --- a/packages/build/src/char-pack.ts +++ b/packages/build/src/char-pack.ts @@ -1,11 +1,13 @@ /** - * Rebuild char.vromfs.bin: config/network.blk pointed at the local service, and the offline - * version's features turned on in config/game_settings.blk. + * Rebuild char.vromfs.bin: config/network.blk pointed at the local service, the offline + * version's features turned on in config/game_settings.blk, and every vehicle shown in the + * tech tree (config/shop.blk). */ import { enableFeaturesForEveryone, GAME_SETTINGS_PATH, OFFLINE_FEATURES } from './features/offline-features.ts'; import type { LocalEndpoint } from './network/local-endpoint.ts'; import { redirectNetworkConfig, type NetworkRedirectReport } from './network/redirect-network-config.ts'; import { editPackBlks } from './pack-edit.ts'; +import { revealHiddenVehicles, SHOP_PATH } from './tech-tree/reveal-hidden-vehicles.ts'; export const NETWORK_CONFIG_PATH = 'config/network.blk'; @@ -14,14 +16,18 @@ export interface CharPackResult { readonly network: NetworkRedirectReport; /** Features that are now on for every player. */ readonly features: readonly string[]; + /** Vehicles that were hidden from players who had not researched or bought them. */ + readonly revealedVehicles: number; } export function rebuildCharPack(stockPack: Uint8Array, endpoint: LocalEndpoint): CharPackResult { let network: NetworkRedirectReport | undefined; let features: string[] = []; + let revealedVehicles = 0; const pack = editPackBlks(stockPack, [ { path: NETWORK_CONFIG_PATH, edit: (root) => { network = redirectNetworkConfig(root, endpoint); } }, { path: GAME_SETTINGS_PATH, edit: (root) => { features = enableFeaturesForEveryone(root, OFFLINE_FEATURES); } }, + { path: SHOP_PATH, edit: (root) => { revealedVehicles = revealHiddenVehicles(root); } }, ]); - return { pack, network: network!, features }; + return { pack, network: network!, features, revealedVehicles }; } diff --git a/packages/build/src/cli.ts b/packages/build/src/cli.ts index b172bc8..bb2a76f 100644 --- a/packages/build/src/cli.ts +++ b/packages/build/src/cli.ts @@ -49,6 +49,7 @@ function main(): number { for (const change of charPack.network.addresses) console.log(` ${change.path}: ${change.before} -> ${change.after}`); } console.log(`game_settings.blk: features on for every player: ${charPack.features.join(', ') || '(already on)'}`); + console.log(`shop.blk: ${charPack.revealedVehicles} hidden vehicles shown in the tech tree`); const mainPack = rebuildMainPack(readFileSync(join(values.game, GAME_FILES.mainPack)), endpoint); console.log(`settings.blk: ${mainPack.addresses.length} address(es) now point to ${endpoint.host}:${endpoint.port}`); diff --git a/packages/build/src/tech-tree/reveal-hidden-vehicles.ts b/packages/build/src/tech-tree/reveal-hidden-vehicles.ts new file mode 100644 index 0000000..b0b3a78 --- /dev/null +++ b/packages/build/src/tech-tree/reveal-hidden-vehicles.ts @@ -0,0 +1,53 @@ +/** + * Show every vehicle in the tech tree, including the ones the live game hides from players + * who have not researched or bought them (local verification: e.g. the German Maus and Panther II). + * + * char.vromfs.bin config/shop.blk: country -> branch -> column -> vehicle (grouped vehicles one + * level deeper). The stock UI hides a vehicle (gui.vromfs.bin scripts/unit/unit.nut + * isVisibleInShop) when its entry has + * showOnlyWhenResearch:b until the player researches it (2.61.0.37: 22 vehicles) + * showOnlyWhenBought:b until the player owns it (208) + * showOnlyWhenAvailableForPurchase:b except while a promotion sells it (74) + * Removing these flags lets the stock rule show them. + * + * Kept hidden: + * - the parts of split vehicles: a vehicle another entry names as its slaveUnit (2.61.0.37: the + * launchers of the 10 split air-defence systems) comes with its master vehicle and is not a + * vehicle of its own in the tech tree (local verification); + * - platform and language variants (showByPlatform, hideByPlatform, hideForLangs, + * hideFeature): they pick one of several versions of the same vehicle. + */ +import type { BlkBlock } from '@wto/blk'; + +export const SHOP_PATH = 'config/shop.blk'; + +const HIDING_FLAGS = new Set(['showOnlyWhenResearch', 'showOnlyWhenBought', 'showOnlyWhenAvailableForPurchase']); +const SLAVE_UNIT = 'slaveUnit'; + +/** Remove the hiding flags in place; returns how many vehicles were hidden before. */ +export function revealHiddenVehicles(shop: BlkBlock): number { + const entries = shopEntries(shop); + const parts = new Set(entries.flatMap((entry) => entry.params.flatMap((param) => (param.name === SLAVE_UNIT && param.value.type === 'string' ? [param.value.value] : [])))); + let revealed = 0; + for (const entry of entries) { + if (parts.has(entry.name)) continue; + const kept = entry.params.filter((param) => !HIDING_FLAGS.has(param.name)); + if (kept.length === entry.params.length) continue; + entry.params = kept; + revealed++; + } + return revealed; +} + +/** Every block below the countries: branches, columns, groups and vehicles. */ +function shopEntries(shop: BlkBlock): BlkBlock[] { + const entries: BlkBlock[] = []; + const walk = (block: BlkBlock): void => { + for (const entry of block.blocks) { + entries.push(entry); + walk(entry); + } + }; + for (const country of shop.blocks) walk(country); + return entries; +} diff --git a/packages/local-service/src/accounts/account-store.ts b/packages/local-service/src/accounts/account-store.ts index f29089f..98cdfd6 100644 --- a/packages/local-service/src/accounts/account-store.ts +++ b/packages/local-service/src/accounts/account-store.ts @@ -56,6 +56,11 @@ export class AccountStore { return this.#file.accounts.find((account) => account.userId === userId); } + /** The account with this login, compared without regard to case. */ + byLogin(login: string): Account | undefined { + return this.#find(login); + } + #find(login: string): Account | undefined { const key = login.toLowerCase(); return this.#file.accounts.find((account) => account.login.toLowerCase() === key); diff --git a/packages/local-service/src/game-data/datasets/shop-branches.ts b/packages/local-service/src/game-data/datasets/shop-branches.ts new file mode 100644 index 0000000..47f1787 --- /dev/null +++ b/packages/local-service/src/game-data/datasets/shop-branches.ts @@ -0,0 +1,18 @@ +/** + * Dataset: the tech-tree branches of each country. + * + * char.vromfs.bin config/shop.blk has one top-level block per country, and under it one block + * per branch: army, helicopters, aviation, ships, boats (2.61.0.37: China, Sweden and Israel + * have no naval branches). + */ +import type { Dataset } from './dataset.ts'; + +const SHOP = 'config/shop.blk'; + +export type ShopBranches = Readonly>; + +export const shopBranches: Dataset = { + id: 'shop-branches', + version: 1, + extract: (source) => Object.fromEntries(source.readBlk('char.vromfs.bin', SHOP).blocks.map((country) => [country.name, country.blocks.map((branch) => branch.name)])), +}; diff --git a/packages/local-service/src/local-service.ts b/packages/local-service/src/local-service.ts index 889124e..2d6e6cb 100644 --- a/packages/local-service/src/local-service.ts +++ b/packages/local-service/src/local-service.ts @@ -10,6 +10,7 @@ import { DownloadableContentService } from './downloadable-content/downloadable- import { DatasetCache } from './game-data/datasets/dataset-cache.ts'; import { multiplayerMissions } from './game-data/datasets/multiplayer-missions.ts'; import { reserveVehicles } from './game-data/datasets/reserve-vehicles.ts'; +import { shopBranches } from './game-data/datasets/shop-branches.ts'; import { shopCountries } from './game-data/datasets/shop-countries.ts'; import { GameFiles } from './game-data/game-files.ts'; import { GameModes } from './game-modes/game-modes.ts'; @@ -46,7 +47,7 @@ export async function startLocalService(options: LocalServiceOptions): Promise max) throw new RangeError(`amount out of range: ${amount}`); +} diff --git a/packages/local-service/src/profile/profile-eras.ts b/packages/local-service/src/profile/profile-eras.ts new file mode 100644 index 0000000..e3bd897 --- /dev/null +++ b/packages/local-service/src/profile/profile-eras.ts @@ -0,0 +1,47 @@ +/** + * The ranks open for research, per country and vehicle type. + * + * eras{ + * Aircraft_country_usa:i=1 + * Tank_country_germany:i=1 + * } + * + * Read and written by the client's profile serializer (aces.exe 2.61.0.37 0x919060, from + * 0x921b86): for each vehicle type it builds the parameter name "_" from the + * table at 0x72b6d58 (Aircraft, Tank, Ship, Helicopter, Boat, Human, Transport) and keeps an + * integer, the highest rank open. The stock tech tree greys out and locks every rank above it + * (gui.vromfs.bin scripts/shop/shop.nut, is_era_available). + * + * The tech-tree branch names of config/shop.blk map to these types as the stock scripts pair + * them (scripts/unit/unitTypesList.nut armyId). + */ +import { ensureBlock, findBlock, findParam, setParam, type BlkBlock } from '@wto/blk'; + +const ERAS = 'eras'; + +const ERA_TYPE_OF_BRANCH: Readonly> = { + aviation: 'Aircraft', + army: 'Tank', + ships: 'Ship', + helicopters: 'Helicopter', + boats: 'Boat', +}; + +/** The rank open for research in a country's branch; 0 when none is. */ +export function openRank(profile: BlkBlock, country: string, branch: string): number { + const eras = findBlock(profile, ERAS); + const value = eras && findParam(eras, eraName(country, branch))?.value; + return value?.type === 'int' ? value.value : 0; +} + +/** Open the branch up to the rank; a higher rank already open stays open. */ +export function openRanksUpTo(profile: BlkBlock, country: string, branch: string, rank: number): void { + if (openRank(profile, country, branch) >= rank) return; + setParam(ensureBlock(profile, ERAS), eraName(country, branch), { type: 'int', value: rank }); +} + +function eraName(country: string, branch: string): string { + const type = ERA_TYPE_OF_BRANCH[branch]; + if (!type) throw new Error(`unknown tech-tree branch: ${branch}`); + return `${type}_${country}`; +} diff --git a/packages/local-service/src/profile/profile-upgrades.ts b/packages/local-service/src/profile/profile-upgrades.ts index 8ca11ee..97729e5 100644 --- a/packages/local-service/src/profile/profile-upgrades.ts +++ b/packages/local-service/src/profile/profile-upgrades.ts @@ -1,14 +1,18 @@ /** - * The save format's upgrade steps (saves/save-upgrades.ts), in version order. A step is - * never changed once released; a later change to saves adds a new step. + * The save format's upgrade steps (saves/save-upgrades.ts), in version order. A later change + * to what saves hold adds a new step; released steps are not removed. */ import { SaveUpgrades } from '../saves/save-upgrades.ts'; import { applyStartingContent, type StartingContent } from './starting-content.ts'; export function profileUpgrades(content: StartingContent): SaveUpgrades { + // Applying the starting content is idempotent, so each step brings the save up to the + // starting content of this release. + const addStartingContent = (profile: Parameters[0]): void => applyStartingContent(profile, content); return new SaveUpgrades([ - // 1 -> 2: the starting content (reserve vehicles of every branch, completed - // difficulty-gating tutorials) for saves made before new accounts had it. - (profile) => applyStartingContent(profile, content), + // 1 -> 2: reserve vehicles of every branch, completed difficulty-gating tutorials. + addStartingContent, + // 2 -> 3: rank I open in every branch. + addStartingContent, ]); } diff --git a/packages/local-service/src/profile/starting-content.ts b/packages/local-service/src/profile/starting-content.ts index 00f901d..713f8dc 100644 --- a/packages/local-service/src/profile/starting-content.ts +++ b/packages/local-service/src/profile/starting-content.ts @@ -1,11 +1,14 @@ /** * What every account starts with besides Gaijin's new-account template - * (config/default_user.blk), which owns only reserve aircraft. + * (config/default_user.blk), which owns only reserve aircraft and opens no rank. * * - Every reserve vehicle of every country and branch (aircraft, ground vehicles, boats, * ships), as accounts have them online. The stock first-choice window and the slotbar * only offer reserve vehicles the player owns (scripts/slotbar/slotbarStateData.nut * checkReserveUnit: isBought), so without them a new player only has aircraft. + * - Rank I open for research in every country's branches (profile-eras.ts), as online; + * without it the whole tech tree is locked (local verification). Higher ranks open through + * research. * - The base tutorials that gate a difficulty count as completed on every difficulty, so no * game mode asks for a tutorial first (user quotes 87, 92; the tutorials stay playable). * The stock rule (gui.vromfs.bin scripts/tutorials/tutorialsState.nut isDiffUnlocked) @@ -16,21 +19,28 @@ * block "training"). * * Applying it again changes nothing, so it also brings older saves up to date - * (profile-upgrades.ts). + * (profile-upgrades.ts). The starting money is only for new accounts (new-profile.ts). */ import type { BlkBlock } from '@wto/blk'; import type { ReserveVehicle } from '../game-data/datasets/reserve-vehicles.ts'; +import type { ShopBranches } from '../game-data/datasets/shop-branches.ts'; +import { openRanksUpTo } from './profile-eras.ts'; import { addOwnedVehicle } from './profile-hangar.ts'; import { recordMissionCompleted } from './profile-mission-progress.ts'; const DIFFICULTY_GATING_TUTORIALS = ['tutorial/tutorialB_takeoff_and_landing'] as const; const HIGHEST_DIFFICULTY = 2; +const FIRST_RANK = 1; export interface StartingContent { readonly reserveVehicles: readonly ReserveVehicle[]; + readonly branches: ShopBranches; } export function applyStartingContent(profile: BlkBlock, content: StartingContent): void { for (const vehicle of content.reserveVehicles) addOwnedVehicle(profile, vehicle.name); + for (const [country, branches] of Object.entries(content.branches)) { + for (const branch of branches) openRanksUpTo(profile, country, branch, FIRST_RANK); + } for (const tutorial of DIFFICULTY_GATING_TUTORIALS) recordMissionCompleted(profile, tutorial, HIGHEST_DIFFICULTY); } diff --git a/packages/local-service/src/save-cli.ts b/packages/local-service/src/save-cli.ts new file mode 100644 index 0000000..c283c16 --- /dev/null +++ b/packages/local-service/src/save-cli.ts @@ -0,0 +1,72 @@ +/** + * Development entry point for editing a save (the save editor of docs/implementation notes + * section A; the launcher will call the same profile functions). + * + * node packages/local-service/src/save-cli.ts --game --data + * --player [--silver ] [--gold ] + * + * Without --silver / --gold it only prints the balance. The edit is written to the save; the + * game shows it after its next profile update (any action, or signing in again). + */ +import { parseArgs } from 'node:util'; +import { AccountStore } from './accounts/account-store.ts'; +import { DatasetCache } from './game-data/datasets/dataset-cache.ts'; +import { reserveVehicles } from './game-data/datasets/reserve-vehicles.ts'; +import { shopBranches } from './game-data/datasets/shop-branches.ts'; +import { GameFiles } from './game-data/game-files.ts'; +import { PlayerProfiles } from './profile/player-profiles.ts'; +import { balanceOf, setGoldenEagles, setSilverLions } from './profile/profile-currency.ts'; +import { profileUpgrades } from './profile/profile-upgrades.ts'; +import type { StartingContent } from './profile/starting-content.ts'; +import { SaveStore } from './saves/save-store.ts'; + +function main(): number { + const { values } = parseArgs({ + options: { + game: { type: 'string' }, + data: { type: 'string' }, + player: { type: 'string' }, + silver: { type: 'string' }, + gold: { type: 'string' }, + }, + }); + if (!values.game || !values.data || !values.player) { + console.error('usage: save-cli.ts --game --data --player [--silver ] [--gold ]'); + return 2; + } + const silver = optionalAmount(values.silver); + const gold = optionalAmount(values.gold); + if (silver === null || gold === null) { + console.error('--silver and --gold take a whole number of 0 or more'); + return 2; + } + + const accounts = new AccountStore(values.data); + const account = accounts.byLogin(values.player) ?? accounts.byUserId(values.player); + if (!account) { + console.error(`no player "${values.player}" in ${values.data}`); + return 1; + } + const gameFiles = new GameFiles(values.game); + const datasets = new DatasetCache(values.data, gameFiles, () => {}); + const startingContent: StartingContent = { reserveVehicles: datasets.load(reserveVehicles), branches: datasets.load(shopBranches) }; + const profiles = new PlayerProfiles(new SaveStore(values.data, profileUpgrades(startingContent)), gameFiles, startingContent); + + const profile = silver === undefined && gold === undefined + ? profiles.profileOf(account.userId) + : profiles.update(account.userId, (saved) => { + if (silver !== undefined) setSilverLions(saved, silver); + if (gold !== undefined) setGoldenEagles(saved, gold); + }); + const balance = balanceOf(profile); + console.log(`${account.login} (${account.userId}): ${balance.silverLions} silver lions, ${balance.goldenEagles} golden eagles`); + return 0; +} + +/** undefined when not given, null when not a valid amount. */ +function optionalAmount(text: string | undefined): number | undefined | null { + if (text === undefined) return undefined; + return /^\d+$/.test(text) && Number.isSafeInteger(Number(text)) ? Number(text) : null; +} + +process.exitCode = main();