Program.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Threading;
  3. using ETModel;
  4. using NLog;
  5. namespace App
  6. {
  7. internal static class Program
  8. {
  9. private static void Main(string[] args)
  10. {
  11. // 异步方法全部会回掉到主线程
  12. SynchronizationContext.SetSynchronizationContext(OneThreadSynchronizationContext.Instance);
  13. try
  14. {
  15. Game.EventSystem.Add(DLLType.Model, typeof(Game).Assembly);
  16. Game.EventSystem.Add(DLLType.Hotfix, DllHelper.GetHotfixAssembly());
  17. Options options = Game.Scene.AddComponent<OptionComponent, string[]>(args).Options;
  18. StartConfig startConfig = Game.Scene.AddComponent<StartConfigComponent, string, int>(options.Config, options.AppId).StartConfig;
  19. if (!options.AppType.Is(startConfig.AppType))
  20. {
  21. Log.Error("命令行参数apptype与配置不一致");
  22. return;
  23. }
  24. IdGenerater.AppId = options.AppId;
  25. LogManager.Configuration.Variables["appType"] = $"{startConfig.AppType}";
  26. LogManager.Configuration.Variables["appId"] = $"{startConfig.AppId}";
  27. LogManager.Configuration.Variables["appTypeFormat"] = $"{startConfig.AppType, -8}";
  28. LogManager.Configuration.Variables["appIdFormat"] = $"{startConfig.AppId:0000}";
  29. Log.Info($"server start........................ {startConfig.AppId} {startConfig.AppType}");
  30. Game.Scene.AddComponent<TimerComponent>();
  31. Game.Scene.AddComponent<OpcodeTypeComponent>();
  32. Game.Scene.AddComponent<MessageDispatcherComponent>();
  33. // 根据不同的AppType添加不同的组件
  34. OuterConfig outerConfig = startConfig.GetComponent<OuterConfig>();
  35. InnerConfig innerConfig = startConfig.GetComponent<InnerConfig>();
  36. ClientConfig clientConfig = startConfig.GetComponent<ClientConfig>();
  37. switch (startConfig.AppType)
  38. {
  39. case AppType.Manager:
  40. Game.Scene.AddComponent<AppManagerComponent>();
  41. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  42. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  43. break;
  44. case AppType.Realm:
  45. Game.Scene.AddComponent<MailboxDispatcherComponent>();
  46. Game.Scene.AddComponent<ActorMessageDispatcherComponent>();
  47. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  48. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  49. Game.Scene.AddComponent<LocationProxyComponent>();
  50. Game.Scene.AddComponent<RealmGateAddressComponent>();
  51. break;
  52. case AppType.Gate:
  53. Game.Scene.AddComponent<PlayerComponent>();
  54. Game.Scene.AddComponent<MailboxDispatcherComponent>();
  55. Game.Scene.AddComponent<ActorMessageDispatcherComponent>();
  56. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  57. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  58. Game.Scene.AddComponent<LocationProxyComponent>();
  59. Game.Scene.AddComponent<ActorMessageSenderComponent>();
  60. Game.Scene.AddComponent<ActorLocationSenderComponent>();
  61. Game.Scene.AddComponent<GateSessionKeyComponent>();
  62. Game.Scene.AddComponent<CoroutineLockComponent>();
  63. break;
  64. case AppType.Location:
  65. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  66. Game.Scene.AddComponent<LocationComponent>();
  67. Game.Scene.AddComponent<CoroutineLockComponent>();
  68. break;
  69. case AppType.Map:
  70. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  71. Game.Scene.AddComponent<UnitComponent>();
  72. Game.Scene.AddComponent<LocationProxyComponent>();
  73. Game.Scene.AddComponent<ActorMessageSenderComponent>();
  74. Game.Scene.AddComponent<ActorLocationSenderComponent>();
  75. Game.Scene.AddComponent<MailboxDispatcherComponent>();
  76. Game.Scene.AddComponent<ActorMessageDispatcherComponent>();
  77. Game.Scene.AddComponent<PathfindingComponent>();
  78. Game.Scene.AddComponent<CoroutineLockComponent>();
  79. break;
  80. case AppType.AllServer:
  81. // 发送普通actor消息
  82. Game.Scene.AddComponent<ActorMessageSenderComponent>();
  83. // 发送location actor消息
  84. Game.Scene.AddComponent<ActorLocationSenderComponent>();
  85. //Game.Scene.AddComponent<DBComponent>();
  86. //Game.Scene.AddComponent<DBProxyComponent>();
  87. // location server需要的组件
  88. Game.Scene.AddComponent<LocationComponent>();
  89. // 访问location server的组件
  90. Game.Scene.AddComponent<LocationProxyComponent>();
  91. // 这两个组件是处理actor消息使用的
  92. Game.Scene.AddComponent<MailboxDispatcherComponent>();
  93. Game.Scene.AddComponent<ActorMessageDispatcherComponent>();
  94. // 内网消息组件
  95. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  96. // 外网消息组件
  97. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  98. // manager server组件,用来管理其它进程使用
  99. Game.Scene.AddComponent<AppManagerComponent>();
  100. Game.Scene.AddComponent<RealmGateAddressComponent>();
  101. Game.Scene.AddComponent<GateSessionKeyComponent>();
  102. // 配置管理
  103. Game.Scene.AddComponent<ConfigComponent>();
  104. // recast寻路组件
  105. Game.Scene.AddComponent<PathfindingComponent>();
  106. Game.Scene.AddComponent<PlayerComponent>();
  107. Game.Scene.AddComponent<UnitComponent>();
  108. Game.Scene.AddComponent<ConsoleComponent>();
  109. Game.Scene.AddComponent<CoroutineLockComponent>();
  110. // Game.Scene.AddComponent<HttpComponent>();
  111. break;
  112. case AppType.Benchmark:
  113. Game.Scene.AddComponent<NetOuterComponent>();
  114. Game.Scene.AddComponent<BenchmarkComponent, string>(clientConfig.Address);
  115. break;
  116. case AppType.BenchmarkWebsocketServer:
  117. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  118. break;
  119. case AppType.BenchmarkWebsocketClient:
  120. Game.Scene.AddComponent<NetOuterComponent>();
  121. Game.Scene.AddComponent<WebSocketBenchmarkComponent, string>(clientConfig.Address);
  122. break;
  123. default:
  124. throw new Exception($"命令行参数没有设置正确的AppType: {startConfig.AppType}");
  125. }
  126. while (true)
  127. {
  128. try
  129. {
  130. Thread.Sleep(1);
  131. OneThreadSynchronizationContext.Instance.Update();
  132. Game.EventSystem.Update();
  133. }
  134. catch (Exception e)
  135. {
  136. Log.Error(e);
  137. }
  138. }
  139. }
  140. catch (Exception e)
  141. {
  142. Log.Error(e);
  143. }
  144. }
  145. }
  146. }