ConsoleManager.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using BepInEx.Configuration;
  5. using UnityInjector.ConsoleUtil;
  6. namespace BepInEx
  7. {
  8. public static class ConsoleManager
  9. {
  10. public static bool ConsoleActive { get; private set; }
  11. public static TextWriter StandardOut => ConsoleWindow.StandardOut;
  12. public static void CreateConsole()
  13. {
  14. if (ConsoleActive)
  15. return;
  16. switch (Environment.OSVersion.Platform)
  17. {
  18. case PlatformID.Win32NT:
  19. {
  20. ConsoleWindow.Attach();
  21. break;
  22. }
  23. default:
  24. throw new PlatformNotSupportedException("Spawning a console is not currently supported on this platform");
  25. }
  26. ConsoleActive = true;
  27. }
  28. public static void DetachConsole()
  29. {
  30. if (!ConsoleActive)
  31. return;
  32. switch (Environment.OSVersion.Platform)
  33. {
  34. case PlatformID.Win32NT:
  35. {
  36. ConsoleWindow.Detach();
  37. break;
  38. }
  39. default:
  40. throw new PlatformNotSupportedException("Spawning a console is not currently supported on this platform");
  41. }
  42. ConsoleActive = false;
  43. }
  44. public static void SetConsoleEncoding(uint encodingCodePage)
  45. {
  46. if (!ConsoleActive)
  47. throw new InvalidOperationException("Console is not currently active");
  48. switch (Environment.OSVersion.Platform)
  49. {
  50. case PlatformID.Win32NT:
  51. {
  52. ConsoleEncoding.ConsoleCodePage = encodingCodePage;
  53. Console.OutputEncoding = Encoding.GetEncoding((int)encodingCodePage);
  54. break;
  55. }
  56. default:
  57. throw new PlatformNotSupportedException("Spawning a console is not currently supported on this platform");
  58. }
  59. }
  60. public static void SetConsoleTitle(string title)
  61. {
  62. if (!ConsoleActive)
  63. throw new InvalidOperationException("Console is not currently active");
  64. switch (Environment.OSVersion.Platform)
  65. {
  66. case PlatformID.Win32NT:
  67. {
  68. ConsoleWindow.Title = title;
  69. break;
  70. }
  71. default:
  72. throw new PlatformNotSupportedException("Spawning a console is not currently supported on this platform");
  73. }
  74. }
  75. public static void ForceSetActive(bool value)
  76. {
  77. ConsoleActive = value;
  78. }
  79. public static readonly ConfigEntry<bool> ConfigConsoleEnabled = ConfigFile.CoreConfig.Bind(
  80. "Logging.Console", "Enabled",
  81. false,
  82. "Enables showing a console for log output.");
  83. public static readonly ConfigEntry<bool> ConfigConsoleShiftJis = ConfigFile.CoreConfig.Bind(
  84. "Logging.Console", "ShiftJisEncoding",
  85. false,
  86. "If true, console is set to the Shift-JIS encoding, otherwise UTF-8 encoding.");
  87. }
  88. }