dsh-game-material-master 0.3.0 → 0.6.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 +74 -9
- package/README.md +60 -8
- package/lib/{tilemedia.js → bitmap.js} +21 -3
- package/lib/client.js +2929 -162
- package/lib/config.js +20 -1
- package/lib/index.js +239 -3
- package/lib/jsonio.js +24 -3
- package/lib/links.js +11 -2
- package/lib/mapauto.js +126 -0
- package/lib/mapdoc.js +444 -0
- package/lib/mapexport.js +177 -0
- package/lib/mapgen.js +1579 -0
- package/lib/mapgeom.js +308 -0
- package/lib/maprender.js +430 -0
- package/lib/mapsample.js +36 -0
- package/lib/mapsamplegen.js +760 -0
- package/lib/maptiles.js +253 -0
- package/lib/rigexport.js +29 -98
- package/lib/riggen.js +6 -4
- package/lib/rigpath.js +47 -10
- package/lib/rigpreview.js +91 -84
- package/lib/rigsemantics.js +4 -4
- package/lib/rigvalidate.js +72 -14
- package/lib/spine.js +147 -80
- package/lib/tilegen.js +1 -1
- package/lib/tilegeom.js +1 -1
- package/lib/tools.js +401 -24
- package/lib/wire.js +179 -3
- package/lib/zip.js +127 -0
- package/package.json +10 -4
package/lib/config.js
CHANGED
|
@@ -8,6 +8,7 @@ import { mkdir, readFile, rename, stat, writeFile } from "node:fs/promises";
|
|
|
8
8
|
import { homedir } from "node:os";
|
|
9
9
|
import { join } from "node:path";
|
|
10
10
|
import { DEFAULT_ROW_ORDER } from "./directions.js";
|
|
11
|
+
import { STUDIO_MODULES } from "./links.js";
|
|
11
12
|
/**
|
|
12
13
|
* 上一版的默认行序(按生成依赖顺序)。方位语义修正后,默认改成罗盘顺时针;
|
|
13
14
|
* 配置里还留着旧默认值的用户应当自动迁过去,而不是被卡在旧顺序上。
|
|
@@ -131,6 +132,7 @@ export const DEFAULT_CONFIG = {
|
|
|
131
132
|
edgeShrink: 0,
|
|
132
133
|
rowOrder: [...DEFAULT_ROW_ORDER],
|
|
133
134
|
rowOrderVersion: ROW_ORDER_VERSION,
|
|
135
|
+
hiddenModules: [],
|
|
134
136
|
concurrency: 3
|
|
135
137
|
};
|
|
136
138
|
export const ARK_MODEL_PRESETS = [
|
|
@@ -177,6 +179,20 @@ function asNumber(value, fallback, min, max) {
|
|
|
177
179
|
return fallback;
|
|
178
180
|
return Math.min(max, Math.max(min, n));
|
|
179
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* 收敛「被隐藏的功能模块」:只留合法 key、去重,并按 `STUDIO_MODULES` 的固定
|
|
184
|
+
* 顺序排列。
|
|
185
|
+
*
|
|
186
|
+
* 排序不是为了好看:它让「同一组勾选」永远序列化成同一份 JSON,于是界面可以
|
|
187
|
+
* 用字符串比较判断「有没有变」,自检里也能直接对整份数组断言。
|
|
188
|
+
* 不认识的 key 一律丢掉——旧版本配置里留着的模块名不该让整个设置页起不来。
|
|
189
|
+
*/
|
|
190
|
+
export function normalizeHiddenModules(value) {
|
|
191
|
+
if (!Array.isArray(value))
|
|
192
|
+
return [];
|
|
193
|
+
const wanted = new Set(value.filter((key) => typeof key === "string"));
|
|
194
|
+
return STUDIO_MODULES.filter((key) => wanted.has(key));
|
|
195
|
+
}
|
|
180
196
|
/** 把任意读入的 JSON 收敛成一份合法配置,缺项一律回落默认值。 */
|
|
181
197
|
export function normalizeConfig(input) {
|
|
182
198
|
const raw = (input ?? {});
|
|
@@ -225,6 +241,7 @@ export function normalizeConfig(input) {
|
|
|
225
241
|
edgeShrink: asInt(raw.edgeShrink, DEFAULT_CONFIG.edgeShrink, 0, 8),
|
|
226
242
|
rowOrder,
|
|
227
243
|
rowOrderVersion: ROW_ORDER_VERSION,
|
|
244
|
+
hiddenModules: normalizeHiddenModules(raw.hiddenModules),
|
|
228
245
|
concurrency: asInt(raw.concurrency, DEFAULT_CONFIG.concurrency, 1, 8)
|
|
229
246
|
};
|
|
230
247
|
}
|
|
@@ -237,7 +254,7 @@ export async function loadConfig() {
|
|
|
237
254
|
cache = normalizeConfig(JSON.parse(text));
|
|
238
255
|
}
|
|
239
256
|
catch {
|
|
240
|
-
cache = { ...DEFAULT_CONFIG, rowOrder: [...DEFAULT_ROW_ORDER] };
|
|
257
|
+
cache = { ...DEFAULT_CONFIG, rowOrder: [...DEFAULT_ROW_ORDER], hiddenModules: [] };
|
|
241
258
|
}
|
|
242
259
|
return cache;
|
|
243
260
|
}
|
|
@@ -257,6 +274,8 @@ export function maskConfig(config) {
|
|
|
257
274
|
return {
|
|
258
275
|
...rest,
|
|
259
276
|
rowOrder: [...config.rowOrder],
|
|
277
|
+
// 复制一份再回传:界面改勾选后会把它当草稿改,共享同一个数组会互相写花。
|
|
278
|
+
hiddenModules: [...config.hiddenModules],
|
|
260
279
|
arkApiKeySet: arkApiKey.trim() !== "",
|
|
261
280
|
arkApiKeyHint: hintOf(arkApiKey),
|
|
262
281
|
minimaxApiKeySet: minimaxApiKey.trim() !== "",
|
package/lib/index.js
CHANGED
|
@@ -26,6 +26,7 @@ import * as imagegen from "./imagegen.js";
|
|
|
26
26
|
import * as seqgen from "./seqgen.js";
|
|
27
27
|
import * as riggen from "./riggen.js";
|
|
28
28
|
import * as tilegen from "./tilegen.js";
|
|
29
|
+
import * as mapgen from "./mapgen.js";
|
|
29
30
|
import { MANIFEST, METHODS, SERVICE_NAME } from "./wire.js";
|
|
30
31
|
import { rememberClientOrigin } from "./links.js";
|
|
31
32
|
import { registerStudioTools } from "./tools.js";
|
|
@@ -170,6 +171,9 @@ const MIME_TYPES = {
|
|
|
170
171
|
".mp4": "video/mp4",
|
|
171
172
|
".webm": "video/webm",
|
|
172
173
|
".json": "application/json; charset=utf-8",
|
|
174
|
+
// 模块六的导出包:`.zip` 少了这一条会掉进 octet-stream —— 浏览器照样存得下来,
|
|
175
|
+
// 但少了这个类型,某些浏览器/壳会把「另存为」的默认扩展名写错。
|
|
176
|
+
".zip": "application/zip",
|
|
173
177
|
// 骨骼动画的自包含预览是一整个 HTML 文件,必须用 text/html 才能被 iframe 渲染。
|
|
174
178
|
".html": "text/html; charset=utf-8",
|
|
175
179
|
".atlas": "text/plain; charset=utf-8"
|
|
@@ -299,7 +303,10 @@ export class GameStudioGateway extends TypertRemoteService {
|
|
|
299
303
|
"bgTolerance",
|
|
300
304
|
"edgeShrink",
|
|
301
305
|
"rowOrder",
|
|
302
|
-
"concurrency"
|
|
306
|
+
"concurrency",
|
|
307
|
+
// 功能可见性:设置页的「功能管理」写它,`normalizeConfig` 会收敛成合法子集。
|
|
308
|
+
// 对话工具面**不允许**改它(见 tools.ts),否则模型可以自己把隐藏的功能放出来。
|
|
309
|
+
"hiddenModules"
|
|
303
310
|
];
|
|
304
311
|
for (const key of directKeys) {
|
|
305
312
|
if (input[key] !== undefined)
|
|
@@ -641,6 +648,16 @@ export class GameStudioGateway extends TypertRemoteService {
|
|
|
641
648
|
});
|
|
642
649
|
return { ok: true, id, module, reviewMode };
|
|
643
650
|
}
|
|
651
|
+
// 地图编辑器(模块六)。**别重犯 rig / tile 那两个漏**:wire 的 module 枚举
|
|
652
|
+
// 与这里必须成对加,否则「固定流程第 0 步必须问清审核模式」对 map 落不了地。
|
|
653
|
+
if (module === "map") {
|
|
654
|
+
// 同样走带写队列的 `patchMapProject`:地图文档的防抖落盘也在同一条队列上。
|
|
655
|
+
await mapgen.patchMapProject(id, (project) => {
|
|
656
|
+
project.reviewMode = reviewMode;
|
|
657
|
+
appendJobLog(project.logs, "info", `审核模式:${reviewMode === "manual" ? "每一步人工审核" : "agent 自动审核"}`);
|
|
658
|
+
});
|
|
659
|
+
return { ok: true, id, module, reviewMode };
|
|
660
|
+
}
|
|
644
661
|
throw new Error(`未知模块:${module}`);
|
|
645
662
|
}
|
|
646
663
|
// ── 地图地块生成(模块五)──────────────────────────────────────────────
|
|
@@ -915,6 +932,183 @@ export class GameStudioGateway extends TypertRemoteService {
|
|
|
915
932
|
}
|
|
916
933
|
return { ok: true, dir };
|
|
917
934
|
}
|
|
935
|
+
// ── 地图编辑器(模块六,单项目 = 单张地图)──────────────────────────────
|
|
936
|
+
async listMapProjects() {
|
|
937
|
+
return { projects: await mapgen.listMapProjects() };
|
|
938
|
+
}
|
|
939
|
+
async createMapProject(payload) {
|
|
940
|
+
const input = asRecord(payload);
|
|
941
|
+
const project = await mapgen.createMapProject(asString(input.name, "未命名地图"), {
|
|
942
|
+
grid: input.grid,
|
|
943
|
+
cols: typeof input.cols === "number" ? input.cols : undefined,
|
|
944
|
+
rows: typeof input.rows === "number" ? input.rows : undefined
|
|
945
|
+
});
|
|
946
|
+
return mapgen.mapView(project);
|
|
947
|
+
}
|
|
948
|
+
async getMapProject(payload) {
|
|
949
|
+
const input = asRecord(payload);
|
|
950
|
+
const id = asString(input.projectId);
|
|
951
|
+
const project = await mapgen.readMapProject(id);
|
|
952
|
+
if (project === undefined)
|
|
953
|
+
throw new Error(`项目不存在:${id}`);
|
|
954
|
+
// `input.mapId` 只是旧深链接的兼容位:单项目 = 单张地图,它没有意义,忽略。
|
|
955
|
+
return mapgen.mapView(project);
|
|
956
|
+
}
|
|
957
|
+
async saveMapProject(payload) {
|
|
958
|
+
const input = asRecord(payload);
|
|
959
|
+
const id = asString(input.projectId);
|
|
960
|
+
const name = typeof input.name === "string" ? input.name.trim() : "";
|
|
961
|
+
const next = await mapgen.patchMapProject(id, (project) => {
|
|
962
|
+
if (name !== "")
|
|
963
|
+
project.name = name;
|
|
964
|
+
return project;
|
|
965
|
+
});
|
|
966
|
+
return mapgen.mapView(next);
|
|
967
|
+
}
|
|
968
|
+
async deleteMapProject(payload) {
|
|
969
|
+
const id = asString(asRecord(payload).projectId);
|
|
970
|
+
await mapgen.deleteMapProject(id);
|
|
971
|
+
return { ok: true };
|
|
972
|
+
}
|
|
973
|
+
async importMapTileset(payload) {
|
|
974
|
+
const input = asRecord(payload);
|
|
975
|
+
const result = await mapgen.importMapTileset(asString(input.projectId), asString(input.name), asString(input.data), {
|
|
976
|
+
cell: input.cell,
|
|
977
|
+
shape: input.shape,
|
|
978
|
+
animRows: input.animRows
|
|
979
|
+
});
|
|
980
|
+
return { tilesets: result.tilesets.map(mapTilesetBrief), warnings: result.warnings };
|
|
981
|
+
}
|
|
982
|
+
/** 内置素材:本机现算,零成本(4 张自动连接图 + 1 张道具图)。 */
|
|
983
|
+
async importSampleTileset(payload) {
|
|
984
|
+
const result = await mapgen.importSampleTileset(asString(asRecord(payload).projectId));
|
|
985
|
+
return { tilesets: result.tilesets.map(mapTilesetBrief), warnings: result.warnings };
|
|
986
|
+
}
|
|
987
|
+
async saveMapTileset(payload) {
|
|
988
|
+
const input = asRecord(payload);
|
|
989
|
+
const result = await mapgen.saveMapTileset(asString(input.projectId), asString(input.tilesetId), {
|
|
990
|
+
name: typeof input.name === "string" ? input.name : undefined,
|
|
991
|
+
cell: input.cell === undefined ? undefined : { width: Number(asRecord(input.cell).width), height: Number(asRecord(input.cell).height) },
|
|
992
|
+
shape: mapgen.parseTilesetShape(input.shape),
|
|
993
|
+
animRows: typeof input.animRows === "boolean" ? input.animRows : undefined
|
|
994
|
+
});
|
|
995
|
+
return { tileset: mapTilesetBrief(result.tileset), warnings: result.warnings };
|
|
996
|
+
}
|
|
997
|
+
async removeMapTileset(payload) {
|
|
998
|
+
const input = asRecord(payload);
|
|
999
|
+
const result = await mapgen.removeMapTileset(asString(input.projectId), asString(input.tilesetId));
|
|
1000
|
+
return { ok: true, ...result };
|
|
1001
|
+
}
|
|
1002
|
+
async saveMapDoc(payload) {
|
|
1003
|
+
const input = asRecord(payload);
|
|
1004
|
+
const id = asString(input.projectId);
|
|
1005
|
+
const result = await mapgen.saveMapDocStructure(id, {
|
|
1006
|
+
name: typeof input.name === "string" ? input.name : undefined,
|
|
1007
|
+
cols: typeof input.cols === "number" ? input.cols : undefined,
|
|
1008
|
+
rows: typeof input.rows === "number" ? input.rows : undefined,
|
|
1009
|
+
layers: Array.isArray(input.layers) ? input.layers : undefined,
|
|
1010
|
+
addLayer: input.addLayer === undefined ? undefined : { name: typeof asRecord(input.addLayer).name === "string" ? asRecord(input.addLayer).name : undefined },
|
|
1011
|
+
removeLayerId: typeof input.removeLayerId === "string" ? input.removeLayerId : undefined,
|
|
1012
|
+
moveLayer: input.moveLayer === undefined ? undefined : { id: asString(asRecord(input.moveLayer).id), toIndex: Number(asRecord(input.moveLayer).toIndex) },
|
|
1013
|
+
clearLayerId: typeof input.clearLayerId === "string" ? input.clearLayerId : undefined,
|
|
1014
|
+
wrap: typeof input.wrap === "boolean" ? input.wrap : undefined
|
|
1015
|
+
});
|
|
1016
|
+
// 结构变了(尺寸 / 图层 / 环绕)→ 界面要重算画布与计划,把最新视图一起回给它。
|
|
1017
|
+
const project = await mapgen.readMapProject(id);
|
|
1018
|
+
return {
|
|
1019
|
+
doc: { id: result.doc.id, name: result.doc.name, cols: result.doc.cols, rows: result.doc.rows, layers: result.doc.layers },
|
|
1020
|
+
dropped: result.dropped,
|
|
1021
|
+
warnings: result.warnings,
|
|
1022
|
+
view: project === undefined ? undefined : await mapgen.mapView(project)
|
|
1023
|
+
};
|
|
1024
|
+
}
|
|
1025
|
+
async applyMapOps(payload) {
|
|
1026
|
+
const input = asRecord(payload);
|
|
1027
|
+
return mapgen.applyMapOps(asString(input.projectId), Array.isArray(input.ops) ? input.ops : [], {
|
|
1028
|
+
stroke: typeof input.stroke === "string" && input.stroke !== "" ? input.stroke : undefined,
|
|
1029
|
+
have: mapHave(input.have)
|
|
1030
|
+
});
|
|
1031
|
+
}
|
|
1032
|
+
async mapUndo(payload) {
|
|
1033
|
+
const input = asRecord(payload);
|
|
1034
|
+
return mapgen.undoMap(asString(input.projectId), mapHave(input.have));
|
|
1035
|
+
}
|
|
1036
|
+
async mapRedo(payload) {
|
|
1037
|
+
const input = asRecord(payload);
|
|
1038
|
+
return mapgen.redoMap(asString(input.projectId), mapHave(input.have));
|
|
1039
|
+
}
|
|
1040
|
+
async mapPlan(payload) {
|
|
1041
|
+
const input = asRecord(payload);
|
|
1042
|
+
const view = asRecord(input.view);
|
|
1043
|
+
return mapgen.mapPlan(asString(input.projectId), {
|
|
1044
|
+
view: { x: Number(view.x) || 0, y: Number(view.y) || 0, width: Number(view.width) || 0, height: Number(view.height) || 0 },
|
|
1045
|
+
have: mapHave(input.have),
|
|
1046
|
+
layerIds: Array.isArray(input.layerIds) ? input.layerIds.map((entry) => Number(entry)) : undefined,
|
|
1047
|
+
pending: Array.isArray(input.pending) ? input.pending : undefined
|
|
1048
|
+
});
|
|
1049
|
+
}
|
|
1050
|
+
/** 一键生成:整层一次提交(一次撤销就能回去),同种子逐字节可复现。 */
|
|
1051
|
+
async runMapGenerate(payload) {
|
|
1052
|
+
const input = asRecord(payload);
|
|
1053
|
+
const kind = asString(input.kind, "random");
|
|
1054
|
+
if (kind !== "random" && kind !== "maze" && kind !== "continent")
|
|
1055
|
+
throw new Error(`未知生成算法:${kind}(可选 random / maze / continent)`);
|
|
1056
|
+
return mapgen.runMapGenerate(asString(input.projectId), {
|
|
1057
|
+
kind,
|
|
1058
|
+
tiles: Array.isArray(input.tiles) ? input.tiles.map((entry) => Number(entry)) : undefined,
|
|
1059
|
+
seed: typeof input.seed === "number" ? input.seed : undefined,
|
|
1060
|
+
layerId: typeof input.layerId === "string" && input.layerId !== "" ? input.layerId : undefined
|
|
1061
|
+
});
|
|
1062
|
+
}
|
|
1063
|
+
async runMapPreview(payload) {
|
|
1064
|
+
return mapgen.runPreviewStage(asString(asRecord(payload).projectId));
|
|
1065
|
+
}
|
|
1066
|
+
async runMapExport(payload) {
|
|
1067
|
+
const input = asRecord(payload);
|
|
1068
|
+
return mapgen.runExportStage(asString(input.projectId), {
|
|
1069
|
+
scale: typeof input.scale === "number" ? input.scale : undefined,
|
|
1070
|
+
formats: input.formats === undefined ? undefined : input.formats
|
|
1071
|
+
});
|
|
1072
|
+
}
|
|
1073
|
+
async cancelMapJob(payload) {
|
|
1074
|
+
mapgen.cancelMapJob(asString(asRecord(payload).projectId));
|
|
1075
|
+
return { ok: true };
|
|
1076
|
+
}
|
|
1077
|
+
async setMapApproved(payload) {
|
|
1078
|
+
const input = asRecord(payload);
|
|
1079
|
+
const id = asString(input.projectId);
|
|
1080
|
+
const stage = asString(input.stage);
|
|
1081
|
+
const approved = input.approved === true;
|
|
1082
|
+
await mapgen.patchMapProject(id, (project) => {
|
|
1083
|
+
const stages = stage === "" ? mapgen.MAP_STAGES : [stage];
|
|
1084
|
+
for (const key of stages) {
|
|
1085
|
+
if (!mapgen.MAP_STAGES.includes(key))
|
|
1086
|
+
throw new Error(`未知阶段:${key}`);
|
|
1087
|
+
const current = project.stages[key];
|
|
1088
|
+
project.stages[key] = { ...current, approved, approvedAt: approved ? new Date().toISOString() : undefined };
|
|
1089
|
+
}
|
|
1090
|
+
appendJobLog(project.logs, "info", `${approved ? "通过" : "取消通过"}:${stage === "" ? "全部阶段" : stage}`);
|
|
1091
|
+
});
|
|
1092
|
+
return { ok: true, stage: stage === "" ? null : stage, approved };
|
|
1093
|
+
}
|
|
1094
|
+
async revealMapProject(payload) {
|
|
1095
|
+
const input = asRecord(payload);
|
|
1096
|
+
const id = asString(input.projectId);
|
|
1097
|
+
/**
|
|
1098
|
+
* `target: "export"` = 打开**导出目录**(界面上的「打开导出目录」按钮);
|
|
1099
|
+
* 其余(含省略)= 项目目录。两者都不是随便拼路径 —— 见 `mapgen.mapRevealDir`:
|
|
1100
|
+
* 它按与导出同一处口径算目录名,还没导出过就退回项目目录并用 `fallback` 说明。
|
|
1101
|
+
*/
|
|
1102
|
+
const { dir, fallback } = await mapgen.mapRevealDir(id, asString(input.target));
|
|
1103
|
+
const command = process.platform === "darwin" ? "open" : process.platform === "win32" ? "explorer" : "xdg-open";
|
|
1104
|
+
try {
|
|
1105
|
+
spawn(command, [dir], { detached: true, stdio: "ignore" }).unref();
|
|
1106
|
+
}
|
|
1107
|
+
catch (error) {
|
|
1108
|
+
throw new Error(`无法打开目录:${error instanceof Error ? error.message : String(error)}`);
|
|
1109
|
+
}
|
|
1110
|
+
return { ok: true, dir, fallback };
|
|
1111
|
+
}
|
|
918
1112
|
// ── 流水线控制 ──────────────────────────────────────────────────────────
|
|
919
1113
|
async runImage(payload) {
|
|
920
1114
|
const input = asRecord(payload);
|
|
@@ -1791,7 +1985,7 @@ export class GameStudioGateway extends TypertRemoteService {
|
|
|
1791
1985
|
if (typeof record.role === "string")
|
|
1792
1986
|
patch.role = record.role;
|
|
1793
1987
|
if (record.parent === null)
|
|
1794
|
-
patch.parent =
|
|
1988
|
+
patch.parent = null;
|
|
1795
1989
|
else if (typeof record.parent === "string")
|
|
1796
1990
|
patch.parent = record.parent;
|
|
1797
1991
|
if (Array.isArray(record.proximal) && record.proximal.length === 2)
|
|
@@ -1876,6 +2070,31 @@ function kickImageKey(jobId) {
|
|
|
1876
2070
|
function disposeImageTasks() {
|
|
1877
2071
|
imageTasks.clear();
|
|
1878
2072
|
}
|
|
2073
|
+
/**
|
|
2074
|
+
* 素材的紧凑摘要(网关回给界面 / 工具面用)。
|
|
2075
|
+
*
|
|
2076
|
+
* 不回**整张 `tiles` 表**:一张大素材图能切出上千个小格,每次导入都把它们发一遍
|
|
2077
|
+
* 没有意义(界面要的是几何 + 调色板,调色板在 `mapView.tilesets[].palette` 里)。
|
|
2078
|
+
*/
|
|
2079
|
+
function mapTilesetBrief(tileset) {
|
|
2080
|
+
return {
|
|
2081
|
+
id: tileset.id,
|
|
2082
|
+
name: tileset.name,
|
|
2083
|
+
file: tileset.file,
|
|
2084
|
+
imageWidth: tileset.imageWidth,
|
|
2085
|
+
imageHeight: tileset.imageHeight,
|
|
2086
|
+
cell: tileset.cell,
|
|
2087
|
+
shape: tileset.shape,
|
|
2088
|
+
animRows: tileset.animRows,
|
|
2089
|
+
variants: tileset.variants,
|
|
2090
|
+
builtin: tileset.builtin === true ? true : undefined,
|
|
2091
|
+
tileCount: tileset.tiles.length
|
|
2092
|
+
};
|
|
2093
|
+
}
|
|
2094
|
+
/** 客户端报上来的 chunk key 列表收口(不是数组就是「没报」)。 */
|
|
2095
|
+
function mapHave(value) {
|
|
2096
|
+
return Array.isArray(value) ? value.map((entry) => String(entry)) : undefined;
|
|
2097
|
+
}
|
|
1879
2098
|
// ── 静态资源路由 ────────────────────────────────────────────────────────
|
|
1880
2099
|
/** 各模块允许通过 HTTP 路由读取的子目录白名单。 */
|
|
1881
2100
|
// `turn/` 是转圈截帧的候选帧与缩略条带:轴上那八个圆圈是对着条带拖的,
|
|
@@ -1889,6 +2108,16 @@ const SERVABLE_SEQUENCE_DIRS = new Set(["refs", "video-refs", "videos", "frames"
|
|
|
1889
2108
|
const SERVABLE_RIG_DIRS = new Set(["source", "sheet", "parts", "layout", "rig", "atlas", "export"]);
|
|
1890
2109
|
/** 地图地块:模板、原始生成、规整产物、装饰、地图、导出包。 */
|
|
1891
2110
|
const SERVABLE_TILE_DIRS = new Set(["template", "raw", "cell", "decor", "map", "export"]);
|
|
2111
|
+
/**
|
|
2112
|
+
* 地图编辑器(模块六):素材原图、验收预览图、导出包。
|
|
2113
|
+
*
|
|
2114
|
+
* ⚠️ 这个白名单、`AssetScope` 类型、`resolveAssetTarget` 的分支、`SCOPES`
|
|
2115
|
+
* 数组**四处必须同步**:漏一处就是 403,而界面只表现为「图裂了」。
|
|
2116
|
+
*
|
|
2117
|
+
* 单项目 = 单张地图之后 `maps/` 目录不存在了(地图就是项目根下的 `map.json`,
|
|
2118
|
+
* 不通过资源路由读)。
|
|
2119
|
+
*/
|
|
2120
|
+
const SERVABLE_MAP_DIRS = new Set(["tilesets", "preview", "export"]);
|
|
1892
2121
|
function resolveAssetTarget(scope, id, relative) {
|
|
1893
2122
|
if (scope === "assets") {
|
|
1894
2123
|
if (!PROJECT_ID_PATTERN.test(id))
|
|
@@ -1910,6 +2139,11 @@ function resolveAssetTarget(scope, id, relative) {
|
|
|
1910
2139
|
return undefined;
|
|
1911
2140
|
return { file: tilegen.tileAssetPath(id, relative), dirs: SERVABLE_TILE_DIRS };
|
|
1912
2141
|
}
|
|
2142
|
+
if (scope === "map-assets") {
|
|
2143
|
+
if (!mapgen.isValidMapProjectId(id))
|
|
2144
|
+
return undefined;
|
|
2145
|
+
return { file: mapgen.mapAssetPath(id, relative), dirs: SERVABLE_MAP_DIRS };
|
|
2146
|
+
}
|
|
1913
2147
|
if (!seqgen.isValidSequenceJobId(id))
|
|
1914
2148
|
return undefined;
|
|
1915
2149
|
return { file: seqgen.sequenceAssetPath(id, relative), dirs: SERVABLE_SEQUENCE_DIRS };
|
|
@@ -1924,6 +2158,8 @@ function sendText(res, status, text) {
|
|
|
1924
2158
|
* /dsh-game-material-master/image-assets/<任务 id>/<相对路径> 图片生成
|
|
1925
2159
|
* /dsh-game-material-master/sequence-assets/<任务 id>/<相对路径> 序列帧生成
|
|
1926
2160
|
* /dsh-game-material-master/rig-assets/<任务 id>/<相对路径> 骨骼动画生成
|
|
2161
|
+
* /dsh-game-material-master/tile-assets/<项目 id>/<相对路径> 45°地图地块生成
|
|
2162
|
+
* /dsh-game-material-master/map-assets/<项目 id>/<相对路径> 地图编辑器
|
|
1927
2163
|
*
|
|
1928
2164
|
* 支持 Range:没有它浏览器里的 <video> 就不能拖动进度条,验收视频时会很难受。
|
|
1929
2165
|
*/
|
|
@@ -1957,7 +2193,7 @@ async function handleAsset(req, res) {
|
|
|
1957
2193
|
// 三处同步。只改类型不改这里,TypeScript 一句话都不会说 —— `scope` 是断言出来的,
|
|
1958
2194
|
// 而漏掉的那个 scope 会在这里直接掉进 404(界面表现只是「图裂了」)。
|
|
1959
2195
|
// 实测:`tile-assets` 就这么漏过一轮,模板图全 404。
|
|
1960
|
-
const SCOPES = ["assets", "image-assets", "sequence-assets", "rig-assets", "tile-assets"];
|
|
2196
|
+
const SCOPES = ["assets", "image-assets", "sequence-assets", "rig-assets", "tile-assets", "map-assets"];
|
|
1961
2197
|
if (!SCOPES.includes(scope)) {
|
|
1962
2198
|
sendText(res, 404, "Not Found");
|
|
1963
2199
|
return;
|
package/lib/jsonio.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* 三个功能模块(八方向图 / 图片生成 / 序列帧生成)各自独立存目录,
|
|
5
5
|
* 但「原子写 + 读文件 + 日志裁剪」这几件事是一样的,抽在这里避免重复。
|
|
6
6
|
*/
|
|
7
|
-
import { mkdir, readFile, readdir, rename, rm, writeFile } from "node:fs/promises";
|
|
7
|
+
import { mkdir, readFile, readdir, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
8
8
|
import { dirname, join } from "node:path";
|
|
9
9
|
import { randomUUID } from "node:crypto";
|
|
10
10
|
export async function readJson(file) {
|
|
@@ -62,7 +62,16 @@ export async function writeJsonAtomic(file, value) {
|
|
|
62
62
|
* `project.json.fa69b405.tmp`。它们不会被读,只会越积越乱,
|
|
63
63
|
* 所以在读项目时顺手扫一次。
|
|
64
64
|
*/
|
|
65
|
-
|
|
65
|
+
/**
|
|
66
|
+
* 清掉目录里**陈旧**的 `*.tmp`(原子写留下的中间态)。
|
|
67
|
+
*
|
|
68
|
+
* ⚠️ **不能见到 `*.tmp` 就删**:`writeJsonAtomic` 是「写 tmp → rename」,而读路径
|
|
69
|
+
* (`readMapProject` / `readTileProject`)会顺手扫一次目录。删除所有 tmp 的话,
|
|
70
|
+
* 读与写并发时正在写的那个中间文件会被读路径删掉,紧接着 `rename` 报 `ENOENT`
|
|
71
|
+
* —— 真机与自检各撞到过一次(`project.json.<hex>.tmp -> project.json`)。
|
|
72
|
+
* 所以只清「超过 `staleMs` 没动过」的那些(崩溃留下的残骸),默认 5 分钟。
|
|
73
|
+
*/
|
|
74
|
+
export async function sweepTempFiles(dir, staleMs = 5 * 60 * 1000) {
|
|
66
75
|
let names;
|
|
67
76
|
try {
|
|
68
77
|
names = await readdir(dir);
|
|
@@ -70,9 +79,21 @@ export async function sweepTempFiles(dir) {
|
|
|
70
79
|
catch {
|
|
71
80
|
return;
|
|
72
81
|
}
|
|
82
|
+
const now = Date.now();
|
|
73
83
|
await Promise.all(names
|
|
74
84
|
.filter((name) => name.endsWith(".tmp"))
|
|
75
|
-
.map((name) =>
|
|
85
|
+
.map(async (name) => {
|
|
86
|
+
const file = join(dir, name);
|
|
87
|
+
try {
|
|
88
|
+
const info = await stat(file);
|
|
89
|
+
if (now - info.mtimeMs < staleMs)
|
|
90
|
+
return; // 可能正有人写:别动它
|
|
91
|
+
await rm(file, { force: true });
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
// 已经被人删掉了 / 读不到属性:都不是错误
|
|
95
|
+
}
|
|
96
|
+
}));
|
|
76
97
|
}
|
|
77
98
|
export function appendJobLog(log, level, message, limit = 200) {
|
|
78
99
|
log.push({ at: Date.now(), level, message });
|
package/lib/links.js
CHANGED
|
@@ -28,8 +28,8 @@
|
|
|
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", "tile"];
|
|
31
|
+
/** 六个模块的 key。 */
|
|
32
|
+
export const STUDIO_MODULES = ["sprite", "image", "sequence", "rig", "tile", "map"];
|
|
33
33
|
/** 八方向图的四个阶段。 */
|
|
34
34
|
export const SPRITE_STAGES = ["images", "videos", "frames", "sheet"];
|
|
35
35
|
/**
|
|
@@ -40,6 +40,15 @@ export const SPRITE_STAGES = ["images", "videos", "frames", "sheet"];
|
|
|
40
40
|
* 老的 `stage=template` 深链接会被忽略(`TILE_STAGES.includes` 不通过)。
|
|
41
41
|
*/
|
|
42
42
|
export const TILE_STAGES = ["generate", "review", "map", "export"];
|
|
43
|
+
/**
|
|
44
|
+
* 地图编辑器(模块六)的四个阶段。
|
|
45
|
+
*
|
|
46
|
+
* 与模块五(AI 生成地块)不同,这条链路**全本地**:导入素材 → 自动连接 → 画地图 →
|
|
47
|
+
* 导出。所以阶段名是素材 / 自动连接 / 地图 / 导出;`rules`(自动连接)没有可配的
|
|
48
|
+
* 东西、由宿主自动完成,界面里不显示它,但 key 保留 —— 对话面的
|
|
49
|
+
* `stage=assets|rules|paint|export` 契约不能变。
|
|
50
|
+
*/
|
|
51
|
+
export const MAP_STAGES = ["assets", "rules", "paint", "export"];
|
|
43
52
|
let clientOrigin = "";
|
|
44
53
|
/** 浏览器半区上报真实 origin。只接受 http(s),其它一律忽略。 */
|
|
45
54
|
export function rememberClientOrigin(origin) {
|
package/lib/mapauto.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 地图编辑器(模块六)的**自动连接**(autotile)内核:纯函数。
|
|
3
|
+
*
|
|
4
|
+
* 只剩两套:
|
|
5
|
+
* · `corner16` 四邻域整格连接(N/E/S/W),4 位掩码 → 16 张
|
|
6
|
+
* · `a2quad` A2 拼块:一格的四个象限各自挑形状号,照 AsLib `map20n_Number`
|
|
7
|
+
*
|
|
8
|
+
* 旧的 `blob47` / 族优先级 / `edgeMode` 全部删除:新模块里「同一类」由调用方
|
|
9
|
+
* 一句话回答(`NeighborAt`),宿主选块时只看「这一格是不是同一张图集」,
|
|
10
|
+
* 于是「优先级」这种约定俗成的隐式规则不再有存在的地方。
|
|
11
|
+
*
|
|
12
|
+
* ## 谁负责 wrap
|
|
13
|
+
*
|
|
14
|
+
* `NeighborAt` 的坐标可能越界(`r = -1`、`c = cols`)。**取模由调用方做** ——
|
|
15
|
+
* 这里对每个方向都发一次问,问的是「那一格是不是同一类」,`at` 自己决定越界怎么答。
|
|
16
|
+
* 在宿主里就是 `wrapCell(...)` 之后查图层数据;忘了 wrap 的表现是「接缝处形状不对」,
|
|
17
|
+
* 只在贴边时才看得出来。
|
|
18
|
+
*
|
|
19
|
+
* ## A2 为什么是「四个象限各一个形状号」
|
|
20
|
+
*
|
|
21
|
+
* AsLib 的 `drawMapBranch`(type_20n)对一格发 4 次 `drawChip`:TL / TR / BL / BR,
|
|
22
|
+
* 每个象限只看它**自己那一边的横竖邻居 + 那个角**:
|
|
23
|
+
* TL 用 left / up / upLeft,TR 用 right / up / upRight,
|
|
24
|
+
* BL 用 left / down / downLeft,BR 用 right / down / downRight。
|
|
25
|
+
* 所以「格子该长什么样」不是一个 0..4 的数,而是四个 0..4 的数 —— 这就是
|
|
26
|
+
* `a2QuadrantShapes` 返回元组的原因。
|
|
27
|
+
*/
|
|
28
|
+
/** 四邻域位值(与 `corner16` 的掩码一致):N=1, E=2, S=4, W=8。 */
|
|
29
|
+
export const NB4_N = 1;
|
|
30
|
+
export const NB4_E = 2;
|
|
31
|
+
export const NB4_S = 4;
|
|
32
|
+
export const NB4_W = 8;
|
|
33
|
+
/**
|
|
34
|
+
* 整格自动连接(16 掩码):四邻域位序 N=1,E=2,S=4,W=8,返回 0..15。
|
|
35
|
+
*
|
|
36
|
+
* 位序是**契约**(掩码是导入时一次性算好、写进图块元数据的语义值),
|
|
37
|
+
* 顺手改成别的顺序只会让所有已存的图块错位,不会报错。
|
|
38
|
+
*/
|
|
39
|
+
export function corner16Mask(at, r, c) {
|
|
40
|
+
let mask = 0;
|
|
41
|
+
if (at(r - 1, c))
|
|
42
|
+
mask |= NB4_N;
|
|
43
|
+
if (at(r, c + 1))
|
|
44
|
+
mask |= NB4_E;
|
|
45
|
+
if (at(r + 1, c))
|
|
46
|
+
mask |= NB4_S;
|
|
47
|
+
if (at(r, c - 1))
|
|
48
|
+
mask |= NB4_W;
|
|
49
|
+
return mask;
|
|
50
|
+
}
|
|
51
|
+
/** `corner16` 的全集(掩码值就是下标,0..15)。 */
|
|
52
|
+
export const CORNER16_MASKS = Array.from({ length: 16 }, (_unused, index) => index);
|
|
53
|
+
/** A2 的形状数:0..4(块行号)。 */
|
|
54
|
+
export const A2_SHAPES = 5;
|
|
55
|
+
/**
|
|
56
|
+
* A2 形状号,照 AsLib `map20n_Number(left, up, diag)`:
|
|
57
|
+
*
|
|
58
|
+
* | left | up | diag | 形状 |
|
|
59
|
+
* |---|---|---|---|
|
|
60
|
+
* | 否 | 否 | * | 0 |
|
|
61
|
+
* | 否 | 是 | * | 1 |
|
|
62
|
+
* | 是 | 否 | * | 2 |
|
|
63
|
+
* | 是 | 是 | 否 | 3 |
|
|
64
|
+
* | 是 | 是 | 是 | 4 |
|
|
65
|
+
*
|
|
66
|
+
* `diag` 只在两条边都接上时才有意义(否则那个角在网格上根本不相邻)——
|
|
67
|
+
* 写成「两边接上就点亮角」的话形状 3 会永远消失,`A2_SHAPES` 也就退化成 4 种。
|
|
68
|
+
*/
|
|
69
|
+
export function a2Shape(left, up, diag) {
|
|
70
|
+
if (left) {
|
|
71
|
+
if (up)
|
|
72
|
+
return diag ? 4 : 3;
|
|
73
|
+
return 2;
|
|
74
|
+
}
|
|
75
|
+
if (up)
|
|
76
|
+
return 1;
|
|
77
|
+
return 0;
|
|
78
|
+
}
|
|
79
|
+
/** 一格四个象限各自的形状号(TL, TR, BL, BR),元组顺序与 `A2_QUADRANT_ORDER` 一致。 */
|
|
80
|
+
export function a2QuadrantShapes(at, r, c) {
|
|
81
|
+
const left = at(r, c - 1);
|
|
82
|
+
const right = at(r, c + 1);
|
|
83
|
+
const up = at(r - 1, c);
|
|
84
|
+
const down = at(r + 1, c);
|
|
85
|
+
return [
|
|
86
|
+
a2Shape(left, up, at(r - 1, c - 1)), // TL
|
|
87
|
+
a2Shape(right, up, at(r - 1, c + 1)), // TR
|
|
88
|
+
a2Shape(left, down, at(r + 1, c - 1)), // BL
|
|
89
|
+
a2Shape(right, down, at(r + 1, c + 1)) // BR
|
|
90
|
+
];
|
|
91
|
+
}
|
|
92
|
+
/** 象限名的顺序(画验收图 / 调试用),与 `a2QuadrantShapes` 的元组一一对应。 */
|
|
93
|
+
export const A2_QUADRANT_ORDER = ["TL", "TR", "BL", "BR"];
|
|
94
|
+
/** 掩码的位数(算 Hamming 距离用)。 */
|
|
95
|
+
function popcount(value) {
|
|
96
|
+
let count = 0;
|
|
97
|
+
let rest = value >>> 0;
|
|
98
|
+
while (rest !== 0) {
|
|
99
|
+
rest &= rest - 1;
|
|
100
|
+
count++;
|
|
101
|
+
}
|
|
102
|
+
return count;
|
|
103
|
+
}
|
|
104
|
+
export function hammingDistance(a, b) {
|
|
105
|
+
return popcount((a ^ b) >>> 0);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* 缺掩码回退:Hamming 最近的已配置掩码,**并列取掩码值最小的**;一个都没配
|
|
109
|
+
* 返回 `undefined`(调用方再退「图集的第一块」)。
|
|
110
|
+
*
|
|
111
|
+
* 为什么必须回退而不是留空:留空在屏幕上就是一个透明洞,而且不报任何错
|
|
112
|
+
* (旧模块的「白色洞」就是这么来的)。并列取最小是为了确定性 —— 同样的地图
|
|
113
|
+
* 每次算出来必须是同一块,否则「导出和预览不一样」这类问题永远查不清。
|
|
114
|
+
*/
|
|
115
|
+
export function nearestMask(mask, configured) {
|
|
116
|
+
let best;
|
|
117
|
+
let bestDistance = Number.POSITIVE_INFINITY;
|
|
118
|
+
for (const candidate of configured) {
|
|
119
|
+
const distance = hammingDistance(mask, candidate);
|
|
120
|
+
if (distance < bestDistance || (distance === bestDistance && best !== undefined && candidate < best)) {
|
|
121
|
+
bestDistance = distance;
|
|
122
|
+
best = candidate;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return best;
|
|
126
|
+
}
|