Skip to content

Commit de7568a

Browse files
committed
Merge branch 'master' of https://github.com/photonstorm/phaser
2 parents e85c66a + 0f0f099 commit de7568a

21 files changed

Lines changed: 257 additions & 151 deletions

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,19 @@
2424
* TextureSource will now remove its respective WebGLTexture from the renderer when destroyed.
2525
* TextureSource will now automatically create a glTexture from its canvas if using one.
2626
* WebGLRenderer will now remove a GL texture from its local `nativeTextures` array when you call the `deleteTexture` method.
27+
* The BaseCache has a new method `exists` that will return a boolean if an entry for the given key exists in the cache or not.
28+
* Loader.File has a new argument in its constructor which is an instance of the LoaderPlugin. It stores this in the `loader` property. It also has a new property `cache` which is a reference to the cache that the file type will be stored in.
29+
* Loader.File has a new method `hasCacheConflict` which checks if a key matching the one used by this file exists in the target Cache or not.
30+
* Loader.File has a new method `addToCache` which will add the file to its target cache and then emit a `filecomplete` event, passing its key and a reference to itself to the listener.
31+
* LoaderPlugin has a new property `cacheManager` which is a reference to the global game cache and is used by the File Types.
32+
* LoaderPlugin has a new property `textureManager` which is a reference to the global Texture Manager and is used by the File Types.
33+
* LoaderPlugin will now check to see if loading a file would cache a cache conflict or not, and prevent it if it will.
34+
* LoaderPlugin now passes off processing of the final file data to the file itself, which will now self-add itself to its target cache.
2735

2836
### Bug Fixes
2937

3038
* DataManagerPlugin would throw an error on Game.destroy if you had any Scenes in the Scene Manager had not been run. Fix #3596 (thanks @kuoruan)
39+
* If you created a Game with no Scenes defined, and then added one via `Game.scene.add` and passed in a data object, the data would be ignored when starting the Scene.
3140

3241
### Examples, Documentation and TypeScript
3342

