views:

459

answers:

3

In Flash Actionscript 3, when you need to load text, you use a class called 'URLLoader', and when you need to load an image (or .swf) you use a class called 'Loader.' As far as I know, loading a .bmp with URLLoader is as useless as loading an .xml into a Loader - it doesn't compute.

I'm making a class that handles a queue of external assets to be loaded - but aside from splitting the target url to check out the file extension, I can't figure out a good way to tell if each URL requires a URLLoader or a Loader. At any rate, it's imaginable that a .php url might return either an image or a document - so there's no way to count on the file names to dictate the right type of loader class to use.

Any ideas on how to reliably detect the right class for the job on a URL-by-URL basis?

A: 

Hi,

Personally, I would specify exactly how you would like to load you object if it is an image or xml. Since I have written a few queue loaders, I suggest that you dont just track a URL string but a set of objects with things like, the URL to load, if it has loaded, load priority, and type. Here is some psudo code.

class QueueObject{
  var URLtoLoad:String;
  private var hasLoaded:Boolean = false;
  var isDataObject:Boolean = false;
  var queuePriority:Number = 3; // 
}

Now in your code when you want to add something into your queue.

simply go

var loadObject:QueueObject = new QueueObject();
loadObject.URLtoLoad = "http://theurl.com/somedata.xml";
loadObject. isDataObject= true;

// Now push this into the awesome loader class that you have written which will manage the queue. I would send the object to load, the reference to the current scope eg, this, and the name of the function you want to call when this object has loaded.

MyStaticLodingClass.addQueueObject(loadObject, this, myFunctionThatYouWillCallWhenDone);

That class above should now have an array which you can sort by priority to give you items to load, what their types are and their URL's. This way you can

This might help or not.

Other wise make something to figure out what type of object you are trying to load, eg BMP and use the correct loader based on a lookup for this object.

John Ballinger
My thinking is more or less along the same lines - but I'm not sure I want to make them specify whether the target is essentially text or binary data. What if that target url is coming from another source, and they have no control over it? What I'm really after is figuring out a way to automatically detect the appropriate loader type.
matt lohkamp
+1  A: 

Bulkloader does some guesswork based on the url, maybe take a peek in the source and see how they do it?

grapefrukt
good link - looks like it attempts to auto-detect the type based on the URL, but allows the user to pass in an additional argument to bypass this default behavior. I like that idea.
matt lohkamp
Yup, that's it. I've played with a few ideas on auto detection before settling for that. Guessing o url file type is the right thing to do most of the time. If not, let users override that. Trying to automagically detect type will add a lot of complexity to your lib... cheers
Arthur Debert
+2  A: 

well, the most tricky question, is determining the type of the target ...

  1. looking at the url is fairly simple, but may not always work ... some people serve images from phps and so on ...
  2. you could do it like browser ... start loading, and then look at what it is ... now again, there's multiple possibilities ...
    1. load the data as binary data ... when done, look at the starting sequence ... is it PNG (89 50 4E 47 0D 0A 1A 0A)? GIF (47 49 46 38 39 61)? JPEG (FF E0)? SWF ("FWS" (funny, isn't it?))? anything else should be text or text based ... in the case of an image, load it into a Loader with Loader::loadBytes ... be careful with SWF though ... you should only load graphicals SWFs like that ... in any other case, convert it to a String using the right encoding (ideally text data is served in utf8) ... then maybe you can already guess, whether it can be XML, JSON or URL-encoded variables ... try parsing (using classes XML, com.adobe.serialization.JSON, flash.net.URLVariables) ... if everything fails, it's probably just text (you can try to verify that superficially ... if you want some input on that, leave me a comment) ...
    2. do the HTTP yourself ... open a socket and load the source ... you will get mime-types in addition ... nothing you can rely on, but it helps ... there is an HTTP implementation in AS3 ... once you have the data
  3. pass the type manually ... pure and simple ... and you don't rely on anyone else ...

there is also an important difference between Loader and URLLoader ... Loader can load data accross domains, simply sandboxing its content so you cannot look into it ... URLLoader can only load from your domain, and domains that explicitely allow this using cross domain policy files ...

also, loading SWFs, that are not just external graphical assets, but you really want to interface with, you should not use this, since you need control over the LoaderContext etc. ...

so, yeah ... whatever way you choose, good luck ... ;)

back2dos
Thanks for the in-depth answer! I didn't know the security sandbox angle, and the idea of looking at the file's opening sequence is intriguing... you might be right, however, in the end it's easier for me to give them the option to manually specify type then it is for me to try to take care of it for them.
matt lohkamp