bin2swf
package {
import com.bit101.components.InputText;
import com.bit101.components.Label;
import com.bit101.components.PushButton;
import com.codeazur.as3swf.data.SWFSymbol;
import com.codeazur.as3swf.SWF;
import com.codeazur.as3swf.SWFData;
import com.codeazur.as3swf.tags.TagDoABC;
import com.codeazur.as3swf.tags.TagEnd;
import com.codeazur.as3swf.tags.TagFileAttributes;
import com.codeazur.as3swf.tags.TagShowFrame;
import com.codeazur.as3swf.tags.TagSymbolClass;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.FileReference;
import flash.utils.ByteArray;
/**
* Wraps any binary file into SWF.
* @see http://github.com/claus/as3swf/raw/master/bin/as3swf.swc
* @author makc
*/
public class bin2swf extends Sprite {
private var butt:PushButton;
private var save:PushButton;
private var className:InputText;
public function bin2swf () {
butt = new PushButton (this, 20, 20, "LOAD FILE", onButtonClicked);
save = new PushButton (this, 20, 60, "SAVE FILE", onSaveClicked);
save.enabled = false;
new Label (this, 150, 20, "Class name (requires reload):");
className = new InputText (this, 150, 60, "FileBytes");
file = new FileReference;
file.addEventListener (Event.CANCEL, onUserCancelled);
file.addEventListener (Event.SELECT, onFileSelected);
file.addEventListener (Event.COMPLETE, onFileLoaded);
}
private function onUserCancelled (e:Event):void { butt.enabled = true; }
private function onButtonClicked (e:Event):void { butt.enabled = false; file.browse (); }
private function onFileSelected (e:Event):void { file.load (); }
private function onFileLoaded (e:Event):void {
var swf:SWF = new SWF;
swf.tags.push (new TagFileAttributes);
var binaryData:TagDefineBinaryDataHack = new TagDefineBinaryDataHack;
binaryData.binaryData.writeBytes (file.data, 0, file.data.bytesAvailable);
binaryData.characterId = 1;
swf.tags.push (binaryData);
var abcData:ByteArray = new ByteArray, i:uint, s:String = className.text;
for (i = 0; i < abc1.length; i++) abcData.writeByte (abc1 [i]);
abcData.writeByte (s.length);
for (i = 0; i < s.length; i++) abcData.writeByte (s.charCodeAt (i));
for (i = 0; i < abc2.length; i++) abcData.writeByte (abc2 [i]);
swf.tags.push (TagDoABC.create (abcData));
var symbolClass:TagSymbolClass = new TagSymbolClass ();
symbolClass.symbols.push (SWFSymbol.create (1, className.text));
swf.tags.push (symbolClass);
swf.tags.push (new TagShowFrame);
swf.tags.push (new TagEnd);
data = new SWFData;
// not needed in up-to-date as3swf
swf.compressed = true;
swf.publish (data);
butt.enabled = true;
save.enabled = true;
}
private function onSaveClicked (e:Event):void {
(new FileReference).save (data, "data.swf");
}
private var file:FileReference;
private var data:SWFData;
private var abc1:Array = [
0x10, 0x00, 0x2e, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00
];
private var abc2:Array = [ 0x0b, 0x66, 0x6c, 0x61, 0x73, 0x68, 0x2e, 0x75, 0x74, 0x69, 0x6c, 0x73,
0x09, 0x42, 0x79, 0x74, 0x65, 0x41, 0x72, 0x72, 0x61, 0x79, 0x06, 0x4f, 0x62, 0x6a, 0x65, 0x63,
0x74, 0x04, 0x16, 0x01, 0x16, 0x03, 0x18, 0x02, 0x00, 0x04, 0x07, 0x01, 0x02, 0x07, 0x02, 0x04,
0x07, 0x01, 0x05, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x01, 0x02, 0x09, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x02, 0x01, 0x01, 0x04,
0x01, 0x00, 0x03, 0x00, 0x01, 0x01, 0x04, 0x05, 0x03, 0xd0, 0x30, 0x47, 0x00, 0x00, 0x01, 0x01,
0x01, 0x05, 0x06, 0x06, 0xd0, 0x30, 0xd0, 0x49, 0x00, 0x47, 0x00, 0x00, 0x02, 0x02, 0x01, 0x01,
0x04, 0x13, 0xd0, 0x30, 0x65, 0x00, 0x60, 0x03, 0x30, 0x60, 0x02, 0x30, 0x60, 0x02, 0x58, 0x00,
0x1d, 0x1d, 0x68, 0x01, 0x47, 0x00, 0x00
];
}
}
// not needed in up-to-date as3swf
import com.codeazur.as3swf.tags.TagDefineBinaryData;
class TagDefineBinaryDataHack extends TagDefineBinaryData {
public function set characterId(value:uint):void { _characterId = value; }
}