Skip to content

Commit 560b0ca

Browse files
committed
Binary files have a new optional dataType property to cast the data to upon load.
1 parent 1cb68a9 commit 560b0ca

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ The Loader has been given an overhaul to improve its performance and extensibili
2424
* If you used a Scene files payload then the format of the object has changed. It used to be a property in the Scene Config called `files` which was an array of files to load. It has been renamed to `pack` and it's an object that exactly matches the new Pack File format. Please see the loader example `scene files payload.js` for an example. In short, where you had: `files: []` before, just change it to `pack: { files: [] }` and it'll work.
2525
* The Loader now supports Texture Atlases with normal maps. Before it would only support single images loaded with normal maps, but now you can provide them for all the atlas formats (json, xml and Unity)
2626
* The Loader `multiatlas` feature will now automatically load texture normal maps, if specified in the json.
27+
* Binary Files have a new optional `dataType` argument and property which will cast the binary data to that format after load, before inserting it into the cache, i.e.: `load.binary('mod', 'music.mod', Uint8Array)`
2728

2829
### Loader Updates
2930

src/loader/filetypes/BinaryFile.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var IsPlainObject = require('../../utils/object/IsPlainObject');
1818
* @property {string} [url] - The absolute or relative URL to load the file from.
1919
* @property {string} [extension='bin'] - The default file extension to use if no url is provided.
2020
* @property {XHRSettingsObject} [xhrSettings] - Extra XHR Settings specifically for this file.
21+
* @property {any} [dataType] - Optional type to cast the binary file to once loaded. For example, `Uint8Array`.
2122
*/
2223

2324
/**
@@ -38,14 +39,15 @@ var IsPlainObject = require('../../utils/object/IsPlainObject');
3839
* @param {(string|Phaser.Loader.FileTypes.BinaryFileConfig)} key - The key to use for this file, or a file configuration object.
3940
* @param {string} [url] - The absolute or relative URL to load this file from. If undefined or `null` it will be set to `<key>.bin`, i.e. if `key` was "alien" then the URL will be "alien.bin".
4041
* @param {XHRSettingsObject} [xhrSettings] - Extra XHR Settings specifically for this file.
42+
* @param {any} [dataType] - Optional type to cast the binary file to once loaded. For example, `Uint8Array`.
4143
*/
4244
var BinaryFile = new Class({
4345

4446
Extends: File,
4547

4648
initialize:
4749

48-
function BinaryFile (loader, key, url, xhrSettings)
50+
function BinaryFile (loader, key, url, xhrSettings, dataType)
4951
{
5052
var extension = 'bin';
5153

@@ -57,6 +59,7 @@ var BinaryFile = new Class({
5759
url = GetFastValue(config, 'url');
5860
xhrSettings = GetFastValue(config, 'xhrSettings');
5961
extension = GetFastValue(config, 'extension', extension);
62+
dataType = GetFastValue(config, 'dataType', dataType);
6063
}
6164

6265
var fileConfig = {
@@ -66,7 +69,10 @@ var BinaryFile = new Class({
6669
responseType: 'arraybuffer',
6770
key: key,
6871
url: url,
69-
xhrSettings: xhrSettings
72+
xhrSettings: xhrSettings,
73+
config: {
74+
dataType: dataType
75+
}
7076
};
7177

7278
File.call(this, loader, fileConfig);
@@ -83,7 +89,9 @@ var BinaryFile = new Class({
8389
{
8490
this.state = CONST.FILE_PROCESSING;
8591

86-
this.data = this.xhrLoader.response;
92+
var ctor = this.config.dataType;
93+
94+
this.data = (ctor) ? new ctor(this.xhrLoader.response) : this.xhrLoader.response;
8795

8896
this.onProcessComplete();
8997
}
@@ -140,11 +148,12 @@ var BinaryFile = new Class({
140148
*
141149
* @param {(string|Phaser.Loader.FileTypes.BinaryFileConfig|Phaser.Loader.FileTypes.BinaryFileConfig[])} key - The key to use for this file, or a file configuration object, or array of them.
142150
* @param {string} [url] - The absolute or relative URL to load this file from. If undefined or `null` it will be set to `<key>.bin`, i.e. if `key` was "alien" then the URL will be "alien.bin".
151+
* @param {any} [dataType] - Optional type to cast the binary file to once loaded. For example, `Uint8Array`.
143152
* @param {XHRSettingsObject} [xhrSettings] - An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
144153
*
145154
* @return {Phaser.Loader.LoaderPlugin} The Loader instance.
146155
*/
147-
FileTypesManager.register('binary', function (key, url, xhrSettings)
156+
FileTypesManager.register('binary', function (key, url, dataType, xhrSettings)
148157
{
149158
if (Array.isArray(key))
150159
{
@@ -156,7 +165,7 @@ FileTypesManager.register('binary', function (key, url, xhrSettings)
156165
}
157166
else
158167
{
159-
this.addFile(new BinaryFile(this, key, url, xhrSettings));
168+
this.addFile(new BinaryFile(this, key, url, xhrSettings, dataType));
160169
}
161170

162171
return this;

0 commit comments

Comments
 (0)