Program.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Threading;
  3. using Model;
  4. using NLog;
  5. namespace App
  6. {
  7. internal static class Program
  8. {
  9. private static void Main(string[] args)
  10. {
  11. // 异步方法全部会回掉到主线程
  12. OneThreadSynchronizationContext contex = new OneThreadSynchronizationContext();
  13. SynchronizationContext.SetSynchronizationContext(contex);
  14. try
  15. {
  16. ObjectEvents.Instance.Add("Model", typeof(Game).Assembly);
  17. ObjectEvents.Instance.Add("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........................");
  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, string, int>(innerConfig.Host, innerConfig.Port);
  41. Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
  42. Game.Scene.AddComponent<AppManagerComponent>();
  43. break;
  44. case AppType.Realm:
  45. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  46. Game.Scene.AddComponent<ActorManagerComponent>();
  47. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  48. Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
  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<ActorManagerComponent>();
  56. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  57. Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
  58. Game.Scene.AddComponent<LocationProxyComponent>();
  59. Game.Scene.AddComponent<ActorProxyComponent>();
  60. Game.Scene.AddComponent<GateSessionKeyComponent>();
  61. break;
  62. case AppType.Location:
  63. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  64. Game.Scene.AddComponent<LocationComponent>();
  65. break;
  66. case AppType.Map:
  67. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  68. Game.Scene.AddComponent<ActorManagerComponent>();
  69. Game.Scene.AddComponent<UnitComponent>();
  70. Game.Scene.AddComponent<LocationProxyComponent>();
  71. Game.Scene.AddComponent<ActorProxyComponent>();
  72. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  73. Game.Scene.AddComponent<ServerFrameComponent>();
  74. break;
  75. case AppType.AllServer:
  76. Game.Scene.AddComponent<ActorProxyComponent>();
  77. Game.Scene.AddComponent<PlayerComponent>();
  78. Game.Scene.AddComponent<UnitComponent>();
  79. Game.Scene.AddComponent<DBComponent>();
  80. Game.Scene.AddComponent<DBProxyComponent>();
  81. Game.Scene.AddComponent<LocationComponent>();
  82. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  83. Game.Scene.AddComponent<ActorManagerComponent>();
  84. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  85. Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
  86. Game.Scene.AddComponent<LocationProxyComponent>();
  87. Game.Scene.AddComponent<AppManagerComponent>();
  88. Game.Scene.AddComponent<RealmGateAddressComponent>();
  89. Game.Scene.AddComponent<GateSessionKeyComponent>();
  90. Game.Scene.AddComponent<ConfigComponent>();
  91. Game.Scene.AddComponent<ServerFrameComponent>();
  92. break;
  93. case AppType.Benchmark:
  94. Game.Scene.AddComponent<NetOuterComponent>();
  95. Game.Scene.AddComponent<BenchmarkComponent, string>(clientConfig.Address);
  96. break;
  97. default:
  98. throw new Exception($"命令行参数没有设置正确的AppType: {startConfig.AppType}");
  99. }
  100. while (true)
  101. {
  102. try
  103. {
  104. Thread.Sleep(1);
  105. contex.Update();
  106. ObjectEvents.Instance.Update();
  107. }
  108. catch (Exception e)
  109. {
  110. Log.Error(e.ToString());
  111. }
  112. }
  113. }
  114. catch (Exception e)
  115. {
  116. Log.Error(e.ToString());
  117. }
  118. }
  119. }
  120. }