fix(patcher): retain stock tank AI movement

Keep the original behavior tree installed and validate the optional script entry and dependencies after mod overlays. Verified stock tree bytes and the enable/disable rebuild on 2.61.0.58.
This commit is contained in:
N0VAN0M4LY committed 2026-12-03 14:24:32 +08:00
1 parent 975a28441a
commit bb680c6abd
5 files changed
+40 -33

No files matched your search

+6 -4
View File
@@ -19,6 +19,7 @@ import { GAME_FILES } from './game-copy.ts';
import {
GAME_PACK_FILE,
rebuildGamePack,
validateGroundAiGamePack,
type GamePackResult,
type GroundAiBuildInputs,
} from './ground-ai/game-pack.ts';
@@ -130,7 +131,7 @@ export function buildGameCopy(inputs: GameCopyInputs): GameCopyBuild {
const executable = patchExecutableWithMods(inputs.stock.executable, folders.exe, enabledMods);
const stockImage = new PeImage(inputs.stock.executable);
const native = resolveNativeModules(stockImage, loadNativeModules(folders.native));
const core = buildCorePacks(inputs, endpoint, folders.gui, enabledMods, packOverlays, groundAiActive);
const core = buildCorePacks(inputs, endpoint, folders.gui, enabledMods, packOverlays);
const executableFolder = dirname(GAME_FILES.executable);
const hookSettings = networkHookSettings(endpoint, stockImage, native.values, {
@@ -174,7 +175,6 @@ function buildCorePacks(
guiPatchFolder: string,
enabledMods: readonly DiscoveredMod[],
packOverlays: PackOverlayReport[],
groundAiActive: boolean,
) {
const coreGuiPack = rebuildGuiPack(inputs.stock.guiPack, endpoint, guiPatchFolder);
const appliedGuiPatches = coreGuiPack.applied.map((item) => item.patch.id);
@@ -199,7 +199,6 @@ function buildCorePacks(
endpoint,
bundles,
addonMounts.map((mount) => mount.mountFolder),
{ yieldStockTankDrive: groundAiActive && inputs.groundAi!.config.layers.navigation },
);
const mainOverlay = applyModPackOverlays(coreMainPack.pack, 'aces.vromfs.bin', enabledMods);
if (mainOverlay.report.operationsApplied > 0) packOverlays.push(mainOverlay.report);
@@ -234,7 +233,10 @@ function appendOptionalPackFiles(
}
const { pack: aiGamePack, ...report } = rebuildGamePack(stockGameBytes, inputs.groundAi);
const overlay = applyModPackOverlays(aiGamePack, GAME_PACK_FILE, enabledMods);
if (overlay.report.operationsApplied > 0) packOverlays.push(overlay.report);
if (overlay.report.operationsApplied > 0) {
validateGroundAiGamePack(overlay.pack, report.modules);
packOverlays.push(overlay.report);
}
files.push({ path: GAME_PACK_FILE, content: overlay.pack });
gamePackReport = report;
}
+32 -2
View File
@@ -4,7 +4,7 @@
*/
import { readdirSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { getFile, putFile, readArchive, writeArchive } from '@wto/vromfs';
import { getFile, putFile, readArchive, writeArchive, type Archive } from '@wto/vromfs';
import { compileGroundAiConfigDas, type GroundAiConfig } from './ground-ai-config.ts';
export const GAME_PACK_FILE = 'game.vromfs.bin';
@@ -13,8 +13,11 @@ export const GAME_AI_ARCHIVE_FOLDER = 'game/wtoai';
export const GAME_AI_CONFIG_FILE = 'wto_ai_config_common.das';
const GAME_MAIN_ANCHOR = 'ok = load_optional_folder("%appGame/ai") && ok';
const GAME_MAIN_INJECT = 'ok = load_optional_folder("%appGame/wtoai") && ok';
// A failure in the optional AI folder must not fail the stock loader.
const GAME_MAIN_INJECT = 'load_optional_folder("%appGame/wtoai")';
const GAME_AI_ENTRY = `${GAME_AI_ARCHIVE_FOLDER}/wto_ai_runtime.das`;
const MODULE_NAME = /^[a-z0-9_]+\.das$/;
const LOCAL_MODULE_REQUIRE = /^[ \t]*require[ \t]+%appGame\.wtoai\.([a-z0-9_]+)\b/gm;
export interface GroundAiBuildInputs {
readonly scriptsFolder: string;
@@ -53,9 +56,36 @@ export function rebuildGamePack(stockGamePack: Uint8Array, inputs: GroundAiBuild
modules.push(archivePath);
}
validateGroundAiArchive(archive, modules);
return { pack: writeArchive(archive), archiveFolder: GAME_AI_ARCHIVE_FOLDER, modules };
}
export function validateGroundAiGamePack(pack: Uint8Array, modules: readonly string[]): void {
validateGroundAiArchive(readArchive(pack), modules);
}
function validateGroundAiArchive(archive: Archive, modules: readonly string[]): void {
const decoder = new TextDecoder('utf-8');
const mainLines = decoder.decode(getFile(archive, GAME_MAIN_SCRIPT_PATH)).split(/\r?\n/);
const aiLoaders = mainLines.filter((line) => line.trim() === GAME_MAIN_INJECT);
const anchor = mainLines.findIndex((line) => line.trim() === GAME_MAIN_ANCHOR);
const indentation = mainLines[anchor]?.match(/^[ \t]*/)?.[0];
if (aiLoaders.length !== 1 || indentation === undefined ||
mainLines[anchor + 1]?.trimEnd() !== indentation + GAME_MAIN_INJECT) {
throw new Error(`ground AI loader is missing or displaced in ${GAME_MAIN_SCRIPT_PATH}`);
}
const required = new Set([GAME_AI_ENTRY, ...modules]);
for (const scriptPath of required) {
const bytes = archive.files.get(scriptPath);
const source = bytes === undefined ? '' : decoder.decode(bytes);
if (!source.trim()) throw new Error(`missing or empty ground AI script in ${GAME_PACK_FILE}: ${scriptPath}`);
for (const match of source.matchAll(LOCAL_MODULE_REQUIRE)) {
required.add(`${GAME_AI_ARCHIVE_FOLDER}/${match[1]}.das`);
}
}
}
function injectLoaderLine(source: string): string {
if (source.includes(GAME_MAIN_INJECT)) {
throw new Error(`${GAME_MAIN_SCRIPT_PATH} already contains ${GAME_MAIN_INJECT}`);
-1
View File
@@ -79,7 +79,6 @@ export {
EXTRA_STORE_BUNDLE_GUID_MAP,
EXTRA_STORE_BUNDLE_GUIDS,
rebuildMainPack,
type MainPackOptions,
type MainPackResult,
} from './main-pack.ts';
export { redirectAddresses, type AddressChange } from './network/address-rewrite.ts';
+1 -25
View File
@@ -35,15 +35,9 @@ export const EXTRA_STORE_BUNDLE_GUIDS: ReadonlyArray<readonly [entitlement: stri
export const EXTRA_STORE_BUNDLE_GUID_MAP: Readonly<Record<string, string>> = Object.fromEntries(EXTRA_STORE_BUNDLE_GUIDS);
export interface MainPackOptions {
/** Replace stock tankBot behavior children with `success` so custom chassis control owns steering. */
readonly yieldStockTankDrive?: boolean;
}
export interface MainPackResult {
readonly pack: Uint8Array;
readonly addresses: readonly AddressChange[];
readonly yieldedStockTankDrive: boolean;
}
export function rebuildMainPack(
@@ -51,7 +45,6 @@ export function rebuildMainPack(
endpoint: LocalEndpoint,
extraBundles: ReadonlyArray<readonly [string, string]> = [],
extraAddonFolders: readonly string[] = [],
options: MainPackOptions = {},
): MainPackResult {
let addresses: AddressChange[] = [];
const edits = [
@@ -68,13 +61,9 @@ export function rebuildMainPack(
path: GUI_CONFIG_PATH,
edit: (root: BlkBlock) => registerStoreBundleGuids(root, [...EXTRA_STORE_BUNDLE_GUIDS, ...extraBundles]),
},
...(options.yieldStockTankDrive ? [{
path: AI_LIST_PATH,
edit: (root: BlkBlock) => yieldTankBotBehaviorTree(root),
}] : []),
];
const pack = editPackBlks(stockPack, edits);
return { pack, addresses, yieldedStockTankDrive: options.yieldStockTankDrive === true };
return { pack, addresses };
}
function registerStoreBundleGuids(guiRoot: BlkBlock, bundles: ReadonlyArray<readonly [string, string]>): void {
@@ -83,16 +72,3 @@ function registerStoreBundleGuids(guiRoot: BlkBlock, bundles: ReadonlyArray<read
setParam(guidBlock, entitlement, { type: 'string', value: guid });
}
}
function yieldTankBotBehaviorTree(aiRoot: BlkBlock): void {
const matches = aiRoot.blocks.filter((block) =>
block.name === 'selector' &&
block.params.some((param) => param.name === 'name' && param.value.type === 'string' && param.value.value === 'tankBot'),
);
if (matches.length !== 1) {
throw new Error(`expected 1 "tankBot" selector in ${AI_LIST_PATH}, found ${matches.length}`);
}
const tankBot = matches[0]!;
tankBot.blocks.length = 0;
tankBot.blocks.push({ name: 'success', params: [], blocks: [] });
}
+1 -1
View File
@@ -185,7 +185,7 @@ function report(build: GameCopyBuild, verbose: boolean): void {
if (verbose) for (const module of build.nativeModules.available) console.log(` ready ${module}`);
reportFailures(build.nativeModules.failures);
if (build.gamePack) {
console.log(`${GAME_PACK_FILE}: ${build.gamePack.modules.length} ground AI module(s) ready in ${build.gamePack.archiveFolder}`);
console.log(`${GAME_PACK_FILE}: ${build.gamePack.modules.length} ground AI module(s) packed in ${build.gamePack.archiveFolder}`);
}
}