ConsoleManager.cs 3.0 KB

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