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