Let the patcher report unavailable patches and build in memory

The launcher has to show the player which patches do not fit their game
build, let them go on without the features those patches cover, and
refuse when a patch that keeps the copy apart from the online game is
missing. The patcher could only refuse the whole build.

- GUI patches are applied one by one. A patch with an edit whose stock
  text is not found exactly once, or whose script is missing, is left
  out whole and reported with its features, like an unavailable exe
  patch; the others are applied.
- patchExecutable always produces the executable with the available
  patches; unavailable ones and those depending on them are listed.
- essential-features.ts names the features whose patches stop an
  installation: A-离线启动, A-与正版隔离, A-不连 Gaijin (the copy does not
  start, touches the online game's data or identity, or connects to
  Gaijin without them). Patches tagged with them are 9 exe patches and
  one GUI patch today.
- buildGameCopy builds every file of the copy in memory from the stock
  files and the caller's inputs (patch folders, hook library, port,
  store pack ids) and returns them with a structured report;
  writeGameCopy writes them, replacing each file instead of writing
  through a hard link. readStockGameFiles reads the four stock files.
- assertWritableCopy takes steamLibraryConfirmed for a Steam library the
  player chose after the warning; isInSteamLibrary is exported.

scripts/build-game-copy.ts uses buildGameCopy and still writes nothing
when any patch is unavailable, unless --skip-unavailable is given and
none of the unavailable patches is essential.

Checked with the root typecheck and three runs of build-game-copy.ts
into scratch folders: all patches as they are (12 exe, 35 GUI, 496
bundle GUIDs, six files written); one ordinary exe patch and one GUI
patch broken (both reported with their features, nothing written, then
written with --skip-unavailable); the network hook patch broken (it and
the TLS patch depending on it reported as essential, nothing written
even with --skip-unavailable).
This commit is contained in:
N0VAN0M4LY committed 2026-11-29 03:32:36 +08:00
1 parent 90cc6b0bb1
commit c8d4972cf0
8 files changed
+285 -84

No files matched your search

+34 -16
View File
@@ -1,21 +1,28 @@
/**
* Rebuild gui.vromfs.bin by applying the script patch definitions in `patches/gui/`
* Rebuild gui.vromfs.bin by applying the script patch definitions in the GUI patch folder
* (implementation notes, format in gui-patch-definition.ts).
*
* Every edit must find its stock text exactly once in the current script; anything else
* refuses the whole build rather than writing a half-patched archive.
* Every edit must find its stock text exactly once in the current script. A patch with an edit
* that does not 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 { getFile, putFile, readArchive, writeArchive } from '@wto/vromfs';
import type { PatchFailure } from '@wto/exepatch';
import { getFile, putFile, readArchive, writeArchive, type Archive } from '@wto/vromfs';
import { loadGuiPatches, type GuiScriptPatch } from './gui-patch-definition.ts';
import type { LocalEndpoint } from './network/local-endpoint.ts';
const ENDPOINT_PLACEHOLDER = '{{LOCAL_ENDPOINT}}';
export interface GuiPackResult {
/** The data pack with every applicable patch written. */
readonly pack: Uint8Array;
readonly applied: readonly GuiScriptPatch[];
readonly failures: readonly PatchFailure[];
}
/** The stock text of an edit was not found exactly once, or the target script is missing. */
class GuiPatchMismatch extends Error {}
export function rebuildGuiPack(
stockPack: Uint8Array,
endpoint: LocalEndpoint,
@@ -24,21 +31,32 @@ export function rebuildGuiPack(
const patches = loadGuiPatches(patchesFolder);
const archive = readArchive(stockPack);
const hostPortText = `${endpoint.host}:${endpoint.port}`;
const applied: GuiScriptPatch[] = [];
const failures: PatchFailure[] = [];
for (const patch of patches) {
let current = Buffer.from(getFile(archive, patch.file)).toString('utf8');
const newline = current.includes('\r\n') ? '\r\n' : '\n';
for (const [index, edit] of patch.edits.entries()) {
const replace = edit.replace.replaceAll('\n', newline).replaceAll(ENDPOINT_PLACEHOLDER, hostPortText);
current = replaceUnique(current, edit.find, replace, `GUI patch ${patch.id} edit ${index + 1} in ${patch.file}`);
try {
putFile(archive, patch.file, Buffer.from(patchedScript(archive, patch, hostPortText), 'utf8'));
applied.push(patch);
} catch (error) {
if (!(error instanceof GuiPatchMismatch)) throw error;
failures.push({ patchId: patch.id, features: patch.features, reason: error.message });
}
putFile(archive, patch.file, Buffer.from(current, 'utf8'));
}
return {
pack: writeArchive(archive),
applied: patches,
};
return { pack: writeArchive(archive), applied, failures };
}
/** The target script with every edit of `patch` made; throws GuiPatchMismatch before any change. */
function patchedScript(archive: Archive, patch: GuiScriptPatch, hostPortText: string): string {
if (!archive.files.has(patch.file)) throw new GuiPatchMismatch(`the data pack has no script ${patch.file}`);
let current = Buffer.from(getFile(archive, patch.file)).toString('utf8');
const newline = current.includes('\r\n') ? '\r\n' : '\n';
for (const [index, edit] of patch.edits.entries()) {
const replace = edit.replace.replaceAll('\n', newline).replaceAll(ENDPOINT_PLACEHOLDER, hostPortText);
current = replaceUnique(current, edit.find, replace, `edit ${index + 1} in ${patch.file}`);
}
return current;
}
/**
@@ -49,8 +67,8 @@ export function rebuildGuiPack(
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 Error(`${label}: stock text not found; refusing to write`);
if (normalized.indexOf(find, first + find.length) !== -1) throw new Error(`${label}: stock text appears more than once; refusing to write`);
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]!);
}