From 8d21a4e0532a23563c0cab844374cd9b07752364 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Thu, 5 Apr 2007 17:49:13 +0000 Subject: [PATCH] For some reason, content loaded into another SWF does not know its URL until after the INIT stage. I don't think this particularly matters for parameters, since if the URL is unknown it almost certainly means that we will not need to do the custom parameter loading anyway, but let's just make sure. This also knocks a "known issue" off my list, and opens up the possibility of remixed items loading content packs from a relative URL. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4646 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/util/ParameterUtil.as | 56 ++++++++++++++++++--- 1 file changed, 49 insertions(+), 7 deletions(-) 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); + } } }