ConsoleManager.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using BepInEx.Configuration;
  5. using BepInEx.Unix;
  6. using MonoMod.Utils;
  7. namespace BepInEx
  8. {
  9. public static class ConsoleManager
  10. {
  11. private const uint SHIFT_JIS_CP = 932;
  12. internal static IConsoleDriver Driver { get; set; }
  13. /// <summary>
  14. /// True if an external console has been started, false otherwise.
  15. /// </summary>
  16. public static bool ConsoleActive => Driver?.ConsoleActive ?? false;
  17. /// <summary>
  18. /// The stream that writes to the standard out stream of the process. Should never be null.
  19. /// </summary>
  20. public static TextWriter StandardOutStream => Driver?.StandardOut;
  21. /// <summary>
  22. /// The stream that writes to an external console. Null if no such console exists
  23. /// </summary>
  24. public static TextWriter ConsoleStream => Driver?.ConsoleOut;
  25. public static void Initialize(bool alreadyActive)
  26. {
  27. switch (Utility.CurrentOs)
  28. {
  29. case Platform.MacOS:
  30. case Platform.Linux:
  31. {
  32. Driver = new LinuxConsoleDriver();
  33. break;
  34. }
  35. case Platform.Windows:
  36. {
  37. Driver = new WindowsConsoleDriver();
  38. break;
  39. }
  40. }
  41. Driver.Initialize(alreadyActive);
  42. }
  43. private static void DriverCheck()
  44. {
  45. if (Driver == null)
  46. throw new InvalidOperationException("Driver has not been initialized");
  47. }
  48. public static void CreateConsole()
  49. {
  50. if (ConsoleActive)
  51. return;
  52. DriverCheck();
  53. Driver.CreateConsole();
  54. SetConsoleStreams();
  55. }
  56. public static void DetachConsole()
  57. {
  58. if (!ConsoleActive)
  59. return;
  60. DriverCheck();
  61. Driver.DetachConsole();
  62. SetConsoleStreams();
  63. }
  64. public static void SetConsoleEncoding()
  65. {
  66. // Apparently some versions of Mono throw a "Encoding name 'xxx' not supported"
  67. // if you use Encoding.GetEncoding
  68. // That's why we use of codepages directly and handle then in console drivers separately
  69. var codepage = ConfigConsoleShiftJis.Value ? SHIFT_JIS_CP: (uint)Encoding.UTF8.CodePage;
  70. SetConsoleEncoding(codepage);
  71. }
  72. public static void SetConsoleEncoding(uint codepage)
  73. {
  74. if (!ConsoleActive)
  75. throw new InvalidOperationException("Console is not currently active");
  76. DriverCheck();
  77. Driver.SetConsoleEncoding(codepage);
  78. }
  79. public static void SetConsoleTitle(string title)
  80. {
  81. DriverCheck();
  82. Driver.SetConsoleTitle(title);
  83. }
  84. public static void SetConsoleColor(ConsoleColor color)
  85. {
  86. DriverCheck();
  87. Driver.SetConsoleColor(color);
  88. }
  89. internal static void SetConsoleStreams()
  90. {
  91. if (ConsoleActive)
  92. {
  93. Console.SetOut(ConsoleStream);
  94. Console.SetError(ConsoleStream);
  95. }
  96. else
  97. {
  98. Console.SetOut(TextWriter.Null);
  99. Console.SetError(TextWriter.Null);
  100. }
  101. }
  102. public static readonly ConfigEntry<bool> ConfigConsoleEnabled = ConfigFile.CoreConfig.Bind(
  103. "Logging.Console", "Enabled",
  104. false,
  105. "Enables showing a console for log output.");
  106. public static readonly ConfigEntry<bool> ConfigConsoleShiftJis = ConfigFile.CoreConfig.Bind(
  107. "Logging.Console", "ShiftJisEncoding",
  108. false,
  109. "If true, console is set to the Shift-JIS encoding, otherwise UTF-8 encoding.");
  110. }
  111. }