src/cache/BaseCache.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ var BaseCache = new Class({
8383

8484
/**
8585
* Checks if this cache contains an item matching the given key.
86+
* This performs the same action as `BaseCache.exists`.
8687
*
8788
* @method Phaser.Cache.BaseCache#has
8889
* @since 3.0.0
@@ -96,6 +97,22 @@ var BaseCache = new Class({
9697
return this.entries.has(key);
9798
},
9899

100+
/**
101+
* Checks if this cache contains an item matching the given key.
102+
* This performs the same action as `BaseCache.has` and is called directly by the Loader.
103+
*
104+
* @method Phaser.Cache.BaseCache#exists
105+
* @since 3.7.0
106+
*
107+
* @param {string} key - The unique key of the item to be checked in this cache.
108+
*
109+
* @return {boolean} Returns `true` if the cache contains an item matching the given key, otherwise `false`.
110+
*/
111+
exists: function (key)
112+
{
113+
return this.entries.has(key);
114+
},
115+
99116
/**
100117
* Gets an item from this cache based on the given key.
101118
*

src/loader/File.js

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,33 @@ var XHRSettings = require('./XHRSettings');
4040
* @constructor
4141
* @since 3.0.0
4242
*
43+
* @param {Phaser.Loader.LoaderPlugin} loader - The Loader that is going to load this File.
4344
* @param {FileConfig} fileConfig - [description]
4445
*/
4546
var File = new Class({
4647

4748
initialize:
4849

49-
function File (fileConfig)
50+
function File (loader, fileConfig)
5051
{
52+
/**
53+
* A reference to the Loader that is going to load this file.
54+
*
55+
* @name Phaser.Loader.File#loader
56+
* @type {Phaser.Loader.LoaderPlugin}
57+
* @since 3.0.0
58+
*/
59+
this.loader = loader;
60+
61+
/**
62+
* A reference to the Cache, or Texture Manager, that is going to store this file if it loads.
63+
*
64+
* @name Phaser.Loader.File#cache
65+
* @type {(Phaser.Cache.BaseCache|Phaser.Textures.TextureManager)}
66+
* @since 3.7.0
67+
*/
68+
this.cache = GetFastValue(fileConfig, 'cache');
69+
5170
/**
5271
* The file type string (image, json, etc) for sorting within the Loader.
5372
*
@@ -112,15 +131,6 @@ var File = new Class({
112131
this.xhrSettings = MergeXHRSettings(this.xhrSettings, GetFastValue(fileConfig, 'xhrSettings', {}));
113132
}
114133

115-
/**
116-
* The LoaderPlugin instance that is loading this file.
117-
*
118-
* @name Phaser.Loader.File#loader
119-
* @type {?Phaser.Loader.LoaderPlugin}
120-
* @since 3.0.0
121-
*/
122-
this.loader = null;
123-
124134
/**
125135
* The XMLHttpRequest instance (as created by XHR Loader) that is loading this File.
126136
*
@@ -275,30 +285,26 @@ var File = new Class({
275285
*
276286
* @method Phaser.Loader.File#load
277287
* @since 3.0.0
278-
*
279-
* @param {Phaser.Loader.LoaderPlugin} loader - The Loader that will load this File.
280288
*/
281-
load: function (loader)
289+
load: function ()
282290
{
283-
this.loader = loader;
284-
285291
if (this.state === CONST.FILE_POPULATED)
286292
{
287293
this.onComplete();
288294

289-
loader.nextFile(this);
295+
this.loader.nextFile(this);
290296
}
291297
else
292298
{
293-
this.src = GetURL(this, loader.baseURL);
299+
this.src = GetURL(this, this.loader.baseURL);
294300

295301
if (this.src.indexOf('data:') === 0)
296302
{
297303
console.warn('Local data URIs are not supported: ' + this.key);
298304
}
299305
else
300306
{
301-
this.xhrLoader = XHRLoader(this, loader.xhr);
307+
this.xhrLoader = XHRLoader(this, this.loader.xhr);
302308
}
303309
}
304310
},
@@ -407,6 +413,36 @@ var File = new Class({
407413
{
408414
this.state = CONST.FILE_COMPLETE;
409415
}
416+
},
417+
418+
/**
419+
* Checks if a key matching the one used by this file exists in the target Cache or not.
420+
* This is called automatically by the LoaderPlugin to decide if the file can be safely
421+
* loaded or will conflict.
422+
*
423+
* @method Phaser.Loader.File#hasCacheConflict
424+
* @since 3.7.0
425+
*
426+
* @return {boolean} `true` if adding this file will cause a conflict, otherwise `false`.
427+
*/
428+
hasCacheConflict: function ()
429+
{
430+
return (this.cache.exists(this.key));
431+
},
432+
433+
/**
434+
* Adds this file to its target cache upon successful loading and processing.
435+
* It will emit a `filecomplete` event from the LoaderPlugin.
436+
* This method is often overridden by specific file types.
437+
*
438+
* @method Phaser.Loader.File#addToCache
439+
* @since 3.7.0
440+
*/
441+
addToCache: function ()
442+
{
443+
this.cache.add(this.key, this.data);
444+
445+
this.loader.emit('filecomplete', this.key, this);
410446
}
411447

412448
});

src/loader/LoaderPlugin.js

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,24 @@ var LoaderPlugin = new Class({
8484
*/
8585
this.systems = scene.sys;
8686

87+
/**
88+
* A reference to the global Cache Manager.
89+
*
90+
* @name Phaser.Loader.LoaderPlugin#cacheManager
91+
* @type {Phaser.Cache.CacheManager}
92+
* @since 3.7.0
93+
*/
94+
this.cacheManager = scene.sys.cache;
95+
96+
/**
97+
* A reference to the global Texture Manager.
98+
*
99+
* @name Phaser.Loader.LoaderPlugin#textureManager
100+
* @type {Phaser.Textures.TextureManager}
101+
* @since 3.7.0
102+
*/
103+
this.textureManager = scene.sys.textures;
104+
87105
/**
88106
* [description]
89107
*
@@ -350,9 +368,13 @@ var LoaderPlugin = new Class({
350368
return -1;
351369
}
352370

353-
file.path = this.path;
371+
// Does the file already exist in the cache or texture manager?
372+
if (!file.hasCacheConflict())
373+
{
374+
file.path = this.path;
354375

355-
this.list.set(file);
376+
this.list.set(file);
377+
}
356378

357379
return file;
358380
},
@@ -721,12 +743,6 @@ var LoaderPlugin = new Class({
721743
animJSON.push(file);
722744
break;
723745

724-
case 'image':
725-
case 'svg':
726-
case 'html':
727-
textures.addImage(file.key, file.data);
728-
break;
729-
730746
case 'atlasjson':
731747

732748
fileA = file.fileA;
@@ -789,34 +805,6 @@ var LoaderPlugin = new Class({
789805
}
790806
break;
791807

792-
case 'spritesheet':
793-
textures.addSpriteSheet(file.key, file.data, file.config);
794-
break;
795-
796-
case 'json':
797-
cache.json.add(file.key, file.data);
798-
break;
799-
800-
case 'xml':
801-
cache.xml.add(file.key, file.data);
802-
break;
803-
804-
case 'text':
805-
cache.text.add(file.key, file.data);
806-
break;
807-
808-
case 'obj':
809-
cache.obj.add(file.key, file.data);
810-
break;
811-
812-
case 'binary':
813-
cache.binary.add(file.key, file.data);
814-
break;
815-
816-
case 'audio':
817-
cache.audio.add(file.key, file.data);
818-
break;
819-
820808
case 'audioSprite':
821809

822810
var files = [ file.fileA, file.fileB ];
@@ -828,14 +816,9 @@ var LoaderPlugin = new Class({
828816

829817
break;
830818

831-
case 'glsl':
832-
cache.shader.add(file.key, file.data);
833-
break;
819+
default:
820+
file.addToCache();
834821

835-
case 'tilemapCSV':
836-
case 'tilemapJSON':
837-
cache.tilemap.add(file.key, { format: file.tilemapFormat, data: file.data });
838-
break;
839822
}
840823
});
841824

@@ -1027,6 +1010,8 @@ var LoaderPlugin = new Class({
10271010

10281011
this.scene = null;
10291012
this.systems = null;
1013+
this.textureManager = null;
1014+
this.cacheManager = null;
10301015
}
10311016

10321017
});

src/loader/filetypes/AnimationJSONFile.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ var JSONFile = require('./JSONFile.js');
2020
*
2121
* @return {Phaser.Loader.FileTypes.JSONFile} A File instance to be added to the Loader.
2222
*/
23-
var AnimationJSONFile = function (key, url, path, xhrSettings)
23+
var AnimationJSONFile = function (loader, key, url, xhrSettings)
2424
{
25-
var json = new JSONFile(key, url, path, xhrSettings);
25+
var json = new JSONFile(loader, key, url, xhrSettings);
2626

2727
// Override the File type
2828
json.type = 'animationJSON';
@@ -55,12 +55,12 @@ FileTypesManager.register('animation', function (key, url, xhrSettings)
5555
for (var i = 0; i < key.length; i++)
5656
{
5757
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
58-
this.addFile(new AnimationJSONFile(key[i], url, this.path, xhrSettings));
58+
this.addFile(new AnimationJSONFile(this, key[i], url, xhrSettings));
5959
}
6060
}
6161
else
6262
{
63-
this.addFile(new AnimationJSONFile(key, url, this.path, xhrSettings));
63+
this.addFile(new AnimationJSONFile(this, key, url, xhrSettings));
6464
}
6565

6666
// For method chaining

src/loader/filetypes/AtlasJSONFile.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ var JSONFile = require('./JSONFile.js');
2323
*
2424
* @return {object} An object containing two File objects to be added to the loader.
2525
*/
26-
var AtlasJSONFile = function (key, textureURL, atlasURL, path, textureXhrSettings, atlasXhrSettings)
26+
var AtlasJSONFile = function (loader, key, textureURL, atlasURL, textureXhrSettings, atlasXhrSettings)
2727
{
28-
var image = new ImageFile(key, textureURL, path, textureXhrSettings);
29-
var data = new JSONFile(key, atlasURL, path, atlasXhrSettings);
28+
var image = new ImageFile(loader, key, textureURL, textureXhrSettings);
29+
var data = new JSONFile(loader, key, atlasURL, atlasXhrSettings);
3030

3131
// Link them together
3232
image.linkFile = data;
@@ -60,20 +60,19 @@ var AtlasJSONFile = function (key, textureURL, atlasURL, path, textureXhrSetting
6060
*/
6161
FileTypesManager.register('atlas', function (key, textureURL, atlasURL, textureXhrSettings, atlasXhrSettings)
6262
{
63-
6463
var files;
6564

6665
// If param key is an object, use object based loading method
6766
if ((typeof key === 'object') && (key !== null))
6867
{
69-
files = new AtlasJSONFile(key.key, key.texture, key.data, this.path, textureXhrSettings, atlasXhrSettings);
68+
files = new AtlasJSONFile(this, key.key, key.texture, key.data, textureXhrSettings, atlasXhrSettings);
7069
}
7170

7271
// Else just use the parameters like normal
7372
else
7473
{
7574
// Returns an object with two properties: 'texture' and 'data'
76-
files = new AtlasJSONFile(key, textureURL, atlasURL, this.path, textureXhrSettings, atlasXhrSettings);
75+
files = new AtlasJSONFile(this, key, textureURL, atlasURL, textureXhrSettings, atlasXhrSettings);
7776
}
7877

7978
this.addFile(files.texture);

src/loader/filetypes/AudioFile.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var AudioFile = new Class({
3333

3434
initialize:
3535

36-
function AudioFile (key, url, path, xhrSettings, audioContext)
36+
function AudioFile (loader, key, url, xhrSettings, audioContext)
3737
{
3838
/**
3939
* [description]
@@ -46,15 +46,16 @@ var AudioFile = new Class({
4646

4747
var fileConfig = {
4848
type: 'audio',
49+
cache: loader.cacheManager.audio,
4950
extension: GetFastValue(url, 'type', ''),
5051
responseType: 'arraybuffer',
5152
key: key,
5253
url: GetFastValue(url, 'uri', url),
53-
path: path,
54+
path: loader.path,
5455
xhrSettings: xhrSettings
5556
};
5657

57-
File.call(this, fileConfig);
58+
File.call(this, loader, fileConfig);
5859
},
5960

6061
/**
@@ -119,11 +120,11 @@ AudioFile.create = function (loader, key, urls, config, xhrSettings)
119120

120121
if (deviceAudio.webAudio && !(audioConfig && audioConfig.disableWebAudio))
121122
{
122-
return new AudioFile(key, url, loader.path, xhrSettings, game.sound.context);
123+
return new AudioFile(loader, key, url, xhrSettings, game.sound.context);
123124
}
124125
else
125126
{
126-
return new HTML5AudioFile(key, url, loader.path, config);
127+
return new HTML5AudioFile(loader, key, url, config);
127128
}
128129
};
129130

0 commit comments

Comments
 (0)