1 goog.provide('FlappLoader');
2 goog.require('FlappIBit');
3 goog.require('FlappSWFHeader')
4 goog.require('FlappSWFTag')
5 goog.require('FlappBitmap')
7 goog.scope(function() {
12 FlappLoader = function(flapp) {
16 FlappLoader.prototype = {
17 fromURL: function(url, dict, movieClip) {
18 console.debug("FlappLoader.prototype.fromURL("+url+",flapp,dict,movieClip");
19 var xhr = new XMLHttpRequest();
20 var ibit = new FlappIBit();
23 xhr.onreadystatechange = function() {
24 if (xhr.readyState > 1) {
25 if (xhr.status == 200) {
26 if (xhr.responseText) {
27 ibit.input(xhr.responseText);
28 loader.parse(ibit, dict, movieClip);
34 // xhr.responseType = 'arraybuffer';
35 xhr.overrideMimeType('text/plain; charset=x-user-defined');
38 parse: function(ibit, dict, movieClip) {
39 // console.debug("FlappLoader.prototype.parse");
40 if (ibit.len() < 20) { // 20 is minumum size for swf header, maybe.
43 if (this.header === null) {
44 this.header = FlappSWFHeader.load(ibit);
45 console.debug(this.header);
46 this.flapp.setHeader(this.header);
48 var jpegTables = null;
49 while (ibit.a(), ibit.hasNext(2)) {
50 var headPos = ibit.getBytePos();
51 var tag_and_length = ibit.ui16(); // tag and length
52 var code = tag_and_length >>> 6;
53 var length = tag_and_length & 0x3f;
54 if (length === 0x3f) {
55 if (ibit.hasNext(4) === false) {
56 ibit.setPos(headPos, 0);
61 if (ibit.hasNext(length) === false) {
62 ibit.setPos(headPos, 0);
65 var startOfContent = ibit.getBytePos();
66 var tag = FlappSWFTag.load(code, length, ibit);
67 // console.debug(tag);
70 movieClip.appendControlTag(tag);
72 case 2: // DefineShape
73 case 22: // DefineShape2
74 case 32: // DefineShape3
75 dict.set(tag.id, tag);
77 case 6: // DefineBits(JPEG)
78 var jpeg = FlappBitmap.toJpeg(tag.jpegData, jpegTables);
79 var image = new Image();
80 image.src = "data:image/jpeg;base64," + goog.global.btoa(jpeg);
82 dict.set(tag.id, tag);
87 case 9: // SetBackgroundColor
88 // set canvas background
91 movieClip.appendControlTag(tag);
93 case 26: // PlaceObject2
94 movieClip.appendControlTag(tag);
96 case 39: // DefineSprite
97 dict.set(tag.id, tag);
100 console.warn("Unknown swf tag code:"+code);
103 ibit.setPos(startOfContent + length, 0);