NetWorkClient.cs 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /*
  2. #########
  3. ############
  4. #############
  5. ## ###########
  6. ### ###### #####
  7. ### ####### ####
  8. ### ########## ####
  9. #### ########### ####
  10. #### ########### #####
  11. ##### ### ######## #####
  12. ##### ### ######## ######
  13. ###### ### ########### ######
  14. ###### #### ############## ######
  15. ####### ##################### ######
  16. ####### ###################### ######
  17. ####### ###### ################# ######
  18. ####### ###### ###### ######### ######
  19. ####### ## ###### ###### ######
  20. ####### ###### ##### #####
  21. ###### ##### ##### ####
  22. ##### #### ##### ###
  23. ##### ### ### #
  24. ### ### ###
  25. ## ### ###
  26. __________#_______####_______####______________
  27. 我们的未来没有BUG
  28. * ==============================================================================
  29. * Filename: NetWorkClient
  30. * Created: 2018/7/13 14:29:22
  31. * Author: エル・プサイ・コングリィ
  32. * Purpose:
  33. * ==============================================================================
  34. */
  35. #if UNITY_EDITOR || USE_LUA_PROFILER
  36. namespace MikuLuaProfiler
  37. {
  38. using System;
  39. using System.Collections.Generic;
  40. using System.IO;
  41. using System.Net.Sockets;
  42. using System.Text;
  43. using System.Threading;
  44. public static class NetWorkClient
  45. {
  46. private static TcpClient m_client = null;
  47. private static Thread m_sendThread;
  48. private static Thread m_receiveThread;
  49. private static Queue<NetBase> m_sampleQueue = new Queue<NetBase>(256);
  50. private const int PACK_HEAD = 0x23333333;
  51. private static NetworkStream ns;
  52. public static MBinaryWriter bw;
  53. private static BinaryReader br;
  54. private static int m_frameCount = 0;
  55. #region public
  56. public static void ConnectServer(string host, int port)
  57. {
  58. if (m_client != null) return;
  59. m_client = new TcpClient();
  60. m_client.NoDelay = true;
  61. try
  62. {
  63. m_client.Connect(host, port);
  64. UnityEngine.Debug.Log("<color=#00ff00>connect success</color>");
  65. m_client.Client.SendTimeout = 30000;
  66. //m_sampleDict.Clear();
  67. m_strDict.Clear();
  68. m_key = 0;
  69. ns = m_client.GetStream();
  70. bw = new MBinaryWriter(ns);
  71. br = new BinaryReader(ns);
  72. m_sendThread = new Thread(new ThreadStart(DoSendMessage));
  73. m_sendThread.Start();
  74. m_receiveThread = new Thread(new ThreadStart(DoRecieveMessage));
  75. m_receiveThread.Priority = ThreadPriority.Lowest;
  76. m_receiveThread.Start();
  77. }
  78. catch (Exception e)
  79. {
  80. UnityEngine.Debug.Log(e);
  81. Close();
  82. }
  83. }
  84. public static void Close()
  85. {
  86. try
  87. {
  88. if (m_client != null)
  89. {
  90. if (m_client.Connected)
  91. {
  92. m_client.Close();
  93. }
  94. m_client = null;
  95. }
  96. m_sampleQueue.Clear();
  97. }
  98. catch (Exception e)
  99. {
  100. UnityEngine.Debug.Log(e);
  101. }
  102. finally
  103. {
  104. m_strDict.Clear();
  105. }
  106. if (m_receiveThread != null)
  107. {
  108. var tmp = m_receiveThread;
  109. m_receiveThread = null;
  110. tmp.Abort();
  111. }
  112. if (m_sendThread != null)
  113. {
  114. var tmp = m_sendThread;
  115. m_sendThread = null;
  116. tmp.Abort();
  117. }
  118. }
  119. //private static Dictionary<string, Sample> m_sampleDict = new Dictionary<string, Sample>(256);
  120. public static void SendMessage(NetBase sample)
  121. {
  122. if (m_client == null)
  123. {
  124. sample.Restore();
  125. return;
  126. }
  127. lock (m_sampleQueue)
  128. {
  129. m_sampleQueue.Enqueue(sample);
  130. }
  131. }
  132. #endregion
  133. #region private
  134. private static void DoSendMessage()
  135. {
  136. while (true)
  137. {
  138. try
  139. {
  140. if (m_sendThread == null)
  141. {
  142. UnityEngine.Debug.LogError("<color=#ff0000>m_sendThread null</color>");
  143. return;
  144. }
  145. if (m_sampleQueue.Count > 0)
  146. {
  147. while (m_sampleQueue.Count > 0)
  148. {
  149. NetBase s = null;
  150. lock (m_sampleQueue)
  151. {
  152. s = m_sampleQueue.Dequeue();
  153. }
  154. bw.Write(PACK_HEAD);
  155. if (s is Sample)
  156. {
  157. bw.Write((int)0);
  158. }
  159. else if (s is LuaRefInfo)
  160. {
  161. bw.Write((int)1);
  162. }
  163. else if (s is LuaDiffInfo)
  164. {
  165. bw.Write((int)2);
  166. }
  167. Serialize(s, bw);
  168. s.Restore();
  169. }
  170. }
  171. else if (m_frameCount != HookLuaSetup.frameCount)
  172. {
  173. bw.Write(PACK_HEAD);
  174. //写入message 头编号
  175. bw.Write((int)0);
  176. Sample s = Sample.Create(0, (int)LuaLib.GetLuaMemory(LuaProfiler.mainL), "");
  177. Serialize(s, bw);
  178. s.Restore();
  179. m_frameCount = HookLuaSetup.frameCount;
  180. }
  181. Thread.Sleep(10);
  182. }
  183. #pragma warning disable 0168
  184. catch (ThreadAbortException e) { }
  185. catch (Exception e)
  186. {
  187. UnityEngine.Debug.Log(e);
  188. Close();
  189. }
  190. #pragma warning restore 0168
  191. }
  192. }
  193. private static void DoRecieveMessage()
  194. {
  195. while (true)
  196. {
  197. try
  198. {
  199. if (m_receiveThread == null)
  200. {
  201. UnityEngine.Debug.LogError("<color=#ff0000>m_receiveThread null</color>");
  202. return;
  203. }
  204. if (ns.CanRead && ns.DataAvailable)
  205. {
  206. int head = br.ReadInt32();
  207. //处理粘包
  208. while (head == PACK_HEAD)
  209. {
  210. int messageId = br.ReadInt32();
  211. switch (messageId)
  212. {
  213. case 0:
  214. {
  215. LuaProfiler.SendAllRef();
  216. }
  217. break;
  218. case 1:
  219. {
  220. HookLuaSetup.RegisterAction(()=> { LuaHook.Record(); });
  221. }
  222. break;
  223. case 2:
  224. {
  225. HookLuaSetup.RegisterAction(LuaHook.DiffServer);
  226. }
  227. break;
  228. }
  229. head = br.ReadInt32();
  230. }
  231. }
  232. }
  233. catch (Exception e)
  234. {
  235. UnityEngine.Debug.Log(e);
  236. }
  237. Thread.Sleep(10);
  238. }
  239. }
  240. private static int m_key = 0;
  241. public static int GetUniqueKey()
  242. {
  243. return m_key++;
  244. }
  245. private static Dictionary<string, KeyValuePair<int, byte[]>> m_strDict = new Dictionary<string, KeyValuePair<int, byte[]>>(8192);
  246. private static bool GetBytes(string s, out byte[] result, out int index)
  247. {
  248. bool ret = true;
  249. KeyValuePair<int, byte[]> keyValuePair;
  250. if (!m_strDict.TryGetValue(s, out keyValuePair))
  251. {
  252. result = Encoding.UTF8.GetBytes(s);
  253. index = GetUniqueKey();
  254. keyValuePair = new KeyValuePair<int, byte[]>(index, result);
  255. m_strDict.Add(s, keyValuePair);
  256. ret = false;
  257. }
  258. else
  259. {
  260. index = keyValuePair.Key;
  261. result = keyValuePair.Value;
  262. }
  263. return ret;
  264. }
  265. private static void Serialize(NetBase o, BinaryWriter bw)
  266. {
  267. if (o is Sample)
  268. {
  269. Sample s = (Sample)o;
  270. bw.Write(s.calls);
  271. bw.Write(s.frameCount);
  272. bw.Write(s.fps);
  273. bw.Write(s.pss);
  274. bw.Write(s.power);
  275. bw.Write(s.costLuaGC);
  276. bw.Write(s.costMonoGC);
  277. WriteString(bw, s.name);
  278. bw.Write(s.costTime);
  279. bw.Write(s.currentLuaMemory);
  280. bw.Write(s.currentMonoMemory);
  281. bw.Write((ushort)s.childs.Count);
  282. // 防止多线程 栈展开故意做的优化,先自己展开28层,后面的就递归
  283. var childs0 = s.childs;
  284. for (int i0 = 0, i0max = childs0.Count; i0 < i0max; i0++)
  285. {
  286. Sample s0 = childs0[i0];
  287. bw.Write(s0.calls);
  288. bw.Write(s0.frameCount);
  289. bw.Write(s0.fps);
  290. bw.Write(s0.pss);
  291. bw.Write(s0.power);
  292. bw.Write(s0.costLuaGC);
  293. bw.Write(s0.costMonoGC);
  294. WriteString(bw, s0.name);
  295. bw.Write(s0.costTime);
  296. bw.Write(s0.currentLuaMemory);
  297. bw.Write(s0.currentMonoMemory);
  298. bw.Write((ushort)s0.childs.Count);
  299. var childs1 = s0.childs;
  300. for (int i1 = 0, i1max = childs1.Count; i1 < i1max; i1++)
  301. {
  302. Sample s1 = childs1[i1];
  303. bw.Write(s1.calls);
  304. bw.Write(s1.frameCount);
  305. bw.Write(s1.fps);
  306. bw.Write(s1.pss);
  307. bw.Write(s1.power);
  308. bw.Write(s1.costLuaGC);
  309. bw.Write(s1.costMonoGC);
  310. WriteString(bw, s1.name);
  311. bw.Write(s1.costTime);
  312. bw.Write(s1.currentLuaMemory);
  313. bw.Write(s1.currentMonoMemory);
  314. bw.Write((ushort)s1.childs.Count);
  315. var childs2 = s1.childs;
  316. for (int i2 = 0, i2max = childs2.Count; i2 < i2max; i2++)
  317. {
  318. Sample s2 = childs2[i2];
  319. bw.Write(s2.calls);
  320. bw.Write(s2.frameCount);
  321. bw.Write(s2.fps);
  322. bw.Write(s2.pss);
  323. bw.Write(s2.power);
  324. bw.Write(s2.costLuaGC);
  325. bw.Write(s2.costMonoGC);
  326. WriteString(bw, s2.name);
  327. bw.Write(s2.costTime);
  328. bw.Write(s2.currentLuaMemory);
  329. bw.Write(s2.currentMonoMemory);
  330. bw.Write((ushort)s2.childs.Count);
  331. var childs3 = s2.childs;
  332. for (int i3 = 0, i3max = childs3.Count; i3 < i3max; i3++)
  333. {
  334. Sample s3 = childs3[i3];
  335. bw.Write(s3.calls);
  336. bw.Write(s3.frameCount);
  337. bw.Write(s3.fps);
  338. bw.Write(s3.pss);
  339. bw.Write(s3.power);
  340. bw.Write(s3.costLuaGC);
  341. bw.Write(s3.costMonoGC);
  342. WriteString(bw, s3.name);
  343. bw.Write(s3.costTime);
  344. bw.Write(s3.currentLuaMemory);
  345. bw.Write(s3.currentMonoMemory);
  346. bw.Write((ushort)s3.childs.Count);
  347. var childs4 = s3.childs;
  348. for (int i4 = 0, i4max = childs4.Count; i4 < i4max; i4++)
  349. {
  350. Sample s4 = childs4[i4];
  351. bw.Write(s4.calls);
  352. bw.Write(s4.frameCount);
  353. bw.Write(s4.fps);
  354. bw.Write(s4.pss);
  355. bw.Write(s4.power);
  356. bw.Write(s4.costLuaGC);
  357. bw.Write(s4.costMonoGC);
  358. WriteString(bw, s4.name);
  359. bw.Write(s4.costTime);
  360. bw.Write(s4.currentLuaMemory);
  361. bw.Write(s4.currentMonoMemory);
  362. bw.Write((ushort)s4.childs.Count);
  363. var childs5 = s4.childs;
  364. for (int i5 = 0, i5max = childs5.Count; i5 < i5max; i5++)
  365. {
  366. Sample s5 = childs5[i5];
  367. bw.Write(s5.calls);
  368. bw.Write(s5.frameCount);
  369. bw.Write(s5.fps);
  370. bw.Write(s5.pss);
  371. bw.Write(s5.power);
  372. bw.Write(s5.costLuaGC);
  373. bw.Write(s5.costMonoGC);
  374. WriteString(bw, s5.name);
  375. bw.Write(s5.costTime);
  376. bw.Write(s5.currentLuaMemory);
  377. bw.Write(s5.currentMonoMemory);
  378. bw.Write((ushort)s5.childs.Count);
  379. var childs6 = s5.childs;
  380. for (int i6 = 0, i6max = childs6.Count; i6 < i6max; i6++)
  381. {
  382. Sample s6 = childs6[i6];
  383. bw.Write(s6.calls);
  384. bw.Write(s6.frameCount);
  385. bw.Write(s6.fps);
  386. bw.Write(s6.pss);
  387. bw.Write(s6.power);
  388. bw.Write(s6.costLuaGC);
  389. bw.Write(s6.costMonoGC);
  390. WriteString(bw, s6.name);
  391. bw.Write(s6.costTime);
  392. bw.Write(s6.currentLuaMemory);
  393. bw.Write(s6.currentMonoMemory);
  394. bw.Write((ushort)s6.childs.Count);
  395. var childs7 = s6.childs;
  396. for (int i7 = 0, i7max = childs7.Count; i7 < i7max; i7++)
  397. {
  398. Sample s7 = childs7[i7];
  399. bw.Write(s7.calls);
  400. bw.Write(s7.frameCount);
  401. bw.Write(s7.fps);
  402. bw.Write(s7.pss);
  403. bw.Write(s7.power);
  404. bw.Write(s7.costLuaGC);
  405. bw.Write(s7.costMonoGC);
  406. WriteString(bw, s7.name);
  407. bw.Write(s7.costTime);
  408. bw.Write(s7.currentLuaMemory);
  409. bw.Write(s7.currentMonoMemory);
  410. bw.Write((ushort)s7.childs.Count);
  411. var childs8 = s7.childs;
  412. for (int i8 = 0, i8max = childs8.Count; i8 < i8max; i8++)
  413. {
  414. Sample s8 = childs8[i8];
  415. bw.Write(s8.calls);
  416. bw.Write(s8.frameCount);
  417. bw.Write(s8.fps);
  418. bw.Write(s8.pss);
  419. bw.Write(s8.power);
  420. bw.Write(s8.costLuaGC);
  421. bw.Write(s8.costMonoGC);
  422. WriteString(bw, s8.name);
  423. bw.Write(s8.costTime);
  424. bw.Write(s8.currentLuaMemory);
  425. bw.Write(s8.currentMonoMemory);
  426. bw.Write((ushort)s8.childs.Count);
  427. var childs9 = s8.childs;
  428. for (int i9 = 0, i9max = childs9.Count; i9 < i9max; i9++)
  429. {
  430. Sample s9 = childs9[i9];
  431. bw.Write(s9.calls);
  432. bw.Write(s9.frameCount);
  433. bw.Write(s9.fps);
  434. bw.Write(s9.pss);
  435. bw.Write(s9.power);
  436. bw.Write(s9.costLuaGC);
  437. bw.Write(s9.costMonoGC);
  438. WriteString(bw, s9.name);
  439. bw.Write(s9.costTime);
  440. bw.Write(s9.currentLuaMemory);
  441. bw.Write(s9.currentMonoMemory);
  442. bw.Write((ushort)s9.childs.Count);
  443. var childs10 = s9.childs;
  444. for (int i10 = 0, i10max = childs10.Count; i10 < i10max; i10++)
  445. {
  446. Sample s10 = childs10[i10];
  447. bw.Write(s10.calls);
  448. bw.Write(s10.frameCount);
  449. bw.Write(s10.fps);
  450. bw.Write(s10.pss);
  451. bw.Write(s10.power);
  452. bw.Write(s10.costLuaGC);
  453. bw.Write(s10.costMonoGC);
  454. WriteString(bw, s10.name);
  455. bw.Write(s10.costTime);
  456. bw.Write(s10.currentLuaMemory);
  457. bw.Write(s10.currentMonoMemory);
  458. bw.Write((ushort)s10.childs.Count);
  459. var childs11 = s10.childs;
  460. for (int i11 = 0, i11max = childs11.Count; i11 < i11max; i11++)
  461. {
  462. Sample s11 = childs11[i11];
  463. bw.Write(s11.calls);
  464. bw.Write(s11.frameCount);
  465. bw.Write(s11.fps);
  466. bw.Write(s11.pss);
  467. bw.Write(s11.power);
  468. bw.Write(s11.costLuaGC);
  469. bw.Write(s11.costMonoGC);
  470. WriteString(bw, s11.name);
  471. bw.Write(s11.costTime);
  472. bw.Write(s11.currentLuaMemory);
  473. bw.Write(s11.currentMonoMemory);
  474. bw.Write((ushort)s11.childs.Count);
  475. var childs12 = s11.childs;
  476. for (int i12 = 0, i12max = childs12.Count; i12 < i12max; i12++)
  477. {
  478. Sample s12 = childs12[i12];
  479. bw.Write(s12.calls);
  480. bw.Write(s12.frameCount);
  481. bw.Write(s12.fps);
  482. bw.Write(s12.pss);
  483. bw.Write(s12.power);
  484. bw.Write(s12.costLuaGC);
  485. bw.Write(s12.costMonoGC);
  486. WriteString(bw, s12.name);
  487. bw.Write(s12.costTime);
  488. bw.Write(s12.currentLuaMemory);
  489. bw.Write(s12.currentMonoMemory);
  490. bw.Write((ushort)s12.childs.Count);
  491. var childs13 = s12.childs;
  492. for (int i13 = 0, i13max = childs13.Count; i13 < i13max; i13++)
  493. {
  494. Sample s13 = childs13[i13];
  495. bw.Write(s13.calls);
  496. bw.Write(s13.frameCount);
  497. bw.Write(s13.fps);
  498. bw.Write(s13.pss);
  499. bw.Write(s13.power);
  500. bw.Write(s13.costLuaGC);
  501. bw.Write(s13.costMonoGC);
  502. WriteString(bw, s13.name);
  503. bw.Write(s13.costTime);
  504. bw.Write(s13.currentLuaMemory);
  505. bw.Write(s13.currentMonoMemory);
  506. bw.Write((ushort)s13.childs.Count);
  507. var childs14 = s13.childs;
  508. for (int i14 = 0, i14max = childs14.Count; i14 < i14max; i14++)
  509. {
  510. Sample s14 = childs14[i14];
  511. bw.Write(s14.calls);
  512. bw.Write(s14.frameCount);
  513. bw.Write(s14.fps);
  514. bw.Write(s14.pss);
  515. bw.Write(s14.power);
  516. bw.Write(s14.costLuaGC);
  517. bw.Write(s14.costMonoGC);
  518. WriteString(bw, s14.name);
  519. bw.Write(s14.costTime);
  520. bw.Write(s14.currentLuaMemory);
  521. bw.Write(s14.currentMonoMemory);
  522. bw.Write((ushort)s14.childs.Count);
  523. var childs15 = s14.childs;
  524. for (int i15 = 0, i15max = childs15.Count; i15 < i15max; i15++)
  525. {
  526. Sample s15 = childs15[i15];
  527. bw.Write(s15.calls);
  528. bw.Write(s15.frameCount);
  529. bw.Write(s15.fps);
  530. bw.Write(s15.pss);
  531. bw.Write(s15.power);
  532. bw.Write(s15.costLuaGC);
  533. bw.Write(s15.costMonoGC);
  534. WriteString(bw, s15.name);
  535. bw.Write(s15.costTime);
  536. bw.Write(s15.currentLuaMemory);
  537. bw.Write(s15.currentMonoMemory);
  538. bw.Write((ushort)s15.childs.Count);
  539. var childs16 = s15.childs;
  540. for (int i16 = 0, i16max = childs16.Count; i16 < i16max; i16++)
  541. {
  542. Sample s16 = childs16[i16];
  543. bw.Write(s16.calls);
  544. bw.Write(s16.frameCount);
  545. bw.Write(s16.fps);
  546. bw.Write(s16.pss);
  547. bw.Write(s16.power);
  548. bw.Write(s16.costLuaGC);
  549. bw.Write(s16.costMonoGC);
  550. WriteString(bw, s16.name);
  551. bw.Write(s16.costTime);
  552. bw.Write(s16.currentLuaMemory);
  553. bw.Write(s16.currentMonoMemory);
  554. bw.Write((ushort)s16.childs.Count);
  555. var childs17 = s16.childs;
  556. for (int i17 = 0, i17max = childs17.Count; i17 < i17max; i17++)
  557. {
  558. Sample s17 = childs17[i17];
  559. bw.Write(s17.calls);
  560. bw.Write(s17.frameCount);
  561. bw.Write(s17.fps);
  562. bw.Write(s17.pss);
  563. bw.Write(s17.power);
  564. bw.Write(s17.costLuaGC);
  565. bw.Write(s17.costMonoGC);
  566. WriteString(bw, s17.name);
  567. bw.Write(s17.costTime);
  568. bw.Write(s17.currentLuaMemory);
  569. bw.Write(s17.currentMonoMemory);
  570. bw.Write((ushort)s17.childs.Count);
  571. var childs18 = s17.childs;
  572. for (int i18 = 0, i18max = childs18.Count; i18 < i18max; i18++)
  573. {
  574. Sample s18 = childs18[i18];
  575. bw.Write(s18.calls);
  576. bw.Write(s18.frameCount);
  577. bw.Write(s18.fps);
  578. bw.Write(s18.pss);
  579. bw.Write(s18.power);
  580. bw.Write(s18.costLuaGC);
  581. bw.Write(s18.costMonoGC);
  582. WriteString(bw, s18.name);
  583. bw.Write(s18.costTime);
  584. bw.Write(s18.currentLuaMemory);
  585. bw.Write(s18.currentMonoMemory);
  586. bw.Write((ushort)s18.childs.Count);
  587. var childs19 = s18.childs;
  588. for (int i19 = 0, i19max = childs19.Count; i19 < i19max; i19++)
  589. {
  590. Sample s19 = childs19[i19];
  591. bw.Write(s19.calls);
  592. bw.Write(s19.frameCount);
  593. bw.Write(s19.fps);
  594. bw.Write(s19.pss);
  595. bw.Write(s19.power);
  596. bw.Write(s19.costLuaGC);
  597. bw.Write(s19.costMonoGC);
  598. WriteString(bw, s19.name);
  599. bw.Write(s19.costTime);
  600. bw.Write(s19.currentLuaMemory);
  601. bw.Write(s19.currentMonoMemory);
  602. bw.Write((ushort)s19.childs.Count);
  603. var childs20 = s19.childs;
  604. for (int i20 = 0, i20max = childs20.Count; i20 < i20max; i20++)
  605. {
  606. Sample s20 = childs20[i20];
  607. bw.Write(s20.calls);
  608. bw.Write(s20.frameCount);
  609. bw.Write(s20.fps);
  610. bw.Write(s20.pss);
  611. bw.Write(s20.power);
  612. bw.Write(s20.costLuaGC);
  613. bw.Write(s20.costMonoGC);
  614. WriteString(bw, s20.name);
  615. bw.Write(s20.costTime);
  616. bw.Write(s20.currentLuaMemory);
  617. bw.Write(s20.currentMonoMemory);
  618. bw.Write((ushort)s20.childs.Count);
  619. var childs21 = s20.childs;
  620. for (int i21 = 0, i21max = childs21.Count; i21 < i21max; i21++)
  621. {
  622. Sample s21 = childs21[i21];
  623. bw.Write(s21.calls);
  624. bw.Write(s21.frameCount);
  625. bw.Write(s21.fps);
  626. bw.Write(s21.pss);
  627. bw.Write(s21.power);
  628. bw.Write(s21.costLuaGC);
  629. bw.Write(s21.costMonoGC);
  630. WriteString(bw, s21.name);
  631. bw.Write(s21.costTime);
  632. bw.Write(s21.currentLuaMemory);
  633. bw.Write(s21.currentMonoMemory);
  634. bw.Write((ushort)s21.childs.Count);
  635. var childs22 = s21.childs;
  636. for (int i22 = 0, i22max = childs22.Count; i22 < i22max; i22++)
  637. {
  638. Sample s22 = childs22[i22];
  639. bw.Write(s22.calls);
  640. bw.Write(s22.frameCount);
  641. bw.Write(s22.fps);
  642. bw.Write(s22.pss);
  643. bw.Write(s22.power);
  644. bw.Write(s22.costLuaGC);
  645. bw.Write(s22.costMonoGC);
  646. WriteString(bw, s22.name);
  647. bw.Write(s22.costTime);
  648. bw.Write(s22.currentLuaMemory);
  649. bw.Write(s22.currentMonoMemory);
  650. bw.Write((ushort)s22.childs.Count);
  651. var childs23 = s22.childs;
  652. for (int i23 = 0, i23max = childs23.Count; i23 < i23max; i23++)
  653. {
  654. Sample s23 = childs23[i23];
  655. bw.Write(s23.calls);
  656. bw.Write(s23.frameCount);
  657. bw.Write(s23.fps);
  658. bw.Write(s23.pss);
  659. bw.Write(s23.power);
  660. bw.Write(s23.costLuaGC);
  661. bw.Write(s23.costMonoGC);
  662. WriteString(bw, s23.name);
  663. bw.Write(s23.costTime);
  664. bw.Write(s23.currentLuaMemory);
  665. bw.Write(s23.currentMonoMemory);
  666. bw.Write((ushort)s23.childs.Count);
  667. var childs24 = s23.childs;
  668. for (int i24 = 0, i24max = childs24.Count; i24 < i24max; i24++)
  669. {
  670. Sample s24 = childs24[i24];
  671. bw.Write(s24.calls);
  672. bw.Write(s24.frameCount);
  673. bw.Write(s24.fps);
  674. bw.Write(s24.pss);
  675. bw.Write(s24.power);
  676. bw.Write(s24.costLuaGC);
  677. bw.Write(s24.costMonoGC);
  678. WriteString(bw, s24.name);
  679. bw.Write(s24.costTime);
  680. bw.Write(s24.currentLuaMemory);
  681. bw.Write(s24.currentMonoMemory);
  682. bw.Write((ushort)s24.childs.Count);
  683. var childs25 = s24.childs;
  684. for (int i25 = 0, i25max = childs25.Count; i25 < i25max; i25++)
  685. {
  686. Sample s25 = childs25[i25];
  687. bw.Write(s25.calls);
  688. bw.Write(s25.frameCount);
  689. bw.Write(s25.fps);
  690. bw.Write(s25.pss);
  691. bw.Write(s25.power);
  692. bw.Write(s25.costLuaGC);
  693. bw.Write(s25.costMonoGC);
  694. WriteString(bw, s25.name);
  695. bw.Write(s25.costTime);
  696. bw.Write(s25.currentLuaMemory);
  697. bw.Write(s25.currentMonoMemory);
  698. bw.Write((ushort)s25.childs.Count);
  699. var childs26 = s25.childs;
  700. for (int i26 = 0, i26max = childs26.Count; i26 < i26max; i26++)
  701. {
  702. Sample s26 = childs26[i26];
  703. bw.Write(s26.calls);
  704. bw.Write(s26.frameCount);
  705. bw.Write(s26.fps);
  706. bw.Write(s26.pss);
  707. bw.Write(s26.power);
  708. bw.Write(s26.costLuaGC);
  709. bw.Write(s26.costMonoGC);
  710. WriteString(bw, s26.name);
  711. bw.Write(s26.costTime);
  712. bw.Write(s26.currentLuaMemory);
  713. bw.Write(s26.currentMonoMemory);
  714. bw.Write((ushort)s26.childs.Count);
  715. var childs27 = s26.childs;
  716. for (int i27 = 0, i27max = childs27.Count; i27 < i27max; i27++)
  717. {
  718. Sample s27 = childs27[i27];
  719. bw.Write(s27.calls);
  720. bw.Write(s27.frameCount);
  721. bw.Write(s27.fps);
  722. bw.Write(s27.pss);
  723. bw.Write(s27.power);
  724. bw.Write(s27.costLuaGC);
  725. bw.Write(s27.costMonoGC);
  726. WriteString(bw, s27.name);
  727. bw.Write(s27.costTime);
  728. bw.Write(s27.currentLuaMemory);
  729. bw.Write(s27.currentMonoMemory);
  730. bw.Write((ushort)s27.childs.Count);
  731. var childs28 = s27.childs;
  732. for (int i28 = 0, i28max = childs28.Count; i28 < i28max; i28++)
  733. {
  734. Sample s28 = childs28[i28];
  735. Serialize(s28, bw);
  736. }
  737. }
  738. }
  739. }
  740. }
  741. }
  742. }
  743. }
  744. }
  745. }
  746. }
  747. }
  748. }
  749. }
  750. }
  751. }
  752. }
  753. }
  754. }
  755. }
  756. }
  757. }
  758. }
  759. }
  760. }
  761. }
  762. }
  763. }
  764. }
  765. }
  766. else if (o is LuaRefInfo)
  767. {
  768. LuaRefInfo r = (LuaRefInfo)o;
  769. bw.Write(r.cmd);
  770. bw.Write(HookLuaSetup.frameCount);
  771. WriteString(bw, r.name);
  772. WriteString(bw, r.addr);
  773. bw.Write(r.type);
  774. }
  775. else if (o is LuaDiffInfo)
  776. {
  777. LuaDiffInfo ld = (LuaDiffInfo)o;
  778. // add
  779. var addDict = ld.addRef;
  780. bw.Write(addDict.Count);
  781. foreach (var item in addDict)
  782. {
  783. bw.Write((int)item.Key);
  784. bw.Write(item.Value.Count);
  785. foreach (var v in item.Value)
  786. {
  787. WriteString(bw, v);
  788. }
  789. }
  790. var addDetail = ld.addDetail;
  791. bw.Write(addDetail.Count);
  792. foreach (var item in addDetail)
  793. {
  794. WriteString(bw, item.Key);
  795. var list = item.Value;
  796. bw.Write(list.Count);
  797. foreach (var listItem in list)
  798. {
  799. WriteString(bw, listItem);
  800. }
  801. }
  802. // rm
  803. var rmDict = ld.rmRef;
  804. bw.Write(rmDict.Count);
  805. foreach (var item in rmDict)
  806. {
  807. bw.Write((int)item.Key);
  808. bw.Write(item.Value.Count);
  809. foreach (var v in item.Value)
  810. {
  811. WriteString(bw, v);
  812. }
  813. }
  814. var rmDetail = ld.rmDetail;
  815. bw.Write(rmDetail.Count);
  816. foreach (var item in rmDetail)
  817. {
  818. WriteString(bw, item.Key);
  819. var list = item.Value;
  820. bw.Write(list.Count);
  821. foreach (var listItem in list)
  822. {
  823. WriteString(bw, listItem);
  824. }
  825. }
  826. // null
  827. var nullDict = ld.nullRef;
  828. bw.Write(nullDict.Count);
  829. foreach (var item in nullDict)
  830. {
  831. bw.Write((int)item.Key);
  832. bw.Write(item.Value.Count);
  833. foreach (var v in item.Value)
  834. {
  835. WriteString(bw, v);
  836. }
  837. }
  838. var nullDetail = ld.nullDetail;
  839. bw.Write(nullDetail.Count);
  840. foreach (var item in nullDetail)
  841. {
  842. WriteString(bw, item.Key);
  843. var list = item.Value;
  844. bw.Write(list.Count);
  845. foreach (var listItem in list)
  846. {
  847. WriteString(bw, listItem);
  848. }
  849. }
  850. }
  851. }
  852. public static void WriteString(BinaryWriter bw, string name)
  853. {
  854. byte[] datas;
  855. int index = 0;
  856. bool isRef = GetBytes(name, out datas, out index);
  857. bw.Write(isRef);
  858. bw.Write(index);
  859. if (!isRef)
  860. {
  861. bw.Write(datas.Length);
  862. bw.Write(datas);
  863. }
  864. }
  865. #endregion
  866. }
  867. }
  868. #endif