Init.cs 2.6 KB

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