Merge pull request #1900 from kchadha/clamp-costumes-during-project-load

Clamp the currentCostume number when loading a project
This commit is contained in:
Paul Kaplan 2019-01-08 14:42:20 -05:00 committed by GitHub
commit 250d88565a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 2 deletions

View file

@ -12,6 +12,7 @@ const Color = require('../util/color');
const log = require('../util/log'); const log = require('../util/log');
const uid = require('../util/uid'); const uid = require('../util/uid');
const StringUtil = require('../util/string-util'); const StringUtil = require('../util/string-util');
const MathUtil = require('../util/math-util');
const specMap = require('./sb2_specmap'); const specMap = require('./sb2_specmap');
const Comment = require('../engine/comment'); const Comment = require('../engine/comment');
const Variable = require('../engine/variable'); const Variable = require('../engine/variable');
@ -642,7 +643,10 @@ const parseScratchObject = function (object, runtime, extensions, topLevel, zip)
target.visible = object.visible; target.visible = object.visible;
} }
if (object.hasOwnProperty('currentCostumeIndex')) { if (object.hasOwnProperty('currentCostumeIndex')) {
target.currentCostume = Math.round(object.currentCostumeIndex); // Current costume index can sometimes be a floating
// point number, use Math.floor to come up with an appropriate index
// and clamp it to the actual number of costumes the object has for good measure.
target.currentCostume = MathUtil.clamp(Math.floor(object.currentCostumeIndex), 0, object.costumes.length - 1);
} }
if (object.hasOwnProperty('rotationStyle')) { if (object.hasOwnProperty('rotationStyle')) {
if (object.rotationStyle === 'none') { if (object.rotationStyle === 'none') {

View file

@ -447,6 +447,13 @@ const serializeTarget = function (target, extensions) {
obj.broadcasts = vars.broadcasts; obj.broadcasts = vars.broadcasts;
[obj.blocks, targetExtensions] = serializeBlocks(target.blocks); [obj.blocks, targetExtensions] = serializeBlocks(target.blocks);
obj.comments = serializeComments(target.comments); obj.comments = serializeComments(target.comments);
// TODO remove this check/patch when (#1901) is fixed
if (target.currentCostume < 0 || target.currentCostume >= target.costumes.length) {
log.warn(`currentCostume property for target ${target.name} is out of range`);
target.currentCostume = MathUtil.clamp(target.currentCostume, 0, target.costumes.length - 1);
}
obj.currentCostume = target.currentCostume; obj.currentCostume = target.currentCostume;
obj.costumes = target.costumes.map(serializeCostume); obj.costumes = target.costumes.map(serializeCostume);
obj.sounds = target.sounds.map(serializeSound); obj.sounds = target.sounds.map(serializeSound);
@ -1007,7 +1014,7 @@ const parseScratchObject = function (object, runtime, extensions, zip) {
target.visible = object.visible; target.visible = object.visible;
} }
if (object.hasOwnProperty('currentCostume')) { if (object.hasOwnProperty('currentCostume')) {
target.currentCostume = object.currentCostume; target.currentCostume = MathUtil.clamp(object.currentCostume, 0, object.costumes.length - 1);
} }
if (object.hasOwnProperty('rotationStyle')) { if (object.hasOwnProperty('rotationStyle')) {
target.rotationStyle = object.rotationStyle; target.rotationStyle = object.rotationStyle;