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
This commit is contained in:
Ray Greenwell
2007-04-05 17:49:13 +00:00
parent 3ac5ab2e10
commit 8d21a4e053
+49 -7
View File
@@ -1,6 +1,7 @@
package com.threerings.util { package com.threerings.util {
import flash.display.DisplayObject; import flash.display.DisplayObject;
import flash.display.LoaderInfo;
import flash.events.Event; import flash.events.Event;
import flash.events.IOErrorEvent; import flash.events.IOErrorEvent;
@@ -27,19 +28,51 @@ public class ParameterUtil
*/ */
public static function getParameters (disp :DisplayObject, callback :Function) :void public static function getParameters (disp :DisplayObject, callback :Function) :void
{ {
var url :String = disp.root.loaderInfo.url; return getInfoParameters(disp.root.loaderInfo, callback);
// normal parameters }
if (url == null || 0 != url.indexOf("file:")) {
callback(disp.root.loaderInfo.parameters); /**
* 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; 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(); var loader :URLLoader = new URLLoader();
loader.addEventListener(IOErrorEvent.IO_ERROR, loader.addEventListener(IOErrorEvent.IO_ERROR,
function (event :Event) :void { function (event :Event) :void {
trace("Error loading params: " + event); logWarning("Error loading params: " + event);
callback(disp.root.loaderInfo.parameters); callback(loaderInfo.parameters);
} }
); );
loader.addEventListener(Event.COMPLETE, loader.addEventListener(Event.COMPLETE,
@@ -54,5 +87,14 @@ public class ParameterUtil
); );
loader.load(new URLRequest("file:parameters.xml")); 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);
}
} }
} }