Init.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using CommandLine;
  4. using MemoryPack;
  5. namespace ET
  6. {
  7. public static class Init
  8. {
  9. private static Process process;
  10. public static void Start()
  11. {
  12. try
  13. {
  14. AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
  15. {
  16. Log.Error(e.ExceptionObject.ToString());
  17. };
  18. process = Game.Instance.Create();
  19. // 异步方法全部会回掉到主线程
  20. process.AddSingleton<MainThreadSynchronizationContext>();
  21. // 命令行参数
  22. Parser.Default.ParseArguments<Options>(System.Environment.GetCommandLineArgs())
  23. .WithNotParsed(error => throw new Exception($"命令行格式错误! {error}"))
  24. .WithParsed(Game.Instance.AddSingleton);
  25. Game.Instance.AddSingleton<Logger>().ILog = new NLogger(Options.Instance.AppType.ToString(), Options.Instance.Process, "../Config/NLog/NLog.config");
  26. Game.Instance.AddSingleton<EventSystem>();
  27. ThreadPoolScheduler threadPoolScheduler = Game.Instance.AddSingleton<ThreadPoolScheduler>();
  28. threadPoolScheduler.ThreadCount = 10;
  29. ETTask.ExceptionHandler += Log.Error;
  30. process.AddSingleton<TimeInfo>();
  31. process.AddSingleton<ObjectPool>();
  32. process.AddSingleton<IdGenerater>();
  33. process.AddSingleton<TimerComponent>();
  34. process.AddSingleton<CoroutineLockComponent>();
  35. Log.Console($"{Parser.Default.FormatCommandLine(Options.Instance)}");
  36. process.AddSingleton<CodeLoader>().Start();
  37. }
  38. catch (Exception e)
  39. {
  40. Log.Error(e);
  41. }
  42. }
  43. public static void Update()
  44. {
  45. process.Update();
  46. }
  47. public static void LateUpdate()
  48. {
  49. process.LateUpdate();
  50. }
  51. public static void FrameFinishUpdate()
  52. {
  53. process.FrameFinishUpdate();
  54. }
  55. }
  56. }