|
@@ -1,29 +1,20 @@
|
|
|
using System;
|
|
|
-using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
-using System.Linq;
|
|
|
using System.Text;
|
|
|
+using BepInEx.Logging;
|
|
|
using HarmonyLib;
|
|
|
|
|
|
-namespace BepInEx.Preloader.Core.RuntimeFixes
|
|
|
+namespace BepInEx.Preloader.RuntimeFixes
|
|
|
{
|
|
|
- /*
|
|
|
- * By default, setting Console.Out removes the previous listener
|
|
|
- * This can be a problem in Unity because it installs its own TextWriter while BepInEx needs to install it
|
|
|
- * one too for the console. This runtime fix collects all Console.Out setters and aggregates them into a single
|
|
|
- * text writer.
|
|
|
- *
|
|
|
- * This allows to both fix the old problem with log overwriting and problem with writing stdout when console is
|
|
|
- * enabled.
|
|
|
- */
|
|
|
- public static class ConsoleSetOutFix
|
|
|
+ internal static class ConsoleSetOutFix
|
|
|
{
|
|
|
- private static AggregatedTextWriter aggregatedTextWriter;
|
|
|
+ private static LoggedTextWriter loggedTextWriter;
|
|
|
+ internal static ManualLogSource ConsoleLogSource = Logger.CreateLogSource("Console");
|
|
|
|
|
|
public static void Apply()
|
|
|
{
|
|
|
- aggregatedTextWriter = new AggregatedTextWriter(Console.Out);
|
|
|
- Console.SetOut(aggregatedTextWriter);
|
|
|
+ loggedTextWriter = new LoggedTextWriter { Parent = Console.Out };
|
|
|
+ Console.SetOut(loggedTextWriter);
|
|
|
HarmonyLib.Harmony.CreateAndPatchAll(typeof(ConsoleSetOutFix));
|
|
|
}
|
|
|
|
|
@@ -31,40 +22,29 @@ namespace BepInEx.Preloader.Core.RuntimeFixes
|
|
|
[HarmonyPrefix]
|
|
|
private static bool OnSetOut(TextWriter newOut)
|
|
|
{
|
|
|
- if (newOut == TextWriter.Null)
|
|
|
- return false;
|
|
|
-
|
|
|
- aggregatedTextWriter.Add(newOut);
|
|
|
+ loggedTextWriter.Parent = newOut;
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- internal class AggregatedTextWriter : TextWriter
|
|
|
+ internal class LoggedTextWriter : TextWriter
|
|
|
{
|
|
|
public override Encoding Encoding { get; } = Encoding.UTF8;
|
|
|
|
|
|
- private List<TextWriter> writers = new List<TextWriter>();
|
|
|
+ public TextWriter Parent { get; set; }
|
|
|
+
|
|
|
+ public override void Flush() => Parent.Flush();
|
|
|
|
|
|
- public AggregatedTextWriter(params TextWriter[] initialWriters)
|
|
|
+ public override void Write(string value)
|
|
|
{
|
|
|
- writers.AddRange(initialWriters.Where(w => w != null));
|
|
|
+ ConsoleSetOutFix.ConsoleLogSource.LogInfo(value);
|
|
|
+ Parent.Write(value);
|
|
|
}
|
|
|
|
|
|
- public void Add(TextWriter tw)
|
|
|
+ public override void WriteLine(string value)
|
|
|
{
|
|
|
- if (writers.Any(t => t == tw))
|
|
|
- return;
|
|
|
- writers.Add(tw);
|
|
|
+ ConsoleSetOutFix.ConsoleLogSource.LogInfo(value);
|
|
|
+ Parent.WriteLine(value);
|
|
|
}
|
|
|
-
|
|
|
- public override void Flush() => writers.ForEach(w => w.Flush());
|
|
|
-
|
|
|
- public override void Write(object value) => writers.ForEach(w => w.Write(value));
|
|
|
- public override void Write(string value) => writers.ForEach(w => w.Write(value));
|
|
|
- public override void Write(char value) => writers.ForEach(w => w.Write(value));
|
|
|
-
|
|
|
- public override void WriteLine(object value) => writers.ForEach(w => w.WriteLine(value));
|
|
|
- public override void WriteLine(string value) => writers.ForEach(w => w.WriteLine(value));
|
|
|
- public override void WriteLine(char value) => writers.ForEach(w => w.WriteLine(value));
|
|
|
}
|
|
|
}
|