diff --git a/src/AssetType.js b/src/AssetType.js index 393d72a..b6a8bb5 100644 --- a/src/AssetType.js +++ b/src/AssetType.js @@ -8,32 +8,38 @@ const DataFormat = require('./DataFormat'); * @property {string} name - The human-readable name of this asset type. * @property {DataFormat} runtimeFormat - The format used for runtime, in-memory storage of this asset. For example, a * project stored in SB2 format on disk will be returned as JSON when loaded into memory. + * @property {boolean} immutable - Indicates if the asset id is determined by the asset content. */ const AssetType = { ImageBitmap: { contentType: 'image/png', name: 'ImageBitmap', - runtimeFormat: DataFormat.PNG + runtimeFormat: DataFormat.PNG, + immutable: true }, ImageVector: { contentType: 'image/svg+xml', name: 'ImageVector', - runtimeFormat: DataFormat.SVG + runtimeFormat: DataFormat.SVG, + immutable: true }, Project: { contentType: 'application/json', name: 'Project', - runtimeFormat: DataFormat.JSON + runtimeFormat: DataFormat.JSON, + immutable: false }, Sound: { contentType: 'audio/x-wav', name: 'Sound', - runtimeFormat: DataFormat.WAV + runtimeFormat: DataFormat.WAV, + immutable: true }, Sprite: { contentType: 'application/json', name: 'Sprite', - runtimeFormat: DataFormat.JSON + runtimeFormat: DataFormat.JSON, + immutable: true } }; diff --git a/src/BuiltinHelper.js b/src/BuiltinHelper.js index 6853244..3ba5d58 100644 --- a/src/BuiltinHelper.js +++ b/src/BuiltinHelper.js @@ -9,7 +9,7 @@ const Helper = require('./Helper'); * @typedef {object} BuiltinAssetRecord * @property {AssetType} type - The type of the asset. * @property {DataFormat} format - The format of the asset's data. - * @property {string} id - The asset's unique ID. + * @property {?string} id - The asset's unique ID. * @property {Buffer} data - The asset's data. */ @@ -97,8 +97,8 @@ class BuiltinHelper extends Helper { cache (assetType, dataFormat, data, id) { if (!dataFormat) throw new Error('Data cached without specifying its format'); if (id) { - if (this.assets.hasOwnProperty(id)) return id; - } else { + if (this.assets.hasOwnProperty(id) && assetType.immutable) return id; + } else if (assetType.immutable) { const hash = crypto.createHash('md5'); hash.update(data); id = hash.digest('hex'); diff --git a/src/ScratchStorage.js b/src/ScratchStorage.js index 36175fd..cf2e5d4 100644 --- a/src/ScratchStorage.js +++ b/src/ScratchStorage.js @@ -117,11 +117,12 @@ class ScratchStorage { tryNextHelper(); } else { // TODO? this.localHelper.cache(assetType, assetId, asset); - if (helper !== this.builtinHelper) { + if (helper !== this.builtinHelper && assetType.immutable) { asset.assetId = this.builtinHelper.cache( assetType, asset.dataFormat, - asset.data + asset.data, + assetId ); } // Note that other attempts may have caused errors, effectively suppressed here.