Added pendingDestroy method and prefix to the key

This commit is contained in:
Richard Davey 2018-05-03 00:36:29 +01:00
parent e7f98fc02f
commit 04487f7510
16 changed files with 349 additions and 112 deletions

View file

@ -83,7 +83,14 @@ var File = new Class({
* @type {string}
* @since 3.0.0
*/
this.key = loader.prefix + GetFastValue(fileConfig, 'key', false);
this.key = GetFastValue(fileConfig, 'key', false);
var loadKey = this.key;
if (loader.prefix && loader.prefix !== '')
{
this.key = loader.prefix + loadKey;
}
if (!this.type || !this.key)
{
@ -101,7 +108,7 @@ var File = new Class({
if (this.url === undefined)
{
this.url = loader.path + this.key + '.' + GetFastValue(fileConfig, 'extension', '');
this.url = loader.path + loadKey + '.' + GetFastValue(fileConfig, 'extension', '');
}
else if (typeof(this.url) !== 'function')
{
@ -248,9 +255,8 @@ var File = new Class({
{
if (this.state === CONST.FILE_POPULATED)
{
this.onComplete();
this.loader.nextFile(this);
// Can happen for example in a JSONFile if they've provided a JSON object instead of a URL
this.loader.nextFile(this, true);
}
else
{
@ -412,12 +418,33 @@ var File = new Class({
this.cache.add(this.key, this.data);
}
this.loader.emit('filecomplete', this.key, this);
this.pendingDestroy();
},
/**
* Adds this file to its target cache upon successful loading and processing.
* It will emit a `filecomplete` event from the LoaderPlugin.
* This method is often overridden by specific file types.
*
* @method Phaser.Loader.File#pendingDestroy
* @since 3.7.0
*/
pendingDestroy: function (data)
{
if (data === undefined) { data = this.data; }
var key = this.key;
var type = this.type;
this.loader.emit('filecomplete', key, type, data);
this.loader.emit(type + 'complete', key, data);
this.loader.flagForRemoval(this);
},
/**
* Destroy this File and any references it holds.
* Called automatically by the Loader.
*
* @method Phaser.Loader.File#destroy
* @since 3.7.0

View file

@ -41,15 +41,19 @@ var AnimationJSONFile = new Class({
onProcess: function ()
{
// We need to hook into this event:
this.loader.once('processcomplete', this.onProcessComplete, this);
this.loader.once('loadcomplete', this.onLoadComplete, this);
// But the rest is the same as a normal JSON file
JSONFile.prototype.onProcess.call(this);
},
onProcessComplete: function ()
onLoadComplete: function ()
{
console.log('AnimationJSONFile.onLoadComplete');
this.loader.scene.sys.anims.fromJSON(this.data);
this.pendingDestroy();
}
});

View file

@ -56,7 +56,7 @@ var AtlasJSONFile = new Class({
addToCache: function ()
{
if (this.failed === 0 && !this.complete)
if (this.isReadyToProcess())
{
var fileA = this.files[0];
var fileB = this.files[1];

View file

@ -1,68 +0,0 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var AudioFile = require('./AudioFile.js');
var CONST = require('../const');
var FileTypesManager = require('../FileTypesManager');
var JSONFile = require('./JSONFile.js');
/**
* Adds an Audio Sprite file to the current load queue.
*
* Note: This method will only be available if the Audio Sprite File type has been built into Phaser.
*
* The file is **not** loaded immediately after calling this method.
* Instead, the file is added to a queue within the Loader, which is processed automatically when the Loader starts.
*
* @method Phaser.Loader.LoaderPlugin#audioSprite
* @since 3.0.0
*
* @param {string} key - [description]
* @param {(string|string[])} urls - [description]
* @param {object} json - [description]
* @param {object} config - [description]
* @param {XHRSettingsObject} [audioXhrSettings] - Optional file specific XHR settings.
* @param {XHRSettingsObject} [jsonXhrSettings] - Optional file specific XHR settings.
*
* @return {Phaser.Loader.LoaderPlugin} The Loader.
*/
FileTypesManager.register('audioSprite', function (key, urls, json, config, audioXhrSettings, jsonXhrSettings)
{
var audioFile = AudioFile.create(this, key, urls, config, audioXhrSettings);
if (audioFile)
{
var jsonFile;
if (typeof json === 'string')
{
jsonFile = new JSONFile(this, key, json, jsonXhrSettings);
this.addFile(jsonFile);
}
else
{
jsonFile = {
type: 'json',
key: key,
data: json,
state: CONST.FILE_WAITING_LINKFILE
};
}
// Link them together
audioFile.linkFile = jsonFile;
jsonFile.linkFile = audioFile;
// Set the type
audioFile.linkType = 'audioSprite';
jsonFile.linkType = 'audioSprite';
this.addFile(audioFile);
}
return this;
});

View file

@ -0,0 +1,166 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var AudioFile = require('./AudioFile.js');
var Class = require('../../utils/Class');
var FileTypesManager = require('../FileTypesManager');
var GetFastValue = require('../../utils/object/GetFastValue');
var IsPlainObject = require('../../utils/object/IsPlainObject');
var JSONFile = require('./JSONFile.js');
var LinkFile = require('../LinkFile.js');
/**
* @classdesc
* An Audio Sprite JSON File.
*
* @class AudioSpriteFile
* @extends Phaser.Loader.LinkFile
* @memberOf Phaser.Loader.FileTypes
* @constructor
* @since 3.7.0
*
* @param {string} key - The key of the file within the loader.
* @param {string} textureURL - The url to load the texture file from.
* @param {string} atlasURL - The url to load the atlas file from.
* @param {string} path - The path of the file.
* @param {XHRSettingsObject} [textureXhrSettings] - Optional texture file specific XHR settings.
* @param {XHRSettingsObject} [atlasXhrSettings] - Optional atlas file specific XHR settings.
*/
var AudioSpriteFile = new Class({
Extends: LinkFile,
initialize:
function AudioSpriteFile (loader, key, urls, json, config, audioXhrSettings, jsonXhrSettings)
{
if (IsPlainObject(key))
{
var config = key;
// key = GetFastValue(config, 'key');
// URLs = GetFastValue(config, 'urls');
// json = GetFastValue(config, 'json');
// atlasURL = GetFastValue(config, 'atlasURL');
// textureXhrSettings = GetFastValue(config, 'textureXhrSettings');
// atlasXhrSettings = GetFastValue(config, 'atlasXhrSettings');
}
var audio = AudioFile.create(loader, key, urls, config, audioXhrSettings)
var data = new JSONFile(loader, key, json, jsonXhrSettings);
LinkFile.call(this, loader, 'audiosprite', key, [ audio, data ]);
},
addToCache: function ()
{
if (this.isReadyToProcess())
{
var fileA = this.files[0];
var fileB = this.files[1];
/*
if (fileA.type === 'image')
{
this.loader.textureManager.addAtlas(fileA.key, fileA.data, fileB.data);
fileB.addToCache();
}
else
{
this.loader.textureManager.addAtlas(fileB.key, fileB.data, fileA.data);
fileA.addToCache();
}
*/
this.complete = true;
}
}
});
/**
* Adds an Audio Sprite file to the current load queue.
*
* Note: This method will only be available if the Audio Sprite File type has been built into Phaser.
*
* The file is **not** loaded immediately after calling this method.
* Instead, the file is added to a queue within the Loader, which is processed automatically when the Loader starts.
*
* @method Phaser.Loader.LoaderPlugin#audioSprite
* @since 3.0.0
*
* @param {string} key - [description]
* @param {(string|string[])} urls - [description]
* @param {object} json - [description]
* @param {object} config - [description]
* @param {XHRSettingsObject} [audioXhrSettings] - Optional file specific XHR settings.
* @param {XHRSettingsObject} [jsonXhrSettings] - Optional file specific XHR settings.
*
* @return {Phaser.Loader.LoaderPlugin} The Loader.
*/
FileTypesManager.register('audioSprite', function (key, urls, json, config, audioXhrSettings, jsonXhrSettings)
{
var linkfile;
// Supports an Object file definition in the key argument
// Or an array of objects in the key argument
// Or a single entry where all arguments have been defined
if (Array.isArray(key))
{
for (var i = 0; i < key.length; i++)
{
linkfile = new AudioSpriteFile(this, key[i]);
this.addFile(linkfile.files);
}
}
else
{
linkfile = new AudioSpriteFile(this, key, urls, json, config, audioXhrSettings, jsonXhrSettings);
this.addFile(linkfile.files);
}
return this;
/*
var audioFile = AudioFile.create(this, key, urls, config, audioXhrSettings);
if (audioFile)
{
var jsonFile;
if (typeof json === 'string')
{
jsonFile = new JSONFile(this, key, json, jsonXhrSettings);
this.addFile(jsonFile);
}
else
{
jsonFile = {
type: 'json',
key: key,
data: json,
state: CONST.FILE_WAITING_LINKFILE
};
}
// Link them together
audioFile.linkFile = jsonFile;
jsonFile.linkFile = audioFile;
// Set the type
audioFile.linkType = 'audioSprite';
jsonFile.linkType = 'audioSprite';
this.addFile(audioFile);
}
return this;
*/
});

View file

@ -57,28 +57,21 @@ var BitmapFontFile = new Class({
addToCache: function ()
{
if (this.failed === 0 && !this.complete)
if (this.isReadyToProcess())
{
var fileA = this.files[0];
var fileB = this.files[1];
fileA.addToCache();
fileB.addToCache();
if (fileA.type === 'image')
{
this.loader.cacheManager.bitmapFont.add(fileB.key, { data: ParseXMLBitmapFont(fileB.data), texture: fileA.key, frame: null });
this.loader.textureManager.addImage(fileA.key, fileA.data);
// Add the XML into the XML cache
fileB.addToCache();
}
else
{
this.loader.cacheManager.bitmapFont.add(fileA.key, { data: ParseXMLBitmapFont(fileA.data), texture: fileB.key, frame: null });
this.loader.textureManager.addImage(fileB.key, fileB.data);
// Add the XML into the XML cache
fileA.addToCache();
}
this.complete = true;

View file

@ -126,9 +126,9 @@ var HTMLFile = new Class({
addToCache: function ()
{
this.cache.addImage(this.key, this.data);
var texture = this.cache.addImage(this.key, this.data);
this.loader.emit('filecomplete', this.key, this);
this.pendingDestroy(texture);
}
});

View file

@ -93,9 +93,9 @@ var ImageFile = new Class({
addToCache: function ()
{
this.cache.addImage(this.key, this.data);
var texture = this.cache.addImage(this.key, this.data);
this.loader.emit('filecomplete', this.key, this);
this.pendingDestroy(texture);
}
});

View file

@ -33,8 +33,9 @@ var JSONFile = new Class({
initialize:
// url can either be a string, in which case it is treated like a proper url, or an object, in which case it is treated as a ready-made JS Object
// dataKey allows you to pluck a specific object out of the JSON and put just that into the cache, rather than the whole thing
function JSONFile (loader, key, url, xhrSettings)
function JSONFile (loader, key, url, xhrSettings, dataKey)
{
var extension = 'json';
@ -46,6 +47,7 @@ var JSONFile = new Class({
url = GetFastValue(config, 'url');
xhrSettings = GetFastValue(config, 'xhrSettings');
extension = GetFastValue(config, 'extension', extension);
dataKey = GetFastValue(config, 'dataKey', dataKey);
}
var fileConfig = {
@ -55,7 +57,8 @@ var JSONFile = new Class({
responseType: 'text',
key: key,
url: url,
xhrSettings: xhrSettings
xhrSettings: xhrSettings,
config: dataKey
};
File.call(this, loader, fileConfig);
@ -63,7 +66,8 @@ var JSONFile = new Class({
if (IsPlainObject(url))
{
// Object provided instead of a URL, so no need to actually load it (populate data with value)
this.data = url;
this.data = (dataKey && dataKey !== '' && url.hasOwnProperty(dataKey)) ? url[dataKey] : url;
this.state = CONST.FILE_POPULATED;
}
@ -71,9 +75,15 @@ var JSONFile = new Class({
onProcess: function ()
{
this.state = CONST.FILE_PROCESSING;
if (this.state !== CONST.FILE_POPULATED)
{
this.state = CONST.FILE_PROCESSING;
this.data = JSON.parse(this.xhrLoader.responseText);
var json = JSON.parse(this.xhrLoader.responseText);
var key = this.config;
this.data = (key && key !== '' && json.hasOwnProperty(key)) ? json[key] : json;
}
this.onProcessComplete();
}
@ -93,11 +103,12 @@ var JSONFile = new Class({
*
* @param {string} key - [description]
* @param {string} url - [description]
* @param {string} [dataKey] - [description]
* @param {XHRSettingsObject} [xhrSettings] - [description]
*
* @return {Phaser.Loader.LoaderPlugin} The Loader.
*/
FileTypesManager.register('json', function (key, url, xhrSettings)
FileTypesManager.register('json', function (key, url, dataKey, xhrSettings)
{
if (Array.isArray(key))
{
@ -109,7 +120,7 @@ FileTypesManager.register('json', function (key, url, xhrSettings)
}
else
{
this.addFile(new JSONFile(this, key, url, xhrSettings));
this.addFile(new JSONFile(this, key, url, xhrSettings, dataKey));
}
return this;

View file

@ -0,0 +1,98 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../../utils/Class');
var CONST = require('../const');
var FileTypesManager = require('../FileTypesManager');
var JSONFile = require('./JSONFile.js');
/**
* @classdesc
* [description]
*
* @class PackFile
* @extends Phaser.Loader.File
* @memberOf Phaser.Loader.FileTypes
* @constructor
* @since 3.7.0
*
* @param {string} key - [description]
* @param {string} url - [description]
* @param {XHRSettingsObject} [xhrSettings] - [description]
*/
var PackFile = new Class({
Extends: JSONFile,
initialize:
// url can either be a string, in which case it is treated like a proper url, or an object, in which case it is treated as a ready-made JS Object
function PackFile (loader, key, url, xhrSettings, packKey)
{
JSONFile.call(this, loader, key, url, xhrSettings, packKey);
this.type = 'packfile';
},
onProcess: function ()
{
if (this.state !== CONST.FILE_POPULATED)
{
this.state = CONST.FILE_PROCESSING;
this.data = JSON.parse(this.xhrLoader.responseText);
}
// Let's pass the pack file data over to the Loader ...
this.loader.addPack(this.data, this.config);
this.onProcessComplete();
}
});
/**
* Adds an Animation JSON file to the current load queue.
*
* Note: This method will only be available if the Animation JSON File type has been built into Phaser.
*
* The file is **not** loaded immediately after calling this method.
* Instead, the file is added to a queue within the Loader, which is processed automatically when the Loader starts.
*
* @method Phaser.Loader.LoaderPlugin#pack
* @since 3.7.0
*
* @param {(string|array|object)} key - A unique string to be used as the key to reference this file from the Cache. Must be unique within this file type.
* @param {string} [url] - URL of the file. If `undefined` or `null` the url will be set to `<key>.json`,
* i.e. if `key` was "alien" then the URL will be "alien.json".
* @param {string} [packKey] - If you just want to process part of a pack, provide the key here. Leave empty to process the whole pack.
* @param {XHRSettingsObject} [xhrSettings] - File specific XHR settings to be used during the load. These settings are merged with the global Loader XHR settings.
*
* @return {Phaser.Loader.LoaderPlugin} The Loader.
*/
FileTypesManager.register('pack', function (key, url, packKey, xhrSettings)
{
// Supports an Object file definition in the key argument
// Or an array of objects in the key argument
// Or a single entry where all arguments have been defined
if (Array.isArray(key))
{
for (var i = 0; i < key.length; i++)
{
this.addFile(new PackFile(this, key[i]));
}
}
else
{
this.addFile(new PackFile(this, key, url, xhrSettings, packKey));
}
return this;
});
module.exports = PackFile;

View file

@ -115,9 +115,9 @@ var SVGFile = new Class({
addToCache: function ()
{
this.cache.addImage(this.key, this.data);
var texture = this.cache.addImage(this.key, this.data);
this.loader.emit('filecomplete', this.key, this);
this.pendingDestroy(texture);
}
});

View file

@ -40,9 +40,9 @@ var SpriteSheetFile = new Class({
addToCache: function ()
{
this.cache.addSpriteSheet(this.key, this.data, this.config);
var texture = this.cache.addSpriteSheet(this.key, this.data, this.config);
this.loader.emit('filecomplete', this.key, this);
this.pendingDestroy(texture);
}
});

View file

@ -74,9 +74,11 @@ var TilemapCSVFile = new Class({
addToCache: function ()
{
this.cache.add(this.key, { format: this.tilemapFormat, data: this.data });
var tiledata = { format: this.tilemapFormat, data: this.data };
this.loader.emit('filecomplete', this.key, this);
this.cache.add(this.key, tiledata);
this.pendingDestroy(tiledata);
}
});

View file

@ -41,9 +41,11 @@ var TilemapImpactFile = new Class({
addToCache: function ()
{
this.cache.add(this.key, { format: TILEMAP_FORMATS.WELTMEISTER, data: this.data });
var tiledata = { format: TILEMAP_FORMATS.WELTMEISTER, data: this.data };
this.loader.emit('filecomplete', this.key, this);
this.cache.add(this.key, tiledata);
this.pendingDestroy(tiledata);
}
});

View file

@ -41,9 +41,11 @@ var TilemapJSONFile = new Class({
addToCache: function ()
{
this.cache.add(this.key, { format: TILEMAP_FORMATS.TILED_JSON, data: this.data });
var tiledata = { format: TILEMAP_FORMATS.TILED_JSON, data: this.data };
this.loader.emit('filecomplete', this.key, this);
this.cache.add(this.key, tiledata);
this.pendingDestroy(tiledata);
}
});

View file

@ -36,7 +36,7 @@ module.exports = {
AnimationJSONFile: require('./AnimationJSONFile'),
AtlasJSONFile: require('./AtlasJSONFile'),
AudioFile: require('./AudioFile'),
AudioSprite: require('./AudioSprite'),
AudioSpriteFile: require('./AudioSpriteFile'),
BinaryFile: require('./BinaryFile'),
BitmapFontFile: require('./BitmapFontFile'),
GLSLFile: require('./GLSLFile'),
@ -44,7 +44,7 @@ module.exports = {
HTMLFile: require('./HTMLFile'),
ImageFile: require('./ImageFile'),
JSONFile: require('./JSONFile'),
MultiAtlas: require('./MultiAtlas'),
PackFile: require('./PackFile'),
PluginFile: require('./PluginFile'),
ScriptFile: require('./ScriptFile'),
SpriteSheetFile: require('./SpriteSheetFile'),