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,6 +1,7 @@
|
||||
/**
|
||||
* Read-only view of a PE executable: its sections, and the mapping between RVAs (addresses
|
||||
* relative to the image base) and file offsets. Only what locating and writing patches needs.
|
||||
* Read-only view of a PE executable: its sections, the mapping between RVAs (addresses relative
|
||||
* to the image base) and file offsets, and the function ranges of its exception directory. Only
|
||||
* what locating and writing patches needs.
|
||||
*/
|
||||
import { PeFormatError } from './errors.ts';
|
||||
|
||||
@@ -12,17 +13,51 @@ export interface Section {
|
||||
readonly rawSize: number;
|
||||
}
|
||||
|
||||
/** A function, or one part of a function, from the exception directory: [begin, end). */
|
||||
export interface FunctionRange {
|
||||
readonly begin: number;
|
||||
readonly end: number;
|
||||
}
|
||||
|
||||
const SECTION_HEADER_SIZE = 40;
|
||||
const PE32_PLUS_MAGIC = 0x20b;
|
||||
/** Offset of the data directories in a PE32+ optional header. */
|
||||
const DATA_DIRECTORIES = 112;
|
||||
const EXCEPTION_DIRECTORY = 3;
|
||||
const RUNTIME_FUNCTION_SIZE = 12;
|
||||
|
||||
export class PeImage {
|
||||
readonly data: Uint8Array;
|
||||
readonly sections: readonly Section[];
|
||||
#functions: readonly FunctionRange[] | undefined;
|
||||
readonly #cache = new Map<string, unknown>();
|
||||
|
||||
constructor(data: Uint8Array) {
|
||||
this.data = data;
|
||||
this.sections = readSections(data);
|
||||
}
|
||||
|
||||
/** The function range containing `rva`, or null when the exception directory has none. */
|
||||
functionContaining(rva: number): FunctionRange | null {
|
||||
const functions = (this.#functions ??= readFunctions(this));
|
||||
let low = 0;
|
||||
let high = functions.length - 1;
|
||||
while (low <= high) {
|
||||
const middle = (low + high) >> 1;
|
||||
const range = functions[middle]!;
|
||||
if (rva < range.begin) high = middle - 1;
|
||||
else if (rva >= range.end) low = middle + 1;
|
||||
else return range;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** A value derived from this image, computed once (indexes the locate methods build). */
|
||||
cached<T>(key: string, compute: () => T): T {
|
||||
if (!this.#cache.has(key)) this.#cache.set(key, compute());
|
||||
return this.#cache.get(key) as T;
|
||||
}
|
||||
|
||||
section(name: string): Section {
|
||||
const section = this.sections.find((item) => item.name === name);
|
||||
if (!section) throw new PeFormatError(`no section ${name}`);
|
||||
@@ -71,3 +106,19 @@ function readSections(data: Uint8Array): Section[] {
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function readFunctions(image: PeImage): FunctionRange[] {
|
||||
const view = new DataView(image.data.buffer, image.data.byteOffset, image.data.byteLength);
|
||||
const optionalHeader = view.getUint32(0x3c, true) + 24;
|
||||
if (view.getUint16(optionalHeader, true) !== PE32_PLUS_MAGIC) throw new PeFormatError('not a PE32+ executable');
|
||||
const directory = optionalHeader + DATA_DIRECTORIES + EXCEPTION_DIRECTORY * 8;
|
||||
const rva = view.getUint32(directory, true);
|
||||
const size = view.getUint32(directory + 4, true);
|
||||
const table = image.read(rva, size);
|
||||
const entries = new DataView(table.buffer, table.byteOffset, table.byteLength);
|
||||
const functions: FunctionRange[] = [];
|
||||
for (let at = 0; at + RUNTIME_FUNCTION_SIZE <= size; at += RUNTIME_FUNCTION_SIZE) {
|
||||
functions.push({ begin: entries.getUint32(at, true), end: entries.getUint32(at + 4, true) });
|
||||
}
|
||||
return functions.sort((left, right) => left.begin - right.begin);
|
||||
}
|
||||
Reference in new issue
Block a user