-
Notifications
You must be signed in to change notification settings - Fork 21
/
serialization.js
executable file
·1140 lines (996 loc) · 32.1 KB
/
serialization.js
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var Sburb = (function(Sburb){
Sburb.loadedFiles = {};
var templateClasses = {};
var loadingDepth = 0;
var loadQueue = [];
var updateLoop = null;
var jsonUpdateLoop = null;
//Save the current state to xml
Sburb.serialize = function(sburbInst) {
var assets = sburbInst.assets;
var effects = sburbInst.effects;
var rooms = sburbInst.rooms;
var sprites = sburbInst.sprites;
var hud = sburbInst.hud;
var dialoger = sburbInst.dialoger;
var curRoom = sburbInst.curRoom;
var gameState = sburbInst.gameState;
var actionQueues = sburbInst.actionQueues;
var char = sburbInst.char;
var loadedFiles = "";
var loadedFilesExist = false;
for(var key in Sburb.loadedFiles) // Not sburbInst.loadedFiles ?
{
if(!Sburb.loadedFiles.hasOwnProperty(key)) continue;
loadedFiles = loadedFiles + (loadedFilesExist?",":"") + key;
loadedFilesExist = true;
}
var out = document.getElementById("serialText");
var output = "<sburb"+
" char='"+char.name+
(Sburb.bgm?"' bgm='"+Sburb.bgm.asset.name+(Sburb.bgm.startLoop?","+Sburb.bgm.startLoop:""):"")+
(Sburb.Stage.scaleX!=1?"' scale='"+Sburb.Stage.scaleX:"")+
(Sburb.nextQueueId>0?"' nextQueueId='"+Sburb.nextQueueId:"")+
(Sburb.assetManager.resourcePath?("' resourcePath='"+Sburb.assetManager.resourcePath):"")+
(Sburb.assetManager.levelPath?("' levelPath='"+Sburb.assetManager.levelPath):"")+
(loadedFilesExist?("' loadedFiles='"+loadedFiles):"")+
"'>\n";
output = serializeAssets(output,assets,effects);
output = serializeTemplates(output,templateClasses);
output = serializeHud(output,hud,dialoger);
output = serializeLooseObjects(output,rooms,sprites);
output = serializeRooms(output,rooms);
output = serializeGameState(output,gameState);
output = serializeActionQueues(output,actionQueues);
output = output.concat("\n</sburb>");
if(out){
out.value = output;
}
return output;
}
///
// Saves state to session or local storage if supported by browser
// Paramters:
// description (String) A descriptive name for the save, i.e. 'Kankri, Second Room'
// auto (Boolean) If true this save is an auto save, false it is not
// local (Boolean) If true, use localStorage, if false, use sessionStorage
// Returns:
// (Boolean) False if storage is not supported by browser, true otherwise
///
Sburb.saveStateToStorage = function(description, auto, local)
{
description = typeof description !== 'undefined' ? description : "SaveFile";
auto = typeof auto !== 'undefined' ? auto : false;
local = typeof local !== 'undefined' ? local : false;
var storage;
if(!Sburb.tests.storage) {
return false;
}
if(Sburb.tests.storage.local && Sburb.tests.storage.session) {
storage = local ? localStorage : sessionStorage;
} else if(Sburb.tests.storage.local) {
storage = localStorage;
} else if(Sburb.tests.storage.session) {
storage = sessionStorage;
} else {
return false;
}
var serialized = Sburb.serialize(Sburb);
compressed = Iuppiter.Base64.encode(Iuppiter.compress(serialized),true);
var saveStateName = description + (auto? " (auto)":"") + '_savedState_' + Sburb.name + ":" + Sburb.version;
Sburb.deleteStateFromStorage(auto);
storage.setItem(saveStateName, compressed);
return true;
}
///
// Loads state from session or local storage if supported by browser
// Paramters:
// auto (Boolean) If true loads the auto save, else it loads the manual save
// local (Boolean) If true, use localStorage, if false, use sessionStorage
// Returns:
// (Boolean) False if storage is not supported by browser or save does not exist, true otherwise
///
Sburb.loadStateFromStorage = function(auto, local)
{
description = typeof description !== 'undefined' ? description : "SaveFile";
auto = typeof auto !== 'undefined' ? auto : false;
local = typeof local !== 'undefined' ? local : false;
var storage;
if(!Sburb.tests.storage) {
return false;
}
if(Sburb.tests.storage.local && Sburb.tests.storage.session) {
storage = local ? localStorage : sessionStorage;
} else if(Sburb.tests.storage.local) {
storage = localStorage;
} else if(Sburb.tests.storage.session) {
storage = sessionStorage;
} else {
return false;
}
var saveStateName = "";
for(var key in storage)
{
if(!storage.hasOwnProperty(key)) continue;
var savedIndex = key.indexOf('_savedState_');
if(savedIndex >= 0) // this key is a saved state
{
if(auto && key.indexOf("(auto)") >= 0 && key.indexOf(Sburb.name + ":" + Sburb.version) >= savedIndex)
{
saveStateName = key;
break;
}
else if(!auto && key.indexOf("(auto)") < 0 && key.indexOf(Sburb.name + ":" + Sburb.version) >= savedIndex)
{
saveStateName = key;
break;
}
}
}
var compressed = storage.getItem(saveStateName);
if(!compressed)
return false;
var decoded = Iuppiter.decompress(Iuppiter.Base64.decode(Iuppiter.toByteArray(compressed),true)).replace(/\0/g,"");
Sburb.loadSerial(decoded);
return true;
}
///
// Gets the descriptive name of the currently saved state
// Paramters:
// auto (Boolean) If true returns the description for the auto save, if false the manual save
// Returns:
// (String) Description of save, returns "" if no description is found or storage is not supported
///
Sburb.getStateDescription = function(auto, local)
{
auto = typeof auto !== 'undefined' ? auto : false;
local = typeof local !== 'undefined' ? local : false;
var storage;
if(!Sburb.tests.storage) {
return null;
}
if(Sburb.tests.storage.local && Sburb.tests.storage.session) {
storage = local ? localStorage : sessionStorage;
} else if(Sburb.tests.storage.local) {
storage = localStorage;
} else if(Sburb.tests.storage.session) {
storage = sessionStorage;
} else {
return null;
}
for(var key in storage)
{
if(!storage.hasOwnProperty(key)) continue;
var savedIndex = key.indexOf('_savedState_');
if(savedIndex >= 0) // this key is a saved state
{
if(auto && key.indexOf("(auto)") >= 0 && key.indexOf(Sburb.name + ":" + Sburb.version) >= savedIndex)
return key.substring(0, savedIndex);
else if(!auto && key.indexOf("(auto)") < 0 && key.indexOf(Sburb.name + ":" + Sburb.version) >= savedIndex)
return key.substring(0, savedIndex);
}
}
return null;
}
///
// Deletes state from session or local storage if supported by browser
// Paramters:
// auto (Boolean) If true, deletes the current auto save, if false deletes the current manual save
// local (Boolean) If true, use localStorage, if false, use sessionStorage
///
Sburb.deleteStateFromStorage = function(auto, local)
{
auto = typeof auto !== 'undefined' ? auto : false;
local = typeof local !== 'undefined' ? local : false;
var storage;
if(!Sburb.tests.storage) {
return;
}
if(Sburb.tests.storage.local && Sburb.tests.storage.session) {
storage = local ? localStorage : sessionStorage;
} else if(Sburb.tests.storage.local) {
storage = localStorage;
} else if(Sburb.tests.storage.session) {
storage = sessionStorage;
} else {
return;
}
Sburb.deleteOldVersionStates(local);
for(var key in storage)
{
if(!storage.hasOwnProperty(key)) continue;
var savedIndex = key.indexOf('_savedState_');
if(savedIndex >= 0) // this key is a saved state
{
if(auto && key.indexOf("(auto)") >= 0 && key.indexOf(Sburb.name + ":" + Sburb.version) >= savedIndex)
storage.removeItem(key);
else if(!auto && key.indexOf("(auto)") < 0 && key.indexOf(Sburb.name + ":" + Sburb.version) >= savedIndex)
storage.removeItem(key);
}
}
}
///
// Deletes any old states from previous versions of the game
// Paramters:
// local (Boolean) If true, use localStorage, if false, use sessionStorage
///
Sburb.deleteOldVersionStates = function(local)
{
local = typeof local !== 'undefined' ? local : false;
var storage;
if(!Sburb.tests.storage) {
return;
}
if(Sburb.tests.storage.local && Sburb.tests.storage.session) {
storage = local ? localStorage : sessionStorage;
} else if(Sburb.tests.storage.local) {
storage = localStorage;
} else if(Sburb.tests.storage.session) {
storage = sessionStorage;
} else {
return;
}
for(var key in storage)
{
if(!storage.hasOwnProperty(key)) continue;
var savedIndex = key.indexOf('_savedState_');
if(savedIndex >= 0) // this key is a saved state
{
// This is a key for our game, but not of the right version
if(key.indexOf(Sburb.name + ":") >= savedIndex && key.indexOf(":" + Sburb.version) < 0)
storage.removeItem(key);
}
}
}
///
// Checks if state is in session of local storage
// Paramters:
// local (Boolean) If true, use localStorage, if false, use sessionStorage
// Returns:
// (Boolean) True is state is in storage, false if it is not (or storage is not supported)
///
Sburb.isStateInStorage = function(auto, local)
{
auto = typeof auto !== 'undefined' ? auto : false;
local = typeof local !== 'undefined' ? local : false;
var storage;
if(!Sburb.tests.storage) {
return false;
}
if(Sburb.tests.storage.local && Sburb.tests.storage.session) {
storage = local ? localStorage : sessionStorage;
} else if(Sburb.tests.storage.local) {
storage = localStorage;
} else if(Sburb.tests.storage.session) {
storage = sessionStorage;
} else {
return false;
}
for(var key in storage)
{
if(!storage.hasOwnProperty(key)) continue;
var savedIndex = key.indexOf('_savedState_');
if(savedIndex >= 0) // this key is a saved state
{
if(auto && key.indexOf("(auto)") >= 0 && key.indexOf(Sburb.name + ":" + Sburb.version) >= savedIndex)
return true;
else if(!auto && key.indexOf("(auto)") < 0 && key.indexOf(Sburb.name + ":" + Sburb.version) >= savedIndex)
return true;
}
}
return false;
}
function encodeXML(s) {
return s.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
};
//Serialize things that aren't actually in any room
function serializeLooseObjects(output,rooms,sprites){
for(var sprite in sprites){
if(!sprites.hasOwnProperty(sprite)) continue;
var theSprite = sprites[sprite];
var contained = false;
for(var room in rooms){
if(!rooms.hasOwnProperty(room)) continue;
if(rooms[room].contains(theSprite)){
contained = true;
break;
}
}
if(!contained){
output = theSprite.serialize(output);
}
}
for(var button in Sburb.buttons){
if(!Sburb.buttons.hasOwnProperty(button)) continue;
var theButton = Sburb.buttons[button];
if(!Sburb.hud[theButton.name]){
output = theButton.serialize(output);
}
}
return output;
}
//Serializes Rooms
function serializeRooms(output, rooms)
{
output = output.concat("\n<rooms>\n");
for(var room in rooms){
if(!rooms.hasOwnProperty(room)) continue;
output = rooms[room].serialize(output);
}
output = output.concat("\n</rooms>\n");
return output;
}
function serializeGameState(output, gameState)
{
output = output.concat("\n<gameState>\n");
for(var key in gameState) {
if(!gameState.hasOwnProperty(key)) continue;
output = output.concat(" <"+key+">"+encodeXML(gameState[key])+"</"+key+">");
}
output = output.concat("\n</gameState>\n");
return output;
}
function serializeActionQueues(output, actionQueues)
{
output = output.concat("<actionQueues>");
for(var i=0;i<actionQueues.length;i++) {
if(actionQueues[i].curAction) {
output = actionQueues[i].serialize(output);
}
}
output = output.concat("\n</actionQueues>\n");
return output;
}
//Serialize assets
function serializeAssets(output,assets,effects){
output = output.concat("\n<assets>");
for(var asset in assets){
if(!assets.hasOwnProperty(asset)) continue;
var curAsset = assets[asset];
var innerHTML = "";
if(curAsset.type=="graphic"){
innerHTML += curAsset.originalVals;
}else if(curAsset.type=="audio"){
var firstSrc = false;
for(var i = 0; i < curAsset.originalVals.length; i++)
{
var srcVal = curAsset.originalVals[i];
innerHTML += (firstSrc?";":"")+srcVal;
firstSrc = true;
}
}else if(curAsset.type=="path"){
for(var i=0;i<curAsset.points.length;i++){
innerHTML = innerHTML.concat(curAsset.points[i].x+","+curAsset.points[i].y);
if(i!=curAsset.points.length-1){
innerHTML = innerHTML.concat(";");
}
}
}else if(curAsset.type=="movie"){
innerHTML += curAsset.originalVals;
}else if(curAsset.type=="font"){
innerHTML += curAsset.originalVals;
}else if(curAsset.type=="text"){
innerHTML += escape(curAsset.text.trim());
}
output = output.concat("\n<asset name='"+curAsset.name+"' type='"+curAsset.type+"' ");
output = output.concat(" >");
output = output.concat(innerHTML);
output = output.concat("</asset>");
}
output = output.concat("\n</assets>\n");
output = output.concat("\n<effects>");
for(var effect in effects){
if(!effects.hasOwnProperty(effect)) continue;
var curEffect = effects[effect];
output = curEffect.serialize(output);
}
output = output.concat("\n</effects>\n");
return output;
}
//Serialize template classes
function serializeTemplates(output,templates){
output = output.concat("\n<classes>");
var serialized;
try {
// XMLSerializer exists in current Mozilla browsers
serializer = new XMLSerializer();
for(var template in templates){
if(!templates.hasOwnProperty(template)) continue;
output = output.concat(serializer.serializeToString(templates[template]));
}
}catch (e) {
// Internet Explorer has a different approach to serializing XML
for(var template in templates){
if(!templates.hasOwnProperty(template)) continue;
output = output.concat(templates[template].xml);
}
}
output = output.concat("\n</classes>\n");
return output;
}
//Serialize Hud
function serializeHud(output,hud,dialoger){
output = output.concat("\n<hud>");
for(var content in hud){
if(!hud.hasOwnProperty(content)) continue;
output = hud[content].serialize(output);
}
output = Sburb.dialoger.serialize(output);
var animations = dialoger.dialogSpriteLeft.animations;
output = output.concat("\n<dialogsprites>");
for(var animation in animations){
if(!animations.hasOwnProperty(animation)) continue;
output = animations[animation].serialize(output);
}
output = output.concat("\n</dialogsprites>");
output = output.concat("\n</hud>\n");
return output;
}
//Purge all assets
function purgeAssets() {
Sburb.assetManager.purge();
Sburb.assets = Sburb.assetManager.assets;
}
//Purge the game state
function purgeState(){
if(Sburb.rooms){
delete Sburb.rooms;
}
if(Sburb.sprites){
delete Sburb.sprites;
}
Sburb.rooms = {};
if(Sburb.bgm){
Sburb.bgm.stop();
Sburb.bgm = null;
}
for(var bin in Sburb.Bins) {
if(!Sburb.Bins.hasOwnProperty(bin)) continue;
Sburb.Bins[bin].innerHTML = "";
}
Sburb.gameState = {};
Sburb.globalVolume = 1;
Sburb.hud = {};
Sburb.sprites = {};
Sburb.buttons = {};
Sburb.effects = {};
Sburb.curAction = null;
Sburb.actionQueues = [];
Sburb.nextQueueId = 0;
Sburb.pressed = {};
Sburb.pressedOrder = [];
Sburb.chooser = new Sburb.Chooser();
Sburb.dialoger = null;
Sburb.curRoom = null;
Sburb.char = null;
Sburb.assetManager.resourcePath = "";
Sburb.assetManager.levelPath = "";
Sburb.loadedFiles = {};
}
//Load state/assets from file
Sburb.loadSerialFromXML = function(file,keepOld) {
Sburb.haltUpdateProcess();
file = Sburb.assetManager.levelPath+file;
if(keepOld && Sburb.loadedFiles[file]){
Sburb.startUpdateProcess();
return;
}else{
Sburb.loadedFiles[file] = true;
}
var request = new XMLHttpRequest();
request.open('GET', file, false);
try {
request.send(null);
} catch(err) {
console.log("Could not load level descriptors.");
fi = document.getElementById("levelFile");
return;
}
if (request.status === 200 || request.status == 0) {
try {
loadSerial(request.responseText, keepOld);
} catch(err) {
if (err instanceof XMLParsingError) {
if (err.file) {
console.error("Loaded from '"+file+"'")
} else {
err.file = file
console.error("Error in '"+file+"'")
}
}
throw err;
}
}
}
//main serial loading
function loadSerial(serialText, keepOld) {
Sburb.haltUpdateProcess();
var inText = serialText; //document.getElementById("serialText");
var input = Sburb.parseXML(inText);
if(!keepOld) {
purgeAssets();
purgeState();
}
var rootAttr = input.attributes;
var levelPath = rootAttr.getNamedItem("levelPath");
if(levelPath){
Sburb.assetManager.levelPath = levelPath.value.charAt(levelPath.value.length-1)=="/" ?
levelPath.value : levelPath.value+"/";
}
var resourcePath = rootAttr.getNamedItem("resourcePath");
if(resourcePath){
Sburb.assetManager.resourcePath = resourcePath.value;
}
var name = rootAttr.getNamedItem("name");
if(name) {
Sburb.name = name.value;
}
var version = rootAttr.getNamedItem("version");
if(version) {
Sburb.version = version.value;
}
var width = rootAttr.getNamedItem("width");
if(width) {
Sburb.setDimensions(width.value, null);
}
var height = rootAttr.getNamedItem("height");
if(height) {
Sburb.setDimensions(null, height.value);
}
var loadedFiles = rootAttr.getNamedItem("loadedFiles");
if(loadedFiles) {
var fileNames = loadedFiles.value.split(",");
for(var i = 0; i< fileNames.length; i++)
{
Sburb.loadedFiles[fileNames[i]] = true;
}
}
loadingDepth++;
loadDependencies(input);
loadingDepth--;
loadSerialAssets(input);
loadQueue.push(input);
loadSerialState(input);
}
Sburb.parseXML = function(inText) {
var parser=new DOMParser();
var parsed=parser.parseFromString(inText,"text/xml");
if (parsed.getElementsByTagName("parsererror").length>0) {
var error = parsed.getElementsByTagName("parsererror")[0];
throw new XMLParsingError(error, inText);
}
return parsed.documentElement;
}
function XMLParsingError(error, input) {
this.name = "XMLParsingError";
this.message = parseXMLError(error);
this.input = (input || "");
}
XMLParsingError.prototype = new Error();
function parseXMLError(n) {
if(n.nodeType == 3) {
return n.nodeValue;
}
if(n.nodeName == "h3") {
return "";
}
var error = ""
for(var i=0; i<n.childNodes.length; i++) {
error = error + parseXMLError(n.childNodes[i]);
}
return error;
}
function loadDependencies(input){
var dependenciesNode = input.getElementsByTagName("dependencies")[0];
if(dependenciesNode){
var dependencies = dependenciesNode.getElementsByTagName("dependency");
for(var i=0; i<dependencies.length;i++){
var dependency = dependencies[i].firstChild.nodeValue.trim();
Sburb.loadSerialFromXML(dependency,true);
}
}
}
function loadSerialAssets(input){
var rootAttr = input.attributes;
var description = rootAttr.getNamedItem("description");
if(description){
Sburb.assetManager.description = description.value;
}else{
Sburb.assetManager.description = "assets"
}
var newAssets = input.getElementsByTagName("asset");
for(var i=0;i<newAssets.length;i++){
var curAsset = newAssets[i];
var attributes = curAsset.attributes;
var name = attributes.getNamedItem("name").value;
if (!Sburb.assetManager.isLoaded(name)) {
loadSerialAsset(curAsset);
}
}
}
function loadSerialAsset(curAsset){
var newAsset = parseSerialAsset(curAsset);
Sburb.assetManager.loadAsset(newAsset);
}
function parseSerialAsset(curAsset) {
var attributes = curAsset.attributes;
var name = attributes.getNamedItem("name").value;
var type = attributes.getNamedItem("type").value;
var value = curAsset.firstChild.nodeValue.trim();
var blobUrlsAttr = attributes.getNamedItem("blob-urls");
var blobUrls = [];
if(blobUrlsAttr)
blobUrls = blobUrlsAttr.value.split(";");
if(blobUrls.length === 0) blobUrls = null;
var newAsset;
if(type=="graphic"){
newAsset = Sburb.createGraphicAsset(name, value, blobUrls);
} else if(type=="audio"){
var sources = value.split(";");
newAsset = Sburb.createAudioAsset(name, sources,blobUrls);
} else if(type=="path"){
var pts = value.split(";");
var path = new Sburb.Path();
for(var j=0;j<pts.length;j++){
var point = pts[j].split(",");
path.push({x:parseInt(point[0]),y:parseInt(point[1])});
}
newAsset = Sburb.createPathAsset(name,path);
}else if(type=="movie"){
newAsset = Sburb.createMovieAsset(name, value,blobUrls);
}else if(type=="font"){
//var sources = value.split(";");
newAsset = Sburb.createFontAsset(name,value);
}else if(type=="text"){
newAsset = Sburb.createTextAsset(name,value);
}
newAsset._raw_xml = curAsset;
return newAsset;
}
function loadSerialState() {
// don't load state until assets are all loaded
if(updateLoop){
clearTimeout(updateLoop);
updateLoop = null;
}
if(!Sburb.assetManager.finishedLoading()) {
updateLoop=setTimeout(function() { loadSerialState(); } ,500);
return;
}
while(loadQueue.length>0){
var input = loadQueue[0];
loadQueue.splice(0,1);
//These two have to be first
parseTemplateClasses(input);
applyTemplateClasses(input);
parseButtons(input);
parseSprites(input);
parseCharacters(input);
parseFighters(input);
parseRooms(input);
parseGameState(input);
parseHud(input);
parseEffects(input);
//should be last
parseState(input);
//Relies on Sburb.nextQueueId being set when no Id is provided
parseActionQueues(input);
}
if(loadQueue.length==0 && loadingDepth==0){
Sburb.startUpdateProcess();
}
}
function parseDialogSprites(input){
var hud = input.getElementsByTagName("hud");
if(hud.length>0){
var dialogSprites = hud[0].getElementsByTagName("dialogsprites");
if(dialogSprites.length>0){
serialLoadDialogSprites(dialogSprites[0],Sburb.assets);
}
}
}
function parseEffects(input){
var effects = input.getElementsByTagName("effects");
if(effects.length>0){
serialLoadEffects(effects[0],Sburb.assets,Sburb.effects);
}
}
function parseTemplateClasses(input){
var classes = input.getElementsByTagName("classes");
if(classes.length>0){
var templates = classes[0].childNodes;
for(var i=0;i<templates.length;i++){
var templateNode = templates[i];
if(templateNode.nodeName!="#text" && templateNode.nodeName!="#comment"){
applyTemplateClasses(templateNode);
var tempAttributes = templateNode.attributes;
templateClasses[tempAttributes.getNamedItem("class").value] =
templateNode.cloneNode(true);
}
}
input.removeChild(input.getElementsByTagName("classes")[0]);
}
}
function applyTemplateClasses(input){
for(var className in templateClasses){
if(!templateClasses.hasOwnProperty(className)) continue;
var templateNode = templateClasses[className];
var candidates = input.getElementsByTagName(templateNode.nodeName);
for(var j=0;j<candidates.length;j++){
var candidate = candidates[j];
tryToApplyTemplate(templateNode,candidate);
}
}
}
function tryToApplyTemplate(templateNode,candidate){
var templateClass = templateNode.attributes.getNamedItem("class").value;
var candClass = candidate.attributes.getNamedItem("class");
if(candClass && candClass.value==templateClass){
applyTemplate(templateNode,candidate);
}
}
function applyTemplate(templateNode,candidate){
var tempAttributes = templateNode.attributes;
var tempChildren = templateNode.childNodes;
var candAttributes = candidate.attributes;
var candChildren = candidate.childNodes;
for(var k=0;k<tempAttributes.length;k++){
var tempAttribute = tempAttributes[k];
if(!candAttributes.getNamedItem(tempAttribute.name)){
candidate.setAttribute(tempAttribute.name, tempAttribute.value);
}
}
for(var k=0;k<tempChildren.length;k++){
candidate.appendChild(tempChildren[k].cloneNode(true));
}
}
function parseButtons(input){
var newButtons = input.getElementsByTagName("spritebutton");
for(var i=0;i<newButtons.length;i++){
var curButton = newButtons[i];
var newButton = Sburb.parseSpriteButton(curButton);
Sburb.buttons[newButton.name] = newButton;
}
}
function parseSprites(input){
var newSprites = input.getElementsByTagName("sprite");
for(var i=0;i<newSprites.length;i++){
var curSprite = newSprites[i];
var newSprite = Sburb.parseSprite(curSprite, Sburb.assets);
Sburb.sprites[newSprite.name] = newSprite;
parseActions(curSprite,newSprite);
}
}
function parseActions(spriteNode,sprite){
var newActions = spriteNode.childNodes;
for(var k=0;k<newActions.length;k++){
if(newActions[k].nodeName == "#text") {
continue;
}
if(newActions[k].nodeName == "action"){
var newAction = Sburb.parseAction(newActions[k]);
sprite.addAction(newAction);
}
}
}
function parseCharacters(input){
var newChars = input.getElementsByTagName("character");
for(var i=0;i<newChars.length;i++){
var curChar = newChars[i];
var newChar = Sburb.parseCharacter(curChar, Sburb.assets);
Sburb.sprites[newChar.name] = newChar;
parseActions(curChar,newChar);
}
}
function parseFighters(input){
var newFighters = input.getElementsByTagName("fighter");
for(var i=0;i<newFighters.length;i++){
var curFighter = newFighters[i];
var newFighter = Sburb.parseFighter(curFighter, Sburb.assets);
Sburb.sprites[newFighter.name] = newFighter;
parseActions(curFighter,newFighter);
}
}
function parseRooms(input){
var newRooms = input.getElementsByTagName("room");
for(var i=0;i<newRooms.length;i++){
var currRoom = newRooms[i];
var newRoom = Sburb.parseRoom(currRoom, Sburb.assets, Sburb.sprites);
Sburb.rooms[newRoom.name] = newRoom;
}
}
function parseGameState(input) {
var gameStates = input.getElementsByTagName("gameState");
for(var i=0; i<gameStates.length; i++) {
var gameState = gameStates[i];
var children = gameState.childNodes;
for(var j=0; j<children.length; j++) {
var node = children[j];
if(node.nodeType === 3) //Text node, formatting node
continue;
var key = node.tagName;
var value = node.firstChild.nodeValue;
Sburb.gameState[key] = value;
}
}
}
function parseActionQueues(input){
var element=input.getElementsByTagName("actionQueues");
if(element.length==0) {
return;
}
var actionQueues = element[0].childNodes;
for(var i=0;i<actionQueues.length;i++) {
if(actionQueues[i].nodeName == "#text") {
continue;
}
var actionQueue = Sburb.parseActionQueue(actionQueues[i]);
Sburb.actionQueues.push(actionQueue);
}
}
function parseState(input){
var rootInfo = input.attributes;
var char = rootInfo.getNamedItem("char");
if(char){
Sburb.focus = Sburb.char = Sburb.sprites[char.value];
Sburb.char.becomePlayer();
}
var mode = rootInfo.getNamedItem("mode");
if(mode){
Sburb.engineMode = mode.value;
}
var scale = rootInfo.getNamedItem("scale");
if(scale){
Sburb.Stage.scaleX = Sburb.Stage.scaleY = parseInt(scale.value);
}
var nextQueueId = rootInfo.getNamedItem("nextQueueId");
if(nextQueueId){
Sburb.nextQueueId = parseInt(nextQueueId.value);
}
var curRoom = rootInfo.getNamedItem("curRoom");
if(curRoom){
Sburb.curRoom = Sburb.rooms[curRoom.value];
Sburb.curRoom.enter();
}else if(Sburb.curRoom==null && Sburb.char!=null){
for(var roomName in Sburb.rooms){
if(!Sburb.rooms.hasOwnProperty(roomName)) continue;
var room = Sburb.rooms[roomName];
if(room.contains(Sburb.char)){
Sburb.curRoom = room;
Sburb.curRoom.enter();
break;
}
}
}
var bgm = rootInfo.getNamedItem("bgm");
if(bgm){
var params = bgm.value.split(",");
Sburb.changeBGM(new Sburb.BGM(Sburb.assets[params[0]],parseFloat(params.length>1?params[1]:"0")));
}