Init.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using CommandLine;
  5. namespace ET.Server
  6. {
  7. internal static class Init
  8. {
  9. private static Process process;
  10. private static int Main(string[] args)
  11. {
  12. AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
  13. {
  14. Log.Error(e.ExceptionObject.ToString());
  15. };
  16. try
  17. {
  18. // 命令行参数
  19. Parser.Default.ParseArguments<Options>(args)
  20. .WithNotParsed(error => throw new Exception($"命令行格式错误! {error}"))
  21. .WithParsed(Game.Instance.AddSingleton);
  22. process = Game.Instance.Create(false);
  23. // 异步方法全部会回掉到主线程
  24. process.AddSingleton<MainThreadSynchronizationContext>();
  25. process.AddSingleton<TimeInfo>();
  26. process.AddSingleton<Logger>().ILog = new NLogger(Options.Instance.AppType.ToString(), Options.Instance.Process, "../Config/NLog/NLog.config");
  27. process.AddSingleton<ObjectPool>();
  28. process.AddSingleton<IdGenerater>();
  29. ETTask.ExceptionHandler += Log.Error;
  30. process.AddSingleton<EventSystem>();
  31. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof (Process).Assembly);
  32. EventSystem.Instance.Add(types);
  33. process.AddSingleton<EntitySystemSingleton>();
  34. process.AddSingleton<Root>();
  35. MongoHelper.Register();
  36. Log.Info($"server start........................ {Root.Instance.Scene.Id}");
  37. switch (Options.Instance.AppType)
  38. {
  39. case AppType.ExcelExporter:
  40. {
  41. Options.Instance.Console = 1;
  42. ExcelExporter.Export();
  43. return 0;
  44. }
  45. case AppType.Proto2CS:
  46. {
  47. Options.Instance.Console = 1;
  48. Proto2CS.Export();
  49. return 0;
  50. }
  51. }
  52. }
  53. catch (Exception e)
  54. {
  55. Log.Console(e.ToString());
  56. }
  57. return 1;
  58. }
  59. }
  60. }