Program.cs 5.0 KB

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