ConsoleManager.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. switch (Environment.OSVersion.Platform)
  64. {
  65. case PlatformID.Win32NT:
  66. {
  67. if (!ConsoleActive)
  68. return;
  69. ConsoleWindow.Title = title;
  70. break;
  71. }
  72. }
  73. }
  74. public static void SetConsoleColor(ConsoleColor color)
  75. {
  76. switch (Environment.OSVersion.Platform)
  77. {
  78. case PlatformID.Win32NT:
  79. {
  80. if (!ConsoleActive)
  81. return;
  82. Kon.ForegroundColor = color;
  83. break;
  84. }
  85. }
  86. Console.ForegroundColor = color;
  87. }
  88. public static void ForceSetActive(bool value)
  89. {
  90. ConsoleActive = value;
  91. }
  92. public static readonly ConfigEntry<bool> ConfigConsoleEnabled = ConfigFile.CoreConfig.Bind(
  93. "Logging.Console", "Enabled",
  94. false,
  95. "Enables showing a console for log output.");
  96. public static readonly ConfigEntry<bool> ConfigConsoleShiftJis = ConfigFile.CoreConfig.Bind(
  97. "Logging.Console", "ShiftJisEncoding",
  98. false,
  99. "If true, console is set to the Shift-JIS encoding, otherwise UTF-8 encoding.");
  100. }
  101. }