dsh-novel-game 0.2.0 → 0.2.1
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 +1 -1
- package/docs/REVIEW-0.1.0.md +39 -0
- package/index.js +3 -3
- package/lib/adapters/kirikiri.mjs +6 -7
- package/lib/adapters/renpy.mjs +14 -11
- package/lib/core/ids.mjs +2 -2
- package/lib/core/paths.mjs +13 -0
- package/lib/ingest.mjs +74 -5
- package/lib/jobs.mjs +5 -2
- package/lib/store.mjs +16 -4
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
+
## 0.2.1 — 2026-11-29
|
|
4
|
+
|
|
5
|
+
修复 Codex 对 0.1.0 代码审查发现的 P0/P1(记录:`docs/REVIEW-0.1.0.md`)。
|
|
6
|
+
|
|
7
|
+
### 修复
|
|
8
|
+
|
|
9
|
+
- **任务 ID 路径穿越**:`jobs` 读写先校验 jobId 只允许 `[A-Za-z0-9_-]`,并走插件根路径守卫。
|
|
10
|
+
- **游戏/插件目录互相包含**:工作区在游戏目录内、或游戏目录在 `.dsh-novel-game/` 内,直接拒绝(防止写入落进游戏目录)。
|
|
11
|
+
- **同标题不同目录共用 gameId**:检测到冲突时把游戏根路径掺进 gameId,两个副本不再互相覆盖。
|
|
12
|
+
- **并发导入**:同一游戏根加文件锁(`locks/<hash>.lock`,10 分钟过期),冲突返回 `BUSY`。
|
|
13
|
+
- **manifest 损坏**:读 manifest 区分「不存在」与「损坏」;损坏时抛 `IO_ERROR`,不再当首次导入、不会 prune 掉旧代。
|
|
14
|
+
- **归档增量丢内容**:`.rpa`/`.xp3` 内文本统一用外层归档作为容器键(条目名进 `src.path`),增量重导不再丢归档内剧情。
|
|
15
|
+
- **删除来源残留**:合并旧聚合前先与当前容器集合求交,删掉的地图/脚本不再留下场景、选项、词条。
|
|
16
|
+
- **Ren'Py 菜单分支体**:选项块里的对白/跳转继续解析(只跳过选项标签行);label 边界重置菜单提示。
|
|
17
|
+
- **复用与计数**:分片复制失败回退重抽;行/资产/未解析/条件计数从新代分片重算,不再报 0;词条型文件无分片时不误判失败。
|
|
18
|
+
- **只读探测副作用**:`game_detect` 不再创建 `games/`、`jobs/`。
|
|
19
|
+
|
|
20
|
+
### 测试
|
|
21
|
+
|
|
22
|
+
- 25 项全过(新增 7 项回归:归档增量、删除来源、manifest 损坏、jobId 越界、重复 gameId、目录边界、菜单分支体)。
|
|
23
|
+
|
|
3
24
|
## 0.2.0 — 2026-11-29
|
|
4
25
|
|
|
5
26
|
新增 KiriKiri / NScripter 两个 Galgame 引擎、Wolf RPG 文本抢救、剧情路线图与摘录失效标记。
|
package/README.md
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# dsh-novel-game — Codex 0.1.0 代码审查与修复(2026-11-29)
|
|
2
|
+
|
|
3
|
+
> 审查:桌面端 Codex,只读,按 P0/P1/P2 带文件行号输出(`tmp/2026-11-29-game-0.1.0-Codex审查任务书.md`)。
|
|
4
|
+
> 修复版本:**0.2.1**。测试:25/25 通过。
|
|
5
|
+
|
|
6
|
+
## 已修(P0/P1)
|
|
7
|
+
|
|
8
|
+
| Codex 结论 | 修复 |
|
|
9
|
+
|---|---|
|
|
10
|
+
| 任务 ID 路径越界 | `jobs` 读写前 `assertSafeId` + `pluginPath`,`../` 直接 `INVALID_INPUT` |
|
|
11
|
+
| 存储根/游戏目录边界未验证 | `assertSeparateRoots`:游戏目录在插件目录内、或工作区在游戏目录内,都拒绝 |
|
|
12
|
+
| 重复 gameId 冲突 | 同指纹不同 root 时把 root 掺进 `gameId`(`gameIdOf` 支持 salt) |
|
|
13
|
+
| 并发导入丢更新 | 按游戏根加文件锁,10 分钟过期,冲突 `BUSY` |
|
|
14
|
+
| 损坏 manifest 后旧代可能被 prune | `readManifest` 区分 ENOENT 与损坏;损坏抛 `IO_ERROR`,不进入 finalize |
|
|
15
|
+
| RPA 增量重导丢内容 | 归档内文本统一外层归档 relPath,条目名进 `src.path`;场景/分片/覆盖率同一容器键 |
|
|
16
|
+
| 删除来源残留 | 合并旧聚合前与当前容器集合求交 |
|
|
17
|
+
| Ren'Py 菜单分支体漏解析 | 只跳过选项标签行,分支体继续解析;label 边界重置 `lastText` |
|
|
18
|
+
|
|
19
|
+
## 顺带修(P2)
|
|
20
|
+
|
|
21
|
+
- 复用分片复制失败 → 回退重抽,不再标 `reused`。
|
|
22
|
+
- 计数从新代分片重算(修复复用文件 `lines/assets/skipped/conditions` 报 0)。
|
|
23
|
+
- 词条型文件无分片时不再误判复制失败。
|
|
24
|
+
- `game_detect` 不再创建目录(只读探测真正只读)。
|
|
25
|
+
- generation 保留数改为「当前代 + 上一代」总 2 代,与注释一致。
|
|
26
|
+
|
|
27
|
+
## 未修 / 排到 0.3.0
|
|
28
|
+
|
|
29
|
+
| 结论 | 原因 |
|
|
30
|
+
|---|---|
|
|
31
|
+
| 搜索/导出全量扫描、内存峰值 | 需要索引与流式导出,0.3.0 统一做 |
|
|
32
|
+
| 通用嗅探的 JSON/CSV 启发式误判 | 需要更强的置信度与人工确认流 |
|
|
33
|
+
| KiriKiri `[link]` 选项未建边 | 需要解析 link 块结构 |
|
|
34
|
+
| Wolf 文本抢救噪声 | 低置信已隔离,后续做白名单/词典过滤 |
|
|
35
|
+
| 游戏目录只读的 OS 级保证 | 目前是代码级只读;OS 级只读需要平台能力 |
|
|
36
|
+
|
|
37
|
+
## 结论
|
|
38
|
+
|
|
39
|
+
P0 全部修复;P1 全部修复;P2 中影响正确性的部分已修,性能与启发式问题登记到 0.3.0。发布 0.2.1。
|
package/index.js
CHANGED
|
@@ -41,9 +41,9 @@ async function requireDir(args, exec) {
|
|
|
41
41
|
return dir;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
async function serviceFor(dir) {
|
|
44
|
+
async function serviceFor(dir, { init = true } = {}) {
|
|
45
45
|
const service = new GameService({ workspace: dir });
|
|
46
|
-
await service.init();
|
|
46
|
+
if (init) await service.init();
|
|
47
47
|
return service;
|
|
48
48
|
}
|
|
49
49
|
|
|
@@ -89,7 +89,7 @@ const tools = [
|
|
|
89
89
|
},
|
|
90
90
|
output,
|
|
91
91
|
execute: guarded(async (args, exec) => {
|
|
92
|
-
const service = await serviceFor(await requireDir(args, exec));
|
|
92
|
+
const service = await serviceFor(await requireDir(args, exec), { init: false });
|
|
93
93
|
return service.detect({ gameRoot: path.resolve(args.path) });
|
|
94
94
|
}),
|
|
95
95
|
},
|
|
@@ -5,7 +5,7 @@ import { isXp3, readXp3Entry, readXp3Index } from '../archive/xp3.mjs';
|
|
|
5
5
|
* KAG 剧本解析(KiriKiri .ks)。
|
|
6
6
|
* 只做静态解析:标签、文本行、跳转;不解释表达式、不执行。
|
|
7
7
|
*/
|
|
8
|
-
export function* parseKagScript(text, { relPath }) {
|
|
8
|
+
export function* parseKagScript(text, { relPath, pathPrefix = '' }) {
|
|
9
9
|
const lines = String(text).split(/\r?\n/);
|
|
10
10
|
let sceneKey = null;
|
|
11
11
|
let sceneOrder = 0;
|
|
@@ -14,8 +14,8 @@ export function* parseKagScript(text, { relPath }) {
|
|
|
14
14
|
|
|
15
15
|
const ensureScene = function* () {
|
|
16
16
|
if (sceneKey) return;
|
|
17
|
-
sceneKey = `${relPath}
|
|
18
|
-
yield { type: 'scene', key: sceneKey, title: '开场', container: relPath, order: sceneOrder++, conditions: null, src: { relPath, path:
|
|
17
|
+
sceneKey = `${relPath}#${pathPrefix}top`;
|
|
18
|
+
yield { type: 'scene', key: sceneKey, title: '开场', container: relPath, order: sceneOrder++, conditions: null, src: { relPath, path: `${pathPrefix}top` } };
|
|
19
19
|
};
|
|
20
20
|
|
|
21
21
|
for (let index = 0; index < lines.length; index += 1) {
|
|
@@ -23,12 +23,12 @@ export function* parseKagScript(text, { relPath }) {
|
|
|
23
23
|
const trimmed = raw.trim();
|
|
24
24
|
if (!trimmed) continue;
|
|
25
25
|
if (trimmed.startsWith(';') || trimmed.startsWith('//')) continue;
|
|
26
|
-
const src = { relPath, path:
|
|
26
|
+
const src = { relPath, path: `${pathPrefix}line:${index + 1}`, index };
|
|
27
27
|
|
|
28
28
|
const labelMatch = /^\*([A-Za-z_][\w.-]*)\s*$/.exec(trimmed);
|
|
29
29
|
if (labelMatch) {
|
|
30
30
|
label = labelMatch[1];
|
|
31
|
-
sceneKey = `${relPath}
|
|
31
|
+
sceneKey = `${relPath}#${pathPrefix}label:${label}`;
|
|
32
32
|
lineOrder = 0;
|
|
33
33
|
yield { type: 'scene', key: sceneKey, title: label, container: relPath, order: sceneOrder++, conditions: null, src };
|
|
34
34
|
continue;
|
|
@@ -185,8 +185,7 @@ export const kirikiri = {
|
|
|
185
185
|
yield { type: 'error', relPath: `${rel}!${entry.name}`, message: error.message };
|
|
186
186
|
continue;
|
|
187
187
|
}
|
|
188
|
-
|
|
189
|
-
yield* parseKagScript(content.toString('utf8'), { relPath: virtual });
|
|
188
|
+
yield* parseKagScript(content.toString('utf8'), { relPath: rel, pathPrefix: `${entry.name}:` });
|
|
190
189
|
}
|
|
191
190
|
}
|
|
192
191
|
},
|
package/lib/adapters/renpy.mjs
CHANGED
|
@@ -71,14 +71,14 @@ function parseMenuBlock(lines, startIndex) {
|
|
|
71
71
|
}
|
|
72
72
|
options.push({ id: options.length, text, targetLabel });
|
|
73
73
|
}
|
|
74
|
-
return { end, options };
|
|
74
|
+
return { end, options, optionLines: optionLines.map((entry) => entry.index) };
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
/**
|
|
78
78
|
* 解析一个 Ren'Py 脚本(明文 .rpy)。
|
|
79
79
|
* 只做静态解析:不执行 $ / python: 块,不求值条件;screen 文本不混入剧情。
|
|
80
80
|
*/
|
|
81
|
-
export function* parseRenpyScript(text, { relPath }) {
|
|
81
|
+
export function* parseRenpyScript(text, { relPath, pathPrefix = '' }) {
|
|
82
82
|
const lines = String(text).split(/\r?\n/);
|
|
83
83
|
const characters = new Map();
|
|
84
84
|
let sceneKey = null;
|
|
@@ -88,10 +88,11 @@ export function* parseRenpyScript(text, { relPath }) {
|
|
|
88
88
|
let lastText = '';
|
|
89
89
|
let pendingVoice = null;
|
|
90
90
|
let skipDeeperThan = null;
|
|
91
|
+
const skipLines = new Set();
|
|
91
92
|
|
|
92
93
|
const ensureScene = function* () {
|
|
93
94
|
if (sceneKey) return;
|
|
94
|
-
sceneKey = `${relPath}
|
|
95
|
+
sceneKey = `${relPath}#${pathPrefix}top`;
|
|
95
96
|
sceneTitle = '开场';
|
|
96
97
|
yield {
|
|
97
98
|
type: 'scene',
|
|
@@ -100,28 +101,30 @@ export function* parseRenpyScript(text, { relPath }) {
|
|
|
100
101
|
container: relPath,
|
|
101
102
|
order: sceneOrder++,
|
|
102
103
|
conditions: null,
|
|
103
|
-
src: { relPath, path:
|
|
104
|
+
src: { relPath, path: `${pathPrefix}top` },
|
|
104
105
|
};
|
|
105
106
|
};
|
|
106
107
|
|
|
107
108
|
for (let index = 0; index < lines.length; index += 1) {
|
|
108
109
|
const raw = lines[index];
|
|
109
110
|
const trimmed = raw.trim();
|
|
110
|
-
if (!trimmed ||
|
|
111
|
+
if (!trimmed || skipLines.has(index)) continue;
|
|
112
|
+
if (trimmed.startsWith('#')) continue;
|
|
111
113
|
const indent = indentOf(raw);
|
|
112
114
|
|
|
113
115
|
if (skipDeeperThan !== null) {
|
|
114
116
|
if (indent > skipDeeperThan) continue;
|
|
115
117
|
skipDeeperThan = null;
|
|
116
118
|
}
|
|
117
|
-
const src = { relPath, path:
|
|
119
|
+
const src = { relPath, path: `${pathPrefix}line:${index + 1}`, index };
|
|
118
120
|
|
|
119
121
|
// label → 新场景
|
|
120
122
|
const labelMatch = /^label\s+([A-Za-z_][\w.]*)\s*(?:\([^)]*\))?\s*:/.exec(trimmed);
|
|
121
123
|
if (labelMatch && indent === 0) {
|
|
122
|
-
sceneKey = `${relPath}
|
|
124
|
+
sceneKey = `${relPath}#${pathPrefix}label:${labelMatch[1]}`;
|
|
123
125
|
sceneTitle = labelMatch[1];
|
|
124
126
|
lineOrder = 0;
|
|
127
|
+
lastText = '';
|
|
125
128
|
yield {
|
|
126
129
|
type: 'scene',
|
|
127
130
|
key: sceneKey,
|
|
@@ -144,7 +147,7 @@ export function* parseRenpyScript(text, { relPath }) {
|
|
|
144
147
|
|
|
145
148
|
// 菜单
|
|
146
149
|
if (/^menu\s*:/.test(trimmed)) {
|
|
147
|
-
const {
|
|
150
|
+
const { options, optionLines } = parseMenuBlock(lines, index);
|
|
148
151
|
yield* ensureScene();
|
|
149
152
|
if (options.length > 0) {
|
|
150
153
|
yield {
|
|
@@ -157,7 +160,8 @@ export function* parseRenpyScript(text, { relPath }) {
|
|
|
157
160
|
};
|
|
158
161
|
lastText = '';
|
|
159
162
|
}
|
|
160
|
-
|
|
163
|
+
// 不跳过整个块:让选项分支体里的对白/跳转继续被主循环解析;只跳过选项标签行。
|
|
164
|
+
for (const lineIndex of optionLines) skipLines.add(lineIndex);
|
|
161
165
|
continue;
|
|
162
166
|
}
|
|
163
167
|
|
|
@@ -373,8 +377,7 @@ export const renpy = {
|
|
|
373
377
|
continue;
|
|
374
378
|
}
|
|
375
379
|
const text = content.toString('utf8');
|
|
376
|
-
|
|
377
|
-
yield* parseRenpyScript(text, { relPath: virtual });
|
|
380
|
+
yield* parseRenpyScript(text, { relPath: rel, pathPrefix: `${entry.name}:` });
|
|
378
381
|
}
|
|
379
382
|
} catch (error) {
|
|
380
383
|
yield { type: 'error', relPath: rel, message: error.message };
|
package/lib/core/ids.mjs
CHANGED
|
@@ -9,8 +9,8 @@ export function makeId(prefix, ...parts) {
|
|
|
9
9
|
return `${prefix}_${digestOf(...parts).slice(0, 10)}`;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
export function gameIdOf(engineId, fingerprintDigest) {
|
|
13
|
-
return makeId('g', engineId, fingerprintDigest);
|
|
12
|
+
export function gameIdOf(engineId, fingerprintDigest, salt = '') {
|
|
13
|
+
return makeId('g', engineId, fingerprintDigest, salt);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export function sceneIdOf(gameId, relPath, address) {
|
package/lib/core/paths.mjs
CHANGED
|
@@ -75,6 +75,19 @@ export function assertSafeId(id, name = 'id') {
|
|
|
75
75
|
return id;
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
/** 游戏目录不得与插件数据目录互相包含(否则只读承诺会被破坏)。 */
|
|
79
|
+
export function assertSeparateRoots(workspace, gameRoot) {
|
|
80
|
+
const plugin = path.resolve(workspace, PLUGIN_DIR);
|
|
81
|
+
const game = path.resolve(gameRoot);
|
|
82
|
+
if (game === plugin || game.startsWith(plugin + path.sep)) {
|
|
83
|
+
throw new GameError(ErrorCode.INVALID_INPUT, '游戏目录不能位于 .dsh-novel-game/ 内');
|
|
84
|
+
}
|
|
85
|
+
if (plugin === game || plugin.startsWith(game + path.sep)) {
|
|
86
|
+
throw new GameError(ErrorCode.INVALID_INPUT, '工作区位于游戏目录内:插件写入会落进游戏目录,请换一个工作区');
|
|
87
|
+
}
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
|
|
78
91
|
/** 只允许写插件目录内的相对路径。 */
|
|
79
92
|
export function pluginPath(workspace, ...segments) {
|
|
80
93
|
const root = pluginRoot(workspace);
|
package/lib/ingest.mjs
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
|
+
import { open as openFile, rm, stat } from 'node:fs/promises';
|
|
2
3
|
import { GameError, ErrorCode } from './core/errors.mjs';
|
|
4
|
+
import { assertSeparateRoots } from './core/paths.mjs';
|
|
5
|
+
import { ensureDir } from './core/jsonl.mjs';
|
|
3
6
|
import { createCtx } from './ctx.mjs';
|
|
4
7
|
import { detectEngines, pickEngine } from './detect.mjs';
|
|
5
8
|
import { getAdapter } from './adapters/registry.mjs';
|
|
@@ -52,7 +55,47 @@ export async function planImport({ gameRoot }) {
|
|
|
52
55
|
* - mode=full 全量;incremental 只重解哈希变化的文件,其余代分片复用
|
|
53
56
|
* - 单文件失败隔离,不中断整体
|
|
54
57
|
*/
|
|
55
|
-
export async function importGame({
|
|
58
|
+
export async function importGame(options) {
|
|
59
|
+
const { workspace, gameRoot } = options;
|
|
60
|
+
if (!options.consent) throw new GameError(ErrorCode.CONSENT_REQUIRED, '需要作者确认本地合法持有该游戏副本(consent=true)');
|
|
61
|
+
const gameRootAbs = path.resolve(gameRoot);
|
|
62
|
+
assertSeparateRoots(workspace, gameRootAbs);
|
|
63
|
+
|
|
64
|
+
const store = new GameStore(workspace);
|
|
65
|
+
await store.init();
|
|
66
|
+
const lockDir = path.join(store.root, 'locks');
|
|
67
|
+
await ensureDir(lockDir);
|
|
68
|
+
const lockFile = path.join(lockDir, `${digestOf('lock', gameRootAbs).slice(0, 16)}.lock`);
|
|
69
|
+
await acquireLock(lockFile);
|
|
70
|
+
try {
|
|
71
|
+
return await importGameUnlocked({ ...options, gameRoot: gameRootAbs });
|
|
72
|
+
} finally {
|
|
73
|
+
await rm(lockFile, { force: true }).catch(() => {});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function acquireLock(file) {
|
|
78
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
79
|
+
try {
|
|
80
|
+
const handle = await openFile(file, 'wx');
|
|
81
|
+
await handle.writeFile(JSON.stringify({ pid: process.pid, at: Date.now() }));
|
|
82
|
+
await handle.close();
|
|
83
|
+
return;
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (error?.code !== 'EEXIST') throw error;
|
|
86
|
+
const info = await stat(file).catch(() => null);
|
|
87
|
+
const stale = !info || Date.now() - info.mtimeMs > 10 * 60 * 1000;
|
|
88
|
+
if (stale && attempt === 0) {
|
|
89
|
+
await rm(file, { force: true }).catch(() => {});
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
throw new GameError(ErrorCode.BUSY, '同一游戏已有导入任务在跑(稍后再试或取消旧任务)');
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
throw new GameError(ErrorCode.BUSY, '无法获取导入锁');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function importGameUnlocked({
|
|
56
99
|
workspace,
|
|
57
100
|
gameRoot,
|
|
58
101
|
consent,
|
|
@@ -80,7 +123,14 @@ export async function importGame({
|
|
|
80
123
|
const byRoot = (library.games ?? []).find(
|
|
81
124
|
(game) => game.root && path.resolve(game.root) === path.resolve(gameRoot),
|
|
82
125
|
);
|
|
83
|
-
|
|
126
|
+
let gameId = byRoot?.gameId ?? gameIdOf(adapter.id, fingerprintDigest);
|
|
127
|
+
if (!byRoot) {
|
|
128
|
+
// 同标题/同指纹但不同目录:不能共用 gameId,否则两个游戏的数据会互相覆盖。
|
|
129
|
+
const conflict = (library.games ?? []).find(
|
|
130
|
+
(game) => game.gameId === gameId && path.resolve(game.root ?? '') !== path.resolve(gameRoot),
|
|
131
|
+
);
|
|
132
|
+
if (conflict) gameId = gameIdOf(adapter.id, fingerprintDigest, path.resolve(gameRoot));
|
|
133
|
+
}
|
|
84
134
|
const previous = await store.readManifest(gameId);
|
|
85
135
|
const previousSources = new Map((previous?.sources ?? []).map((row) => [row.rel, row]));
|
|
86
136
|
|
|
@@ -100,8 +150,12 @@ export async function importGame({
|
|
|
100
150
|
if (previous?.activeGeneration) {
|
|
101
151
|
for (const container of textContainers) {
|
|
102
152
|
if (shouldRead(container.rel)) continue;
|
|
153
|
+
const previousRow = previousSources.get(container.rel);
|
|
154
|
+
// 上一代没有分片(只产词条、没有台词的文件)时不需要复制,也不算失败。
|
|
155
|
+
if (previousRow && (previousRow.units ?? 0) === 0) continue;
|
|
103
156
|
const ok = await store.copyShard(gameId, previous.activeGeneration, genId, container.rel);
|
|
104
157
|
if (ok) reused.push(container.rel);
|
|
158
|
+
else changed.add(container.rel); // 旧分片缺失/损坏:回退重抽,不能标 reused
|
|
105
159
|
}
|
|
106
160
|
}
|
|
107
161
|
|
|
@@ -244,11 +298,15 @@ export async function importGame({
|
|
|
244
298
|
}
|
|
245
299
|
await flush();
|
|
246
300
|
|
|
247
|
-
//
|
|
301
|
+
// 合并未变化文件的旧场景/选项/词条;已删除的来源(不在当前容器里)一律不保留。
|
|
302
|
+
const currentRels = new Set(containers.map((container) => container.rel));
|
|
248
303
|
const prevScenes = previous?.activeGeneration ? await store.readScenes(gameId, previous.activeGeneration) : [];
|
|
249
304
|
const prevChoices = previous?.activeGeneration ? await store.readChoices(gameId, previous.activeGeneration) : [];
|
|
250
305
|
const prevGlossary = previous?.activeGeneration ? await store.readGlossary(gameId, previous.activeGeneration) : [];
|
|
251
|
-
const keepIfUnchanged = (row) =>
|
|
306
|
+
const keepIfUnchanged = (row) => {
|
|
307
|
+
const rel = row.src?.relPath ?? '';
|
|
308
|
+
return !shouldRead(rel) && currentRels.has(rel);
|
|
309
|
+
};
|
|
252
310
|
|
|
253
311
|
const sceneMap = new Map(prevScenes.filter(keepIfUnchanged).map((row) => [row.sceneId, row]));
|
|
254
312
|
for (const row of scenes) sceneMap.set(row.sceneId, row);
|
|
@@ -275,13 +333,24 @@ export async function importGame({
|
|
|
275
333
|
counts.choices = allChoices.length;
|
|
276
334
|
counts.glossary = allGlossary.length;
|
|
277
335
|
|
|
336
|
+
// 计数从新代分片重算:复用文件的分片不经过本次抽取,不能只统计新单元。
|
|
337
|
+
const recount = { lines: 0, assets: 0, skipped: 0, conditions: 0 };
|
|
338
|
+
for await (const row of store.iterateShards(gameId, genId)) {
|
|
339
|
+
if (row.t === 'line') recount.lines += 1;
|
|
340
|
+
else if (row.t === 'asset') recount.assets += 1;
|
|
341
|
+
else if (row.t === 'skip') recount.skipped += 1;
|
|
342
|
+
else if (row.t === 'condition') recount.conditions += 1;
|
|
343
|
+
}
|
|
344
|
+
Object.assign(counts, recount);
|
|
345
|
+
|
|
278
346
|
onProgress({ phase: 'finalize', done: 0, total: 1 });
|
|
279
347
|
await store.writeAggregates(gameId, genId, { scenes: allScenes, choices: allChoices, glossary: allGlossary });
|
|
280
348
|
|
|
281
349
|
const errorByRel = new Map(coverageErrors.map((row) => [row.rel, row.message]));
|
|
282
350
|
const sources = containers.map((container) => {
|
|
283
351
|
const rel = container.rel;
|
|
284
|
-
const
|
|
352
|
+
const previousRow = previousSources.get(rel);
|
|
353
|
+
const stats = perFile.get(rel) ?? (previousRow ? { lines: previousRow.units ?? 0, scenes: previousRow.scenes ?? 0 } : {});
|
|
285
354
|
const read = shouldRead(rel);
|
|
286
355
|
return {
|
|
287
356
|
rel,
|
package/lib/jobs.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import path from 'node:path';
|
|
|
2
2
|
import { readFile } from 'node:fs/promises';
|
|
3
3
|
import { writeJsonAtomic } from './core/jsonl.mjs';
|
|
4
4
|
import { GameError, ErrorCode } from './core/errors.mjs';
|
|
5
|
+
import { assertSafeId, pluginPath } from './core/paths.mjs';
|
|
5
6
|
import { jobIdOf } from './core/ids.mjs';
|
|
6
7
|
|
|
7
8
|
function nowIso() {
|
|
@@ -17,7 +18,8 @@ export class JobRunner {
|
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
pathOf(jobId) {
|
|
20
|
-
|
|
21
|
+
assertSafeId(jobId, 'jobId');
|
|
22
|
+
return pluginPath(this.store.workspace, 'jobs', `${jobId}.json`);
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
async create({ kind, gameRoot, meta = {} }) {
|
|
@@ -52,11 +54,12 @@ export class JobRunner {
|
|
|
52
54
|
}
|
|
53
55
|
|
|
54
56
|
async get(jobId) {
|
|
57
|
+
const file = this.pathOf(jobId); // jobId 非法在这里就抛 INVALID_INPUT
|
|
55
58
|
// Windows 上 rename 与读取可能撞车:短暂重试,避免把瞬时错误当成任务不存在。
|
|
56
59
|
let lastError = null;
|
|
57
60
|
for (let attempt = 0; attempt < 5; attempt += 1) {
|
|
58
61
|
try {
|
|
59
|
-
const job = JSON.parse(await readFile(
|
|
62
|
+
const job = JSON.parse(await readFile(file, 'utf8'));
|
|
60
63
|
return job;
|
|
61
64
|
} catch (error) {
|
|
62
65
|
lastError = error;
|
package/lib/store.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
|
-
import { copyFile, readdir, rm } from 'node:fs/promises';
|
|
2
|
+
import { copyFile, readdir, readFile, rm } from 'node:fs/promises';
|
|
3
3
|
import { pluginRoot } from './core/paths.mjs';
|
|
4
|
+
import { GameError, ErrorCode } from './core/errors.mjs';
|
|
4
5
|
import {
|
|
5
6
|
appendJsonl,
|
|
6
7
|
ensureDir,
|
|
@@ -90,8 +91,17 @@ export class GameStore {
|
|
|
90
91
|
return Array.isArray(library.games) ? library.games : [];
|
|
91
92
|
}
|
|
92
93
|
|
|
94
|
+
/**
|
|
95
|
+
* 读 manifest:文件不存在返回 null;存在但损坏必须抛错。
|
|
96
|
+
* 否则损坏会被当成「首次导入」,随后的 prune 可能删掉仍可读的旧代。
|
|
97
|
+
*/
|
|
93
98
|
async readManifest(gameId) {
|
|
94
|
-
|
|
99
|
+
try {
|
|
100
|
+
return JSON.parse(await readFile(this.manifestPath(gameId), 'utf8'));
|
|
101
|
+
} catch (error) {
|
|
102
|
+
if (error?.code === 'ENOENT') return null;
|
|
103
|
+
throw new GameError(ErrorCode.IO_ERROR, `manifest 损坏或不可读:${gameId}(${error.message})`, { gameId });
|
|
104
|
+
}
|
|
95
105
|
}
|
|
96
106
|
|
|
97
107
|
async readSettings() {
|
|
@@ -243,11 +253,13 @@ export class GameStore {
|
|
|
243
253
|
return manifest;
|
|
244
254
|
}
|
|
245
255
|
|
|
246
|
-
/**
|
|
256
|
+
/** 只保留当前代与上一代(keep = 总代数)。 */
|
|
247
257
|
async pruneGenerations(gameId, keep = 2) {
|
|
248
258
|
const manifest = await this.readManifest(gameId);
|
|
249
259
|
if (!manifest?.generations?.length) return;
|
|
250
|
-
const
|
|
260
|
+
const history = manifest.generations.filter((gen) => gen.id !== manifest.activeGeneration);
|
|
261
|
+
const kept = history.slice(-Math.max(0, keep - 1)).map((gen) => gen.id);
|
|
262
|
+
const keepIds = new Set([manifest.activeGeneration, ...kept]);
|
|
251
263
|
const dir = path.join(this.gameDir(gameId), 'generations');
|
|
252
264
|
let entries = [];
|
|
253
265
|
try {
|