StaticLuaCallbacks.cs 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. /*
  2. * Tencent is pleased to support the open source community by making xLua available.
  3. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  4. * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  5. * http://opensource.org/licenses/MIT
  6. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  7. */
  8. #if USE_UNI_LUA
  9. using LuaAPI = UniLua.Lua;
  10. using RealStatePtr = UniLua.ILuaState;
  11. using LuaCSFunction = UniLua.CSharpFunctionDelegate;
  12. #else
  13. using LuaAPI = XLua.LuaDLL.Lua;
  14. using RealStatePtr = System.IntPtr;
  15. using LuaCSFunction = XLua.LuaDLL.lua_CSFunction;
  16. #endif
  17. namespace XLua
  18. {
  19. using System;
  20. using System.IO;
  21. using System.Reflection;
  22. public partial class StaticLuaCallbacks
  23. {
  24. internal LuaCSFunction GcMeta, ToStringMeta, EnumAndMeta, EnumOrMeta;
  25. internal LuaCSFunction StaticCSFunctionWraper, FixCSFunctionWraper;
  26. internal LuaCSFunction DelegateCtor;
  27. public StaticLuaCallbacks()
  28. {
  29. GcMeta = new LuaCSFunction(StaticLuaCallbacks.LuaGC);
  30. ToStringMeta = new LuaCSFunction(StaticLuaCallbacks.ToString);
  31. EnumAndMeta = new LuaCSFunction(EnumAnd);
  32. EnumOrMeta = new LuaCSFunction(EnumOr);
  33. StaticCSFunctionWraper = new LuaCSFunction(StaticLuaCallbacks.StaticCSFunction);
  34. FixCSFunctionWraper = new LuaCSFunction(StaticLuaCallbacks.FixCSFunction);
  35. DelegateCtor = new LuaCSFunction(DelegateConstructor);
  36. }
  37. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  38. public static int EnumAnd(RealStatePtr L)
  39. {
  40. try
  41. {
  42. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  43. object left = translator.FastGetCSObj(L, 1);
  44. object right = translator.FastGetCSObj(L, 2);
  45. Type typeOfLeft = left.GetType();
  46. if (!typeOfLeft.IsEnum() || typeOfLeft != right.GetType())
  47. {
  48. return LuaAPI.luaL_error(L, "invalid argument for Enum BitwiseAnd");
  49. }
  50. translator.PushAny(L, Enum.ToObject(typeOfLeft, Convert.ToInt64(left) & Convert.ToInt64(right)));
  51. return 1;
  52. }
  53. catch (Exception e)
  54. {
  55. return LuaAPI.luaL_error(L, "c# exception in Enum BitwiseAnd:" + e);
  56. }
  57. }
  58. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  59. public static int EnumOr(RealStatePtr L)
  60. {
  61. try
  62. {
  63. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  64. object left = translator.FastGetCSObj(L, 1);
  65. object right = translator.FastGetCSObj(L, 2);
  66. Type typeOfLeft = left.GetType();
  67. if (!typeOfLeft.IsEnum() || typeOfLeft != right.GetType())
  68. {
  69. return LuaAPI.luaL_error(L, "invalid argument for Enum BitwiseOr");
  70. }
  71. translator.PushAny(L, Enum.ToObject(typeOfLeft, Convert.ToInt64(left) | Convert.ToInt64(right)));
  72. return 1;
  73. }
  74. catch (Exception e)
  75. {
  76. return LuaAPI.luaL_error(L, "c# exception in Enum BitwiseOr:" + e);
  77. }
  78. }
  79. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  80. static int StaticCSFunction(RealStatePtr L)
  81. {
  82. try
  83. {
  84. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  85. LuaCSFunction func = (LuaCSFunction)translator.FastGetCSObj(L, LuaAPI.xlua_upvalueindex(1));
  86. return func(L);
  87. }
  88. catch (Exception e)
  89. {
  90. return LuaAPI.luaL_error(L, "c# exception in StaticCSFunction:" + e);
  91. }
  92. }
  93. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  94. static int FixCSFunction(RealStatePtr L)
  95. {
  96. try
  97. {
  98. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  99. int idx = LuaAPI.xlua_tointeger(L, LuaAPI.xlua_upvalueindex(1));
  100. LuaCSFunction func = (LuaCSFunction)translator.GetFixCSFunction(idx);
  101. return func(L);
  102. }
  103. catch (Exception e)
  104. {
  105. return LuaAPI.luaL_error(L, "c# exception in FixCSFunction:" + e);
  106. }
  107. }
  108. #if GEN_CODE_MINIMIZE
  109. [MonoPInvokeCallback(typeof(LuaDLL.CSharpWrapperCaller))]
  110. internal static int CSharpWrapperCallerImpl(RealStatePtr L, int funcidx, int top)
  111. {
  112. try
  113. {
  114. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  115. return translator.CallCSharpWrapper(L, funcidx, top);
  116. }
  117. catch (Exception e)
  118. {
  119. return LuaAPI.luaL_error(L, "c# exception:" + e);
  120. }
  121. }
  122. #endif
  123. #if GEN_CODE_MINIMIZE
  124. public static int DelegateCall(RealStatePtr L, int top)
  125. #else
  126. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  127. public static int DelegateCall(RealStatePtr L)
  128. #endif
  129. {
  130. try
  131. {
  132. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  133. object objDelegate = translator.FastGetCSObj(L, 1);
  134. if (objDelegate == null || !(objDelegate is Delegate))
  135. {
  136. return LuaAPI.luaL_error(L, "trying to invoke a value that is not delegate nor callable");
  137. }
  138. return translator.methodWrapsCache.GetDelegateWrap(objDelegate.GetType())(L);
  139. }
  140. catch (Exception e)
  141. {
  142. return LuaAPI.luaL_error(L, "c# exception in DelegateCall:" + e);
  143. }
  144. }
  145. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  146. public static int LuaGC(RealStatePtr L)
  147. {
  148. try
  149. {
  150. int udata = LuaAPI.xlua_tocsobj_safe(L, 1);
  151. if (udata != -1)
  152. {
  153. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  154. if ( translator != null )
  155. {
  156. translator.collectObject(udata);
  157. }
  158. }
  159. return 0;
  160. }
  161. catch (Exception e)
  162. {
  163. return LuaAPI.luaL_error(L, "c# exception in LuaGC:" + e);
  164. }
  165. }
  166. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  167. public static int ToString(RealStatePtr L)
  168. {
  169. try
  170. {
  171. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  172. object obj = translator.FastGetCSObj(L, 1);
  173. translator.PushAny(L, obj != null ? (obj.ToString() + ": " + obj.GetHashCode()) : "<invalid c# object>");
  174. return 1;
  175. }
  176. catch (Exception e)
  177. {
  178. return LuaAPI.luaL_error(L, "c# exception in ToString:" + e);
  179. }
  180. }
  181. #if GEN_CODE_MINIMIZE
  182. public static int DelegateCombine(RealStatePtr L, int top)
  183. #else
  184. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  185. public static int DelegateCombine(RealStatePtr L)
  186. #endif
  187. {
  188. try
  189. {
  190. var translator = ObjectTranslatorPool.Instance.Find(L);
  191. Type type = translator.FastGetCSObj(L, LuaAPI.lua_type(L, 1) == LuaTypes.LUA_TUSERDATA ? 1 : 2).GetType();
  192. Delegate d1 = translator.GetObject(L, 1, type) as Delegate;
  193. Delegate d2 = translator.GetObject(L, 2, type) as Delegate;
  194. if (d1 == null || d2 == null)
  195. {
  196. return LuaAPI.luaL_error(L, "one parameter must be a delegate, other one must be delegate or function");
  197. }
  198. translator.PushAny(L, Delegate.Combine(d1, d2));
  199. return 1;
  200. }
  201. catch (Exception e)
  202. {
  203. return LuaAPI.luaL_error(L, "c# exception in DelegateCombine:" + e);
  204. }
  205. }
  206. #if GEN_CODE_MINIMIZE
  207. public static int DelegateRemove(RealStatePtr L, int top)
  208. #else
  209. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  210. public static int DelegateRemove(RealStatePtr L)
  211. #endif
  212. {
  213. try
  214. {
  215. var translator = ObjectTranslatorPool.Instance.Find(L);
  216. Delegate d1 = translator.FastGetCSObj(L, 1) as Delegate;
  217. if (d1 == null)
  218. {
  219. return LuaAPI.luaL_error(L, "#1 parameter must be a delegate");
  220. }
  221. Delegate d2 = translator.GetObject(L, 2, d1.GetType()) as Delegate;
  222. if (d2 == null)
  223. {
  224. return LuaAPI.luaL_error(L, "#2 parameter must be a delegate or a function ");
  225. }
  226. translator.PushAny(L, Delegate.Remove(d1, d2));
  227. return 1;
  228. }
  229. catch (Exception e)
  230. {
  231. return LuaAPI.luaL_error(L, "c# exception in DelegateRemove:" + e);
  232. }
  233. }
  234. static bool tryPrimitiveArrayGet(Type type, RealStatePtr L, object obj, int index)
  235. {
  236. bool ok = true;
  237. if (type == typeof(int[]))
  238. {
  239. int[] array = obj as int[];
  240. LuaAPI.xlua_pushinteger(L, array[index]);
  241. }
  242. else if (type == typeof(float[]))
  243. {
  244. float[] array = obj as float[];
  245. LuaAPI.lua_pushnumber(L, array[index]);
  246. }
  247. else if (type == typeof(double[]))
  248. {
  249. double[] array = obj as double[];
  250. LuaAPI.lua_pushnumber(L, array[index]);
  251. }
  252. else if (type == typeof(bool[]))
  253. {
  254. bool[] array = obj as bool[];
  255. LuaAPI.lua_pushboolean(L, array[index]);
  256. }
  257. else if (type == typeof(long[]))
  258. {
  259. long[] array = obj as long[];
  260. LuaAPI.lua_pushint64(L, array[index]);
  261. }
  262. else if (type == typeof(ulong[]))
  263. {
  264. ulong[] array = obj as ulong[];
  265. LuaAPI.lua_pushuint64(L, array[index]);
  266. }
  267. else if (type == typeof(sbyte[]))
  268. {
  269. sbyte[] array = obj as sbyte[];
  270. LuaAPI.xlua_pushinteger(L, array[index]);
  271. }
  272. else if (type == typeof(short[]))
  273. {
  274. short[] array = obj as short[];
  275. LuaAPI.xlua_pushinteger(L, array[index]);
  276. }
  277. else if (type == typeof(ushort[]))
  278. {
  279. ushort[] array = obj as ushort[];
  280. LuaAPI.xlua_pushinteger(L, array[index]);
  281. }
  282. else if (type == typeof(char[]))
  283. {
  284. char[] array = obj as char[];
  285. LuaAPI.xlua_pushinteger(L, array[index]);
  286. }
  287. else if (type == typeof(uint[]))
  288. {
  289. uint[] array = obj as uint[];
  290. LuaAPI.xlua_pushuint(L, array[index]);
  291. }
  292. else if (type == typeof(IntPtr[]))
  293. {
  294. IntPtr[] array = obj as IntPtr[];
  295. LuaAPI.lua_pushlightuserdata(L, array[index]);
  296. }
  297. else if (type == typeof(decimal[]))
  298. {
  299. decimal[] array = obj as decimal[];
  300. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  301. translator.PushDecimal(L, array[index]);
  302. }
  303. else if (type == typeof(string[]))
  304. {
  305. string[] array = obj as string[];
  306. LuaAPI.lua_pushstring(L, array[index]);
  307. }
  308. else
  309. {
  310. ok = false;
  311. }
  312. return ok;
  313. }
  314. #if GEN_CODE_MINIMIZE
  315. public static int ArrayIndexer(RealStatePtr L, int top)
  316. #else
  317. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  318. public static int ArrayIndexer(RealStatePtr L)
  319. #endif
  320. {
  321. try
  322. {
  323. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  324. System.Array array = (System.Array)translator.FastGetCSObj(L, 1);
  325. if (array == null)
  326. {
  327. return LuaAPI.luaL_error(L, "#1 parameter is not a array!");
  328. }
  329. int i = LuaAPI.xlua_tointeger(L, 2);
  330. if (i >= array.Length)
  331. {
  332. return LuaAPI.luaL_error(L, "index out of range! i =" + i + ", array.Length=" + array.Length);
  333. }
  334. Type type = array.GetType();
  335. if (tryPrimitiveArrayGet(type, L, array, i))
  336. {
  337. return 1;
  338. }
  339. if (InternalGlobals.genTryArrayGetPtr != null)
  340. {
  341. try
  342. {
  343. if (InternalGlobals.genTryArrayGetPtr(type, L, translator, array, i))
  344. {
  345. return 1;
  346. }
  347. }
  348. catch (Exception e)
  349. {
  350. return LuaAPI.luaL_error(L, "c# exception:" + e.Message + ",stack:" + e.StackTrace);
  351. }
  352. }
  353. object ret = array.GetValue(i);
  354. translator.PushAny(L, ret);
  355. return 1;
  356. }
  357. catch (Exception e)
  358. {
  359. return LuaAPI.luaL_error(L, "c# exception in ArrayIndexer:" + e);
  360. }
  361. }
  362. public static bool TryPrimitiveArraySet(Type type, RealStatePtr L, object obj, int array_idx, int obj_idx)
  363. {
  364. bool ok = true;
  365. LuaTypes lua_type = LuaAPI.lua_type(L, obj_idx);
  366. if (type == typeof(int[]) && lua_type == LuaTypes.LUA_TNUMBER)
  367. {
  368. int[] array = obj as int[];
  369. array[array_idx] = LuaAPI.xlua_tointeger(L, obj_idx);
  370. }
  371. else if (type == typeof(float[]) && lua_type == LuaTypes.LUA_TNUMBER)
  372. {
  373. float[] array = obj as float[];
  374. array[array_idx] = (float)LuaAPI.lua_tonumber(L, obj_idx);
  375. }
  376. else if (type == typeof(double[]) && lua_type == LuaTypes.LUA_TNUMBER)
  377. {
  378. double[] array = obj as double[];
  379. array[array_idx] = LuaAPI.lua_tonumber(L, obj_idx); ;
  380. }
  381. else if (type == typeof(bool[]) && lua_type == LuaTypes.LUA_TBOOLEAN)
  382. {
  383. bool[] array = obj as bool[];
  384. array[array_idx] = LuaAPI.lua_toboolean(L, obj_idx);
  385. }
  386. else if (type == typeof(long[]) && LuaAPI.lua_isint64(L, obj_idx))
  387. {
  388. long[] array = obj as long[];
  389. array[array_idx] = LuaAPI.lua_toint64(L, obj_idx);
  390. }
  391. else if (type == typeof(ulong[]) && LuaAPI.lua_isuint64(L, obj_idx))
  392. {
  393. ulong[] array = obj as ulong[];
  394. array[array_idx] = LuaAPI.lua_touint64(L, obj_idx);
  395. }
  396. else if (type == typeof(sbyte[]) && lua_type == LuaTypes.LUA_TNUMBER)
  397. {
  398. sbyte[] array = obj as sbyte[];
  399. array[array_idx] = (sbyte)LuaAPI.xlua_tointeger(L, obj_idx);
  400. }
  401. else if (type == typeof(short[]) && lua_type == LuaTypes.LUA_TNUMBER)
  402. {
  403. short[] array = obj as short[];
  404. array[array_idx] = (short)LuaAPI.xlua_tointeger(L, obj_idx);
  405. }
  406. else if (type == typeof(ushort[]) && lua_type == LuaTypes.LUA_TNUMBER)
  407. {
  408. ushort[] array = obj as ushort[];
  409. array[array_idx] = (ushort)LuaAPI.xlua_tointeger(L, obj_idx);
  410. }
  411. else if (type == typeof(char[]) && lua_type == LuaTypes.LUA_TNUMBER)
  412. {
  413. char[] array = obj as char[];
  414. array[array_idx] = (char)LuaAPI.xlua_tointeger(L, obj_idx);
  415. }
  416. else if (type == typeof(uint[]) && lua_type == LuaTypes.LUA_TNUMBER)
  417. {
  418. uint[] array = obj as uint[];
  419. array[array_idx] = LuaAPI.xlua_touint(L, obj_idx);
  420. }
  421. else if (type == typeof(IntPtr[]) && lua_type == LuaTypes.LUA_TLIGHTUSERDATA)
  422. {
  423. IntPtr[] array = obj as IntPtr[];
  424. array[array_idx] = LuaAPI.lua_touserdata(L, obj_idx);
  425. }
  426. else if (type == typeof(decimal[]))
  427. {
  428. decimal[] array = obj as decimal[];
  429. if (lua_type == LuaTypes.LUA_TNUMBER)
  430. {
  431. array[array_idx] = (decimal)LuaAPI.lua_tonumber(L, obj_idx);
  432. }
  433. if (lua_type == LuaTypes.LUA_TUSERDATA)
  434. {
  435. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  436. if (translator.IsDecimal(L, obj_idx))
  437. {
  438. translator.Get(L, obj_idx, out array[array_idx]);
  439. }
  440. else
  441. {
  442. ok = false;
  443. }
  444. }
  445. else
  446. {
  447. ok = false;
  448. }
  449. }
  450. else if (type == typeof(string[]) && lua_type == LuaTypes.LUA_TSTRING)
  451. {
  452. string[] array = obj as string[];
  453. array[array_idx] = LuaAPI.lua_tostring(L, obj_idx);
  454. }
  455. else
  456. {
  457. ok = false;
  458. }
  459. return ok;
  460. }
  461. #if GEN_CODE_MINIMIZE
  462. public static int ArrayNewIndexer(RealStatePtr L, int top)
  463. #else
  464. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  465. public static int ArrayNewIndexer(RealStatePtr L)
  466. #endif
  467. {
  468. try
  469. {
  470. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  471. System.Array array = (System.Array)translator.FastGetCSObj(L, 1);
  472. if (array == null)
  473. {
  474. return LuaAPI.luaL_error(L, "#1 parameter is not a array!");
  475. }
  476. int i = LuaAPI.xlua_tointeger(L, 2);
  477. if (i >= array.Length)
  478. {
  479. return LuaAPI.luaL_error(L, "index out of range! i =" + i + ", array.Length=" + array.Length);
  480. }
  481. Type type = array.GetType();
  482. if (TryPrimitiveArraySet(type, L, array, i, 3))
  483. {
  484. return 0;
  485. }
  486. if (InternalGlobals.genTryArraySetPtr != null)
  487. {
  488. try
  489. {
  490. if (InternalGlobals.genTryArraySetPtr(type, L, translator, array, i, 3))
  491. {
  492. return 0;
  493. }
  494. }
  495. catch (Exception e)
  496. {
  497. return LuaAPI.luaL_error(L, "c# exception:" + e.Message + ",stack:" + e.StackTrace);
  498. }
  499. }
  500. object val = translator.GetObject(L, 3, type.GetElementType());
  501. array.SetValue(val, i);
  502. return 0;
  503. }
  504. catch (Exception e)
  505. {
  506. return LuaAPI.luaL_error(L, "c# exception in ArrayNewIndexer:" + e);
  507. }
  508. }
  509. #if GEN_CODE_MINIMIZE
  510. public static int ArrayLength(RealStatePtr L, int top)
  511. #else
  512. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  513. public static int ArrayLength(RealStatePtr L)
  514. #endif
  515. {
  516. try
  517. {
  518. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  519. System.Array array = (System.Array)translator.FastGetCSObj(L, 1);
  520. LuaAPI.xlua_pushinteger(L, array.Length);
  521. return 1;
  522. }
  523. catch (System.Exception e)
  524. {
  525. return LuaAPI.luaL_error(L, "c# exception in ArrayLength:" + e);
  526. }
  527. }
  528. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  529. public static int MetaFuncIndex(RealStatePtr L)
  530. {
  531. try
  532. {
  533. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  534. Type type = translator.FastGetCSObj(L, 2) as Type;
  535. if (type == null)
  536. {
  537. return LuaAPI.luaL_error(L, "#2 param need a System.Type!");
  538. }
  539. //UnityEngine.Debug.Log("============================load type by __index:" + type);
  540. //translator.TryDelayWrapLoader(L, type);
  541. translator.GetTypeId(L, type);
  542. LuaAPI.lua_pushvalue(L, 2);
  543. LuaAPI.lua_rawget(L, 1);
  544. return 1;
  545. }
  546. catch (System.Exception e)
  547. {
  548. return LuaAPI.luaL_error(L, "c# exception in MetaFuncIndex:" + e);
  549. }
  550. }
  551. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  552. internal static int Panic(RealStatePtr L)
  553. {
  554. string reason = String.Format("unprotected error in call to Lua API ({0})", LuaAPI.lua_tostring(L, -1));
  555. throw new LuaException(reason);
  556. }
  557. #if !XLUA_GENERAL
  558. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  559. internal static int Print(RealStatePtr L)
  560. {
  561. try
  562. {
  563. int n = LuaAPI.lua_gettop(L);
  564. string s = String.Empty;
  565. if (0 != LuaAPI.xlua_getglobal(L, "tostring"))
  566. {
  567. return LuaAPI.luaL_error(L, "can not get tostring in print:");
  568. }
  569. for (int i = 1; i <= n; i++)
  570. {
  571. LuaAPI.lua_pushvalue(L, -1); /* function to be called */
  572. LuaAPI.lua_pushvalue(L, i); /* value to print */
  573. if (0 != LuaAPI.lua_pcall(L, 1, 1, 0))
  574. {
  575. return LuaAPI.lua_error(L);
  576. }
  577. s += LuaAPI.lua_tostring(L, -1);
  578. if (i != n) s += "\t";
  579. LuaAPI.lua_pop(L, 1); /* pop result */
  580. }
  581. UnityEngine.Debug.Log("LUA: " + s);
  582. return 0;
  583. }
  584. catch (System.Exception e)
  585. {
  586. return LuaAPI.luaL_error(L, "c# exception in print:" + e);
  587. }
  588. }
  589. #endif
  590. #if (!UNITY_SWITCH && !UNITY_WEBGL) || UNITY_EDITOR
  591. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  592. internal static int LoadSocketCore(RealStatePtr L)
  593. {
  594. return LuaAPI.luaopen_socket_core(L);
  595. }
  596. #endif
  597. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  598. internal static int LoadCS(RealStatePtr L)
  599. {
  600. LuaAPI.xlua_pushasciistring(L, LuaEnv.CSHARP_NAMESPACE);
  601. LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX);
  602. return 1;
  603. }
  604. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  605. internal static int LoadBuiltinLib(RealStatePtr L)
  606. {
  607. try
  608. {
  609. string builtin_lib = LuaAPI.lua_tostring(L, 1);
  610. LuaEnv self = ObjectTranslatorPool.Instance.Find(L).luaEnv;
  611. LuaCSFunction initer;
  612. if (self.buildin_initer.TryGetValue(builtin_lib, out initer))
  613. {
  614. LuaAPI.lua_pushstdcallcfunction(L, initer);
  615. }
  616. else
  617. {
  618. LuaAPI.lua_pushstring(L, string.Format(
  619. "\n\tno such builtin lib '{0}'", builtin_lib));
  620. }
  621. return 1;
  622. }
  623. catch (System.Exception e)
  624. {
  625. return LuaAPI.luaL_error(L, "c# exception in LoadBuiltinLib:" + e);
  626. }
  627. }
  628. #if !XLUA_GENERAL
  629. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  630. internal static int LoadFromResource(RealStatePtr L)
  631. {
  632. try
  633. {
  634. string filename = LuaAPI.lua_tostring(L, 1).Replace('.', '/') + ".lua";
  635. // Load with Unity3D resources
  636. UnityEngine.TextAsset file = (UnityEngine.TextAsset)UnityEngine.Resources.Load(filename);
  637. if (file == null)
  638. {
  639. LuaAPI.lua_pushstring(L, string.Format(
  640. "\n\tno such resource '{0}'", filename));
  641. }
  642. else
  643. {
  644. if (LuaAPI.xluaL_loadbuffer(L, file.bytes, file.bytes.Length, "@" + filename) != 0)
  645. {
  646. return LuaAPI.luaL_error(L, String.Format("error loading module {0} from resource, {1}",
  647. LuaAPI.lua_tostring(L, 1), LuaAPI.lua_tostring(L, -1)));
  648. }
  649. }
  650. return 1;
  651. }
  652. catch (System.Exception e)
  653. {
  654. return LuaAPI.luaL_error(L, "c# exception in LoadFromResource:" + e);
  655. }
  656. }
  657. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  658. internal static int LoadFromStreamingAssetsPath(RealStatePtr L)
  659. {
  660. try
  661. {
  662. string filename = LuaAPI.lua_tostring(L, 1).Replace('.', '/') + ".lua";
  663. var filepath = UnityEngine.Application.streamingAssetsPath + "/" + filename;
  664. #if UNITY_ANDROID && !UNITY_EDITOR
  665. UnityEngine.WWW www = new UnityEngine.WWW(filepath);
  666. while (true)
  667. {
  668. if (www.isDone || !string.IsNullOrEmpty(www.error))
  669. {
  670. System.Threading.Thread.Sleep(50); //�Ƚ�hacker������
  671. if (!string.IsNullOrEmpty(www.error))
  672. {
  673. LuaAPI.lua_pushstring(L, string.Format(
  674. "\n\tno such file '{0}' in streamingAssetsPath!", filename));
  675. }
  676. else
  677. {
  678. UnityEngine.Debug.LogWarning("load lua file from StreamingAssets is obsolete, filename:" + filename);
  679. if (LuaAPI.xluaL_loadbuffer(L, www.bytes, www.bytes.Length , "@" + filename) != 0)
  680. {
  681. return LuaAPI.luaL_error(L, String.Format("error loading module {0} from streamingAssetsPath, {1}",
  682. LuaAPI.lua_tostring(L, 1), LuaAPI.lua_tostring(L, -1)));
  683. }
  684. }
  685. break;
  686. }
  687. }
  688. #else
  689. if (File.Exists(filepath))
  690. {
  691. // string text = File.ReadAllText(filepath);
  692. var bytes = File.ReadAllBytes(filepath);
  693. UnityEngine.Debug.LogWarning("load lua file from StreamingAssets is obsolete, filename:" + filename);
  694. if (LuaAPI.xluaL_loadbuffer(L, bytes, bytes.Length, "@" + filename) != 0)
  695. {
  696. return LuaAPI.luaL_error(L, String.Format("error loading module {0} from streamingAssetsPath, {1}",
  697. LuaAPI.lua_tostring(L, 1), LuaAPI.lua_tostring(L, -1)));
  698. }
  699. }
  700. else
  701. {
  702. LuaAPI.lua_pushstring(L, string.Format(
  703. "\n\tno such file '{0}' in streamingAssetsPath!", filename));
  704. }
  705. #endif
  706. return 1;
  707. }
  708. catch (System.Exception e)
  709. {
  710. return LuaAPI.luaL_error(L, "c# exception in LoadFromStreamingAssetsPath:" + e);
  711. }
  712. }
  713. #endif
  714. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  715. internal static int LoadFromCustomLoaders(RealStatePtr L)
  716. {
  717. try
  718. {
  719. string filename = LuaAPI.lua_tostring(L, 1);
  720. LuaEnv self = ObjectTranslatorPool.Instance.Find(L).luaEnv;
  721. foreach (var loader in self.customLoaders)
  722. {
  723. string real_file_path = filename;
  724. byte[] bytes = loader(ref real_file_path);
  725. if (bytes != null)
  726. {
  727. if (LuaAPI.xluaL_loadbuffer(L, bytes, bytes.Length, "@" + real_file_path) != 0)
  728. {
  729. return LuaAPI.luaL_error(L, String.Format("error loading module {0} from CustomLoader, {1}",
  730. LuaAPI.lua_tostring(L, 1), LuaAPI.lua_tostring(L, -1)));
  731. }
  732. return 1;
  733. }
  734. }
  735. LuaAPI.lua_pushstring(L, string.Format(
  736. "\n\tno such file '{0}' in CustomLoaders!", filename));
  737. return 1;
  738. }
  739. catch (System.Exception e)
  740. {
  741. return LuaAPI.luaL_error(L, "c# exception in LoadFromCustomLoaders:" + e);
  742. }
  743. }
  744. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  745. public static int LoadAssembly(RealStatePtr L)
  746. {
  747. #if UNITY_WSA && !UNITY_EDITOR
  748. return LuaAPI.luaL_error(L, "xlua.load_assembly no support in uwp!");
  749. #else
  750. try
  751. {
  752. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  753. string assemblyName = LuaAPI.lua_tostring(L, 1);
  754. Assembly assembly = null;
  755. try
  756. {
  757. assembly = Assembly.Load(assemblyName);
  758. }
  759. catch (BadImageFormatException)
  760. {
  761. // The assemblyName was invalid. It is most likely a path.
  762. }
  763. if (assembly == null)
  764. {
  765. assembly = Assembly.Load(AssemblyName.GetAssemblyName(assemblyName));
  766. }
  767. if (assembly != null && !translator.assemblies.Contains(assembly))
  768. {
  769. translator.assemblies.Add(assembly);
  770. }
  771. return 0;
  772. }
  773. catch (System.Exception e)
  774. {
  775. return LuaAPI.luaL_error(L, "c# exception in xlua.load_assembly:" + e);
  776. }
  777. #endif
  778. }
  779. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  780. public static int ImportType(RealStatePtr L)
  781. {
  782. try
  783. {
  784. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  785. string className = LuaAPI.lua_tostring(L, 1);
  786. Type type = translator.FindType(className);
  787. if (type != null)
  788. {
  789. if (translator.GetTypeId(L, type) >= 0)
  790. {
  791. LuaAPI.lua_pushboolean(L, true);
  792. }
  793. else
  794. {
  795. return LuaAPI.luaL_error(L, "can not load type " + type);
  796. }
  797. }
  798. else
  799. {
  800. LuaAPI.lua_pushnil(L);
  801. }
  802. return 1;
  803. }
  804. catch (System.Exception e)
  805. {
  806. return LuaAPI.luaL_error(L, "c# exception in xlua.import_type:" + e);
  807. }
  808. }
  809. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  810. public static int ImportGenericType(RealStatePtr L)
  811. {
  812. try
  813. {
  814. int top = LuaAPI.lua_gettop(L);
  815. if (top < 2) return LuaAPI.luaL_error(L, "import generic type need at lease 2 arguments");
  816. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  817. string className = LuaAPI.lua_tostring(L, 1);
  818. if (className.EndsWith("<>")) className = className.Substring(0, className.Length - 2);
  819. Type genericDef = translator.FindType(className + "`" + (top - 1));
  820. if (genericDef == null || !genericDef.IsGenericTypeDefinition())
  821. {
  822. LuaAPI.lua_pushnil(L);
  823. }
  824. else
  825. {
  826. Type[] typeArguments = new Type[top - 1];
  827. for(int i = 2; i <= top; i++)
  828. {
  829. typeArguments[i - 2] = getType(L, translator, i);
  830. if (typeArguments[i - 2] == null)
  831. {
  832. return LuaAPI.luaL_error(L, "param need a type");
  833. }
  834. }
  835. Type genericInc = genericDef.MakeGenericType(typeArguments);
  836. translator.GetTypeId(L, genericInc);
  837. translator.PushAny(L, genericInc);
  838. }
  839. return 1;
  840. }
  841. catch (System.Exception e)
  842. {
  843. return LuaAPI.luaL_error(L, "c# exception in xlua.import_type:" + e);
  844. }
  845. }
  846. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  847. public static int Cast(RealStatePtr L)
  848. {
  849. try
  850. {
  851. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  852. Type type;
  853. translator.Get(L, 2, out type);
  854. if (type == null)
  855. {
  856. return LuaAPI.luaL_error(L, "#2 param[" + LuaAPI.lua_tostring(L, 2) + "]is not valid type indicator");
  857. }
  858. LuaAPI.luaL_getmetatable(L, type.FullName);
  859. if (LuaAPI.lua_isnil(L, -1))
  860. {
  861. return LuaAPI.luaL_error(L, "no gen code for " + LuaAPI.lua_tostring(L, 2));
  862. }
  863. LuaAPI.lua_setmetatable(L, 1);
  864. return 0;
  865. }
  866. catch (System.Exception e)
  867. {
  868. return LuaAPI.luaL_error(L, "c# exception in xlua.cast:" + e);
  869. }
  870. }
  871. static Type getType(RealStatePtr L, ObjectTranslator translator, int idx)
  872. {
  873. if (LuaAPI.lua_type(L, idx) == LuaTypes.LUA_TTABLE)
  874. {
  875. LuaTable tbl;
  876. translator.Get(L, idx, out tbl);
  877. return tbl.Get<Type>("UnderlyingSystemType");
  878. }
  879. else if (LuaAPI.lua_type(L, idx) == LuaTypes.LUA_TSTRING)
  880. {
  881. string className = LuaAPI.lua_tostring(L, idx);
  882. return translator.FindType(className);
  883. }
  884. else if (translator.GetObject(L, idx) is Type)
  885. {
  886. return translator.GetObject(L, idx) as Type;
  887. }
  888. else
  889. {
  890. return null;
  891. }
  892. }
  893. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  894. public static int XLuaAccess(RealStatePtr L)
  895. {
  896. try
  897. {
  898. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  899. Type type = getType(L, translator, 1);
  900. object obj = null;
  901. if (type == null && LuaAPI.lua_type(L, 1) == LuaTypes.LUA_TUSERDATA)
  902. {
  903. obj = translator.SafeGetCSObj(L, 1);
  904. if (obj == null)
  905. {
  906. return LuaAPI.luaL_error(L, "xlua.access, #1 parameter must a type/c# object/string");
  907. }
  908. type = obj.GetType();
  909. }
  910. if (type == null)
  911. {
  912. return LuaAPI.luaL_error(L, "xlua.access, can not find c# type");
  913. }
  914. string fieldName = LuaAPI.lua_tostring(L, 2);
  915. BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
  916. if (LuaAPI.lua_gettop(L) > 2) // set
  917. {
  918. var field = type.GetField(fieldName, bindingFlags);
  919. if (field != null)
  920. {
  921. field.SetValue(obj, translator.GetObject(L, 3, field.FieldType));
  922. return 0;
  923. }
  924. var prop = type.GetProperty(fieldName, bindingFlags);
  925. if (prop != null)
  926. {
  927. prop.SetValue(obj, translator.GetObject(L, 3, prop.PropertyType), null);
  928. return 0;
  929. }
  930. }
  931. else
  932. {
  933. var field = type.GetField(fieldName, bindingFlags);
  934. if (field != null)
  935. {
  936. translator.PushAny(L, field.GetValue(obj));
  937. return 1;
  938. }
  939. var prop = type.GetProperty(fieldName, bindingFlags);
  940. if (prop != null)
  941. {
  942. translator.PushAny(L, prop.GetValue(obj, null));
  943. return 1;
  944. }
  945. }
  946. return LuaAPI.luaL_error(L, "xlua.access, no field " + fieldName);
  947. }
  948. catch (Exception e)
  949. {
  950. return LuaAPI.luaL_error(L, "c# exception in xlua.access: " + e);
  951. }
  952. }
  953. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  954. public static int XLuaPrivateAccessible(RealStatePtr L)
  955. {
  956. try
  957. {
  958. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  959. Type type = getType(L, translator, 1); ;
  960. if (type == null)
  961. {
  962. return LuaAPI.luaL_error(L, "xlua.private_accessible, can not find c# type");
  963. }
  964. while(type != null)
  965. {
  966. translator.PrivateAccessible(L, type);
  967. type = type.BaseType();
  968. }
  969. return 0;
  970. }
  971. catch (Exception e)
  972. {
  973. return LuaAPI.luaL_error(L, "c# exception in xlua.private_accessible: " + e);
  974. }
  975. }
  976. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  977. public static int XLuaMetatableOperation(RealStatePtr L)
  978. {
  979. try
  980. {
  981. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  982. Type type = getType(L, translator, 1);
  983. if (type == null)
  984. {
  985. return LuaAPI.luaL_error(L, "xlua.metatable_operation, can not find c# type");
  986. }
  987. bool is_first = false;
  988. int type_id = translator.getTypeId(L, type, out is_first);
  989. var param_num = LuaAPI.lua_gettop(L);
  990. if (param_num == 1) //get
  991. {
  992. LuaAPI.xlua_rawgeti(L, LuaIndexes.LUA_REGISTRYINDEX, type_id);
  993. return 1;
  994. }
  995. else if (param_num == 2) //set
  996. {
  997. if (LuaAPI.lua_type(L, 2) != LuaTypes.LUA_TTABLE)
  998. {
  999. return LuaAPI.luaL_error(L, "argument #2 must be a table");
  1000. }
  1001. LuaAPI.lua_pushnumber(L, type_id);
  1002. LuaAPI.xlua_rawseti(L, 2, 1);
  1003. LuaAPI.xlua_rawseti(L, LuaIndexes.LUA_REGISTRYINDEX, type_id);
  1004. return 0;
  1005. }
  1006. else
  1007. {
  1008. return LuaAPI.luaL_error(L, "invalid argument num for xlua.metatable_operation: " + param_num);
  1009. }
  1010. }
  1011. catch (Exception e)
  1012. {
  1013. return LuaAPI.luaL_error(L, "c# exception in xlua.metatable_operation: " + e);
  1014. }
  1015. }
  1016. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  1017. public static int DelegateConstructor(RealStatePtr L)
  1018. {
  1019. try
  1020. {
  1021. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  1022. Type type = getType(L, translator, 1);
  1023. if (type == null || !typeof(Delegate).IsAssignableFrom(type))
  1024. {
  1025. return LuaAPI.luaL_error(L, "delegate constructor: #1 argument must be a Delegate's type");
  1026. }
  1027. translator.PushAny(L, translator.GetObject(L, 2, type));
  1028. return 1;
  1029. }
  1030. catch (Exception e)
  1031. {
  1032. return LuaAPI.luaL_error(L, "c# exception in delegate constructor: " + e);
  1033. }
  1034. }
  1035. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  1036. public static int ToFunction(RealStatePtr L)
  1037. {
  1038. try
  1039. {
  1040. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  1041. MethodBase m;
  1042. translator.Get(L, 1, out m);
  1043. if (m == null)
  1044. {
  1045. return LuaAPI.luaL_error(L, "ToFunction: #1 argument must be a MethodBase");
  1046. }
  1047. translator.PushFixCSFunction(L,
  1048. new LuaCSFunction(translator.methodWrapsCache._GenMethodWrap(m.DeclaringType, m.Name, new MethodBase[] { m }).Call));
  1049. return 1;
  1050. }
  1051. catch (Exception e)
  1052. {
  1053. return LuaAPI.luaL_error(L, "c# exception in ToFunction: " + e);
  1054. }
  1055. }
  1056. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  1057. public static int GenericMethodWraper(RealStatePtr L)
  1058. {
  1059. try
  1060. {
  1061. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  1062. MethodInfo genericMethod;
  1063. translator.Get(L, LuaAPI.xlua_upvalueindex(1), out genericMethod);
  1064. int n = LuaAPI.lua_gettop(L);
  1065. Type[] typeArguments = new Type[n];
  1066. for(int i = 0; i < n; i++)
  1067. {
  1068. Type type = getType(L, translator, i + 1);
  1069. if (type == null)
  1070. {
  1071. return LuaAPI.luaL_error(L, "param #" + (i + 1) + " is not a type");
  1072. }
  1073. typeArguments[i] = type;
  1074. }
  1075. var method = genericMethod.MakeGenericMethod(typeArguments);
  1076. translator.PushFixCSFunction(L,
  1077. new LuaCSFunction(translator.methodWrapsCache._GenMethodWrap(method.DeclaringType, method.Name, new MethodBase[] { method }).Call));
  1078. return 1;
  1079. }
  1080. catch (Exception e)
  1081. {
  1082. return LuaAPI.luaL_error(L, "c# exception in GenericMethodWraper: " + e);
  1083. }
  1084. }
  1085. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  1086. public static int GetGenericMethod(RealStatePtr L)
  1087. {
  1088. try
  1089. {
  1090. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  1091. Type type = getType(L, translator, 1);
  1092. if (type == null)
  1093. {
  1094. return LuaAPI.luaL_error(L, "xlua.get_generic_method, can not find c# type");
  1095. }
  1096. string methodName = LuaAPI.lua_tostring(L, 2);
  1097. if (string.IsNullOrEmpty(methodName))
  1098. {
  1099. return LuaAPI.luaL_error(L, "xlua.get_generic_method, #2 param need a string");
  1100. }
  1101. System.Collections.Generic.List<MethodInfo> matchMethods = new System.Collections.Generic.List<MethodInfo>();
  1102. var allMethods = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
  1103. for(int i = 0; i < allMethods.Length; i++)
  1104. {
  1105. var method = allMethods[i];
  1106. if (method.Name == methodName && method.IsGenericMethodDefinition)
  1107. {
  1108. matchMethods.Add(method);
  1109. }
  1110. }
  1111. int methodIdx = 0;
  1112. if (matchMethods.Count == 0)
  1113. {
  1114. LuaAPI.lua_pushnil(L);
  1115. }
  1116. else
  1117. {
  1118. if (LuaAPI.lua_isinteger(L, 3))
  1119. {
  1120. methodIdx = LuaAPI.xlua_tointeger(L, 3);
  1121. }
  1122. translator.PushAny(L, matchMethods[methodIdx]);
  1123. LuaAPI.lua_pushstdcallcfunction(L, GenericMethodWraper, 1);
  1124. }
  1125. }
  1126. catch (Exception e)
  1127. {
  1128. return LuaAPI.luaL_error(L, "c# exception in xlua.get_generic_method: " + e);
  1129. }
  1130. return 1;
  1131. }
  1132. [MonoPInvokeCallback(typeof(LuaCSFunction))]
  1133. public static int ReleaseCsObject(RealStatePtr L)
  1134. {
  1135. try
  1136. {
  1137. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  1138. translator.ReleaseCSObj(L, 1);
  1139. return 0;
  1140. }
  1141. catch (Exception e)
  1142. {
  1143. return LuaAPI.luaL_error(L, "c# exception in ReleaseCsObject: " + e);
  1144. }
  1145. }
  1146. }
  1147. }