dsh-game-material-master 0.10.2 → 0.11.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 +25 -3
- package/README.md +21 -3
- package/lib/client.js +291 -109
- package/lib/imagedraw.js +185 -0
- package/lib/index.js +80 -7
- package/lib/isodoc.js +153 -4
- package/lib/isoexport.js +14 -2
- package/lib/isogen.js +406 -19
- package/lib/isorender.js +215 -0
- package/lib/links.js +1 -1
- package/lib/mapdoc.js +162 -3
- package/lib/mapexport.js +14 -2
- package/lib/mapgen.js +421 -22
- package/lib/maprender.js +169 -0
- package/lib/maptiles.js +7 -2
- package/lib/toolguides.js +53 -8
- package/lib/tools.js +35 -7
- package/lib/wire.js +90 -8
- package/package.json +1 -1
package/lib/isogen.js
CHANGED
|
@@ -6,21 +6,21 @@
|
|
|
6
6
|
* 编辑期间文档以内存为权威副本、防抖落盘;导出前强制 flush。
|
|
7
7
|
*/
|
|
8
8
|
import { mkdir, readdir, rm, stat, writeFile } from "node:fs/promises";
|
|
9
|
-
import { randomUUID } from "node:crypto";
|
|
9
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
10
10
|
import { join } from "node:path";
|
|
11
11
|
import { appendJobLog, messageOf, readJson, sweepTempFiles, writeJsonAtomic } from "./jsonio.js";
|
|
12
12
|
import { dataRoot } from "./config.js";
|
|
13
13
|
import { decodeFile, encodeBitmap, pngSize, probeImageSize, sniffImageExt } from "./bitmap.js";
|
|
14
14
|
import { keyGreen } from "./chroma.js";
|
|
15
15
|
import { MAX_ASSET_SCALE, cropTile, flatTiles, keyStampBackground, parseAssetFps, parseAssetLoop, parseClarity, resampleToSize, safeAssetStem, STAMP_KEY_OPTIONS } from "./maptiles.js";
|
|
16
|
-
import { DEFAULT_ISO_CELL, MAX_ISO_DIM, MAX_ISO_PLAN_CHUNKS, assertIsoCell, assertIsoMapSize, chunkKey, chunksInRange, dirtyChunksFor, layoutOf, normalizeIsoCell, viewCellRange } from "./isogeom.js";
|
|
17
|
-
import { addLayer as docAddLayer, applyIsoOps as applyOpsToDoc, clearLayer as docClearLayer, cloneIsoDoc, createIsoDoc, filledCount, moveLayer as docMoveLayer, normalizeIsoDoc, objectCount, removeLayer as docRemoveLayer, renameLayer as docRenameLayer, resizeDoc } from "./isodoc.js";
|
|
16
|
+
import { DEFAULT_ISO_CELL, MAX_ISO_DIM, MAX_ISO_PLAN_CHUNKS, assertIsoCell, assertIsoMapSize, chunkKey, chunksInRange, dirtyChunksFor, layoutOf, normalizeIsoCell, viewCellRange, wrapCell } from "./isogeom.js";
|
|
17
|
+
import { addLayer as docAddLayer, applyIsoOps as applyOpsToDoc, clearLayer as docClearLayer, cellIndex, cloneIsoDoc, createIsoDoc, filledCount, moveLayer as docMoveLayer, normalizeIsoDoc, objectCount, removeLayer as docRemoveLayer, renameLayer as docRenameLayer, resizeDoc } from "./isodoc.js";
|
|
18
18
|
import { briefOf, parseIsoAnchor, parseIsoObjectScale, parseIsoTilesetKind, parsePassCells, rebuildIsoTiles, shapeOfKind } from "./isotiles.js";
|
|
19
|
-
import { buildIsoPlan, isoBitmapToPng, isoRenderContext, passCellsOf, planBounds, rasterizeIso } from "./isorender.js";
|
|
19
|
+
import { buildIsoPlan, isoBitmapToPng, isoRenderContext, passCellsOf, planBounds, rasterizeIso, renderIsoView } from "./isorender.js";
|
|
20
20
|
import { exportIsoMap } from "./isoexport.js";
|
|
21
21
|
import { renderIsoSamples } from "./isosamplegen.js";
|
|
22
22
|
import { composeIsoFrames } from "./isocompose.js";
|
|
23
|
-
import { assetBasePath } from "./links.js";
|
|
23
|
+
import { assetBasePath, originForLinks } from "./links.js";
|
|
24
24
|
export const ISO_STAGES = ["assets", "paint", "export"];
|
|
25
25
|
// ── 路径与 id ─────────────────────────────────────────────────────────────
|
|
26
26
|
export function isoJobsRoot() {
|
|
@@ -662,8 +662,11 @@ export async function composeIsoAsset(input) {
|
|
|
662
662
|
await patchIsoProject(project.id, (fresh) => {
|
|
663
663
|
appendJobLog(fresh.logs, "info", `整理 AI 素材:${name}(${input.kind} / ${composed.frameCount} 帧 → ${composed.bitmap.width}×${composed.bitmap.height}${composed.scale > 1 ? `,每格 ${project.cell.width * composed.scale}px(${composed.scale} 倍)` : ""},落在 ${file},等你在「新增素材」里确认)`);
|
|
664
664
|
});
|
|
665
|
+
const localPath = isoAssetPath(project.id, file);
|
|
665
666
|
return {
|
|
666
667
|
file,
|
|
668
|
+
localPath,
|
|
669
|
+
previewPath: localPath,
|
|
667
670
|
name,
|
|
668
671
|
kind: input.kind,
|
|
669
672
|
cell: project.cell,
|
|
@@ -871,6 +874,49 @@ export async function tilesetRevisions(projectId) {
|
|
|
871
874
|
const project = await readProjectOrThrow(projectId);
|
|
872
875
|
return new Map(await Promise.all(project.tilesets.map(async (tileset) => [tileset.id, await fileRevision(join(isoProjectDir(projectId), tileset.file))])));
|
|
873
876
|
}
|
|
877
|
+
/**
|
|
878
|
+
* 文档 + 素材元数据 + 素材图 mtime 的短哈希(与 `mapgen.mapRevision` 同口径)。
|
|
879
|
+
* 不落盘;同一状态稳定。
|
|
880
|
+
*/
|
|
881
|
+
export async function isoRevision(project, doc, revisions) {
|
|
882
|
+
const revs = revisions ?? (await tilesetRevisions(project.id));
|
|
883
|
+
const payload = JSON.stringify({
|
|
884
|
+
cell: project.cell,
|
|
885
|
+
wrap: project.wrap,
|
|
886
|
+
doc: {
|
|
887
|
+
cols: doc.cols,
|
|
888
|
+
rows: doc.rows,
|
|
889
|
+
layers: doc.layers,
|
|
890
|
+
data: doc.data,
|
|
891
|
+
objects: doc.objects,
|
|
892
|
+
markers: doc.markers
|
|
893
|
+
},
|
|
894
|
+
tilesets: project.tilesets.map((tileset) => ({
|
|
895
|
+
id: tileset.id,
|
|
896
|
+
name: tileset.name,
|
|
897
|
+
file: tileset.file,
|
|
898
|
+
imageWidth: tileset.imageWidth,
|
|
899
|
+
imageHeight: tileset.imageHeight,
|
|
900
|
+
cell: tileset.cell,
|
|
901
|
+
kind: tileset.kind,
|
|
902
|
+
columns: tileset.columns,
|
|
903
|
+
frames: tileset.frames,
|
|
904
|
+
variants: tileset.variants,
|
|
905
|
+
animRows: tileset.animRows,
|
|
906
|
+
footprint: tileset.footprint,
|
|
907
|
+
anchor: tileset.anchor,
|
|
908
|
+
objectScale: tileset.objectScale,
|
|
909
|
+
pass: tileset.pass,
|
|
910
|
+
loop: tileset.loop,
|
|
911
|
+
clarity: tileset.clarity,
|
|
912
|
+
fps: tileset.fps,
|
|
913
|
+
builtin: tileset.builtin === true,
|
|
914
|
+
tileCount: tileset.tiles.length
|
|
915
|
+
})),
|
|
916
|
+
files: project.tilesets.map((tileset) => [tileset.id, revs.get(tileset.id) ?? 0])
|
|
917
|
+
});
|
|
918
|
+
return createHash("sha1").update(payload).digest("hex").slice(0, 12);
|
|
919
|
+
}
|
|
874
920
|
export async function isoView(project) {
|
|
875
921
|
const doc = await theDocOrFresh(project.id);
|
|
876
922
|
const layout = layoutOf(project.cell, doc.cols, doc.rows, project.wrap);
|
|
@@ -879,6 +925,7 @@ export async function isoView(project) {
|
|
|
879
925
|
const state = historyState(project.id);
|
|
880
926
|
const layers = doc.layers.map((layer) => ({ ...layer, filled: filledCount(doc, layer.id) }));
|
|
881
927
|
const listing = await readExportListingRelative(project);
|
|
928
|
+
const preview = (await readPreviewFiles(project.id)).map((name) => `preview/${name}`);
|
|
882
929
|
return {
|
|
883
930
|
id: project.id,
|
|
884
931
|
name: project.name,
|
|
@@ -887,6 +934,7 @@ export async function isoView(project) {
|
|
|
887
934
|
wrap: project.wrap,
|
|
888
935
|
cols: doc.cols,
|
|
889
936
|
rows: doc.rows,
|
|
937
|
+
revision: await isoRevision(project, doc, revisions),
|
|
890
938
|
layout: { stepX: layout.stepX, stepY: layout.stepY, diamondHeight: layout.diamondHeight },
|
|
891
939
|
bounds: planBounds(isoRenderContext(project, doc, project.tilesets), doc),
|
|
892
940
|
tilesets: project.tilesets.map((tileset) => briefOf(tileset, revisions.get(tileset.id), project.tilesets)),
|
|
@@ -895,7 +943,17 @@ export async function isoView(project) {
|
|
|
895
943
|
return { index: flat.index, tilesetId: flat.tilesetId, rect: flat.tile.rect, kind: (tileset?.kind ?? "ground") };
|
|
896
944
|
}),
|
|
897
945
|
layers,
|
|
898
|
-
markers: doc.markers.map((marker, index) => ({
|
|
946
|
+
markers: doc.markers.map((marker, index) => ({
|
|
947
|
+
id: marker.id,
|
|
948
|
+
n: index + 1,
|
|
949
|
+
r: marker.r,
|
|
950
|
+
c: marker.c,
|
|
951
|
+
name: marker.name,
|
|
952
|
+
...(marker.kind === undefined ? {} : { kind: marker.kind }),
|
|
953
|
+
...(marker.color === undefined ? {} : { color: marker.color }),
|
|
954
|
+
...(marker.note === undefined ? {} : { note: marker.note }),
|
|
955
|
+
...(marker.tags === undefined ? {} : { tags: marker.tags })
|
|
956
|
+
})),
|
|
899
957
|
filled: filledCount(doc),
|
|
900
958
|
objects: objectCount(doc),
|
|
901
959
|
objectList: doc.objects.map((object) => {
|
|
@@ -910,12 +968,229 @@ export async function isoView(project) {
|
|
|
910
968
|
maxChunks: MAX_ISO_PLAN_CHUNKS,
|
|
911
969
|
warnings: warningsFor(project, doc),
|
|
912
970
|
logs: project.logs.slice(-40),
|
|
913
|
-
preview
|
|
971
|
+
preview,
|
|
972
|
+
previewPaths: preview.map((relative) => join(isoProjectDir(project.id), relative)),
|
|
914
973
|
exportBase: listing?.base ?? "",
|
|
915
974
|
exportFiles: listing?.files ?? [],
|
|
916
975
|
exportPath: listing === undefined ? "" : join(isoProjectDir(project.id), "export", isoExportDirName(doc, project))
|
|
917
976
|
};
|
|
918
977
|
}
|
|
978
|
+
const ISO_INSPECT_SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
979
|
+
function isoInspectInt(value, fallback, min, max) {
|
|
980
|
+
const n = Math.round(Number(value));
|
|
981
|
+
if (!Number.isFinite(n))
|
|
982
|
+
return fallback;
|
|
983
|
+
return Math.max(min, Math.min(max, n));
|
|
984
|
+
}
|
|
985
|
+
function isoInspectRegion(value, doc) {
|
|
986
|
+
const raw = (value ?? {});
|
|
987
|
+
const r0 = isoInspectInt(raw.r0, 0, -MAX_ISO_DIM, MAX_ISO_DIM);
|
|
988
|
+
const c0 = isoInspectInt(raw.c0, 0, -MAX_ISO_DIM, MAX_ISO_DIM);
|
|
989
|
+
const r1 = isoInspectInt(raw.r1, doc.rows, -MAX_ISO_DIM, MAX_ISO_DIM);
|
|
990
|
+
const c1 = isoInspectInt(raw.c1, doc.cols, -MAX_ISO_DIM, MAX_ISO_DIM);
|
|
991
|
+
if (r1 <= r0 || c1 <= c0)
|
|
992
|
+
throw new Error("region 必须是正矩形(r1 > r0、c1 > c0)。");
|
|
993
|
+
return { r0, c0, r1, c1 };
|
|
994
|
+
}
|
|
995
|
+
function isoInspectNextRange(region, limit) {
|
|
996
|
+
const width = Math.max(1, Math.min(region.c1 - region.c0, limit));
|
|
997
|
+
const height = Math.max(1, Math.min(region.r1 - region.r0, Math.ceil(limit / width)));
|
|
998
|
+
return { r0: region.r0, c0: region.c0, r1: region.r0 + height, c1: region.c0 + width };
|
|
999
|
+
}
|
|
1000
|
+
function isoObjectInRegion(object, footprint, region) {
|
|
1001
|
+
for (let dr = 0; dr < footprint.rows; dr++) {
|
|
1002
|
+
for (let dc = 0; dc < footprint.cols; dc++) {
|
|
1003
|
+
const r = object.r + dr;
|
|
1004
|
+
const c = object.c + dc;
|
|
1005
|
+
if (r >= region.r0 && r < region.r1 && c >= region.c0 && c < region.c1)
|
|
1006
|
+
return true;
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
return false;
|
|
1010
|
+
}
|
|
1011
|
+
/**
|
|
1012
|
+
* 读一块 45° 地图的语义状态:地面网格、物件、标记、通行性、统计。
|
|
1013
|
+
* 与 `isoPlan` 分开:这里没有像素坐标 / chunk / visibleKeys。
|
|
1014
|
+
*/
|
|
1015
|
+
export async function isoInspect(projectId, options = {}) {
|
|
1016
|
+
const project = await readProjectOrThrow(projectId);
|
|
1017
|
+
const doc = await theDocOrFresh(projectId);
|
|
1018
|
+
const layout = layoutOf(project.cell, doc.cols, doc.rows, project.wrap);
|
|
1019
|
+
const region = isoInspectRegion(options.region, doc);
|
|
1020
|
+
const maxCells = isoInspectInt(options.maxCells, 4096, 1, 65_536);
|
|
1021
|
+
const maxItems = isoInspectInt(options.maxItems, 200, 1, 2_000);
|
|
1022
|
+
const format = options.format === "text" || options.format === "json" ? options.format : "both";
|
|
1023
|
+
const include = new Set(options.include ?? ["layers", "markers", "objects", "pass", "stats", "legend"]);
|
|
1024
|
+
const layerFilter = options.layerIds === undefined ? undefined : new Set(options.layerIds.map(String));
|
|
1025
|
+
const cells = (region.r1 - region.r0) * (region.c1 - region.c0);
|
|
1026
|
+
const truncated = cells > maxCells;
|
|
1027
|
+
const nextRange = truncated ? isoInspectNextRange(region, maxCells) : null;
|
|
1028
|
+
const flat = flatTiles(project.tilesets);
|
|
1029
|
+
const byIndex = new Map(flat.map((entry) => [entry.index, entry]));
|
|
1030
|
+
const used = new Map();
|
|
1031
|
+
const symbolOf = new Map();
|
|
1032
|
+
const symbolFor = (index) => {
|
|
1033
|
+
const existing = symbolOf.get(index);
|
|
1034
|
+
if (existing !== undefined)
|
|
1035
|
+
return existing;
|
|
1036
|
+
const symbol = symbolOf.size < ISO_INSPECT_SYMBOLS.length ? ISO_INSPECT_SYMBOLS[symbolOf.size] : "?";
|
|
1037
|
+
symbolOf.set(index, symbol);
|
|
1038
|
+
return symbol;
|
|
1039
|
+
};
|
|
1040
|
+
const layers = [];
|
|
1041
|
+
const rowsByLayer = new Map();
|
|
1042
|
+
const histogram = new Map();
|
|
1043
|
+
let filled = 0;
|
|
1044
|
+
const wantRows = !truncated && format !== "json";
|
|
1045
|
+
for (const layer of doc.layers) {
|
|
1046
|
+
if (layerFilter !== undefined && !layerFilter.has(layer.id))
|
|
1047
|
+
continue;
|
|
1048
|
+
if (layer.kind === "object") {
|
|
1049
|
+
const count = objectCount(doc, layer.id);
|
|
1050
|
+
layers.push({ id: layer.id, name: layer.name, kind: layer.kind, filled: count });
|
|
1051
|
+
rowsByLayer.set(layer.id, []);
|
|
1052
|
+
continue;
|
|
1053
|
+
}
|
|
1054
|
+
const data = doc.data[layer.id] ?? [];
|
|
1055
|
+
const rows = [];
|
|
1056
|
+
let layerFilled = 0;
|
|
1057
|
+
for (let r = region.r0; r < region.r1; r++) {
|
|
1058
|
+
let row = "";
|
|
1059
|
+
for (let c = region.c0; c < region.c1; c++) {
|
|
1060
|
+
const wrapped = wrapCell(layout, r, c);
|
|
1061
|
+
const outside = wrapped.r < 0 || wrapped.c < 0 || wrapped.r >= doc.rows || wrapped.c >= doc.cols;
|
|
1062
|
+
const value = outside ? 0 : data[cellIndex(doc, wrapped.r, wrapped.c)] ?? 0;
|
|
1063
|
+
if (value > 0) {
|
|
1064
|
+
layerFilled++;
|
|
1065
|
+
filled++;
|
|
1066
|
+
used.set(value, (used.get(value) ?? 0) + 1);
|
|
1067
|
+
histogram.set(value, (histogram.get(value) ?? 0) + 1);
|
|
1068
|
+
}
|
|
1069
|
+
if (!wantRows)
|
|
1070
|
+
continue;
|
|
1071
|
+
if (outside)
|
|
1072
|
+
row += "#";
|
|
1073
|
+
else if (value <= 0)
|
|
1074
|
+
row += "·";
|
|
1075
|
+
else
|
|
1076
|
+
row += symbolFor(value);
|
|
1077
|
+
}
|
|
1078
|
+
if (wantRows)
|
|
1079
|
+
rows.push(row);
|
|
1080
|
+
}
|
|
1081
|
+
rowsByLayer.set(layer.id, rows);
|
|
1082
|
+
layers.push({ id: layer.id, name: layer.name, kind: layer.kind, filled: layerFilled, ...(format === "text" || truncated ? {} : { rows }) });
|
|
1083
|
+
}
|
|
1084
|
+
const legend = [...used.keys()]
|
|
1085
|
+
.sort((a, b) => a - b)
|
|
1086
|
+
.slice(0, ISO_INSPECT_SYMBOLS.length)
|
|
1087
|
+
.map((index) => {
|
|
1088
|
+
const entry = byIndex.get(index);
|
|
1089
|
+
const tileset = entry === undefined ? undefined : project.tilesets.find((item) => item.id === entry.tilesetId);
|
|
1090
|
+
return {
|
|
1091
|
+
symbol: symbolFor(index),
|
|
1092
|
+
index,
|
|
1093
|
+
...(entry === undefined
|
|
1094
|
+
? {}
|
|
1095
|
+
: {
|
|
1096
|
+
tilesetId: entry.tilesetId,
|
|
1097
|
+
tilesetName: tileset?.name ?? entry.tilesetId,
|
|
1098
|
+
tileId: entry.tile.id,
|
|
1099
|
+
rect: entry.tile.rect,
|
|
1100
|
+
kind: tileset?.kind,
|
|
1101
|
+
mask: entry.tile.mask,
|
|
1102
|
+
variant: entry.tile.variant,
|
|
1103
|
+
frame: entry.tile.frame
|
|
1104
|
+
})
|
|
1105
|
+
};
|
|
1106
|
+
});
|
|
1107
|
+
const markersInRegion = doc.markers.filter((marker) => marker.r >= region.r0 && marker.r < region.r1 && marker.c >= region.c0 && marker.c < region.c1);
|
|
1108
|
+
const markers = markersInRegion.slice(0, maxItems).map((marker) => ({
|
|
1109
|
+
id: marker.id,
|
|
1110
|
+
n: doc.markers.indexOf(marker) + 1,
|
|
1111
|
+
r: marker.r,
|
|
1112
|
+
c: marker.c,
|
|
1113
|
+
name: marker.name,
|
|
1114
|
+
kind: marker.kind ?? "custom",
|
|
1115
|
+
color: marker.color ?? null,
|
|
1116
|
+
note: marker.note ?? "",
|
|
1117
|
+
tags: marker.tags ?? []
|
|
1118
|
+
}));
|
|
1119
|
+
const objectsInRegion = doc.objects.filter((object) => {
|
|
1120
|
+
if (layerFilter !== undefined && !layerFilter.has(object.layerId))
|
|
1121
|
+
return false;
|
|
1122
|
+
const tileset = project.tilesets.find((entry) => entry.id === object.tilesetId);
|
|
1123
|
+
return isoObjectInRegion(object, tileset?.footprint ?? { cols: 1, rows: 1 }, region);
|
|
1124
|
+
});
|
|
1125
|
+
const objects = objectsInRegion.slice(0, maxItems).map((object) => {
|
|
1126
|
+
const tileset = project.tilesets.find((entry) => entry.id === object.tilesetId);
|
|
1127
|
+
const footprint = tileset?.footprint ?? { cols: 1, rows: 1 };
|
|
1128
|
+
return {
|
|
1129
|
+
id: object.id,
|
|
1130
|
+
layerId: object.layerId,
|
|
1131
|
+
tilesetId: object.tilesetId,
|
|
1132
|
+
name: tileset?.name ?? object.tilesetId,
|
|
1133
|
+
r: object.r,
|
|
1134
|
+
c: object.c,
|
|
1135
|
+
cols: footprint.cols,
|
|
1136
|
+
rows: footprint.rows,
|
|
1137
|
+
pass: tileset?.pass ?? []
|
|
1138
|
+
};
|
|
1139
|
+
});
|
|
1140
|
+
const pass = passCellsOf(isoRenderContext(project, doc, project.tilesets), doc).filter((cell) => cell.r >= region.r0 && cell.r < region.r1 && cell.c >= region.c0 && cell.c < region.c1);
|
|
1141
|
+
const markersByKind = new Map();
|
|
1142
|
+
for (const marker of markersInRegion) {
|
|
1143
|
+
const kind = typeof marker.kind === "string" && marker.kind !== "" ? marker.kind : "custom";
|
|
1144
|
+
markersByKind.set(kind, (markersByKind.get(kind) ?? 0) + 1);
|
|
1145
|
+
}
|
|
1146
|
+
const stats = {
|
|
1147
|
+
cells,
|
|
1148
|
+
filled,
|
|
1149
|
+
objects: objectsInRegion.length,
|
|
1150
|
+
markers: markersInRegion.length,
|
|
1151
|
+
tileHistogram: [...histogram.entries()].sort((a, b) => a[0] - b[0]).map(([index, count]) => ({ index, count })),
|
|
1152
|
+
markersByKind: [...markersByKind.entries()].map(([kind, count]) => ({ kind, count }))
|
|
1153
|
+
};
|
|
1154
|
+
const text = format === "json"
|
|
1155
|
+
? undefined
|
|
1156
|
+
: [
|
|
1157
|
+
`45° 地图 ${doc.cols}×${doc.rows} · 区域 (${region.r0},${region.c0})–(${region.r1 - 1},${region.c1 - 1})`,
|
|
1158
|
+
...layers.map((layer) => `${String(layer.name)}\n${layer.kind === "object"
|
|
1159
|
+
? "(物件层,见 objects)"
|
|
1160
|
+
: (rowsByLayer.get(String(layer.id)) ?? []).join("\n") || "(内容过大,已省略逐格文本)"}`),
|
|
1161
|
+
`图例:${legend.map((entry) => `${String(entry.symbol)}=${String(entry.tilesetName ?? entry.tilesetId ?? "未知")}/${String(entry.index)}`).join(" ")}`
|
|
1162
|
+
].join("\n\n");
|
|
1163
|
+
return {
|
|
1164
|
+
module: "iso",
|
|
1165
|
+
projectId,
|
|
1166
|
+
revision: await isoRevision(project, doc),
|
|
1167
|
+
cols: doc.cols,
|
|
1168
|
+
rows: doc.rows,
|
|
1169
|
+
cell: project.cell,
|
|
1170
|
+
wrap: project.wrap,
|
|
1171
|
+
range: region,
|
|
1172
|
+
truncated,
|
|
1173
|
+
nextRange,
|
|
1174
|
+
truncatedItems: [
|
|
1175
|
+
...(markersInRegion.length > markers.length ? ["markers"] : []),
|
|
1176
|
+
...(objectsInRegion.length > objects.length ? ["objects"] : [])
|
|
1177
|
+
],
|
|
1178
|
+
legend: include.has("legend") ? legend : [],
|
|
1179
|
+
layers: include.has("layers") ? layers : [],
|
|
1180
|
+
markers: include.has("markers") ? markers : undefined,
|
|
1181
|
+
objects: include.has("objects") ? objects : undefined,
|
|
1182
|
+
pass: include.has("pass") ? pass : undefined,
|
|
1183
|
+
stats,
|
|
1184
|
+
warnings: [
|
|
1185
|
+
...warningsFor(project, doc),
|
|
1186
|
+
...(used.size > ISO_INSPECT_SYMBOLS.length ? [`不同图块超过 ${ISO_INSPECT_SYMBOLS.length} 种,文本网格用 ? 代替,精确值请用 format=json。`] : [])
|
|
1187
|
+
],
|
|
1188
|
+
busy: isoBusy(projectId),
|
|
1189
|
+
canUndo: historyState(projectId).canUndo,
|
|
1190
|
+
canRedo: historyState(projectId).canRedo,
|
|
1191
|
+
...(text === undefined ? {} : { text })
|
|
1192
|
+
};
|
|
1193
|
+
}
|
|
919
1194
|
function warningsFor(project, doc) {
|
|
920
1195
|
const out = [];
|
|
921
1196
|
if (project.tilesets.length === 0)
|
|
@@ -989,9 +1264,14 @@ export async function isoPlan(projectId, o) {
|
|
|
989
1264
|
export async function applyIsoOps(projectId, ops, options = {}) {
|
|
990
1265
|
const project = await readProjectOrThrow(projectId);
|
|
991
1266
|
const doc = await theDocOrFresh(projectId);
|
|
992
|
-
const
|
|
993
|
-
|
|
994
|
-
|
|
1267
|
+
const currentRevision = await isoRevision(project, doc);
|
|
1268
|
+
if (options.ifRevision !== undefined && options.ifRevision !== "" && options.ifRevision !== currentRevision) {
|
|
1269
|
+
throw new Error(`版本不一致:当前 ${currentRevision},请求 ${options.ifRevision};请重新 isoInspect 后再改。`);
|
|
1270
|
+
}
|
|
1271
|
+
const target = options.dryRun === true ? cloneIsoDoc(doc) : doc;
|
|
1272
|
+
const layout = layoutOf(project.cell, target.cols, target.rows, project.wrap);
|
|
1273
|
+
const result = applyDocOps(target, ops, layout, project);
|
|
1274
|
+
if (options.dryRun !== true && (result.changed > 0 || result.touched)) {
|
|
995
1275
|
pushHistory(projectId, { undo: result.undo, redo: result.redo, stroke: options.stroke }, doc);
|
|
996
1276
|
scheduleDocWrite(projectId, doc);
|
|
997
1277
|
}
|
|
@@ -999,14 +1279,30 @@ export async function applyIsoOps(projectId, ops, options = {}) {
|
|
|
999
1279
|
const have = new Set(options.have ?? []);
|
|
1000
1280
|
const dropKeys = dirty.filter((key) => have.has(key));
|
|
1001
1281
|
const state = historyState(projectId);
|
|
1002
|
-
return {
|
|
1282
|
+
return {
|
|
1283
|
+
changed: result.changed,
|
|
1284
|
+
skipped: result.skipped,
|
|
1285
|
+
dirtyChunks: dirty,
|
|
1286
|
+
dropKeys,
|
|
1287
|
+
canUndo: state.canUndo,
|
|
1288
|
+
canRedo: state.canRedo,
|
|
1289
|
+
touched: result.touched,
|
|
1290
|
+
revision: options.dryRun === true ? currentRevision : await isoRevision(project, doc),
|
|
1291
|
+
...(options.dryRun === true ? { dryRun: true } : {}),
|
|
1292
|
+
summary: {
|
|
1293
|
+
cells: result.changed,
|
|
1294
|
+
markers: ops.filter((op) => op?.kind === "marker").length,
|
|
1295
|
+
stamps: 0,
|
|
1296
|
+
objects: ops.filter((op) => op?.kind === "object").length
|
|
1297
|
+
}
|
|
1298
|
+
};
|
|
1003
1299
|
}
|
|
1004
1300
|
export async function isoUndo(projectId, have = []) {
|
|
1005
1301
|
const project = await readProjectOrThrow(projectId);
|
|
1006
1302
|
const doc = await theDocOrFresh(projectId);
|
|
1007
1303
|
const state = history.get(projectId);
|
|
1008
1304
|
if (state === undefined || state.index < 0) {
|
|
1009
|
-
return { changed: 0, skipped: [], dirtyChunks: [], dropKeys: [], canUndo: false, canRedo: false, touched: false };
|
|
1305
|
+
return { changed: 0, skipped: [], dirtyChunks: [], dropKeys: [], canUndo: false, canRedo: false, touched: false, revision: await isoRevision(project, doc) };
|
|
1010
1306
|
}
|
|
1011
1307
|
const entry = state.entries[state.index];
|
|
1012
1308
|
const layout = layoutOf(project.cell, doc.cols, doc.rows, project.wrap);
|
|
@@ -1030,7 +1326,8 @@ export async function isoUndo(projectId, have = []) {
|
|
|
1030
1326
|
dropKeys: dirtyChunks.filter((key) => haveSet.has(key)),
|
|
1031
1327
|
canUndo: state.index >= 0,
|
|
1032
1328
|
canRedo: state.index < state.entries.length - 1,
|
|
1033
|
-
touched: true
|
|
1329
|
+
touched: true,
|
|
1330
|
+
revision: await isoRevision(project, doc)
|
|
1034
1331
|
};
|
|
1035
1332
|
}
|
|
1036
1333
|
export async function isoRedo(projectId, have = []) {
|
|
@@ -1038,7 +1335,7 @@ export async function isoRedo(projectId, have = []) {
|
|
|
1038
1335
|
const doc = await theDocOrFresh(projectId);
|
|
1039
1336
|
const state = history.get(projectId);
|
|
1040
1337
|
if (state === undefined || state.index >= state.entries.length - 1) {
|
|
1041
|
-
return { changed: 0, skipped: [], dirtyChunks: [], dropKeys: [], canUndo: false, canRedo: false, touched: false };
|
|
1338
|
+
return { changed: 0, skipped: [], dirtyChunks: [], dropKeys: [], canUndo: false, canRedo: false, touched: false, revision: await isoRevision(project, doc) };
|
|
1042
1339
|
}
|
|
1043
1340
|
const entry = state.entries[state.index + 1];
|
|
1044
1341
|
const layout = layoutOf(project.cell, doc.cols, doc.rows, project.wrap);
|
|
@@ -1062,7 +1359,8 @@ export async function isoRedo(projectId, have = []) {
|
|
|
1062
1359
|
dropKeys: dirtyChunks.filter((key) => haveSet.has(key)),
|
|
1063
1360
|
canUndo: state.index >= 0,
|
|
1064
1361
|
canRedo: state.index < state.entries.length - 1,
|
|
1065
|
-
touched: true
|
|
1362
|
+
touched: true,
|
|
1363
|
+
revision: await isoRevision(project, doc)
|
|
1066
1364
|
};
|
|
1067
1365
|
}
|
|
1068
1366
|
export async function saveIsoDoc(projectId, patch) {
|
|
@@ -1118,7 +1416,14 @@ export async function saveIsoDoc(projectId, patch) {
|
|
|
1118
1416
|
scheduleDocWrite(projectId, doc);
|
|
1119
1417
|
await flushIsoDoc(projectId);
|
|
1120
1418
|
}
|
|
1121
|
-
return {
|
|
1419
|
+
return {
|
|
1420
|
+
doc: cloneIsoDoc(doc),
|
|
1421
|
+
dropped: 0,
|
|
1422
|
+
droppedMarkers: 0,
|
|
1423
|
+
droppedObjects: 0,
|
|
1424
|
+
warnings,
|
|
1425
|
+
revision: await isoRevision(await readProjectOrThrow(projectId), doc)
|
|
1426
|
+
};
|
|
1122
1427
|
}
|
|
1123
1428
|
export async function saveIsoProject(projectId, patch) {
|
|
1124
1429
|
const project = await patchIsoProject(projectId, (fresh) => {
|
|
@@ -1270,7 +1575,7 @@ export async function runIsoGenerate(projectId, options) {
|
|
|
1270
1575
|
}
|
|
1271
1576
|
}
|
|
1272
1577
|
const outcome = await applyIsoOps(projectId, [{ kind: "paint", layerId: layer.id, cells }], { stroke: `generate-${seed}` });
|
|
1273
|
-
return { changed: outcome.changed, seed, warnings: outcome.skipped };
|
|
1578
|
+
return { changed: outcome.changed, seed, warnings: outcome.skipped, revision: outcome.revision };
|
|
1274
1579
|
}
|
|
1275
1580
|
// ── 预览 / 导出 ─────────────────────────────────────────────────────────
|
|
1276
1581
|
const reserved = new Set();
|
|
@@ -1336,10 +1641,12 @@ export async function runIsoPreview(projectId) {
|
|
|
1336
1641
|
reserved.delete(projectId);
|
|
1337
1642
|
throw error;
|
|
1338
1643
|
}
|
|
1644
|
+
const previewPath = join(isoProjectDir(projectId), "preview", "map.png");
|
|
1339
1645
|
if (project.tilesets.length === 0) {
|
|
1340
1646
|
reserved.delete(projectId);
|
|
1341
|
-
return { started: false, reason: "还没有素材。" };
|
|
1647
|
+
return { started: false, reason: "还没有素材。", previewPath };
|
|
1342
1648
|
}
|
|
1649
|
+
const revisionAtStart = await isoRevision(project, await theDocOrFresh(projectId));
|
|
1343
1650
|
void background(projectId, "paint", async () => {
|
|
1344
1651
|
const fresh = await readProjectOrThrow(projectId);
|
|
1345
1652
|
const doc = await theDocOrFresh(projectId);
|
|
@@ -1359,7 +1666,87 @@ export async function runIsoPreview(projectId) {
|
|
|
1359
1666
|
appendJobLog(current.logs, "warn", `预览告警:${warning}`);
|
|
1360
1667
|
});
|
|
1361
1668
|
});
|
|
1362
|
-
return { started: true };
|
|
1669
|
+
return { started: true, previewPath, revisionAtStart };
|
|
1670
|
+
}
|
|
1671
|
+
async function pruneAgentRenders(projectId, keep = 20) {
|
|
1672
|
+
const dir = join(isoProjectDir(projectId), "preview", "agent");
|
|
1673
|
+
let names;
|
|
1674
|
+
try {
|
|
1675
|
+
names = (await readdir(dir)).filter((name) => name.endsWith(".png"));
|
|
1676
|
+
}
|
|
1677
|
+
catch {
|
|
1678
|
+
return;
|
|
1679
|
+
}
|
|
1680
|
+
if (names.length <= keep)
|
|
1681
|
+
return;
|
|
1682
|
+
const entries = await Promise.all(names.map(async (name) => {
|
|
1683
|
+
try {
|
|
1684
|
+
return { name, mtime: (await stat(join(dir, name))).mtimeMs };
|
|
1685
|
+
}
|
|
1686
|
+
catch {
|
|
1687
|
+
return { name, mtime: 0 };
|
|
1688
|
+
}
|
|
1689
|
+
}));
|
|
1690
|
+
entries.sort((a, b) => b.mtime - a.mtime);
|
|
1691
|
+
for (const entry of entries.slice(keep))
|
|
1692
|
+
await rm(join(dir, entry.name), { force: true }).catch(() => undefined);
|
|
1693
|
+
}
|
|
1694
|
+
/** 同步渲染一块 45° 地图区域;与导出共用 `buildIsoPlan` 和同一个 z 排序。 */
|
|
1695
|
+
export async function runIsoRender(projectId, options = {}) {
|
|
1696
|
+
const startedAt = Date.now();
|
|
1697
|
+
const project = await readProjectOrThrow(projectId);
|
|
1698
|
+
const doc = await theDocOrFresh(projectId);
|
|
1699
|
+
const revisionAtStart = await isoRevision(project, doc);
|
|
1700
|
+
const region = isoInspectRegion(options.region, doc);
|
|
1701
|
+
const layerIndexes = options.layerIds === undefined
|
|
1702
|
+
? undefined
|
|
1703
|
+
: options.layerIds
|
|
1704
|
+
.map((id) => doc.layers.findIndex((layer) => layer.id === id))
|
|
1705
|
+
.filter((index) => index >= 0);
|
|
1706
|
+
const context = isoRenderContext(project, doc, project.tilesets);
|
|
1707
|
+
const lookup = await decodeTilesetsForProject(project);
|
|
1708
|
+
const passCells = options.overlay?.pass === true ? passCellsOf(context, doc) : undefined;
|
|
1709
|
+
const rendered = renderIsoView(context, doc, lookup, {
|
|
1710
|
+
range: { r0: region.r0, c0: region.c0, r1: region.r1, c1: region.c1 },
|
|
1711
|
+
scale: options.scale,
|
|
1712
|
+
layers: layerIndexes,
|
|
1713
|
+
frame: options.frame,
|
|
1714
|
+
background: options.background,
|
|
1715
|
+
overlay: options.overlay,
|
|
1716
|
+
passCells,
|
|
1717
|
+
maxPixels: options.maxPixels
|
|
1718
|
+
});
|
|
1719
|
+
const dir = join(isoProjectDir(projectId), "preview", "agent");
|
|
1720
|
+
await mkdir(dir, { recursive: true });
|
|
1721
|
+
const safe = (options.name ?? "inspect").replace(/[^\w\u4e00-\u9fa5.-]+/g, "_").replace(/^_+|_+$/g, "").slice(0, 40) || "inspect";
|
|
1722
|
+
const optionsHash = createHash("sha1")
|
|
1723
|
+
.update(JSON.stringify({ region, scale: rendered.scale, layerIndexes, frame: options.frame ?? 0, background: options.background ?? "transparent", overlay: options.overlay ?? {} }))
|
|
1724
|
+
.digest("hex")
|
|
1725
|
+
.slice(0, 8);
|
|
1726
|
+
const file = `preview/agent/${safe}-${revisionAtStart}-${optionsHash}.png`;
|
|
1727
|
+
const localPath = join(isoProjectDir(projectId), file);
|
|
1728
|
+
await writeFile(localPath, isoBitmapToPng(rendered.bitmap));
|
|
1729
|
+
await pruneAgentRenders(projectId);
|
|
1730
|
+
const revision = await isoRevision(project, doc);
|
|
1731
|
+
return {
|
|
1732
|
+
module: "iso",
|
|
1733
|
+
projectId,
|
|
1734
|
+
revision,
|
|
1735
|
+
revisionAtStart,
|
|
1736
|
+
stale: revision !== revisionAtStart,
|
|
1737
|
+
file,
|
|
1738
|
+
localPath,
|
|
1739
|
+
url: `${originForLinks()}${isoAssetBase(projectId)}${file}`,
|
|
1740
|
+
width: rendered.bitmap.width,
|
|
1741
|
+
height: rendered.bitmap.height,
|
|
1742
|
+
crop: rendered.crop,
|
|
1743
|
+
scale: rendered.scale,
|
|
1744
|
+
range: rendered.range,
|
|
1745
|
+
drawn: rendered.drawn,
|
|
1746
|
+
markersDrawn: rendered.markersDrawn,
|
|
1747
|
+
warnings: rendered.warnings,
|
|
1748
|
+
elapsedMs: Date.now() - startedAt
|
|
1749
|
+
};
|
|
1363
1750
|
}
|
|
1364
1751
|
export function isoExportDirName(doc, project) {
|
|
1365
1752
|
const name = doc?.name ?? project.name;
|