dsh-game-material-master 0.2.1 → 0.4.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/README.en.md +383 -0
- package/README.md +83 -6
- package/lib/client.js +4436 -877
- package/lib/config.js +45 -5
- package/lib/directions.js +120 -5
- package/lib/index.js +375 -12
- package/lib/jsonio.js +49 -3
- package/lib/links.js +10 -2
- package/lib/tilegen.js +1702 -0
- package/lib/tilegeom.js +1392 -0
- package/lib/tilemap.js +560 -0
- package/lib/tilemedia.js +243 -0
- package/lib/tools.js +397 -19
- package/lib/wire.js +100 -4
- package/package.json +3 -2
package/lib/index.js
CHANGED
|
@@ -14,8 +14,9 @@ import { basename, dirname, extname, join } from "node:path";
|
|
|
14
14
|
import { fileURLToPath } from "node:url";
|
|
15
15
|
import { spawn } from "node:child_process";
|
|
16
16
|
import { TypertRemoteService } from "@deepseek-ai/dsh-typert-protocol";
|
|
17
|
+
import { appendJobLog } from "./jsonio.js";
|
|
17
18
|
import { ARK_MODEL_PRESETS, DEFAULT_CONFIG, ROW_ORDER_VERSION, MINIMAX_HOST_PRESETS, MINIMAX_MODEL_PRESETS, loadConfig, maskConfig, migrateLegacyDataRoot, projectsRoot, saveConfig } from "./config.js";
|
|
18
|
-
import { DEFAULT_ROW_ORDER,
|
|
19
|
+
import { DEFAULT_ROW_ORDER, DIRECTION_KEYS, TURN_DIRECTION_DEFAULT, TURN_FRAME_COUNT_MAX, TURN_FRAME_COUNT_MIN, defaultPrompts, directionOf, localizePrompts, normalizePromptLang } from "./directions.js";
|
|
19
20
|
import { checkFfmpeg } from "./media.js";
|
|
20
21
|
import { testArk } from "./ark.js";
|
|
21
22
|
import { COMP_SHARE_BASE_URL, COMP_SHARE_MODEL_ID, capabilityOf, isCompshareModel, normalizeDuration, normalizeResolution, pathPrefixOf, testMiniMax } from "./minimax.js";
|
|
@@ -24,18 +25,71 @@ import { assetPath, createProject, deleteProject, listProjects, log, patchProjec
|
|
|
24
25
|
import * as imagegen from "./imagegen.js";
|
|
25
26
|
import * as seqgen from "./seqgen.js";
|
|
26
27
|
import * as riggen from "./riggen.js";
|
|
28
|
+
import * as tilegen from "./tilegen.js";
|
|
27
29
|
import { MANIFEST, METHODS, SERVICE_NAME } from "./wire.js";
|
|
28
30
|
import { rememberClientOrigin } from "./links.js";
|
|
29
31
|
import { registerStudioTools } from "./tools.js";
|
|
30
32
|
/**
|
|
31
33
|
* 游戏素材大师 —— 宿主半区。
|
|
32
34
|
*
|
|
33
|
-
* 一个 Typert 远程服务("gameStudio"
|
|
34
|
-
* ① 八方向图生成 ② 图片生成 ③ 序列帧生成
|
|
35
|
-
*
|
|
35
|
+
* 一个 Typert 远程服务("gameStudio")承载五个功能模块的控制面:
|
|
36
|
+
* ① 八方向图生成 ② 图片生成 ③ 序列帧生成 ④ 骨骼动画生成 ⑤ 地图地块生成
|
|
37
|
+
* 五者共用同一套配置(API Key / 模型 / 抠像默认值),但各自独立存项目。
|
|
36
38
|
*
|
|
37
|
-
* 另外用 `ctx.webServer` 注册一条 prefix
|
|
39
|
+
* 另外用 `ctx.webServer` 注册一条 prefix 路由,把各模块的产物直接发给浏览器。
|
|
38
40
|
*/
|
|
41
|
+
/**
|
|
42
|
+
* 合并界面提交的地块清单:保留已生成的变体,只更新可编辑字段。
|
|
43
|
+
*
|
|
44
|
+
* 为什么要合并而不是直接覆盖:清单里带着 `variants`(那是花钱生成的产物),
|
|
45
|
+
* 界面只提交 `key/label/content/variantCount/...`。直接覆盖会让用户
|
|
46
|
+
* **改一个标签就丢掉整批已生成的图**,而且不会报错。
|
|
47
|
+
*
|
|
48
|
+
* 返回 `changed`:**提示词真的变了**的那些地块 key。调用方只能作废这些 ——
|
|
49
|
+
* 只改标签 / 只改变体数不该把别的(或自己的)产物废掉。
|
|
50
|
+
* 判断「提示词是否变化」看的是 `content` 与 `mode`,因为只有它们会进提示词。
|
|
51
|
+
*/
|
|
52
|
+
function mergeTileItems(existing, incoming) {
|
|
53
|
+
const byKey = new Map(existing.map((item) => [item.key, item]));
|
|
54
|
+
const out = [];
|
|
55
|
+
const changed = [];
|
|
56
|
+
for (const raw of incoming) {
|
|
57
|
+
const key = typeof raw?.key === "string" ? raw.key : "";
|
|
58
|
+
if (key === "")
|
|
59
|
+
continue;
|
|
60
|
+
const previous = byKey.get(key);
|
|
61
|
+
const variantCount = clampInt(raw.variantCount, previous?.variantCount ?? 1, 1, 6);
|
|
62
|
+
// ★ 形状:以「相对锚点的格子集合」为准,任意形状(L 形 / T 形 / 线形)都收。
|
|
63
|
+
// 老数据只有 `footprint`(列×行),由 `normalizeShape` 无损转成矩形形状。
|
|
64
|
+
const shape = tilegen.normalizeShape(raw, previous);
|
|
65
|
+
const footprint = tilegen.boundingRectOf(shape);
|
|
66
|
+
const next = {
|
|
67
|
+
key,
|
|
68
|
+
label: typeof raw.label === "string" && raw.label !== "" ? raw.label : (previous?.label ?? key),
|
|
69
|
+
kind: raw.kind === "decor" || raw.kind === "building" ? raw.kind : "terrain",
|
|
70
|
+
family: typeof raw.family === "string" && raw.family !== "" ? raw.family : (previous?.family ?? key),
|
|
71
|
+
// `footprint` 保留为**包围矩形**:老版本读到的至少还是个合理的矩形
|
|
72
|
+
footprint,
|
|
73
|
+
shape,
|
|
74
|
+
content: typeof raw.content === "string" ? raw.content : (previous?.content ?? ""),
|
|
75
|
+
mode: raw.mode === "plain" || raw.mode === "grid2x2" ? raw.mode : (previous?.mode ?? "template"),
|
|
76
|
+
variantCount,
|
|
77
|
+
variants: previous === undefined ? [] : previous.variants.slice(0, variantCount),
|
|
78
|
+
approved: previous?.approved
|
|
79
|
+
};
|
|
80
|
+
// 内容、生成方式或**形状**变了 → 这个地块已生成的变体失效
|
|
81
|
+
// (它们画的是旧内容 / 旧轮廓)。**只作废这一个**,别连累别的。
|
|
82
|
+
const shapeChanged = previous !== undefined
|
|
83
|
+
&& tilegen.shapeKey(shape) !== tilegen.shapeKey(tilegen.normalizeShape(previous, undefined));
|
|
84
|
+
if (previous !== undefined && (previous.content !== next.content || previous.mode !== next.mode || shapeChanged)) {
|
|
85
|
+
next.variants = [];
|
|
86
|
+
next.approved = undefined;
|
|
87
|
+
changed.push(key);
|
|
88
|
+
}
|
|
89
|
+
out.push(next);
|
|
90
|
+
}
|
|
91
|
+
return { items: out.length > 0 ? out : existing, changed };
|
|
92
|
+
}
|
|
39
93
|
export const name = "dsh-game-material-master";
|
|
40
94
|
/**
|
|
41
95
|
* `webServer` 必须声明成硬依赖:它要等真正 listen 成功之后才可用,
|
|
@@ -245,7 +299,10 @@ export class GameStudioGateway extends TypertRemoteService {
|
|
|
245
299
|
"bgTolerance",
|
|
246
300
|
"edgeShrink",
|
|
247
301
|
"rowOrder",
|
|
248
|
-
"concurrency"
|
|
302
|
+
"concurrency",
|
|
303
|
+
// 功能可见性:设置页的「功能管理」写它,`normalizeConfig` 会收敛成合法子集。
|
|
304
|
+
// 对话工具面**不允许**改它(见 tools.ts),否则模型可以自己把隐藏的功能放出来。
|
|
305
|
+
"hiddenModules"
|
|
249
306
|
];
|
|
250
307
|
for (const key of directKeys) {
|
|
251
308
|
if (input[key] !== undefined)
|
|
@@ -303,10 +360,20 @@ export class GameStudioGateway extends TypertRemoteService {
|
|
|
303
360
|
return { projectId: project.id };
|
|
304
361
|
}
|
|
305
362
|
async getProject(payload) {
|
|
306
|
-
const
|
|
307
|
-
const
|
|
363
|
+
const input = asRecord(payload);
|
|
364
|
+
const projectId = asString(input.projectId);
|
|
365
|
+
// 界面语言由浏览器半区带过来(宿主没有 locale 服务)。带上它,内置默认提示词
|
|
366
|
+
// 才会跟着 DSH 的语言走——只有「从没被改过」的那些会被换成目标语言的默认值。
|
|
367
|
+
const lang = input.lang === undefined ? undefined : normalizePromptLang(input.lang);
|
|
368
|
+
let project = await readProject(projectId);
|
|
308
369
|
if (project === undefined)
|
|
309
370
|
throw new Error(`项目不存在:${projectId}`);
|
|
371
|
+
if (lang !== undefined && localizePrompts(project.prompts, lang)) {
|
|
372
|
+
project = await patchProject(projectId, (draft) => {
|
|
373
|
+
localizePrompts(draft.prompts, lang);
|
|
374
|
+
return draft;
|
|
375
|
+
});
|
|
376
|
+
}
|
|
310
377
|
return {
|
|
311
378
|
...project,
|
|
312
379
|
jobs: listJobs(projectId),
|
|
@@ -366,16 +433,18 @@ export class GameStudioGateway extends TypertRemoteService {
|
|
|
366
433
|
const resetImages = input.resetImagesToDefault === true;
|
|
367
434
|
const resetVideo = input.resetVideoToDefault === true;
|
|
368
435
|
const resetTurn = input.resetTurnToDefault === true;
|
|
436
|
+
// 「重置为默认」要重置成**当前界面语言**的默认值,否则英文界面点一下会掉出中文提示词。
|
|
437
|
+
const defaults = defaultPrompts(normalizePromptLang(input.lang));
|
|
369
438
|
await patchProject(projectId, (project) => {
|
|
370
439
|
// 提示词模板升级后(例如这次把方位语义改对),老项目要能一键重新套用默认值。
|
|
371
440
|
if (resetImages)
|
|
372
|
-
project.prompts.images =
|
|
441
|
+
project.prompts.images = defaults.images;
|
|
373
442
|
if (resetVideo) {
|
|
374
|
-
project.prompts.video =
|
|
443
|
+
project.prompts.video = defaults.video;
|
|
375
444
|
project.prompts.videoPerDirection = {};
|
|
376
445
|
}
|
|
377
446
|
if (resetTurn)
|
|
378
|
-
project.prompts.turn =
|
|
447
|
+
project.prompts.turn = defaults.turn;
|
|
379
448
|
if (turn !== undefined && turn.trim() !== "")
|
|
380
449
|
project.prompts.turn = turn;
|
|
381
450
|
if (images !== undefined) {
|
|
@@ -565,8 +634,290 @@ export class GameStudioGateway extends TypertRemoteService {
|
|
|
565
634
|
await riggen.writeRigJob(job);
|
|
566
635
|
return { ok: true, id, module, reviewMode };
|
|
567
636
|
}
|
|
637
|
+
// 地图地块(模块五)。**别重犯 rig 早期那个漏**:
|
|
638
|
+
// 少了这个分支,「固定流程第 0 步必须问清审核模式」对 tile 就落不了地。
|
|
639
|
+
if (module === "tile") {
|
|
640
|
+
// 走 `patchTileProject`(带写队列),别用裸的 `writeTileProject`:
|
|
641
|
+
// 裸写会绕过队列,与后台作业的 report() 撞同一个 project.json。
|
|
642
|
+
await tilegen.patchTileProject(id, (project) => {
|
|
643
|
+
project.reviewMode = reviewMode;
|
|
644
|
+
});
|
|
645
|
+
return { ok: true, id, module, reviewMode };
|
|
646
|
+
}
|
|
568
647
|
throw new Error(`未知模块:${module}`);
|
|
569
648
|
}
|
|
649
|
+
// ── 地图地块生成(模块五)──────────────────────────────────────────────
|
|
650
|
+
async listTileProjects() {
|
|
651
|
+
return { projects: await tilegen.listTileProjects() };
|
|
652
|
+
}
|
|
653
|
+
async createTileProject(payload) {
|
|
654
|
+
const input = asRecord(payload);
|
|
655
|
+
const project = await tilegen.createTileProject(asString(input.name, "未命名地图"), {
|
|
656
|
+
style: typeof input.style === "string" ? input.style : undefined,
|
|
657
|
+
lang: typeof input.lang === "string" ? input.lang : undefined,
|
|
658
|
+
settings: input.settings
|
|
659
|
+
});
|
|
660
|
+
return tilegen.tileView(project);
|
|
661
|
+
}
|
|
662
|
+
/**
|
|
663
|
+
* 读项目。
|
|
664
|
+
* `lang` 决定「从没改过的地块清单 / 画风描述」用哪一国话的默认值 ——
|
|
665
|
+
* 它必须出现在 wire 的 schema 里,否则会被静默丢掉(见 wire.ts 的注释)。
|
|
666
|
+
*/
|
|
667
|
+
async getTileProject(payload) {
|
|
668
|
+
const input = asRecord(payload);
|
|
669
|
+
const project = await tilegen.readTileProject(asString(input.projectId), asString(input.lang));
|
|
670
|
+
if (project === undefined)
|
|
671
|
+
throw new Error(`项目不存在:${asString(input.projectId)}`);
|
|
672
|
+
return tilegen.tileView(project);
|
|
673
|
+
}
|
|
674
|
+
async deleteTileProject(payload) {
|
|
675
|
+
const id = asString(asRecord(payload).projectId);
|
|
676
|
+
await tilegen.deleteTileProject(id);
|
|
677
|
+
return { ok: true };
|
|
678
|
+
}
|
|
679
|
+
async saveTileProject(payload) {
|
|
680
|
+
const input = asRecord(payload);
|
|
681
|
+
const id = asString(input.projectId);
|
|
682
|
+
const lang = typeof input.lang === "string" ? input.lang : undefined;
|
|
683
|
+
// ⚠️ 整个「读 → 改 → 写」都放进 `patchTileProject`(它带写队列)。
|
|
684
|
+
// 以前这里是自己 read、改完再调裸的 `writeTileProject` —— 那条路径**绕过队列**,
|
|
685
|
+
// 于是它和后台作业的 `report()` 会同时 rename 同一个 project.json,
|
|
686
|
+
// 在 Windows 上报 EPERM、并且互相覆盖(丢更新)。
|
|
687
|
+
// 实测:用户点「保存布局」时后台恰好在收尾,就报「保存失败」。
|
|
688
|
+
const next = await tilegen.patchTileProject(id, (project) => {
|
|
689
|
+
if (typeof input.name === "string" && input.name.trim() !== "")
|
|
690
|
+
project.name = input.name.trim();
|
|
691
|
+
if (typeof input.style === "string" && input.style !== project.style) {
|
|
692
|
+
project.style = input.style;
|
|
693
|
+
// 改画风 = 改**每一个**地块的提示词 → 全部产物作废(重新生成要花钱)
|
|
694
|
+
tilegen.invalidateItems(project);
|
|
695
|
+
}
|
|
696
|
+
if (input.resetItemsToDefault === true) {
|
|
697
|
+
project.items = tilegen.defaultTileItems(lang);
|
|
698
|
+
tilegen.invalidateItems(project);
|
|
699
|
+
}
|
|
700
|
+
else if (Array.isArray(input.items)) {
|
|
701
|
+
const merged = mergeTileItems(project.items, input.items);
|
|
702
|
+
project.items = merged.items;
|
|
703
|
+
// ⚠️ 只作废**提示词真的变了**的那些地块。
|
|
704
|
+
// 这里一度无脑调 `invalidateFrom(project, "generate")`,而它是全量销毁 ——
|
|
705
|
+
// 结果「只改一个标签」也会把全部(花钱生成的)产物清空。
|
|
706
|
+
// 真机验证时抓出来的:验收之后任何一次保存都会让产物消失。
|
|
707
|
+
if (merged.changed.length > 0)
|
|
708
|
+
tilegen.invalidateItems(project, merged.changed);
|
|
709
|
+
}
|
|
710
|
+
if (input.settings !== undefined) {
|
|
711
|
+
const settings = { ...project.settings, ...input.settings };
|
|
712
|
+
if (settings.cellWidth !== project.settings.cellWidth || settings.cellHeight !== project.settings.cellHeight) {
|
|
713
|
+
project.settings = settings;
|
|
714
|
+
// 几何规格变了:模板与全部产物都作废
|
|
715
|
+
tilegen.invalidateFrom(project, "template");
|
|
716
|
+
}
|
|
717
|
+
else {
|
|
718
|
+
project.settings = settings;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
return project;
|
|
722
|
+
});
|
|
723
|
+
return tilegen.tileView(next);
|
|
724
|
+
}
|
|
725
|
+
async runTileItems(payload) {
|
|
726
|
+
const input = asRecord(payload);
|
|
727
|
+
const keys = Array.isArray(input.keys) ? input.keys.filter((k) => typeof k === "string") : undefined;
|
|
728
|
+
return tilegen.runGenerateStage(asString(input.projectId), { keys });
|
|
729
|
+
}
|
|
730
|
+
async runTileItem(payload) {
|
|
731
|
+
const input = asRecord(payload);
|
|
732
|
+
return tilegen.runGenerateStage(asString(input.projectId), {
|
|
733
|
+
key: asString(input.key),
|
|
734
|
+
variant: typeof input.variant === "number" ? input.variant : undefined
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
async setTileApproved(payload) {
|
|
738
|
+
const input = asRecord(payload);
|
|
739
|
+
const id = asString(input.projectId);
|
|
740
|
+
const approved = input.approved === true;
|
|
741
|
+
const key = typeof input.key === "string" ? input.key : undefined;
|
|
742
|
+
const variant = typeof input.variant === "number" ? input.variant : undefined;
|
|
743
|
+
await tilegen.patchTileProject(id, (project) => {
|
|
744
|
+
let touched = 0;
|
|
745
|
+
for (const item of project.items) {
|
|
746
|
+
if (key !== undefined && item.key !== key)
|
|
747
|
+
continue;
|
|
748
|
+
if (variant !== undefined) {
|
|
749
|
+
const entry = item.variants[variant];
|
|
750
|
+
if (entry === undefined)
|
|
751
|
+
continue;
|
|
752
|
+
entry.approved = approved;
|
|
753
|
+
touched++;
|
|
754
|
+
continue;
|
|
755
|
+
}
|
|
756
|
+
item.approved = approved;
|
|
757
|
+
for (const entry of item.variants)
|
|
758
|
+
entry.approved = approved;
|
|
759
|
+
touched++;
|
|
760
|
+
}
|
|
761
|
+
if (touched === 0)
|
|
762
|
+
throw new Error(key === undefined ? "没有可验收的地块" : `找不到地块:${key}`);
|
|
763
|
+
// 验收只影响打勾,不作废任何东西(这一条是有意的)
|
|
764
|
+
const allApproved = project.items.every((item) => item.variants.length > 0 && item.variants.every((v) => v.approved === true));
|
|
765
|
+
project.stages.review = { status: allApproved ? "done" : "idle", at: new Date().toISOString() };
|
|
766
|
+
// ⚠️ 日志要用 tile 项目自己的字段(`project.logs`),不能用模块一那个 `log()` ——
|
|
767
|
+
// 它写的是 `project.log`,对 tile 项目来说是 undefined,一调就抛
|
|
768
|
+
// `Cannot read properties of undefined (reading 'push')`。
|
|
769
|
+
// 实测:走网关验收一个地块就会直接崩(纯函数自检发现不了)。
|
|
770
|
+
appendJobLog(project.logs, "info", `${approved ? "通过" : "取消通过"}:${key ?? "全部"}${variant !== undefined ? ` 第 ${variant + 1} 张` : ""}`);
|
|
771
|
+
});
|
|
772
|
+
return { ok: true };
|
|
773
|
+
}
|
|
774
|
+
async runTileMap(payload) {
|
|
775
|
+
const input = asRecord(payload);
|
|
776
|
+
return tilegen.runMapStage(asString(input.projectId), {
|
|
777
|
+
rows: typeof input.rows === "number" ? input.rows : undefined,
|
|
778
|
+
cols: typeof input.cols === "number" ? input.cols : undefined,
|
|
779
|
+
seed: typeof input.seed === "number" ? input.seed : undefined,
|
|
780
|
+
fill: typeof input.fill === "string" ? input.fill : undefined,
|
|
781
|
+
decorDensity: typeof input.decorDensity === "number" ? input.decorDensity : undefined,
|
|
782
|
+
reroll: input.reroll === true
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
async saveTileMapCells(payload) {
|
|
786
|
+
const input = asRecord(payload);
|
|
787
|
+
const id = asString(input.projectId);
|
|
788
|
+
await tilegen.patchTileProject(id, (project) => {
|
|
789
|
+
if (Array.isArray(input.cells)) {
|
|
790
|
+
const cells = input.cells
|
|
791
|
+
.filter((row) => Array.isArray(row))
|
|
792
|
+
.map((row) => row.map((cell) => String(cell)));
|
|
793
|
+
if (cells.length > 0) {
|
|
794
|
+
project.map.cells = cells;
|
|
795
|
+
project.map.rows = cells.length;
|
|
796
|
+
project.map.cols = Math.max(...cells.map((row) => row.length));
|
|
797
|
+
// ⚠️ `buildings` / `buildingGround` 是**由布局推导出来的**记录,
|
|
798
|
+
// 布局一换就必须丢掉重推。留着的话会出现「幽灵建筑」:
|
|
799
|
+
// 用户把建筑挪到别处,旧位置那条记录还在,拼图会在**没有建筑的地方**
|
|
800
|
+
// 硬画一栋楼(实测地图上凭空多出一片屋顶)。
|
|
801
|
+
project.map.buildings = [];
|
|
802
|
+
project.map.buildingGround = [];
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
if (typeof input.seed === "number")
|
|
806
|
+
project.map.seed = Math.trunc(input.seed);
|
|
807
|
+
// 装饰与垫底地面:界面回传的既可能是**地块键**(`tree`),也可能是
|
|
808
|
+
// 早期版本留下的**贴图路径**(`decor/tree.v1.png`)。两种都接受,
|
|
809
|
+
// 统一落成宿主认识的 `key#index`。
|
|
810
|
+
const resolveVariant = (raw) => {
|
|
811
|
+
const value = String(raw ?? "");
|
|
812
|
+
if (value === "")
|
|
813
|
+
return "";
|
|
814
|
+
const item = project.items.find((it) => it.key === value);
|
|
815
|
+
if (item !== undefined) {
|
|
816
|
+
const first = item.variants.find((v) => v.cell !== undefined);
|
|
817
|
+
if (first !== undefined)
|
|
818
|
+
return `${value}#${first.index}`;
|
|
819
|
+
}
|
|
820
|
+
for (const it of project.items) {
|
|
821
|
+
const hit = it.variants.find((v) => v.cell === value);
|
|
822
|
+
if (hit !== undefined)
|
|
823
|
+
return `${it.key}#${hit.index}`;
|
|
824
|
+
}
|
|
825
|
+
return "";
|
|
826
|
+
};
|
|
827
|
+
if (input.decor !== undefined && typeof input.decor === "object") {
|
|
828
|
+
const next = {};
|
|
829
|
+
for (const [pos, raw] of Object.entries(input.decor)) {
|
|
830
|
+
const name = resolveVariant(raw);
|
|
831
|
+
if (name !== "")
|
|
832
|
+
next[pos] = name;
|
|
833
|
+
}
|
|
834
|
+
project.map.decor = next;
|
|
835
|
+
}
|
|
836
|
+
// ★ 建筑由界面**显式回传**(`layouts`):它带着占格与垫底地面。
|
|
837
|
+
//
|
|
838
|
+
// 不靠「cells 里有没有建筑键」反推:那样界面为了预览就得把建筑键铺满占格,
|
|
839
|
+
// 而一旦占格被清空(拼图会清)就再也推不出来了 —— 建筑会凭空消失。
|
|
840
|
+
if (Array.isArray(input.layouts)) {
|
|
841
|
+
project.map.buildings = [];
|
|
842
|
+
project.map.buildingGround = [];
|
|
843
|
+
for (const raw of input.layouts) {
|
|
844
|
+
const e = raw;
|
|
845
|
+
const r = Math.trunc(Number(e.r));
|
|
846
|
+
const c = Math.trunc(Number(e.c));
|
|
847
|
+
const key = String(e.key ?? "");
|
|
848
|
+
if (!Number.isFinite(r) || !Number.isFinite(c) || key === "")
|
|
849
|
+
continue;
|
|
850
|
+
// ★ 形状优先(任意形状,含 L 形);界面只给了宽高才退回矩形。
|
|
851
|
+
const shape = tilegen.parseShape(e.shape)
|
|
852
|
+
?? tilegen.rectShape(Math.max(1, Math.min(64, Math.trunc(Number(e.width ?? 1)))), Math.max(1, Math.min(64, Math.trunc(Number(e.height ?? 1)))));
|
|
853
|
+
const [fw, fh] = tilegen.boundingRectOf(shape);
|
|
854
|
+
// 越界按**每个格子**判,不是包围矩形 —— L 形的包围盒可能出界而格子还在里面。
|
|
855
|
+
if (r < 0 || c < 0)
|
|
856
|
+
continue;
|
|
857
|
+
if (shape.some(([dr, dc]) => r + dr >= project.map.rows || c + dc >= project.map.cols))
|
|
858
|
+
continue;
|
|
859
|
+
const name = (project.items.find((it) => it.key === key)?.variants ?? [])
|
|
860
|
+
.find((v) => v.cell !== undefined);
|
|
861
|
+
if (name === undefined)
|
|
862
|
+
continue;
|
|
863
|
+
const underName = resolveVariant(e.under);
|
|
864
|
+
project.map.buildings.push([r, c, `${key}#${name.index}`]);
|
|
865
|
+
// 第 5 位是形状:拼图/包围盒读它,`fw/fh` 只是包围矩形(兼容老版本)。
|
|
866
|
+
project.map.buildingGround.push([r, c, fw, fh, underName, shape]);
|
|
867
|
+
// ⚠️ **不要**把占格清成空串。
|
|
868
|
+
//
|
|
869
|
+
// 清了的话这一格什么都不铺,建筑底面菱形的四个角是透明的,
|
|
870
|
+
// 露出来的就是那块**纯色垫底**,和周围有纹理的草地格格不入
|
|
871
|
+
// (真机上看着像建筑拖着一块塑料板)。占格留着地面键,
|
|
872
|
+
// 拼图会照铺地面、再让建筑贴图盖上去。
|
|
873
|
+
//
|
|
874
|
+
// 填的是**地块键**:界面回传的 `cells` 里本来就是地块键;
|
|
875
|
+
// 万一那格是空串或还留着建筑键,就从垫底名反推地块键
|
|
876
|
+
// (`grass#0` → `grass`),反推不出来就退回界面的 `under` 地块键。
|
|
877
|
+
const underItem = String(e.under ?? "");
|
|
878
|
+
const groundKeyForFootprint = project.items.some((it) => it.key === underItem)
|
|
879
|
+
? underItem
|
|
880
|
+
: String(underName).split("#")[0];
|
|
881
|
+
for (const [dr, dc] of shape) {
|
|
882
|
+
const row = project.map.cells[r + dr];
|
|
883
|
+
const cell = row?.[c + dc];
|
|
884
|
+
if (row === undefined)
|
|
885
|
+
continue;
|
|
886
|
+
// 已经有真地面就留着;空串或残留的建筑键才替换
|
|
887
|
+
const isGround = project.items.some((it) => it.key === cell && it.kind === "terrain");
|
|
888
|
+
if (!isGround && groundKeyForFootprint !== "")
|
|
889
|
+
row[c + dc] = groundKeyForFootprint;
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
// 手动改布局不作废地块(那是花钱买的),只作废下游
|
|
894
|
+
tilegen.invalidateFrom(project, "map");
|
|
895
|
+
});
|
|
896
|
+
return { ok: true };
|
|
897
|
+
}
|
|
898
|
+
async runTileExport(payload) {
|
|
899
|
+
return tilegen.runExportStage(asString(asRecord(payload).projectId));
|
|
900
|
+
}
|
|
901
|
+
async cancelTileJob(payload) {
|
|
902
|
+
const id = asString(asRecord(payload).projectId);
|
|
903
|
+
tilegen.cancelTileJob(id);
|
|
904
|
+
return { ok: true };
|
|
905
|
+
}
|
|
906
|
+
async revealTileProject(payload) {
|
|
907
|
+
const id = asString(asRecord(payload).projectId);
|
|
908
|
+
const project = await tilegen.readTileProject(id);
|
|
909
|
+
if (project === undefined)
|
|
910
|
+
throw new Error(`项目不存在:${id}`);
|
|
911
|
+
const dir = tilegen.tileProjectDir(id);
|
|
912
|
+
const command = process.platform === "darwin" ? "open" : process.platform === "win32" ? "explorer" : "xdg-open";
|
|
913
|
+
try {
|
|
914
|
+
spawn(command, [dir], { detached: true, stdio: "ignore" }).unref();
|
|
915
|
+
}
|
|
916
|
+
catch (error) {
|
|
917
|
+
throw new Error(`无法打开目录:${error instanceof Error ? error.message : String(error)}`);
|
|
918
|
+
}
|
|
919
|
+
return { ok: true, dir };
|
|
920
|
+
}
|
|
570
921
|
// ── 流水线控制 ──────────────────────────────────────────────────────────
|
|
571
922
|
async runImage(payload) {
|
|
572
923
|
const input = asRecord(payload);
|
|
@@ -1539,6 +1890,8 @@ const SERVABLE_SEQUENCE_DIRS = new Set(["refs", "video-refs", "videos", "frames"
|
|
|
1539
1890
|
// `export/` 是后加的第二条导出路径(DragonBones 5.5 的 `_ske.json`)。
|
|
1540
1891
|
// 白名单只比对**第一段**路径,所以 `export/dragonbones/xxx.json` 加一条 `export` 就够。
|
|
1541
1892
|
const SERVABLE_RIG_DIRS = new Set(["source", "sheet", "parts", "layout", "rig", "atlas", "export"]);
|
|
1893
|
+
/** 地图地块:模板、原始生成、规整产物、装饰、地图、导出包。 */
|
|
1894
|
+
const SERVABLE_TILE_DIRS = new Set(["template", "raw", "cell", "decor", "map", "export"]);
|
|
1542
1895
|
function resolveAssetTarget(scope, id, relative) {
|
|
1543
1896
|
if (scope === "assets") {
|
|
1544
1897
|
if (!PROJECT_ID_PATTERN.test(id))
|
|
@@ -1555,6 +1908,11 @@ function resolveAssetTarget(scope, id, relative) {
|
|
|
1555
1908
|
return undefined;
|
|
1556
1909
|
return { file: riggen.rigAssetPath(id, relative), dirs: SERVABLE_RIG_DIRS };
|
|
1557
1910
|
}
|
|
1911
|
+
if (scope === "tile-assets") {
|
|
1912
|
+
if (!tilegen.isValidTileProjectId(id))
|
|
1913
|
+
return undefined;
|
|
1914
|
+
return { file: tilegen.tileAssetPath(id, relative), dirs: SERVABLE_TILE_DIRS };
|
|
1915
|
+
}
|
|
1558
1916
|
if (!seqgen.isValidSequenceJobId(id))
|
|
1559
1917
|
return undefined;
|
|
1560
1918
|
return { file: seqgen.sequenceAssetPath(id, relative), dirs: SERVABLE_SEQUENCE_DIRS };
|
|
@@ -1598,7 +1956,12 @@ async function handleAsset(req, res) {
|
|
|
1598
1956
|
return;
|
|
1599
1957
|
}
|
|
1600
1958
|
const scope = segments[0];
|
|
1601
|
-
|
|
1959
|
+
// ⚠️ 这是**运行时**白名单,必须与 `AssetScope` 类型和 `resolveAssetTarget` 的分支
|
|
1960
|
+
// 三处同步。只改类型不改这里,TypeScript 一句话都不会说 —— `scope` 是断言出来的,
|
|
1961
|
+
// 而漏掉的那个 scope 会在这里直接掉进 404(界面表现只是「图裂了」)。
|
|
1962
|
+
// 实测:`tile-assets` 就这么漏过一轮,模板图全 404。
|
|
1963
|
+
const SCOPES = ["assets", "image-assets", "sequence-assets", "rig-assets", "tile-assets"];
|
|
1964
|
+
if (!SCOPES.includes(scope)) {
|
|
1602
1965
|
sendText(res, 404, "Not Found");
|
|
1603
1966
|
return;
|
|
1604
1967
|
}
|
package/lib/jsonio.js
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* 三个功能模块(八方向图 / 图片生成 / 序列帧生成)各自独立存目录,
|
|
5
5
|
* 但「原子写 + 读文件 + 日志裁剪」这几件事是一样的,抽在这里避免重复。
|
|
6
6
|
*/
|
|
7
|
-
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
8
|
-
import { dirname } from "node:path";
|
|
7
|
+
import { mkdir, readFile, readdir, rename, rm, writeFile } from "node:fs/promises";
|
|
8
|
+
import { dirname, join } from "node:path";
|
|
9
9
|
import { randomUUID } from "node:crypto";
|
|
10
10
|
export async function readJson(file) {
|
|
11
11
|
try {
|
|
@@ -21,12 +21,58 @@ export async function readJson(file) {
|
|
|
21
21
|
* 临时文件名必须**每次唯一**:轮询器和用户操作可能同时写同一个 job.json,
|
|
22
22
|
* 共用一个 `.tmp` 名字时,先完成的 rename 会把临时文件移走,后一个就会
|
|
23
23
|
* ENOENT(实测踩过)。
|
|
24
|
+
*
|
|
25
|
+
* ⚠️ Windows 上 `rename` 会被**暂时占用目标文件的进程**挡下来,报
|
|
26
|
+
* `EPERM: operation not permitted`(杀软扫描、索引器,或同一进程里另一个
|
|
27
|
+
* 还没释放句柄的写)。实测:用户点「保存布局」时后台恰好在收尾写同一次
|
|
28
|
+
* project.json,就报「保存失败」,而且**几次都一样**,看起来像功能坏了。
|
|
29
|
+
* 所以这里要重试;重试仍失败就清掉临时文件再抛,别留一地 `.tmp`。
|
|
24
30
|
*/
|
|
25
31
|
export async function writeJsonAtomic(file, value) {
|
|
26
32
|
await mkdir(dirname(file), { recursive: true });
|
|
27
33
|
const tmp = `${file}.${randomUUID().slice(0, 8)}.tmp`;
|
|
28
34
|
await writeFile(tmp, `${JSON.stringify(value, null, 2)}\n`, "utf8");
|
|
29
|
-
|
|
35
|
+
let lastError;
|
|
36
|
+
// 实测 225ms 不够:杀软/索引器的扫描句柄能占住几百毫秒。
|
|
37
|
+
// 8 次、递增到 240ms,总共约 1.1s —— 用户点一次保存等得住,
|
|
38
|
+
// 而这一点延迟远好过报一句「保存失败」。
|
|
39
|
+
for (let attempt = 0; attempt < 8; attempt++) {
|
|
40
|
+
try {
|
|
41
|
+
await rename(tmp, file);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
lastError = error;
|
|
46
|
+
// EPERM / EBUSY / EACCES 都是「目标被占着」,等一下再试;
|
|
47
|
+
// 其它错误(比如路径不存在)重试也没意义
|
|
48
|
+
const code = error.code;
|
|
49
|
+
if (code !== "EPERM" && code !== "EBUSY" && code !== "EACCES")
|
|
50
|
+
break;
|
|
51
|
+
await new Promise((resolve) => setTimeout(resolve, Math.min(240, 30 * (attempt + 1))));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
await rm(tmp, { force: true }).catch(() => undefined);
|
|
55
|
+
throw lastError;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* 清掉某个目录里遗留的 `*.tmp`(写失败留下的)。
|
|
59
|
+
*
|
|
60
|
+
* `writeJsonAtomic` 失败时会尽力删掉自己的临时文件,但**进程被杀**
|
|
61
|
+
* (DSH 重启、任务管理器结束)时来不及 —— 实测在项目目录里留下过
|
|
62
|
+
* `project.json.fa69b405.tmp`。它们不会被读,只会越积越乱,
|
|
63
|
+
* 所以在读项目时顺手扫一次。
|
|
64
|
+
*/
|
|
65
|
+
export async function sweepTempFiles(dir) {
|
|
66
|
+
let names;
|
|
67
|
+
try {
|
|
68
|
+
names = await readdir(dir);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
await Promise.all(names
|
|
74
|
+
.filter((name) => name.endsWith(".tmp"))
|
|
75
|
+
.map((name) => rm(join(dir, name), { force: true }).catch(() => undefined)));
|
|
30
76
|
}
|
|
31
77
|
export function appendJobLog(log, level, message, limit = 200) {
|
|
32
78
|
log.push({ at: Date.now(), level, message });
|
package/lib/links.js
CHANGED
|
@@ -28,10 +28,18 @@
|
|
|
28
28
|
export const OPEN_QUERY_KEY = "dsh-gmm";
|
|
29
29
|
/** 工作台在 `main` 槽里的 key,也是侧栏 `sidebar.panellist` 的 id。 */
|
|
30
30
|
export const PANEL_KEY = "gameStudio";
|
|
31
|
-
/**
|
|
32
|
-
export const STUDIO_MODULES = ["sprite", "image", "sequence", "rig"];
|
|
31
|
+
/** 五个模块的 key。 */
|
|
32
|
+
export const STUDIO_MODULES = ["sprite", "image", "sequence", "rig", "tile"];
|
|
33
33
|
/** 八方向图的四个阶段。 */
|
|
34
34
|
export const SPRITE_STAGES = ["images", "videos", "frames", "sheet"];
|
|
35
|
+
/**
|
|
36
|
+
* 地图地块的四个阶段。
|
|
37
|
+
*
|
|
38
|
+
* ⚠️ 这里**故意不含 `template`**:模板(参考图)已经不是单独一步了,
|
|
39
|
+
* 它在「生成地块」开始时按当前占格形状自动准备(见 `tilegen.ensureTemplates`)。
|
|
40
|
+
* 老的 `stage=template` 深链接会被忽略(`TILE_STAGES.includes` 不通过)。
|
|
41
|
+
*/
|
|
42
|
+
export const TILE_STAGES = ["generate", "review", "map", "export"];
|
|
35
43
|
let clientOrigin = "";
|
|
36
44
|
/** 浏览器半区上报真实 origin。只接受 http(s),其它一律忽略。 */
|
|
37
45
|
export function rememberClientOrigin(origin) {
|