claus / as3swf

Low level Actionscript 3 library to parse, create, modify and publish SWF files.
MIT License
526 stars 127 forks source link

Modyfied BinaryData does not publish #20

Closed er453r closed 12 years ago

er453r commented 12 years ago

Hi. Great piece of software! I'm trying to modify an embed text file with your library, but after publishing there is no change... My code:

package{
    import com.codeazur.as3swf.SWF;
    import com.codeazur.as3swf.SWFData;
    import com.codeazur.as3swf.tags.TagDefineBinaryData;

    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.text.TextField;

    import mx.core.ByteArrayAsset;

    public class Test extends Sprite{       
        [Embed(source="test.txt",mimeType="application/octet-stream")] private var text:Class;

        public function Test(){
            var textField:TextField = new TextField();

            textField.text = ByteArrayAsset(new text()).toString();

            addChild(textField);

            var swf:SWF = new SWF(root.loaderInfo.bytes);

            trace(swf);

            var tag:TagDefineBinaryData = swf.getCharacter(1) as TagDefineBinaryData;

            trace(tag);

            tag.binaryData.clear();
            tag.binaryData.writeMultiByte("this is a new value", "iso-8859-1");

            trace(tag);

            var ba:SWFData = new SWFData();
            swf.publish(ba);

            var loader:Loader = new Loader();
            loader.x = 200;
            loader.loadBytes(ba);
            addChild(loader);
        }
    }
}
claus commented 12 years ago

as3swf is working fine. You have to force a new ApplicationDomain when loading the modified SWF, because otherwise the parent's document class (with the old text) is used, as the class names are the same in both parent and child.

package{
    import com.codeazur.as3swf.SWF;
    import com.codeazur.as3swf.SWFData;
    import com.codeazur.as3swf.tags.TagDefineBinaryData;

    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.system.ApplicationDomain;
    import flash.system.LoaderContext;

    import mx.core.ByteArrayAsset;

    public class Test extends Sprite{       
        [Embed(source="test.txt",mimeType="application/octet-stream")] private var text:Class;

        public function Test(){
            var textField:TextField = new TextField();

            textField.text = ByteArrayAsset(new text()).toString();

            addChild(textField);

            var swf:SWF = new SWF(root.loaderInfo.bytes);

            trace(swf);

            var tag:TagDefineBinaryData = swf.getCharacter(1) as TagDefineBinaryData;

            trace(tag);

            tag.binaryData.clear();
            tag.binaryData.writeMultiByte("this is a new value", "iso-8859-1");

            trace(tag);

            var ba:SWFData = new SWFData();
            swf.publish(ba);

            var loader:Loader = new Loader();
            loader.x = 200;
            loader.loadBytes(ba, new LoaderContext(false, new ApplicationDomain(null)));
            addChild(loader);
        }
    }
}