AS3 – Hacking PreloadSwf for fun and profit

PreloadSwf is one fine gem in Flash Player’s mm.cfg, the historical and little known Flash Player configuration file.

In this post I will tell you all you have to know about this feature and how you are going to use it in your projects.

This was invented for Flash profiling: Clement Wong, from the Flex SDK team, presents PreloadSwf as a major piece of the “Flash profiling” puzzle in his about the Flex profiler post in 2008.

If you want to know most of the secrets of mm.cfg, dive into Jean-Philippe “jpauclair” Auclair post on mm.cfg hidden treasures. This is a must read if you like bytecode porn 🙂


How does it work? What does it do?

PreloadSwf=path/to/an/user/swf

As soon as this parameter is defined, absolutely all the SWFs that the player will load will be “monitored” by this custom SWF.

That is PreloadSwf will be loaded first (pre-loaded), then the monitored SWF will be loaded.

It is worth noting that PreloadSwf will be created with a temporary stage which is only valid during initialization so you can read the FlashVars – the stage is lost after the constructor ends and you shouldn’t keep a reference to this stage object as any later access will make the player crash.

So at first PreloadSwf doesn’t have any knowledge of the monitored SWF – no reference, no common stage, no nothing. How do you take control of the monitored SWF?

All you need is “allComplete”

But don’t ever use “allComplete” as the type of your custom events.

This is one of the strangest undocumented event – some kind of “catch-all” event that you can listen on absolutely any EventDispatcher.

This event will fire each time one SWF is completely loaded, including from PreloadSwf when the monitored SWF is loaded.

The event target is the loaded SWF’s LoaderInfo – this finally gives us access to everything: url, parameters, content, stage, applicationDomain and even the SWF’s raw bytes data.

A PreloadSwf template

package 
{
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	
	public class PreloadSwf extends Sprite 
	{
		public function PreloadSwf():void 
		{
			addEventListener("allComplete", allComplete);
		}
		
		private function allComplete(e:Event):void 
		{
			removeEventListener("allComplete", allComplete);
			
			var info:LoaderInfo = e.target as LoaderInfo;
			trace(info.url, "is being monitored");
			
			// now let the fun begin! try:
			// info.content
			// info.content.stage
			// info.parameters
			// info.applicationDomain
			// info.bytes
		}
	}
}

Fun with DoomsdayConsole

The DoomsdayConsole is a combined logger/command-line interface for Flash 10 ActionScript 3 developers.

This is a power-user tool which can be used with any project – so why not preload it in any project without having to reference and initialize the console?

I used my little template with the console, and even added a little twist: press Ctrl+Shift and click on an object to set the scope of the console without having to boringly explore the display list container by container.

Profit with FlashDevelop

Hopefully, if you’re a FlashDevelop user, you are aware that it now sports a simple profiler featuring: memory graph, live objects count, objects creation stack inspection.

image

The profiler can be open from FlashDevelop toolbar by clicking the “stopwatch” button.

The Profiler panel has a toolbar with a few buttons – toggle the flag to automatically start the profiler as soon as your project start.

This is all neat and it works just like Flash Builder’s profiler thanks to this PreloadSwf feature – the source of FlashDevelop’s PreloadSwf is available if you want to hack your own profiler 😉

But the real fun is that starting with FlashDevelop 3.3 you can now define your custom PreloadSwfs and let FlashDevelop automatically configure mm.cfg for you when you run your projects so you don’t even have to get your hands dirty editing mm.cfg. Hooray!

Open FlashDevelop program settings, type “profiler” in the filter field and locate the Custom Profilers list of the AS3Context plugin. Add here the full path to your custom SWF and your done.

image

image Now see how the Profiler panel has a new button in its toolbar where you can choose the PreloadSwf to use.

Here I added my DoomsdayConnector – this obviously disables FlashDevelop’s built-in profiler but I can now benefit from this lightweight console for debugging my projects.

Where to go now?

Back to jpauclair’s blog and try his fancy PreloadSwf: FlashPreloadProfiler

Now I’d be happy to hear about your PreloadSwf experiments!

8 comments

  1. Wow man, I’ve been thinking about trying this. This is awesome! Thanks for proving the concept 😉
    We should really talk about adding this to the DConsole trunk somehow.

    By the way the display list navigation is something i’m looking to improve a lot. Adding a tree-view to the next version of the console so you can browse visually instead of endless select statements.

  2. @Andreas thanks and feel free to reuse any code I published, but I think some ConsoleUtil.select(target:DisplayObject) would be nice to have. Can’t wait to try the v2 😉

  3. @Tim This should work with Flash IDE projects too, if you set the configuration to “Debug” mode:
    FD will then take control of the SWF after build and play it in a standalone debug player (ask in the forum if you have a problem).

  4. Philippe; v2 has select(), and also execute() and other public methods for accessing the introspection commands. I went and stuck select() in the current trunk too so if you update now your DREAMS HAVE COME TRUE etc. The direct selection of display objects is doable through the measurement tool btw; Sort of obscure, but you can initialize it with “measure true” enabling the selection mode, and snapping to an object with the ctrl key modifier will also select that object.

  5. Hi,
    Thank you for posting your example.
    I have set up the SWF, adjusted the Global Settings.
    When I load a SWF in my browser, pressing cmd & shift (I’m on a Mac) does make the Console slide down into view, the problem is that all I can see is the what looks like could be text on the top 3 pixels of the browser. Any ideas on what I might be able to do to view all the text?

  6. Thanks for the simple instructions on installing this, and a bit of background info to understand what exactly is going on. Even more reason to love Flash Developer.

Leave a Reply

Your email address will not be published. Required fields are marked *