Skip to content

Commit 7e0b04d

Browse files
committed
You can now specify Loader settings (baseURL, path, xhr settings, etc) in either the Game Config, the Scene Config or a File Config. Game config is used as the defaults. Scene config overrides those, and a File config overrides the Scene config. This fixes phaserjs#3168.
1 parent faf89ea commit 7e0b04d

4 files changed

Lines changed: 34 additions & 14 deletions

File tree

src/boot/Config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ var Config = new Class({
182182
this.physics = GetValue(config, 'physics', {});
183183
this.defaultPhysicsSystem = GetValue(this.physics, 'default', false);
184184

185+
// Loader Defaults
186+
this.loaderBaseURL = GetValue(config, 'loader.baseURL', '');
187+
this.loaderPath = GetValue(config, 'loader.path', '');
188+
this.loaderEnableParallel = GetValue(config, 'loader.enableParallel', true);
189+
this.loaderMaxParallelDownloads = GetValue(config, 'loader.maxParallelDownloads', 4);
190+
this.loaderCrossOrigin = GetValue(config, 'loader.crossOrigin', undefined);
191+
this.loaderResponseType = GetValue(config, 'loader.responseType', '');
192+
this.loaderAsync = GetValue(config, 'loader.async', true);
193+
this.loaderUser = GetValue(config, 'loader.user', '');
194+
this.loaderPassword = GetValue(config, 'loader.password', '');
195+
this.loaderTimeout = GetValue(config, 'loader.timeout', 0);
196+
185197
// Scene Plugins
186198
this.defaultPlugins = GetValue(config, 'plugins', DefaultScenePlugins);
187199

src/loader/File.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ var File = new Class({
8181
this.xhrLoader.onprogress = undefined;
8282
},
8383

84-
// Called when the Image loads
85-
// ProgressEvent
84+
// Called when the file loads, is sent a DOM ProgressEvent
8685
onLoad: function (event)
8786
{
8887
this.resetXHR();

src/loader/LoaderPlugin.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var CONST = require('./const');
33
var CustomSet = require('../structs/Set');
44
var EventEmitter = require('eventemitter3');
55
var FileTypesManager = require('./FileTypesManager');
6+
var GetFastValue = require('../utils/object/GetFastValue');
67
var ParseXMLBitmapFont = require('../gameobjects/bitmaptext/ParseXMLBitmapFont');
78
var PluginManager = require('../plugins/PluginManager');
89
var XHRSettings = require('./XHRSettings');
@@ -33,17 +34,25 @@ var LoaderPlugin = new Class({
3334
// Inject the available filetypes into the Loader
3435
FileTypesManager.install(this);
3536

36-
this.baseURL = '';
37-
this.path = '';
37+
var sceneConfig = this.systems.settings.loader;
38+
var gameConfig = this.systems.game.config;
39+
40+
this.baseURL = GetFastValue(sceneConfig, 'baseURL', gameConfig.loaderBaseURL);
41+
this.path = GetFastValue(sceneConfig, 'path', gameConfig.loaderPath);
3842

39-
// Read from Game / Scene Config
40-
this.enableParallel = true;
41-
this.maxParallelDownloads = 4;
43+
this.enableParallel = GetFastValue(sceneConfig, 'enableParallel', gameConfig.loaderEnableParallel);
44+
this.maxParallelDownloads = GetFastValue(sceneConfig, 'maxParallelDownloads', gameConfig.loaderMaxParallelDownloads);
4245

4346
// xhr specific global settings (can be overridden on a per-file basis)
44-
this.xhr = XHRSettings();
47+
this.xhr = XHRSettings(
48+
GetFastValue(sceneConfig, 'responseType', gameConfig.loaderResponseType),
49+
GetFastValue(sceneConfig, 'async', gameConfig.loaderAsync),
50+
GetFastValue(sceneConfig, 'user', gameConfig.loaderUser),
51+
GetFastValue(sceneConfig, 'password', gameConfig.loaderPassword),
52+
GetFastValue(sceneConfig, 'timeout', gameConfig.loaderTimeout)
53+
);
4554

46-
this.crossOrigin = undefined;
55+
this.crossOrigin = GetFastValue(sceneConfig, 'crossOrigin', gameConfig.loaderCrossOrigin);
4756

4857
this.totalToLoad = 0;
4958
this.progress = 0;
@@ -59,10 +68,6 @@ var LoaderPlugin = new Class({
5968

6069
boot: function ()
6170
{
62-
// Set values from scene / game configs
63-
64-
65-
6671
var eventEmitter = this.systems.events;
6772

6873
eventEmitter.on('shutdown', this.shutdown, this);
@@ -193,7 +198,7 @@ var LoaderPlugin = new Class({
193198
file.crossOrigin = this.crossOrigin;
194199
}
195200

196-
file.load(this.nextFile.bind(this), this.baseURL);
201+
file.load(this.nextFile.bind(this), this.baseURL, this.xhr);
197202
},
198203

199204
nextFile: function (previousFile, success)

src/scene/Settings.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ var Settings = {
4747

4848
physics: GetValue(config, 'physics', {}),
4949

50+
// Loader
51+
52+
loader: GetValue(config, 'loader', {}),
53+
5054
// Plugins
5155

5256
plugins: GetValue(config, 'plugins', false),

0 commit comments

Comments
 (0)