ConsoleManager.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. internal static IConsoleDriver Driver { get; set; }
  12. /// <summary>
  13. /// True if an external console has been started, false otherwise.
  14. /// </summary>
  15. public static bool ConsoleActive => Driver?.ConsoleActive ?? false;
  16. /// <summary>
  17. /// The stream that writes to the standard out stream of the process. Should never be null.
  18. /// </summary>
  19. public static TextWriter StandardOutStream => Driver?.StandardOut;
  20. /// <summary>
  21. /// The stream that writes to an external console. Null if no such console exists
  22. /// </summary>
  23. public static TextWriter ConsoleStream => Driver?.ConsoleOut;
  24. public static void Initialize(bool alreadyActive)
  25. {
  26. switch (Utility.CurrentOs)
  27. {
  28. case Platform.MacOS:
  29. case Platform.Linux:
  30. {
  31. Driver = new LinuxConsoleDriver();
  32. break;
  33. }
  34. case Platform.Windows:
  35. {
  36. Driver = new WindowsConsoleDriver();
  37. break;
  38. }
  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. Driver.CreateConsole();
  53. SetConsoleStreams();
  54. }
  55. public static void DetachConsole()
  56. {
  57. if (!ConsoleActive)
  58. return;
  59. DriverCheck();
  60. Driver.DetachConsole();
  61. SetConsoleStreams();
  62. }
  63. public static void SetConsoleEncoding()
  64. {
  65. // Apparently Windows code-pages work in Mono.
  66. // https://stackoverflow.com/a/33456543
  67. // Alternatively we can pass in "shift-jis"
  68. var encoding = ConfigConsoleShiftJis.Value ? Encoding.GetEncoding(932): Encoding.UTF8;
  69. SetConsoleEncoding(encoding);
  70. }
  71. public static void SetConsoleEncoding(Encoding encoding)
  72. {
  73. if (!ConsoleActive)
  74. throw new InvalidOperationException("Console is not currently active");
  75. DriverCheck();
  76. Driver.SetConsoleEncoding(encoding);
  77. }
  78. public static void SetConsoleTitle(string title)
  79. {
  80. DriverCheck();
  81. Driver.SetConsoleTitle(title);
  82. }
  83. public static void SetConsoleColor(ConsoleColor color)
  84. {
  85. DriverCheck();
  86. Driver.SetConsoleColor(color);
  87. }
  88. internal static void SetConsoleStreams()
  89. {
  90. if (ConsoleActive)
  91. {
  92. Console.SetOut(ConsoleStream);
  93. Console.SetError(ConsoleStream);
  94. }
  95. else
  96. {
  97. Console.SetOut(TextWriter.Null);
  98. Console.SetError(TextWriter.Null);
  99. }
  100. }
  101. public static readonly ConfigEntry<bool> ConfigConsoleEnabled = ConfigFile.CoreConfig.Bind(
  102. "Logging.Console", "Enabled",
  103. false,
  104. "Enables showing a console for log output.");
  105. public static readonly ConfigEntry<bool> ConfigConsoleShiftJis = ConfigFile.CoreConfig.Bind(
  106. "Logging.Console", "ShiftJisEncoding",
  107. false,
  108. "If true, console is set to the Shift-JIS encoding, otherwise UTF-8 encoding.");
  109. }
  110. }