How to get all definitions in an ApplicationDomain of a loaded SWF?

When you load a SWF into another, the loader SWF can get specific definitions from the loaded SWF using ApplicationDomain.getDefinition(name:String). For example:

package 
{
    // ... imports

    public class SWFLoader extends Sprite
    {
        private var loadedAppDomain:ApplicationDomain;

        public function SWFLoader()
        {
            var request:URLRequest = new URLRequest("test.swf");
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onTestLoadComplete);
            loader.load(request);
        }

        private function onTestLoadComplete(event:Event):void
        {
            var loaderInfo:LoaderInfo = LoaderInfo(event.target);
            loadedAppDomain = loaderInfo.applicationDomain;

            // Here we can get ANY defined symbol (class, namespace or function according to Adobe Flash help)
            var someSymbolClass:Class = Class(loadedAppDomain.getDefinition("SomeSymbol"));
            var someSymbolSprite:Sprite = Sprite(new someSymbolClass());

            addChild(sprite);
        }
    }
}

How can I get all of the definitions in a SWF, without specifying each explicitly?

Upvotes: 13

Views: 9602

Answers (4)

Mark
Mark

Reputation: 176

As of Flash Player 11.3, you can use ApplicationDomain.getQualifiedDefinitionNames().

See the official documentation for the method and this blog post about the Flash Player release.

Upvotes: 14

OXMO456
OXMO456

Reputation: 3548

EDIT: This is the quickest solution to your problem : http://www.bytearray.org/?p=175

Hi, you could use this library : https://github.com/claus/as3swf/wiki/ Don't have the time to do deeper test, but here is what i found :

1 - I have created a .swf containing in the library 2 exported MC, $Test and $Test2 2 - Once the .swf loaded by a Loader, i run this code :

var swf : SWF = new SWF(loader.contentLoaderInfo.bytes);
trace(swf);

3 - In the output you'll notice theses lines :

[76:SymbolClass] 
  Symbols:
    [0] TagID: 2, Name: $Test2
    [1] TagID: 1, Name: $Test

I think that there is a way to obtain this info directly thru the library API

Upvotes: 10

PatrickS
PatrickS

Reputation: 9572

Following from the answer I received from a previous question I asked here a few days ago (it's about SWC , but in your case, it doesn't really make a difference )
Working with SWCs - getDefinitionByName issue

If both SWFs share the same ApplicationDomain, you should be able to access the loaded SWF classes directly by doing this:

//provided that SomeSymbol extends Sprite...
var someSymbolSprite:Sprite =new SomeSymbol();

On the other hand, you won't be able to do this

var SomeSymbol:Class = getDefinitionByName("SomeSymbol");

unless you create a library of objects from the loaded SWF

var ssym:SomeSymbol;

Check the above link for more details.

Upvotes: -1

blue112
blue112

Reputation: 56582

You have to put the loaded SWF in the current ApplicationDomain.

Use ApplicationDomain.currentDomain to do that, on the ContextLoader info.

loader.load(request, new ContextLoader(false, ApplicationDomain.currentDomain));

It should work.

Upvotes: 0

Related Questions