Init.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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($"命令行格式错误!"))
  23. .WithParsed(o => { options = o; });
  24. Options.Instance = options;
  25. LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration("../Config/NLog/NLog.config");
  26. LogManager.Configuration.Variables["appIdFormat"] = $"{Game.Options.Process:000000}";
  27. LogManager.Configuration.Variables["currentDir"] = Environment.CurrentDirectory;
  28. Game.ILog = new NLogger(Game.Options.AppType.ToString());
  29. ETTask.ExceptionHandler += Log.Error;
  30. CodeLoader.Instance.Start();
  31. Log.Console($"app start: {Game.Scene.Id} options: {JsonHelper.ToJson(Game.Options)} ");
  32. while (true)
  33. {
  34. try
  35. {
  36. Thread.Sleep(1);
  37. Game.Update();
  38. Game.LateUpdate();
  39. }
  40. catch (Exception e)
  41. {
  42. Log.Error(e);
  43. }
  44. }
  45. }
  46. catch (Exception e)
  47. {
  48. Log.Error(e);
  49. }
  50. }
  51. }
  52. }