ConsoleManager.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.ComponentModel;
  3. using System.IO;
  4. using System.Text;
  5. using BepInEx.Configuration;
  6. using BepInEx.Unix;
  7. using MonoMod.Utils;
  8. namespace BepInEx
  9. {
  10. public static class ConsoleManager
  11. {
  12. private const uint SHIFT_JIS_CP = 932;
  13. internal static IConsoleDriver Driver { get; set; }
  14. /// <summary>
  15. /// True if an external console has been started, false otherwise.
  16. /// </summary>
  17. public static bool ConsoleActive => Driver?.ConsoleActive ?? false;
  18. /// <summary>
  19. /// The stream that writes to the standard out stream of the process. Should never be null.
  20. /// </summary>
  21. public static TextWriter StandardOutStream => Driver?.StandardOut;
  22. /// <summary>
  23. /// The stream that writes to an external console. Null if no such console exists
  24. /// </summary>
  25. public static TextWriter ConsoleStream => Driver?.ConsoleOut;
  26. public static void Initialize(bool alreadyActive)
  27. {
  28. if (PlatformHelper.Is(Platform.MacOS) || PlatformHelper.Is(Platform.Linux))
  29. {
  30. Driver = new LinuxConsoleDriver();
  31. }
  32. else if (PlatformHelper.Is(Platform.Windows))
  33. {
  34. Driver = new WindowsConsoleDriver();
  35. }
  36. else
  37. {
  38. throw new PlatformNotSupportedException("Was unable to determine console driver for platform " + PlatformHelper.Current);
  39. }
  40. Driver.Initialize(alreadyActive);
  41. }
  42. private static void DriverCheck()
  43. {
  44. if (Driver == null)
  45. throw new InvalidOperationException("Driver has not been initialized");
  46. }
  47. public static void CreateConsole()
  48. {
  49. if (ConsoleActive)
  50. return;
  51. DriverCheck();
  52. // Apparently some versions of Mono throw a "Encoding name 'xxx' not supported"
  53. // if you use Encoding.GetEncoding
  54. // That's why we use of codepages directly and handle then in console drivers separately
  55. var codepage = ConfigConsoleShiftJis.Value ? SHIFT_JIS_CP: (uint)Encoding.UTF8.CodePage;
  56. Driver.CreateConsole(codepage);
  57. Console.SetOut(ConsoleStream);
  58. }
  59. public static void DetachConsole()
  60. {
  61. if (!ConsoleActive)
  62. return;
  63. DriverCheck();
  64. Driver.DetachConsole();
  65. }
  66. public static void SetConsoleTitle(string title)
  67. {
  68. DriverCheck();
  69. Driver.SetConsoleTitle(title);
  70. }
  71. public static void SetConsoleColor(ConsoleColor color)
  72. {
  73. DriverCheck();
  74. Driver.SetConsoleColor(color);
  75. }
  76. public static readonly ConfigEntry<bool> ConfigConsoleEnabled = ConfigFile.CoreConfig.Bind(
  77. "Logging.Console", "Enabled",
  78. false,
  79. "Enables showing a console for log output.");
  80. public static readonly ConfigEntry<bool> ConfigConsoleShiftJis = ConfigFile.CoreConfig.Bind(
  81. "Logging.Console", "ShiftJisEncoding",
  82. false,
  83. "If true, console is set to the Shift-JIS encoding, otherwise UTF-8 encoding.");
  84. public static readonly ConfigEntry<ConsoleOutRedirectType> ConfigConsoleOutRedirectType = ConfigFile.CoreConfig.Bind(
  85. "Logging.Console", "StandardOutType",
  86. ConsoleOutRedirectType.Auto,
  87. new StringBuilder()
  88. .AppendLine("Hints console manager on what handle to assign as StandardOut. Possible values:")
  89. .AppendLine("Auto - lets BepInEx decide how to redirect console output")
  90. .AppendLine("ConsoleOut - prefer redirecting to console output; if possible, closes original standard output")
  91. .AppendLine("StandardOut - prefer redirecting to standard output; if possible, closes console out")
  92. .ToString()
  93. );
  94. public enum ConsoleOutRedirectType
  95. {
  96. [Description("Auto")] Auto = 0,
  97. [Description("Console Out")] ConsoleOut,
  98. [Description("Standard Out")] StandardOut,
  99. }
  100. }
  101. }