Skip to content

Commit db515d8

Browse files
committed
All FileTypes now register themselves with the FileTypesManager, allowing devs to specify which loaders are included in their builds.
1 parent 676c5be commit db515d8

21 files changed

Lines changed: 323 additions & 108 deletions

src/loader/Loader.js

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,9 @@
11
var BaseLoader = require('./BaseLoader');
22
var Class = require('../utils/Class');
33
var CONST = require('./const');
4+
var FileTypesManager = require('./FileTypesManager');
45
var NumberArray = require('../utils/array/NumberArray');
56
var PluginManager = require('../plugins/PluginManager');
6-
var TILEMAP_FORMATS = require('../gameobjects/tilemap/Formats');
7-
8-
// TODO - Injection mapped
9-
var AnimationJSONFile = require('./filetypes/AnimationJSONFile');
10-
var AtlasJSONFile = require('./filetypes/AtlasJSONFile');
11-
var AudioFile = require('./filetypes/AudioFile');
12-
var BinaryFile = require('./filetypes/BinaryFile');
13-
var BitmapFontFile = require('./filetypes/BitmapFontFile');
14-
var GLSLFile = require('./filetypes/GLSLFile');
15-
var HTMLFile = require('./filetypes/HTMLFile');
16-
var ImageFile = require('./filetypes/ImageFile');
17-
var JSONFile = require('./filetypes/JSONFile');
18-
var PluginFile = require('./filetypes/PluginFile');
19-
var ScriptFile = require('./filetypes/ScriptFile');
20-
var SpriteSheet = require('./filetypes/SpriteSheet');
21-
var SVGFile = require('./filetypes/SVGFile');
22-
var TextFile = require('./filetypes/TextFile');
23-
var TilemapCSVFile = require('./filetypes/TilemapCSVFile');
24-
var TilemapJSONFile = require('./filetypes/TilemapJSONFile');
25-
var UnityAtlasFile = require('./filetypes/UnityAtlasFile');
26-
var WavefrontFile = require('./filetypes/WavefrontFile');
27-
var XMLFile = require('./filetypes/XMLFile');
287

