Merge pull request #579 from cwillisf/load-jpeg-backdrops

Specify dataFormat when loading asset for import
This commit is contained in:
Ray Schamp 2017-06-02 09:06:37 -04:00 committed by GitHub
commit ecf535f4c3
8 changed files with 45 additions and 10 deletions

View file

@ -45,7 +45,7 @@
"scratch-audio": "^0.1.0-prerelease.0", "scratch-audio": "^0.1.0-prerelease.0",
"scratch-blocks": "^0.1.0-prerelease.0", "scratch-blocks": "^0.1.0-prerelease.0",
"scratch-render": "^0.1.0-prerelease.0", "scratch-render": "^0.1.0-prerelease.0",
"scratch-storage": "^0.1.0", "scratch-storage": "^0.2.0",
"script-loader": "0.7.0", "script-loader": "0.7.0",
"socket.io-client": "1.7.3", "socket.io-client": "1.7.3",
"stats.js": "^0.17.0", "stats.js": "^0.17.0",

View file

@ -1,3 +1,4 @@
const StringUtil = require('../util/string-util');
const log = require('../util/log'); const log = require('../util/log');
/** /**
@ -19,17 +20,17 @@ const loadCostume = function (md5ext, costume, runtime) {
} }
const AssetType = runtime.storage.AssetType; const AssetType = runtime.storage.AssetType;
const idParts = md5ext.split('.'); const idParts = StringUtil.splitFirst(md5ext, '.');
const md5 = idParts[0]; const md5 = idParts[0];
const ext = idParts[1].toUpperCase(); const ext = idParts[1].toLowerCase();
const assetType = (ext === 'SVG') ? AssetType.ImageVector : AssetType.ImageBitmap; const assetType = (ext === 'svg') ? AssetType.ImageVector : AssetType.ImageBitmap;
const rotationCenter = [ const rotationCenter = [
costume.rotationCenterX / costume.bitmapResolution, costume.rotationCenterX / costume.bitmapResolution,
costume.rotationCenterY / costume.bitmapResolution costume.rotationCenterY / costume.bitmapResolution
]; ];
let promise = runtime.storage.load(assetType, md5).then(costumeAsset => { let promise = runtime.storage.load(assetType, md5, ext).then(costumeAsset => {
costume.assetId = costumeAsset.assetId; costume.assetId = costumeAsset.assetId;
costume.assetType = assetType; costume.assetType = assetType;
return costumeAsset; return costumeAsset;

View file

@ -1,3 +1,4 @@
const StringUtil = require('../util/string-util');
const log = require('../util/log'); const log = require('../util/log');
/** /**
@ -17,9 +18,10 @@ const loadSound = function (sound, runtime) {
log.error('No audio engine present; cannot load sound asset: ', sound.md5); log.error('No audio engine present; cannot load sound asset: ', sound.md5);
return Promise.resolve(sound); return Promise.resolve(sound);
} }
const idParts = sound.md5.split('.'); const idParts = StringUtil.splitFirst(sound.md5, '.');
const md5 = idParts[0]; const md5 = idParts[0];
return runtime.storage.load(runtime.storage.AssetType.Sound, md5) const ext = idParts[1].toLowerCase();
return runtime.storage.load(runtime.storage.AssetType.Sound, md5, ext)
.then(soundAsset => { .then(soundAsset => {
sound.assetId = soundAsset.assetId; sound.assetId = soundAsset.assetId;
sound.assetType = runtime.storage.AssetType.Sound; sound.assetType = runtime.storage.AssetType.Sound;

View file

@ -34,7 +34,7 @@ const getAssetUrl = function (asset) {
'internalapi/asset/', 'internalapi/asset/',
asset.assetId, asset.assetId,
'.', '.',
asset.assetType.runtimeFormat, asset.dataFormat,
'/get/' '/get/'
]; ];
return assetUrlParts.join(''); return assetUrlParts.join('');

View file

@ -75,7 +75,7 @@ const parseScratchObject = function (object, runtime) {
rotationCenterX: costumeSource.rotationCenterX, rotationCenterX: costumeSource.rotationCenterX,
rotationCenterY: costumeSource.rotationCenterY rotationCenterY: costumeSource.rotationCenterY
}; };
const costumeMd5 = `${costumeSource.assetId}.${costumeSource.assetType.runtimeFormat}`; const costumeMd5 = `${costumeSource.assetId}.${costumeSource.dataFormat}`;
return loadCostume(costumeMd5, costume, runtime); return loadCostume(costumeMd5, costume, runtime);
}); });
// Sounds from JSON // Sounds from JSON

View file

@ -12,6 +12,30 @@ class StringUtil {
while (existingNames.indexOf(name + i) >= 0) i++; while (existingNames.indexOf(name + i) >= 0) i++;
return name + i; return name + i;
} }
/**
* Split a string on the first occurrence of a split character.
* @param {string} text - the string to split.
* @param {string} separator - split the text on this character.
* @returns {[string, string]} - the two parts of the split string, or [text, null] if no split character found.
* @example
* // returns ['foo', 'tar.gz']
* splitFirst('foo.tar.gz', '.');
* @example
* // returns ['foo', null]
* splitFirst('foo', '.');
* @example
* // returns ['foo', '']
* splitFirst('foo.', '.');
*/
static splitFirst (text, separator) {
const index = text.indexOf(separator);
if (index >= 0) {
return [text.substring(0, index), text.substring(index + 1)];
} else {
return [text, null];
}
}
} }
module.exports = StringUtil; module.exports = StringUtil;

View file

@ -26,7 +26,7 @@ const getAssetUrl = function (asset) {
'internalapi/asset/', 'internalapi/asset/',
asset.assetId, asset.assetId,
'.', '.',
asset.assetType.runtimeFormat, asset.dataFormat,
'/get/' '/get/'
]; ];
return assetUrlParts.join(''); return assetUrlParts.join('');

View file

@ -1,6 +1,14 @@
const test = require('tap').test; const test = require('tap').test;
const StringUtil = require('../../src/util/string-util'); const StringUtil = require('../../src/util/string-util');
test('splitFirst', t => {
t.deepEqual(StringUtil.splitFirst('asdf.1234', '.'), ['asdf', '1234']);
t.deepEqual(StringUtil.splitFirst('asdf.', '.'), ['asdf', '']);
t.deepEqual(StringUtil.splitFirst('.1234', '.'), ['', '1234']);
t.deepEqual(StringUtil.splitFirst('foo', '.'), ['foo', null]);
t.end();
});
test('withoutTrailingDigits', t => { test('withoutTrailingDigits', t => {
t.strictEqual(StringUtil.withoutTrailingDigits('boeing747'), 'boeing'); t.strictEqual(StringUtil.withoutTrailingDigits('boeing747'), 'boeing');
t.strictEqual(StringUtil.withoutTrailingDigits('boeing747 '), 'boeing747 '); t.strictEqual(StringUtil.withoutTrailingDigits('boeing747 '), 'boeing747 ');