Program.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. MongoHelper.Init();
  15. try
  16. {
  17. ObjectEvents.Instance.Add("Model", typeof(Game).Assembly);
  18. ObjectEvents.Instance.Add("Hotfix", DllHelper.GetHotfixAssembly());
  19. Options options = Game.Scene.AddComponent<OptionComponent, string[]>(args).Options;
  20. StartConfig startConfig = Game.Scene.AddComponent<StartConfigComponent, string, int>(options.Config, options.AppId).StartConfig;
  21. if (!options.AppType.Is(startConfig.AppType))
  22. {
  23. Log.Error("命令行参数apptype与配置不一致");
  24. return;
  25. }
  26. IdGenerater.AppId = options.AppId;
  27. LogManager.Configuration.Variables["appType"] = startConfig.AppType.ToString();
  28. LogManager.Configuration.Variables["appId"] = startConfig.AppId.ToString();
  29. LogManager.Configuration.Variables["appTypeFormat"] = $"{startConfig.AppType,-8}";
  30. LogManager.Configuration.Variables["appIdFormat"] = $"{startConfig.AppId:D3}";
  31. Log.Info("server start........................");
  32. Game.Scene.AddComponent<OpcodeTypeComponent>();
  33. Game.Scene.AddComponent<MessageDispatherComponent>();
  34. Unit unit = new Unit();
  35. NumericComponent numericComponent = unit.AddComponent<NumericComponent>();
  36. numericComponent.Set(NumericType.Speed, 100);
  37. Log.Debug(MongoHelper.ToJson(unit));
  38. Unit unit2 = MongoHelper.FromJson<Unit>(MongoHelper.ToJson(unit));
  39. Log.Debug(MongoHelper.ToJson(unit2));
  40. // 根据不同的AppType添加不同的组件
  41. OuterConfig outerConfig = startConfig.GetComponent<OuterConfig>();
  42. InnerConfig innerConfig = startConfig.GetComponent<InnerConfig>();
  43. ClientConfig clientConfig = startConfig.GetComponent<ClientConfig>();
  44. switch (startConfig.AppType)
  45. {
  46. case AppType.Manager:
  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<AppManagerComponent>();
  50. break;
  51. case AppType.Realm:
  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<RealmGateAddressComponent>();
  58. break;
  59. case AppType.Gate:
  60. Game.Scene.AddComponent<PlayerComponent>();
  61. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  62. Game.Scene.AddComponent<ActorManagerComponent>();
  63. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  64. Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
  65. Game.Scene.AddComponent<LocationProxyComponent>();
  66. Game.Scene.AddComponent<ActorProxyComponent>();
  67. Game.Scene.AddComponent<GateSessionKeyComponent>();
  68. break;
  69. case AppType.Location:
  70. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  71. Game.Scene.AddComponent<LocationComponent>();
  72. break;
  73. case AppType.Map:
  74. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  75. Game.Scene.AddComponent<ActorManagerComponent>();
  76. Game.Scene.AddComponent<UnitComponent>();
  77. Game.Scene.AddComponent<LocationProxyComponent>();
  78. Game.Scene.AddComponent<ActorProxyComponent>();
  79. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  80. Game.Scene.AddComponent<ServerFrameComponent>();
  81. break;
  82. case AppType.AllServer:
  83. Game.Scene.AddComponent<ActorProxyComponent>();
  84. Game.Scene.AddComponent<PlayerComponent>();
  85. Game.Scene.AddComponent<UnitComponent>();
  86. Game.Scene.AddComponent<DBComponent>();
  87. Game.Scene.AddComponent<DBProxyComponent>();
  88. Game.Scene.AddComponent<LocationComponent>();
  89. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  90. Game.Scene.AddComponent<ActorManagerComponent>();
  91. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  92. Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
  93. Game.Scene.AddComponent<LocationProxyComponent>();
  94. Game.Scene.AddComponent<AppManagerComponent>();
  95. Game.Scene.AddComponent<RealmGateAddressComponent>();
  96. Game.Scene.AddComponent<GateSessionKeyComponent>();
  97. Game.Scene.AddComponent<ConfigComponent>();
  98. Game.Scene.AddComponent<ServerFrameComponent>();
  99. break;
  100. case AppType.Benchmark:
  101. Game.Scene.AddComponent<NetOuterComponent>();
  102. Game.Scene.AddComponent<BenchmarkComponent, 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. contex.Update();
  113. ObjectEvents.Instance.Update();
  114. }
  115. catch (Exception e)
  116. {
  117. Log.Error(e.ToString());
  118. }
  119. }
  120. }
  121. catch (Exception e)
  122. {
  123. Log.Error(e.ToString());
  124. }
  125. }
  126. }
  127. }