hoi4-agent-tools 4.11.1 → 4.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/README.md +3 -3
- package/dist/bin/setup.js +38 -0
- package/dist/bin/stdio.js +12 -10
- package/dist/hoi4_agent_tools/core/configuration.js +29 -0
- package/dist/hoi4_agent_tools/core/domain-tools.js +4 -0
- package/dist/hoi4_agent_tools/core/engine-check.js +87 -0
- package/dist/hoi4_agent_tools/core/engine-client.js +1 -1
- package/dist/hoi4_agent_tools/core/engine.js +18 -2
- package/dist/hoi4_agent_tools/core/job-operation-registration.js +2 -0
- package/dist/hoi4_agent_tools/core/job-store.js +1 -1
- package/dist/hoi4_agent_tools/focus/native-effects.js +1 -1
- package/dist/hoi4_agent_tools/localisation/job-operations.js +42 -0
- package/dist/hoi4_agent_tools/localisation/write-diff.js +96 -0
- package/dist/hoi4_agent_tools/localisation/write-format.js +329 -0
- package/dist/hoi4_agent_tools/localisation/write-operations.js +213 -0
- package/dist/hoi4_agent_tools/localisation/write-plan.js +557 -0
- package/dist/hoi4_agent_tools/map/gameplay/crossing-validation.js +17 -0
- package/dist/hoi4_agent_tools/map/model.js +31 -1
- package/dist/hoi4_agent_tools/map/operations.js +37 -8
- package/dist/hoi4_agent_tools/map/references.js +87 -0
- package/dist/hoi4_agent_tools/map/relief/hydrology.js +7 -2
- package/dist/hoi4_agent_tools/map/relief/terrain-synthesis.js +1 -0
- package/dist/hoi4_agent_tools/map/service.js +13 -4
- package/dist/hoi4_agent_tools/map/tool-operations.js +6 -0
- package/dist/hoi4_agent_tools/map/world-edge.js +0 -25
- package/dist/hoi4_agent_tools/map/world.js +181 -64
- package/dist/hoi4_agent_tools/mcp/plugins.js +74 -0
- package/dist/hoi4_agent_tools/mcp/server/argument-errors.js +5 -0
- package/dist/hoi4_agent_tools/mcp/server/create-modern-operations.js +15 -21
- package/dist/hoi4_agent_tools/mcp/server/create.js +5 -1
- package/dist/hoi4_agent_tools/mcp/server/request-lifecycle.js +10 -2
- package/dist/hoi4_agent_tools/mcp/server/task-tool-catalog.js +2 -0
- package/dist/hoi4_agent_tools/mcp/server/tool-profiles.js +83 -0
- package/dist/hoi4_agent_tools/mcp/tools/localisation-write.js +21 -0
- package/dist/hoi4_agent_tools/mcp/tools/reference.js +34 -0
- package/dist/hoi4_agent_tools/mcp/transports/http.js +6 -1
- package/dist/hoi4_agent_tools/mcp/transports/negotiated-stdio.js +64 -6
- package/dist/hoi4_agent_tools/probability/model.js +4 -4
- package/dist/hoi4_agent_tools/reference/compat-patch.js +555 -0
- package/dist/hoi4_agent_tools/reference/compat.js +255 -0
- package/dist/hoi4_agent_tools/reference/docs-pack.js +164 -0
- package/dist/hoi4_agent_tools/reference/docs-pack.json +339 -0
- package/dist/hoi4_agent_tools/reference/documented-commands.js +26 -0
- package/dist/hoi4_agent_tools/reference/error-explain.js +235 -0
- package/dist/hoi4_agent_tools/reference/error-explanations.json +262 -0
- package/dist/hoi4_agent_tools/reference/error-log.js +30 -10
- package/dist/hoi4_agent_tools/reference/mod-health.js +255 -0
- package/dist/hoi4_agent_tools/reference/mod-index.js +63 -65
- package/dist/hoi4_agent_tools/reference/operations.js +435 -21
- package/dist/hoi4_agent_tools/reference/script-guard.js +109 -0
- package/dist/hoi4_agent_tools/reference/service.js +58 -20
- package/dist/hoi4_agent_tools/runtime.js +17 -2
- package/dist/hoi4_agent_tools/schemas/compat.js +153 -0
- package/dist/hoi4_agent_tools/schemas/localisation-write.js +154 -0
- package/dist/hoi4_agent_tools/schemas/map.js +5 -1
- package/dist/hoi4_agent_tools/schemas/mod-tools.js +50 -0
- package/dist/hoi4_agent_tools/schemas/modder-tools.js +191 -0
- package/dist/hoi4_agent_tools/version.js +1 -1
- package/docs/configuration.md +14 -11
- package/docs/development.md +2 -0
- package/docs/jobs.md +3 -1
- package/docs/map.md +8 -4
- package/docs/reference.md +47 -5
- package/docs/research/probability-adapter-evidence.md +11 -1
- package/docs/setup.md +23 -3
- package/docs/tools.md +18 -4
- package/docs/troubleshooting.md +1 -1
- package/engine/bin/darwin-arm64/hoi4-engine +0 -0
- package/engine/bin/linux-arm64/hoi4-engine +0 -0
- package/engine/bin/linux-x64/hoi4-engine +0 -0
- package/engine/bin/manifest.json +8 -8
- package/engine/bin/win32-x64/hoi4-engine.exe +0 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/schemas/configuration.schema.json +181 -138
- package/schemas/continuous-focus-palette.schema.json +1 -1
- package/schemas/custom-weighted-pool.schema.json +1 -1
- package/schemas/decision-inspect-input.schema.json +1 -1
- package/schemas/error-log-input.schema.json +1 -1
- package/schemas/event-feature-manifest.schema.json +1 -1
- package/schemas/focus-plan.schema.json +1 -1
- package/schemas/focus-planning-sidecar.schema.json +1 -1
- package/schemas/geography-model.schema.json +1 -1
- package/schemas/gui-animation-source.schema.json +1 -1
- package/schemas/gui-helper.schema.json +1 -1
- package/schemas/gui-scenario.schema.json +1 -1
- package/schemas/helper-expansion-request.schema.json +1 -1
- package/schemas/helper-expansion-summary.schema.json +1 -1
- package/schemas/impact-inspect-input.schema.json +1 -1
- package/schemas/map-operation.schema.json +153 -139
- package/schemas/mechanic-test-input.schema.json +1 -1
- package/schemas/mod-index-input.schema.json +1 -1
- package/schemas/operation-result.schema.json +1 -1
- package/schemas/package-check-input.schema.json +1 -1
- package/schemas/package-manifest.schema.json +1 -1
- package/schemas/probability-analysis-result.schema.json +1 -1
- package/schemas/probability-compare-input.schema.json +1 -1
- package/schemas/probability-evaluate-input.schema.json +1 -1
- package/schemas/probability-inspect-input.schema.json +1 -1
- package/schemas/probability-render-input.schema.json +1 -1
- package/schemas/probability-scenario-set.schema.json +1 -1
- package/schemas/probability-sequence-input.schema.json +1 -1
- package/schemas/probability-simulate-input.schema.json +1 -1
- package/schemas/probability-sweep-input.schema.json +1 -1
- package/schemas/reference-context-input.schema.json +1 -1
- package/schemas/reference-read-input.schema.json +1 -1
- package/schemas/reference-search-input.schema.json +1 -1
- package/schemas/reference-sheets.schema.json +1 -1
- package/schemas/scenario-suite.schema.json +1 -1
- package/schemas/scenario-test-input.schema.json +1 -1
- package/schemas/script-validate-input.schema.json +1 -1
- package/schemas/shared-scenario.schema.json +1 -1
- package/schemas/source-lookup-input.schema.json +1 -1
- package/server.json +2 -2
- package/dist/hoi4_agent_tools/mcp/tools/chaosx.js +0 -509
|
@@ -0,0 +1,555 @@
|
|
|
1
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
2
|
+
import { createReadStream } from 'node:fs';
|
|
3
|
+
import { mkdir, readdir, readFile, rename, rm, stat, writeFile } from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { z } from 'zod/v4';
|
|
6
|
+
import { compareCodeUnits } from '../core/canonical.js';
|
|
7
|
+
import { runEngineCheck } from '../core/engine-check.js';
|
|
8
|
+
import { ServiceError } from '../core/result.js';
|
|
9
|
+
import { canonicalPath, containedGeneratedPath, isWithin } from '../core/workspace.js';
|
|
10
|
+
import { fitToBudget } from './compat.js';
|
|
11
|
+
/**
|
|
12
|
+
* `hoi4.compat_check` mode `patch` (decision R-5): what a game patch changed under the mod's
|
|
13
|
+
* overrides. Each call stores a snapshot of the vanilla files the mod overrides for the installed
|
|
14
|
+
* game build, in the workspace's cache folder and never in the mod. Two snapshots of different
|
|
15
|
+
* builds show which overridden files the patch changed, added or removed.
|
|
16
|
+
*/
|
|
17
|
+
export const SNAPSHOT_SCHEMA = 'hoi4-compat-snapshot/1';
|
|
18
|
+
export const SNAPSHOT_FOLDER = 'compat-snapshots';
|
|
19
|
+
export const MAX_COPIED_FILE_BYTES = 4 * 1024 * 1024;
|
|
20
|
+
export const MAX_SNAPSHOT_BYTES = 128 * 1024 * 1024;
|
|
21
|
+
export const MAX_SNAPSHOTS = 3;
|
|
22
|
+
const MAX_SNAPSHOT_FILES = 20_000;
|
|
23
|
+
const HUNK_CONTEXT = 3;
|
|
24
|
+
const HUNK_GAP = 6;
|
|
25
|
+
const HUNK_MAX_LINES = 40;
|
|
26
|
+
const HUNK_MAX_CHARS = 1_500;
|
|
27
|
+
const HUNK_LINE_CHARS = 160;
|
|
28
|
+
const DIFF_CELL_LIMIT = 4_000_000;
|
|
29
|
+
const TEXT_SNIFF_BYTES = 8_192;
|
|
30
|
+
const snapshotFileSchema = z
|
|
31
|
+
.object({
|
|
32
|
+
layer: z.string().max(512),
|
|
33
|
+
path: z.string().max(1024),
|
|
34
|
+
sha256: z.string().regex(/^[0-9a-f]{64}$/u),
|
|
35
|
+
size: z.number().int().min(0),
|
|
36
|
+
/** The file looks like text (no NUL byte in its start). */
|
|
37
|
+
text: z.boolean(),
|
|
38
|
+
/** A copy of the file is stored for diffing. */
|
|
39
|
+
copied: z.boolean(),
|
|
40
|
+
})
|
|
41
|
+
.strict();
|
|
42
|
+
const manifestSchema = z
|
|
43
|
+
.object({
|
|
44
|
+
schema: z.literal(SNAPSHOT_SCHEMA),
|
|
45
|
+
build: z.string().min(1).max(64),
|
|
46
|
+
sequence: z.number().int().min(1),
|
|
47
|
+
/** Files beyond the per-snapshot file limit, not recorded. */
|
|
48
|
+
unrecorded: z.number().int().min(0),
|
|
49
|
+
files: z.array(snapshotFileSchema).max(MAX_SNAPSHOT_FILES),
|
|
50
|
+
})
|
|
51
|
+
.strict();
|
|
52
|
+
const sha256Hex = (text) => createHash('sha256').update(text).digest('hex');
|
|
53
|
+
function safeRelative(raw) {
|
|
54
|
+
const relative = raw.replaceAll('\\', '/').replace(/^\/+|\/+$/gu, '');
|
|
55
|
+
if (relative.length === 0 || relative.includes('\0') || relative.includes(':'))
|
|
56
|
+
return undefined;
|
|
57
|
+
const segments = relative.split('/');
|
|
58
|
+
if (segments.some((segment) => segment === '' || segment === '.' || segment === '..'))
|
|
59
|
+
return undefined;
|
|
60
|
+
return relative;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The game and DLC files the mod replaces: the `replaced` side of the check's override entries,
|
|
64
|
+
* and the files the game and DLCs ship directly in the mod's replace_path folders.
|
|
65
|
+
*/
|
|
66
|
+
export async function overriddenVanillaFiles(report, signal) {
|
|
67
|
+
const vanilla = report.inputs.layers.filter(({ kind }) => kind === 'game' || kind === 'dlc');
|
|
68
|
+
const found = new Map();
|
|
69
|
+
const add = (layer, relative) => {
|
|
70
|
+
const key = `${layer.name}\0${relative}`;
|
|
71
|
+
if (!found.has(key))
|
|
72
|
+
found.set(key, {
|
|
73
|
+
layer: layer.name,
|
|
74
|
+
path: relative,
|
|
75
|
+
absolute: path.join(layer.path, ...relative.split('/')),
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
for (const entry of report.overrides)
|
|
79
|
+
for (const replaced of entry.replaced) {
|
|
80
|
+
const layer = vanilla.find(({ name }) => replaced.startsWith(`${name}:`));
|
|
81
|
+
const relative = layer === undefined ? undefined : safeRelative(replaced.slice(layer.name.length + 1));
|
|
82
|
+
if (layer !== undefined && relative !== undefined)
|
|
83
|
+
add(layer, relative);
|
|
84
|
+
}
|
|
85
|
+
const mod = report.inputs.layers.findLast(({ kind }) => kind === 'mod');
|
|
86
|
+
for (const raw of new Set(mod?.replace_paths ?? [])) {
|
|
87
|
+
const folder = safeRelative(raw);
|
|
88
|
+
if (folder === undefined)
|
|
89
|
+
continue;
|
|
90
|
+
for (const layer of vanilla) {
|
|
91
|
+
signal?.throwIfAborted();
|
|
92
|
+
try {
|
|
93
|
+
const directory = await canonicalPath(path.join(layer.path, ...folder.split('/')));
|
|
94
|
+
if (!isWithin(layer.path, directory))
|
|
95
|
+
continue;
|
|
96
|
+
for (const dirent of await readdir(directory, { withFileTypes: true }))
|
|
97
|
+
if (dirent.isFile())
|
|
98
|
+
add(layer, `${folder}/${dirent.name}`);
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
/* a folder the layer lacks has nothing to record */
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return [...found.values()].sort((left, right) => compareCodeUnits(left.layer, right.layer) || compareCodeUnits(left.path, right.path));
|
|
106
|
+
}
|
|
107
|
+
// ---------------------------------------------------------------------------------------------
|
|
108
|
+
// Snapshot storage
|
|
109
|
+
function snapshotsRoot(workspace) {
|
|
110
|
+
return containedGeneratedPath(workspace.cacheRoot, SNAPSHOT_FOLDER);
|
|
111
|
+
}
|
|
112
|
+
async function directorySize(directory) {
|
|
113
|
+
let total = 0;
|
|
114
|
+
try {
|
|
115
|
+
for (const dirent of await readdir(path.join(directory, 'files'), { withFileTypes: true }))
|
|
116
|
+
if (dirent.isFile())
|
|
117
|
+
total += (await stat(path.join(directory, 'files', dirent.name))).size;
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
/* an unreadable snapshot is sized as empty */
|
|
121
|
+
}
|
|
122
|
+
return total;
|
|
123
|
+
}
|
|
124
|
+
/** Every valid snapshot in the cache folder, newest first. A damaged one is ignored. */
|
|
125
|
+
export async function listSnapshots(workspace) {
|
|
126
|
+
const root = await snapshotsRoot(workspace);
|
|
127
|
+
let names;
|
|
128
|
+
try {
|
|
129
|
+
names = await readdir(root);
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
return [];
|
|
133
|
+
}
|
|
134
|
+
const snapshots = [];
|
|
135
|
+
for (const name of names) {
|
|
136
|
+
if (!/^[0-9a-f]{16}$/u.test(name))
|
|
137
|
+
continue;
|
|
138
|
+
const directory = path.join(root, name);
|
|
139
|
+
try {
|
|
140
|
+
const manifest = manifestSchema.parse(JSON.parse(await readFile(path.join(directory, 'manifest.json'), 'utf8')));
|
|
141
|
+
if (directoryKey(manifest.build) !== name)
|
|
142
|
+
continue;
|
|
143
|
+
snapshots.push({ manifest, directory, bytes: await directorySize(directory) });
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
/* not a snapshot this tool wrote */
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return snapshots.sort((left, right) => right.manifest.sequence - left.manifest.sequence);
|
|
150
|
+
}
|
|
151
|
+
function directoryKey(build) {
|
|
152
|
+
return sha256Hex(build).slice(0, 16);
|
|
153
|
+
}
|
|
154
|
+
async function hashFile(file, maxCopied) {
|
|
155
|
+
try {
|
|
156
|
+
const info = await stat(file);
|
|
157
|
+
if (!info.isFile())
|
|
158
|
+
return undefined;
|
|
159
|
+
if (info.size <= maxCopied) {
|
|
160
|
+
const content = await readFile(file);
|
|
161
|
+
return {
|
|
162
|
+
sha256: createHash('sha256').update(content).digest('hex'),
|
|
163
|
+
size: content.length,
|
|
164
|
+
text: !content.subarray(0, TEXT_SNIFF_BYTES).includes(0),
|
|
165
|
+
content,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
const hash = createHash('sha256');
|
|
169
|
+
let head = Buffer.alloc(0);
|
|
170
|
+
for await (const chunk of createReadStream(file)) {
|
|
171
|
+
hash.update(chunk);
|
|
172
|
+
if (head.length < TEXT_SNIFF_BYTES)
|
|
173
|
+
head = Buffer.concat([head, chunk.subarray(0, TEXT_SNIFF_BYTES - head.length)]);
|
|
174
|
+
}
|
|
175
|
+
return { sha256: hash.digest('hex'), size: info.size, text: !head.includes(0) };
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Store the snapshot of `build` and prune the oldest beyond three. A snapshot of the same build
|
|
183
|
+
* is replaced. The new one is written beside the others and renamed into place, so a failed
|
|
184
|
+
* write leaves the old snapshots as they were.
|
|
185
|
+
*/
|
|
186
|
+
export async function recordSnapshot(workspace, build, files, signal, limits = {}) {
|
|
187
|
+
const maxCopiedFileBytes = limits.maxCopiedFileBytes ?? MAX_COPIED_FILE_BYTES;
|
|
188
|
+
const maxSnapshotBytes = limits.maxSnapshotBytes ?? MAX_SNAPSHOT_BYTES;
|
|
189
|
+
const root = await snapshotsRoot(workspace);
|
|
190
|
+
await mkdir(root, { recursive: true });
|
|
191
|
+
const existing = await listSnapshots(workspace);
|
|
192
|
+
const sequence = Math.max(0, ...existing.map(({ manifest }) => manifest.sequence)) + 1;
|
|
193
|
+
const key = directoryKey(build);
|
|
194
|
+
const staging = path.join(root, `.writing-${key}-${randomBytes(6).toString('hex')}`);
|
|
195
|
+
await mkdir(path.join(staging, 'files'), { recursive: true });
|
|
196
|
+
try {
|
|
197
|
+
const recorded = [];
|
|
198
|
+
const stored = new Set();
|
|
199
|
+
let copiedBytes = 0;
|
|
200
|
+
for (const file of files.slice(0, MAX_SNAPSHOT_FILES)) {
|
|
201
|
+
signal?.throwIfAborted();
|
|
202
|
+
const hashed = await hashFile(file.absolute, maxCopiedFileBytes);
|
|
203
|
+
if (hashed === undefined)
|
|
204
|
+
continue;
|
|
205
|
+
let copied = false;
|
|
206
|
+
if (hashed.text && hashed.content !== undefined) {
|
|
207
|
+
if (stored.has(hashed.sha256))
|
|
208
|
+
copied = true;
|
|
209
|
+
else if (copiedBytes + hashed.size <= maxSnapshotBytes) {
|
|
210
|
+
await writeFile(path.join(staging, 'files', hashed.sha256), hashed.content);
|
|
211
|
+
stored.add(hashed.sha256);
|
|
212
|
+
copiedBytes += hashed.size;
|
|
213
|
+
copied = true;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
recorded.push({
|
|
217
|
+
layer: file.layer,
|
|
218
|
+
path: file.path,
|
|
219
|
+
sha256: hashed.sha256,
|
|
220
|
+
size: hashed.size,
|
|
221
|
+
text: hashed.text,
|
|
222
|
+
copied,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
const manifest = {
|
|
226
|
+
schema: SNAPSHOT_SCHEMA,
|
|
227
|
+
build,
|
|
228
|
+
sequence,
|
|
229
|
+
unrecorded: Math.max(0, files.length - MAX_SNAPSHOT_FILES),
|
|
230
|
+
files: recorded,
|
|
231
|
+
};
|
|
232
|
+
await writeFile(path.join(staging, 'manifest.json'), `${JSON.stringify(manifest)}\n`);
|
|
233
|
+
const final = path.join(root, key);
|
|
234
|
+
await rm(final, { recursive: true, force: true });
|
|
235
|
+
await rename(staging, final);
|
|
236
|
+
const pruned = [];
|
|
237
|
+
const all = await listSnapshots(workspace);
|
|
238
|
+
for (const old of all.slice(MAX_SNAPSHOTS)) {
|
|
239
|
+
await rm(old.directory, { recursive: true, force: true });
|
|
240
|
+
pruned.push(old.manifest.build);
|
|
241
|
+
}
|
|
242
|
+
return { manifest, bytes: copiedBytes, pruned };
|
|
243
|
+
}
|
|
244
|
+
catch (error) {
|
|
245
|
+
await rm(staging, { recursive: true, force: true });
|
|
246
|
+
throw error;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
function splitLines(text) {
|
|
250
|
+
const body = text.startsWith('') ? text.slice(1) : text;
|
|
251
|
+
const lines = body.split(/\r?\n/u);
|
|
252
|
+
if (lines.at(-1) === '')
|
|
253
|
+
lines.pop();
|
|
254
|
+
return lines;
|
|
255
|
+
}
|
|
256
|
+
/** Line operations from the older text to the newer; a common prefix and suffix are trimmed first. */
|
|
257
|
+
export function diffLines(older, newer) {
|
|
258
|
+
let start = 0;
|
|
259
|
+
while (start < older.length && start < newer.length && older[start] === newer[start])
|
|
260
|
+
start += 1;
|
|
261
|
+
let endOld = older.length;
|
|
262
|
+
let endNew = newer.length;
|
|
263
|
+
while (endOld > start && endNew > start && older[endOld - 1] === newer[endNew - 1]) {
|
|
264
|
+
endOld -= 1;
|
|
265
|
+
endNew -= 1;
|
|
266
|
+
}
|
|
267
|
+
const operations = older.slice(0, start).map((line) => ({ kind: ' ', line }));
|
|
268
|
+
const a = older.slice(start, endOld);
|
|
269
|
+
const b = newer.slice(start, endNew);
|
|
270
|
+
if (a.length * b.length > DIFF_CELL_LIMIT) {
|
|
271
|
+
// Too large for a line-by-line match: the changed middle is one replacement.
|
|
272
|
+
for (const line of a)
|
|
273
|
+
operations.push({ kind: '-', line });
|
|
274
|
+
for (const line of b)
|
|
275
|
+
operations.push({ kind: '+', line });
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
const width = b.length + 1;
|
|
279
|
+
const table = new Uint32Array((a.length + 1) * width);
|
|
280
|
+
for (let i = a.length - 1; i >= 0; i -= 1)
|
|
281
|
+
for (let j = b.length - 1; j >= 0; j -= 1)
|
|
282
|
+
table[i * width + j] =
|
|
283
|
+
a[i] === b[j]
|
|
284
|
+
? (table[(i + 1) * width + j + 1] ?? 0) + 1
|
|
285
|
+
: Math.max(table[(i + 1) * width + j] ?? 0, table[i * width + j + 1] ?? 0);
|
|
286
|
+
let i = 0;
|
|
287
|
+
let j = 0;
|
|
288
|
+
while (i < a.length && j < b.length) {
|
|
289
|
+
if (a[i] === b[j]) {
|
|
290
|
+
operations.push({ kind: ' ', line: a[i] ?? '' });
|
|
291
|
+
i += 1;
|
|
292
|
+
j += 1;
|
|
293
|
+
}
|
|
294
|
+
else if ((table[(i + 1) * width + j] ?? 0) >= (table[i * width + j + 1] ?? 0)) {
|
|
295
|
+
operations.push({ kind: '-', line: a[i] ?? '' });
|
|
296
|
+
i += 1;
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
operations.push({ kind: '+', line: b[j] ?? '' });
|
|
300
|
+
j += 1;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
for (; i < a.length; i += 1)
|
|
304
|
+
operations.push({ kind: '-', line: a[i] ?? '' });
|
|
305
|
+
for (; j < b.length; j += 1)
|
|
306
|
+
operations.push({ kind: '+', line: b[j] ?? '' });
|
|
307
|
+
}
|
|
308
|
+
for (const line of older.slice(endOld))
|
|
309
|
+
operations.push({ kind: ' ', line });
|
|
310
|
+
return operations;
|
|
311
|
+
}
|
|
312
|
+
/** Line counts and the first hunk of a unified diff between two texts. */
|
|
313
|
+
export function describeTextChange(older, newer) {
|
|
314
|
+
const oldLines = splitLines(older);
|
|
315
|
+
const newLines = splitLines(newer);
|
|
316
|
+
const operations = diffLines(oldLines, newLines);
|
|
317
|
+
const linesAdded = operations.filter(({ kind }) => kind === '+').length;
|
|
318
|
+
const linesRemoved = operations.filter(({ kind }) => kind === '-').length;
|
|
319
|
+
const first = operations.findIndex(({ kind }) => kind !== ' ');
|
|
320
|
+
if (first < 0)
|
|
321
|
+
return {
|
|
322
|
+
oldLines: oldLines.length,
|
|
323
|
+
newLines: newLines.length,
|
|
324
|
+
linesAdded,
|
|
325
|
+
linesRemoved,
|
|
326
|
+
firstHunk: null,
|
|
327
|
+
};
|
|
328
|
+
const begin = Math.max(0, first - HUNK_CONTEXT);
|
|
329
|
+
let lastChange = first;
|
|
330
|
+
let gap = 0;
|
|
331
|
+
for (let index = first; index < operations.length; index += 1) {
|
|
332
|
+
if (operations[index]?.kind === ' ') {
|
|
333
|
+
gap += 1;
|
|
334
|
+
if (gap > HUNK_GAP)
|
|
335
|
+
break;
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
gap = 0;
|
|
339
|
+
lastChange = index;
|
|
340
|
+
}
|
|
341
|
+
if (index - begin >= HUNK_MAX_LINES) {
|
|
342
|
+
lastChange = Math.min(lastChange, index);
|
|
343
|
+
break;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
const end = Math.min(operations.length, lastChange + HUNK_CONTEXT + 1, begin + HUNK_MAX_LINES);
|
|
347
|
+
const window = operations.slice(begin, end);
|
|
348
|
+
const before = operations.slice(0, begin);
|
|
349
|
+
const oldStart = before.filter(({ kind }) => kind !== '+').length + 1;
|
|
350
|
+
const newStart = before.filter(({ kind }) => kind !== '-').length + 1;
|
|
351
|
+
const oldCount = window.filter(({ kind }) => kind !== '+').length;
|
|
352
|
+
const newCount = window.filter(({ kind }) => kind !== '-').length;
|
|
353
|
+
const clipLine = (line) => line.length > HUNK_LINE_CHARS ? `${line.slice(0, HUNK_LINE_CHARS - 1)}…` : line;
|
|
354
|
+
let hunk = `@@ -${oldStart},${oldCount} +${newStart},${newCount} @@\n${window
|
|
355
|
+
.map(({ kind, line }) => `${kind}${clipLine(line)}`)
|
|
356
|
+
.join('\n')}`;
|
|
357
|
+
if (hunk.length > HUNK_MAX_CHARS)
|
|
358
|
+
hunk = `${hunk.slice(0, HUNK_MAX_CHARS - 1)}…`;
|
|
359
|
+
return {
|
|
360
|
+
oldLines: oldLines.length,
|
|
361
|
+
newLines: newLines.length,
|
|
362
|
+
linesAdded,
|
|
363
|
+
linesRemoved,
|
|
364
|
+
firstHunk: hunk,
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
const CHANGE_ORDER = { changed: 0, removed: 1, added: 2 };
|
|
368
|
+
async function copyText(snapshot, file) {
|
|
369
|
+
if (!file.copied)
|
|
370
|
+
return undefined;
|
|
371
|
+
try {
|
|
372
|
+
return await readFile(path.join(snapshot.directory, 'files', file.sha256), 'utf8');
|
|
373
|
+
}
|
|
374
|
+
catch {
|
|
375
|
+
return undefined;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* What changed in the vanilla files between two snapshots. `modShips` tells whether the mod still
|
|
380
|
+
* ships a file at a path the older snapshot recorded: only then did vanilla remove the file,
|
|
381
|
+
* otherwise the mod simply no longer overrides it.
|
|
382
|
+
*/
|
|
383
|
+
export async function compareSnapshots(older, newer, modShips, signal) {
|
|
384
|
+
const key = (file) => `${file.layer}\0${file.path}`;
|
|
385
|
+
const before = new Map(older.manifest.files.map((file) => [key(file), file]));
|
|
386
|
+
const after = new Map(newer.manifest.files.map((file) => [key(file), file]));
|
|
387
|
+
const result = {
|
|
388
|
+
from: older.manifest.build,
|
|
389
|
+
to: newer.manifest.build,
|
|
390
|
+
changed: 0,
|
|
391
|
+
added: 0,
|
|
392
|
+
removed: 0,
|
|
393
|
+
unchanged: 0,
|
|
394
|
+
noLongerOverridden: 0,
|
|
395
|
+
files: [],
|
|
396
|
+
};
|
|
397
|
+
const row = (file, change, extra = {}) => ({
|
|
398
|
+
path: file.path,
|
|
399
|
+
layer: file.layer,
|
|
400
|
+
change,
|
|
401
|
+
binary: !file.text,
|
|
402
|
+
oldLines: null,
|
|
403
|
+
newLines: null,
|
|
404
|
+
linesAdded: null,
|
|
405
|
+
linesRemoved: null,
|
|
406
|
+
firstHunk: null,
|
|
407
|
+
...extra,
|
|
408
|
+
});
|
|
409
|
+
const keys = [...new Set([...before.keys(), ...after.keys()])].sort(compareCodeUnits);
|
|
410
|
+
const limit = 400;
|
|
411
|
+
for (const name of keys) {
|
|
412
|
+
signal?.throwIfAborted();
|
|
413
|
+
const oldFile = before.get(name);
|
|
414
|
+
const newFile = after.get(name);
|
|
415
|
+
if (oldFile !== undefined && newFile !== undefined) {
|
|
416
|
+
if (oldFile.sha256 === newFile.sha256) {
|
|
417
|
+
result.unchanged += 1;
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
result.changed += 1;
|
|
421
|
+
if (result.files.length >= limit)
|
|
422
|
+
continue;
|
|
423
|
+
const [oldText, newText] = await Promise.all([
|
|
424
|
+
copyText(older, oldFile),
|
|
425
|
+
copyText(newer, newFile),
|
|
426
|
+
]);
|
|
427
|
+
result.files.push(row(newFile, 'changed', {
|
|
428
|
+
binary: !(oldFile.text && newFile.text),
|
|
429
|
+
...(oldText !== undefined && newText !== undefined
|
|
430
|
+
? describeTextChange(oldText, newText)
|
|
431
|
+
: {}),
|
|
432
|
+
}));
|
|
433
|
+
}
|
|
434
|
+
else if (newFile !== undefined) {
|
|
435
|
+
result.added += 1;
|
|
436
|
+
if (result.files.length < limit) {
|
|
437
|
+
const text = await copyText(newer, newFile);
|
|
438
|
+
result.files.push(row(newFile, 'added', text === undefined ? {} : { newLines: splitLines(text).length }));
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
else if (oldFile !== undefined) {
|
|
442
|
+
if (await modShips(oldFile.path)) {
|
|
443
|
+
result.removed += 1;
|
|
444
|
+
if (result.files.length < limit) {
|
|
445
|
+
const text = await copyText(older, oldFile);
|
|
446
|
+
result.files.push(row(oldFile, 'removed', text === undefined ? {} : { oldLines: splitLines(text).length }));
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
else
|
|
450
|
+
result.noLongerOverridden += 1;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
result.files.sort((left, right) => CHANGE_ORDER[left.change] - CHANGE_ORDER[right.change] ||
|
|
454
|
+
compareCodeUnits(left.layer, right.layer) ||
|
|
455
|
+
compareCodeUnits(left.path, right.path));
|
|
456
|
+
return result;
|
|
457
|
+
}
|
|
458
|
+
// ---------------------------------------------------------------------------------------------
|
|
459
|
+
// The tool
|
|
460
|
+
function snapshotInfo(snapshot) {
|
|
461
|
+
const copied = snapshot.manifest.files.filter(({ copied: kept }) => kept).length;
|
|
462
|
+
return {
|
|
463
|
+
build: snapshot.manifest.build,
|
|
464
|
+
files: snapshot.manifest.files.length,
|
|
465
|
+
copiedFiles: copied,
|
|
466
|
+
uncopiedFiles: snapshot.manifest.files.length - copied,
|
|
467
|
+
bytes: snapshot.bytes,
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
function missingSnapshot(build, stored) {
|
|
471
|
+
const builds = stored.map(({ manifest }) => manifest.build);
|
|
472
|
+
return new ServiceError('COMPAT_SNAPSHOT_NOT_FOUND', `No snapshot is stored for game build ${build}${builds.length === 0 ? '' : `; stored builds: ${builds.join(', ')}`}`, { build, stored: builds });
|
|
473
|
+
}
|
|
474
|
+
export async function checkPatchCompatibility(workspace, input, signal, services = {}) {
|
|
475
|
+
const started = Date.now();
|
|
476
|
+
if (workspace.registration.kind !== 'mod')
|
|
477
|
+
throw new ServiceError('COMPAT_NEEDS_MOD', 'compat_check needs a mod workspace');
|
|
478
|
+
const workspaceRef = { id: workspace.id, name: workspace.name };
|
|
479
|
+
const modShips = async (relative) => {
|
|
480
|
+
const safe = safeRelative(relative);
|
|
481
|
+
if (safe === undefined)
|
|
482
|
+
return false;
|
|
483
|
+
try {
|
|
484
|
+
const canonical = await canonicalPath(path.join(workspace.modRoot, ...safe.split('/')));
|
|
485
|
+
return isWithin(workspace.modRoot, canonical) && (await stat(canonical)).isFile();
|
|
486
|
+
}
|
|
487
|
+
catch {
|
|
488
|
+
return false;
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
let installedBuild = null;
|
|
492
|
+
let recorded = null;
|
|
493
|
+
let stored = await listSnapshots(workspace);
|
|
494
|
+
if (input.from === undefined || input.to === undefined) {
|
|
495
|
+
if (workspace.gameRoot === undefined)
|
|
496
|
+
throw new ServiceError('COMPAT_NO_GAME_ROOT', 'The workspace has no game root to compare');
|
|
497
|
+
const report = await (services.runCheck ?? runEngineCheck)({
|
|
498
|
+
mod: workspace.modRoot,
|
|
499
|
+
game: workspace.gameRoot,
|
|
500
|
+
dependencies: workspace.dependencyRoots,
|
|
501
|
+
includeDlc: workspace.dlcLayers.length > 0,
|
|
502
|
+
}, signal === undefined ? {} : { signal });
|
|
503
|
+
const build = report.inputs.game_version;
|
|
504
|
+
if (build === null || build === undefined || build.length === 0)
|
|
505
|
+
throw new ServiceError('COMPAT_GAME_BUILD_UNKNOWN', 'The engine could not read the installed game build, so no snapshot can be named');
|
|
506
|
+
installedBuild = build;
|
|
507
|
+
recorded = await recordSnapshot(workspace, build, await overriddenVanillaFiles(report, signal), signal);
|
|
508
|
+
stored = await listSnapshots(workspace);
|
|
509
|
+
}
|
|
510
|
+
const target = input.to ?? installedBuild;
|
|
511
|
+
const to = stored.find(({ manifest }) => manifest.build === target);
|
|
512
|
+
if (to === undefined)
|
|
513
|
+
throw missingSnapshot(target ?? '', stored);
|
|
514
|
+
const fromSnapshot = input.from === undefined
|
|
515
|
+
? stored.find(({ manifest }) => manifest.build !== to.manifest.build)
|
|
516
|
+
: stored.find(({ manifest }) => manifest.build === input.from);
|
|
517
|
+
if (input.from !== undefined && fromSnapshot === undefined)
|
|
518
|
+
throw missingSnapshot(input.from, stored);
|
|
519
|
+
if (fromSnapshot?.manifest.build === to.manifest.build)
|
|
520
|
+
throw new ServiceError('COMPAT_SNAPSHOT_SAME_BUILD', 'from and to name the same game build');
|
|
521
|
+
const comparison = fromSnapshot === undefined ? null : await compareSnapshots(fromSnapshot, to, modShips, signal);
|
|
522
|
+
const build = (limit) => {
|
|
523
|
+
const listed = comparison?.files.slice(0, limit) ?? [];
|
|
524
|
+
const total = comparison === null ? 0 : comparison.changed + comparison.added + comparison.removed;
|
|
525
|
+
const summary = comparison === null
|
|
526
|
+
? `Recorded the baseline for game build ${to.manifest.build}: ${to.manifest.files.length} vanilla files the mod overrides. Run again after the next game update to see which overrides the patch made stale.`
|
|
527
|
+
: `Between game builds ${comparison.from} and ${comparison.to}: ${comparison.changed} overridden files changed, ${comparison.added} were added and ${comparison.removed} removed in vanilla, ${comparison.unchanged} are unchanged. A changed file may mean the mod's override is stale.`;
|
|
528
|
+
return {
|
|
529
|
+
mode: 'patch',
|
|
530
|
+
workspace: workspaceRef,
|
|
531
|
+
installedBuild,
|
|
532
|
+
recorded: recorded === null
|
|
533
|
+
? null
|
|
534
|
+
: snapshotInfo({ manifest: recorded.manifest, bytes: recorded.bytes }),
|
|
535
|
+
pruned: recorded?.pruned ?? [],
|
|
536
|
+
stored: stored.slice(0, 8).map(snapshotInfo),
|
|
537
|
+
comparison: comparison === null
|
|
538
|
+
? null
|
|
539
|
+
: {
|
|
540
|
+
from: comparison.from,
|
|
541
|
+
to: comparison.to,
|
|
542
|
+
changed: comparison.changed,
|
|
543
|
+
added: comparison.added,
|
|
544
|
+
removed: comparison.removed,
|
|
545
|
+
unchanged: comparison.unchanged,
|
|
546
|
+
noLongerOverridden: comparison.noLongerOverridden,
|
|
547
|
+
omitted: Math.max(0, total - listed.length),
|
|
548
|
+
files: listed,
|
|
549
|
+
},
|
|
550
|
+
summary,
|
|
551
|
+
durationMs: Date.now() - started,
|
|
552
|
+
};
|
|
553
|
+
};
|
|
554
|
+
return fitToBudget(build, input.limit);
|
|
555
|
+
}
|