From 5c6c6b9be02f7aa9998fb8ec8383d0b4e81f783c Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 30 May 2024 08:57:12 -0300 Subject: [PATCH 1/2] fromSb3: handle undefined sb3Block.next Also cleans up the parameters for blockWithNext. --- src/io/sb3/fromSb3.ts | 4 ++-- src/io/sb3/interfaces.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/io/sb3/fromSb3.ts b/src/io/sb3/fromSb3.ts index 86424d75..8e64b2ca 100644 --- a/src/io/sb3/fromSb3.ts +++ b/src/io/sb3/fromSb3.ts @@ -292,7 +292,7 @@ function getBlockScript(blocks: { [key: string]: sb3.Block }) { } } - function blockWithNext(blockId: string, parentId: string | undefined = undefined): Block[] { + function blockWithNext(blockId: string, parentId?: string): Block[] { const sb3Block = blocks[blockId]; const block = new BlockBase({ opcode: sb3Block.opcode, @@ -302,7 +302,7 @@ function getBlockScript(blocks: { [key: string]: sb3.Block }) { next: sb3Block.next ?? undefined }) as Block; let next: Block[] = []; - if (sb3Block.next !== null) { + if (typeof sb3Block.next === "string") { next = blockWithNext(sb3Block.next, blockId); } return [block, ...next]; diff --git a/src/io/sb3/interfaces.ts b/src/io/sb3/interfaces.ts index ba123044..aa4e4d31 100644 --- a/src/io/sb3/interfaces.ts +++ b/src/io/sb3/interfaces.ts @@ -77,8 +77,8 @@ type MutationFor = Op extends OpCode.procedures_prototype export interface Block { opcode: Op; - next: string | null; - parent: string | null; + next?: string | null; + parent?: string | null; inputs: { [key: string]: BlockInput; From 89c55d2bbfd1cf4e0e2fb5339f5b5d2986b417b9 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 30 May 2024 08:58:17 -0300 Subject: [PATCH 2/2] fromSb3: don't re-run getBlockScript redundantly getBlockScript returns a function which is useful for getting any top-level script, and getBlockScript only operates on the total list of the target's blocks, which doesn't change dependant on the script you are trying to access. So use it like the currying function it presents itself as and don't re-run getBlockScript for every script in the target. --- src/io/sb3/fromSb3.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/io/sb3/fromSb3.ts b/src/io/sb3/fromSb3.ts index 8e64b2ca..cf40e06a 100644 --- a/src/io/sb3/fromSb3.ts +++ b/src/io/sb3/fromSb3.ts @@ -391,6 +391,8 @@ export async function fromSb3JSON(json: sb3.ProjectJSON, options: { getAsset: Ge extractSounds(target, options.getAsset) ]); + const getScript = getBlockScript(target.blocks); + return { name: target.name, isStage: target.isStage, @@ -402,7 +404,7 @@ export async function fromSb3JSON(json: sb3.ProjectJSON, options: { getAsset: Ge .map( ([id, block]) => new Script({ - blocks: getBlockScript(target.blocks)(id), + blocks: getScript(id), x: block.x, y: block.y })