Search code examples
alchemy

How can I load an adobe Alchemy swf build dynamically


With Alchemy most builds are static swc builds. These are great for linking directly against a library, but if you try to use more than one at a time, you usually (always?) run into problems with the shared memory space. One solution I've read about is to build swf's instead and load those dynamically into their own ApplicationDomain. This would give all libraries their own memory and would allow you to load multiple Alchemy libraries. (As mentioned in these threads on the adobe forums: http://forums.adobe.com/message/3665801 and http://forums.adobe.com/message/3927791)

My question is: how do I load these swf's and get to the code inside of them? There doesn't seem to be any documentation on this issue and although I do know how to load an swf, I don't know how to get to the code because I don't have any interface to the swf.


Solution

  • The problem is that we want to dynamically load the Alchemy lib, but if we build the library as a SWF it won't work (as that makes a standalone app). But we can't dynamically load a SWC-- or can we?

    Here's what to do (assuming a library named mylib):

    1. Build mylib as a SWC.
    2. Unzip mylib.swc and extract the library.swf file; rename it to mylib.swf
    3. Embed mylib.swf as a binary asset
    4. At runtime, instantiate the asset as a ByteArray
    5. Pass the asset ByteArray to Loader#loadBytes
    6. When the loader fires COMPLETE, use Loader#contentLoaderInfo to get the ApplicationDomain of the loaded SWF
    7. Use ApplicationDomain#getDefinition to look up the Alchemy initializer class. e.g., cmodule.mylib.CLibInit
    8. Use the initializer like you normally would. e.g., call init, etc.
    9. ...profit!

    You can embed an arbitrary number of Alchemy libraries this way, each running in its own ApplicationDomain.