ConsoleManager.cs 4.1 KB

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