Init.cs 2.5 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 int Main(string[] args)
  10. {
  11. AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
  12. {
  13. Log.Error(e.ExceptionObject.ToString());
  14. };
  15. // 异步方法全部会回掉到主线程
  16. SynchronizationContext.SetSynchronizationContext(ThreadSynchronizationContext.Instance);
  17. try
  18. {
  19. // 异步方法全部会回掉到主线程
  20. SynchronizationContext.SetSynchronizationContext(ThreadSynchronizationContext.Instance);
  21. // 命令行参数
  22. Options options = null;
  23. Parser.Default.ParseArguments<Options>(args)
  24. .WithNotParsed(error => throw new Exception($"命令行格式错误! {error}"))
  25. .WithParsed(o => { options = o; });
  26. Game.AddSingleton(options);
  27. Game.AddSingleton<RandomGenerator>();
  28. Game.AddSingleton<TimeInfo>();
  29. Game.AddSingleton<Logger>().ILog = new NLogger(Options.Instance.AppType.ToString(), Options.Instance.Process, "../Config/NLog/NLog.config");
  30. Game.AddSingleton<ObjectPool>();
  31. Game.AddSingleton<IdGenerater>();
  32. Game.AddSingleton<EventSystem>();
  33. Game.AddSingleton<Root>();
  34. ETTask.ExceptionHandler += Log.Error;
  35. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof (Game).Assembly);
  36. EventSystem.Instance.Add(types);
  37. MongoRegister.Init();
  38. Log.Info($"server start........................ {Game.Scene.Id}");
  39. switch (Options.Instance.AppType)
  40. {
  41. case AppType.ExcelExporter:
  42. {
  43. Options.Instance.Console = 1;
  44. ExcelExporter.Export();
  45. return 0;
  46. }
  47. case AppType.Proto2CS:
  48. {
  49. Options.Instance.Console = 1;
  50. Proto2CS.Export();
  51. return 0;
  52. }
  53. }
  54. }
  55. catch (Exception e)
  56. {
  57. Log.Console(e.ToString());
  58. }
  59. return 1;
  60. }
  61. }
  62. }