Swf Class Explorer for AS3

The ApplicationDomain class in Actionscript 3 is a really handy class. It allows you to get any class from a loaded (or the current) swf and instantiate it.

ApplicationDomain currently has two public methods, hasDefinition(name:String):Boolean and getDefinition(name:String):Object. The simplicity of using these classes is a real treat, instantiating a class is as easy as

var myInstance:DisplayObject = new
(ApplicationDomain.currentDomain. getDefinition("com.flassari.MyClass"));

The biggest pain about the ApplicationDomain class is however its lack of a function for displaying all of the classes in a particular ApplicationDomain.
ApplicationDomain.getAllDefinitions():Array would be great! But unfortunately we must find our own way of getting around this flaw… so here it is!

I give you, the SwfClassExplorer class. Its usage is simple, as it has only one static function, getClasses(bytes:ByteArray):Array. To use it you must provide it with the bytes of the swf clip you want to explore. Here is an example of basic usage:

public function init():void {
	// Load the swf
	var urlLoader:URLLoader = new URLLoader();
	urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
	urlLoader.addEventListener(Event.COMPLETE, onSwfLoaded);
	urlLoader.load(new URLRequest("pathToSwf"));
}

private function onSwfLoaded(e:Event):void {
	// Get the bytes from the swf
	var bytes:ByteArray = ByteArray(URLLoader(e.currentTarget).data);
	// And feed it to the SwfClassExplorer
	var traits:Array = SwfClassExplorer.getClasses(bytes);
	for each (var trait:Traits in traits) {
		trace(trait.toString()); // The full class name
	}
}

I’ve prepared an example to demonstrate how it works.
First we have the Primitives.swf file. It will act as our “MovieClip library”. Its library consists of four movieclips, Box, Circle, Polystar and Triangle, each with its linkage class set to its library name:

library

Next we have the Example.swf file. It loads any swf file (Primitives.swf is in the path box by default) and lists all of the classes found in it. If the user clicks any of the classes, it will try to instantiate the class using no parameters (exceptions will be caught and displayed to the user) and if is or extends DisplayObject it will display it in the preview area.

Get Adobe Flash player

You can get the example files and source code here or the compiled swc here.

15 Responses to “Swf Class Explorer for AS3”

  1. JoeTheProgrammer Says:

    Hey good job. It stopped working for me with SWF files with many class definitions, so I investigated into porting SwfOp until I realized that that’s exactly what you did.

    The problem was that somehow my SWF got corrupted. Some headers were outrageously large, in the billions.

    The strange part, however, is that FlashDevelop was still able to parse all the classes! But now your version is working for me, as I re-built the original FLA. Hopefully they don’t get corrupted anymore…

  2. Jonathan Dumaine Says:

    I. Fracking. Love you.

    I needed this so much. Thank you, thank you!

  3. Tyler Madison Says:

    THANK YOU!

  4. bjorn Says:

    Awesome work - thanks for sharing this! :-)

  5. Weston Says:

    Is there a compiled version of the swfclassexplorer.swc?

  6. Flassari Says:

    No, but since you have the source code there you don’t have to or you can do it yourself with a quick google search =)

  7. Weston Says:

    I know how to build, but at my company if we build third party swcs ourselves, we have to go through legal, (yes, even if you’re using the MIT license). Was trying to avoid the red tape hassel!! :)

  8. Flassari Says:

    Ok, added the swc to the bottom of the post.

  9. Weston Says:

    Thanks very much!!! BTW - a quick question - what does trait.baseName tell us?

    The direct parent of the class? ie “extends”?

    Or the lowest level object?

    Thanks again! Great product!!

  10. pTymN Says:

    Hey, the code breaks when I try to load this swf: http://tim.kerchmar.com/level2-background3.swf

    I tried publishing the swf as Flash Player 9 compiled and Flash Player 10 compiled, and neither option changed the error from: Error: Record header length too big.

    with these values:

    br.bytesAvailable = 50168
    br.endian = “littleEndian”
    br.length = 51732
    br.objectEncoding = 3
    br.position = 1564
    tagCl = 50431
    tagCode = 787
    tagLength = 1073408

  11. Flassari Says:

    Odd, I’ll look into it

  12. Flassari Says:

    @pTymN: I tried using Flash Develop’s swf class browser to see what classes are in the swf, but even Flash Develop fails. The swf might be corrupted. See this picture:
    Swf fail :(
    Can you use any other libraries like SwfOp and see if they succeed in getting the classes of your swf?

  13. pTymN Says:

    I recreated the FLA, and the best that I can tell is that somehow the png that is used is causing the problem. I’ll try re-saving the png from gimp with different settings and seeing if I can find a combination of FLA/PNG settings that allows that swf to load. The very same project is loading 3 other nearly identical swfs without any problems.

  14. pTymN Says:

    That was retarted. Restarting the Flash IDE fixed the issue.

  15. Flassari Says:

    Lol, well, glad it’s fixed =)

Leave a Reply

Spam protection by WP Captcha-Free