Init.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Threading;
  5. using CommandLine;
  6. namespace ET
  7. {
  8. public class ToolScene: IScene
  9. {
  10. public Fiber Fiber { get; set; }
  11. public int SceneType
  12. {
  13. get;
  14. set;
  15. }
  16. public ToolScene()
  17. {
  18. }
  19. public ToolScene(int sceneType)
  20. {
  21. this.SceneType = sceneType;
  22. }
  23. }
  24. public struct ToolEvent
  25. {
  26. }
  27. internal static class Init
  28. {
  29. private static int Main(string[] args)
  30. {
  31. AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
  32. {
  33. Log.Error(e.ExceptionObject.ToString());
  34. };
  35. try
  36. {
  37. // 命令行参数
  38. CommandLine.Parser.Default.ParseArguments<Options>(args)
  39. .WithNotParsed(error => throw new Exception($"命令行格式错误! {error}"))
  40. .WithParsed((o)=>World.Instance.AddSingleton(o));
  41. World.Instance.AddSingleton<Logger>().Log = new NLogger(Options.Instance.SceneName, Options.Instance.Process, 0);
  42. World.Instance.AddSingleton<CodeTypes, Assembly[]>([typeof (Init).Assembly]);
  43. World.Instance.AddSingleton<EventSystem>();
  44. World.Instance.AddSingleton<SceneTypeSingleton, Type>(typeof(SceneType));
  45. // 强制调用一下mongo,避免mongo库被裁剪
  46. MongoHelper.ToJson(1);
  47. ETTask.ExceptionHandler += Log.Error;
  48. int sceneType = SceneTypeSingleton.Instance.GetSceneType(Options.Instance.SceneName);
  49. ToolScene scene = new(sceneType);
  50. EventSystem.Instance.Publish(scene, new ToolEvent());
  51. Log.Console($"{Options.Instance.SceneName} run finish!");
  52. }
  53. catch (Exception e)
  54. {
  55. Log.Console(e.ToString());
  56. }
  57. return 1;
  58. }
  59. }
  60. }