feat(mods): compose shared tech tree insertions
Accept manifest declarations for inserting vehicles into an existing research link. Compose shared links in mod load order after ordinary overlays and write matching shop and economy prerequisites. Validate anchors, rank order, and cycles, and document the manifest field. Verified type checks, a 2.61.0.64 build with shared, folded, and cross-column insertions, load-order and disable readback, and the local server health endpoint. In-game acceptance remains pending.
This commit is contained in:
1 parent
0843fde6ff
commit
f86ab99c17
11 files changed
+313
-10
No files matched your search
@@ -109,7 +109,8 @@ export function inspectModContent(rootDir: string, manifest: ModManifest): ModCo
|
||||
: undefined;
|
||||
|
||||
const badges = computeRiskBadges({
|
||||
hasData: serverTuningFiles.length > 0 || packOperations.length > 0 || hasClientAddon || clientFiles.length > 0 || configBlkPatch !== undefined,
|
||||
hasData: serverTuningFiles.length > 0 || packOperations.length > 0 || hasClientAddon || clientFiles.length > 0
|
||||
|| configBlkPatch !== undefined || !!manifest.techTreeInsertions?.length,
|
||||
hasGui: guiPatchDirs.length > 0,
|
||||
hasScript: serverEntry !== undefined,
|
||||
hasExe: exePatchDirs.length > 0,
|
||||
@@ -270,6 +271,7 @@ export function touchedClientPacks(mods: readonly DiscoveredMod[]): ModdableClie
|
||||
const set = new Set<ModdableClientPack>();
|
||||
for (const mod of mods) {
|
||||
for (const op of mod.content.packOperations) set.add(op.pack);
|
||||
if (mod.manifest.techTreeInsertions?.length) set.add('char.vromfs.bin');
|
||||
if (mod.content.guiPatchDirs.length > 0) set.add('gui.vromfs.bin');
|
||||
if (mod.content.hasClientAddon) set.add('aces.vromfs.bin');
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ export {
|
||||
type ModPreset,
|
||||
type ModRiskBadge,
|
||||
type ModServerConfig,
|
||||
type ModTechTreeInsertion,
|
||||
type ModdableClientPack,
|
||||
type PackFileAction,
|
||||
type ResolvedModLoadOrder,
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { parseJsonc } from '@wto/jsonc';
|
||||
import { MOD_FORMAT_VERSION, type LocalizedText, type ModManifest, type ModServerConfig } from './types.ts';
|
||||
import { MOD_FORMAT_VERSION, type LocalizedText, type ModManifest, type ModServerConfig, type ModTechTreeInsertion } from './types.ts';
|
||||
|
||||
export const MOD_MANIFEST_FILE = 'mod.jsonc';
|
||||
|
||||
@@ -22,9 +22,11 @@ const ALLOWED_ROOT_KEYS = new Set([
|
||||
'conflicts',
|
||||
'load_after',
|
||||
'load_before',
|
||||
'tech_tree_insertions',
|
||||
'server',
|
||||
]);
|
||||
const ALLOWED_SERVER_KEYS = new Set(['entry', 'tuning']);
|
||||
const TECH_TREE_INSERTION_KEYS = new Set(['vehicle', 'after', 'before']);
|
||||
|
||||
export class ModManifestError extends Error {
|
||||
override name = 'ModManifestError';
|
||||
@@ -62,6 +64,7 @@ export function parseModManifest(raw: unknown, where = MOD_MANIFEST_FILE): ModMa
|
||||
const conflicts = optionalIdList(obj.conflicts, `${where} "conflicts"`, id);
|
||||
const loadAfter = optionalIdList(obj.load_after, `${where} "load_after"`, id);
|
||||
const loadBefore = optionalIdList(obj.load_before, `${where} "load_before"`, id);
|
||||
const techTreeInsertions = parseTechTreeInsertions(obj.tech_tree_insertions, `${where} "tech_tree_insertions"`);
|
||||
const server = obj.server === undefined ? undefined : parseServerConfig(obj.server, `${where} "server"`);
|
||||
|
||||
return {
|
||||
@@ -77,10 +80,31 @@ export function parseModManifest(raw: unknown, where = MOD_MANIFEST_FILE): ModMa
|
||||
conflicts,
|
||||
loadAfter,
|
||||
loadBefore,
|
||||
...(techTreeInsertions.length > 0 ? { techTreeInsertions } : {}),
|
||||
...(server !== undefined ? { server } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
function parseTechTreeInsertions(raw: unknown, where: string): ModTechTreeInsertion[] {
|
||||
if (raw === undefined) return [];
|
||||
if (!Array.isArray(raw)) throw new ModManifestError(`${where}: expected an array`);
|
||||
const seen = new Set<string>();
|
||||
return raw.map((item, index) => {
|
||||
const location = `${where}[${index}]`;
|
||||
const value = expectObject(item, location);
|
||||
rejectUnknownKeys(value, TECH_TREE_INSERTION_KEYS, location);
|
||||
const vehicle = expectString(value.vehicle, `${location}.vehicle`);
|
||||
const after = expectString(value.after, `${location}.after`);
|
||||
const before = expectString(value.before, `${location}.before`);
|
||||
if (new Set([vehicle, after, before]).size !== 3) {
|
||||
throw new ModManifestError(`${location}: vehicle, after, and before must be different vehicles`);
|
||||
}
|
||||
if (seen.has(vehicle)) throw new ModManifestError(`${location}: duplicate insertion for "${vehicle}"`);
|
||||
seen.add(vehicle);
|
||||
return { vehicle, after, before };
|
||||
});
|
||||
}
|
||||
|
||||
function parseServerConfig(raw: unknown, where: string): ModServerConfig {
|
||||
const obj = expectObject(raw, where);
|
||||
rejectUnknownKeys(obj, ALLOWED_SERVER_KEYS, where);
|
||||
|
||||
@@ -33,6 +33,12 @@ export interface ModServerConfig {
|
||||
readonly tuning?: string;
|
||||
}
|
||||
|
||||
export interface ModTechTreeInsertion {
|
||||
readonly vehicle: string;
|
||||
readonly after: string;
|
||||
readonly before: string;
|
||||
}
|
||||
|
||||
export interface ModManifest {
|
||||
readonly format: 1;
|
||||
readonly id: string;
|
||||
@@ -46,6 +52,7 @@ export interface ModManifest {
|
||||
readonly conflicts: readonly string[];
|
||||
readonly loadAfter: readonly string[];
|
||||
readonly loadBefore: readonly string[];
|
||||
readonly techTreeInsertions?: readonly ModTechTreeInsertion[];
|
||||
readonly server?: ModServerConfig;
|
||||
}
|
||||
|
||||
|
||||
@@ -226,6 +226,9 @@ export function computeModsBuildFingerprint(
|
||||
hash.update(`port=${port};salt=${extraSalt}\n`);
|
||||
for (const mod of mods) {
|
||||
hash.update(`mod=${mod.manifest.id}@${mod.manifest.version}\n`);
|
||||
if (mod.manifest.techTreeInsertions?.length) {
|
||||
hash.update(`tech_tree_insertions=${JSON.stringify(mod.manifest.techTreeInsertions)}\n`);
|
||||
}
|
||||
const filesToStamp = [
|
||||
...mod.content.packOperations.map((op) => op.sourceRelativePath),
|
||||
...mod.content.guiPatchDirs,
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
*/
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { decodeBlk, encodeBlk, isBinaryBlk, parseBlkText, parseSharedNameMap } from '@wto/blk';
|
||||
import { decodeBlk, encodeBlk, isBinaryBlk, parseBlkText, parseSharedNameMap, type SharedNameContext } from '@wto/blk';
|
||||
import {
|
||||
applyBlkPatch,
|
||||
applyBlkTextPatch,
|
||||
@@ -18,6 +18,8 @@ import {
|
||||
type ModdableClientPack,
|
||||
} from '@wto/mods';
|
||||
import { getFile, NAME_MAP_PATH, putFile, readArchive, writeArchive, type Archive } from '@wto/vromfs';
|
||||
import { composeTechTreeInsertions, VEHICLE_ECONOMY_PATH } from '../tech-tree/compose-insertions.ts';
|
||||
import { SHOP_PATH } from '../tech-tree/reveal-hidden-vehicles.ts';
|
||||
|
||||
export interface PackOverlayReport {
|
||||
readonly pack: ModdableClientPack;
|
||||
@@ -35,7 +37,10 @@ export function applyModPackOverlays(
|
||||
ops: mod.content.packOperations.filter((op) => op.pack === packName),
|
||||
}))
|
||||
.filter((entry) => entry.ops.length > 0);
|
||||
const totalOps = relevantMods.reduce((sum, entry) => sum + entry.ops.length, 0);
|
||||
const insertions = packName === 'char.vromfs.bin'
|
||||
? mods.flatMap(({ manifest }) => (manifest.techTreeInsertions ?? []).map((entry) => ({ ...entry, modId: manifest.id })))
|
||||
: [];
|
||||
const totalOps = relevantMods.reduce((sum, entry) => sum + entry.ops.length, insertions.length);
|
||||
if (totalOps === 0) {
|
||||
return { pack: basePack, report: { pack: packName, operationsApplied: 0 } };
|
||||
}
|
||||
@@ -74,10 +79,7 @@ export function applyModPackOverlays(
|
||||
if (!resolvedPath) {
|
||||
throw new Error(`${where}: target BLK "${op.targetPath}" not found in ${packName}`);
|
||||
}
|
||||
const raw = getFile(archive, resolvedPath);
|
||||
const root = isBinaryBlk(raw)
|
||||
? decodeBlk(raw, { names, dictionary })
|
||||
: parseBlkText(Buffer.from(raw).toString('utf8'));
|
||||
const root = readOverlayBlk(archive, resolvedPath, { names, dictionary });
|
||||
const patchText = readFileSync(sourcePath, 'utf8');
|
||||
if (op.sourceRelativePath.toLowerCase().endsWith('.patch.blk')) {
|
||||
applyBlkTextPatch(root, patchText, {
|
||||
@@ -123,12 +125,25 @@ export function applyModPackOverlays(
|
||||
}
|
||||
}
|
||||
|
||||
if (insertions.length > 0) {
|
||||
const shop = readOverlayBlk(archive, SHOP_PATH, { names, dictionary });
|
||||
const economy = readOverlayBlk(archive, VEHICLE_ECONOMY_PATH, { names, dictionary });
|
||||
composeTechTreeInsertions(shop, economy, insertions);
|
||||
putFile(archive, SHOP_PATH, encodeBlk(shop));
|
||||
putFile(archive, VEHICLE_ECONOMY_PATH, encodeBlk(economy));
|
||||
}
|
||||
|
||||
return {
|
||||
pack: writeArchive(archive),
|
||||
report: { pack: packName, operationsApplied: totalOps },
|
||||
};
|
||||
}
|
||||
|
||||
function readOverlayBlk(archive: Archive, path: string, shared: SharedNameContext) {
|
||||
const raw = getFile(archive, path);
|
||||
return isBinaryBlk(raw) ? decodeBlk(raw, shared) : parseBlkText(Buffer.from(raw).toString('utf8'));
|
||||
}
|
||||
|
||||
function createArchiveIncludeResolver(params: {
|
||||
readonly archive: Archive;
|
||||
readonly names: readonly string[];
|
||||
|
||||
@@ -0,0 +1,221 @@
|
||||
/** Compose opt-in research links after the normal BLK overlays have supplied the vehicles. */
|
||||
import { emptyBlock, findParam, setParam, type BlkBlock } from '@wto/blk';
|
||||
import type { ModTechTreeInsertion } from '@wto/mods';
|
||||
|
||||
export const VEHICLE_ECONOMY_PATH = 'config/wpcost.blk';
|
||||
|
||||
export interface RequestedTechTreeInsertion extends ModTechTreeInsertion {
|
||||
readonly modId: string;
|
||||
}
|
||||
|
||||
interface ShopPosition {
|
||||
readonly entry: BlkBlock;
|
||||
readonly page: BlkBlock;
|
||||
readonly country: string;
|
||||
readonly column: BlkBlock;
|
||||
readonly group?: BlkBlock;
|
||||
}
|
||||
|
||||
interface InsertionPlan {
|
||||
readonly destination: BlkBlock;
|
||||
readonly anchor: BlkBlock;
|
||||
readonly successor: ShopPosition;
|
||||
readonly updateGroupLink: boolean;
|
||||
readonly entries: readonly { entry: BlkBlock; from: ShopPosition | undefined; rank: number; previous: string }[];
|
||||
readonly lastVehicle: string;
|
||||
}
|
||||
|
||||
export function composeTechTreeInsertions(
|
||||
shop: BlkBlock,
|
||||
economy: BlkBlock,
|
||||
requests: readonly RequestedTechTreeInsertion[],
|
||||
): void {
|
||||
const catalog = indexByName(economy.blocks);
|
||||
const positions = indexShop(shop);
|
||||
const owners = new Map<string, string>();
|
||||
const bySuccessor = new Map<string, RequestedTechTreeInsertion[]>();
|
||||
for (const request of requests) {
|
||||
const owner = owners.get(request.vehicle);
|
||||
if (owner !== undefined) throw invalid(request, `"${request.vehicle}" is already inserted by mod "${owner}"`);
|
||||
owners.set(request.vehicle, request.modId);
|
||||
addToIndex(bySuccessor, request.before, request);
|
||||
}
|
||||
|
||||
const links = new Map<string, { previous: string; request: RequestedTechTreeInsertion }>();
|
||||
const plans: InsertionPlan[] = [];
|
||||
for (const group of bySuccessor.values()) {
|
||||
const first = group[0]!;
|
||||
for (const request of group) {
|
||||
if (owners.has(request.after) || owners.has(request.before)) {
|
||||
throw invalid(request, 'insertion anchors must belong to the existing line');
|
||||
}
|
||||
if (request.after !== first.after) throw invalid(request, `mod "${first.modId}" names a different predecessor of "${request.before}"`);
|
||||
}
|
||||
const plan = planInsertion(catalog, positions, group);
|
||||
for (const [index, entry] of plan.entries.entries()) {
|
||||
links.set(entry.entry.name, { previous: entry.previous, request: group[index]! });
|
||||
}
|
||||
links.set(first.before, { previous: plan.lastVehicle, request: first });
|
||||
plans.push(plan);
|
||||
}
|
||||
validateAcyclicLinks(catalog, links);
|
||||
|
||||
for (const [name, { previous, request }] of links) {
|
||||
setParam(requireOne(catalog, name, VEHICLE_ECONOMY_PATH, request), 'reqAir', { type: 'string', value: previous });
|
||||
}
|
||||
for (const plan of plans) applyPlacement(plan);
|
||||
}
|
||||
|
||||
function planInsertion(
|
||||
catalog: ReadonlyMap<string, BlkBlock[]>,
|
||||
positions: ReadonlyMap<string, ShopPosition[]>,
|
||||
requests: readonly RequestedTechTreeInsertion[],
|
||||
): InsertionPlan {
|
||||
const first = requests[0]!;
|
||||
const predecessor = requireOne(positions, first.after, 'config/shop.blk', first);
|
||||
const successor = requireOne(positions, first.before, 'config/shop.blk', first);
|
||||
if (predecessor.page !== successor.page) throw invalid(first, 'the two anchors must belong to the same country and branch');
|
||||
const after = requireOne(catalog, first.after, VEHICLE_ECONOMY_PATH, first);
|
||||
const before = requireOne(catalog, first.before, VEHICLE_ECONOMY_PATH, first);
|
||||
const declaredPredecessor = textParam(before, 'reqAir');
|
||||
if (declaredPredecessor !== first.after) {
|
||||
throw invalid(first, `"${first.before}" requires ${declaredPredecessor ? `"${declaredPredecessor}"` : 'no vehicle'} in ${VEHICLE_ECONOMY_PATH}`);
|
||||
}
|
||||
const withinGroup = successor.group !== undefined && predecessor.group === successor.group;
|
||||
const anchor = withinGroup ? successor.entry : successor.group ?? successor.entry;
|
||||
if (successor.group && !withinGroup && successor.group.blocks[0] !== successor.entry) {
|
||||
throw invalid(first, 'the link enters a non-leading variant of another folder');
|
||||
}
|
||||
validateShopPredecessor(anchor, first);
|
||||
const maxRank = researchRank(before, successor, successor.country, first);
|
||||
let lastRank = researchRank(after, predecessor, successor.country, first);
|
||||
let lastVehicle = first.after;
|
||||
|
||||
const entries = requests.map((request) => {
|
||||
const source = requireOne(catalog, request.vehicle, VEHICLE_ECONOMY_PATH, request);
|
||||
const from = optionalOne(positions, request.vehicle, 'config/shop.blk', request);
|
||||
if (from && from.page !== successor.page) throw invalid(request, `"${request.vehicle}" is placed in a different country or branch`);
|
||||
if (from?.group && (!withinGroup || from.group !== successor.group)) {
|
||||
throw invalid(request, `"${request.vehicle}" is already inside a different folder`);
|
||||
}
|
||||
const rank = researchRank(source, from, successor.country, request);
|
||||
if (rank < lastRank || rank > maxRank) {
|
||||
throw invalid(request, `rank ${rank} must be between ${lastRank} and ${maxRank} in mod load order`);
|
||||
}
|
||||
const entry = { entry: from?.entry ?? emptyBlock(request.vehicle), from, rank, previous: lastVehicle };
|
||||
lastRank = rank;
|
||||
lastVehicle = request.vehicle;
|
||||
return entry;
|
||||
});
|
||||
return { destination: withinGroup ? successor.group! : successor.column, anchor, successor, updateGroupLink: anchor !== successor.entry, entries, lastVehicle };
|
||||
}
|
||||
|
||||
function validateShopPredecessor(anchor: BlkBlock, request: RequestedTechTreeInsertion): void {
|
||||
for (const key of ['reqAir', 'futureReqAir']) {
|
||||
const value = findParam(anchor, key)?.value;
|
||||
if (value !== undefined && (value.type !== 'string' || value.value !== request.after)) {
|
||||
throw invalid(request, `"${anchor.name}" has a different ${key} in config/shop.blk`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function applyPlacement(plan: InsertionPlan): void {
|
||||
for (const item of plan.entries) {
|
||||
if (item.from) {
|
||||
const parent = item.from.group ?? item.from.column;
|
||||
parent.blocks.splice(parent.blocks.indexOf(item.entry), 1);
|
||||
}
|
||||
setParam(item.entry, 'rank', { type: 'int', value: item.rank });
|
||||
setPrerequisite(item.entry, item.previous);
|
||||
}
|
||||
plan.destination.blocks.splice(plan.destination.blocks.indexOf(plan.anchor), 0, ...plan.entries.map(({ entry }) => entry));
|
||||
setPrerequisite(plan.successor.entry, plan.lastVehicle);
|
||||
if (plan.updateGroupLink) setPrerequisite(plan.anchor, plan.lastVehicle);
|
||||
}
|
||||
|
||||
function setPrerequisite(block: BlkBlock, previous: string): void {
|
||||
setParam(block, 'reqAir', { type: 'string', value: previous });
|
||||
if (findParam(block, 'futureReqAir')) setParam(block, 'futureReqAir', { type: 'string', value: previous });
|
||||
}
|
||||
|
||||
function researchRank(entry: BlkBlock, position: ShopPosition | undefined, country: string, request: RequestedTechTreeInsertion): number {
|
||||
const rank = findParam(entry, 'rank')?.value;
|
||||
const gold = findParam(entry, 'costGold')?.value;
|
||||
if (textParam(entry, 'country') !== country || rank?.type !== 'int' || rank.value < 1) {
|
||||
throw invalid(request, `"${entry.name}" must have a valid rank in ${country} in ${VEHICLE_ECONOMY_PATH}`);
|
||||
}
|
||||
if (textParam(entry, 'gift') !== undefined || textParam(entry, 'event') !== undefined || textParam(entry, 'researchType') === 'clanVehicle'
|
||||
|| findParam(entry, 'premPackAir')?.value.value === true || (gold?.type === 'int' && gold.value > 0)
|
||||
|| (position && findParam(position.entry, 'showOnlyWhenBought')?.value.value === true)) {
|
||||
throw invalid(request, `"${entry.name}" must be a regular vehicle in the research line`);
|
||||
}
|
||||
return rank.value;
|
||||
}
|
||||
|
||||
function validateAcyclicLinks(
|
||||
catalog: ReadonlyMap<string, BlkBlock[]>,
|
||||
links: ReadonlyMap<string, { previous: string; request: RequestedTechTreeInsertion }>,
|
||||
): void {
|
||||
const checked = new Set<string>();
|
||||
for (const [vehicle, { request }] of links) {
|
||||
const path = new Set<string>();
|
||||
let name: string | undefined = vehicle;
|
||||
while (name !== undefined && !checked.has(name)) {
|
||||
if (path.has(name)) throw invalid(request, `research prerequisites form a cycle at "${name}"`);
|
||||
path.add(name);
|
||||
const existing: BlkBlock | undefined = catalog.get(name)?.[0];
|
||||
name = links.get(name)?.previous ?? (existing ? textParam(existing, 'reqAir') : undefined);
|
||||
}
|
||||
for (const visited of path) checked.add(visited);
|
||||
}
|
||||
}
|
||||
|
||||
function indexShop(shop: BlkBlock): Map<string, ShopPosition[]> {
|
||||
const positions = new Map<string, ShopPosition[]>();
|
||||
for (const country of shop.blocks) {
|
||||
for (const page of country.blocks) {
|
||||
for (const column of page.blocks) {
|
||||
for (const entry of column.blocks) {
|
||||
addToIndex(positions, entry.name, { entry, page, country: country.name, column });
|
||||
for (const variant of entry.blocks) {
|
||||
addToIndex(positions, variant.name, { entry: variant, page, country: country.name, column, group: entry });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return positions;
|
||||
}
|
||||
|
||||
function indexByName(blocks: readonly BlkBlock[]): Map<string, BlkBlock[]> {
|
||||
const entries = new Map<string, BlkBlock[]>();
|
||||
for (const block of blocks) addToIndex(entries, block.name, block);
|
||||
return entries;
|
||||
}
|
||||
|
||||
function addToIndex<T>(index: Map<string, T[]>, key: string, entry: T): void {
|
||||
const list = index.get(key);
|
||||
if (list) list.push(entry);
|
||||
else index.set(key, [entry]);
|
||||
}
|
||||
|
||||
function requireOne<T>(index: ReadonlyMap<string, T[]>, name: string, where: string, request: RequestedTechTreeInsertion): T {
|
||||
const entry = optionalOne(index, name, where, request);
|
||||
if (entry === undefined) throw invalid(request, `"${name}" is missing from ${where}`);
|
||||
return entry;
|
||||
}
|
||||
|
||||
function optionalOne<T>(index: ReadonlyMap<string, T[]>, name: string, where: string, request: RequestedTechTreeInsertion): T | undefined {
|
||||
const entries = index.get(name);
|
||||
if (entries && entries.length > 1) throw invalid(request, `"${name}" is ambiguous in ${where}`);
|
||||
return entries?.[0];
|
||||
}
|
||||
|
||||
function textParam(block: BlkBlock, name: string): string | undefined {
|
||||
const value = findParam(block, name)?.value;
|
||||
return value?.type === 'string' ? value.value : undefined;
|
||||
}
|
||||
|
||||
function invalid(request: RequestedTechTreeInsertion, reason: string): Error {
|
||||
return new Error(`mod "${request.modId}" tech_tree_insertions (${request.after} -> ${request.vehicle} -> ${request.before}): ${reason}`);
|
||||
}
|
||||
Reference in new issue
Block a user