11import { Serialized } from "@/types/node" ;
2+ import { Path } from "@/utils/path" ;
3+ import { readFile } from "@tauri-apps/plugin-fs" ;
24import { v4 as uuidv4 } from "uuid" ;
5+ import { URI } from "vscode-uri" ;
36
47export 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