Init.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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<TimeInfo>();
  28. Game.AddSingleton<Logger>().ILog = new NLogger(Options.Instance.AppType.ToString(), Options.Instance.Process, "../Config/NLog/NLog.config");
  29. Game.AddSingleton<ObjectPool>();
  30. Game.AddSingleton<IdGenerater>();
  31. Game.AddSingleton<EventSystem>();
  32. Game.AddSingleton<Root>();
  33. ETTask.ExceptionHandler += Log.Error;
  34. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof (Game).Assembly);
  35. EventSystem.Instance.Add(types);
  36. MongoRegister.Init();
  37. Log.Info($"server start........................ {Game.Scene.Id}");
  38. switch (Options.Instance.AppType)
  39. {
  40. case AppType.ExcelExporter:
  41. {
  42. Options.Instance.Console = 1;
  43. ExcelExporter.Export();
  44. return 0;
  45. }
  46. case AppType.Proto2CS:
  47. {
  48. Options.Instance.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. }