|
@@ -1,7 +1,10 @@
|
|
|
using System;
|
|
|
using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using System.Reflection;
|
|
|
using System.Text;
|
|
|
using BepInEx.ConsoleUtil;
|
|
|
+using HarmonyLib;
|
|
|
using Microsoft.Win32.SafeHandles;
|
|
|
using UnityInjector.ConsoleUtil;
|
|
|
|
|
@@ -9,11 +12,6 @@ namespace BepInEx
|
|
|
{
|
|
|
internal class WindowsConsoleDriver : IConsoleDriver
|
|
|
{
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- private SafeFileHandle _originalOutHandle, _consoleOutHandle;
|
|
|
-
|
|
|
public TextWriter StandardOut { get; private set; }
|
|
|
public TextWriter ConsoleOut { get; private set; }
|
|
|
|
|
@@ -27,18 +25,32 @@ namespace BepInEx
|
|
|
StandardOut = Console.Out;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private static readonly ConstructorInfo FileStreamCtor = new[]
|
|
|
+ {
|
|
|
+ AccessTools.Constructor(typeof(FileStream), new[] { typeof(SafeFileHandle), typeof(FileAccess) }),
|
|
|
+ AccessTools.Constructor(typeof(FileStream), new[] { typeof(IntPtr), typeof(FileAccess) }),
|
|
|
+ }.FirstOrDefault(m => m != null);
|
|
|
+
|
|
|
+ private static FileStream OpenFileStream(IntPtr handle)
|
|
|
+ {
|
|
|
+ var fileHandle = new SafeFileHandle(handle, false);
|
|
|
+ var ctorParams = AccessTools.ActualParameters(FileStreamCtor, new object[] { fileHandle, fileHandle.DangerousGetHandle(), FileAccess.Write });
|
|
|
+ return (FileStream)Activator.CreateInstance(typeof(FileStream), ctorParams);
|
|
|
+ }
|
|
|
+
|
|
|
public void CreateConsole(uint codepage)
|
|
|
{
|
|
|
-
|
|
|
-
|
|
|
-#pragma warning disable 618
|
|
|
ConsoleWindow.Attach();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ConsoleEncoding.ConsoleCodePage = codepage;
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
var stdout = GetOutHandle();
|
|
@@ -49,22 +61,19 @@ namespace BepInEx
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- _originalOutHandle = new SafeFileHandle(stdout, false);
|
|
|
- var originalOutStream = new FileStream(_originalOutHandle.DangerousGetHandle(), FileAccess.Write);
|
|
|
+ var originalOutStream = OpenFileStream(stdout);
|
|
|
StandardOut = new StreamWriter(originalOutStream, new UTF8Encoding(false))
|
|
|
{
|
|
|
AutoFlush = true
|
|
|
};
|
|
|
|
|
|
- _consoleOutHandle = new SafeFileHandle(ConsoleWindow.ConsoleOutHandle, false);
|
|
|
- var consoleOutStream = new FileStream(_consoleOutHandle.DangerousGetHandle(), FileAccess.Write);
|
|
|
+ var consoleOutStream = OpenFileStream(ConsoleWindow.ConsoleOutHandle);
|
|
|
|
|
|
ConsoleOut = new StreamWriter(consoleOutStream, ConsoleEncoding.OutputEncoding)
|
|
|
{
|
|
|
AutoFlush = true
|
|
|
};
|
|
|
ConsoleActive = true;
|
|
|
-#pragma warning restore 618
|
|
|
}
|
|
|
|
|
|
private IntPtr GetOutHandle()
|