Header Image - charles newman's blog

Category Archives

5 Articles

OSMF – Loading plug-ins locally with the file:/// protocol

Some of you may already know you can load an OSMF plug-in locally using the file protocol like this:

For Mac:

file:///Users/me/Documents/dev/projects/myproject/plugins/SomeOSMFPlugin.swf

For Win:

file:///C:/dev/projects/myproject/plugins/SomeOSMFPlugin.swf

This is very handy when developing a player because you don’t need to copy your player to localhost or a Web server to run and test.

However, I just discovered this oddity:
The plug-in will fail to load if the plug-in is a release build and the player is a debug build.

The failure happens down in the OSMF 1.5 core class DynamicPluginLoader in the onSWFLoaderStateChange method on this line:

var pluginInfo:PluginInfo = root[PLUGININFO_PROPERTY_NAME] as PluginInfo;

Flash Builder clearly shows the pluginInfo property on the root variable but the pluginInfo local variable is null after the above line executes.

This is not an OSMF bug, just something that you need to be aware of. I wasted about 30 minutes on this.

Dynamically loading a SWF and instantiating a Class in that SWF

There are a few blog posts out there on how to do this, but most of them involve a solution that requires a dummy reference in your Application code to the class you wish to instantiate, which forces the compiler to pull in the class, and therefore, allows getDefinition() to work.  But that solution defeats the whole purpose of loading a SWF dynamically.

If you want to load a SWF remotely, and then instantiate a class within that SWF at run-time, you are in the right place.

In the example below, the “application” is Foo and the class we’ll instantiate will be called Bar.  Let’s look at the SWF containing Bar first. Our goal, is to load the SWF that contains Bar and instantiate Bar in our application.

Dynamic.swf

Below is the root level class of the SWF we’ll dynamically load, let’s say it’s called Dynamic.swf. Notice the private var of type Bar. This is necessary to get the compiler to pull the Bar class into our Dynamic.swf. The idea here, is we’ll build Dynamic.swf and put it up on a server somewhere, or localhost so we can test this.

package
{
	import com.charlespatricknewman.sample.Bar;

	import flash.display.Sprite;

	public class Dynamic extends Sprite
	{
		// Need a dummy reference
		private var bar:Bar;

		public function Dynamic()
		{

		}
	}
}

And here is the Bar class we want to instantiate in our Application at run-time:

package com.charlespatricknewman.sample
{
	public class Bar
	{
		public function Bar()
		{
			trace(">>> Bar() constructor!");
		}

		public function doSomething():void
		{
			trace(">>> Bar.doSomething() called!");
		}
	}
}

All this class does is trace out some text, but this is sufficient to prove this is working.

The Application

Next is Foo, the application that will load the SWF and instantiate Bar.  Note this project has no reference or dummy variables of type Bar.

Here is the root level class:

package
{
import com.charlespatricknewman.sample.DynamicClassLoader;

import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

public class Foo extends Sprite
{
	public function Foo()
	{
		this.addEventListener(Event.ADDED_TO_STAGE,
					onAddedToStage);
	}

	private function onAddedToStage(event:Event):void
	{
		go();
	}

	private function go():void
	{
		var loader:DynamicClassLoader = new DynamicClassLoader();
		setupListeners();
		loader.loadSWF("http://localhost/test/Dynamic.swf",
				"com.charlespatricknewman.sample.Bar");

		function setupListeners(add:Boolean=true):void
		{
			if (add)
			{
				loader.addEventListener(Event.COMPLETE,
					onClassLoaded);
			}
			else
			{
				loader.removeEventListener(Event.COMPLETE,
					onClassLoaded);
			}
		}

		function onClassLoaded(event:Event):void
		{
			setupListeners(false);
			var Bar:Class = loader.instantiateClass(
				"com.charlespatricknewman.sample.Bar");
			var bar:Object = new Bar();
			bar.doSomething();
		}
	}
}
}

And here is the DynamicClassLoader class you see referenced above. This class could reside in the Application SWF or a library (SWC) the application uses at compile time. It’s job is to load the SWF and allow the Application to instantiate a class in the loaded SWF.

package com.charlespatricknewman.sample
{
 import flash.display.Loader;
 import flash.errors.IllegalOperationError;
 import flash.events.Event;
 import flash.net.URLRequest;
 import flash.system.ApplicationDomain;
 import flash.system.LoaderContext;

 public class DynamicClassLoader extends Loader
 {
	public function DynamicClassLoader()
	{
		super();
		contentLoaderInfo.addEventListener(Event.COMPLETE,
						onComplete);
	}

	public function loadSWF(swfPath:String, classPath:String):void
	{
		if (ApplicationDomain.currentDomain.hasDefinition(classPath))
		{
			throw new IllegalOperationError("Class is already loaded!");
		}

		var request:URLRequest = new URLRequest(swfPath);
		var context:LoaderContext = new LoaderContext();

		context.applicationDomain = ApplicationDomain.currentDomain;

		load(request, context);
	}

	public function instantiateClass(classPath:String):Class
	{
		return ApplicationDomain.currentDomain.getDefinition(classPath)
				as Class;
	}

	private function onComplete(event:Event):void
	{
		dispatchEvent(new Event(Event.COMPLETE));
	}
}
}

