diff --git a/src/as/com/threerings/util/ParameterUtil.as b/src/as/com/threerings/util/ParameterUtil.as index 766b1f38c..dc0d8018d 100644 --- a/src/as/com/threerings/util/ParameterUtil.as +++ b/src/as/com/threerings/util/ParameterUtil.as @@ -1,6 +1,7 @@ package com.threerings.util { import flash.display.DisplayObject; +import flash.display.LoaderInfo; import flash.events.Event; import flash.events.IOErrorEvent; @@ -27,19 +28,51 @@ public class ParameterUtil */ public static function getParameters (disp :DisplayObject, callback :Function) :void { - var url :String = disp.root.loaderInfo.url; - // normal parameters - if (url == null || 0 != url.indexOf("file:")) { - callback(disp.root.loaderInfo.parameters); + return getInfoParameters(disp.root.loaderInfo, callback); + } + + /** + * Get the parameters. + * Note: the callback function may be called prior to this method + * returning. + */ + public static function getInfoParameters (loaderInfo :LoaderInfo, callback :Function) :void + { + // ensure that it's initialized... + if (loaderInfo.url == null) { + // Create a function to wait until the loaded object is initialized + var initWaiter :Function = function (event :Event) :void { + loaderInfo.removeEventListener(Event.INIT, initWaiter); + if (loaderInfo.url != null) { + // re-call + getInfoParameters(loaderInfo, callback); + + } else { + // url is still null, don't infinite loop + logWarning("Unable to determine url, bailing"); + callback(loaderInfo.parameters); + } + }; // end- initWaiter function + + // and wait. + loaderInfo.addEventListener(Event.INIT, initWaiter); return; } - // instead read from XML + // Simply use the parameters in the loaderInfo if we were not + // loaded from a file. + if (0 != loaderInfo.url.indexOf("file:")) { + callback(loaderInfo.parameters); + return; + } + + // If we were loaded from a file, read our parameters from the + // parameters.xml file. var loader :URLLoader = new URLLoader(); loader.addEventListener(IOErrorEvent.IO_ERROR, function (event :Event) :void { - trace("Error loading params: " + event); - callback(disp.root.loaderInfo.parameters); + logWarning("Error loading params: " + event); + callback(loaderInfo.parameters); } ); loader.addEventListener(Event.COMPLETE, @@ -54,5 +87,14 @@ public class ParameterUtil ); loader.load(new URLRequest("file:parameters.xml")); } + + /** + * Convenience function to log a warning, since we don't keep around + * a Log instance. + */ + protected static function logWarning (msg :String) :void + { + Log.getLog(ParameterUtil).warning(msg); + } } }