Program.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Threading;
  5. using ETModel;
  6. using NLog;
  7. using PF;
  8. using ABPath = ETModel.ABPath;
  9. using Path = System.IO.Path;
  10. namespace App
  11. {
  12. internal static class Program
  13. {
  14. private static void Main(string[] args)
  15. {
  16. // 异步方法全部会回掉到主线程
  17. SynchronizationContext.SetSynchronizationContext(OneThreadSynchronizationContext.Instance);
  18. try
  19. {
  20. Game.EventSystem.Add(DLLType.Model, typeof(Game).Assembly);
  21. Game.EventSystem.Add(DLLType.Hotfix, DllHelper.GetHotfixAssembly());
  22. Options options = Game.Scene.AddComponent<OptionComponent, string[]>(args).Options;
  23. StartConfig startConfig = Game.Scene.AddComponent<StartConfigComponent, string, int>(options.Config, options.AppId).StartConfig;
  24. if (!options.AppType.Is(startConfig.AppType))
  25. {
  26. Log.Error("命令行参数apptype与配置不一致");
  27. return;
  28. }
  29. IdGenerater.AppId = options.AppId;
  30. LogManager.Configuration.Variables["appType"] = startConfig.AppType.ToString();
  31. LogManager.Configuration.Variables["appId"] = startConfig.AppId.ToString();
  32. LogManager.Configuration.Variables["appTypeFormat"] = $"{startConfig.AppType,-8}";
  33. LogManager.Configuration.Variables["appIdFormat"] = $"{startConfig.AppId:D3}";
  34. Log.Info($"server start........................ {startConfig.AppId} {startConfig.AppType}");
  35. Game.Scene.AddComponent<OpcodeTypeComponent>();
  36. Game.Scene.AddComponent<MessageDispatherComponent>();
  37. // 根据不同的AppType添加不同的组件
  38. OuterConfig outerConfig = startConfig.GetComponent<OuterConfig>();
  39. InnerConfig innerConfig = startConfig.GetComponent<InnerConfig>();
  40. ClientConfig clientConfig = startConfig.GetComponent<ClientConfig>();
  41. switch (startConfig.AppType)
  42. {
  43. case AppType.Manager:
  44. Game.Scene.AddComponent<AppManagerComponent>();
  45. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  46. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  47. break;
  48. case AppType.Realm:
  49. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  50. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  51. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  52. Game.Scene.AddComponent<LocationProxyComponent>();
  53. Game.Scene.AddComponent<RealmGateAddressComponent>();
  54. break;
  55. case AppType.Gate:
  56. Game.Scene.AddComponent<PlayerComponent>();
  57. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  58. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  59. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  60. Game.Scene.AddComponent<LocationProxyComponent>();
  61. Game.Scene.AddComponent<ActorMessageSenderComponent>();
  62. Game.Scene.AddComponent<ActorLocationSenderComponent>();
  63. Game.Scene.AddComponent<GateSessionKeyComponent>();
  64. break;
  65. case AppType.Location:
  66. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  67. Game.Scene.AddComponent<LocationComponent>();
  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<ActorMessageDispatherComponent>();
  76. Game.Scene.AddComponent<PathfindingComponent>();
  77. break;
  78. case AppType.AllServer:
  79. Game.Scene.AddComponent<ActorMessageSenderComponent>();
  80. Game.Scene.AddComponent<ActorLocationSenderComponent>();
  81. Game.Scene.AddComponent<PlayerComponent>();
  82. Game.Scene.AddComponent<UnitComponent>();
  83. Game.Scene.AddComponent<DBComponent>();
  84. Game.Scene.AddComponent<DBProxyComponent>();
  85. Game.Scene.AddComponent<DBCacheComponent>();
  86. Game.Scene.AddComponent<LocationComponent>();
  87. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  88. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  89. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  90. Game.Scene.AddComponent<LocationProxyComponent>();
  91. Game.Scene.AddComponent<AppManagerComponent>();
  92. Game.Scene.AddComponent<RealmGateAddressComponent>();
  93. Game.Scene.AddComponent<GateSessionKeyComponent>();
  94. Game.Scene.AddComponent<ConfigComponent>();
  95. //Game.Scene.AddComponent<ServerFrameComponent>();
  96. Game.Scene.AddComponent<PathfindingComponent>();
  97. // Game.Scene.AddComponent<HttpComponent>();
  98. break;
  99. case AppType.Benchmark:
  100. Game.Scene.AddComponent<NetOuterComponent>();
  101. Game.Scene.AddComponent<BenchmarkComponent, string>(clientConfig.Address);
  102. break;
  103. case AppType.BenchmarkWebsocketServer:
  104. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  105. break;
  106. case AppType.BenchmarkWebsocketClient:
  107. Game.Scene.AddComponent<NetOuterComponent>();
  108. Game.Scene.AddComponent<WebSocketBenchmarkComponent, string>(clientConfig.Address);
  109. break;
  110. default:
  111. throw new Exception($"命令行参数没有设置正确的AppType: {startConfig.AppType}");
  112. }
  113. while (true)
  114. {
  115. try
  116. {
  117. Thread.Sleep(1);
  118. OneThreadSynchronizationContext.Instance.Update();
  119. Game.EventSystem.Update();
  120. }
  121. catch (Exception e)
  122. {
  123. Log.Error(e);
  124. }
  125. }
  126. }
  127. catch (Exception e)
  128. {
  129. Log.Error(e);
  130. }
  131. }
  132. }
  133. }