Skip to content

Commit

Permalink
Add an empty stage if a project is missing one entirely
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin committed Apr 22, 2024
1 parent c3315cc commit 6c94ef1
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 13 deletions.
54 changes: 43 additions & 11 deletions src/sb3fix.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,23 +330,55 @@ const fixJSON = (data, options = {}) => {
fixTargetInPlace(target);
}

let stage;
const allStages = targets.filter((target) => target.isStage);
if (allStages.length !== 1) {
throw new Error(`wrong number of stages: ${allStages.length}`);
}
const stageIndex = targets.findIndex((target) => target.isStage);
// stageIndex guaranteed to not be -1 by earlier check
const stage = targets[stageIndex];
// stage must be the first target
if (stageIndex !== 0) {
log('stage was not at start');
targets.splice(stageIndex, 1);
if (allStages.length === 0) {
log('stage is missing; adding an empty one');
stage = {
isStage: true,
name: 'Stage',
variables: {},
lists: {},
broadcasts: {},
blocks: {},
currentCostume: 0,
costumes: [
{
name: 'backdrop1',
dataFormat: 'svg',
assetId: 'cd21514d0531fdffb22204e0ec5ed84a',
md5ext: 'cd21514d0531fdffb22204e0ec5ed84a.svg',
rotationCenterX: 240,
rotationCenterY: 180
}
],
sounds: [],
volume: 100,
layerOrder: 0,
tempo: 60,
videoTransparency: 50,
videoState: "on",
textToSpeechLanguage: null
};
targets.unshift(stage);
} else if (allStages.length === 1) {
const stageIndex = targets.findIndex((target) => target.isStage);
// stageIndex guaranteed to not be -1 by earlier filter check
stage = targets[stageIndex];
// stage must be the first target
if (stageIndex !== 0) {
log(`stage was at wrong index: ${stageIndex}`);
targets.splice(stageIndex, 1);
targets.unshift(stage);
}
} else {
throw new Error(`wrong number of stages: ${allStages.length}`);
}

// stage's name must match exactly
if (stage.name !== 'Stage') {
log(`stage had wrong name: ${stage.name}`);
stage.name = 'Stage';
log('stage had wrong name');
}

const knownExtensions = getKnownExtensions(project);
Expand Down
Binary file added tests/expected-output/no-stage.sb3
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/expected-output/no-stage.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
checking target 0
stage is missing; adding an empty one
2 changes: 1 addition & 1 deletion tests/expected-output/renamed-stage.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
checking target 0
checking target 1
stage had wrong name
stage had wrong name: Not The Stage
2 changes: 1 addition & 1 deletion tests/expected-output/reordered-stage.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
checking target 0
checking target 1
stage was not at start
stage was at wrong index: 1
Binary file added tests/samples/no-stage.sb3
Binary file not shown.

0 comments on commit 6c94ef1

Please sign in to comment.