Program.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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<UnitComponent>();
  39. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  40. Game.Scene.AddComponent<ActorManagerComponent>();
  41. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  42. Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
  43. Game.Scene.AddComponent<LocationProxyComponent>();
  44. Game.Scene.AddComponent<ActorComponent>();
  45. Game.Scene.AddComponent<RealmGateAddressComponent>();
  46. break;
  47. case AppType.Gate:
  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.AllServer:
  61. Game.Scene.AddComponent<GamerComponent>();
  62. Game.Scene.AddComponent<UnitComponent>();
  63. Game.Scene.AddComponent<LocationComponent>();
  64. Game.Scene.AddComponent<ActorMessageDispatherComponent>();
  65. Game.Scene.AddComponent<ActorManagerComponent>();
  66. Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
  67. Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
  68. Game.Scene.AddComponent<LocationProxyComponent>();
  69. Game.Scene.AddComponent<ActorComponent>();
  70. Game.Scene.AddComponent<AppManagerComponent>();
  71. Game.Scene.AddComponent<RealmGateAddressComponent>();
  72. Game.Scene.AddComponent<GateSessionKeyComponent>();
  73. break;
  74. case AppType.Benchmark:
  75. Game.Scene.AddComponent<NetOuterComponent>();
  76. Game.Scene.AddComponent<BenchmarkComponent, string>(clientConfig.Address);
  77. break;
  78. default:
  79. throw new Exception($"命令行参数没有设置正确的AppType: {startConfig.AppType}");
  80. }
  81. while (true)
  82. {
  83. try
  84. {
  85. Thread.Sleep(1);
  86. contex.Update();
  87. ObjectEvents.Instance.Update();
  88. }
  89. catch (Exception e)
  90. {
  91. Log.Error(e.ToString());
  92. }
  93. }
  94. }
  95. catch (Exception e)
  96. {
  97. Log.Error(e.ToString());
  98. }
  99. }
  100. }
  101. }