Init.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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<TimeInfo>();
  26. Game.AddSingleton<Logger>().ILog = new NLogger(Options.Instance.AppType.ToString(), Options.Instance.Process, "../Config/NLog/NLog.config");
  27. Game.AddSingleton<ObjectPool>();
  28. Game.AddSingleton<IdGenerater>();
  29. Game.AddSingleton<EventSystem>();
  30. Game.AddSingleton<Root>();
  31. ETTask.ExceptionHandler += Log.Error;
  32. Game.AddSingleton<CodeLoader>().Start();
  33. Log.Console($"app start: {Game.Scene.Id} options: {JsonHelper.ToJson(Options.Instance)} ");
  34. while (true)
  35. {
  36. try
  37. {
  38. Thread.Sleep(1);
  39. Game.Update();
  40. Game.LateUpdate();
  41. }
  42. catch (Exception e)
  43. {
  44. Log.Error(e);
  45. }
  46. }
  47. }
  48. catch (Exception e)
  49. {
  50. Log.Error(e);
  51. }
  52. }
  53. }
  54. }