Skip to content

Commit 69e5524

Browse files
committed
✨ Convert core:image_node from old versions
1 parent 5e5f361 commit 69e5524

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

app/src/core/service/GlobalMenu.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -608,13 +608,13 @@ export async function onOpenFile(uri?: URI, source: string = "unknown") {
608608
if (!path) return;
609609
uri = URI.file(path);
610610
}
611-
let stage: Record<string, any>[];
611+
let upgraded: ReturnType<typeof ProjectUpgrader.convertVAnyToN1> extends Promise<infer T> ? T : never;
612612
if (uri.fsPath.endsWith(".json")) {
613613
const content = await readTextFile(uri.fsPath);
614614
const json = JSON.parse(content);
615615
const t = performance.now();
616-
stage = await toast
617-
.promise(ProjectUpgrader.convertVAnyToN1(json), {
616+
upgraded = await toast
617+
.promise(ProjectUpgrader.convertVAnyToN1(json, uri), {
618618
loading: "正在转换旧版项目文件...",
619619
success: () => {
620620
const time = performance.now() - t;
@@ -649,8 +649,9 @@ export async function onOpenFile(uri?: URI, source: string = "unknown") {
649649
toast.promise(project.init(), {
650650
loading: "正在打开文件...",
651651
success: () => {
652-
if (stage) {
653-
project.stage = deserialize(stage, project);
652+
if (upgraded) {
653+
project.stage = deserialize(upgraded.data, project);
654+
project.attachments = upgraded.attachments;
654655
}
655656
const readFileTime = performance.now() - t;
656657
store.set(projectsAtom, [...store.get(projectsAtom), project]);

app/src/core/stage/ProjectUpgrader.tsx

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Serialized } from "@/types/node";
2+
import { Path } from "@/utils/path";
3+
import { readFile } from "@tauri-apps/plugin-fs";
24
import { v4 as uuidv4 } from "uuid";
5+
import { URI } from "vscode-uri";
36

47
export namespace ProjectUpgrader {
58
export function upgrade(data: Record<string, any>): Record<string, any> {
@@ -317,12 +320,15 @@ export namespace ProjectUpgrader {
317320
return data;
318321
}
319322

320-
export async function convertVAnyToN1(json: Record<string, any>) {
323+
export async function convertVAnyToN1(json: Record<string, any>, uri: URI) {
321324
// 升级json数据到最新版本
322-
json = ProjectUpgrader.upgrade(json);
325+
// json = ProjectUpgrader.upgrade(json);
323326

324327
const uuidMap = new Map<string, Record<string, any>>();
325328
const resultStage: Record<string, any>[] = [];
329+
const attachments = new Map<string, Blob>();
330+
331+
const basePath = new Path(uri.fsPath).parent;
326332

327333
// Helper functions for repeated structures
328334
const toColor = (arr: number[]) => ({
@@ -340,11 +346,11 @@ export namespace ProjectUpgrader {
340346
});
341347

342348
// Recursively convert all entities
343-
function convertEntityVAnyToN1(
349+
async function convertEntityVAnyToN1(
344350
entity: Record<string, any>,
345351
uuidMap: Map<string, Record<string, any>>,
346352
entities: Array<Record<string, any>>,
347-
): Record<string, any> | undefined {
353+
): Promise<Record<string, any> | undefined> {
348354
if (uuidMap.has(entity.uuid)) {
349355
return uuidMap.get(entity.uuid);
350356
}
@@ -381,7 +387,7 @@ export namespace ProjectUpgrader {
381387
if (!childData) {
382388
const childEntity = entities.find((e) => e.uuid === childUUID);
383389
if (childEntity) {
384-
childData = convertEntityVAnyToN1(childEntity, uuidMap, entities);
390+
childData = await convertEntityVAnyToN1(childEntity, uuidMap, entities);
385391
}
386392
}
387393
if (childData) {
@@ -417,6 +423,28 @@ export namespace ProjectUpgrader {
417423
}
418424
case "core:image_node": {
419425
// 图片
426+
const path = entity.path;
427+
const imageContent = await readFile(basePath.join(path).toString());
428+
const blob = new Blob([imageContent], { type: "image/png" });
429+
const attachmentId = crypto.randomUUID();
430+
attachments.set(attachmentId, blob);
431+
data = {
432+
_: "ImageNode",
433+
uuid: entity.uuid,
434+
attachmentId,
435+
details: entity.details,
436+
collisionBox: {
437+
_: "CollisionBox",
438+
shapes: [
439+
{
440+
_: "Rectangle",
441+
location: toVector(entity.location),
442+
size: toVector(entity.size),
443+
},
444+
],
445+
},
446+
scale: entity.scale || 1,
447+
};
420448
break;
421449
}
422450
case "core:connect_point": {
@@ -449,7 +477,7 @@ export namespace ProjectUpgrader {
449477

450478
// Convert all top-level entities
451479
for (const entity of json.entities) {
452-
const data = convertEntityVAnyToN1(entity, uuidMap, json.entities);
480+
const data = await convertEntityVAnyToN1(entity, uuidMap, json.entities);
453481
if (data) {
454482
resultStage.push(data);
455483
}
@@ -496,6 +524,6 @@ export namespace ProjectUpgrader {
496524
// 遍历所有标签
497525
// TODO
498526

499-
return resultStage;
527+
return { data: resultStage, attachments };
500528
}
501529
}

0 commit comments

Comments
 (0)