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.
48 lines
2.1 KiB
TypeScript
48 lines
2.1 KiB
TypeScript
/**
|
|
* A "locate" block: how to find one place in the executable, and which values to read there.
|
|
* Exe patch variants use it to find where to write; native definitions use it to find the
|
|
* functions, globals and structure offsets the hook library needs.
|
|
*/
|
|
import { readCaptures, readCaptureSpecs, type Captures, type CaptureSpec } from './captures.ts';
|
|
import { DefinitionError, LocateError } from './errors.ts';
|
|
import { asObject, rejectUnknownKeys } from './json-fields.ts';
|
|
import { locateMethod, NO_CONTEXT, type LocateContext, type LocateMethod } from './locate/index.ts';
|
|
import type { PeImage } from './pe-image.ts';
|
|
|
|
export interface SiteSpec {
|
|
readonly method: LocateMethod;
|
|
/** The method's own keys (pattern, section, ...). */
|
|
readonly spec: Readonly<Record<string, unknown>>;
|
|
readonly captures: readonly CaptureSpec[];
|
|
}
|
|
|
|
export interface FoundSite {
|
|
readonly rva: number;
|
|
readonly captures: Captures;
|
|
}
|
|
|
|
export function readSiteSpec(raw: unknown, where: string): SiteSpec {
|
|
const block = asObject(raw, where);
|
|
const method = locateMethod(block.method);
|
|
rejectUnknownKeys(block, new Set(['method', 'captures', ...method.keys]), where);
|
|
let length: number;
|
|
try {
|
|
length = method.length(block);
|
|
} catch (error) {
|
|
throw new DefinitionError(`${where}: ${(error as Error).message}`);
|
|
}
|
|
return { method, spec: block, captures: readCaptureSpecs(block.captures, length, where) };
|
|
}
|
|
|
|
/** The single place the spec matches, with its captures; anything else is a LocateError. */
|
|
export function findSite(image: PeImage, site: SiteSpec, context: LocateContext = NO_CONTEXT): FoundSite {
|
|
const hits = site.method.locate(image, site.spec, context);
|
|
if (!hits.length) throw new LocateError('site not found: this game build differs from the verified ones, or the executable is already patched');
|
|
if (hits.length > 1) throw new LocateError(`site found in ${hits.length} places, so it is ambiguous: ${hits.map(hex).join(', ')}`);
|
|
return { rva: hits[0]!, captures: readCaptures(image, hits[0]!, site.captures) };
|
|
}
|
|
|
|
export function hex(value: number): string {
|
|
return `0x${value.toString(16)}`;
|
|
}
|