Program.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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<GateSessionKeyComponent>();
  55. break;
  56. case AppType.Location:
  57. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  58. Game.Scene.AddComponent<LocationComponent>();
  59. break;
  60. case AppType.Map:
  61. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  62. Game.Scene.AddComponent<LocationComponent>();
  63. Game.Scene.AddComponent<UnitComponent>();
  64. Game.Scene.AddComponent<LocationProxyComponent>();
  65. Game.Scene.AddComponent<ActorComponent>();
  66. break;
  67. case AppType.AllServer:
  68. Game.Scene.AddComponent<ActorProxyComponent>();
  69. Game.Scene.AddComponent<PlayerComponent>();
  70. Game.Scene.AddComponent<UnitComponent>();
  71. Game.Scene.AddComponent<DBComponent>();
  72. Game.Scene.AddComponent<DBProxyComponent>();
  73. Game.Scene.AddComponent<LocationComponent>();
  74. Game.Scene.AddComponent<ActorMessageDispatherComponent, AppType>(AppType.AllServer);
  75. Game.Scene.AddComponent<ActorManagerComponent>();
  76. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  77. Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
  78. Game.Scene.AddComponent<LocationProxyComponent>();
  79. Game.Scene.AddComponent<ActorComponent>();
  80. Game.Scene.AddComponent<AppManagerComponent>();
  81. Game.Scene.AddComponent<RealmGateAddressComponent>();
  82. Game.Scene.AddComponent<GateSessionKeyComponent>();
  83. break;
  84. case AppType.Benchmark:
  85. Game.Scene.AddComponent<NetOuterComponent>();
  86. Game.Scene.AddComponent<BenchmarkComponent, string>(clientConfig.Address);
  87. break;
  88. default:
  89. throw new Exception($"命令行参数没有设置正确的AppType: {startConfig.AppType}");
  90. }
  91. while (true)
  92. {
  93. try
  94. {
  95. Thread.Sleep(1);
  96. contex.Update();
  97. ObjectEvents.Instance.Update();
  98. }
  99. catch (Exception e)
  100. {
  101. Log.Error(e.ToString());
  102. }
  103. }
  104. }
  105. catch (Exception e)
  106. {
  107. Log.Error(e.ToString());
  108. }
  109. }
  110. }
  111. }