Program.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Threading;
  5. using CommandLine;
  6. using NLog;
  7. namespace ETModel
  8. {
  9. internal static class Program
  10. {
  11. private static void Main(string[] args)
  12. {
  13. // 异步方法全部会回掉到主线程
  14. SynchronizationContext.SetSynchronizationContext(OneThreadSynchronizationContext.Instance);
  15. try
  16. {
  17. Game.EventSystem.Add(DLLType.Model, typeof(Game).Assembly);
  18. Game.EventSystem.Add(DLLType.Hotfix, DllHelper.GetHotfixAssembly());
  19. MongoHelper.Init();
  20. // 命令行参数
  21. Parser.Default.ParseArguments<Options>(args)
  22. .WithNotParsed(error => throw new Exception($"命令行格式错误!"))
  23. .WithParsed(o => { Game.Options = o; });
  24. IdGenerater.AppId = Game.Options.Id;
  25. // 启动配置
  26. StartConfig allConfig = MongoHelper.FromJson<StartConfig>(File.ReadAllText(Path.Combine("../Config/StartConfig/", Game.Options.Config)));
  27. StartConfig startConfig = allConfig.Get(Game.Options.Id);
  28. Game.Scene = EntityFactory.CreateScene(0, "Process", SceneType.Process);
  29. LogManager.Configuration.Variables["appIdFormat"] = $"{Game.Scene.Id:0000}";
  30. Game.Scene.AddComponent<StartConfigComponent, StartConfig, long>(allConfig, startConfig.Id);
  31. Log.Info($"server start........................ {Game.Scene.Id}");
  32. Game.Scene.AddComponent<TimerComponent>();
  33. Game.Scene.AddComponent<OpcodeTypeComponent>();
  34. Game.Scene.AddComponent<MessageDispatcherComponent>();
  35. Game.Scene.AddComponent<ConfigComponent>();
  36. Game.Scene.AddComponent<CoroutineLockComponent>();
  37. // 发送普通actor消息
  38. Game.Scene.AddComponent<ActorMessageSenderComponent>();
  39. // 发送location actor消息
  40. Game.Scene.AddComponent<ActorLocationSenderComponent>();
  41. // 访问location server的组件
  42. Game.Scene.AddComponent<LocationProxyComponent>();
  43. Game.Scene.AddComponent<ActorMessageDispatcherComponent>();
  44. // 数值订阅组件
  45. Game.Scene.AddComponent<NumericWatcherComponent>();
  46. // 控制台组件
  47. Game.Scene.AddComponent<ConsoleComponent>();
  48. OuterConfig outerConfig = startConfig.GetComponent<OuterConfig>();
  49. if (outerConfig != null)
  50. {
  51. // 外网消息组件
  52. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  53. }
  54. InnerConfig innerConfig = startConfig.GetComponent<InnerConfig>();
  55. if (innerConfig != null)
  56. {
  57. // 内网消息组件
  58. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  59. }
  60. DBConfig dbConfig = startConfig.GetComponent<DBConfig>();
  61. if (dbConfig != null)
  62. {
  63. Game.Scene.AddComponent<DBComponent, DBConfig>(dbConfig);
  64. }
  65. // 先加这里,后面删掉
  66. Game.EventSystem.Run(EventIdType.AfterScenesAdd);
  67. while (true)
  68. {
  69. try
  70. {
  71. Thread.Sleep(1);
  72. OneThreadSynchronizationContext.Instance.Update();
  73. Game.EventSystem.Update();
  74. }
  75. catch (Exception e)
  76. {
  77. Log.Error(e);
  78. }
  79. }
  80. }
  81. catch (Exception e)
  82. {
  83. Log.Error(e);
  84. }
  85. }
  86. }
  87. }