Program.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.ToString();
  26. LogManager.Configuration.Variables["appId"] = startConfig.AppId.ToString();
  27. LogManager.Configuration.Variables["appTypeFormat"] = $"{startConfig.AppType,-8}";
  28. LogManager.Configuration.Variables["appIdFormat"] = $"{startConfig.AppId:D3}";
  29. Log.Info($"server start........................ {startConfig.AppId} {startConfig.AppType}");
  30. Game.Scene.AddComponent<OpcodeTypeComponent>();
  31. Game.Scene.AddComponent<MessageDispatherComponent>();
  32. // 根据不同的AppType添加不同的组件
  33. OuterConfig outerConfig = startConfig.GetComponent<OuterConfig>();
  34. InnerConfig innerConfig = startConfig.GetComponent<InnerConfig>();
  35. ClientConfig clientConfig = startConfig.GetComponent<ClientConfig>();
  36. switch (startConfig.AppType)
  37. {
  38. case AppType.Manager:
  39. Game.Scene.AddComponent<AppManagerComponent>();
  40. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  41. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  42. break;
  43. case AppType.Realm:
  44. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  45. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  46. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  47. Game.Scene.AddComponent<LocationProxyComponent>();
  48. Game.Scene.AddComponent<RealmGateAddressComponent>();
  49. break;
  50. case AppType.Gate:
  51. Game.Scene.AddComponent<PlayerComponent>();
  52. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  53. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  54. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  55. Game.Scene.AddComponent<LocationProxyComponent>();
  56. Game.Scene.AddComponent<ActorMessageSenderComponent>();
  57. Game.Scene.AddComponent<ActorLocationSenderComponent>();
  58. Game.Scene.AddComponent<GateSessionKeyComponent>();
  59. break;
  60. case AppType.Location:
  61. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  62. Game.Scene.AddComponent<LocationComponent>();
  63. break;
  64. case AppType.Map:
  65. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  66. Game.Scene.AddComponent<UnitComponent>();
  67. Game.Scene.AddComponent<LocationProxyComponent>();
  68. Game.Scene.AddComponent<ActorMessageSenderComponent>();
  69. Game.Scene.AddComponent<ActorLocationSenderComponent>();
  70. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  71. Game.Scene.AddComponent<PathfindingComponent>();
  72. break;
  73. case AppType.AllServer:
  74. Game.Scene.AddComponent<ActorMessageSenderComponent>();
  75. Game.Scene.AddComponent<ActorLocationSenderComponent>();
  76. Game.Scene.AddComponent<PlayerComponent>();
  77. Game.Scene.AddComponent<UnitComponent>();
  78. //Game.Scene.AddComponent<DBComponent>();
  79. //Game.Scene.AddComponent<DBProxyComponent>();
  80. //Game.Scene.AddComponent<DBCacheComponent>();
  81. Game.Scene.AddComponent<LocationComponent>();
  82. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  83. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  84. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  85. Game.Scene.AddComponent<LocationProxyComponent>();
  86. Game.Scene.AddComponent<AppManagerComponent>();
  87. Game.Scene.AddComponent<RealmGateAddressComponent>();
  88. Game.Scene.AddComponent<GateSessionKeyComponent>();
  89. Game.Scene.AddComponent<ConfigComponent>();
  90. Game.Scene.AddComponent<PathfindingComponent>();
  91. // Game.Scene.AddComponent<HttpComponent>();
  92. break;
  93. case AppType.Benchmark:
  94. Game.Scene.AddComponent<NetOuterComponent>();
  95. Game.Scene.AddComponent<BenchmarkComponent, string>(clientConfig.Address);
  96. break;
  97. case AppType.BenchmarkWebsocketServer:
  98. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  99. break;
  100. case AppType.BenchmarkWebsocketClient:
  101. Game.Scene.AddComponent<NetOuterComponent>();
  102. Game.Scene.AddComponent<WebSocketBenchmarkComponent, string>(clientConfig.Address);
  103. break;
  104. default:
  105. throw new Exception($"命令行参数没有设置正确的AppType: {startConfig.AppType}");
  106. }
  107. while (true)
  108. {
  109. try
  110. {
  111. Thread.Sleep(1);
  112. OneThreadSynchronizationContext.Instance.Update();
  113. Game.EventSystem.Update();
  114. }
  115. catch (Exception e)
  116. {
  117. Log.Error(e);
  118. }
  119. }
  120. }
  121. catch (Exception e)
  122. {
  123. Log.Error(e);
  124. }
  125. }
  126. }
  127. }