ConsoleManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using BepInEx.Configuration;
  5. using BepInEx.ConsoleUtil;
  6. using UnityInjector.ConsoleUtil;
  7. namespace BepInEx
  8. {
  9. public static class ConsoleManager
  10. {
  11. /// <summary>
  12. /// True if an external console has been started, false otherwise.
  13. /// </summary>
  14. public static bool ConsoleActive { get; private set; }
  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 { get; internal set; }
  19. /// <summary>
  20. /// The stream that writes to an external console. Null if no such console exists
  21. /// </summary>
  22. public static TextWriter ConsoleStream { get; internal set; }
  23. public static void Initialize(bool active)
  24. {
  25. StandardOutStream = Console.Out;
  26. Console.SetOut(StandardOutStream);
  27. ConsoleActive = active;
  28. switch (Environment.OSVersion.Platform)
  29. {
  30. case PlatformID.MacOSX:
  31. case PlatformID.Unix:
  32. {
  33. ConsoleActive = true;
  34. break;
  35. }
  36. }
  37. }
  38. public static void CreateConsole()
  39. {
  40. if (ConsoleActive)
  41. return;
  42. switch (Environment.OSVersion.Platform)
  43. {
  44. case PlatformID.Win32NT:
  45. {
  46. ConsoleWindow.Attach();
  47. break;
  48. }
  49. default:
  50. throw new PlatformNotSupportedException("Spawning a console is not currently supported on this platform");
  51. }
  52. SetConsoleStreams();
  53. ConsoleActive = true;
  54. }
  55. public static void DetachConsole()
  56. {
  57. if (!ConsoleActive)
  58. return;
  59. switch (Environment.OSVersion.Platform)
  60. {
  61. case PlatformID.Win32NT:
  62. {
  63. ConsoleWindow.Detach();
  64. break;
  65. }
  66. default:
  67. throw new PlatformNotSupportedException("Spawning a console is not currently supported on this platform");
  68. }
  69. ConsoleActive = false;
  70. }
  71. public static void SetConsoleEncoding()
  72. {
  73. uint encoding = ConfigConsoleShiftJis.Value ? 932 : (uint)Encoding.UTF8.CodePage;
  74. SetConsoleEncoding(encoding);
  75. }
  76. public static void SetConsoleEncoding(uint encodingCodePage)
  77. {
  78. if (!ConsoleActive)
  79. throw new InvalidOperationException("Console is not currently active");
  80. switch (Environment.OSVersion.Platform)
  81. {
  82. case PlatformID.Win32NT:
  83. {
  84. ConsoleEncoding.ConsoleCodePage = encodingCodePage;
  85. Console.OutputEncoding = Encoding.GetEncoding((int)encodingCodePage);
  86. break;
  87. }
  88. case PlatformID.MacOSX:
  89. case PlatformID.Unix:
  90. {
  91. break;
  92. }
  93. }
  94. }
  95. public static void SetConsoleTitle(string title)
  96. {
  97. switch (Environment.OSVersion.Platform)
  98. {
  99. case PlatformID.Win32NT:
  100. {
  101. if (!ConsoleActive)
  102. return;
  103. ConsoleWindow.Title = title;
  104. break;
  105. }
  106. }
  107. }
  108. public static void SetConsoleColor(ConsoleColor color)
  109. {
  110. switch (Environment.OSVersion.Platform)
  111. {
  112. case PlatformID.Win32NT:
  113. {
  114. if (!ConsoleActive)
  115. return;
  116. Kon.ForegroundColor = color;
  117. break;
  118. }
  119. }
  120. Console.ForegroundColor = color;
  121. }
  122. internal static void SetConsoleStreams()
  123. {
  124. switch (Environment.OSVersion.Platform)
  125. {
  126. case PlatformID.Win32NT:
  127. {
  128. Console.SetOut(ConsoleStream);
  129. Console.SetError(ConsoleStream);
  130. break;
  131. }
  132. case PlatformID.MacOSX:
  133. case PlatformID.Unix:
  134. {
  135. // We do not have external consoles on Unix platforms.
  136. // Set the console output to standard output
  137. Console.SetOut(StandardOutStream);
  138. Console.SetError(StandardOutStream);
  139. break;
  140. }
  141. }
  142. }
  143. public static readonly ConfigEntry<bool> ConfigConsoleEnabled = ConfigFile.CoreConfig.Bind(
  144. "Logging.Console", "Enabled",
  145. false,
  146. "Enables showing a console for log output.");
  147. public static readonly ConfigEntry<bool> ConfigConsoleShiftJis = ConfigFile.CoreConfig.Bind(
  148. "Logging.Console", "ShiftJisEncoding",
  149. false,
  150. "If true, console is set to the Shift-JIS encoding, otherwise UTF-8 encoding.");
  151. }
  152. }