HotfixTest2.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. using UnityEngine;
  2. using XLua;
  3. namespace XLuaTest
  4. {
  5. [Hotfix]
  6. public class HotfixCalc
  7. {
  8. public int Add(int a, int b)
  9. {
  10. return a - b;
  11. }
  12. public Vector3 Add(Vector3 a, Vector3 b)
  13. {
  14. return a - b;
  15. }
  16. public int TestOut(int a, out double b, ref string c)
  17. {
  18. b = a + 2;
  19. c = "wrong version";
  20. return a + 3;
  21. }
  22. public int TestOut(int a, out double b, ref string c, GameObject go)
  23. {
  24. return TestOut(a, out b, ref c);
  25. }
  26. public T Test1<T>()
  27. {
  28. return default(T);
  29. }
  30. public T1 Test2<T1, T2, T3>(T1 a, out T2 b, ref T3 c)
  31. {
  32. b = default(T2);
  33. return a;
  34. }
  35. public static int Test3<T>(T a)
  36. {
  37. return 0;
  38. }
  39. public static void Test4<T>(T a)
  40. {
  41. }
  42. public void Test5<T>(int a, params T[] arg)
  43. {
  44. }
  45. }
  46. public class NoHotfixCalc
  47. {
  48. public int Add(int a, int b)
  49. {
  50. return a + b;
  51. }
  52. }
  53. [Hotfix]
  54. public class GenericClass<T>
  55. {
  56. T a;
  57. public GenericClass(T a)
  58. {
  59. this.a = a;
  60. }
  61. public void Func1()
  62. {
  63. Debug.Log("a=" + a);
  64. }
  65. public T Func2()
  66. {
  67. return default(T);
  68. }
  69. }
  70. [Hotfix]
  71. public class InnerTypeTest
  72. {
  73. public void Foo()
  74. {
  75. _InnerStruct ret = Bar();
  76. Debug.Log("{x=" + ret.x + ",y= " + ret.y + "}");
  77. }
  78. struct _InnerStruct
  79. {
  80. public int x;
  81. public int y;
  82. }
  83. _InnerStruct Bar()
  84. {
  85. return new _InnerStruct { x = 1, y = 2 };
  86. }
  87. }
  88. public class BaseTestHelper
  89. {
  90. }
  91. public class BaseTestBase<T> : BaseTestHelper
  92. {
  93. public virtual void Foo(int p)
  94. {
  95. Debug.Log("BaseTestBase<>.Foo, p = " + p);
  96. }
  97. }
  98. [Hotfix]
  99. [LuaCallCSharp]
  100. public class BaseTest : BaseTestBase<InnerTypeTest>
  101. {
  102. public override void Foo(int p)
  103. {
  104. Debug.Log("BaseTest<>.Foo, p = " + p);
  105. }
  106. public void Proxy(int p)
  107. {
  108. base.Foo(p);
  109. }
  110. public override string ToString()
  111. {
  112. return base.ToString();
  113. }
  114. }
  115. [Hotfix]
  116. public struct StructTest
  117. {
  118. GameObject go;
  119. public StructTest(GameObject go)
  120. {
  121. this.go = go;
  122. }
  123. public GameObject GetGo(int a, object b)
  124. {
  125. return go;
  126. }
  127. public override string ToString()
  128. {
  129. return base.ToString();
  130. }
  131. public string Proxy()
  132. {
  133. return base.ToString();
  134. }
  135. }
  136. [Hotfix]
  137. public struct GenericStruct<T>
  138. {
  139. T a;
  140. public GenericStruct(T a)
  141. {
  142. this.a = a;
  143. }
  144. public T GetA(int p)
  145. {
  146. return a;
  147. }
  148. }
  149. public class HotfixTest2 : MonoBehaviour
  150. {
  151. // Use this for initialization
  152. void Start()
  153. {
  154. LuaEnv luaenv = new LuaEnv();
  155. HotfixCalc calc = new HotfixCalc();
  156. NoHotfixCalc ordinaryCalc = new NoHotfixCalc();
  157. int CALL_TIME = 100 * 1000 * 1000;
  158. var start = System.DateTime.Now;
  159. for (int i = 0; i < CALL_TIME; i++)
  160. {
  161. calc.Add(2, 1);
  162. }
  163. var d1 = (System.DateTime.Now - start).TotalMilliseconds;
  164. Debug.Log("Hotfix using:" + d1);
  165. start = System.DateTime.Now;
  166. for (int i = 0; i < CALL_TIME; i++)
  167. {
  168. ordinaryCalc.Add(2, 1);
  169. }
  170. var d2 = (System.DateTime.Now - start).TotalMilliseconds;
  171. Debug.Log("No Hotfix using:" + d2);
  172. Debug.Log("drop:" + ((d1 - d2) / d1));
  173. Debug.Log("Before Fix: 2 + 1 = " + calc.Add(2, 1));
  174. Debug.Log("Before Fix: Vector3(2, 3, 4) + Vector3(1, 2, 3) = " + calc.Add(new Vector3(2, 3, 4), new Vector3(1, 2, 3)));
  175. luaenv.DoString(@"
  176. xlua.hotfix(CS.XLuaTest.HotfixCalc, 'Add', function(self, a, b)
  177. return a + b
  178. end)
  179. ");
  180. Debug.Log("After Fix: 2 + 1 = " + calc.Add(2, 1));
  181. Debug.Log("After Fix: Vector3(2, 3, 4) + Vector3(1, 2, 3) = " + calc.Add(new Vector3(2, 3, 4), new Vector3(1, 2, 3)));
  182. double num;
  183. string str = "hehe";
  184. int ret = calc.TestOut(100, out num, ref str);
  185. Debug.Log("ret = " + ret + ", num = " + num + ", str = " + str);
  186. luaenv.DoString(@"
  187. xlua.hotfix(CS.XLuaTest.HotfixCalc, 'TestOut', function(self, a, c, go)
  188. print('TestOut', self, a, c, go)
  189. if go then error('test error') end
  190. return a + 10, a + 20, 'right version'
  191. end)
  192. ");
  193. str = "hehe";
  194. ret = calc.TestOut(100, out num, ref str);
  195. Debug.Log("ret = " + ret + ", num = " + num + ", str = " + str);
  196. luaenv.DoString(@"
  197. xlua.hotfix(CS.XLuaTest.HotfixCalc, {
  198. Test1 = function(self)
  199. print('Test1', self)
  200. return 1
  201. end;
  202. Test2 = function(self, a, b)
  203. print('Test1', self, a, b)
  204. return a + 10, 1024, b
  205. end;
  206. Test3 = function(a)
  207. print(a)
  208. return 10
  209. end;
  210. Test4 = function(a)
  211. print(a)
  212. end;
  213. Test5 = function(self, a, ...)
  214. print('Test4', self, a, ...)
  215. end
  216. })
  217. ");
  218. int r1 = calc.Test1<int>();
  219. double r2 = calc.Test1<double>();
  220. Debug.Log("r1:" + r1 + ",r2:" + r2);
  221. string ss = "heihei";
  222. int r3 = calc.Test2(r1, out r2, ref ss);
  223. Debug.Log("r1:" + r1 + ",r2:" + r2 + ",r3:" + r3 + ",ss:" + ss);
  224. r3 = HotfixCalc.Test3("test3");
  225. r3 = HotfixCalc.Test3(2);
  226. r3 = HotfixCalc.Test3(this);
  227. Debug.Log("r3:" + r3);
  228. HotfixCalc.Test4(this);
  229. HotfixCalc.Test4(2);
  230. calc.Test5(10, "a", "b", "c");
  231. calc.Test5(10, 1, 3, 5);
  232. Debug.Log("----------------------before------------------------");
  233. TestStateful();
  234. System.GC.Collect();
  235. System.GC.WaitForPendingFinalizers();
  236. luaenv.DoString(@"
  237. local util = require 'xlua.util'
  238. xlua.hotfix(CS.XLuaTest.StatefullTest, {
  239. ['.ctor'] = function(csobj)
  240. util.state(csobj, {evt = {}, start = 0, prop = 0})
  241. end;
  242. set_AProp = function(self, v)
  243. print('set_AProp', v)
  244. self.prop = v
  245. end;
  246. get_AProp = function(self)
  247. return self.prop
  248. end;
  249. get_Item = function(self, k)
  250. print('get_Item', k)
  251. return 1024
  252. end;
  253. set_Item = function(self, k, v)
  254. print('set_Item', k, v)
  255. end;
  256. add_AEvent = function(self, cb)
  257. print('add_AEvent', cb)
  258. table.insert(self.evt, cb)
  259. end;
  260. remove_AEvent = function(self, cb)
  261. print('remove_AEvent', cb)
  262. for i, v in ipairs(self.evt) do
  263. if v == cb then
  264. table.remove(self.evt, i)
  265. break
  266. end
  267. end
  268. end;
  269. Start = function(self)
  270. print('Start')
  271. for _, cb in ipairs(self.evt) do
  272. cb(self.start, 2)
  273. end
  274. self.start = self.start + 1
  275. end;
  276. StaticFunc = function(a, b, c)
  277. print(a, b, c)
  278. end;
  279. GenericTest = function(self, a)
  280. print(self, a)
  281. end;
  282. Finalize = function(self)
  283. print('Finalize', self)
  284. end
  285. })
  286. ");
  287. Debug.Log("----------------------after------------------------");
  288. TestStateful();
  289. luaenv.FullGc();
  290. System.GC.Collect();
  291. System.GC.WaitForPendingFinalizers();
  292. var genericObj = new GenericClass<double>(1.1);
  293. genericObj.Func1();
  294. Debug.Log(genericObj.Func2());
  295. luaenv.DoString(@"
  296. xlua.hotfix(CS.XLuaTest.GenericClass(CS.System.Double), {
  297. ['.ctor'] = function(obj, a)
  298. print('GenericClass<double>', obj, a)
  299. end;
  300. Func1 = function(obj)
  301. print('GenericClass<double>.Func1', obj)
  302. end;
  303. Func2 = function(obj)
  304. print('GenericClass<double>.Func2', obj)
  305. return 1314
  306. end
  307. })
  308. ");
  309. genericObj = new GenericClass<double>(1.1);
  310. genericObj.Func1();
  311. Debug.Log(genericObj.Func2());
  312. InnerTypeTest itt = new InnerTypeTest();
  313. itt.Foo();
  314. luaenv.DoString(@"
  315. xlua.hotfix(CS.XLuaTest.InnerTypeTest, 'Bar', function(obj)
  316. print('lua Bar', obj)
  317. return {x = 10, y = 20}
  318. end)
  319. ");
  320. itt.Foo();
  321. StructTest st = new StructTest(gameObject);
  322. Debug.Log("go=" + st.GetGo(123, "john"));
  323. luaenv.DoString(@"
  324. xlua.hotfix(CS.XLuaTest.StructTest, 'GetGo', function(self, a, b)
  325. print('GetGo', self, a, b)
  326. return nil
  327. end)
  328. ");
  329. Debug.Log("go=" + st.GetGo(123, "john"));
  330. GenericStruct<int> gs = new GenericStruct<int>(1);
  331. Debug.Log("gs.GetA()=" + gs.GetA(123));
  332. luaenv.DoString(@"
  333. xlua.hotfix(CS.XLuaTest.GenericStruct(CS.System.Int32), 'GetA', function(self, a)
  334. print('GetA',self, a)
  335. return 789
  336. end)
  337. ");
  338. Debug.Log("gs.GetA()=" + gs.GetA(123));
  339. try
  340. {
  341. calc.TestOut(100, out num, ref str, gameObject);
  342. }
  343. catch (LuaException e)
  344. {
  345. Debug.Log("throw in lua an catch in c# ok, e.Message:" + e.Message);
  346. }
  347. BaseTestBase<InnerTypeTest> bt = new BaseTest();
  348. bt.Foo(1);
  349. Debug.Log(bt);
  350. luaenv.DoString(@"
  351. xlua.hotfix(CS.XLuaTest.BaseTest, 'Foo', function(self, p)
  352. print('BaseTest', p)
  353. end)
  354. xlua.hotfix(CS.XLuaTest.BaseTest, 'ToString', function(self)
  355. return '>>>' .. base(self):ToString()
  356. end)
  357. ");
  358. bt.Foo(2);
  359. Debug.Log(bt);
  360. }
  361. void TestStateful()
  362. {
  363. StatefullTest sft = new StatefullTest();
  364. sft.AProp = 10;
  365. Debug.Log("sft.AProp:" + sft.AProp);
  366. sft["1"] = 1;
  367. Debug.Log("sft['1']:" + sft["1"]);
  368. System.Action<int, double> cb = (a, b) =>
  369. {
  370. Debug.Log("a:" + a + ",b:" + b);
  371. };
  372. sft.AEvent += cb;
  373. sft.Start();
  374. sft.Start();
  375. sft.AEvent -= cb;
  376. sft.Start();
  377. StatefullTest.StaticFunc(1, 2);
  378. StatefullTest.StaticFunc("e", 3, 4);
  379. sft.GenericTest(1);
  380. sft.GenericTest("hehe");
  381. }
  382. // Update is called once per frame
  383. void Update()
  384. {
  385. }
  386. }
  387. }