views:

136

answers:

3

Is it possible to load an ActionScript 3 SWF and interact with it, for example by replacing some of its functions or editing variables? Similar to how it's possible in .NET using reflection and code generation.

I'm looking to add an extra layer of functionality over an existing 3rd-party AS3 app.

+4  A: 

If you are loading it to another AS3 SWF, yes, you can access the public properties of the loaded SWF. You cannot replace functions - you can call functions, public ones, that is.

var ldr:Loader = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad);
ldr.load(new URLRequest("filename.swf"));
private function onLoad(e:Event):void
{
    var swf:Object = LoaderInfo(e.target).content;
    swf.somePublicVar = newValue;
    swf.somePublicObject.publicMethod();
    swf.getChildAt(0).x = 30;
    //assuming there is a sprite at index 1
    var child:Sprite = Sprite(swf.getChildAt(1));
    child.graphics.lineStyle(1);
    child.graphics.drawCircle(10, 10, 10);
}

If you are loading 3rd party SWF from another domain (instead of copying it to your domain and loading it from there), the following rule apply

  • If the loaded content is a SWF file written with ActionScript 3.0, it cannot be cross-scripted by a SWF file in another security sandbox unless that cross-scripting arrangement was approved through a call to the Security.allowDomain() or the Security.allowInsecureDomain() method in the loaded content file.

Which in short means that unless the SWF creator has explicitly allowed it to be modified from your domain, you cannot modify it by loading it remotely. This doesn't apply if you can get the SWF copied to your domain.

Amarghosh
A: 

Well, you could could control the things(clips and other swf components) that are publicly available by digging in through the hierarchy of objects, just basic stuff. The other choice is you could decompile the loaded swf at runtime. This library sounds pretty interesting for that task.

George Profenza
A: 

Aspose.Flash can help you with editing variables since it has support for the action script byte code decompilation, however it's not free and can't emit new functions unless you use an external as3 compiler(e.g. the one from Adobe that's free and open source)

alexm