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
@@ -1,21 +1,36 @@
|
||||
/**
|
||||
* Ways to find a site. Each method returns every RVA it matched; it never picks a winner among
|
||||
* several matches, the caller requires exactly one.
|
||||
*
|
||||
* signature a byte pattern with wildcards in a section
|
||||
* string_ref the code that loads the address of a given string
|
||||
* within a byte pattern inside the function of an already located site
|
||||
*/
|
||||
import { DefinitionError } from '../errors.ts';
|
||||
import type { PeImage } from '../pe-image.ts';
|
||||
import * as signature from './signature.ts';
|
||||
import * as stringRef from './string-ref.ts';
|
||||
import * as within from './within.ts';
|
||||
|
||||
/** Sites located before this one, by name, for methods that search relative to them. */
|
||||
export interface LocateContext {
|
||||
readonly sites: ReadonlyMap<string, number>;
|
||||
}
|
||||
|
||||
export const NO_CONTEXT: LocateContext = { sites: new Map() };
|
||||
|
||||
export interface LocateMethod {
|
||||
/** Keys of a locate block this method reads, besides "method" and "captures". */
|
||||
readonly keys: readonly string[];
|
||||
locate(image: PeImage, spec: Readonly<Record<string, unknown>>): number[];
|
||||
locate(image: PeImage, spec: Readonly<Record<string, unknown>>, context: LocateContext): number[];
|
||||
/** Number of bytes from the site that the method checked; captures must lie inside them. */
|
||||
length(spec: Readonly<Record<string, unknown>>): number;
|
||||
}
|
||||
|
||||
const METHODS: ReadonlyMap<string, LocateMethod> = new Map([
|
||||
[signature.METHOD, { keys: signature.KEYS, locate: signature.locateSignature, length: signature.signatureLength }],
|
||||
[stringRef.METHOD, { keys: stringRef.KEYS, locate: stringRef.locateStringReference, length: stringRef.stringReferenceLength }],
|
||||
[within.METHOD, { keys: within.KEYS, locate: within.locateWithin, length: within.withinLength }],
|
||||
]);
|
||||
|
||||
export function locateMethod(method: unknown): LocateMethod {
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* Find code by the string it uses: the instructions that load the address of a zero-terminated
|
||||
* string with a rip-relative lea, or the functions containing them. Engine code names its
|
||||
* subsystems, script bindings, data fields and log messages with fixed English strings, so a
|
||||
* string the code refers to identifies that code across builds by name rather than by bytes.
|
||||
*
|
||||
* { "method": "string_ref", "string": "kills", "target": "function" }
|
||||
* { "method": "string_ref", "string": "kills",
|
||||
* "pattern": "4c 0f bf 86 ?? ?? ?? ?? 4c 89 f1 48 8d 15 ?? ?? ?? ??", "lea_at": 11 }
|
||||
*
|
||||
* target "reference" (the default) returns each lea, whose 7 bytes may be captured; "function"
|
||||
* returns the start of each function containing one. With "pattern", the site is where the
|
||||
* pattern starts, the lea of the string must be at "lea_at" inside it, and captures may read any
|
||||
* byte of the pattern: this reads, say, the structure field that the code stores under that name.
|
||||
*/
|
||||
import { parseBytePattern, patternMatches, type BytePattern } from '../byte-pattern.ts';
|
||||
import { DefinitionError } from '../errors.ts';
|
||||
import type { PeImage } from '../pe-image.ts';
|
||||
|
||||
export const METHOD = 'string_ref';
|
||||
export const KEYS: readonly string[] = ['string', 'section', 'target', 'pattern', 'lea_at'];
|
||||
const DEFAULT_SECTION = '.rdata';
|
||||
const LEA_SIZE = 7;
|
||||
const TARGETS = ['reference', 'function'] as const;
|
||||
|
||||
interface StringRefSpec {
|
||||
readonly text: string;
|
||||
readonly section: string;
|
||||
readonly target: (typeof TARGETS)[number];
|
||||
readonly around?: { readonly pattern: BytePattern; readonly leaAt: number };
|
||||
}
|
||||
|
||||
export function locateStringReference(image: PeImage, spec: Readonly<Record<string, unknown>>): number[] {
|
||||
const parsed = readSpec(spec);
|
||||
const references = stringAddresses(image, parsed.text, parsed.section).flatMap((address) => leaIndex(image).get(address) ?? []);
|
||||
if (parsed.around) return withPattern(image, references, parsed.around);
|
||||
if (parsed.target === 'reference') return references.sort((left, right) => left - right);
|
||||
const starts = references.map((rva) => {
|
||||
const range = image.functionContaining(rva);
|
||||
if (!range) throw new DefinitionError(`string_ref "${parsed.text}": the reference at 0x${rva.toString(16)} is in no known function`);
|
||||
return range.begin;
|
||||
});
|
||||
return [...new Set(starts)].sort((left, right) => left - right);
|
||||
}
|
||||
|
||||
export function stringReferenceLength(spec: Readonly<Record<string, unknown>>): number {
|
||||
const parsed = readSpec(spec);
|
||||
if (parsed.around) return parsed.around.pattern.bytes.length;
|
||||
return parsed.target === 'reference' ? LEA_SIZE : 0;
|
||||
}
|
||||
|
||||
function withPattern(image: PeImage, references: readonly number[], around: NonNullable<StringRefSpec['around']>): number[] {
|
||||
const starts: number[] = [];
|
||||
for (const reference of references) {
|
||||
const start = reference - around.leaAt;
|
||||
if (patternMatches(around.pattern, image.read(start, around.pattern.bytes.length))) starts.push(start);
|
||||
}
|
||||
return starts.sort((left, right) => left - right);
|
||||
}
|
||||
|
||||
function readSpec(spec: Readonly<Record<string, unknown>>): StringRefSpec {
|
||||
if (typeof spec.string !== 'string' || spec.string === '') throw new DefinitionError('string_ref needs a non-empty "string"');
|
||||
const target = (spec.target ?? 'reference') as (typeof TARGETS)[number];
|
||||
if (!TARGETS.includes(target)) throw new DefinitionError(`string_ref "target" must be one of ${TARGETS.join(', ')}`);
|
||||
const section = typeof spec.section === 'string' ? spec.section : DEFAULT_SECTION;
|
||||
if (spec.pattern === undefined) {
|
||||
if (spec.lea_at !== undefined) throw new DefinitionError('string_ref "lea_at" needs a "pattern"');
|
||||
return { text: spec.string, section, target };
|
||||
}
|
||||
if (target !== 'reference') throw new DefinitionError('string_ref with a "pattern" locates the pattern, so "target" must be "reference"');
|
||||
const pattern = parseBytePattern(spec.pattern, 'string_ref pattern');
|
||||
const leaAt = spec.lea_at;
|
||||
if (typeof leaAt !== 'number' || !Number.isInteger(leaAt) || leaAt < 0 || leaAt + LEA_SIZE > pattern.bytes.length) {
|
||||
throw new DefinitionError('string_ref "lea_at" must be the position of a 7-byte lea inside the pattern');
|
||||
}
|
||||
return { text: spec.string, section, target, around: { pattern, leaAt } };
|
||||
}
|
||||
|
||||
/**
|
||||
* RVAs where `text` followed by a zero byte is stored. The linker merges a string into the tail of
|
||||
* a longer one that ends the same way, so a match need not start a string; only the code that
|
||||
* loads one of these addresses decides which is meant.
|
||||
*/
|
||||
function stringAddresses(image: PeImage, text: string, sectionName: string): number[] {
|
||||
const data = Buffer.from(image.sectionBytes(sectionName));
|
||||
const needle = Buffer.concat([Buffer.from(text, 'utf8'), Buffer.of(0)]);
|
||||
const base = image.section(sectionName).rva;
|
||||
const found: number[] = [];
|
||||
for (let at = data.indexOf(needle); at >= 0; at = data.indexOf(needle, at + 1)) found.push(base + at);
|
||||
return found;
|
||||
}
|
||||
|
||||
/** Every rip-relative lea in .text, by the address it loads. */
|
||||
function leaIndex(image: PeImage): ReadonlyMap<number, number[]> {
|
||||
return image.cached('string_ref:lea', () => {
|
||||
const text = image.sectionBytes('.text');
|
||||
const base = image.section('.text').rva;
|
||||
const view = new DataView(text.buffer, text.byteOffset, text.byteLength);
|
||||
const index = new Map<number, number[]>();
|
||||
for (let at = 0; at + LEA_SIZE <= text.length; at++) {
|
||||
// REX.W (48) or REX.WR (4c), opcode 8d, ModRM with mod 00 and r/m 101 (rip-relative).
|
||||
if ((text[at] !== 0x48 && text[at] !== 0x4c) || text[at + 1] !== 0x8d || (text[at + 2]! & 0xc7) !== 0x05) continue;
|
||||
const address = base + at + LEA_SIZE + view.getInt32(at + 3, true);
|
||||
const list = index.get(address);
|
||||
if (list) list.push(base + at);
|
||||
else index.set(address, [base + at]);
|
||||
}
|
||||
return index;
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Find a byte pattern inside the function that contains an already located site. The function
|
||||
* range comes from the exception directory. Searching one function instead of the whole code
|
||||
* section lets a short pattern, such as the one instruction that reads a structure field, be
|
||||
* unique, and ties it to the function it was written for.
|
||||
*
|
||||
* { "method": "within", "in": "mplayer_add_score", "pattern": "f3 0f 2a 84 81 ?? ?? ?? ??" }
|
||||
*
|
||||
* "in" names a site resolved earlier in the same definition set.
|
||||
*/
|
||||
import { parseBytePattern, patternMatches } from '../byte-pattern.ts';
|
||||
import { DefinitionError, LocateError } from '../errors.ts';
|
||||
import type { PeImage } from '../pe-image.ts';
|
||||
import type { LocateContext } from './index.ts';
|
||||
|
||||
export const METHOD = 'within';
|
||||
export const KEYS: readonly string[] = ['in', 'pattern'];
|
||||
|
||||
export function locateWithin(image: PeImage, spec: Readonly<Record<string, unknown>>, context: LocateContext): number[] {
|
||||
if (typeof spec.in !== 'string') throw new DefinitionError('within needs the name of a located site in "in"');
|
||||
const anchor = context.sites.get(spec.in);
|
||||
if (anchor === undefined) throw new LocateError(`site "${spec.in}" that this one is searched in is unavailable`);
|
||||
const range = image.functionContaining(anchor);
|
||||
if (!range) throw new LocateError(`site "${spec.in}" at 0x${anchor.toString(16)} is in no known function`);
|
||||
const pattern = parseBytePattern(spec.pattern, 'within pattern');
|
||||
const code = image.read(range.begin, range.end - range.begin);
|
||||
const hits: number[] = [];
|
||||
for (let at = 0; at + pattern.bytes.length <= code.length; at++) {
|
||||
if (patternMatches(pattern, code, at)) hits.push(range.begin + at);
|
||||
}
|
||||
return hits;
|
||||
}
|
||||
|
||||
export function withinLength(spec: Readonly<Record<string, unknown>>): number {
|
||||
return parseBytePattern(spec.pattern, 'within pattern').bytes.length;
|
||||
}
|
||||
Reference in new issue
Block a user