home *** CD-ROM | disk | FTP | other *** search
- package mochicrypt
- {
- import flash.display.*;
- import flash.events.*;
- import flash.net.*;
- import flash.utils.*;
-
- public dynamic class Preloader extends MovieClip
- {
-
- private static const AD_BACKGROUND:Boolean = Config.getBool("adBackground",false);
-
- private static const AD_URL:String = Config.getString("adURL","http://x.mochiads.com/srv/1/");
-
- private static const PATCH_URL:String = Config.getString("patchURL","http://cdn.mochiads.com/patch.swf");
-
- private static const ID:String = Config.getString("id","test");
-
- private static const PAYLOAD_NAME:String = "mochicrypt.Payload";
-
- private static const VERSION:String = "2.3c";
-
-
- private var payloadLoader:Loader;
-
- private var patchLoader:Loader;
-
- private var adDone:Boolean = false;
-
- private var theme:Theme;
-
- private var adServerControl:Boolean = false;
-
- private var adLoader:Loader;
-
- private var adDuration:int = 11000;
-
- private var payloadProgress:Number = 0;
-
- private var patchProgress:Number = 0;
-
- private var lastProgress:Number = 0;
-
- private var patchDisabled:Boolean = false;
-
- private var adStarted:int;
-
- private var patchDone:Boolean = false;
-
- private var payloadDone:Boolean = false;
-
- private var origFrameRate:Number = NaN;
-
- private var adComplete:Boolean = false;
-
- private var lc:LocalConnection;
-
- public function Preloader()
- {
- lc = new LocalConnection();
- adLoader = new Loader();
- patchLoader = new Loader();
- adStarted = getTimer();
- super();
- loaderInfo.addEventListener(ProgressEvent.PROGRESS,progressHandler);
- loaderInfo.addEventListener(Event.INIT,initHandler);
- loaderInfo.addEventListener(Event.COMPLETE,completeHandler);
- loaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
- }
-
- private function patchProgressHandler(param1:ProgressEvent) : void
- {
- if(param1.bytesLoaded == param1.bytesTotal)
- {
- patchProgress = NaN;
- }
- else
- {
- patchProgress = Number(param1.bytesLoaded) / param1.bytesTotal;
- }
- progress();
- }
-
- private function adIOErrorHandler(param1:IOErrorEvent) : void
- {
- adDone = true;
- finish();
- }
-
- private function makeLCClient() : Object
- {
- var obj:Object = {};
- obj.unloadAd = function():void
- {
- trace("unloadAd");
- adDone = true;
- finish();
- };
- obj.adSkipped = function():void
- {
- trace("adSkipped");
- adDone = true;
- finish();
- };
- obj.adLoaded = function(param1:Number, param2:Number):void
- {
- trace("adLoaded called");
- theme.adLoaded(param1,param2);
- };
- obj.adjustProgress = function(param1:Number):void
- {
- trace("adjustProgress called");
- adServerControl = true;
- adStarted = getTimer();
- adDuration = param1;
- };
- obj.adjustFrameRate = this.adjustFrameRate;
- obj.disablePatch = function():void
- {
- patchDisabled = true;
- };
- return obj;
- }
-
- private function finish() : void
- {
- var payloadClass:Class;
- var data:ByteArray;
- var S:ByteArray;
- var i:uint = 0;
- var j:uint = 0;
- var k:uint = 0;
- var n:uint = 0;
- var u:uint = 0;
- var v:uint = 0;
- if(!payloadDone || !adDone || !patchDone)
- {
- return;
- }
- if(payloadLoader)
- {
- trace("unexpected call to finish()");
- return;
- }
- payloadClass = Class(getDefinitionByName(PAYLOAD_NAME));
- data = ByteArray(new payloadClass());
- trace("data.length",data.length);
- S = new ByteArray();
- n = uint(data.length - 16);
- i = 0;
- while(i < 256)
- {
- S.writeByte(i);
- i++;
- }
- j = 0;
- i = 0;
- while(i < 256)
- {
- j = uint(j + S[i] + data[n + (i & 15)] & 255);
- u = uint(S[i]);
- S[i] = S[j];
- S[j] = u;
- i++;
- }
- if(n > 131072)
- {
- n = 131072;
- }
- i = j = 0;
- k = 0;
- while(k < n)
- {
- i = uint(i + 1 & 255);
- u = uint(S[i]);
- j = uint(j + u & 255);
- v = uint(S[j]);
- S[i] = v;
- S[j] = u;
- data[k] ^= S[u + v & 255];
- k++;
- }
- data.uncompress();
- try
- {
- data = patchLoader.content["patch"](data);
- }
- catch(error:Error)
- {
- trace("patch failed",error);
- }
- try
- {
- if(!isNaN(origFrameRate))
- {
- stage.frameRate = origFrameRate;
- }
- }
- catch(error:Error)
- {
- trace("resetting frameRate failed",error);
- }
- payloadLoader = new Loader();
- addChild(payloadLoader);
- payloadLoader.loadBytes(data);
- patchLoader.unload();
- adLoader.unload();
- trace("ad should be gone...");
- removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
- loaderInfo.removeEventListener(ProgressEvent.PROGRESS,progressHandler);
- loaderInfo.removeEventListener(Event.INIT,initHandler);
- loaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
- loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
- patchLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,patchIOErrorHandler);
- patchLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,patchProgressHandler);
- patchLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE,patchCompleteHandler);
- adLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,adIOErrorHandler);
- lc.close();
- removeChild(theme);
- theme.removeChild(adLoader);
- lc = null;
- adLoader = null;
- patchLoader = null;
- theme = null;
- }
-
- private function enterFrameHandler(param1:Event) : void
- {
- progress();
- if(!payloadDone && currentFrame == 2)
- {
- payloadDone = true;
- finish();
- }
- }
-
- private function initHandler(param1:Event) : void
- {
- var lv:URLVariables;
- var lcName:String;
- var req:URLRequest;
- var event:Event = param1;
- try
- {
- origFrameRate = stage.frameRate;
- }
- catch(error:Error)
- {
- trace("can\'t access stage.frameRate in initHandler",error);
- }
- adjustFrameRate(30);
- lcName = ["",Math.floor(new Date().getTime()),Math.floor(Math.random() * 999999)].join("_");
- lc.client = makeLCClient();
- lc.allowDomain("*","x.mochiads.com");
- lc.allowInsecureDomain("*","x.mochiads.com");
- lc.connect(lcName);
- adLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,adIOErrorHandler);
- adLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,adCompleteHandler);
- lv = new URLVariables();
- lv.id = ID;
- lv.res = "" + loaderInfo.width + "x" + loaderInfo.height;
- lv.method = "showPreloaderAd";
- lv.swfv = loaderInfo.swfVersion;
- lv.mav = VERSION;
- lv.lc = lcName;
- lv.st = getTimer();
- lv.as3_swf = loaderInfo.loaderURL;
- lv.no_bg = !AD_BACKGROUND;
- req = new URLRequest(AD_URL + ID + ".swf");
- req.contentType = "application/x-www-form-urlencoded";
- req.method = URLRequestMethod.POST;
- req.data = lv;
- adLoader.x = 0.5 * loaderInfo.width;
- adLoader.y = 0.5 * loaderInfo.height;
- try
- {
- adLoader.load(req);
- }
- catch(error:Error)
- {
- adDone = true;
- }
- patchLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,patchIOErrorHandler);
- patchLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,patchProgressHandler);
- patchLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,patchCompleteHandler);
- try
- {
- patchLoader.load(new URLRequest(PATCH_URL));
- }
- catch(error:Error)
- {
- patchDone = true;
- }
- theme = new Theme(adLoader,loaderInfo.width,loaderInfo.height);
- addChild(theme);
- addEventListener(Event.ENTER_FRAME,enterFrameHandler);
- finish();
- }
-
- private function progressHandler(param1:ProgressEvent) : void
- {
- if(param1.bytesLoaded == param1.bytesTotal)
- {
- payloadProgress = NaN;
- }
- else
- {
- payloadProgress = Number(param1.bytesLoaded) / param1.bytesTotal;
- }
- progress();
- }
-
- private function adjustFrameRate(param1:Number) : void
- {
- var newFrameRate:Number = param1;
- try
- {
- if(!isNaN(origFrameRate))
- {
- stage.frameRate = newFrameRate;
- }
- }
- catch(error:Error)
- {
- trace("couldn\'t adjust stage.frameRate",error);
- }
- }
-
- private function ioErrorHandler(param1:IOErrorEvent) : void
- {
- }
-
- private function patchCompleteHandler(param1:Event) : void
- {
- patchDone = true;
- finish();
- }
-
- private function progress() : void
- {
- var _loc1_:Number = NaN;
- _loc1_ = 1;
- if(!adDone)
- {
- _loc1_ = Number(getTimer() - adStarted) / adDuration;
- }
- if(!payloadDone && !isNaN(payloadProgress) && payloadProgress < _loc1_)
- {
- _loc1_ = payloadProgress;
- }
- if(!patchDone && !isNaN(patchProgress) && patchProgress < _loc1_)
- {
- _loc1_ = patchProgress;
- }
- if(_loc1_ > 1)
- {
- _loc1_ = 1;
- }
- if(_loc1_ > lastProgress)
- {
- lastProgress = _loc1_;
- theme.updateProgress(lastProgress);
- }
- if(!adServerControl && getTimer() > adStarted + adDuration)
- {
- trace("giving up... maybe?");
- if(!adComplete)
- {
- adLoader.close();
- }
- adDone = true;
- finish();
- }
- }
-
- private function patchIOErrorHandler(param1:IOErrorEvent) : void
- {
- patchDone = true;
- finish();
- }
-
- private function adCompleteHandler(param1:Event) : void
- {
- if(!adServerControl)
- {
- adStarted = getTimer();
- }
- adComplete = true;
- progress();
- }
-
- private function completeHandler(param1:Event) : void
- {
- nextFrame();
- }
- }
- }
-