actionscript-3flashapache-flexswfloader

Import classes from SWF into Flex app without starting the imported SWF


Background

I would like to write an AI for a certain Flash game, and as part of that I want to train an image recognizer with elements of the game. In order to do that, first I want to generate many samples with various configurations that can be used as training data.

In the generating process I have to use the game itself which I have in SWF format. My conception is to create my own Flash app that accesses the game SWF and uses its classes to generate the images (I have reverse engineered the game SWF with a tool, hence I know which classes I need).

Question

I created an mxml file with the following content:

<?xml version="1.0"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
                       xmlns:mx="library://ns.adobe.com/flex/mx">
    <fx:Script><![CDATA[
        public function onLoad():void {
            var clazz:Class = loader.loaderInfo.applicationDomain.getDefinition("com.game.Foo") as Class;
            // here I will use clazz
        }
        ]]></fx:Script>
    <mx:SWFLoader id="loader" source="GAME.swf" complete="onLoad()" />
</s:WindowedApplication>

The problem is that when GAME.swf is loaded, its main MovieClip is started automatically, its constructor is invoked even before my onLoad() event handler. Since the environment is different from what the game expects, the constructor fails with an exception and onLoad() is never called.

So the ultimate question is how to achieve that GAME.swf not be started when it is loaded, but I am also looking for other suggestion or solution that is different from that described above.


Solution

  • The solution was to modify the byte code in SWF files using FFDec, and remove the failing part from the main MovieClip's constructor.