Program.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Threading;
  5. using CommandLine;
  6. using NLog;
  7. namespace ETModel
  8. {
  9. internal static class Program
  10. {
  11. private static void Main(string[] args)
  12. {
  13. // 异步方法全部会回掉到主线程
  14. SynchronizationContext.SetSynchronizationContext(OneThreadSynchronizationContext.Instance);
  15. try
  16. {
  17. Game.EventSystem.Add(DLLType.Model, typeof(Game).Assembly);
  18. Game.EventSystem.Add(DLLType.Hotfix, DllHelper.GetHotfixAssembly());
  19. MongoHelper.Init();
  20. // 命令行参数
  21. Parser.Default.ParseArguments<Options>(args)
  22. .WithNotParsed(error => throw new Exception($"命令行格式错误!"))
  23. .WithParsed(o => { Game.Options = o; });
  24. IdGenerater.AppId = Game.Options.Id;
  25. // 启动配置
  26. StartConfig allConfig = MongoHelper.FromJson<StartConfig>(File.ReadAllText(Path.Combine("../Config/StartConfig/", Game.Options.Config)));
  27. StartConfig startConfig = allConfig.Get(Game.Options.Id);
  28. Game.Scene = EntityFactory.CreateScene(0, "Process", SceneType.Process);
  29. LogManager.Configuration.Variables["appIdFormat"] = $"{Game.Scene.Id:0000}";
  30. Game.Scene.AddComponent<StartConfigComponent, StartConfig, long>(allConfig, startConfig.Id);
  31. Log.Info($"server start........................ {Game.Scene.Id}");
  32. Game.Scene.AddComponent<TimerComponent>();
  33. Game.Scene.AddComponent<OpcodeTypeComponent>();
  34. Game.Scene.AddComponent<MessageDispatcherComponent>();
  35. Game.Scene.AddComponent<ConfigComponent>();
  36. Game.Scene.AddComponent<CoroutineLockComponent>();
  37. // 发送普通actor消息
  38. Game.Scene.AddComponent<ActorMessageSenderComponent>();
  39. // 发送location actor消息
  40. Game.Scene.AddComponent<ActorLocationSenderComponent>();
  41. // 访问location server的组件
  42. Game.Scene.AddComponent<LocationProxyComponent>();
  43. // 这两个组件是处理actor消息使用的
  44. Game.Scene.AddComponent<MailboxDispatcherComponent>();
  45. Game.Scene.AddComponent<ActorMessageDispatcherComponent>();
  46. // 数值订阅组件
  47. Game.Scene.AddComponent<NumericWatcherComponent>();
  48. // 控制台组件
  49. Game.Scene.AddComponent<ConsoleComponent>();
  50. OuterConfig outerConfig = startConfig.GetComponent<OuterConfig>();
  51. if (outerConfig != null)
  52. {
  53. // 外网消息组件
  54. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  55. }
  56. InnerConfig innerConfig = startConfig.GetComponent<InnerConfig>();
  57. if (innerConfig != null)
  58. {
  59. // 内网消息组件
  60. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  61. }
  62. DBConfig dbConfig = startConfig.GetComponent<DBConfig>();
  63. if (dbConfig != null)
  64. {
  65. Game.Scene.AddComponent<DBComponent, DBConfig>(dbConfig);
  66. }
  67. // 先加这里,后面删掉
  68. Game.EventSystem.Run(EventIdType.AfterScenesAdd);
  69. while (true)
  70. {
  71. try
  72. {
  73. Thread.Sleep(1);
  74. OneThreadSynchronizationContext.Instance.Update();
  75. Game.EventSystem.Update();
  76. }
  77. catch (Exception e)
  78. {
  79. Log.Error(e);
  80. }
  81. }
  82. }
  83. catch (Exception e)
  84. {
  85. Log.Error(e);
  86. }
  87. }
  88. }
  89. }