Skip to content

Commit 9487ff4

Browse files
committed
Remove extra stages (fixes #35)
1 parent 6917cff commit 9487ff4

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

src/sb3fix.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,10 @@ const fixJSON = (data, options = {}) => {
351351
fixTargetInPlace(target);
352352
}
353353

354-
let stage;
355354
const allStages = targets.filter((target) => target.isStage);
356355
if (allStages.length === 0) {
357356
log('stage is missing; adding an empty one');
358-
stage = {
357+
targets.unshift({
359358
isStage: true,
360359
name: 'Stage',
361360
variables: {},
@@ -380,22 +379,31 @@ const fixJSON = (data, options = {}) => {
380379
videoTransparency: 50,
381380
videoState: "on",
382381
textToSpeechLanguage: null
383-
};
384-
targets.unshift(stage);
385-
} else if (allStages.length === 1) {
386-
const stageIndex = targets.findIndex((target) => target.isStage);
387-
// stageIndex guaranteed to not be -1 by earlier filter check
388-
stage = targets[stageIndex];
389-
// stage must be the first target
390-
if (stageIndex !== 0) {
391-
log(`stage was at wrong index: ${stageIndex}`);
392-
targets.splice(stageIndex, 1);
382+
});
383+
} else {
384+
// We will accept the first stage in targets as the real stage
385+
const firstStageIndex = targets.findIndex((target) => target.isStage);
386+
387+
// Stage must be the first target
388+
if (firstStageIndex !== 0) {
389+
log(`stage was at wrong index: ${firstStageIndex}`);
390+
const stage = targets[firstStageIndex];
391+
targets.splice(firstStageIndex, 1);
393392
targets.unshift(stage);
394393
}
395-
} else {
396-
throw new Error(`wrong number of stages: ${allStages.length}`);
394+
395+
// Remove all the other stages
396+
for (let i = targets.length - 1; i > 0; i--) {
397+
if (targets[i].isStage) {
398+
log(`removing extra stage at index ${i}`);
399+
targets.splice(i, 1);
400+
}
401+
}
397402
}
398403

404+
// Above checks ensure this invariant holds
405+
const stage = targets[0];
406+
399407
// stage's name must match exactly
400408
if (stage.name !== 'Stage') {
401409
log(`stage had wrong name: ${stage.name}`);
1.26 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
checking target 0
2+
checking target 1
3+
checking target 2
4+
checking target 3
5+
removing extra stage at index 3
6+
removing extra stage at index 1

tests/samples/multiple-stages.sb3

1.39 KB
Binary file not shown.

0 commit comments

Comments
 (0)