|
const JSZip = require('jszip'); |
|
const log = require('../util/log'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const deserializeSound = function (sound, runtime, zip, assetFileName) { |
|
const fileName = assetFileName ? assetFileName : sound.md5; |
|
const storage = runtime.storage; |
|
if (!storage) { |
|
log.warn('No storage module present; cannot load sound asset: ', fileName); |
|
return Promise.resolve(null); |
|
} |
|
|
|
if (!zip) { |
|
return Promise.resolve(null); |
|
} |
|
|
|
let soundFile = zip.file(fileName); |
|
if (!soundFile) { |
|
|
|
const fileMatch = new RegExp(`^([^/]*/)?${fileName}$`); |
|
soundFile = zip.file(fileMatch)[0]; |
|
} |
|
|
|
if (!soundFile) { |
|
log.error(`Could not find sound file associated with the ${sound.name} sound.`); |
|
return Promise.resolve(null); |
|
} |
|
|
|
if (!JSZip.support.uint8array) { |
|
log.error('JSZip uint8array is not supported in this browser.'); |
|
return Promise.resolve(null); |
|
} |
|
|
|
let dataFormat = storage.DataFormat.WAV; |
|
switch (sound.dataFormat.toLowerCase()) { |
|
case "mp3": |
|
dataFormat = storage.DataFormat.MP3; |
|
break; |
|
case "ogg": |
|
dataFormat = storage.DataFormat.OGG; |
|
break; |
|
case "flac": |
|
dataFormat = storage.DataFormat.FLAC; |
|
break; |
|
} |
|
return soundFile.async('uint8array').then(data => storage.createAsset( |
|
storage.AssetType.Sound, |
|
dataFormat, |
|
data, |
|
null, |
|
true |
|
)) |
|
.then(asset => { |
|
sound.asset = asset; |
|
sound.assetId = asset.assetId; |
|
sound.md5 = `${asset.assetId}.${asset.dataFormat}`; |
|
}); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const deserializeCostume = function (costume, runtime, zip, assetFileName, textLayerFileName) { |
|
const storage = runtime.storage; |
|
const assetId = costume.assetId; |
|
const fileName = assetFileName ? assetFileName : |
|
`${assetId}.${costume.dataFormat}`; |
|
|
|
if (!storage) { |
|
log.warn('No storage module present; cannot load costume asset: ', fileName); |
|
return Promise.resolve(null); |
|
} |
|
|
|
if (costume.asset) { |
|
|
|
|
|
return Promise.resolve(storage.createAsset( |
|
costume.asset.assetType, |
|
costume.asset.dataFormat, |
|
new Uint8Array(Object.keys(costume.asset.data).map(key => costume.asset.data[key])), |
|
null, |
|
true |
|
)).then(asset => { |
|
costume.asset = asset; |
|
costume.assetId = asset.assetId; |
|
costume.md5 = `${asset.assetId}.${asset.dataFormat}`; |
|
}); |
|
} |
|
|
|
if (!zip) { |
|
|
|
return Promise.resolve(null); |
|
} |
|
|
|
let costumeFile = zip.file(fileName); |
|
if (!costumeFile) { |
|
|
|
const fileMatch = new RegExp(`^([^/]*/)?${fileName}$`); |
|
costumeFile = zip.file(fileMatch)[0]; |
|
} |
|
|
|
if (!costumeFile) { |
|
log.error(`Could not find costume file associated with the ${costume.name} costume.`); |
|
return Promise.resolve(null); |
|
} |
|
let assetType = null; |
|
const costumeFormat = costume.dataFormat.toLowerCase(); |
|
if (costumeFormat === 'svg') { |
|
assetType = storage.AssetType.ImageVector; |
|
} else if (['png', 'bmp', 'jpeg', 'jpg', 'gif'].indexOf(costumeFormat) >= 0) { |
|
assetType = storage.AssetType.ImageBitmap; |
|
} else { |
|
log.error(`Unexpected file format for costume: ${costumeFormat}`); |
|
} |
|
if (!JSZip.support.uint8array) { |
|
log.error('JSZip uint8array is not supported in this browser.'); |
|
return Promise.resolve(null); |
|
} |
|
|
|
|
|
|
|
let textLayerFilePromise; |
|
if (costume.textLayerMD5) { |
|
const textLayerFile = zip.file(textLayerFileName); |
|
if (!textLayerFile) { |
|
log.error(`Could not find text layer file associated with the ${costume.name} costume.`); |
|
return Promise.resolve(null); |
|
} |
|
textLayerFilePromise = textLayerFile.async('uint8array') |
|
.then(data => storage.createAsset( |
|
storage.AssetType.ImageBitmap, |
|
'png', |
|
data, |
|
costume.textLayerMD5 |
|
)) |
|
.then(asset => { |
|
costume.textLayerAsset = asset; |
|
}); |
|
} else { |
|
textLayerFilePromise = Promise.resolve(null); |
|
} |
|
|
|
return Promise.all([textLayerFilePromise, |
|
costumeFile.async('uint8array') |
|
.then(data => storage.createAsset( |
|
assetType, |
|
|
|
costumeFormat, |
|
data, |
|
null, |
|
true |
|
)) |
|
.then(asset => { |
|
costume.asset = asset; |
|
costume.assetId = asset.assetId; |
|
costume.md5 = `${asset.assetId}.${asset.dataFormat}`; |
|
}) |
|
]); |
|
}; |
|
|
|
module.exports = { |
|
deserializeSound, |
|
deserializeCostume |
|
}; |
|
|