From e7117c9a541edf77c0d2aecacc51a66f7a9154be Mon Sep 17 00:00:00 2001 From: ManlyMarco Date: Wed, 1 Sep 2021 10:53:41 +0200 Subject: [PATCH] Make log button pick the newest log file --- ConfigurationManager/Utilities/Utilities.cs | 32 +++++++++++---------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/ConfigurationManager/Utilities/Utilities.cs b/ConfigurationManager/Utilities/Utilities.cs index 02467ee..197f64e 100644 --- a/ConfigurationManager/Utilities/Utilities.cs +++ b/ConfigurationManager/Utilities/Utilities.cs @@ -96,7 +96,7 @@ public static void OpenLog() { bool TryOpen(string path) { - if (!File.Exists(path)) return false; + if (path == null) return false; try { Process.Start(path); @@ -108,37 +108,39 @@ bool TryOpen(string path) } } + var candidates = new List(); + // Redirected by preloader to game root - if (TryOpen(Path.Combine(Path.Combine(Application.dataPath, ".."), "output_log.txt"))) return; + var rootDir = Path.Combine(Application.dataPath, ".."); + candidates.Add(Path.Combine(rootDir, "output_log.txt")); // Generated in most versions unless disabled - if (TryOpen(Path.Combine(Application.dataPath, "output_log.txt"))) return; + candidates.Add(Path.Combine(Application.dataPath, "output_log.txt")); // Available since 2018.3 var prop = typeof(Application).GetProperty("consoleLogPath", BindingFlags.Static | BindingFlags.Public); if (prop != null) { var path = prop.GetValue(null, null) as string; - if (TryOpen(path)) return; + candidates.Add(path); } if (Directory.Exists(Application.persistentDataPath)) { var file = Directory.GetFiles(Application.persistentDataPath, "output_log.txt", SearchOption.AllDirectories).FirstOrDefault(); - if (TryOpen(file)) return; + candidates.Add(file); } - // Fall back to more aggresive brute search - var rootDir = Directory.GetParent(Application.dataPath); - if (rootDir.Exists) - { - // BepInEx 5.x log file - var result = rootDir.GetFiles("LogOutput.log", SearchOption.AllDirectories).FirstOrDefault(); - if (result == null) - result = rootDir.GetFiles("output_log.txt", SearchOption.AllDirectories).FirstOrDefault(); + var latestLog = candidates.Where(File.Exists).OrderByDescending(File.GetLastWriteTimeUtc).FirstOrDefault(); + if (TryOpen(latestLog)) return; - if (result != null && TryOpen(result.FullName)) return; - } + candidates.Clear(); + // Fall back to more aggresive brute search + // BepInEx 5.x log file, can be "LogOutput.log.1" or higher if multiple game instances run + candidates.AddRange(Directory.GetFiles(rootDir,"LogOutput.log*", SearchOption.AllDirectories)); + candidates.AddRange(Directory.GetFiles(rootDir,"output_log.txt", SearchOption.AllDirectories)); + latestLog = candidates.Where(File.Exists).OrderByDescending(File.GetLastWriteTimeUtc).FirstOrDefault(); + if (TryOpen(latestLog)) return; throw new FileNotFoundException("No log files were found"); }