ConsoleManager.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 readonly ConfigEntry<bool> ConfigConsoleEnabled = ConfigFile.CoreConfig.Bind(
  76. "Logging.Console", "Enabled",
  77. false,
  78. "Enables showing a console for log output.");
  79. public static readonly ConfigEntry<bool> ConfigConsoleShiftJis = ConfigFile.CoreConfig.Bind(
  80. "Logging.Console", "ShiftJisEncoding",
  81. false,
  82. "If true, console is set to the Shift-JIS encoding, otherwise UTF-8 encoding.");
  83. }
  84. }