home *** CD-ROM | disk | FTP | other *** search
/ Micromanía: 150 Juegos 2010 / 150Juegos_16.iso / Shareware / Shape Smash / shape-smash.swf / scripts / mx / modules / ModuleManager.as < prev    next >
Encoding:
Text File  |  2010-05-14  |  14.2 KB  |  555 lines

  1. package mx.modules
  2. {
  3.    import mx.core.IFlexModuleFactory;
  4.    import mx.core.mx_internal;
  5.    
  6.    use namespace mx_internal;
  7.    
  8.    public class ModuleManager
  9.    {
  10.       mx_internal static const VERSION:String = "2.0.1.0";
  11.       
  12.       public function ModuleManager()
  13.       {
  14.          super();
  15.       }
  16.       
  17.       public static function getModule(param1:String) : IModuleInfo
  18.       {
  19.          return getSingleton().getModule(param1);
  20.       }
  21.       
  22.       private static function getSingleton() : Object
  23.       {
  24.          if(!ModuleManagerGlobals.managerSingleton)
  25.          {
  26.             ModuleManagerGlobals.managerSingleton = new ModuleManagerImpl();
  27.          }
  28.          return ModuleManagerGlobals.managerSingleton;
  29.       }
  30.       
  31.       public static function getAssociatedFactory(param1:Object) : IFlexModuleFactory
  32.       {
  33.          return getSingleton().getAssociatedFactory(param1);
  34.       }
  35.    }
  36. }
  37.  
  38. import flash.display.Loader;
  39. import flash.events.ErrorEvent;
  40. import flash.events.Event;
  41. import flash.events.EventDispatcher;
  42. import flash.events.IOErrorEvent;
  43. import flash.events.ProgressEvent;
  44. import flash.events.SecurityErrorEvent;
  45. import flash.net.URLRequest;
  46. import flash.system.ApplicationDomain;
  47. import flash.system.LoaderContext;
  48. import flash.system.Security;
  49. import flash.system.SecurityDomain;
  50. import flash.utils.Dictionary;
  51. import flash.utils.getQualifiedClassName;
  52. import mx.core.IFlexModuleFactory;
  53. import mx.events.ModuleEvent;
  54.  
  55. class ModuleInfoProxy extends EventDispatcher implements IModuleInfo
  56. {
  57.    private var _data:Object;
  58.    
  59.    private var info:ModuleInfo;
  60.    
  61.    private var referenced:Boolean = false;
  62.    
  63.    public function ModuleInfoProxy(param1:ModuleInfo)
  64.    {
  65.       referenced = false;
  66.       super();
  67.       this.info = param1;
  68.       param1.addEventListener(ModuleEvent.SETUP,moduleEventHandler);
  69.       param1.addEventListener(ModuleEvent.PROGRESS,moduleEventHandler);
  70.       param1.addEventListener(ModuleEvent.READY,moduleEventHandler);
  71.       param1.addEventListener(ModuleEvent.ERROR,moduleEventHandler);
  72.       param1.addEventListener(ModuleEvent.UNLOAD,moduleEventHandler);
  73.    }
  74.    
  75.    public function get loaded() : Boolean
  76.    {
  77.       return info.loaded;
  78.    }
  79.    
  80.    public function get error() : Boolean
  81.    {
  82.       return info.error;
  83.    }
  84.    
  85.    public function set data(param1:Object) : void
  86.    {
  87.       _data = param1;
  88.    }
  89.    
  90.    public function publish(param1:IFlexModuleFactory) : void
  91.    {
  92.       info.publish(param1);
  93.    }
  94.    
  95.    public function get factory() : IFlexModuleFactory
  96.    {
  97.       return info.factory;
  98.    }
  99.    
  100.    public function release() : void
  101.    {
  102.       if(referenced)
  103.       {
  104.          info.removeReference();
  105.          referenced = false;
  106.       }
  107.    }
  108.    
  109.    public function get ready() : Boolean
  110.    {
  111.       return info.ready;
  112.    }
  113.    
  114.    public function load(param1:ApplicationDomain = null, param2:SecurityDomain = null) : void
  115.    {
  116.       var _loc3_:ModuleEvent = null;
  117.       info.resurrect();
  118.       if(!referenced)
  119.       {
  120.          info.addReference();
  121.          referenced = true;
  122.       }
  123.       if(info.error)
  124.       {
  125.          dispatchEvent(new ModuleEvent(ModuleEvent.ERROR));
  126.       }
  127.       else if(info.loaded)
  128.       {
  129.          if(info.setup)
  130.          {
  131.             dispatchEvent(new ModuleEvent(ModuleEvent.SETUP));
  132.             if(info.ready)
  133.             {
  134.                _loc3_ = new ModuleEvent(ModuleEvent.PROGRESS);
  135.                _loc3_.bytesLoaded = info.size;
  136.                _loc3_.bytesTotal = info.size;
  137.                dispatchEvent(_loc3_);
  138.                dispatchEvent(new ModuleEvent(ModuleEvent.READY));
  139.             }
  140.          }
  141.       }
  142.       else
  143.       {
  144.          info.load(param1,param2);
  145.       }
  146.    }
  147.    
  148.    public function get data() : Object
  149.    {
  150.       return _data;
  151.    }
  152.    
  153.    private function moduleEventHandler(param1:ModuleEvent) : void
  154.    {
  155.       dispatchEvent(param1);
  156.    }
  157.    
  158.    public function get url() : String
  159.    {
  160.       return info.url;
  161.    }
  162.    
  163.    public function get setup() : Boolean
  164.    {
  165.       return info.setup;
  166.    }
  167.    
  168.    public function unload() : void
  169.    {
  170.       info.unload();
  171.       info.removeEventListener(ModuleEvent.SETUP,moduleEventHandler);
  172.       info.removeEventListener(ModuleEvent.PROGRESS,moduleEventHandler);
  173.       info.removeEventListener(ModuleEvent.READY,moduleEventHandler);
  174.       info.removeEventListener(ModuleEvent.ERROR,moduleEventHandler);
  175.       info.removeEventListener(ModuleEvent.UNLOAD,moduleEventHandler);
  176.    }
  177. }
  178.  
  179. class ModuleManagerImpl extends EventDispatcher
  180. {
  181.    private var moduleList:Object;
  182.    
  183.    public function ModuleManagerImpl()
  184.    {
  185.       moduleList = {};
  186.       super();
  187.    }
  188.    
  189.    public function getModule(param1:String) : IModuleInfo
  190.    {
  191.       var _loc2_:ModuleInfo = null;
  192.       _loc2_ = moduleList[param1] as ModuleInfo;
  193.       if(!_loc2_)
  194.       {
  195.          _loc2_ = new ModuleInfo(param1);
  196.          moduleList[param1] = _loc2_;
  197.       }
  198.       return new ModuleInfoProxy(_loc2_);
  199.    }
  200.    
  201.    public function getAssociatedFactory(param1:Object) : IFlexModuleFactory
  202.    {
  203.       var className:String = null;
  204.       var m:Object = null;
  205.       var info:ModuleInfo = null;
  206.       var domain:ApplicationDomain = null;
  207.       var cls:Class = null;
  208.       var object:Object = param1;
  209.       className = getQualifiedClassName(object);
  210.       for each(m in moduleList)
  211.       {
  212.          info = m as ModuleInfo;
  213.          if(info.ready)
  214.          {
  215.             domain = info.applicationDomain;
  216.             try
  217.             {
  218.                cls = Class(domain.getDefinition(className));
  219.                if(object is cls)
  220.                {
  221.                   return info.factory;
  222.                }
  223.             }
  224.             catch(error:Error)
  225.             {
  226.                continue;
  227.             }
  228.          }
  229.       }
  230.       return null;
  231.    }
  232. }
  233.  
  234. class ModuleInfo extends EventDispatcher
  235. {
  236.    private var _error:Boolean = false;
  237.    
  238.    private var loader:Loader;
  239.    
  240.    private var _loaded:Boolean = false;
  241.    
  242.    private var _ready:Boolean = false;
  243.    
  244.    private var numReferences:int = 0;
  245.    
  246.    private var _url:String;
  247.    
  248.    private var factoryInfo:FactoryInfo;
  249.    
  250.    private var limbo:Dictionary;
  251.    
  252.    private var _setup:Boolean = false;
  253.    
  254.    public function ModuleInfo(param1:String)
  255.    {
  256.       numReferences = 0;
  257.       _error = false;
  258.       _loaded = false;
  259.       _ready = false;
  260.       _setup = false;
  261.       super();
  262.       _url = param1;
  263.    }
  264.    
  265.    public function get loaded() : Boolean
  266.    {
  267.       return !limbo ? Boolean(_loaded) : false;
  268.    }
  269.    
  270.    public function get error() : Boolean
  271.    {
  272.       return !limbo ? Boolean(_error) : false;
  273.    }
  274.    
  275.    public function get factory() : IFlexModuleFactory
  276.    {
  277.       return !limbo && Boolean(factoryInfo) ? factoryInfo.factory : null;
  278.    }
  279.    
  280.    public function release() : void
  281.    {
  282.       if(Boolean(_ready) && !limbo)
  283.       {
  284.          limbo = new Dictionary(true);
  285.          limbo[factoryInfo] = 1;
  286.          factoryInfo = null;
  287.       }
  288.       else
  289.       {
  290.          unload();
  291.       }
  292.    }
  293.    
  294.    public function get size() : int
  295.    {
  296.       return !limbo && Boolean(factoryInfo) ? int(factoryInfo.bytesTotal) : 0;
  297.    }
  298.    
  299.    public function publish(param1:IFlexModuleFactory) : void
  300.    {
  301.       if(factoryInfo)
  302.       {
  303.          return;
  304.       }
  305.       if(_url.indexOf("published://") != 0)
  306.       {
  307.          return;
  308.       }
  309.       factoryInfo = new FactoryInfo();
  310.       factoryInfo.factory = param1;
  311.       _loaded = true;
  312.       _setup = true;
  313.       _ready = true;
  314.       _error = false;
  315.       dispatchEvent(new ModuleEvent(ModuleEvent.SETUP));
  316.       dispatchEvent(new ModuleEvent(ModuleEvent.PROGRESS));
  317.       dispatchEvent(new ModuleEvent(ModuleEvent.READY));
  318.    }
  319.    
  320.    public function initHandler(param1:Event) : void
  321.    {
  322.       var moduleEvent:ModuleEvent = null;
  323.       var event:Event = param1;
  324.       factoryInfo = new FactoryInfo();
  325.       try
  326.       {
  327.          factoryInfo.factory = loader.content as IFlexModuleFactory;
  328.       }
  329.       catch(error:Error)
  330.       {
  331.       }
  332.       if(!factoryInfo.factory)
  333.       {
  334.          moduleEvent = new ModuleEvent(ModuleEvent.ERROR,event.bubbles,event.cancelable);
  335.          moduleEvent.bytesLoaded = 0;
  336.          moduleEvent.bytesTotal = 0;
  337.          moduleEvent.errorText = "SWF is not a loadable module";
  338.          dispatchEvent(moduleEvent);
  339.          return;
  340.       }
  341.       loader.content.addEventListener("ready",readyHandler);
  342.       try
  343.       {
  344.          factoryInfo.applicationDomain = loader.contentLoaderInfo.applicationDomain;
  345.       }
  346.       catch(error:Error)
  347.       {
  348.       }
  349.       _setup = true;
  350.       dispatchEvent(new ModuleEvent(ModuleEvent.SETUP));
  351.    }
  352.    
  353.    public function resurrect() : void
  354.    {
  355.       var _loc1_:Object = null;
  356.       if(!factoryInfo && Boolean(limbo))
  357.       {
  358.          var _loc2_:int = 0;
  359.          var _loc3_:* = limbo;
  360.          for(_loc1_ in _loc3_)
  361.          {
  362.             factoryInfo = _loc1_ as FactoryInfo;
  363.          }
  364.          limbo = null;
  365.       }
  366.       if(!factoryInfo)
  367.       {
  368.          if(_loaded)
  369.          {
  370.             dispatchEvent(new ModuleEvent(ModuleEvent.UNLOAD));
  371.          }
  372.          loader = null;
  373.          _loaded = false;
  374.          _setup = false;
  375.          _ready = false;
  376.          _error = false;
  377.       }
  378.    }
  379.    
  380.    public function errorHandler(param1:ErrorEvent) : void
  381.    {
  382.       var _loc2_:ModuleEvent = null;
  383.       _error = true;
  384.       _loc2_ = new ModuleEvent(ModuleEvent.ERROR,param1.bubbles,param1.cancelable);
  385.       _loc2_.bytesLoaded = 0;
  386.       _loc2_.bytesTotal = 0;
  387.       _loc2_.errorText = param1.text;
  388.       dispatchEvent(_loc2_);
  389.    }
  390.    
  391.    public function get ready() : Boolean
  392.    {
  393.       return !limbo ? Boolean(_ready) : false;
  394.    }
  395.    
  396.    public function removeReference() : void
  397.    {
  398.       --numReferences;
  399.       if(numReferences == 0)
  400.       {
  401.          release();
  402.       }
  403.    }
  404.    
  405.    public function addReference() : void
  406.    {
  407.       ++numReferences;
  408.    }
  409.    
  410.    public function get applicationDomain() : ApplicationDomain
  411.    {
  412.       return !limbo && Boolean(factoryInfo) ? factoryInfo.applicationDomain : null;
  413.    }
  414.    
  415.    public function readyHandler(param1:Event) : void
  416.    {
  417.       _ready = true;
  418.       factoryInfo.bytesTotal = loader.contentLoaderInfo.bytesTotal;
  419.       clearLoader();
  420.       dispatchEvent(new ModuleEvent(ModuleEvent.READY));
  421.    }
  422.    
  423.    private function clearLoader() : void
  424.    {
  425.       if(loader)
  426.       {
  427.          if(loader.contentLoaderInfo)
  428.          {
  429.             loader.contentLoaderInfo.removeEventListener(Event.INIT,initHandler);
  430.             loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
  431.             loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,progressHandler);
  432.             loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,errorHandler);
  433.             loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,errorHandler);
  434.          }
  435.          try
  436.          {
  437.             if(loader.content)
  438.             {
  439.                loader.content.removeEventListener("ready",readyHandler);
  440.             }
  441.          }
  442.          catch(error:Error)
  443.          {
  444.          }
  445.          if(_loaded)
  446.          {
  447.             try
  448.             {
  449.                loader.close();
  450.             }
  451.             catch(error:Error)
  452.             {
  453.             }
  454.          }
  455.          try
  456.          {
  457.             loader.unload();
  458.          }
  459.          catch(error:Error)
  460.          {
  461.          }
  462.          loader = null;
  463.       }
  464.    }
  465.    
  466.    public function progressHandler(param1:ProgressEvent) : void
  467.    {
  468.       var _loc2_:ModuleEvent = null;
  469.       _loc2_ = new ModuleEvent(ModuleEvent.PROGRESS,param1.bubbles,param1.cancelable);
  470.       _loc2_.bytesLoaded = param1.bytesLoaded;
  471.       _loc2_.bytesTotal = param1.bytesTotal;
  472.       dispatchEvent(_loc2_);
  473.    }
  474.    
  475.    public function load(param1:ApplicationDomain = null, param2:SecurityDomain = null) : void
  476.    {
  477.       var _loc3_:URLRequest = null;
  478.       var _loc4_:LoaderContext = null;
  479.       if(_loaded)
  480.       {
  481.          return;
  482.       }
  483.       _loaded = true;
  484.       limbo = null;
  485.       if(_url.indexOf("published://") == 0)
  486.       {
  487.          return;
  488.       }
  489.       _loc3_ = new URLRequest(_url);
  490.       _loc4_ = new LoaderContext();
  491.       _loc4_.applicationDomain = !!param1 ? param1 : new ApplicationDomain(ApplicationDomain.currentDomain);
  492.       _loc4_.securityDomain = param2;
  493.       if(param2 == null && Security.sandboxType == Security.REMOTE)
  494.       {
  495.          _loc4_.securityDomain = SecurityDomain.currentDomain;
  496.       }
  497.       loader = new Loader();
  498.       loader.contentLoaderInfo.addEventListener(Event.INIT,initHandler);
  499.       loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
  500.       loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressHandler);
  501.       loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
  502.       loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,errorHandler);
  503.       loader.load(_loc3_,_loc4_);
  504.    }
  505.    
  506.    public function completeHandler(param1:Event) : void
  507.    {
  508.       var _loc2_:ModuleEvent = null;
  509.       _loc2_ = new ModuleEvent(ModuleEvent.PROGRESS,param1.bubbles,param1.cancelable);
  510.       _loc2_.bytesLoaded = loader.contentLoaderInfo.bytesLoaded;
  511.       _loc2_.bytesTotal = loader.contentLoaderInfo.bytesTotal;
  512.       dispatchEvent(_loc2_);
  513.    }
  514.    
  515.    public function get url() : String
  516.    {
  517.       return _url;
  518.    }
  519.    
  520.    public function get setup() : Boolean
  521.    {
  522.       return !limbo ? Boolean(_setup) : false;
  523.    }
  524.    
  525.    public function unload() : void
  526.    {
  527.       clearLoader();
  528.       if(_loaded)
  529.       {
  530.          dispatchEvent(new ModuleEvent(ModuleEvent.UNLOAD));
  531.       }
  532.       limbo = null;
  533.       factoryInfo = null;
  534.       _loaded = false;
  535.       _setup = false;
  536.       _ready = false;
  537.       _error = false;
  538.    }
  539. }
  540.  
  541. class FactoryInfo
  542. {
  543.    public var bytesTotal:int = 0;
  544.    
  545.    public var factory:IFlexModuleFactory;
  546.    
  547.    public var applicationDomain:ApplicationDomain;
  548.    
  549.    public function FactoryInfo()
  550.    {
  551.       bytesTotal = 0;
  552.       super();
  553.    }
  554. }
  555.