Init.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Threading;
  3. using CommandLine;
  4. using NLog;
  5. namespace ET
  6. {
  7. internal static class Init
  8. {
  9. private static void Main(string[] args)
  10. {
  11. try
  12. {
  13. AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
  14. {
  15. Log.Error(e.ExceptionObject.ToString());
  16. };
  17. // 异步方法全部会回掉到主线程
  18. SynchronizationContext.SetSynchronizationContext(ThreadSynchronizationContext.Instance);
  19. // 命令行参数
  20. Options options = null;
  21. Parser.Default.ParseArguments<Options>(args)
  22. .WithNotParsed(error => throw new Exception($"命令行格式错误! {error}"))
  23. .WithParsed(o => { options = o; });
  24. Game.AddSingleton(options);
  25. Game.AddSingleton<RandomGenerator>();
  26. Game.AddSingleton<TimeInfo>();
  27. Game.AddSingleton<Logger>().ILog = new NLogger(Options.Instance.AppType.ToString(), Options.Instance.Process, "../Config/NLog/NLog.config");
  28. Game.AddSingleton<ObjectPool>();
  29. Game.AddSingleton<IdGenerater>();
  30. Game.AddSingleton<EventSystem>();
  31. Game.AddSingleton<NetServices>();
  32. Game.AddSingleton<Root>();
  33. ETTask.ExceptionHandler += Log.Error;
  34. Game.AddSingleton<CodeLoader>().Start();
  35. Log.Console($"app start: {Game.Scene.Id} options: {JsonHelper.ToJson(Options.Instance)} ");
  36. while (true)
  37. {
  38. try
  39. {
  40. Thread.Sleep(1);
  41. Game.Update();
  42. Game.LateUpdate();
  43. }
  44. catch (Exception e)
  45. {
  46. Log.Error(e);
  47. }
  48. }
  49. }
  50. catch (Exception e)
  51. {
  52. Log.Error(e);
  53. }
  54. }
  55. }
  56. }