Program.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.Register("Model", typeof(Game).Assembly);
  17. ObjectEvents.Instance.Register("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. IdGenerater.AppId = options.AppId;
  21. LogManager.Configuration.Variables["appType"] = startConfig.AppType.ToString();
  22. LogManager.Configuration.Variables["appId"] = startConfig.AppId.ToString();
  23. Log.Info("server start........................");
  24. Game.Scene.AddComponent<OpcodeTypeComponent>();
  25. Game.Scene.AddComponent<MessageDispatherComponent, AppType>(startConfig.AppType);
  26. // 根据不同的AppType添加不同的组件
  27. OuterConfig outerConfig = startConfig.GetComponent<OuterConfig>();
  28. InnerConfig innerConfig = startConfig.GetComponent<InnerConfig>();
  29. ClientConfig clientConfig = startConfig.GetComponent<ClientConfig>();
  30. switch (startConfig.AppType)
  31. {
  32. case AppType.Manager:
  33. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  34. Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
  35. Game.Scene.AddComponent<AppManagerComponent>();
  36. break;
  37. case AppType.Realm:
  38. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  39. Game.Scene.AddComponent<ActorManagerComponent>();
  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<LocationProxyComponent>();
  43. Game.Scene.AddComponent<ActorComponent>();
  44. Game.Scene.AddComponent<RealmGateAddressComponent>();
  45. break;
  46. case AppType.Gate:
  47. Game.Scene.AddComponent<PlayerComponent>();
  48. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  49. Game.Scene.AddComponent<ActorManagerComponent>();
  50. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  51. Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
  52. Game.Scene.AddComponent<LocationProxyComponent>();
  53. Game.Scene.AddComponent<ActorComponent>();
  54. Game.Scene.AddComponent<ActorProxyComponent>();
  55. Game.Scene.AddComponent<GateSessionKeyComponent>();
  56. break;
  57. case AppType.Location:
  58. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  59. Game.Scene.AddComponent<LocationComponent>();
  60. break;
  61. case AppType.Map:
  62. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  63. Game.Scene.AddComponent<ActorManagerComponent>();
  64. Game.Scene.AddComponent<UnitComponent>();
  65. Game.Scene.AddComponent<LocationProxyComponent>();
  66. Game.Scene.AddComponent<ActorComponent>();
  67. Game.Scene.AddComponent<ActorProxyComponent>();
  68. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  69. break;
  70. case AppType.AllServer:
  71. Game.Scene.AddComponent<ActorProxyComponent>();
  72. Game.Scene.AddComponent<PlayerComponent>();
  73. Game.Scene.AddComponent<UnitComponent>();
  74. Game.Scene.AddComponent<DBComponent>();
  75. Game.Scene.AddComponent<DBProxyComponent>();
  76. Game.Scene.AddComponent<LocationComponent>();
  77. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  78. Game.Scene.AddComponent<ActorManagerComponent>();
  79. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  80. Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
  81. Game.Scene.AddComponent<LocationProxyComponent>();
  82. Game.Scene.AddComponent<ActorComponent>();
  83. Game.Scene.AddComponent<AppManagerComponent>();
  84. Game.Scene.AddComponent<RealmGateAddressComponent>();
  85. Game.Scene.AddComponent<GateSessionKeyComponent>();
  86. break;
  87. case AppType.Benchmark:
  88. Game.Scene.AddComponent<NetOuterComponent>();
  89. Game.Scene.AddComponent<BenchmarkComponent, string>(clientConfig.Address);
  90. break;
  91. default:
  92. throw new Exception($"命令行参数没有设置正确的AppType: {startConfig.AppType}");
  93. }
  94. while (true)
  95. {
  96. try
  97. {
  98. Thread.Sleep(1);
  99. contex.Update();
  100. ObjectEvents.Instance.Update();
  101. }
  102. catch (Exception e)
  103. {
  104. Log.Error(e.ToString());
  105. }
  106. }
  107. }
  108. catch (Exception e)
  109. {
  110. Log.Error(e.ToString());
  111. }
  112. }
  113. }
  114. }