-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathyyASync.js
More file actions
423 lines (364 loc) · 13.6 KB
/
yyASync.js
File metadata and controls
423 lines (364 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// **********************************************************************************************************************
//
// Copyright (c)2011, YoYo Games Ltd. All Rights reserved.
//
// File: yyASync.js
// Created: 02/08/2011
// Author: Mike
// Project: HTML5
// Description: Used to load images, sounds and files ASync across the web.
//
// Date Version BY Comment
// ----------------------------------------------------------------------------------------------------------------------
// 02/08/2011 V1.0 MJD 1st version
//
// **********************************************************************************************************************
var ASYNC_UNKNOWN = 0,
ASYNC_IMAGE = 1,
ASYNC_SPRITE = 2,
ASYNC_BACKGROUND = 3,
ASYNC_SOUND = 4,
ASYNC_WEB = 5,
ASYNC_USER = 6,
ASYNC_BINARY = 7,
ASYNC_NETWORKING = 8,
ASYNC_AUDIO_PLAYBACK = 9,
ASYNC_SYSTEM_EVENT = 10, // device discovery/loss, user login
ASYNC_AUDIO_PLAYBACK_ENDED = 11,
ASYNC_STATUS_NONE=0,
ASYNC_STATUS_LOADED=1,
ASYNC_STATUS_ERROR=-1,
ASYNC_WEB_STATUS_LOADED=0,
ASYNC_WEB_STATUS_LOADING=1,
ASYNC_WEB_STATUS_ERROR=-1;
var g_AsyncLookup_obj = [];
var g_AsyncLookup_data = [];
function AsyncAlloc(_obj, _data) {
g_AsyncLookup_obj.push(_obj);
g_AsyncLookup_data.push(_data);
}
function AsyncAlloc_pop(_obj) {
var i=0;
for (i = 0; i < g_AsyncLookup_obj.length; i++)
{
if (g_AsyncLookup_obj[i] == _obj)
{
var pFile = g_AsyncLookup_data[i];
g_AsyncLookup_data.splice(i, 1);
g_AsyncLookup_obj.splice(i, 1);
return pFile;
}
}
return undefined;
}
// #############################################################################################
/// Function:<summary>
/// An ASync node
/// </summary>
///
/// Out: <returns>
///
/// </returns>
// #############################################################################################
/** @constructor */
function yyASyncNode() {
this.m_Name = "";
this.m_ID = -1;
this.m_Status = 0;
this.m_pObject = null;
this.m_Type = ASYNC_UNKNOWN;
this.m_Complete = false;
}
// #############################################################################################
/// Function:<summary>
/// IMAGE load callback
/// </summary>
///
/// In: <param name="_event"></param>
/// Out: <returns>
///
/// </returns>
// #############################################################################################
function ASync_ImageLoad_Callback(_event) {
var pTPE,pTexture,pFile = AsyncAlloc_pop(_event.currentTarget); // _event.currentTarget.GameMakerASyncLoad;
//var pFile = _event.currentTarget.GameMakerASyncLoad;
if( !pFile ) return;
pFile.m_Complete = true;
pFile.m_Status = ASYNC_STATUS_LOADED;
// @if feature("sprites")
if( pFile.m_Type == ASYNC_SPRITE )
{
// Now actually UPDATE the sprite and TPage stuff.
var pSpr = g_pSpriteManager.Get( pFile.m_ID );
if (pSpr == undefined) { pFile.m_Status = ASYNC_STATUS_ERROR; return; }
if (pSpr === null) { pFile.m_Status = ASYNC_STATUS_ERROR; return; }
if (!pSpr.ppTPE) { pFile.m_Status = ASYNC_STATUS_ERROR; return; }
if (!pSpr.ppTPE[0]) { pFile.m_Status = ASYNC_STATUS_ERROR; return; }
if (!pSpr.ppTPE[0].texture) { pFile.m_Status = ASYNC_STATUS_ERROR; return; }
pTexture = pSpr.ppTPE[0].texture;
pTexture.webgl_textureid=undefined;
var w = pTexture.width;
var h = pTexture.height;
var sprw = Math.floor(w/pSpr.numb);
var x = 0;
pSpr.width = sprw;
pSpr.height = h;
pSpr.bbox.right = sprw;
pSpr.bbox.bottom = h;
pSpr.CalcCullRadius();
for(var i=0;i<pSpr.numb;i++)
{
pTPE = pSpr.ppTPE[i];
pTPE.x = x;
pTPE.w = sprw;
pTPE.h = h;
pTPE.CropWidth = pTPE.w;
pTPE.CropHeight = pTPE.h;
pTPE.ow = pTPE.w;
pTPE.oh = pTPE.h;
pTPE.cache = [];
pTPE.count = 0;
pTPE.maxcache = 4;
x+=sprw;
}
if (g_webGL)
{
if (pSpr.prefetchOnLoad != undefined)
{
if (pSpr.prefetchOnLoad == true)
{
// We need to create the GL texture here and set it up
for(var i = 0; i < pSpr.numb;i++)
{
WebGL_BindTexture(pSpr.ppTPE[i]);
// It should only be necessary to do this for the first one
// as they all share the same texture, but this is safer in case
// things change in the future
if (pSpr.ppTPE[i].texture.webgl_textureid)
{
WebGL_RecreateTexture(pTPE.texture.webgl_textureid);
}
}
}
}
}
return;
}
// @endif sprites
if( pFile.m_Type == ASYNC_BACKGROUND )
{
// Now actually UPDATE the sprite and TPage stuff.
var pBack = g_pBackgroundManager.GetImage( pFile.m_ID );
if( pBack === null ) return;
pTPE =pBack.TPEntry;
pTexture = pTPE.texture;
pTexture.webgl_textureid=undefined;
pTPE.w = pTexture.width;
pTPE.h = pTexture.height;
pTPE.CropWidth = pTPE.w;
pTPE.CropHeight = pTPE.h;
pTPE.ow = pTPE.w;
pTPE.oh = pTPE.h;
pTPE.cache = [];
pTPE.count = 0;
pTPE.maxcache = 4;
return;
}
if (pFile.m_Type == ASYNC_SOUND)
{
pFile.m_pObject.complete = true;
var pSnd = g_pSoundManager.Get(pFile.m_ID);
pSnd.AddSound( g_RawSounds[pFile.m_Name] );
return;
}
if (pFile.m_Type == ASYNC_BINARY)
{
var X = pFile.m_pObject;
if( X.status!=200 ){
pFile.m_Status = X.status;
}else{
var pBuff = g_BufferStorage.Get(pFile.m_pObject.ms_buffer);
var offset = pFile.m_pObject.ms_offset;
var doresize=false;
var size = pFile.m_pObject.ms_size;
var arrayBuffer = pFile.m_pObject.response; // Note: not oReq.responseText
if (arrayBuffer)
{
var byteArray = new Uint8Array(arrayBuffer);
var _type =0;
if( size==-1 ){
size = byteArray.byteLength;
// on a -1, native copies in the WHOLE file, allowing the buffer to grow to fit
doresize=true;
_type = pBuff.m_Type;
pBuff.m_Type = eBuffer_Format_Grow;
}
if( size>byteArray.byteLength ) size = byteArray.byteLength;
var CurrentIndex = buffer_tell(pFile.m_pObject.ms_buffer);
pBuff.yyb_seek(eBuffer_Start,offset);
for (var i = 0; i < byteArray.byteLength; i++) {
var a = byteArray[i];
pBuff.yyb_write(eBuffer_U8, a);
}
pBuff.yyb_seek(eBuffer_Start,CurrentIndex);
if(doresize) pBuff.m_Type = _type;
}
}
return;
}
}
// #############################################################################################
/// Function:<summary>
/// IMAGE load error callback
/// </summary>
///
/// In: <param name="_event"></param>
/// Out: <returns>
///
/// </returns>
// #############################################################################################
function ASync_ImageLoad_Error_Callback(_event)
{
var pFile = AsyncAlloc_pop(_event.currentTarget); // _event.currentTarget.GameMakerASyncLoad;
if( !pFile ) return;
pFile.m_Complete = true;
pFile.m_Status = ASYNC_STATUS_ERROR;
}
// #############################################################################################
/// Function:<summary>
///
/// </summary>
///
/// Out: <returns>
///
/// </returns>
// #############################################################################################
/** @constructor */
function yyASyncManager() {
this.queue = [];
this.queueLength = 0;
}
// #############################################################################################
/// Function: <summary>
/// Get the pool
/// </summary>
// #############################################################################################
yyASyncManager.prototype.Add = function (_id, _filename, _type, _object) {
// Add a new file to "watch"
var pFile = new yyASyncNode();
pFile.m_ID = _id;
pFile.m_Name = _filename;
pFile.m_pObject = _object;
pFile.m_Type = _type;
this.queue[this.queueLength++] = pFile;
AsyncAlloc(_object , pFile );
//_object.GameMakerASyncLoad = pFile;
return pFile;
};
// #############################################################################################
/// Function: <summary>
/// Get the pool
/// </summary>
// #############################################################################################
yyASyncManager.prototype.Process = function () {
var map = ds_map_create();
g_pBuiltIn.async_load = map;
var queue = this.queue;
for (var i = 0; i < this.queueLength; i++)
{
var pFile = queue[i];
if (pFile !== null)
{
if (pFile.m_Complete)
{
ds_map_clear(map);
//
if (pFile.m_Type == ASYNC_NETWORKING)
{
// (all async_load fields are already filled out in m_pObject)
var pObject = pFile.m_pObject;
for (var prop in pObject) {
if (pObject.hasOwnProperty(prop)) {
ds_map_add(map, prop, pObject[prop]);
}
}
} else if (pFile.m_Type == ASYNC_BINARY)
{
ds_map_add(map, "filename", pFile.m_Name);
ds_map_add(map, "url", pFile.m_Name);
} else if (pFile.m_Type == ASYNC_WEB)
{
ds_map_add(map, "filename", "");
ds_map_add(map, "url", pFile.m_Name);
ds_map_add(map, "result", pFile.m_Data);
ds_map_add(map, "http_status", pFile.m_http_status);
ds_map_add(map, "response_headers", pFile.m_ResponseHeadersMap);
} else if (pFile.m_Type == ASYNC_USER)
{
ds_map_add(map, "username", pFile.username);
ds_map_add(map, "password", pFile.password);
ds_map_add(map, "url", "");
ds_map_add(map, "result", pFile.result);
ds_map_add(map, "value", pFile.value);
ds_map_add(map, "http_status", 0);
}
// @if feature("audio")
else if (pFile.m_Type == ASYNC_AUDIO_PLAYBACK) {
ds_map_add(map, "queue_id", pFile.queue_id);
ds_map_add(map, "buffer_id", pFile.buffer_id);
ds_map_add(map, "queue_shutdown", pFile.queue_shutdown);
} else if (pFile.m_Type == ASYNC_AUDIO_PLAYBACK_ENDED) {
ds_map_add(map, "sound_id", pFile.voiceHandle);
ds_map_add(map, "asset_id", pFile.assetIndex);
ds_map_add(map, "was_stopped", pFile.wasStopped);
}
// @endif audio
else if (pFile.m_Type == ASYNC_SYSTEM_EVENT) {
ds_map_add(map, "event_type", pFile.event_type);
ds_map_add(map, "pad_index", pFile.pad_index);
}else{
ds_map_add(map, "url", "");
ds_map_add(map, "result", "");
ds_map_add(map, "http_status", 0);
ds_map_add(map, "filename", pFile.m_Name);
}
if (pFile.m_Type != ASYNC_NETWORKING)
{
ds_map_add(map, "id", pFile.m_ID);
ds_map_add(map, "status", pFile.m_Status);
}
if (pFile.m_Type == ASYNC_IMAGE) g_pObjectManager.ThrowEvent(EVENT_OTHER_WEB_IMAGE_LOAD, 0, true); // Throw an event for the image
// @if feature("sprites")
else if (pFile.m_Type == ASYNC_SPRITE) g_pObjectManager.ThrowEvent(EVENT_OTHER_WEB_IMAGE_LOAD, 0, true); // Throw an event for the image
// @endif sprites
else if (pFile.m_Type == ASYNC_BACKGROUND) g_pObjectManager.ThrowEvent(EVENT_OTHER_WEB_IMAGE_LOAD, 0, true); // Throw an event for the image
else if (pFile.m_Type == ASYNC_SOUND) g_pObjectManager.ThrowEvent(EVENT_OTHER_WEB_SOUND_LOAD, 0, true);
else if (pFile.m_Type == ASYNC_WEB) g_pObjectManager.ThrowEvent(EVENT_OTHER_WEB_ASYNC, 0, true);
else if (pFile.m_Type == ASYNC_USER) g_pObjectManager.ThrowEvent(EVENT_OTHER_WEB_USER_INTERACTION, 0, true);
else if (pFile.m_Type == ASYNC_BINARY) g_pObjectManager.ThrowEvent(EVENT_OTHER_ASYNC_SAVE_LOAD, 0, true);
else if (pFile.m_Type == ASYNC_NETWORKING) g_pObjectManager.ThrowEvent(EVENT_OTHER_NETWORKING, 0, true);
// @if feature("audio")
else if (pFile.m_Type == ASYNC_AUDIO_PLAYBACK) g_pObjectManager.ThrowEvent(EVENT_OTHER_AUDIO_PLAYBACK, 0, true);
else if (pFile.m_Type == ASYNC_AUDIO_PLAYBACK_ENDED) g_pObjectManager.ThrowEvent(EVENT_OTHER_AUDIO_PLAYBACK_ENDED, 0, true);
// @endif audio
else if (pFile.m_Type == ASYNC_SYSTEM_EVENT) g_pObjectManager.ThrowEvent(EVENT_OTHER_SYSTEM_EVENT, 0, true);
// Done load, so delete handle.
//this.queue[i] = null;
this.queue.splice(i,1);
i--;
this.queueLength--;
// Web specifically needs to kill the ds_map used for response headers
if (pFile.m_Type == ASYNC_WEB) {
ds_map_destroy(pFile.m_ResponseHeadersMap);
}
// Networking data event needs to get rid of the temporary buffer
if (pFile.m_Type == ASYNC_NETWORKING && pFile.m_pObject.type == NETWORK_TYPE_DATA) {
buffer_delete(pFile.m_pObject.buffer);
}
}
}
}
//this.queueLength = 0;
ds_map_destroy(map);
g_pBuiltIn.async_load = -1;
};