Program.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. break;
  63. case AppType.Location:
  64. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  65. Game.Scene.AddComponent<LocationComponent>();
  66. break;
  67. case AppType.Map:
  68. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  69. Game.Scene.AddComponent<UnitComponent>();
  70. Game.Scene.AddComponent<LocationProxyComponent>();
  71. Game.Scene.AddComponent<ActorMessageSenderComponent>();
  72. Game.Scene.AddComponent<ActorLocationSenderComponent>();
  73. Game.Scene.AddComponent<MailboxDispatcherComponent>();
  74. Game.Scene.AddComponent<ActorMessageDispatcherComponent>();
  75. Game.Scene.AddComponent<PathfindingComponent>();
  76. break;
  77. case AppType.AllServer:
  78. // 发送普通actor消息
  79. Game.Scene.AddComponent<ActorMessageSenderComponent>();
  80. // 发送location actor消息
  81. Game.Scene.AddComponent<ActorLocationSenderComponent>();
  82. //Game.Scene.AddComponent<DBComponent>();
  83. //Game.Scene.AddComponent<DBProxyComponent>();
  84. // location server需要的组件
  85. Game.Scene.AddComponent<LocationComponent>();
  86. // 访问location server的组件
  87. Game.Scene.AddComponent<LocationProxyComponent>();
  88. // 这两个组件是处理actor消息使用的
  89. Game.Scene.AddComponent<MailboxDispatcherComponent>();
  90. Game.Scene.AddComponent<ActorMessageDispatcherComponent>();
  91. // 内网消息组件
  92. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  93. // 外网消息组件
  94. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  95. // manager server组件,用来管理其它进程使用
  96. Game.Scene.AddComponent<AppManagerComponent>();
  97. Game.Scene.AddComponent<RealmGateAddressComponent>();
  98. Game.Scene.AddComponent<GateSessionKeyComponent>();
  99. // 配置管理
  100. Game.Scene.AddComponent<ConfigComponent>();
  101. // recast寻路组件
  102. Game.Scene.AddComponent<PathfindingComponent>();
  103. Game.Scene.AddComponent<PlayerComponent>();
  104. Game.Scene.AddComponent<UnitComponent>();
  105. Game.Scene.AddComponent<ConsoleComponent>();
  106. // Game.Scene.AddComponent<HttpComponent>();
  107. break;
  108. case AppType.Benchmark:
  109. Game.Scene.AddComponent<NetOuterComponent>();
  110. Game.Scene.AddComponent<BenchmarkComponent, string>(clientConfig.Address);
  111. break;
  112. case AppType.BenchmarkWebsocketServer:
  113. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  114. break;
  115. case AppType.BenchmarkWebsocketClient:
  116. Game.Scene.AddComponent<NetOuterComponent>();
  117. Game.Scene.AddComponent<WebSocketBenchmarkComponent, string>(clientConfig.Address);
  118. break;
  119. default:
  120. throw new Exception($"命令行参数没有设置正确的AppType: {startConfig.AppType}");
  121. }
  122. while (true)
  123. {
  124. try
  125. {
  126. Thread.Sleep(1);
  127. OneThreadSynchronizationContext.Instance.Update();
  128. Game.EventSystem.Update();
  129. }
  130. catch (Exception e)
  131. {
  132. Log.Error(e);
  133. }
  134. }
  135. }
  136. catch (Exception e)
  137. {
  138. Log.Error(e);
  139. }
  140. }
  141. }
  142. }