Program.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Threading;
  5. using ETModel;
  6. using NLog;
  7. namespace App
  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. Options options = Game.Scene.AddComponent<OptionComponent, string[]>(args).Options;
  20. StartConfig startConfig = Game.Scene.AddComponent<StartConfigComponent, string, int>(options.Config, options.AppId).StartConfig;
  21. if (!options.AppType.Is(startConfig.AppType))
  22. {
  23. Log.Error("命令行参数apptype与配置不一致");
  24. return;
  25. }
  26. IdGenerater.AppId = options.AppId;
  27. LogManager.Configuration.Variables["appType"] = startConfig.AppType.ToString();
  28. LogManager.Configuration.Variables["appId"] = startConfig.AppId.ToString();
  29. LogManager.Configuration.Variables["appTypeFormat"] = $"{startConfig.AppType,-8}";
  30. LogManager.Configuration.Variables["appIdFormat"] = $"{startConfig.AppId:D3}";
  31. Log.Info($"server start........................ {startConfig.AppId} {startConfig.AppType}");
  32. Game.Scene.AddComponent<OpcodeTypeComponent>();
  33. Game.Scene.AddComponent<MessageDispatherComponent>();
  34. // 根据不同的AppType添加不同的组件
  35. OuterConfig outerConfig = startConfig.GetComponent<OuterConfig>();
  36. InnerConfig innerConfig = startConfig.GetComponent<InnerConfig>();
  37. ClientConfig clientConfig = startConfig.GetComponent<ClientConfig>();
  38. switch (startConfig.AppType)
  39. {
  40. case AppType.Manager:
  41. Game.Scene.AddComponent<AppManagerComponent>();
  42. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  43. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  44. break;
  45. case AppType.Realm:
  46. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  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<ActorMessageDispatherComponent>();
  55. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  56. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  57. Game.Scene.AddComponent<LocationProxyComponent>();
  58. Game.Scene.AddComponent<ActorMessageSenderComponent>();
  59. Game.Scene.AddComponent<ActorLocationSenderComponent>();
  60. Game.Scene.AddComponent<GateSessionKeyComponent>();
  61. break;
  62. case AppType.Location:
  63. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  64. Game.Scene.AddComponent<LocationComponent>();
  65. break;
  66. case AppType.Map:
  67. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  68. Game.Scene.AddComponent<UnitComponent>();
  69. Game.Scene.AddComponent<LocationProxyComponent>();
  70. Game.Scene.AddComponent<ActorMessageSenderComponent>();
  71. Game.Scene.AddComponent<ActorLocationSenderComponent>();
  72. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  73. Game.Scene.AddComponent<ServerFrameComponent>();
  74. break;
  75. case AppType.AllServer:
  76. Game.Scene.AddComponent<ActorMessageSenderComponent>();
  77. Game.Scene.AddComponent<ActorLocationSenderComponent>();
  78. Game.Scene.AddComponent<PlayerComponent>();
  79. Game.Scene.AddComponent<UnitComponent>();
  80. Game.Scene.AddComponent<DBComponent>();
  81. Game.Scene.AddComponent<DBProxyComponent>();
  82. Game.Scene.AddComponent<DBCacheComponent>();
  83. Game.Scene.AddComponent<LocationComponent>();
  84. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  85. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  86. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  87. Game.Scene.AddComponent<LocationProxyComponent>();
  88. Game.Scene.AddComponent<AppManagerComponent>();
  89. Game.Scene.AddComponent<RealmGateAddressComponent>();
  90. Game.Scene.AddComponent<GateSessionKeyComponent>();
  91. Game.Scene.AddComponent<ConfigComponent>();
  92. Game.Scene.AddComponent<ServerFrameComponent>();
  93. // Game.Scene.AddComponent<HttpComponent>();
  94. break;
  95. case AppType.Benchmark:
  96. Game.Scene.AddComponent<NetOuterComponent>();
  97. Game.Scene.AddComponent<BenchmarkComponent, string>(clientConfig.Address);
  98. break;
  99. case AppType.BenchmarkWebsocketServer:
  100. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  101. break;
  102. case AppType.BenchmarkWebsocketClient:
  103. Game.Scene.AddComponent<NetOuterComponent>();
  104. Game.Scene.AddComponent<WebSocketBenchmarkComponent, string>(clientConfig.Address);
  105. break;
  106. default:
  107. throw new Exception($"命令行参数没有设置正确的AppType: {startConfig.AppType}");
  108. }
  109. while (true)
  110. {
  111. try
  112. {
  113. Thread.Sleep(1);
  114. OneThreadSynchronizationContext.Instance.Update();
  115. Game.EventSystem.Update();
  116. }
  117. catch (Exception e)
  118. {
  119. Log.Error(e);
  120. }
  121. }
  122. }
  123. catch (Exception e)
  124. {
  125. Log.Error(e);
  126. }
  127. }
  128. }
  129. }