Program.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.IO;
  3. using System.IO.Pipes;
  4. using System.Threading;
  5. using CommandLine;
  6. using GFGGame;
  7. using NLog;
  8. namespace ET
  9. {
  10. internal static class Program
  11. {
  12. private static void Main(string[] args)
  13. {
  14. var processId = Environment.ProcessId;
  15. Console.WriteLine("Process ID: " + processId);
  16. AppDomain.CurrentDomain.UnhandledException += (sender, e) => { Log.Error(e.ExceptionObject.ToString()); };
  17. ETTask.ExceptionHandler += Log.Error;
  18. // 异步方法全部会回掉到主线程
  19. SynchronizationContext.SetSynchronizationContext(ThreadSynchronizationContext.Instance);
  20. try
  21. {
  22. // 命令行参数
  23. Options options = null;
  24. Parser.Default.ParseArguments<Options>(args)
  25. .WithNotParsed(error => throw new Exception($"命令行格式错误!"))
  26. .WithParsed(o => { options = o; });
  27. Options.Instance = options;
  28. Log.ILog = new NLogger(Game.Options.AppType.ToString());
  29. //用于后台收集日志
  30. LogExt.ILog = new NLogger("GFGLog");
  31. LogManager.Configuration.Variables["appIdFormat"] = $"{Game.Options.Process:000000}";
  32. Game.EventSystem.Add(typeof(Game).Assembly);
  33. Game.EventSystem.Add(DllHelper.GetHotfixAssembly());
  34. ProtobufHelper.Init();
  35. MongoRegister.Init();
  36. Log.Info($"server start........................ {Game.Scene.Id}");
  37. Game.EventSystem.Publish(new EventType.AppStart());
  38. AppDomain.CurrentDomain.ProcessExit += (sender, e) =>
  39. {
  40. var isClose = new ParamHolder<bool>(false);
  41. if (options.AppType == AppType.Watcher)
  42. {
  43. Game.EventSystem.AppClose(isClose);
  44. while (!isClose.Value)
  45. {
  46. Thread.Sleep(1000);
  47. }
  48. }
  49. else
  50. {
  51. Game.EventSystem.AppClose(isClose);
  52. Thread.Sleep(3000);
  53. }
  54. };
  55. while (true)
  56. {
  57. try
  58. {
  59. Thread.Sleep(1);
  60. Game.Update();
  61. Game.LateUpdate();
  62. Game.FrameFinish();
  63. }
  64. catch (Exception e)
  65. {
  66. Log.Error(e);
  67. }
  68. }
  69. }
  70. catch (Exception e)
  71. {
  72. Log.Error(e);
  73. }
  74. }
  75. }
  76. }