Init.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Buffers;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using CommandLine;
  6. using MemoryPack;
  7. using UnityEngine;
  8. namespace ET
  9. {
  10. public class Init: MonoBehaviour
  11. {
  12. private void Start()
  13. {
  14. DontDestroyOnLoad(gameObject);
  15. AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
  16. {
  17. Log.Error(e.ExceptionObject.ToString());
  18. };
  19. Game.AddSingleton<MainThreadSynchronizationContext>();
  20. // 命令行参数
  21. string[] args = "".Split(" ");
  22. Parser.Default.ParseArguments<Options>(args)
  23. .WithNotParsed(error => throw new Exception($"命令行格式错误! {error}"))
  24. .WithParsed(Game.AddSingleton);
  25. Options.Instance.StartConfig = "StartConfig/LockStep";
  26. Game.AddSingleton<TimeInfo>();
  27. Game.AddSingleton<Logger>().ILog = new UnityLogger();
  28. Game.AddSingleton<ObjectPool>();
  29. Game.AddSingleton<IdGenerater>();
  30. Game.AddSingleton<EventSystem>();
  31. Game.AddSingleton<TimerComponent>();
  32. Game.AddSingleton<CoroutineLockComponent>();
  33. ETTask.ExceptionHandler += Log.Error;
  34. Game.AddSingleton<CodeLoader>().Start();
  35. this.cc = new Cc() { aaa = new Aa()};
  36. cc.bbb = new List<Bb>();
  37. cc.bbb.Add(new Bb() {B = 5});
  38. cc.ccc = new Dictionary<long, Bb>();
  39. cc.ccc.Add(1, new Bb() {B = 4});
  40. fixedArrayBufferWriter = new FixedArrayBufferWriter(this.bytes);
  41. //Dd d = new Dd() { aaa = 1, bbb = "ggs" };
  42. //d.ccc = new List<int>() { 1, 2, 3 };
  43. //bytes = MemoryPackSerializer.Serialize(d);
  44. }
  45. private byte[] bytes = new byte[1024];
  46. private Cc cc;
  47. private Cc cs = new Cc() {bbb = new List<Bb>(), ccc = new Dictionary<long, Bb>()};
  48. private Dd dd;
  49. private FixedArrayBufferWriter fixedArrayBufferWriter;
  50. private void Update()
  51. {
  52. fixedArrayBufferWriter.Reset(this.bytes);
  53. MemoryPackSerializer.Serialize(typeof(Cc), fixedArrayBufferWriter, this.cc);
  54. MemoryPackSerializer.Deserialize(bytes, ref this.cs);
  55. //this.bytes = MemoryPackSerializer.Serialize(this.cc);
  56. //MemoryPackSerializer.Deserialize(bytes, ref this.cs);
  57. Game.Update();
  58. }
  59. private void LateUpdate()
  60. {
  61. Game.LateUpdate();
  62. Game.FrameFinishUpdate();
  63. }
  64. private void OnApplicationQuit()
  65. {
  66. Game.Close();
  67. }
  68. }
  69. }