Hwang
Hwang

Reputation: 502

How to load item/classes in library to a dictionary?

Without creating a list for the name of the items in the library, isit possible to auto get the name of the item and load it into a dictionary?

Currently what I'm doing is, 1st loading the swf using DataLoader from greensock

var contentLoader:DataLoader = new DataLoader('s_res.swf', {onComplete:completeHandler, format:'binary' } )

Then after I get the content, I use swfclassexplorer from flassari

to pass in the binary data I got

private static var list:Array;

    public function setup($byeArray:ByteArray):void
    {
        ba = $byeArray;
        var _loader:Loader = new Loader();
        var loaderContext:LoaderContext = new LoaderContext();
        _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
        _loader.loadBytes(ba, loaderContext);
    }

    private function completeHandler($e:Event):void
    {list = SwfClassExplorer.getClassNames(ba);}

From here I can get a list of the name which I can get the file using Loadermax as well

for (var i:int = 0; i < list.length; i++)
        {
            var _class:Class = (LoaderMax.getLoader("mcA").getClass(list[i])) as Class;
        }

From here it all looks fine but I feel that this might not be the most direct to do since it has to go through another loader to get the name of the file, and then loop the list to add the items in 1 by 1

Upvotes: 0

Views: 60

Answers (2)

Hwang
Hwang

Reputation: 502

apparently using swfloader from greensock this will work.

var ad:ApplicationDomain = $swfLoader.rawContent.loaderInfo.applicationDomain;
        list = ad.getQualifiedDefinitionNames();

Upvotes: 0

Vesper
Vesper

Reputation: 18747

According to the given link to swfclassexplorer, you should be able to switch to the SWF's context and call flash.utils.getDefinitionByName() against each of the listed classes. The result of each call will be a Class object which you then stuff into your dictionary. Also you can get rid of all the frameworks if you dig a bit into flash.display.Loader class and application domain context.

private var list:Vector.<String>;
private var dict:Dictionary=new Dictionary(); // here you want classes
var loader:Loader=new Loader();
var url:URLRequest = new URLRequest("s_res.swf");
var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);
loader.addEventListener(Event.COMPLETE,setup);
loader.load(url, loaderContext);

function setup(e:Event):void {
    var loader:Loader=e.target as Loader;
    var ad:ApplicationDomain=loader.contentLoaderInfo.applicationDomain;
    list=ad.getQualifiedDefinitionNames();
    // here you get all class definitions
    for each (var s:String in list) dict[s]=flash.utils.getDefinitionByName(s); 
    // fill the dictionary
}

Maybe there are more humps, but this actually should do.

Upvotes: 1

Related Questions