Newer
Older
exporter / Job.as
package
{
	import flash.utils.*;
	import flash.display.*;
	import flash.system.LoaderContext;
	import flash.events.*;

	public class Job
	{
		private var callback;
		
		private var id:int;
		private var name:String;
		private var input:ByteArray;
		private var details;
		private var inputLoader:Loader = null;
		public function GetID():int { return this.id; }
		public function Job(id:int, name:String, input:ByteArray, details)
		{
			this.id = id;
			this.name = name;
			this.input = input;
			this.details = details;
			Exporter.Instance.Trace("new job: " + id);
		}
		private function Fail(err:String)
		{
			Exporter.Instance.Print("Failure: job " + id + ": " + err);
			callback();
		}
		public function Go(callback)
		{
			this.callback = callback;
			
			if (this.input == null)
			{				
				Fail("contained no input");
				return;
			}			
			
			inputLoader = new Loader();
			inputLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, LoaderComplete);
			inputLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, LoaderError);
			inputLoader.loadBytes(input, new LoaderContext());
		}
		private function LoaderComplete(e)
		{
			try
			{
				var classDef = e.target.applicationDomain.getDefinition(name);
				if (classDef == null)
				{
					Fail("didn't contain a definition for " + name);
					return;
				}
				
				var clip = new classDef();
				if (!(clip is MovieClip))
				{
					Fail(name + " wasn't a movieclip");
				}
				
				Utils.RecursivelyStop(clip);
				
				Done();
			} catch (e)
			{
				Exporter.Instance.Print(e.getStackTrace());
			}
		}
		private function LoaderError(e)
		{
			Fail("couldn't load input: " + e.toString());
		}
		public function Done()
		{
			Exporter.Instance.Trace("Job.Done() " + id);
			this.callback();
		}
		public function GetCompletedData()
		{
			return {id:id};
		}
	}
}