Program.cs 5.3 KB

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