From 58b0f884151d5a57d19c1aef360178bea6f420a3 Mon Sep 17 00:00:00 2001 From: Gurge44 Date: Sun, 17 Nov 2024 13:08:41 +0100 Subject: [PATCH] fix --- Modules/Debugger.cs | 109 +++++++++++++++++++++++++++++------- Modules/DevManager.cs | 1 + Patches/GameOptionsPatch.cs | 4 +- main.cs | 1 + 4 files changed, 94 insertions(+), 21 deletions(-) diff --git a/Modules/Debugger.cs b/Modules/Debugger.cs index fd7a517f3..1f38926e2 100644 --- a/Modules/Debugger.cs +++ b/Modules/Debugger.cs @@ -105,24 +105,7 @@ namespace EHR log_text = $"[{t}][{tag}]{text}"; } - if (multiLine) - { - Main.Instance.StartCoroutine(LogMultilineAsync()); - return; - } - - CustomLogger.Instance.Log(level.ToString(), log_text); - - return; - - IEnumerator LogMultilineAsync() - { - foreach (string s in log_text.Split("\\n")) - { - CustomLogger.Instance.Log(level.ToString(), s); - yield return null; - } - } + CustomLogger.Instance.Log(level.ToString(), log_text, multiLine); } public static void Test(object content, string tag = "======= Test =======", bool escapeCRLF = true, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string fileName = "", bool multiLine = false) @@ -229,8 +212,10 @@ namespace EHR File.WriteAllText(LOGFilePath, HtmlHeader); } - public void Log(string level, string message) + public void Log(string level, string message, bool multiLine = false) { + if (multiLine) message = message.Replace("\\n", "
"); + string logEntry = $"""
{message} @@ -254,4 +239,90 @@ namespace EHR PrivateInstance = null; } } + + public static class LogMonitor + { + private const string SourceLogFilePath = "./BepInEx/LogOutput.log"; + private const string HtmlLogFilePath = "./EHR_DATA/log.html"; + private static long LastProcessedLength; + + public static void StartMonitoring() + { + Main.Instance.StartCoroutine(MonitorLog()); + return; + + IEnumerator MonitorLog() + { + while (true) + { + ProcessLogUpdates(); + yield return new WaitForSeconds(1f); + } + + // ReSharper disable once IteratorNeverReturns + } + } + + private static void ProcessLogUpdates() + { + try + { + using FileStream stream = new FileStream(SourceLogFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + using StreamReader reader = new StreamReader(stream); + + if (stream.Length == LastProcessedLength) return; + + stream.Seek(LastProcessedLength, SeekOrigin.Begin); + + string currentGroup = null; + string currentClass = "info"; + + while (reader.ReadLine() is { } line) + { + if (line.StartsWith("[")) + { + if (currentGroup != null) + { + AppendLogToHtml($"
{currentGroup}
"); + } + + currentClass = line.Split(':')[0].TrimStart('[').Replace(" ", "").ToLower(); + currentGroup = line; + } + else if (currentGroup != null) + { + currentGroup += $"
{line}"; + } + } + + if (currentGroup != null) + { + AppendLogToHtml($"
{currentGroup}
"); + } + + LastProcessedLength = stream.Length; + } + catch (Exception ex) + { + Utils.ThrowException(ex); + } + } + + private static void AppendLogToHtml(string line) + { + string htmlEntry = ParseLogLine(line); + File.AppendAllText(HtmlLogFilePath, htmlEntry); + } + + private static string ParseLogLine(string line) + { + string cssClass = line.Split(':')[0].TrimStart('[').Replace(" ", "").ToLower(); + + return $""" +
+ {line} +
+ """; + } + } } \ No newline at end of file diff --git a/Modules/DevManager.cs b/Modules/DevManager.cs index 34d9210bf..8e851f93a 100644 --- a/Modules/DevManager.cs +++ b/Modules/DevManager.cs @@ -126,6 +126,7 @@ namespace EHR new("stiltedgap#2406", "#ffff00", "Youtuber", true), // Marcopolo1982 new("spaglad#3866", isUp: true), // spider_191 new("stapleecho#3635", isUp: true), // Thaylor1819 + new("strangeant#6543", isUp: true), // after.edits // Sponsor new("recentduct#6068", "#FF00FF", "高冷男模法师", false, true), diff --git a/Patches/GameOptionsPatch.cs b/Patches/GameOptionsPatch.cs index 565061511..6e2135717 100644 --- a/Patches/GameOptionsPatch.cs +++ b/Patches/GameOptionsPatch.cs @@ -11,7 +11,7 @@ namespace EHR.Patches public static bool Prefix(GameModes gameMode) { - if ((!AmongUsClient.Instance.AmHost && PlayerControl.LocalPlayer != null) || gameMode != GameModes.HideNSeek || Warned || !HudManager.Instance) return true; + if (!Options.IsLoaded || (AmongUsClient.Instance != null && !AmongUsClient.Instance.AmHost && PlayerControl.LocalPlayer != null) || gameMode != GameModes.HideNSeek || Warned || !HudManager.Instance) return true; HudManager.Instance.ShowPopUp(Translator.GetString("HnSUnloadWarning")); return false; @@ -19,7 +19,7 @@ namespace EHR.Patches public static void Postfix(GameModes gameMode) { - if ((!AmongUsClient.Instance.AmHost && PlayerControl.LocalPlayer != null) || gameMode != GameModes.HideNSeek) return; + if (!Options.IsLoaded || (!AmongUsClient.Instance.AmHost && PlayerControl.LocalPlayer != null) || gameMode != GameModes.HideNSeek) return; if (!Warned) { diff --git a/main.cs b/main.cs index be4c775f1..67a9841f0 100644 --- a/main.cs +++ b/main.cs @@ -708,6 +708,7 @@ namespace EHR ClassInjector.RegisterTypeInIl2Cpp(); CustomLogger.ClearLog(); + LogMonitor.StartMonitoring(); Harmony.PatchAll();