Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Instance metadata editor
package {
import com.bit101.components.PushButton;
import com.bit101.components.TextArea;
import com.codeazur.as3swf.SWF;
import com.codeazur.as3swf.tags.*;//TagPlaceObject;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.utils.ByteArray;
import flash.utils.describeType;
[SWF(width = 465, height = 465)]
public class InstanceMetadataEditor extends Sprite {
private var loader:Loader;
private var file:FileReference;
private var ta:TextArea;
private var index:int;
public function InstanceMetadataEditor() {
new PushButton(this, 10, 10, "LOAD SWF", onLoadClick);
(new PushButton(this, 120, 10, "SAVE SWF", onSaveClick)).enabled = false;
file = new FileReference;
file.addEventListener (Event.SELECT, onFileSelected);
file.addEventListener (Event.COMPLETE, onFileLoaded);
loader = new Loader();
loader.addEventListener(MouseEvent.CLICK, onClick);
loader.y = 40;
addChild(loader);
ta = new TextArea(this, 230, 10);
ta.width = 465 - 10 - 230;
ta.height = 465 - 10 - 10;
ta.alpha = 0.75;
}
private function onLoadClick (...a):void { file.browse ([new FileFilter("Flash files: (*.swf)", "*.swf")]); }
private function onFileSelected (e:Event):void { file.load (); }
private function onFileLoaded (e:Event):void {
loader.unloadAndStop(true);
loader.loadBytes (file.data);
(getChildAt(1) as PushButton).enabled = true;
ta.text = "click on object in loaded swf to read/write its metaData";
}
private function onClick (e:MouseEvent):void {
var objects:Array = loader.getObjectsUnderPoint(new Point(mouseX, mouseY));
if (objects.length > 0) {
// how do you pick topmost object? idk, will just grab [0]
var topmost:DisplayObject = objects[0];
while (topmost.parent != loader.content) topmost = topmost.parent;
ta.text = JSON.stringify(topmost.metaData);
// how do you identify this object in parsed swf? idk, will use its position in display list
var container:DisplayObjectContainer = loader.content as DisplayObjectContainer;
for (var i:int = 0, index = -1; i < container.numChildren; i++) {
if (container.getChildAt(i) == topmost) {
index = i; break;
}
}
}
}
private function onSaveClick (...a):void {
var count:int = index;
var swf:SWF = new SWF(file.data);
for (var i:int = 0; i < swf.tags.length; i++) {
if (swf.tags[i] is TagPlaceObject) {
count--;
if (count < 0) {
var po1:* = swf.tags[i];
var po4:TagPlaceObject4 = new TagPlaceObject4();
var typeInfo:XML = describeType(po1);
for each (var varName:String in typeInfo.variable.@name) {
po4[varName] = po1[varName];
}
// inject metadata
if (ta.text.charAt(0) == "{") {
po4.metaData = JSON.parse(ta.text);
}
swf.tags.splice(i, 1, po4);
break;
}
}
}
var bytes:ByteArray = new ByteArray();
swf.publish(bytes);
new FileReference().save(bytes, "output.swf");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment