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.

*UPDATE!
The code is now on GitHub: https://github.com/Flassari/Swf-Class-Explorer
And apparently the swc file has gone MIA, I’ll upload it again if I have the time/can be bothered.

27 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 =)

  16. Jeff Says:

    Is it possible to extend this tool to determine from the SWF bytecode what classes a SWF is dependent upon which it does not define?

    That is, if I compile a SWF which references one or more SWC files without static linking, I have a SWF that is dependent on other SWF/SWC files being loaded first.

    So the question is, can I determine those dependencies on any given SWF file at runtime without having to resort to the link report? AFAIK, there’s no way to determine this without the link report.

  17. Jeff Says:

    Nevermind – I’ve found the swfdump tool that’s provided with the Flex SDK which does exactly what I need. That is, you can determine which classes any given .swf file depends on.

  18. gudubeth Says:

    Thank you very much, this helped me a lot.

  19. Jgrant Says:

    Hey there, this is exactly what I’ve been looking for but I have a question. After you’ve downloaded the swf and gotten the array of the class names how do you instantiate the classes from the loaded swf? Since it was downloaded as a ByteArray instead of just a normal swf is it even possible? Do you have to download it again in the correct format?

  20. Flassari Says:

    @Jgrant: You can create a loader by using Loader.loadBytes().
    That’s what I’m doing in the ExampleMain.as file in the onSwfLoaded function, check it out.

  21. Jgrant Says:

    Thanks for the help, I got it working in the normal case but I can’t get working in a certain situation. When loading a SWF compiled normally from an FLA it works fine, but if I try to just export a single MovieClip into a swf and try to load that and find the class name it fails saying “Record header length too big.”.

    The main reason for doing this is because I want to split up assets into multiple SWF files but they are all stored in a single FLA, because making a new FLA for each asset swf would just be stupid.

  22. Flassari Says:

    I haven’t encountered that… the only thing I can recommend is to make sure your fla or swf file isn’t corrupted.

  23. Joshua Says:

    A little late to this party but this tool is incredible. Being able to examine loaded class names at runtime is amazing. Thanks much!

  24. Dylon Says:

    Do we have something like this supports AS2?

  25. dudu Says:

    This source code doesn’t work with this exemple

    var traits:Array = SwfClassExplorer.getClassNames(systemManager.loaderInfo.bytes);

  26. Grigori Says:

    Thanks a lot. Your code really helped me. Great job !!!

  27. John Says:

    Guys, did anyone noticed some problems when you have tweens in library, is there a fix for that so i dont need to remove those tweens?