ConsoleManager.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. public static bool ConsoleActive { get; private set; }
  12. public static TextWriter StandardOut => ConsoleWindow.StandardOut;
  13. public static void CreateConsole()
  14. {
  15. if (ConsoleActive)
  16. return;
  17. switch (Environment.OSVersion.Platform)
  18. {
  19. case PlatformID.Win32NT:
  20. {
  21. ConsoleWindow.Attach();
  22. break;
  23. }
  24. default:
  25. throw new PlatformNotSupportedException("Spawning a console is not currently supported on this platform");
  26. }
  27. ConsoleActive = true;
  28. }
  29. public static void DetachConsole()
  30. {
  31. if (!ConsoleActive)
  32. return;
  33. switch (Environment.OSVersion.Platform)
  34. {
  35. case PlatformID.Win32NT:
  36. {
  37. ConsoleWindow.Detach();
  38. break;
  39. }
  40. default:
  41. throw new PlatformNotSupportedException("Spawning a console is not currently supported on this platform");
  42. }
  43. ConsoleActive = false;
  44. }
  45. public static void SetConsoleEncoding(uint encodingCodePage)
  46. {
  47. if (!ConsoleActive)
  48. throw new InvalidOperationException("Console is not currently active");
  49. switch (Environment.OSVersion.Platform)
  50. {
  51. case PlatformID.Win32NT:
  52. {
  53. ConsoleEncoding.ConsoleCodePage = encodingCodePage;
  54. Console.OutputEncoding = Encoding.GetEncoding((int)encodingCodePage);
  55. break;
  56. }
  57. default:
  58. throw new PlatformNotSupportedException("Spawning a console is not currently supported on this platform");
  59. }
  60. }
  61. public static void SetConsoleTitle(string title)
  62. {
  63. if (!ConsoleActive)
  64. throw new InvalidOperationException("Console is not currently active");
  65. switch (Environment.OSVersion.Platform)
  66. {
  67. case PlatformID.Win32NT:
  68. {
  69. ConsoleWindow.Title = title;
  70. break;
  71. }
  72. default:
  73. throw new PlatformNotSupportedException("Spawning a console is not currently supported on this platform");
  74. }
  75. }
  76. public static void SetConsoleColor(ConsoleColor color)
  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. Kon.ForegroundColor = color;
  85. break;
  86. }
  87. default:
  88. {
  89. break;
  90. }
  91. }
  92. Console.ForegroundColor = color;
  93. }
  94. public static void ForceSetActive(bool value)
  95. {
  96. ConsoleActive = value;
  97. }
  98. public static readonly ConfigEntry<bool> ConfigConsoleEnabled = ConfigFile.CoreConfig.Bind(
  99. "Logging.Console", "Enabled",
  100. false,
  101. "Enables showing a console for log output.");
  102. public static readonly ConfigEntry<bool> ConfigConsoleShiftJis = ConfigFile.CoreConfig.Bind(
  103. "Logging.Console", "ShiftJisEncoding",
  104. false,
  105. "If true, console is set to the Shift-JIS encoding, otherwise UTF-8 encoding.");
  106. }
  107. }