ConsoleWindow.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 Microsoft.Win32.SafeHandles;
  10. namespace UnityInjector.ConsoleUtil
  11. {
  12. internal class ConsoleWindow
  13. {
  14. public static bool IsAttached { get; private set; }
  15. private static IntPtr _cOut;
  16. private static IntPtr _oOut;
  17. public static TextWriter OriginalOut { get; set; }
  18. public static TextWriter StandardOut { get; private set; }
  19. public static void Attach()
  20. {
  21. if (IsAttached)
  22. return;
  23. if (_oOut == IntPtr.Zero)
  24. _oOut = GetStdHandle(-11);
  25. // Store Current Window
  26. IntPtr currWnd = GetForegroundWindow();
  27. //Check for existing console before allocating
  28. if (GetConsoleWindow() == IntPtr.Zero)
  29. if (!AllocConsole())
  30. throw new Exception("AllocConsole() failed");
  31. // Restore Foreground
  32. SetForegroundWindow(currWnd);
  33. _cOut = CreateFile("CONOUT$", 0x80000000 | 0x40000000, 2, IntPtr.Zero, 3, 0, IntPtr.Zero);
  34. BepInEx.ConsoleUtil.Kon.conOut = _cOut;
  35. if (!SetStdHandle(-11, _cOut))
  36. throw new Exception("SetStdHandle() failed");
  37. var originalOutStream = new FileStream(new SafeFileHandle(_oOut, false), FileAccess.Write);
  38. OriginalOut = new StreamWriter(originalOutStream, new UTF8Encoding(false));
  39. Init();
  40. IsAttached = true;
  41. }
  42. public static string Title
  43. {
  44. set
  45. {
  46. if (!IsAttached)
  47. return;
  48. if (value == null)
  49. {
  50. throw new ArgumentNullException(nameof(value));
  51. }
  52. if (value.Length > 24500)
  53. {
  54. throw new InvalidOperationException("Console title too long");
  55. }
  56. if (!SetConsoleTitle(value))
  57. {
  58. throw new InvalidOperationException("Console title invalid");
  59. }
  60. }
  61. }
  62. public static void Detach()
  63. {
  64. if (!IsAttached)
  65. return;
  66. if (!CloseHandle(_cOut))
  67. throw new Exception("CloseHandle() failed");
  68. _cOut = IntPtr.Zero;
  69. if (!FreeConsole())
  70. throw new Exception("FreeConsole() failed");
  71. if (!SetStdHandle(-11, _oOut))
  72. throw new Exception("SetStdHandle() failed");
  73. Init();
  74. IsAttached = false;
  75. }
  76. [DllImport("user32.dll")]
  77. private static extern IntPtr GetForegroundWindow();
  78. [DllImport("user32.dll")]
  79. [return: MarshalAs(UnmanagedType.Bool)]
  80. private static extern bool SetForegroundWindow(IntPtr hWnd);
  81. [DllImport("kernel32.dll", SetLastError = true)]
  82. private static extern bool AllocConsole();
  83. [DllImport("kernel32.dll")]
  84. private static extern IntPtr GetConsoleWindow();
  85. [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
  86. private static extern bool CloseHandle(IntPtr handle);
  87. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  88. private static extern IntPtr CreateFile(
  89. string fileName,
  90. uint desiredAccess,
  91. int shareMode,
  92. IntPtr securityAttributes,
  93. int creationDisposition,
  94. int flagsAndAttributes,
  95. IntPtr templateFile);
  96. [DllImport("kernel32.dll", SetLastError = false)]
  97. private static extern bool FreeConsole();
  98. [DllImport("kernel32.dll", SetLastError = true)]
  99. private static extern IntPtr GetStdHandle(int nStdHandle);
  100. private static void Init()
  101. {
  102. var stdOut = Console.OpenStandardOutput();
  103. StandardOut = new StreamWriter(stdOut, Encoding.Default)
  104. {
  105. AutoFlush = true
  106. };
  107. Console.SetOut(StandardOut);
  108. Console.SetError(StandardOut);
  109. }
  110. [DllImport("kernel32.dll", SetLastError = true)]
  111. private static extern bool SetStdHandle(int nStdHandle, IntPtr hConsoleOutput);
  112. [DllImport("kernel32.dll", BestFitMapping = true, CharSet = CharSet.Auto, SetLastError = true)]
  113. private static extern bool SetConsoleTitle(string title);
  114. }
  115. }