Question

I am new to the ActionScript world and I bumped into a problem that is hard for me. I have an SWF file that contains some classes that I cannot recompile from source but want to use again in a different project. I see that SWC files are containing compiled classes that can be easily reused. My question is how could I convert the existing SWF file to an SWC file so that I can use it as a regular library? Google did not help me.

Was it helpful?

Solution

SWC files use ZIP compression, and actually contain a SWF. If you change the file extension from .swc to .zip you can browse the contents and directory structure. It looks like this:

foobar.swc
    -> catalog.xml
    -> library.swf

With some reverse engineering, you might be able to create a SWC from a SWF by building the catalog.xml file and packing them together in ZIP file.

But that seems rather complex! You could also simply load the external SWF into your own SWF using Loader. All of the classes, symbols, and timelines of the external SWF will then be available. Of course, this creates a run-time dependency.

OTHER TIPS

Since your are not able to recompile your code to an SWC, I suggest you to use a Loader if you don't need to access the classes as compile-time.

[Event(name="complete", type="flash.events.Event")]
public class SWFLibrary extends EventDispatcher
{
    private var loader:Loader;
    private var loaded:Boolean;

    public function SWFLibrary(urlOrBytes:*)
    {
        super();

        loader = new Loader();
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);

        if (urlOrBytes is String) {
            loader.load(new URLRequest(urlOrBytes));
        } else if (urlOrBytes is URLRequest) {
            loader.load(urlOrBytes);
        } else if (urlOrBytes is ByteArray) {
            loader.loadBytes(urlOrBytes);
        } else {
            throw new ArgumentError("Invalid urlOrBytes argument");
        }
    }

    public function getAssetClass(className:String):Class
    {
        if (!loaded) {
            throw new IllegalOperationError("The SWF library isn't loaded");
        }

        return loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;
    }

    private function completeHandler(event:Event):void
    {
        loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
        loaded = true;

        dispatchEvent(new Event(Event.COMPLETE));
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top