Skip to content

Commit 04487f7

Browse files
committed
Added pendingDestroy method and prefix to the key
1 parent e7f98fc commit 04487f7

16 files changed

Lines changed: 349 additions & 112 deletions

src/loader/File.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,14 @@ var File = new Class({
8383
* @type {string}
8484
* @since 3.0.0
8585
*/
86-
this.key = loader.prefix + GetFastValue(fileConfig, 'key', false);
86+
this.key = GetFastValue(fileConfig, 'key', false);
87+
88+
var loadKey = this.key;
89+
90+
if (loader.prefix && loader.prefix !== '')
91+
{
92+
this.key = loader.prefix + loadKey;
93+
}
8794

8895
if (!this.type || !this.key)
8996
{
@@ -101,7 +108,7 @@ var File = new Class({
101108

102109
if (this.url === undefined)
103110
{
104-
this.url = loader.path + this.key + '.' + GetFastValue(fileConfig, 'extension', '');
111+
this.url = loader.path + loadKey + '.' + GetFastValue(fileConfig, 'extension', '');
105112
}
106113
else if (typeof(this.url) !== 'function')
107114
{
@@ -248,9 +255,8 @@ var File = new Class({
248255
{
249256
if (this.state === CONST.FILE_POPULATED)
250257
{
251-
this.onComplete();
252-
253-
this.loader.nextFile(this);
258+
// Can happen for example in a JSONFile if they've provided a JSON object instead of a URL
259+
this.loader.nextFile(this, true);
254260
}
255261
else
256262
{
@@ -412,12 +418,33 @@ var File = new Class({
412418
this.cache.add(this.key, this.data);
413419
}
414420

415-
this.loader.emit('filecomplete', this.key, this);
421+
this.pendingDestroy();
422+
},
423+
424+
/**
425+
* Adds this file to its target cache upon successful loading and processing.
426+
* It will emit a `filecomplete` event from the LoaderPlugin.
427+
* This method is often overridden by specific file types.
428+
*
429+
* @method Phaser.Loader.File#pendingDestroy
430+
* @since 3.7.0
431+
*/
432+
pendingDestroy: function (data)
433+
{
434+
if (data === undefined) { data = this.data; }
435+
436+
var key = this.key;
437+
var type = this.type;
438+
439+
this.loader.emit('filecomplete', key, type, data);
440+
441+
this.loader.emit(type + 'complete', key, data);
442+
443+
this.loader.flagForRemoval(this);
416444
},
417445

418446
/**
419447
* Destroy this File and any references it holds.
420-
* Called automatically by the Loader.
421448
*
422449
* @method Phaser.Loader.File#destroy
423450
* @since 3.7.0

src/loader/filetypes/AnimationJSONFile.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,19 @@ var AnimationJSONFile = new Class({
4141
onProcess: function ()
4242
{
4343
// We need to hook into this event:
44-
this.loader.once('processcomplete', this.onProcessComplete, this);
44+
this.loader.once('loadcomplete', this.onLoadComplete, this);
4545

4646
// But the rest is the same as a normal JSON file
4747
JSONFile.prototype.onProcess.call(this);
4848
},
4949

50-
onProcessComplete: function ()
50+
onLoadComplete: function ()
5151
{
52+
console.log('AnimationJSONFile.onLoadComplete');
53+
5254
this.loader.scene.sys.anims.fromJSON(this.data);
55+
56+
this.pendingDestroy();
5357
}
5458

5559
});

src/loader/filetypes/AtlasJSONFile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var AtlasJSONFile = new Class({
5656

5757
addToCache: function ()
5858
{
59-
if (this.failed === 0 && !this.complete)
59+
if (this.isReadyToProcess())
6060
{
6161
var fileA = this.files[0];
6262
var fileB = this.files[1];

src/loader/filetypes/AudioSprite.js

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2018 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
var AudioFile = require('./AudioFile.js');
8+
var Class = require('../../utils/Class');
9+
var FileTypesManager = require('../FileTypesManager');
10+
var GetFastValue = require('../../utils/object/GetFastValue');
11+
var IsPlainObject = require('../../utils/object/IsPlainObject');
12+
var JSONFile = require('./JSONFile.js');
13+
var LinkFile = require('../LinkFile.js');
14+
15+
/**
16+
* @classdesc
17+
* An Audio Sprite JSON File.
18+
*
19+
* @class AudioSpriteFile
20+
* @extends Phaser.Loader.LinkFile
21+
* @memberOf Phaser.Loader.FileTypes
22+
* @constructor
23+
* @since 3.7.0
24+
*
25+
* @param {string} key - The key of the file within the loader.
26+
* @param {string} textureURL - The url to load the texture file from.
27+
* @param {string} atlasURL - The url to load the atlas file from.
28+
* @param {string} path - The path of the file.
29+
* @param {XHRSettingsObject} [textureXhrSettings] - Optional texture file specific XHR settings.
30+
* @param {XHRSettingsObject} [atlasXhrSettings] - Optional atlas file specific XHR settings.
31+
*/
32+
var AudioSpriteFile = new Class({
33+
34+
Extends: LinkFile,
35+
36+
initialize:
37+
38+
function AudioSpriteFile (loader, key, urls, json, config, audioXhrSettings, jsonXhrSettings)
39+
{
40+
if (IsPlainObject(key))
41+
{
42+
var config = key;
43+
44+
// key = GetFastValue(config, 'key');
45+
// URLs = GetFastValue(config, 'urls');
46+
// json = GetFastValue(config, 'json');
47+
// atlasURL = GetFastValue(config, 'atlasURL');
48+
// textureXhrSettings = GetFastValue(config, 'textureXhrSettings');
49+
// atlasXhrSettings = GetFastValue(config, 'atlasXhrSettings');
50+
}
51+
52+
var audio = AudioFile.create(loader, key, urls, config, audioXhrSettings)
53+
var data = new JSONFile(loader, key, json, jsonXhrSettings);
54+
55+
LinkFile.call(this, loader, 'audiosprite', key, [ audio, data ]);
56+
},
57+
58+
addToCache: function ()
59+
{
60+
if (this.isReadyToProcess())
61+
{
62+
var fileA = this.files[0];
63+
var fileB = this.files[1];
64+
65+
/*
66+
if (fileA.type === 'image')
67+
{
68+
this.loader.textureManager.addAtlas(fileA.key, fileA.data, fileB.data);
69+
fileB.addToCache();
70+
}
71+
else
72+
{
73+
this.loader.textureManager.addAtlas(fileB.key, fileB.data, fileA.data);
74+
fileA.addToCache();
75+
}
76+
*/
77+
78+
this.complete = true;
79+
}
80+
}
81+
82+
});
83+
84+
/**
85+
* Adds an Audio Sprite file to the current load queue.
86+
*
87+
* Note: This method will only be available if the Audio Sprite File type has been built into Phaser.
88+
*
89+
* The file is **not** loaded immediately after calling this method.
90+
* Instead, the file is added to a queue within the Loader, which is processed automatically when the Loader starts.
91+
*
92+
* @method Phaser.Loader.LoaderPlugin#audioSprite
93+
* @since 3.0.0
94+
*
95+
* @param {string} key - [description]
96+
* @param {(string|string[])} urls - [description]
97+
* @param {object} json - [description]
98+
* @param {object} config - [description]
99+
* @param {XHRSettingsObject} [audioXhrSettings] - Optional file specific XHR settings.
100+
* @param {XHRSettingsObject} [jsonXhrSettings] - Optional file specific XHR settings.
101+
*
102+
* @return {Phaser.Loader.LoaderPlugin} The Loader.
103+
*/
104+
FileTypesManager.register('audioSprite', function (key, urls, json, config, audioXhrSettings, jsonXhrSettings)
105+
{
106+
var linkfile;
107+
108+
// Supports an Object file definition in the key argument
109+
// Or an array of objects in the key argument
110+
// Or a single entry where all arguments have been defined
111+
112+
if (Array.isArray(key))
113+
{
114+
for (var i = 0; i < key.length; i++)
115+
{
116+
linkfile = new AudioSpriteFile(this, key[i]);
117+
118+
this.addFile(linkfile.files);
119+
}
120+
}
121+
else
122+
{
123+
linkfile = new AudioSpriteFile(this, key, urls, json, config, audioXhrSettings, jsonXhrSettings);
124+
125+
this.addFile(linkfile.files);
126+
}
127+
128+
return this;
129+
130+
/*
131+
var audioFile = AudioFile.create(this, key, urls, config, audioXhrSettings);
132+
133+
if (audioFile)
134+
{
135+
var jsonFile;
136+
137+
if (typeof json === 'string')
138+
{
139+
jsonFile = new JSONFile(this, key, json, jsonXhrSettings);
140+
141+
this.addFile(jsonFile);
142+
}
143+
else
144+
{
145+
jsonFile = {
146+
type: 'json',
147+
key: key,
148+
data: json,
149+
state: CONST.FILE_WAITING_LINKFILE
150+
};
151+
}
152+
153+
// Link them together
154+
audioFile.linkFile = jsonFile;
155+
jsonFile.linkFile = audioFile;
156+
157+
// Set the type
158+
audioFile.linkType = 'audioSprite';
159+
jsonFile.linkType = 'audioSprite';
160+
161+
this.addFile(audioFile);
162+
}
163+
164+
return this;
165+
*/
166+
});

src/loader/filetypes/BitmapFontFile.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,21 @@ var BitmapFontFile = new Class({
5757

5858
addToCache: function ()
5959
{
60-
if (this.failed === 0 && !this.complete)
60+
if (this.isReadyToProcess())
6161
{
6262
var fileA = this.files[0];
6363
var fileB = this.files[1];
6464

65+
fileA.addToCache();
66+
fileB.addToCache();
67+
6568
if (fileA.type === 'image')
6669
{
6770
this.loader.cacheManager.bitmapFont.add(fileB.key, { data: ParseXMLBitmapFont(fileB.data), texture: fileA.key, frame: null });
68-
69-
this.loader.textureManager.addImage(fileA.key, fileA.data);
70-
71-
// Add the XML into the XML cache
72-
fileB.addToCache();
7371
}
7472
else
7573
{
7674
this.loader.cacheManager.bitmapFont.add(fileA.key, { data: ParseXMLBitmapFont(fileA.data), texture: fileB.key, frame: null });
77-
78-
this.loader.textureManager.addImage(fileB.key, fileB.data);
79-
80-
// Add the XML into the XML cache
81-
fileA.addToCache();
8275
}
8376

8477
this.complete = true;

src/loader/filetypes/HTMLFile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ var HTMLFile = new Class({
126126

127127
addToCache: function ()
128128
{
129-
this.cache.addImage(this.key, this.data);
129+
var texture = this.cache.addImage(this.key, this.data);
130130

131-
this.loader.emit('filecomplete', this.key, this);
131+
this.pendingDestroy(texture);
132132
}
133133

134134
});

src/loader/filetypes/ImageFile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ var ImageFile = new Class({
9393

9494
addToCache: function ()
9595
{
96-
this.cache.addImage(this.key, this.data);
96+
var texture = this.cache.addImage(this.key, this.data);
9797

98-
this.loader.emit('filecomplete', this.key, this);
98+
this.pendingDestroy(texture);
9999
}
100100

101101
});

0 commit comments

Comments
 (0)