PageRenderTime 28ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/com/lele/Plugin/Lesp/LespSpace.as

https://gitlab.com/Treeky-NULL/LezaiNiubi
ActionScript | 640 lines | 595 code | 28 blank | 17 comment | 86 complexity | ed08b5bbd3541d9a393cffcbd9133fce MD5 | raw file
  1. package com.lele.Plugin.Lesp
  2. {
  3. import adobe.utils.CustomActions;
  4. import com.lele.Data.GloableData;
  5. import com.lele.LeleSocket.Command;
  6. import com.lele.Manager.Events.APIEvent;
  7. import com.lele.Manager.GameManager;
  8. import com.lele.MathTool.LeleMath;
  9. import com.lele.Plugin.GUID.Guid;
  10. import flash.display.Loader;
  11. import flash.display.Sprite;
  12. import flash.display.TriangleCulling;
  13. import flash.events.Event;
  14. import flash.events.TimerEvent;
  15. import flash.geom.Point;
  16. import flash.net.URLRequest;
  17. import flash.utils.getDefinitionByName;
  18. import flash.utils.getQualifiedClassName;
  19. import flash.utils.Timer;
  20. /**
  21. * ...
  22. * @author Lele
  23. */
  24. public class LespSpace
  25. {
  26. private var _Root:String;
  27. private var _FunctionMap:Object;
  28. private var _ValueMap:Object;
  29. private var _Packs:Array;
  30. private var _PackLoaders:Array;
  31. private var _LespLoaders:Array;
  32. private var _printer:Function;
  33. public function LespSpace()
  34. {
  35. _FunctionMap = new Object();
  36. _ValueMap = new Object();
  37. _PackLoaders = new Array();
  38. _LespLoaders = new Array();
  39. _Packs = new Array();
  40. }
  41. //清除空间
  42. public function Clean()
  43. {
  44. for (var a:int = 0; a < _PackLoaders.length; a++ )
  45. {
  46. (_PackLoaders[a] as LoadUnit).Release();
  47. }
  48. _FunctionMap = null;
  49. _ValueMap = null;
  50. _Packs = null;
  51. _PackLoaders = null;
  52. }
  53. //加载外部包
  54. public function LoadPack(url:String,callback:Function)
  55. {
  56. var loadUnit:LoadUnit = new LoadUnit(url, _Packs, callback);
  57. _PackLoaders.push(loadUnit);
  58. }
  59. //加载对象
  60. public function LoadObj(obj:Object)
  61. {
  62. _Packs.push(obj);
  63. }
  64. //根据语句重建原始函数
  65. private function GetCustomFunc(func:Func):Func
  66. {
  67. var custF:CustomFunc = _FunctionMap[func.Name];
  68. var funcS:String = custF.func;
  69. //这边存在一定的问题
  70. for (var a:int = 0; a < custF.param.length; a++ )
  71. {
  72. while (funcS.search(" "+custF.param[a]) >= 0)
  73. {
  74. funcS = funcS.replace(" " + String(custF.param[a]), " " + String(func.params[a]));
  75. }
  76. }
  77. funcS = "#" + funcS.substr(2, funcS.length);
  78. return ProcessOnce(funcS);
  79. }
  80. //执行Func
  81. public function ExecFunc(func:String):Object
  82. {
  83. if (func.charAt(1) == "!")
  84. {
  85. func = "#" + String(func).substr(2, String(func).length);
  86. }
  87. return ExecFuncHelp(ProcessOnce(func));
  88. }
  89. private function ExecFuncHelp(func:Func) : Object
  90. {
  91. //这里替换自定义函数
  92. while (_FunctionMap[func.Name] != undefined)
  93. {
  94. func = GetCustomFunc(func);
  95. }
  96. //替换结束
  97. var isDoing:Boolean = true;
  98. while (isDoing)
  99. {
  100. isDoing = false;
  101. for (var a:int = 0; a < func.params.length; a++ )
  102. {
  103. if (func.params[a] is Func)
  104. {
  105. func.params[a] = this.ExecFuncHelp(func.params[a] as Func);
  106. isDoing = true;
  107. }
  108. else if (func.params[a] is String && String(func.params[a]).charAt(0) == "#")
  109. {
  110. if (String(func.params[a]).charAt(1) != "!")
  111. {
  112. func.params[a] = this.ProcessOnce(func.params[a]);
  113. isDoing = true;
  114. //func.params[a] = "#" + String(func.params[a]).substr(2, String(func.params[a]).length);
  115. }
  116. }
  117. else if (func.params[a] is String && String(func.params[a]).charAt(0) == "&")
  118. {
  119. func.params[a] = this.GetFunction(String(func.params[a]).substr(1, String(func.params[a]).length));
  120. isDoing = true;
  121. }
  122. else if (func.params[a] is String && String(func.params[a]).charAt(0) == "@")
  123. {
  124. func.params[a] = this.GetUrl(String(func.params[a]).substr(1, String(func.params[a]).length));
  125. isDoing = true;
  126. }
  127. else if (func.params[a] is String && String(func.params[a]).charAt(0) == "$")
  128. {
  129. func.params[a] = this.GetInnerVariable(String(func.params[a]).substr(1, String(func.params[a]).length));
  130. isDoing = true;
  131. }
  132. }
  133. }
  134. return GetFunction(func.Name)(func.params);
  135. }
  136. private function GetInnerVariable(name:String) : Object
  137. {
  138. if (GloableData[name] != null) { return GloableData[name]; }
  139. return _ValueMap[name];
  140. }
  141. private function GetUrl(name:String) : String
  142. {
  143. return _Root+name;
  144. }
  145. private function GetFunction(name:String) : Function
  146. {
  147. var geted:Boolean = false;
  148. var retFunc:Function;
  149. try { retFunc = this[name] as Function; geted = true; }
  150. catch (er:Error) { geted = false; }
  151. if (geted == true) { return retFunc; }
  152. for (var a:int = 0; a < _Packs.length; a++ )
  153. {
  154. try { retFunc = _Packs[a][name] as Function; geted = true;}
  155. catch (er:Error) { geted = false; }
  156. if (geted == true) { return retFunc; }
  157. }
  158. if (!geted) { throw new Error("No such function!"+"function name:"+name); }
  159. return function() { };
  160. }
  161. private function ProcessOnce(str:String) : Func
  162. {
  163. var temp:String = null;
  164. str = str.substr(2, str.length - 3);
  165. var stack:Array = new Array();
  166. var func:Func = new Func();
  167. var params:Array = new Array();
  168. var ignore:int = 0;
  169. for (var a:int = 0; a < str.length; a++ )
  170. {
  171. if (str.charAt(a) == " " && ignore == 0)
  172. {
  173. temp = "";
  174. stack = stack.reverse();
  175. while (stack.length > 0)
  176. {
  177. temp = temp + stack.pop();
  178. }
  179. params.push(temp);
  180. }
  181. else
  182. {
  183. stack.push(str.charAt(a));
  184. }
  185. if (str.charAt(a) == "#")
  186. {
  187. ignore++;
  188. }
  189. if (str.charAt(a) == ")")
  190. {
  191. ignore = ignore - 1;
  192. }
  193. }
  194. if (stack.length != 0)
  195. {
  196. temp = "";
  197. stack = stack.reverse();
  198. while (stack.length > 0)
  199. {
  200. temp = temp + stack.pop();
  201. }
  202. params.push(temp);
  203. }
  204. func.Name = params[0];
  205. func.params = params.slice(1, params.length);
  206. return func;
  207. }
  208. ///lele func
  209. private function Abs(args:Array):Number
  210. {
  211. return Math.abs(Number(args[0]));
  212. }
  213. private function NextGaussian(args:Array):Number
  214. {
  215. return LeleMath.NextGaussian2(Number(args[0]), Number(args[1]));
  216. }
  217. private function MakeInt(args:Array):int //str
  218. {
  219. return int(args[0]);
  220. }
  221. private function LessThen(args:Array):Boolean
  222. {
  223. return Number(args[0]) < Number(args[1]);
  224. }
  225. private function GetSpliteAt(args:Array):String //str splite_char position
  226. {
  227. return String(args[0]).split(args[1])[args[2]];
  228. }
  229. private function BineString(args:Array):String
  230. {
  231. var str:String = "";
  232. for (var a:int = 0; a < args.length; a++ )
  233. {
  234. str += args[a];
  235. }
  236. return str;
  237. }
  238. private function MakeObject(args:Array = null):Object
  239. {
  240. return new Object();
  241. }
  242. private function SetObjValue(args:Array) //obj memberName memberValue
  243. {
  244. args[0][args[1]] = args[2];
  245. }
  246. private function GetObjValue(args:Array):Object //obj valueName
  247. {
  248. return args[0][args[1]];
  249. }
  250. private function CallObjFunction(args:Array):Object //obj funcName
  251. {
  252. return args[0][args[1]](args.slice(2, args.length));
  253. }
  254. private function Typeof(args:Array):String //obj
  255. {
  256. return getQualifiedClassName(args[0]);
  257. }
  258. private function Unload(args:Array)//url
  259. {
  260. for (var a:int = 0; a < _LespLoaders.length; a++ )
  261. {
  262. if ((_LespLoaders[a] as LespLoader)._url == args[0])
  263. {
  264. (_LespLoaders[a] as LespLoader).Release();
  265. _LespLoaders.splice(a, 1);
  266. return;
  267. }
  268. }
  269. }
  270. private function Load(args:Array)//url callback
  271. {
  272. var loader:LespLoader = new LespLoader(args[0], args[1], this);
  273. _LespLoaders.push(loader);
  274. }
  275. private function MakeArray(args:Array):Array
  276. {
  277. var array:Array = new Array();
  278. if (args != null)
  279. {
  280. for (var a:int = 0; a < args.length; a++ )
  281. {
  282. array.push(args[a] );
  283. }
  284. }
  285. return array;
  286. }
  287. private function Random(args:Array):Number //get random num between a to b
  288. {
  289. if (Number(args[0]) > Number(args[1]))
  290. {
  291. return Math.random() * (Number(args[0]) -Number(args[1])) + Number(args[1]);
  292. }
  293. return Math.random() * (Number(args[1]) -Number(args[0])) +Number( args[0]);
  294. }
  295. private function Multiply(args:Array):Number
  296. {
  297. var result:Number = Number(args[0]);
  298. for (var a:int = 1; a < args.length; a++ )
  299. {
  300. result *= Number(args[a]);
  301. }
  302. return result;
  303. }
  304. private function Add(args:Array):Number //source add1 add2 add3....
  305. {
  306. var result:Number =Number(args[0]);
  307. for (var a:int = 1; a < args.length; a++ )
  308. {
  309. result +=Number( args[a]);
  310. }
  311. return result;
  312. }
  313. private function IsEqual(args:Array):Boolean// obj1 obj2
  314. {
  315. return args[0] == args[1];
  316. }
  317. private function Branch(args:Array) //condition yesFunc noFunc
  318. {
  319. if (args[0])
  320. {
  321. if (args[1] != null) { ExecFunc(args[1]); }
  322. }
  323. else
  324. {
  325. if (args[2] != null) { ExecFunc(args[2]); }
  326. }
  327. }
  328. private function Clock(args:Array) //delNum times==0 StepFunc CompleteFunc
  329. {
  330. var timer:Timer = new Timer(args[0], args[1]);
  331. var funcA:Function=function(evt:Event)
  332. {
  333. ExecFunc(args[2]);
  334. }
  335. var funcB:Function=function(evt:Event)
  336. {
  337. timer.removeEventListener(TimerEvent.TIMER, funcA);
  338. timer.removeEventListener(TimerEvent.TIMER, funcB);
  339. if (args[3] != null)
  340. {
  341. ExecFunc(args[3]);
  342. }
  343. }
  344. timer.addEventListener(TimerEvent.TIMER,funcA );
  345. timer.addEventListener(TimerEvent.TIMER_COMPLETE, funcB );
  346. timer.start();
  347. }
  348. private function DefFunc(args:Array) //name func params....
  349. {
  350. var custc:CustomFunc = new CustomFunc();
  351. custc.name = args[0];
  352. custc.func = args[1];
  353. custc.param = args.slice(2, args.length);
  354. _FunctionMap[custc.name] = custc;
  355. }
  356. private function CheckBoolean(args:Array):Boolean
  357. {
  358. if (args[0] == false) { return false; }
  359. return true;
  360. }
  361. private function SetRoot(args:Array):String
  362. {
  363. _Root = args[0];
  364. return args[0];
  365. }
  366. private function False(args:Array=null):Boolean
  367. {
  368. return false;
  369. }
  370. private function True(args:Array=null):Boolean
  371. {
  372. return true;
  373. }
  374. private function MakeString(args:Array = null):String
  375. {
  376. var str:String = "";
  377. if (args != null)
  378. {
  379. for (var a:int = 0; a < args.length; a++ )
  380. {
  381. str +=" "+ args[a];
  382. }
  383. }
  384. return str.substr(1,str.length);
  385. }
  386. private function NULL(args:Array = null) {}
  387. private function MakePoint(args:Array):Point
  388. {
  389. return new Point(Number(args[0]), Number(args[1]));
  390. }
  391. private function LoadPackFunc(args:Array)//url callback(func)
  392. {
  393. var arr:Array = new Array();
  394. arr.push(args[1]);
  395. var ld:LoadUnit = new LoadUnit(args[0], _Packs,GenerateFunction(arr));
  396. _PackLoaders.push(ld);
  397. }
  398. private function LoadPackFunction(args:Array)//url callback(function)
  399. {
  400. var ld:LoadUnit = new LoadUnit(args[0], _Packs, args[1]);
  401. _PackLoaders.push(ld);
  402. }
  403. private function SetPrinter(args:Array)//function
  404. {
  405. _printer = args[0];
  406. }
  407. private function Print(args:Array)//str
  408. {
  409. if (_printer != null) { _printer(args[0]); }
  410. else
  411. {
  412. trace(args[0]);
  413. }
  414. }
  415. private function StructFor(args:Array)//times func
  416. {
  417. for (var a:int = 0; a < int(args[0]); a++ )
  418. {
  419. ExecFunc(args[1]);
  420. }
  421. }
  422. private function DefValue(args:Array):Object//type name val
  423. {
  424. try
  425. {
  426. var a:Class = getDefinitionByName(args[0]) as Class;
  427. }catch (er:Error) { Print(["No such definition"]); return null; }
  428. try
  429. {
  430. var test = a(args[2]);
  431. }
  432. catch (er:Error)
  433. {
  434. try
  435. {
  436. test = args[2] as a;
  437. if (test == null)
  438. {
  439. Print(["Type cast faild"]);
  440. return null;
  441. }
  442. }catch (errr:Error)
  443. {
  444. Print(["Type cast faild"]); return null;
  445. }
  446. }
  447. _ValueMap[args[1]] = test;
  448. return test;
  449. }
  450. private function SetValue(args:Array)//type name val
  451. {
  452. try
  453. {
  454. var a:Class = getDefinitionByName(args[0]) as Class;
  455. }catch (er:Error) { trace("No such definition"); return; }
  456. try
  457. {
  458. var test = a(args[2]);
  459. }catch (er:Error) { trace("Type cast faild"); return; }
  460. _ValueMap[args[1]]=test;
  461. }
  462. private function GenerateFunction(args:Array):Function//func
  463. {
  464. var temp:Function = function():Object
  465. {
  466. return ExecFunc(args[0]);
  467. }
  468. return temp;
  469. }
  470. private function MakeNetFunction(args:Array):Function //func params:name v1 v2 v3...
  471. {
  472. var temp:Function = function(cmd:Command):Object
  473. {
  474. if (args[0].charAt(1) == "!")
  475. {
  476. args[0] = "#" + String(args[0]).substr(2, String(args[0]).length);
  477. }
  478. var par:Array = args.slice(1, args.length);
  479. while (args[0].search(" " + par[0]) >= 0)
  480. {
  481. args[0] = args[0].replace(" " + String(par[0]), " " + cmd._commandName);
  482. }
  483. var pa:Array = par.slice(1, par.length);
  484. for (var a:int = 0; a < pa.length; a++ )
  485. {
  486. while (args[0].search(" "+pa[a]) >= 0)
  487. {
  488. args[0] = args[0].replace(" " + String(pa[a]), " " + cmd.GetValueByName(pa[a]));
  489. }
  490. }
  491. return ExecFunc(args[0]);
  492. }
  493. return temp;
  494. }
  495. private function MakeFunction(args:Array):Function //func,params
  496. {
  497. var temp:Function = function(...arg):Object
  498. {
  499. if (args[0].charAt(1) == "!")
  500. {
  501. args[0] = "#" + String(args[0]).substr(2, String(args[0]).length);
  502. }
  503. var par:Array = args.slice(1, args.length);
  504. for (var a:int = 0; a < par.length; a++ )
  505. {
  506. while (args[0].search(" "+par[a]) >= 0)
  507. {
  508. args[0] = args[0].replace(" " + String(par[a]), " " + String(arg[a]));
  509. }
  510. }
  511. return ExecFunc(args[0]);
  512. }
  513. return temp;
  514. }
  515. private function ExecFunction_p_p(args:Array):Object//function
  516. {
  517. return (args[0] as Function)(args[1],args[2]);
  518. }
  519. private function ExecFunction_p(args:Array):Object//function
  520. {
  521. return (args[0] as Function)(args[1]);
  522. }
  523. private function ExecFunction(args:Array):Object//function
  524. {
  525. return (args[0] as Function)();
  526. }
  527. //Lele Function End
  528. //特化的关于该game 的
  529. public function AddAPIEventListener(args:Array):Function //evtStr:String action:Func name
  530. {
  531. var func:Function = function(evt:APIEvent)//如果这里的data要是任意类型的就必须在下面申请一个临时空间存放,执行完后清除
  532. {
  533. ExecFunc("#(" + args[1] + " " + evt.data + ")");
  534. }
  535. GameManager.GetEventDispatcher().addEventListener(args[0], func);
  536. return func;
  537. }
  538. public function RemoveAPIEventListener(args:Array) //evtStr:String action:Function
  539. {
  540. GameManager.GetEventDispatcher().removeEventListener(args[0], args[1]);
  541. }
  542. public function SetGloableData(args:Array) //name value
  543. {
  544. trace(args[1]);
  545. GloableData[args[0]] = args[1];
  546. }
  547. }
  548. }
  549. import flash.display.Loader;
  550. import flash.events.Event;
  551. import flash.net.URLRequest;
  552. import com.lele.Plugin.Lesp.LespSpace;
  553. class LespLoader
  554. {
  555. public var _callBack:String;
  556. public var _url:String;
  557. public var _lesp:LespSpace;
  558. public var _loader:Loader;
  559. public function LespLoader(url:String, callBack:String,lesp:LespSpace)
  560. {
  561. _url = url;
  562. _callBack = callBack;
  563. _lesp = lesp;
  564. _loader = new Loader();
  565. var request:URLRequest = new URLRequest(_url);
  566. var func:Function = function(evt:Event)
  567. {
  568. _loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, func);
  569. _lesp.ExecFunc(_callBack);
  570. }
  571. _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,func);
  572. _loader.load(request);
  573. }
  574. public function Release()
  575. {
  576. _lesp = null;
  577. _loader.unloadAndStop(true);
  578. }
  579. }
  580. import flash.display.Loader;
  581. import flash.events.Event;
  582. import flash.net.URLRequest;
  583. class LoadUnit
  584. {
  585. //来自Lesp的引用
  586. private var _packs:Array;
  587. private var _loader:Loader;
  588. private var _callBack:Function;
  589. public function LoadUnit(url:String,packs:Array,callBack:Function)
  590. {
  591. _packs = packs;
  592. _loader = new Loader();
  593. _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(evt:Event)
  594. {
  595. _packs.push(evt.target.content);
  596. callBack();
  597. });
  598. var req:URLRequest = new URLRequest(url);
  599. _loader.load(req);
  600. }
  601. public function Release()
  602. {
  603. _loader.unloadAndStop(true);
  604. _callBack = null;
  605. }
  606. }
  607. class CustomFunc
  608. {
  609. public var name:String;
  610. public var func:String;
  611. public var param:Array;
  612. }