feat(patcher): resolve native LocalMP values from patch definitions
The LocalMP modules of wto_ws.dll scanned for 42 functions at start-up and used about a hundred structure offsets compiled in as constants, so a game update could move a field without anything noticing. Their needs are now definitions under patches/native/<module>.jsonc, resolved by the patcher when it builds the game copy, the same way exe patches are located: - Locate methods: "string_ref" finds code by the string it loads (optionally with a pattern around the lea, to read the field a scoreboard row stores under that name), and "within" searches a short pattern inside the function of a site located before it (function ranges from the exception directory). - Each module lists its sites (functions, globals, call targets and the instructions whose captures give structure offsets), its features and the modules it requires. A module whose sites are not all found is reported like an unavailable patch, with the modules depending on it. - The resolved values go into wto_ws.cfg as native.<site>[.<capture>] lines, with the executable's PE time stamp and image size so the library can refuse values resolved for another build. - Version records include the native module variants and values. - Patch-set folders: the patcher, the scripts and the launcher take the root of a patch set (exe, gui, native, versions) instead of separate folders. - New feature tags K-Chat and radio and K-Nuclear bomb in both language packs. The library still scans by itself; reading these values is the next change. Checked: root and launcher type checks; all 10 modules resolve on 2.61.0.37, where the 239 values equal what the drafting tool read with a disassembler, and on 2.61.0.43, where every structure offset is unchanged except the GameSaveData HUD event queue (0xae8 -> 0xb18); build-game-copy from 2.61.0.37 into a temporary folder writes the values into wto_ws.cfg.
This commit is contained in:
1 parent
373b7a037a
commit
dde852e2b1
36 files changed
+3318
-91
No files matched your search
@@ -0,0 +1,118 @@
|
||||
/**
|
||||
* Native module definitions: what the network hook library's LocalMP modules
|
||||
* (native/wto-net/src/local_mp_*.c) need from aces.exe, one file per module under
|
||||
* patches/native/<module>.jsonc:
|
||||
*
|
||||
* module the C module's name, equal to the file name
|
||||
* features the feature inventory rows the module provides
|
||||
* requires modules that must be available for this one to work
|
||||
* sites named places in the executable: functions to call or hook, and code whose
|
||||
* captures give global addresses and structure offsets; each site has variants
|
||||
* written for game builds, exactly one of which must locate it
|
||||
*
|
||||
* The launcher resolves every site against the player's executable when it builds the game copy
|
||||
* (native-resolve.ts) and hands the values to the library, so the library holds no address or
|
||||
* offset of its own. Comments explain what each site is and how it was derived.
|
||||
*/
|
||||
import { readdirSync } from 'node:fs';
|
||||
import { basename, join } from 'node:path';
|
||||
import {
|
||||
asObject,
|
||||
DefinitionError,
|
||||
readJsonObject,
|
||||
readSiteSpec,
|
||||
rejectUnknownKeys,
|
||||
requiredString,
|
||||
stringList,
|
||||
variantObjects,
|
||||
type SiteSpec,
|
||||
} from '@wto/exepatch';
|
||||
|
||||
const SUFFIX = '.jsonc';
|
||||
const MODULE_KEYS = new Set(['module', 'features', 'requires', 'sites']);
|
||||
const SITE_KEYS = new Set(['variants']);
|
||||
const VARIANT_KEYS = new Set(['written_for', 'locate']);
|
||||
const NAME = /^[a-z][a-z0-9_]*$/;
|
||||
|
||||
export interface NativeSiteVariant {
|
||||
readonly writtenFor: string;
|
||||
readonly locate: SiteSpec;
|
||||
}
|
||||
|
||||
export interface NativeSite {
|
||||
readonly name: string;
|
||||
readonly variants: readonly NativeSiteVariant[];
|
||||
/** Capture names every variant provides. */
|
||||
readonly captures: readonly string[];
|
||||
}
|
||||
|
||||
export interface NativeModule {
|
||||
readonly module: string;
|
||||
readonly features: readonly string[];
|
||||
readonly requires: readonly string[];
|
||||
readonly sites: readonly NativeSite[];
|
||||
}
|
||||
|
||||
/** Every module in `folder`, in an order where each module comes after the ones it requires. */
|
||||
export function loadNativeModules(folder: string): NativeModule[] {
|
||||
const modules = readdirSync(folder).filter((name) => name.endsWith(SUFFIX)).sort().map((name) => readModule(join(folder, name)));
|
||||
rejectDuplicateSites(modules);
|
||||
return dependencyOrder(modules);
|
||||
}
|
||||
|
||||
function readModule(path: string): NativeModule {
|
||||
const raw = readJsonObject(path);
|
||||
rejectUnknownKeys(raw, MODULE_KEYS, path);
|
||||
const module = requiredString(raw.module, `${path} module`);
|
||||
if (`${module}${SUFFIX}` !== basename(path)) throw new DefinitionError(`${path}: module "${module}" must be in ${module}${SUFFIX}`);
|
||||
const sites = Object.entries(asObject(raw.sites, `${path} sites`)).map(([name, site]) => readSite(name, site, `${path} site ${name}`));
|
||||
return { module, features: stringList(raw.features, `${path} features`), requires: stringList(raw.requires, `${path} requires`), sites };
|
||||
}
|
||||
|
||||
function readSite(name: string, raw: unknown, where: string): NativeSite {
|
||||
if (!NAME.test(name)) throw new DefinitionError(`${where}: site names are lower case letters, digits and "_"`);
|
||||
const site = asObject(raw, where);
|
||||
rejectUnknownKeys(site, SITE_KEYS, where);
|
||||
const variants = variantObjects(site.variants, where).map((variant) => {
|
||||
const label = `${where} variant ${String(variant.written_for)}`;
|
||||
rejectUnknownKeys(variant, VARIANT_KEYS, label);
|
||||
return { writtenFor: variant.written_for as string, locate: readSiteSpec(variant.locate, `${label} locate`) };
|
||||
});
|
||||
const captures = variants[0]!.locate.captures.map((capture) => capture.name).sort();
|
||||
for (const variant of variants) {
|
||||
const names = variant.locate.captures.map((capture) => capture.name).sort();
|
||||
if (names.join() !== captures.join()) throw new DefinitionError(`${where}: every variant must capture the same values (${captures.join(', ')})`);
|
||||
}
|
||||
return { name, variants, captures };
|
||||
}
|
||||
|
||||
function rejectDuplicateSites(modules: readonly NativeModule[]): void {
|
||||
const seen = new Map<string, string>();
|
||||
for (const module of modules) {
|
||||
for (const site of module.sites) {
|
||||
const previous = seen.get(site.name);
|
||||
if (previous) throw new DefinitionError(`site "${site.name}" is defined in both ${previous} and ${module.module}`);
|
||||
seen.set(site.name, module.module);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function dependencyOrder(modules: readonly NativeModule[]): NativeModule[] {
|
||||
const byName = new Map(modules.map((module) => [module.module, module]));
|
||||
const ordered: NativeModule[] = [];
|
||||
const visiting = new Set<string>();
|
||||
const visit = (module: NativeModule): void => {
|
||||
if (ordered.includes(module)) return;
|
||||
if (visiting.has(module.module)) throw new DefinitionError(`native modules require each other in a cycle through ${module.module}`);
|
||||
visiting.add(module.module);
|
||||
for (const name of module.requires) {
|
||||
const required = byName.get(name);
|
||||
if (!required) throw new DefinitionError(`${module.module} requires unknown native module ${name}`);
|
||||
visit(required);
|
||||
}
|
||||
visiting.delete(module.module);
|
||||
ordered.push(module);
|
||||
};
|
||||
modules.forEach(visit);
|
||||
return ordered;
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Resolve the native module definitions (native-definition.ts) against a stock executable.
|
||||
*
|
||||
* Modules are resolved in dependency order and their sites in definition order, so a site can be
|
||||
* searched inside a function located before it. Every site of a module must be located for the
|
||||
* module to be available; a module that is not, and every module requiring it, is listed with its
|
||||
* features the same way an unavailable patch is, and the library leaves that module off.
|
||||
*
|
||||
* The values handed to the library are, per site of an available module, `<site>` (its RVA) and
|
||||
* `<site>.<capture>` for each capture.
|
||||
*/
|
||||
import { chooseVariant, findSite, PatchError, type PatchFailure, type PeImage } from '@wto/exepatch';
|
||||
import type { NativeModule } from './native-definition.ts';
|
||||
|
||||
export interface NativeResolution {
|
||||
/** Values by name, from the available modules only. */
|
||||
readonly values: ReadonlyMap<string, number>;
|
||||
/** Module name -> the variant used for each of its sites. */
|
||||
readonly available: ReadonlyMap<string, ReadonlyMap<string, string>>;
|
||||
/** Unavailable modules, with patchId `native/<module>`. */
|
||||
readonly failures: readonly PatchFailure[];
|
||||
}
|
||||
|
||||
export function resolveNativeModules(image: PeImage, modules: readonly NativeModule[]): NativeResolution {
|
||||
const values = new Map<string, number>();
|
||||
const sites = new Map<string, number>();
|
||||
const available = new Map<string, ReadonlyMap<string, string>>();
|
||||
const failures: PatchFailure[] = [];
|
||||
|
||||
for (const module of modules) {
|
||||
const missing = module.requires.filter((name) => !available.has(name));
|
||||
try {
|
||||
if (missing.length) throw new PatchError(`depends on unavailable native module(s): ${missing.join(', ')}`);
|
||||
const resolved = resolveModule(image, module, sites);
|
||||
for (const [name, value] of resolved.values) values.set(name, value);
|
||||
for (const [name, rva] of resolved.sites) sites.set(name, rva);
|
||||
available.set(module.module, resolved.variants);
|
||||
} catch (error) {
|
||||
if (!(error instanceof PatchError)) throw error;
|
||||
failures.push({ patchId: `native/${module.module}`, features: module.features, reason: error.message });
|
||||
}
|
||||
}
|
||||
return { values, available, failures };
|
||||
}
|
||||
|
||||
interface ResolvedModule {
|
||||
readonly values: Map<string, number>;
|
||||
readonly sites: Map<string, number>;
|
||||
readonly variants: Map<string, string>;
|
||||
}
|
||||
|
||||
/** Every site of `module`, or a PatchError naming the first one that cannot be located. */
|
||||
function resolveModule(image: PeImage, module: NativeModule, located: ReadonlyMap<string, number>): ResolvedModule {
|
||||
const result: ResolvedModule = { values: new Map(), sites: new Map(), variants: new Map() };
|
||||
for (const site of module.sites) {
|
||||
const context = { sites: new Map([...located, ...result.sites]) };
|
||||
let chosen;
|
||||
try {
|
||||
chosen = chooseVariant(site.variants, (variant) => findSite(image, variant.locate, context));
|
||||
} catch (error) {
|
||||
if (error instanceof PatchError) throw new PatchError(`site ${site.name}: ${error.message}`);
|
||||
throw error;
|
||||
}
|
||||
result.sites.set(site.name, chosen.value.rva);
|
||||
result.variants.set(site.name, chosen.variant.writtenFor);
|
||||
result.values.set(site.name, chosen.value.rva);
|
||||
for (const [capture, value] of chosen.value.captures) result.values.set(`${site.name}.${capture}`, value);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* The lines of the hook library's settings file that carry the resolved native values.
|
||||
*
|
||||
* exe.time_date_stamp=0x... the executable the values were resolved for (PE file header);
|
||||
* exe.size_of_image=0x... the library compares both with the running executable and
|
||||
* leaves every LocalMP module off when they differ
|
||||
* native.<name>=0x... one line per value (native-resolve.ts), signed values as -0x...
|
||||
*/
|
||||
import type { PeImage } from '@wto/exepatch';
|
||||
|
||||
export function nativeSettingsLines(image: PeImage, values: ReadonlyMap<string, number>): string[] {
|
||||
const view = new DataView(image.data.buffer, image.data.byteOffset, image.data.byteLength);
|
||||
const peHeader = view.getUint32(0x3c, true);
|
||||
const timeDateStamp = view.getUint32(peHeader + 8, true);
|
||||
const sizeOfImage = view.getUint32(peHeader + 24 + 56, true);
|
||||
const lines = [`exe.time_date_stamp=${hex(timeDateStamp)}`, `exe.size_of_image=${hex(sizeOfImage)}`];
|
||||
for (const [name, value] of [...values].sort(([left], [right]) => left.localeCompare(right))) lines.push(`native.${name}=${hex(value)}`);
|
||||
return lines;
|
||||
}
|
||||
|
||||
function hex(value: number): string {
|
||||
return value < 0 ? `-0x${(-value).toString(16)}` : `0x${value.toString(16)}`;
|
||||
}
|
||||
Reference in new issue
Block a user