From bb680c6abd245b2e1787ffe55b15dc00f6ab2d70 Mon Sep 17 00:00:00 2001 From: N0VAN0M4LY <224130499+N0VAN0M4LY@users.noreply.github.com> Date: Thu, 3 Dec 2026 14:24:32 +0800 Subject: [PATCH] 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. --- packages/patcher/src/build-game-copy.ts | 10 +++--- packages/patcher/src/ground-ai/game-pack.ts | 34 +++++++++++++++++++-- packages/patcher/src/index.ts | 1 - packages/patcher/src/main-pack.ts | 26 +--------------- scripts/build-game-copy.ts | 2 +- 5 files changed, 40 insertions(+), 33 deletions(-) diff --git a/packages/patcher/src/build-game-copy.ts b/packages/patcher/src/build-game-copy.ts index 86da7ee..ac10235 100644 --- a/packages/patcher/src/build-game-copy.ts +++ b/packages/patcher/src/build-game-copy.ts @@ -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; } diff --git a/packages/patcher/src/ground-ai/game-pack.ts b/packages/patcher/src/ground-ai/game-pack.ts index 85f7014..9b782cf 100644 --- a/packages/patcher/src/ground-ai/game-pack.ts +++ b/packages/patcher/src/ground-ai/game-pack.ts @@ -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}`); diff --git a/packages/patcher/src/index.ts b/packages/patcher/src/index.ts index 631db46..ddc6806 100644 --- a/packages/patcher/src/index.ts +++ b/packages/patcher/src/index.ts @@ -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'; diff --git a/packages/patcher/src/main-pack.ts b/packages/patcher/src/main-pack.ts index f61d508..00f2b7e 100644 --- a/packages/patcher/src/main-pack.ts +++ b/packages/patcher/src/main-pack.ts @@ -35,15 +35,9 @@ export const EXTRA_STORE_BUNDLE_GUIDS: ReadonlyArray> = 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 = [], 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): void { @@ -83,16 +72,3 @@ function registerStoreBundleGuids(guiRoot: BlkBlock, bundles: ReadonlyArray - 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: [] }); -} diff --git a/scripts/build-game-copy.ts b/scripts/build-game-copy.ts index 60ddbc3..cfd760a 100644 --- a/scripts/build-game-copy.ts +++ b/scripts/build-game-copy.ts @@ -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}`); } }