home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Acao / BoomBot.swf / scripts / mochicrypt / Preloader.as < prev    next >
Encoding:
Text File  |  2008-09-23  |  11.6 KB  |  398 lines

  1. package mochicrypt
  2. {
  3.    import flash.display.*;
  4.    import flash.events.*;
  5.    import flash.net.*;
  6.    import flash.utils.*;
  7.    
  8.    public dynamic class Preloader extends MovieClip
  9.    {
  10.       
  11.       private static const AD_BACKGROUND:Boolean = Config.getBool("adBackground",false);
  12.       
  13.       private static const AD_URL:String = Config.getString("adURL","http://x.mochiads.com/srv/1/");
  14.       
  15.       private static const PATCH_URL:String = Config.getString("patchURL","http://cdn.mochiads.com/patch.swf");
  16.       
  17.       private static const ID:String = Config.getString("id","test");
  18.       
  19.       private static const PAYLOAD_NAME:String = "mochicrypt.Payload";
  20.       
  21.       private static const VERSION:String = "2.3c";
  22.        
  23.       
  24.       private var payloadLoader:Loader;
  25.       
  26.       private var patchLoader:Loader;
  27.       
  28.       private var adDone:Boolean = false;
  29.       
  30.       private var theme:Theme;
  31.       
  32.       private var adServerControl:Boolean = false;
  33.       
  34.       private var adLoader:Loader;
  35.       
  36.       private var adDuration:int = 11000;
  37.       
  38.       private var payloadProgress:Number = 0;
  39.       
  40.       private var patchProgress:Number = 0;
  41.       
  42.       private var lastProgress:Number = 0;
  43.       
  44.       private var patchDisabled:Boolean = false;
  45.       
  46.       private var adStarted:int;
  47.       
  48.       private var patchDone:Boolean = false;
  49.       
  50.       private var payloadDone:Boolean = false;
  51.       
  52.       private var origFrameRate:Number = NaN;
  53.       
  54.       private var adComplete:Boolean = false;
  55.       
  56.       private var lc:LocalConnection;
  57.       
  58.       public function Preloader()
  59.       {
  60.          lc = new LocalConnection();
  61.          adLoader = new Loader();
  62.          patchLoader = new Loader();
  63.          adStarted = getTimer();
  64.          super();
  65.          loaderInfo.addEventListener(ProgressEvent.PROGRESS,progressHandler);
  66.          loaderInfo.addEventListener(Event.INIT,initHandler);
  67.          loaderInfo.addEventListener(Event.COMPLETE,completeHandler);
  68.          loaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
  69.       }
  70.       
  71.       private function patchProgressHandler(param1:ProgressEvent) : void
  72.       {
  73.          if(param1.bytesLoaded == param1.bytesTotal)
  74.          {
  75.             patchProgress = NaN;
  76.          }
  77.          else
  78.          {
  79.             patchProgress = Number(param1.bytesLoaded) / param1.bytesTotal;
  80.          }
  81.          progress();
  82.       }
  83.       
  84.       private function adIOErrorHandler(param1:IOErrorEvent) : void
  85.       {
  86.          adDone = true;
  87.          finish();
  88.       }
  89.       
  90.       private function makeLCClient() : Object
  91.       {
  92.          var obj:Object = {};
  93.          obj.unloadAd = function():void
  94.          {
  95.             trace("unloadAd");
  96.             adDone = true;
  97.             finish();
  98.          };
  99.          obj.adSkipped = function():void
  100.          {
  101.             trace("adSkipped");
  102.             adDone = true;
  103.             finish();
  104.          };
  105.          obj.adLoaded = function(param1:Number, param2:Number):void
  106.          {
  107.             trace("adLoaded called");
  108.             theme.adLoaded(param1,param2);
  109.          };
  110.          obj.adjustProgress = function(param1:Number):void
  111.          {
  112.             trace("adjustProgress called");
  113.             adServerControl = true;
  114.             adStarted = getTimer();
  115.             adDuration = param1;
  116.          };
  117.          obj.adjustFrameRate = this.adjustFrameRate;
  118.          obj.disablePatch = function():void
  119.          {
  120.             patchDisabled = true;
  121.          };
  122.          return obj;
  123.       }
  124.       
  125.       private function finish() : void
  126.       {
  127.          var payloadClass:Class;
  128.          var data:ByteArray;
  129.          var S:ByteArray;
  130.          var i:uint = 0;
  131.          var j:uint = 0;
  132.          var k:uint = 0;
  133.          var n:uint = 0;
  134.          var u:uint = 0;
  135.          var v:uint = 0;
  136.          if(!payloadDone || !adDone || !patchDone)
  137.          {
  138.             return;
  139.          }
  140.          if(payloadLoader)
  141.          {
  142.             trace("unexpected call to finish()");
  143.             return;
  144.          }
  145.          payloadClass = Class(getDefinitionByName(PAYLOAD_NAME));
  146.          data = ByteArray(new payloadClass());
  147.          trace("data.length",data.length);
  148.          S = new ByteArray();
  149.          n = uint(data.length - 16);
  150.          i = 0;
  151.          while(i < 256)
  152.          {
  153.             S.writeByte(i);
  154.             i++;
  155.          }
  156.          j = 0;
  157.          i = 0;
  158.          while(i < 256)
  159.          {
  160.             j = uint(j + S[i] + data[n + (i & 15)] & 255);
  161.             u = uint(S[i]);
  162.             S[i] = S[j];
  163.             S[j] = u;
  164.             i++;
  165.          }
  166.          if(n > 131072)
  167.          {
  168.             n = 131072;
  169.          }
  170.          i = j = 0;
  171.          k = 0;
  172.          while(k < n)
  173.          {
  174.             i = uint(i + 1 & 255);
  175.             u = uint(S[i]);
  176.             j = uint(j + u & 255);
  177.             v = uint(S[j]);
  178.             S[i] = v;
  179.             S[j] = u;
  180.             data[k] ^= S[u + v & 255];
  181.             k++;
  182.          }
  183.          data.uncompress();
  184.          try
  185.          {
  186.             data = patchLoader.content["patch"](data);
  187.          }
  188.          catch(error:Error)
  189.          {
  190.             trace("patch failed",error);
  191.          }
  192.          try
  193.          {
  194.             if(!isNaN(origFrameRate))
  195.             {
  196.                stage.frameRate = origFrameRate;
  197.             }
  198.          }
  199.          catch(error:Error)
  200.          {
  201.             trace("resetting frameRate failed",error);
  202.          }
  203.          payloadLoader = new Loader();
  204.          addChild(payloadLoader);
  205.          payloadLoader.loadBytes(data);
  206.          patchLoader.unload();
  207.          adLoader.unload();
  208.          trace("ad should be gone...");
  209.          removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
  210.          loaderInfo.removeEventListener(ProgressEvent.PROGRESS,progressHandler);
  211.          loaderInfo.removeEventListener(Event.INIT,initHandler);
  212.          loaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
  213.          loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
  214.          patchLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,patchIOErrorHandler);
  215.          patchLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,patchProgressHandler);
  216.          patchLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE,patchCompleteHandler);
  217.          adLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,adIOErrorHandler);
  218.          lc.close();
  219.          removeChild(theme);
  220.          theme.removeChild(adLoader);
  221.          lc = null;
  222.          adLoader = null;
  223.          patchLoader = null;
  224.          theme = null;
  225.       }
  226.       
  227.       private function enterFrameHandler(param1:Event) : void
  228.       {
  229.          progress();
  230.          if(!payloadDone && currentFrame == 2)
  231.          {
  232.             payloadDone = true;
  233.             finish();
  234.          }
  235.       }
  236.       
  237.       private function initHandler(param1:Event) : void
  238.       {
  239.          var lv:URLVariables;
  240.          var lcName:String;
  241.          var req:URLRequest;
  242.          var event:Event = param1;
  243.          try
  244.          {
  245.             origFrameRate = stage.frameRate;
  246.          }
  247.          catch(error:Error)
  248.          {
  249.             trace("can\'t access stage.frameRate in initHandler",error);
  250.          }
  251.          adjustFrameRate(30);
  252.          lcName = ["",Math.floor(new Date().getTime()),Math.floor(Math.random() * 999999)].join("_");
  253.          lc.client = makeLCClient();
  254.          lc.allowDomain("*","x.mochiads.com");
  255.          lc.allowInsecureDomain("*","x.mochiads.com");
  256.          lc.connect(lcName);
  257.          adLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,adIOErrorHandler);
  258.          adLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,adCompleteHandler);
  259.          lv = new URLVariables();
  260.          lv.id = ID;
  261.          lv.res = "" + loaderInfo.width + "x" + loaderInfo.height;
  262.          lv.method = "showPreloaderAd";
  263.          lv.swfv = loaderInfo.swfVersion;
  264.          lv.mav = VERSION;
  265.          lv.lc = lcName;
  266.          lv.st = getTimer();
  267.          lv.as3_swf = loaderInfo.loaderURL;
  268.          lv.no_bg = !AD_BACKGROUND;
  269.          req = new URLRequest(AD_URL + ID + ".swf");
  270.          req.contentType = "application/x-www-form-urlencoded";
  271.          req.method = URLRequestMethod.POST;
  272.          req.data = lv;
  273.          adLoader.x = 0.5 * loaderInfo.width;
  274.          adLoader.y = 0.5 * loaderInfo.height;
  275.          try
  276.          {
  277.             adLoader.load(req);
  278.          }
  279.          catch(error:Error)
  280.          {
  281.             adDone = true;
  282.          }
  283.          patchLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,patchIOErrorHandler);
  284.          patchLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,patchProgressHandler);
  285.          patchLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,patchCompleteHandler);
  286.          try
  287.          {
  288.             patchLoader.load(new URLRequest(PATCH_URL));
  289.          }
  290.          catch(error:Error)
  291.          {
  292.             patchDone = true;
  293.          }
  294.          theme = new Theme(adLoader,loaderInfo.width,loaderInfo.height);
  295.          addChild(theme);
  296.          addEventListener(Event.ENTER_FRAME,enterFrameHandler);
  297.          finish();
  298.       }
  299.       
  300.       private function progressHandler(param1:ProgressEvent) : void
  301.       {
  302.          if(param1.bytesLoaded == param1.bytesTotal)
  303.          {
  304.             payloadProgress = NaN;
  305.          }
  306.          else
  307.          {
  308.             payloadProgress = Number(param1.bytesLoaded) / param1.bytesTotal;
  309.          }
  310.          progress();
  311.       }
  312.       
  313.       private function adjustFrameRate(param1:Number) : void
  314.       {
  315.          var newFrameRate:Number = param1;
  316.          try
  317.          {
  318.             if(!isNaN(origFrameRate))
  319.             {
  320.                stage.frameRate = newFrameRate;
  321.             }
  322.          }
  323.          catch(error:Error)
  324.          {
  325.             trace("couldn\'t adjust stage.frameRate",error);
  326.          }
  327.       }
  328.       
  329.       private function ioErrorHandler(param1:IOErrorEvent) : void
  330.       {
  331.       }
  332.       
  333.       private function patchCompleteHandler(param1:Event) : void
  334.       {
  335.          patchDone = true;
  336.          finish();
  337.       }
  338.       
  339.       private function progress() : void
  340.       {
  341.          var _loc1_:Number = NaN;
  342.          _loc1_ = 1;
  343.          if(!adDone)
  344.          {
  345.             _loc1_ = Number(getTimer() - adStarted) / adDuration;
  346.          }
  347.          if(!payloadDone && !isNaN(payloadProgress) && payloadProgress < _loc1_)
  348.          {
  349.             _loc1_ = payloadProgress;
  350.          }
  351.          if(!patchDone && !isNaN(patchProgress) && patchProgress < _loc1_)
  352.          {
  353.             _loc1_ = patchProgress;
  354.          }
  355.          if(_loc1_ > 1)
  356.          {
  357.             _loc1_ = 1;
  358.          }
  359.          if(_loc1_ > lastProgress)
  360.          {
  361.             lastProgress = _loc1_;
  362.             theme.updateProgress(lastProgress);
  363.          }
  364.          if(!adServerControl && getTimer() > adStarted + adDuration)
  365.          {
  366.             trace("giving up... maybe?");
  367.             if(!adComplete)
  368.             {
  369.                adLoader.close();
  370.             }
  371.             adDone = true;
  372.             finish();
  373.          }
  374.       }
  375.       
  376.       private function patchIOErrorHandler(param1:IOErrorEvent) : void
  377.       {
  378.          patchDone = true;
  379.          finish();
  380.       }
  381.       
  382.       private function adCompleteHandler(param1:Event) : void
  383.       {
  384.          if(!adServerControl)
  385.          {
  386.             adStarted = getTimer();
  387.          }
  388.          adComplete = true;
  389.          progress();
  390.       }
  391.       
  392.       private function completeHandler(param1:Event) : void
  393.       {
  394.          nextFrame();
  395.       }
  396.    }
  397. }
  398.