Files
WT-Offline/packages/patcher/src/native/native-resolve.ts
T
N0VAN0M4LY dde852e2b1 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.
2026-11-29 12:15:30 +08:00

71 lines
3.3 KiB
TypeScript

/**
* 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;
}