2018-02-13 10:48:33 -05:00
|
|
|
/**
|
|
|
|
* Serialize all the assets of the given type ('sounds' or 'costumes')
|
|
|
|
* in the provided runtime into an array of file descriptors.
|
|
|
|
* A file descriptor is an object containing the name of the file
|
|
|
|
* to be written and the contents of the file, the serialized asset.
|
|
|
|
* @param {Runtime} runtime The runtime with the assets to be serialized
|
|
|
|
* @param {string} assetType The type of assets to be serialized: 'sounds' | 'costumes'
|
2018-06-19 08:51:16 -04:00
|
|
|
* @param {string=} optTargetId Optional target id to serialize assets for
|
2018-02-13 10:48:33 -05:00
|
|
|
* @returns {Array<object>} An array of file descriptors for each asset
|
|
|
|
*/
|
2018-06-19 08:51:16 -04:00
|
|
|
const serializeAssets = function (runtime, assetType, optTargetId) {
|
|
|
|
const targets = optTargetId ? [runtime.getTargetById(optTargetId)] : runtime.targets;
|
2018-02-13 10:48:33 -05:00
|
|
|
const assetDescs = [];
|
|
|
|
for (let i = 0; i < targets.length; i++) {
|
|
|
|
const currTarget = targets[i];
|
|
|
|
const currAssets = currTarget.sprite[assetType];
|
|
|
|
for (let j = 0; j < currAssets.length; j++) {
|
|
|
|
const currAsset = currAssets[j];
|
2018-10-18 07:39:28 -04:00
|
|
|
const asset = currAsset.asset;
|
2018-02-13 10:48:33 -05:00
|
|
|
assetDescs.push({
|
2018-10-18 07:39:28 -04:00
|
|
|
fileName: `${asset.assetId}.${asset.dataFormat}`,
|
|
|
|
fileContent: asset.data});
|
2018-02-13 10:48:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return assetDescs;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2018-06-19 08:51:16 -04:00
|
|
|
* Serialize all the sounds in the provided runtime or, if a target id is provided,
|
|
|
|
* in the specified target into an array of file descriptors.
|
|
|
|
* A file descriptor is an object containing the name of the file
|
2018-02-13 10:48:33 -05:00
|
|
|
* to be written and the contents of the file, the serialized sound.
|
|
|
|
* @param {Runtime} runtime The runtime with the sounds to be serialized
|
2018-06-19 08:51:16 -04:00
|
|
|
* @param {string=} optTargetId Optional targetid for serializing sounds of a single target
|
2018-02-13 10:48:33 -05:00
|
|
|
* @returns {Array<object>} An array of file descriptors for each sound
|
|
|
|
*/
|
2018-06-19 08:51:16 -04:00
|
|
|
const serializeSounds = function (runtime, optTargetId) {
|
|
|
|
return serializeAssets(runtime, 'sounds', optTargetId);
|
2018-02-13 10:48:33 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize all the costumes in the provided runtime into an array of file
|
|
|
|
* descriptors. A file descriptor is an object containing the name of the file
|
|
|
|
* to be written and the contents of the file, the serialized costume.
|
|
|
|
* @param {Runtime} runtime The runtime with the costumes to be serialized
|
2018-06-19 08:51:16 -04:00
|
|
|
* @param {string} optTargetId Optional targetid for serializing costumes of a single target
|
2018-02-13 10:48:33 -05:00
|
|
|
* @returns {Array<object>} An array of file descriptors for each costume
|
|
|
|
*/
|
2018-06-19 08:51:16 -04:00
|
|
|
const serializeCostumes = function (runtime, optTargetId) {
|
|
|
|
return serializeAssets(runtime, 'costumes', optTargetId);
|
2018-02-13 10:48:33 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
serializeSounds,
|
|
|
|
serializeCostumes
|
|
|
|
};
|