Program.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // 这两个组件是处理actor消息使用的
  44. Game.Scene.AddComponent<MailboxDispatcherComponent>();
  45. Game.Scene.AddComponent<ActorMessageDispatcherComponent>();
  46. // manager server组件,用来管理其它进程使用
  47. Game.Scene.AddComponent<NumericWatcherComponent>();
  48. Game.Scene.AddComponent<ConsoleComponent>();
  49. OuterConfig outerConfig = startConfig.GetComponent<OuterConfig>();
  50. if (outerConfig != null)
  51. {
  52. // 外网消息组件
  53. Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  54. }
  55. InnerConfig innerConfig = startConfig.GetComponent<InnerConfig>();
  56. if (innerConfig != null)
  57. {
  58. // 内网消息组件
  59. Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  60. }
  61. DBConfig dbConfig = startConfig.GetComponent<DBConfig>();
  62. if (dbConfig != null)
  63. {
  64. Game.Scene.AddComponent<DBComponent, DBConfig>(dbConfig);
  65. }
  66. // 先加这里,后面删掉
  67. Game.EventSystem.Run(EventIdType.AfterScenesAdd);
  68. while (true)
  69. {
  70. try
  71. {
  72. Thread.Sleep(1);
  73. OneThreadSynchronizationContext.Instance.Update();
  74. Game.EventSystem.Update();
  75. }
  76. catch (Exception e)
  77. {
  78. Log.Error(e);
  79. }
  80. }
  81. }
  82. catch (Exception e)
  83. {
  84. Log.Error(e);
  85. }
  86. }
  87. }
  88. }