Advertisement
Guest User

Untitled

a guest
Dec 15th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package egg82.objects {
  2.     import egg82.engines.StateEngine;
  3.     import flash.events.TimerEvent;
  4.     import flash.system.Security;
  5.     import flash.utils.ByteArray;
  6.     import flash.utils.getDefinitionByName;
  7.     import flash.utils.getQualifiedClassName;
  8.     import flash.utils.Timer;
  9.    
  10.     /**
  11.      * ...
  12.      * @author egg82
  13.      */
  14.    
  15.     public class Util {
  16.         //vars
  17.         private static var timers:Vector.<Timer> = new Vector.<Timer>();
  18.         private static var functions:Vector.<Function> = new Vector.<Function>();
  19.         private static var parameters:Vector.<Array> = new Vector.<Array>();
  20.        
  21.         //constructor
  22.         public function Util() {
  23.            
  24.         }
  25.        
  26.         //public
  27.         public static function getObjectClass(obj:*):Class {
  28.             return getDefinitionByName(getQualifiedClassName(obj)) as Class;
  29.         }
  30.         public static function getObjectClassName(obj:*):String {
  31.             return getQualifiedClassName(obj);
  32.         }
  33.        
  34.         public static function random(min:Number, max:Number):Number {
  35.             return Math.random() * (max - min) + min;
  36.         }
  37.         public static function betterRoundedRandom(min:int, max:int):int {
  38.             var num:int;
  39.             max++;
  40.            
  41.             do {
  42.                 num = Math.floor(Math.random() * (max - min) + min);
  43.             } while (num > max - 1);
  44.            
  45.             return num;
  46.         }
  47.        
  48.         public static function deepCopy(source:Object):* {
  49.             var bytes:ByteArray = new ByteArray();
  50.            
  51.             bytes.writeObject(source);
  52.             bytes.position = 0;
  53.             return bytes.readObject();
  54.         }
  55.        
  56.         public static function timedFunction(delay:Number, func:Function, params:Array):void {
  57.             if (func == null) {
  58.                 return;
  59.             }
  60.             if (delay <= 0) {
  61.                 func.apply(null, params);
  62.                 return;
  63.             }
  64.            
  65.             timers.push(new Timer(delay, 1));
  66.             timers[timers.length - 1].addEventListener(TimerEvent.TIMER, onTimer);
  67.             functions.push(func);
  68.             parameters.push(params);
  69.             timers[timers.length - 1].start();
  70.         }
  71.        
  72.         public static function loadPolicyFile(host:String, port:uint):void {
  73.             if (port > 65535) {
  74.                 return;
  75.             }
  76.            
  77.             try {
  78.                 Security.allowDomain(host);
  79.                 Security.allowInsecureDomain(host);
  80.             } catch (e:Error) {
  81.                
  82.             }
  83.            
  84.             if (host.search("://") > -1) {
  85.                 Security.loadPolicyFile(host + ":" + port);
  86.                 Security.loadPolicyFile(host + ":" + port + "/crossdomain.xml");
  87.             } else {
  88.                 Security.loadPolicyFile("xmlsocket://" + host + ":" + port);
  89.                 Security.loadPolicyFile("https://" + host + ":" + port);
  90.                 Security.loadPolicyFile("http://" + host + ":" + port);
  91.                
  92.                 Security.loadPolicyFile("xmlsocket://" + host + ":" + port + "/crossdomain.xml");
  93.                 Security.loadPolicyFile("https://" + host + ":" + port + "/crossdomain.xml");
  94.                 Security.loadPolicyFile("http://" + host + ":" + port + "/crossdomain.xml");
  95.             }
  96.         }
  97.        
  98.         public static function toByteArray(objs:Array):ByteArray {
  99.             if (!objs) {
  100.                 return null;
  101.             }
  102.            
  103.             var temp:ByteArray = new ByteArray();
  104.            
  105.             for each (var obj:* in objs) {
  106.                 if (getQualifiedClassName(obj) == "String") {
  107.                     temp.writeUTF(obj as String);
  108.                 } else if (getQualifiedClassName(obj) == "Array") {
  109.                     toByteArray(obj as Array).readBytes(temp);
  110.                 }/* else if (obj is ByteArray) {
  111.                     (obj as ByteArray).readBytes(temp);
  112.                 }*/ else if (getQualifiedClassName(obj) == "Boolean") {
  113.                     temp.writeBoolean(obj as Boolean);
  114.                 } else if (getQualifiedClassName(obj) == "Number") {
  115.                     temp.writeDouble(obj as Number);
  116.                 } else if (getQualifiedClassName(obj) == "int") {
  117.                     temp.writeInt(obj as int);
  118.                 } else if (getQualifiedClassName(obj) == "uint") {
  119.                     temp.writeUnsignedInt(obj as uint);
  120.                 } else {
  121.                     temp.writeObject(temp as Object);
  122.                 }
  123.             }
  124.            
  125.             temp.position = 0;
  126.             return temp;
  127.         }
  128.         public static function fromByteArray(bytes:ByteArray, classes:Array):Array {
  129.             if (!bytes || !classes || bytes.length == 0 || classes.length == 0) {
  130.                 return null;
  131.             }
  132.            
  133.             var retArr:Array = new Array();
  134.            
  135.             bytes.position = 0;
  136.            
  137.             for each (var cl:* in classes) {
  138.                 if (!(cl is Class)) {
  139.                     return null;
  140.                 }
  141.                
  142.                 try {
  143.                     if (getQualifiedClassName(cl) == "String") {
  144.                         retArr.push(bytes.readUTF());
  145.                     } else if (getQualifiedClassName(cl) == "Array") {
  146.                         retArr.push(fromByteArray(bytes, classes));
  147.                     }/* else if (cl is ByteArray) {
  148.                         retArr.push(bytes
  149.                     }*/ else if (getQualifiedClassName(cl) == "Boolean") {
  150.                         retArr.push(bytes.readBoolean());
  151.                     } else if (getQualifiedClassName(cl) == "Number") {
  152.                         retArr.push(bytes.readDouble());
  153.                     } else if (getQualifiedClassName(cl) == "int") {
  154.                         retArr.push(bytes.readInt());
  155.                     } else if (getQualifiedClassName(cl) == "uint") {
  156.                         retArr.push(bytes.readUnsignedInt());
  157.                     } else {
  158.                         retArr.push(bytes.readObject() as cl);
  159.                     }
  160.                 } catch (e:Error) {
  161.                     return null;
  162.                 }
  163.             }
  164.            
  165.             return retArr;
  166.         }
  167.        
  168.         public static function getA(color:uint):uint {
  169.             return (color >> 24) & 0xFF
  170.         }
  171.         public static function getR(color:uint):uint {
  172.             return color >> 16;
  173.         }
  174.         public static function getG(color:uint):uint {
  175.             return (color >> 8) & 0xFF;
  176.         }
  177.         public static function getB(color:uint):uint {
  178.             return color & 0x00FF;
  179.         }
  180.        
  181.         public static function setColor(r:uint, g:uint, b:uint, a:uint):uint {
  182.             return (a << 24) | (r << 16) | (g << 8) | b;
  183.         }
  184.        
  185.         //private
  186.         private static function onTimer(e:TimerEvent):void {
  187.             for (var i:uint = 0; i < timers.length; i++) {
  188.                 if (timers[i] === e.target) {
  189.                     timers[i].removeEventListener(TimerEvent.TIMER, onTimer);
  190.                     functions[i].apply(null, parameters[i]);
  191.                     timers.splice(i, 1);
  192.                     functions.splice(i, 1);
  193.                     parameters.splice(i, 1);
  194.                     return;
  195.                 }
  196.             }
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement