|
| 1 | +using BepInEx; |
| 2 | +using HarmonyLib; |
| 3 | +using System.Text.RegularExpressions; |
| 4 | + |
| 5 | +namespace QModInstaller.BepInEx.Plugins |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Handles filtering noisy logs from the QModManager logs. |
| 9 | + /// </summary> |
| 10 | + [BepInPlugin(PluginGuid, PluginName, PluginVersion)] |
| 11 | + [BepInProcess("Subnautica"), BepInProcess("SubnauticaZero")] |
| 12 | + internal class LogFilter : BaseUnityPlugin |
| 13 | + { |
| 14 | + internal const string PluginGuid = "QModManager.LogFilter"; |
| 15 | + internal const string PluginName = PluginGuid; |
| 16 | + internal const string PluginVersion = "4.2"; |
| 17 | + |
| 18 | + private void Awake() |
| 19 | + { |
| 20 | + var harmony = new Harmony(PluginGuid); |
| 21 | + harmony.Patch(AccessTools.Method("MirrorInternalLogs.Util.LibcHelper:Format"), |
| 22 | + postfix: new HarmonyMethod(AccessTools.Method(typeof(LogFilter), nameof(LogFilter.LibcHelper_Format_Postfix)))); |
| 23 | + } |
| 24 | + |
| 25 | + private readonly static Regex[] DirtyRegexPatterns = new Regex[] { |
| 26 | + new Regex(@"([\r\n]+)?(\(Filename: .*\))", RegexOptions.Compiled), |
| 27 | + new Regex(@"([\r\n]+)?(Replacing cell.*)", RegexOptions.Compiled), |
| 28 | + new Regex(@"([\r\n]+)?(Resetting cell with.*)", RegexOptions.Compiled), |
| 29 | + new Regex(@"([\r\n]+)?(Fallback handler could not load.*)", RegexOptions.Compiled), |
| 30 | + new Regex(@"([\r\n]+)?(Heartbeat CSV.*,[0-9])", RegexOptions.Compiled), |
| 31 | + new Regex(@"([\r\n]+)?(L[0-9]: .*)", RegexOptions.Compiled), |
| 32 | + new Regex(@"([\r\n]+)?(Kinematic body only supports Speculative Continuous collision detection)", RegexOptions.Compiled) |
| 33 | + }; |
| 34 | + |
| 35 | + private static void LibcHelper_Format_Postfix(ref string __result) |
| 36 | + { |
| 37 | + bool match = false; |
| 38 | + foreach (Regex pattern in DirtyRegexPatterns) |
| 39 | + { |
| 40 | + if (pattern.IsMatch(__result)) |
| 41 | + { |
| 42 | + __result = pattern.Replace(__result, string.Empty).Trim(); |
| 43 | + match = true; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // if our filtering resulted in an empty string, return null so that MirrorInternalLogs will skip the line |
| 48 | + if (match && string.IsNullOrWhiteSpace(__result)) |
| 49 | + { |
| 50 | + __result = null; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | +} |
0 commit comments