A game update used to fail a patch whenever any byte it compared moved, even when only an address in the instruction changed. Patch definitions now say what a change needs and read the rest from the code: - patch.jsonc holds a list of variants, each written for one game build (written_for) with its own locate block and edit; locate.jsonc is merged into it. Exactly one variant must apply, otherwise the patch is left out. GUI script patches use the same variants rule. - expect_original and the bytes to write use the signature syntax, so addresses that differ between builds can be wildcards. - A locate block can capture values at the site (rel32 targets, immediates, displacements); a rel32 fixup fills a hole in the written bytes from a capture. allow-local-host-bot-spawns now jumps to the target of the conditional jump it replaces instead of a stored distance, which makes it apply to 2.61.0.43 as well. - verified_on is gone from the definitions. patches/versions/<version>.jsonc records, per checked game build, which variant of every patch applies and where; scripts/record-game-version.ts writes it and the launcher reads the recorded versions from there. The Python tools (check-patches, patch-exe, check-gui) read the same format; check-patches assembles a variant whose assembly names captures at the site it was located at. Checked: root and launcher type checks; check-patches and check-gui on 2.61.0.37 and 2.61.0.43 (12 exe and 35 GUI patches each); check-patches and check-gui report a deliberately broken patch with a non-zero exit; build-game-copy from 2.61.0.43 into a temporary folder. The computed bytes of allow-local-host-bot-spawns on 2.61.0.37 equal the bytes it stored before.
105 lines
4.4 KiB
TypeScript
105 lines
4.4 KiB
TypeScript
/**
|
|
* Rebuild gui.vromfs.bin by applying the script patch definitions in the GUI patch folder
|
|
* (format in gui-patch-definition.ts).
|
|
*
|
|
* A variant applies when every one of its edits finds its stock text exactly once in the current
|
|
* script; exactly one variant of a patch must apply. A patch with no applicable variant is left
|
|
* out whole, never half applied, and listed with the features it covers, the same way an
|
|
* unavailable exe patch is; the other patches are applied.
|
|
*/
|
|
import { chooseVariant, PatchError, type PatchFailure } from '@wto/exepatch';
|
|
import { getFile, putFile, readArchive, writeArchive, type Archive } from '@wto/vromfs';
|
|
import { loadGuiPatches, type GuiScriptPatch, type GuiScriptVariant } from './gui-patch-definition.ts';
|
|
import type { LocalEndpoint } from './network/local-endpoint.ts';
|
|
|
|
const ENDPOINT_PLACEHOLDER = '{{LOCAL_ENDPOINT}}';
|
|
|
|
export interface AppliedGuiPatch {
|
|
readonly patch: GuiScriptPatch;
|
|
readonly variant: GuiScriptVariant;
|
|
}
|
|
|
|
export interface GuiPackResult {
|
|
/** The data pack with every applicable patch written. */
|
|
readonly pack: Uint8Array;
|
|
readonly applied: readonly AppliedGuiPatch[];
|
|
readonly failures: readonly PatchFailure[];
|
|
}
|
|
|
|
/** The stock text of an edit was not found exactly once, or the target script is missing. */
|
|
class GuiPatchMismatch extends PatchError {
|
|
override name = 'GuiPatchMismatch';
|
|
}
|
|
|
|
export function rebuildGuiPack(
|
|
stockPack: Uint8Array,
|
|
endpoint: LocalEndpoint,
|
|
patchesFolder: string,
|
|
): GuiPackResult {
|
|
const patches = loadGuiPatches(patchesFolder);
|
|
const archive = readArchive(stockPack);
|
|
const hostPortText = `${endpoint.host}:${endpoint.port}`;
|
|
const applied: AppliedGuiPatch[] = [];
|
|
const failures: PatchFailure[] = [];
|
|
|
|
for (const patch of patches) {
|
|
try {
|
|
const chosen = chooseVariant(patch.variants, (variant) => patchedScript(archive, variant, hostPortText));
|
|
putFile(archive, chosen.variant.file, Buffer.from(chosen.value, 'utf8'));
|
|
applied.push({ patch, variant: chosen.variant });
|
|
} catch (error) {
|
|
if (!(error instanceof PatchError)) throw error;
|
|
failures.push({ patchId: patch.id, features: patch.features, reason: error.message });
|
|
}
|
|
}
|
|
|
|
return { pack: writeArchive(archive), applied, failures };
|
|
}
|
|
|
|
/** The target script with every edit of `variant` made; throws GuiPatchMismatch before any change. */
|
|
function patchedScript(archive: Archive, variant: GuiScriptVariant, hostPortText: string): string {
|
|
if (!archive.files.has(variant.file)) throw new GuiPatchMismatch(`the data pack has no script ${variant.file}`);
|
|
let current = Buffer.from(getFile(archive, variant.file)).toString('utf8');
|
|
const newline = current.includes('\r\n') ? '\r\n' : '\n';
|
|
for (const [index, edit] of variant.edits.entries()) {
|
|
const replace = edit.replace.replaceAll('\n', newline).replaceAll(ENDPOINT_PLACEHOLDER, hostPortText);
|
|
current = replaceUnique(current, edit.find, replace, `edit ${index + 1} in ${variant.file}`);
|
|
}
|
|
return current;
|
|
}
|
|
|
|
/**
|
|
* Replace the single occurrence of `find` (written with `\n` line endings) in `text`, treating
|
|
* `\r\n` and `\n` in the script as the same line break; stock scripts mix both. Only the matched
|
|
* span changes, every other byte of the script is kept.
|
|
*/
|
|
function replaceUnique(text: string, find: string, replace: string, label: string): string {
|
|
const { normalized, original } = withoutCarriageReturns(text);
|
|
const first = normalized.indexOf(find);
|
|
if (first === -1) throw new GuiPatchMismatch(`${label}: stock text not found`);
|
|
if (normalized.indexOf(find, first + find.length) !== -1) throw new GuiPatchMismatch(`${label}: stock text appears more than once`);
|
|
return text.slice(0, original[first]!) + replace + text.slice(original[first + find.length]!);
|
|
}
|
|
|
|
/**
|
|
* `text` with `\r\n` turned into `\n`, and for every normalized index the index in `text` where
|
|
* that character's original sequence starts (a `\r\n` maps to its `\r`).
|
|
*/
|
|
function withoutCarriageReturns(text: string): { readonly normalized: string; readonly original: readonly number[] } {
|
|
const kept: string[] = [];
|
|
const original: number[] = [];
|
|
let index = 0;
|
|
while (index < text.length) {
|
|
original.push(index);
|
|
if (text[index] === '\r' && text[index + 1] === '\n') {
|
|
kept.push('\n');
|
|
index += 2;
|
|
} else {
|
|
kept.push(text[index]!);
|
|
index += 1;
|
|
}
|
|
}
|
|
original.push(text.length);
|
|
return { normalized: kept.join(''), original };
|
|
}
|