ソースを参照

Implement stdout ouput for windows platform

Bepis 4 年 前
コミット
e18e932610

+ 1 - 0
BepInEx.Core/BepInEx.Core.csproj

@@ -82,6 +82,7 @@
     <Compile Include="Logging\ILogListener.cs" />
     <Compile Include="Logging\ILogSource.cs" />
     <Compile Include="Logging\ManualLogSource.cs" />
+    <Compile Include="Logging\StdOutLogListener.cs" />
     <Compile Include="Logging\TraceLogSource.cs" />
     <Compile Include="Logging\ConsoleLogListener.cs" />
     <Compile Include="Paths.cs" />

+ 8 - 0
BepInEx.Core/ConsoleUtil/ConsoleWindow.cs

@@ -7,6 +7,7 @@ using System;
 using System.IO;
 using System.Runtime.InteropServices;
 using System.Text;
+using Microsoft.Win32.SafeHandles;
 
 namespace UnityInjector.ConsoleUtil
 {
@@ -16,6 +17,8 @@ namespace UnityInjector.ConsoleUtil
 		private static IntPtr _cOut;
 		private static IntPtr _oOut;
 
+		public static TextWriter OriginalOut { get; set; }
+
 		public static TextWriter StandardOut { get; private set; }
 
 		public static void Attach()
@@ -42,6 +45,11 @@ namespace UnityInjector.ConsoleUtil
 
 			if (!SetStdHandle(-11, _cOut))
 				throw new Exception("SetStdHandle() failed");
+
+
+			var originalOutStream = new FileStream(new SafeFileHandle(_oOut, false), FileAccess.Write);
+			OriginalOut = new StreamWriter(originalOutStream, new UTF8Encoding(false));
+
 			Init();
 
 			IsAttached = true;

+ 1 - 2
BepInEx.Core/Logging/ConsoleLogListener.cs

@@ -1,11 +1,10 @@
 using System;
 using BepInEx.Configuration;
-using BepInEx.ConsoleUtil;
 
 namespace BepInEx.Logging
 {
 	/// <summary>
-	/// Logs entries using Unity specific outputs.
+	/// Logs entries using a console spawned by BepInEx.
 	/// </summary>
 	public class ConsoleLogListener : ILogListener
 	{

+ 20 - 0
BepInEx.Core/Logging/StdOutLogListener.cs

@@ -0,0 +1,20 @@
+using BepInEx.Logging;
+using UnityInjector.ConsoleUtil;
+
+namespace BepInEx.Core.Logging
+{
+	/// <summary>
+	/// Logs entries to StdOut, for platforms that spawn a console instance and hijack the original output stream.
+	/// </summary>
+	public class StdOutLogListener : ILogListener
+	{
+		public void LogEvent(object sender, LogEventArgs eventArgs)
+		{
+			string log = $"[{eventArgs.Level,-7}:{((ILogSource)sender).SourceName,10}] {eventArgs.Data}\r\n";
+
+			ConsoleWindow.OriginalOut?.Write(log);
+		}
+
+		public void Dispose() { }
+	}
+}

+ 3 - 2
BepInEx.Preloader.Unity/UnityPreloader.cs

@@ -3,10 +3,10 @@ using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
 using System.Linq;
-using System.Reflection;
 using System.Text;
 using BepInEx.Bootstrap;
 using BepInEx.Configuration;
+using BepInEx.Core.Logging;
 using BepInEx.Logging;
 using BepInEx.Preloader.Core;
 using BepInEx.Preloader.Core.Logging;
@@ -14,7 +14,6 @@ using BepInEx.Preloader.Core.RuntimeFixes;
 using BepInEx.Preloader.RuntimeFixes;
 using Mono.Cecil;
 using Mono.Cecil.Cil;
-using Mono.Cecil.Rocks;
 using MonoMod.RuntimeDetour;
 using MethodAttributes = Mono.Cecil.MethodAttributes;
 
@@ -122,6 +121,8 @@ namespace BepInEx.Preloader.Unity
 				Logger.Listeners.Add(new ConsoleLogListener());
 
 				PreloaderLog.Dispose();
+
+				Logger.Listeners.Add(new StdOutLogListener());
 			}
 			catch (Exception ex)
 			{