298
var Loader = new Class({
309

@@ -44,6 +23,9 @@ var Loader = new Class({
4423
}
4524

4625
this._multilist = {};
26+
27+
// Inject the available filetypes into the Loader
28+
FileTypesManager.install(this);
4729
},
4830

4931
boot: function ()
@@ -56,6 +38,7 @@ var Loader = new Class({
5638

5739
// key can be either a string, an object or an array of objects
5840

41+
/*
5942
image: function (key, url, xhrSettings)
6043
{
6144
return ImageFile.create(this, key, url, xhrSettings);
@@ -86,6 +69,7 @@ var Loader = new Class({
8669
return XMLFile.create(this, key, url, xhrSettings);
8770
},
8871
72+
/*
8973
binary: function (key, url, xhrSettings)
9074
{
9175
return BinaryFile.create(this, key, url, xhrSettings);
@@ -121,8 +105,10 @@ var Loader = new Class({
121105
{
122106
return SpriteSheet.create(this, key, url, config, xhrSettings);
123107
},
108+
*/
124109

125110
// config can include: instances
111+
/*
126112
audio: function (key, urls, config, xhrSettings)
127113
{
128114
var audioFile = AudioFile.create(this, key, urls, config, xhrSettings);
@@ -134,7 +120,9 @@ var Loader = new Class({
134120
135121
return this;
136122
},
123+
*/
137124

125+
/*
138126
tilemapCSV: function (key, url, xhrSettings)
139127
{
140128
return TilemapCSVFile.create(this, key, url, TILEMAP_FORMATS.CSV, xhrSettings);
@@ -149,6 +137,7 @@ var Loader = new Class({
149137
{
150138
return TilemapJSONFile.create(this, key, url, TILEMAP_FORMATS.WELTMEISTER, xhrSettings);
151139
},
140+
*/
152141

153142
// ---------------------------------------------------
154143
// Multi-File Loaders
@@ -192,6 +181,7 @@ var Loader = new Class({
192181
return this;
193182
},
194183

184+
/*
195185
unityAtlas: function (key, textureURL, atlasURL, textureXhrSettings, atlasXhrSettings)
196186
{
197187
// Returns an object with two properties: 'texture' and 'data'
@@ -224,6 +214,7 @@ var Loader = new Class({
224214
225215
return this;
226216
},
217+
*/
227218

228219
multiatlas: function (key, textureURLs, atlasURLs, textureXhrSettings, atlasXhrSettings)
229220
{
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
var FileTypesManager = require('../FileTypesManager');
12
var JSONFile = require('./JSONFile.js');
23

4+
// Phaser.Loader.FileTypes.AnimationJSONFile
5+
36
var AnimationJSONFile = function (key, url, path, xhrSettings)
47
{
58
var json = new JSONFile(key, url, path, xhrSettings);
@@ -10,23 +13,29 @@ var AnimationJSONFile = function (key, url, path, xhrSettings)
1013
return json;
1114
};
1215

13-
AnimationJSONFile.create = function (loader, key, url, xhrSettings)
16+
// When registering a factory function 'this' refers to the Loader context.
17+
//
18+
// There are several properties available to use:
19+
//
20+
// this.scene - a reference to the Scene that owns the GameObjectFactory
21+
22+
FileTypesManager.register('animation', function (key, url, xhrSettings)
1423
{
1524
if (Array.isArray(key))
1625
{
1726
for (var i = 0; i < key.length; i++)
1827
{
1928
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
20-
loader.addFile(new AnimationJSONFile(key[i], url, loader.path, xhrSettings));
29+
this.addFile(new AnimationJSONFile(key[i], url, this.path, xhrSettings));
2130
}
2231
}
2332
else
2433
{
25-
loader.addFile(new AnimationJSONFile(key, url, loader.path, xhrSettings));
34+
this.addFile(new AnimationJSONFile(key, url, this.path, xhrSettings));
2635
}
2736

2837
// For method chaining
29-
return loader;
30-
};
38+
return this;
39+
});
3140

3241
module.exports = AnimationJSONFile;

src/loader/filetypes/AtlasJSONFile.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
var FileTypesManager = require('../FileTypesManager');
12
var ImageFile = require('./ImageFile.js');
23
var JSONFile = require('./JSONFile.js');
34

5+
// Phaser.Loader.FileTypes.AtlasJSONFile
6+
47
var AtlasJSONFile = function (key, textureURL, atlasURL, path, textureXhrSettings, atlasXhrSettings)
58
{
69
var image = new ImageFile(key, textureURL, path, textureXhrSettings);
@@ -17,4 +20,15 @@ var AtlasJSONFile = function (key, textureURL, atlasURL, path, textureXhrSetting
1720
return { texture: image, data: data };
1821
};
1922

23+
FileTypesManager.register('atlas', function (key, textureURL, atlasURL, textureXhrSettings, atlasXhrSettings)
24+
{
25+
// Returns an object with two properties: 'texture' and 'data'
26+
var files = new AtlasJSONFile(key, textureURL, atlasURL, this.path, textureXhrSettings, atlasXhrSettings);
27+
28+
this.addFile(files.texture);
29+
this.addFile(files.data);
30+
31+
return this;
32+
});
33+
2034
module.exports = AtlasJSONFile;

src/loader/filetypes/AudioFile.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var Class = require('../../utils/Class');
22
var File = require('../File');
3+
var FileTypesManager = require('../FileTypesManager');
34
var GetFastValue = require('../../utils/object/GetFastValue');
45
var CONST = require('../../const');
56
var HTML5AudioFile = require('./HTML5AudioFile');
@@ -58,33 +59,50 @@ var AudioFile = new Class({
5859

5960
});
6061

61-
AudioFile.create = function (loader, key, urls, config, xhrSettings)
62+
// When registering a factory function 'this' refers to the Loader context.
63+
//
64+
// There are several properties available to use:
65+
//
66+
// this.scene - a reference to the Scene that owns the GameObjectFactory
67+
68+
FileTypesManager.register('audio', function (key, urls, config, xhrSettings)
6269
{
63-
var game = loader.scene.game;
70+
var game = this.systems.game;
6471
var audioConfig = game.config.audio;
6572
var deviceAudio = game.device.Audio;
6673

6774
if ((audioConfig && audioConfig.noAudio) || (!deviceAudio.webAudio && !deviceAudio.audioData))
6875
{
6976
console.info('Skipping loading audio \'' + key + '\' since sounds are disabled.');
70-
return null;
77+
return this;
7178
}
7279

7380
var url = AudioFile.findAudioURL(game, urls);
7481

7582
if (!url)
7683
{
7784
console.warn('No supported url provided for audio \'' + key + '\'!');
78-
return null;
85+
return this;
7986
}
8087

81-
if(deviceAudio.webAudio && !(audioConfig && audioConfig.disableWebAudio))
88+
var audioFile;
89+
90+
if (deviceAudio.webAudio && !(audioConfig && audioConfig.disableWebAudio))
8291
{
83-
return new AudioFile(key, url, loader.path, xhrSettings, game.sound.context);
92+
audioFile = new AudioFile(key, url, this.path, xhrSettings, game.sound.context);
93+
}
94+
else
95+
{
96+
audioFile = new HTML5AudioFile(key, url, this.path, config);
8497
}
8598

86-
return new HTML5AudioFile(key, url, loader.path, config);
87-
};
99+
if (audioFile)
100+
{
101+
this.addFile(audioFile);
102+
}
103+
104+
return this;
105+
});
88106

89107
// this.load.audio('sound', 'assets/audio/booom.ogg', config, xhrSettings);
90108
//
@@ -129,6 +147,7 @@ AudioFile.create = function (loader, key, urls, config, xhrSettings)
129147
// }
130148
// ],
131149
// config, xhrSettings);
150+
132151
AudioFile.findAudioURL = function (game, urls)
133152
{
134153
if (urls.constructor !== Array)

src/loader/filetypes/BinaryFile.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Class = require('../../utils/Class');
22
var CONST = require('../const');
33
var File = require('../File');
4+
var FileTypesManager = require('../FileTypesManager');
45
var GetFastValue = require('../../utils/object/GetFastValue');
56

67
// Phaser.Loader.FileTypes.BinaryFile
@@ -41,23 +42,29 @@ var BinaryFile = new Class({
4142

4243
});
4344

44-
BinaryFile.create = function (loader, key, url, xhrSettings)
45+
// When registering a factory function 'this' refers to the Loader context.
46+
//
47+
// There are several properties available to use:
48+
//
49+
// this.scene - a reference to the Scene that owns the GameObjectFactory
50+
51+
FileTypesManager.register('binary', function (key, url, xhrSettings)
4552
{
4653
if (Array.isArray(key))
4754
{
4855
for (var i = 0; i < key.length; i++)
4956
{
5057
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
51-
loader.addFile(new BinaryFile(key[i], url, loader.path, xhrSettings));
58+
this.addFile(new BinaryFile(key[i], url, this.path, xhrSettings));
5259
}
5360
}
5461
else
5562
{
56-
loader.addFile(new BinaryFile(key, url, loader.path, xhrSettings));
63+
this.addFile(new BinaryFile(key, url, this.path, xhrSettings));
5764
}
5865

5966
// For method chaining
60-
return loader;
61-
};
67+
return this;
68+
});
6269

6370
module.exports = BinaryFile;

src/loader/filetypes/BitmapFontFile.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var FileTypesManager = require('../FileTypesManager');
12
var ImageFile = require('./ImageFile.js');
23
var XMLFile = require('./XMLFile.js');
34

@@ -17,4 +18,21 @@ var BitmapFontFile = function (key, textureURL, xmlURL, path, textureXhrSettings
1718
return { texture: image, data: data };
1819
};
1920

21+
// When registering a factory function 'this' refers to the Loader context.
22+
//
23+
// There are several properties available to use:
24+
//
25+
// this.scene - a reference to the Scene that owns the GameObjectFactory
26+
27+
FileTypesManager.register('bitmapFont', function (key, textureURL, xmlURL, textureXhrSettings, xmlXhrSettings)
28+
{
29+
// Returns an object with two properties: 'texture' and 'data'
30+
var files = new BitmapFontFile(key, textureURL, xmlURL, this.path, textureXhrSettings, xmlXhrSettings);
31+
32+
this.addFile(files.texture);
33+
this.addFile(files.data);
34+
35+
return this;
36+
});
37+
2038
module.exports = BitmapFontFile;

src/loader/filetypes/GLSLFile.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Class = require('../../utils/Class');
22
var CONST = require('../const');
33
var File = require('../File');
4+
var FileTypesManager = require('../FileTypesManager');
45
var GetFastValue = require('../../utils/object/GetFastValue');
56

67
// Phaser.Loader.FileTypes.GLSLFile
@@ -41,23 +42,29 @@ var GLSLFile = new Class({
4142

4243
});
4344

44-
GLSLFile.create = function (loader, key, url, xhrSettings)
45+
// When registering a factory function 'this' refers to the Loader context.
46+
//
47+
// There are several properties available to use:
48+
//
49+
// this.scene - a reference to the Scene that owns the GameObjectFactory
50+
51+
FileTypesManager.register('glsl', function (key, url, xhrSettings)
4552
{
4653
if (Array.isArray(key))
4754
{
4855
for (var i = 0; i < key.length; i++)
4956
{
5057
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
51-
loader.addFile(new GLSLFile(key[i], url, loader.path, xhrSettings));
58+
this.addFile(new GLSLFile(key[i], url, this.path, xhrSettings));
5259
}
5360
}
5461
else
5562
{
56-
loader.addFile(new GLSLFile(key, url, loader.path, xhrSettings));
63+
this.addFile(new GLSLFile(key, url, this.path, xhrSettings));
5764
}
5865

5966
// For method chaining
60-
return loader;
61-
};
67+
return this;
68+
});
6269

6370
module.exports = GLSLFile;

0 commit comments

Comments
 (0)