ConsoleManager.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // Apparently some versions of Mono throw a "Encoding name 'xxx' not supported"
  54. // if you use Encoding.GetEncoding
  55. // That's why we use of codepages directly and handle then in console drivers separately
  56. var codepage = ConfigConsoleShiftJis.Value ? SHIFT_JIS_CP: (uint)Encoding.UTF8.CodePage;
  57. Driver.CreateConsole(codepage);
  58. SetConsoleStreams();
  59. }
  60. public static void DetachConsole()
  61. {
  62. if (!ConsoleActive)
  63. return;
  64. DriverCheck();
  65. Driver.DetachConsole();
  66. SetConsoleStreams();
  67. }
  68. public static void SetConsoleTitle(string title)
  69. {
  70. DriverCheck();
  71. Driver.SetConsoleTitle(title);
  72. }
  73. public static void SetConsoleColor(ConsoleColor color)
  74. {
  75. DriverCheck();
  76. Driver.SetConsoleColor(color);
  77. }
  78. internal static void SetConsoleStreams()
  79. {
  80. if (ConsoleActive)
  81. {
  82. Console.SetOut(ConsoleStream);
  83. Console.SetError(ConsoleStream);
  84. }
  85. else
  86. {
  87. Console.SetOut(TextWriter.Null);
  88. Console.SetError(TextWriter.Null);
  89. }
  90. }
  91. public static readonly ConfigEntry<bool> ConfigConsoleEnabled = ConfigFile.CoreConfig.Bind(
  92. "Logging.Console", "Enabled",
  93. false,
  94. "Enables showing a console for log output.");
  95. public static readonly ConfigEntry<bool> ConfigConsoleShiftJis = ConfigFile.CoreConfig.Bind(
  96. "Logging.Console", "ShiftJisEncoding",
  97. false,
  98. "If true, console is set to the Shift-JIS encoding, otherwise UTF-8 encoding.");
  99. }
  100. }