Due to the security restrictions in the Flash Player, both Dynamic.swf and the Application SWF need to be loaded from either localhost or a web server. You might also need a crossdomain.xml file on the web server, but it’s best to try this from localhost first so you can see it working.

This might be a good solution if you have a SWF or a SWC that contains a large amount of code that is rarely used. Adding that code to your SWF or SWC might cause an unnecessarily large SWF. But loading that bloated Class only when you need it, will provide a much faster initial download and startup for your end users.

Mac Ant Build – Error: PermGen space

This is worthy of a post because it took me a while to figure it out.

I created a fairly large Ant build script that kicks off builds for a project’s dependencies and it was working great until I added a few more things. It started failing with this:

[compc] Loading configuration file /Applications/Adobe Flash Builder 4/sdks/4.1.0/frameworks/flex-config.xml
[compc] Error: PermGen space

I’m not a hard core Java guy but I figured this had something to do with the Java Virtual Machine running out of memory. So after digging a bit and trying a few different things the solution I came up with was this:

export ANT_OPTS=-XX:MaxPermSize=512m

Try it on the command line first. If that works for you, you might want to add it to your .bash_profile. Once I added that, my build script was able to complete.

Flash Builder/Flex Builder – No Support for Relative Paths

You can’t enter a relative source path in Flex/Flash builder. For example, if I want the Flash authoring environment to find a class or classes on my system, I can tell it to look here: “../../libs” and it will go up 2 directories, go to libs and look in every directory under that until it finds the class. This allows me to check-in the FLA file and everyone on the team is going to be able to build it.

However, Flex/Flash Builder is built on Eclipse, and from what I’ve been a able to gather, relative paths are not accepted for source paths, which is a real pain. Unless everyone on your team stores code under “${DOCUMENTS}/everyone/uses/the/same/dev/path”, which in that case you could use the built in Linked Resources that Builder defines. But if you work on a framework or a public API, or just a dev team where people want choice over where they place their source code, this sucks.

I complained to one of the guys on the Builder team at Adobe about this and he told me this was a limitation of Eclipse and there is nothing they can do about it. But, he gave me a nice workaround and it occurred to me today, while showing this trick to a co-worker, that I should post this because others may not know about it.

Here’s the problem:

You create a new project in Flex/Flash builder and you want to reference the classes in another project. If that other project is a library, and you don’t want to reference the SWC, but the actual project source, you can simply hit “Add Project…” and select the project from the window that pops up and you are good to go, no relative paths required, Builder knows where everything is:

However, if you want to reference another project that is not a library project, such as an OSMF plugin (a project that creates a SWF, not a SWC), you need to use the “Source path” tab on this dialog and click the “Add Folder…” button:


Here’s the trick:

Don’t use the Browse button on the window that pops-up when you click “Add Folder…” above. Just type a slash (‘/’) followed by the project name.

Of course the project needs to be present in Builder (i.e., you either created it in Builder or imported it into Builder). The problem occurs when you use the browse button to locate the project because the entire path, which is specific to your system, will show up in the project files and no one except you is going to be able to build the project.  But if you just type a slash followed by the project name, builder will find the project and everyone will be able to build it.

In the image just above is what you should see on this tab, not the full path to the projects.

And your .actionScriptProperties file will have entries like this:

<compilerSourcePath>

<compilerSourcePathEntry kind=”1″ linkType=”1″ path=”/AkamaiAdvancedStreamingPlugin”/>

<compilerSourcePathEntry kind=”1″ linkType=”1″ path=”/AkamaiBasicStreamingPlugin”/>

</compilerSourcePath>

Notice the full path is not there.

One more thing:

You need to do one more thing before checking your code into a repository (SVN, Perforce, etc.):
Check your .project file and delete the <linkedResources> tag:
<linkedResources>
<link>
<name>[source path] AkamaiAdvancedStreamingPlugin</name>
<type>2</type>
<location>/Users/cnewman/Documents/Akamai/p4/projects/flash-client-dev/osmf/plugins/advanced-streaming-    plugin/trunk/AkamaiAdvancedStreamingPlugin</location>
</link>
</linkedResources>
Notice the full system path is in there. Builder doesn’t need this tag to build your project, it’s only for the Eclipse GUI, so just remove it from the file so no one gets errors importing your project.

I realize there are Linked Resources at the General->Workspace level for builder such as ${DOCUMENTS}. But this is not a great solution if you work on a project that a lot of people are going to need to be able to build, such as a public framework or API.  Everyone who wants to build your project is going to need to setup your required Linked Resource before they can build. But with the trick above, all they need to do is import your Builder project and go.

Flex Builder/Flash Builder – Switch Perspectives Quickly

I just discovered this by accidentally hitting the wrong key while debugging:

To switch from Debug Perspective back to Development Perspective, hit Cmd+F8.

Flex Builder pops up a list of Perspectives, hit Cmd+F8 again to cycle through your list.  This is very handy when you’re finished debugging and want to quickly get back to the Development Perspective without reaching for the mouse.  I occasionally stumble on these kinds of things I should already know…