diff --git a/BetterGenshinImpact/App.xaml.cs b/BetterGenshinImpact/App.xaml.cs index 24a268b6..47529397 100644 --- a/BetterGenshinImpact/App.xaml.cs +++ b/BetterGenshinImpact/App.xaml.cs @@ -69,6 +69,7 @@ public partial class App : Application { loggerConfiguration.WriteTo.RichTextBox(richTextBox, LogEventLevel.Information, outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"); } + loggerConfiguration.WriteTo.Sink(new HutaoNamedPipeLogEventSink(), LogEventLevel.Information); Log.Logger = loggerConfiguration.CreateLogger(); services.AddLogging(c => c.AddSerilog()); diff --git a/BetterGenshinImpact/Hutao/BGINamedPipe.cs b/BetterGenshinImpact/Hutao/BGINamedPipe.cs index caf6454a..08fa5b9c 100644 --- a/BetterGenshinImpact/Hutao/BGINamedPipe.cs +++ b/BetterGenshinImpact/Hutao/BGINamedPipe.cs @@ -92,6 +92,10 @@ internal sealed partial class BGINamedPipe : IDisposable } break; + + case (PipePacketType.SessionTermination, _): + serverStream.Disconnect(); + return; } } } diff --git a/BetterGenshinImpact/Hutao/HutaoNamedPipe.cs b/BetterGenshinImpact/Hutao/HutaoNamedPipe.cs index d8fa570b..a6188b55 100644 --- a/BetterGenshinImpact/Hutao/HutaoNamedPipe.cs +++ b/BetterGenshinImpact/Hutao/HutaoNamedPipe.cs @@ -1,9 +1,6 @@ -using Newtonsoft.Json; -using System; -using System.Collections.Generic; +using System; using System.IO.Pipes; using System.Text; -using System.Text.Json.Serialization; namespace BetterGenshinImpact.Hutao; @@ -40,6 +37,18 @@ internal sealed partial class HutaoNamedPipe : IDisposable get => isSupported.Value; } + public bool TryRedirectLog(string log) + { + if (!IsSupported) + { + return false; + } + + byte[] buffer = Encoding.UTF8.GetBytes(log); + clientStream.Write(buffer, 0, buffer.Length); + return true; + } + public void Dispose() { clientStream.Dispose(); diff --git a/BetterGenshinImpact/Hutao/HutaoNamedPipeLogEventSink.cs b/BetterGenshinImpact/Hutao/HutaoNamedPipeLogEventSink.cs new file mode 100644 index 00000000..8f42bc7b --- /dev/null +++ b/BetterGenshinImpact/Hutao/HutaoNamedPipeLogEventSink.cs @@ -0,0 +1,37 @@ +using Serilog.Core; +using Serilog.Events; +using Serilog.Formatting.Display; +using System.IO; + +namespace BetterGenshinImpact.Hutao; + +internal sealed class HutaoNamedPipeLogEventSink : ILogEventSink +{ + private readonly MessageTemplateTextFormatter textFormatter = new("[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"); + + private readonly MemoryStream buffer; + private readonly TextWriter writer; + private readonly TextReader reader; + + private HutaoNamedPipe? namedPipe; + + public HutaoNamedPipeLogEventSink() + { + buffer = new(); + writer = new StreamWriter(buffer); + reader = new StreamReader(buffer); + } + + private HutaoNamedPipe NamedPipe + { + get => namedPipe ??= App.GetService()!; + } + + public void Emit(LogEvent logEvent) + { + textFormatter.Format(logEvent, writer); + buffer.Position = 0; + NamedPipe.TryRedirectLog(reader.ReadToEnd()); + buffer.SetLength(0); + } +} \ No newline at end of file