Skip to content

Commit

Permalink
Improve SB3 typings, remove ts-util
Browse files Browse the repository at this point in the history
  • Loading branch information
adroitwhiz committed Feb 28, 2023
1 parent 7b9f6d1 commit 6641431
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 35 deletions.
16 changes: 8 additions & 8 deletions src/io/sb3/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,7 @@ export type BlockInputValue = Readonly<
// (which could imply special handling for the other INPUT_BLOCK_* values,
// when none such is required and whose values are never specified in this
// mapping).
export const inputPrimitiveOrShadowMap: {
// Custom procedure blocks should be serialized separately from how normal
// blocks are, since most of their data is stored on a "mutation" field not
// accounted for here.
[opcode in Exclude<KnownBlock["opcode"], ProcedureBlock["opcode"]>]: {
[fieldName: string]: number | OpCode;
};
} = {
export const inputPrimitiveOrShadowMap = {
[OpCode.motion_movesteps]: { STEPS: BIS.MATH_NUM_PRIMITIVE },
[OpCode.motion_turnright]: { DEGREES: BIS.MATH_NUM_PRIMITIVE },
[OpCode.motion_turnleft]: { DEGREES: BIS.MATH_NUM_PRIMITIVE },
Expand Down Expand Up @@ -524,4 +517,11 @@ export const inputPrimitiveOrShadowMap: {
[OpCode.data_hidelist]: {},
[OpCode.argument_reporter_boolean]: {},
[OpCode.argument_reporter_string_number]: {}
} as const satisfies {
// Custom procedure blocks should be serialized separately from how normal
// blocks are, since most of their data is stored on a "mutation" field not
// accounted for here.
[opcode in Exclude<KnownBlock["opcode"], ProcedureBlock["opcode"]>]: {
[fieldName: string]: number | OpCode;
};
};
25 changes: 9 additions & 16 deletions src/io/sb3/toSb3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Target, { Sprite, Stage } from "../../Target";
import * as BlockInput from "../../BlockInput";
import * as sb3 from "./interfaces";
import { OpCode } from "../../OpCode";
import { prop } from "../../util/ts-util";

const BIS = sb3.BlockInputStatus;

Expand Down Expand Up @@ -506,13 +505,10 @@ export default function toSb3(project: Project, options: Partial<ToSb3Options> =
for (const arg of args) {
const shadowId = arg.id + "-prototype-shadow";
blockData[shadowId] = {
opcode: prop(
{
boolean: OpCode.argument_reporter_boolean,
numberOrString: OpCode.argument_reporter_string_number
},
arg.type
),
opcode: {
boolean: OpCode.argument_reporter_boolean,
numberOrString: OpCode.argument_reporter_string_number
}[arg.type],

next: null,
parent: prototypeId,
Expand Down Expand Up @@ -617,7 +613,7 @@ export default function toSb3(project: Project, options: Partial<ToSb3Options> =
}

default: {
const inputEntries = prop(sb3.inputPrimitiveOrShadowMap, block.opcode);
const inputEntries = sb3.inputPrimitiveOrShadowMap[block.opcode];

const initialValues: Record<string, Readonly<BlockInput.Any["value"]>> = {};
for (const key of Object.keys(inputEntries)) {
Expand Down Expand Up @@ -843,13 +839,10 @@ export default function toSb3(project: Project, options: Partial<ToSb3Options> =
id,
name,
type,
default: prop(
{
boolean: "false",
numberOrString: ""
},
type
)
default: {
boolean: "false",
numberOrString: ""
}[type]
});
}

Expand Down
11 changes: 0 additions & 11 deletions src/util/ts-util.ts

This file was deleted.

0 comments on commit 6641431

Please sign in to comment.