In a recent project I had to use runtime shared libraries. In ActionScript 3, the use of external libraries is made easy through the ApplicationDomain class and the getDefinition method. But one common limitation of the getDefinition method is that you have to know the name of the class definition you want to extract.
A cool addition would be a getDefinitions method which would return an Array of definitions.
Unfortunately such a feature is still missing in Flash Player 10.
Vote for a getDefinitions() method on ApplicationDomain.
That's the reason why is created this tiny library called SWFExplorer which lets you browse an SWF and extract all the exported classes definitions available in an SWF.
Here is a tiny piece of code to illustrate how it works :
var explorer:SWFExplorer = new SWFExplorer();
explorer.load ( new URLRequest ( "library.swf" ));
explorer.addEventListener ( SWFExplorerEvent.COMPLETE, assetsReady );
function assetsReady (e:SWFExplorerEvent):void
{
// outputs : org.groove.Funk,org.funk.Soul,org.groove.Jazz
trace( e.definitions );
// outputs : org.groove.Funk,org.funk.Soul,org.groove.Jazz
trace( e.target.getDefinitions() );
// ouputs : 3
trace( e.target.getTotalDefinitions() );
}
The getDefinitions method returns an array of class names that you can use to call the getDefinition method.
The code above can be used for a remote library, if you want to parse some SWF bytes you may have already loaded, the SWFExplorer class contains a simple parse() method which returns the definitions :
var definitions:Array = explorer.parse(libraryBytes); // outputs : org.groove.Funk,org.funk.Soul,org.groove.Jazz trace( definitions ); // outputs : org.groove.Funk,org.funk.Soul,org.groove.Jazz trace( explorer.getDefinitions() );
I hope this library will help you guys working with runtime shared libraries.
This is a first beta version that I will extend, SWC support will be added and more to come.
You can download the library here.
Note : This library does not require Flash Player 10 to run. You can use it with Flash Player 9.
Update 08/22/08 : Denis Kolyako posted a few days ago another class which also extracts also the non linked classes. Very nice!
Update 06/11/09 : Released SWFExplorer 0.6 (fixed a bug related to SWF dimensions).
Comments (26)
Very helpful library. Thanks.
I decided to re-open the feature request for “getDefinitions” http://bugs.adobe.com/jira/browse/FP-564
Hopefully it can make it in the new version with a few votes behind it.
Hi Ramsey,
I am happy you like it
Great idea, I will definitely vote and try to make it happen for future releases.
Thibault
You are so awesome, this library absolutely kicks ass. This is so useful and I can see a lot of future ideas with this lib.
This is awesome, Thibault.
The ability to get a list of classes within a project makes reflection so much more powerful! Thanks so much.
Marty
salut tibo,
cette ligne
[Event(name='complete', type='org.bytearray.loader.events.SWFExplorerErrorEvent')]
elle sert a quoi exactement?
a++
Salut gropapa,
C’est juste pour que Eclipse affiche directement lors de l’écriture de la méthode addEventListener les événements diffusés par l’objet.
Ca permet de faire gagner du temps au développeur qui voit directement les événements diffusés.
Je t’ai fait une petite capture disponible ici :
http://www.bytearray.org/wp-content/projects/swfexplorer/event-parsing.png
++
Thibault
Hi guys,
I am very happy you like it. There are many use cases with this lib. I will add some more stuff to it if you need something special let me know.
Thibault
Thibault – Can you confirm if this requires FP10 to run?
Hi Marty,
This library does not require the Player 10. You can use it with the Player 9, I used the Player 10 to use the new FileReference capabilities to load a local stream in the app
best,
Thibault
Hi
I tried to analyze the bytes of an application, through “this.root.loaderInfo.bytes” inside the Application class
Unfortunately, I only see those classes that are generated through embedded assets.
I do not see any of my custom classes… too bad !
Do you have an idea of how to list all classes of an already loaded application ?
Thanks for this great development anyhow, you have to master SWF files !
Hi Julien,
The library for now is extracting all the classes attached to symbols in the SWF library. If I understand right, what you would like to do is extract also the custom classes available in the SWF ?
best,
Thibault
Yes Thibault, you understood me right. It would be great to be able to discover all custom classes in any SWF, so that we could unleash introspection power.
Thanks
julien
Darn it darn it! I just made a class like this! Granted, it only finds definitions for objects in the display list, but I was proud.
I guess I’ll just have to use your class. Nice. REAL NICE.
Request- why are the class definitions returned in an Array? It’d be much cooler, I think, if you’d return an Object like so:
// output: [object MyClass]
trace( new SWFExplorer.library.MyClass() );
Hi, Thibault.
It’s kind of off topico, but, excuse me if it is a beginner question, but when you retrieve the FrameRate and FrameCount (lines 113 to 115) you use readByte() and readShort() respectively, shouldn’t it be inverted, since the frame rate is a 32-bit?
Cheers,
CaioToOn!
Hi CaioToOn!
This is an interesting question. In fact the frame rate is supposed to be stored as a 16-bit integer. So 2 bytes are reserved for the frame rate. Actually, the first byte is ignored. That’s the reason why in the Flash IDE the maxiumum frame rate you can set it to 120 maxiumum, which fits in 1 byte.
If the SWF is compiled with the Flex compiler and the frame rate is set to something higher than 255 (full byte) then the other byte should be considered and I should read the frame rate with readShort().
I will take that in account and read it this way.
Thanks
Thibault
Hi Thibault,
Thanks for sharing this code with us! I’m excited to implement this functionality, but I can’t seem to get it to recognize my exported library assets.
I have a Flash app that needs to load in some external font libraries, which are .swf files with fonts embedded in the library, and exported for actionscript (with the “export in first frame” option checked by default). I must be doing something wrong, because I can see that the symbolClasses array has a length of zero after it’s been filtered.
Well, I wish I knew enough about this to troubleshoot, but any help is greatly appreciated.
Cheers,
Benjamin
Hi Benjamin,
Can you send me the SWF so that I can test it ?
thibault at bytearray.org
best,
Thibault
Hi Thibault,
I was looking for exactly what you did here. But I can’t seem to get the explorer to see my classes in the external .swf
Basically what I did was create an as3 project in flash cs3, then created a bunch of shapes and imported some images.
Then for each of these movieclips ( i converted all of them to separate movieclips ), in their properties under the linkage section, I typed in a class name for them e.g. ‘item_1′, ‘item_2′, etc, and the options ‘export for actions script’ and ‘export in first frame’ are both selected.
Using the above exmaple code given, i directed the explorer to load the .swf, but i get a 0 from the trace.
Did i do something wrong in my swf?
Hi hajme,
I am interested to see your SWF. Can you send me by email thibault {at} bytearray.org your FLA so that I can take a look ?
best,
Thibault
code given above is not working (SWFExplorer which lets you browse an SWF and extract all the exported classes definitions available in an SWF.
).can any one please send the complete code which will retrieve the images from the .swf file and store in to the array.
Thank you very very much. This was very useful!
looks really useful Thibault, gets the contents of a swf just fine. I can’t seeem to instantiate an instance of any of the classes tho. normally using a loader I would expect to have to do something like
var context:LoaderContext = new LoaderContext(); context.applicationDomain = ApplicationDomain.currentDomain;
so I can use getDefinitionByName. Obviously I’m missing something as I can’t do that with swfexplorer. How does ApplicationDomains work with reagrd to swfexplorer
link to my solution to the previous query. If there is an obvious problem with using SWFExplorer this way perhaps you could let me know. Thanks a lot.
http://ironfilingz.blogspot.com/2009/11/i-found-myself-this-week-with-need-for.html
Sorry – link got truncated
Hi wm,
Yes! Nice use and modification done to the SWFExplorer class. What I usually do is using the SWFExplorer class which loads the SWF then I retrieve the definitions then inject the SWF in a Loader through the loadBytes method.
I have then, all the definitions and a Loader with its ApplicationDomain where I can retrieve the definitions.
Thibault
Trackbacks/Pingbacks (8)
[...] haven’t tried this out yet, but if it’s from Thibault Imbert, it has to be good. It’s a handy utility class that will rummage through a loaded .swf file and [...]
[...] i have often found myself in a situation where they seemed a good idea in theory at least. So this looks [...]
[...] http://www.bytearray.org/?p=175 http://etcs.ru/pre/getDefinitionNamesSource/ [...]
[...] updated the SWFExplorer library so that you can check if any hardware acceleration has been set in the current SWF and [...]
[...] on peut utiliser des classes qui introspectent la totalité des classes disponibles dans le SWF http://www.bytearray.org/?p=175 (uniquement pour les symboles DisplayObject) ou http://etcs.ru/pre/getDefinitionNamesSource/ [...]
[...] out the different options – RSL, Module, straight up loading a SWF. Just found a post by Thibault Imbert (bytearray.org) that is making me think I’ll go with loading a SWF, but still thinking it [...]
[...] Explorer, 佢既應用 http://www.bytearray.org/?p=175 About adminNo description. Please complete your profile. Tagged as: actionscript, code Leave a [...]
[...] http://www.bytearray.org/?p=175 Filed under: 無分類 Leave a comment Comments (0) Trackbacks (0) ( subscribe to comments on this post ) [...]