Program.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 != 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. Log.Info("server start........................");
  29. Game.Scene.AddComponent<OpcodeTypeComponent>();
  30. Game.Scene.AddComponent<MessageDispatherComponent>();
  31. // 根据不同的AppType添加不同的组件
  32. OuterConfig outerConfig = startConfig.GetComponent<OuterConfig>();
  33. InnerConfig innerConfig = startConfig.GetComponent<InnerConfig>();
  34. ClientConfig clientConfig = startConfig.GetComponent<ClientConfig>();
  35. switch (startConfig.AppType)
  36. {
  37. case AppType.Manager:
  38. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  39. Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
  40. Game.Scene.AddComponent<AppManagerComponent>();
  41. break;
  42. case AppType.Realm:
  43. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  44. Game.Scene.AddComponent<ActorManagerComponent>();
  45. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  46. Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
  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<ActorManagerComponent>();
  54. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  55. Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
  56. Game.Scene.AddComponent<LocationProxyComponent>();
  57. Game.Scene.AddComponent<ActorProxyComponent>();
  58. Game.Scene.AddComponent<GateSessionKeyComponent>();
  59. break;
  60. case AppType.Location:
  61. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  62. Game.Scene.AddComponent<LocationComponent>();
  63. break;
  64. case AppType.Map:
  65. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  66. Game.Scene.AddComponent<ActorManagerComponent>();
  67. Game.Scene.AddComponent<UnitComponent>();
  68. Game.Scene.AddComponent<LocationProxyComponent>();
  69. Game.Scene.AddComponent<ActorProxyComponent>();
  70. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  71. break;
  72. case AppType.AllServer:
  73. Game.Scene.AddComponent<ActorProxyComponent>();
  74. Game.Scene.AddComponent<PlayerComponent>();
  75. Game.Scene.AddComponent<UnitComponent>();
  76. Game.Scene.AddComponent<DBComponent>();
  77. Game.Scene.AddComponent<DBProxyComponent>();
  78. Game.Scene.AddComponent<LocationComponent>();
  79. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  80. Game.Scene.AddComponent<ActorManagerComponent>();
  81. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  82. Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
  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. Log.Debug(Game.Scene.GetComponent<ConfigComponent>().Get<BuffConfig>(1).Name);
  89. break;
  90. case AppType.Benchmark:
  91. Game.Scene.AddComponent<NetOuterComponent>();
  92. Game.Scene.AddComponent<BenchmarkComponent, string>(clientConfig.Address);
  93. break;
  94. default:
  95. throw new Exception($"命令行参数没有设置正确的AppType: {startConfig.AppType}");
  96. }
  97. while (true)
  98. {
  99. try
  100. {
  101. Thread.Sleep(1);
  102. contex.Update();
  103. ObjectEvents.Instance.Update();
  104. }
  105. catch (Exception e)
  106. {
  107. Log.Error(e.ToString());
  108. }
  109. }
  110. }
  111. catch (Exception e)
  112. {
  113. Log.Error(e.ToString());
  114. }
  115. }
  116. }
  117. }