ConsoleWindow.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // --------------------------------------------------
  2. // UnityInjector - ConsoleWindow.cs
  3. // Copyright (c) Usagirei 2015 - 2015
  4. // --------------------------------------------------
  5. using System;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using BepInEx.Configuration;
  10. namespace UnityInjector.ConsoleUtil
  11. {
  12. internal class ConsoleWindow
  13. {
  14. public static readonly ConfigEntry<bool> ConfigConsoleEnabled = ConfigFile.CoreConfig.AddSetting(
  15. "Logging.Console", "Enabled",
  16. false,
  17. "Enables showing a console for log output.");
  18. public static readonly ConfigEntry<bool> ConfigConsoleShiftJis = ConfigFile.CoreConfig.AddSetting(
  19. "Logging.Console", "ShiftJisEncoding",
  20. false,
  21. "If true, console is set to the Shift-JIS encoding, otherwise UTF-8 encoding.");
  22. public static bool IsAttached { get; private set; }
  23. private static IntPtr _cOut;
  24. private static IntPtr _oOut;
  25. public static TextWriter StandardOut { get; private set; }
  26. public static void Attach()
  27. {
  28. if (IsAttached)
  29. return;
  30. if (_oOut == IntPtr.Zero)
  31. _oOut = GetStdHandle(-11);
  32. // Store Current Window
  33. IntPtr currWnd = GetForegroundWindow();
  34. //Check for existing console before allocating
  35. if (GetConsoleWindow() == IntPtr.Zero)
  36. if (!AllocConsole())
  37. throw new Exception("AllocConsole() failed");
  38. // Restore Foreground
  39. SetForegroundWindow(currWnd);
  40. _cOut = CreateFile("CONOUT$", 0x80000000 | 0x40000000, 2, IntPtr.Zero, 3, 0, IntPtr.Zero);
  41. BepInEx.ConsoleUtil.Kon.conOut = _cOut;
  42. if (!SetStdHandle(-11, _cOut))
  43. throw new Exception("SetStdHandle() failed");
  44. Init();
  45. IsAttached = true;
  46. }
  47. public static string Title
  48. {
  49. set
  50. {
  51. if (!IsAttached)
  52. return;
  53. if (value == null)
  54. {
  55. throw new ArgumentNullException(nameof(value));
  56. }
  57. if (value.Length > 24500)
  58. {
  59. throw new InvalidOperationException("Console title too long");
  60. }
  61. if (!SetConsoleTitle(value))
  62. {
  63. throw new InvalidOperationException("Console title invalid");
  64. }
  65. }
  66. }
  67. public static void Detach()
  68. {
  69. if (!IsAttached)
  70. return;
  71. if (!CloseHandle(_cOut))
  72. throw new Exception("CloseHandle() failed");
  73. _cOut = IntPtr.Zero;
  74. if (!FreeConsole())
  75. throw new Exception("FreeConsole() failed");
  76. if (!SetStdHandle(-11, _oOut))
  77. throw new Exception("SetStdHandle() failed");
  78. Init();
  79. IsAttached = false;
  80. }
  81. [DllImport("user32.dll")]
  82. private static extern IntPtr GetForegroundWindow();
  83. [DllImport("user32.dll")]
  84. [return: MarshalAs(UnmanagedType.Bool)]
  85. private static extern bool SetForegroundWindow(IntPtr hWnd);
  86. [DllImport("kernel32.dll", SetLastError = true)]
  87. private static extern bool AllocConsole();
  88. [DllImport("kernel32.dll")]
  89. private static extern IntPtr GetConsoleWindow();
  90. [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
  91. private static extern bool CloseHandle(IntPtr handle);
  92. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  93. private static extern IntPtr CreateFile(
  94. string fileName,
  95. uint desiredAccess,
  96. int shareMode,
  97. IntPtr securityAttributes,
  98. int creationDisposition,
  99. int flagsAndAttributes,
  100. IntPtr templateFile);
  101. [DllImport("kernel32.dll", SetLastError = false)]
  102. private static extern bool FreeConsole();
  103. [DllImport("kernel32.dll", SetLastError = true)]
  104. private static extern IntPtr GetStdHandle(int nStdHandle);
  105. private static void Init()
  106. {
  107. var stdOut = Console.OpenStandardOutput();
  108. StandardOut = new StreamWriter(stdOut, Encoding.Default)
  109. {
  110. AutoFlush = true
  111. };
  112. Console.SetOut(StandardOut);
  113. Console.SetError(StandardOut);
  114. }
  115. [DllImport("kernel32.dll", SetLastError = true)]
  116. private static extern bool SetStdHandle(int nStdHandle, IntPtr hConsoleOutput);
  117. [DllImport("kernel32.dll", BestFitMapping = true, CharSet = CharSet.Auto, SetLastError = true)]
  118. private static extern bool SetConsoleTitle(string title);
  119. }
  120. }