From b72b856fbe8088d8e6b7eb0d041a71f653b7c7a5 Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Sun, 29 Jul 2018 16:21:11 -0500 Subject: [PATCH] - Updated IPA to 4.6 for Beat Saber 0.11 - Updated Logger to use more streamlined queueing that that allows --- IPA.Tests/IPA.Tests.csproj | 2 +- IPA/IPA.csproj | 8 +- IPA/app.config | 3 + .../Debug/IPA.csproj.CoreCompileInputs.cache | 2 +- IPA/packages.config | 2 +- IllusionInjector/IllusionInjector.csproj | 6 +- ...ionInjector.csproj.CoreCompileInputs.cache | 2 +- IllusionPlugin/IllusionPlugin.csproj | 6 +- IllusionPlugin/Logger.cs | 112 +++++++++++------- ...usionPlugin.csproj.CoreCompileInputs.cache | 2 +- 10 files changed, 90 insertions(+), 55 deletions(-) create mode 100644 IPA/app.config diff --git a/IPA.Tests/IPA.Tests.csproj b/IPA.Tests/IPA.Tests.csproj index 43612e6d..d4919d23 100644 --- a/IPA.Tests/IPA.Tests.csproj +++ b/IPA.Tests/IPA.Tests.csproj @@ -10,7 +10,7 @@ Properties IPA.Tests IPA.Tests - v4.5.2 + v4.6.2 512 diff --git a/IPA/IPA.csproj b/IPA/IPA.csproj index c4b5e3e9..e3ce2d01 100644 --- a/IPA/IPA.csproj +++ b/IPA/IPA.csproj @@ -9,9 +9,10 @@ Properties IPA IPA - v3.5 + v4.6.2 512 - Client + + AnyCPU @@ -22,6 +23,7 @@ DEBUG;TRACE prompt 4 + false AnyCPU @@ -31,6 +33,7 @@ TRACE prompt 4 + false favicon.ico @@ -71,6 +74,7 @@ + diff --git a/IPA/app.config b/IPA/app.config new file mode 100644 index 00000000..2a0024f7 --- /dev/null +++ b/IPA/app.config @@ -0,0 +1,3 @@ + + + diff --git a/IPA/obj/Debug/IPA.csproj.CoreCompileInputs.cache b/IPA/obj/Debug/IPA.csproj.CoreCompileInputs.cache index f3c8d81c..bb31db24 100644 --- a/IPA/obj/Debug/IPA.csproj.CoreCompileInputs.cache +++ b/IPA/obj/Debug/IPA.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -65c33cfc23e8dcfc89c7fd78e8cd8344a2518294 +6744afebdfdc05ced46858bdac21e047b0d6e43f diff --git a/IPA/packages.config b/IPA/packages.config index bc985110..74fdd86f 100644 --- a/IPA/packages.config +++ b/IPA/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file diff --git a/IllusionInjector/IllusionInjector.csproj b/IllusionInjector/IllusionInjector.csproj index aeb8f75b..43c2ecff 100644 --- a/IllusionInjector/IllusionInjector.csproj +++ b/IllusionInjector/IllusionInjector.csproj @@ -1,5 +1,5 @@  - + Debug @@ -9,7 +9,7 @@ Properties IllusionInjector IllusionInjector - v3.5 + v4.6 512 @@ -22,6 +22,7 @@ DEBUG;TRACE prompt 4 + false none @@ -30,6 +31,7 @@ TRACE prompt 4 + false diff --git a/IllusionInjector/obj/Debug/IllusionInjector.csproj.CoreCompileInputs.cache b/IllusionInjector/obj/Debug/IllusionInjector.csproj.CoreCompileInputs.cache index 7e88f8ec..2a3e91d1 100644 --- a/IllusionInjector/obj/Debug/IllusionInjector.csproj.CoreCompileInputs.cache +++ b/IllusionInjector/obj/Debug/IllusionInjector.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -c4f9eceab04df8633c0ae4f922a37f459ec45217 +e3db2f4e6085eb1ed3ec468191bb328ebb0f8b7f diff --git a/IllusionPlugin/IllusionPlugin.csproj b/IllusionPlugin/IllusionPlugin.csproj index 29046e9a..91d11847 100644 --- a/IllusionPlugin/IllusionPlugin.csproj +++ b/IllusionPlugin/IllusionPlugin.csproj @@ -1,5 +1,5 @@  - + Debug @@ -9,7 +9,7 @@ Properties IllusionPlugin IllusionPlugin - v3.5 + v4.6 512 @@ -22,6 +22,7 @@ DEBUG;TRACE prompt 4 + false none @@ -31,6 +32,7 @@ prompt 4 bin\Release\IllusionPlugin.XML + false diff --git a/IllusionPlugin/Logger.cs b/IllusionPlugin/Logger.cs index 8a7ed970..3cb26e1b 100644 --- a/IllusionPlugin/Logger.cs +++ b/IllusionPlugin/Logger.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Concurrent; using System.IO; using System.Runtime.Remoting.Messaging; using System.Threading; @@ -7,96 +8,119 @@ using IllusionPlugin; namespace IllusionPlugin { public class Logger { - private readonly Queue _logQueue; + private static BlockingCollection _logQueue; + private static Thread _watcherThread; + private static bool _threadRunning; private readonly FileInfo _logFile; - private readonly Thread _watcherThread; - private bool _threadRunning; private string ModName; - - private logMessage oldLog; struct logMessage { + public static readonly string logFormat = "[{3} @ {2:HH:mm:ss} | {1}] {0}"; + public WarningLevel WarningLevel; + public DateTime Time; + public Logger Log; public string Message; - public logMessage(string msg, WarningLevel wl) { + public logMessage(string msg, Logger log, DateTime time, WarningLevel wl) { Message = msg; WarningLevel = wl; + Log = log; + Time = time; } } enum WarningLevel { Log, Error, Exception, Warning } - + + static void SetupStatic() + { + if (_logQueue == null) + _logQueue = new BlockingCollection(); + if (_watcherThread == null || !_watcherThread.IsAlive) + { + _watcherThread = new Thread(QueueWatcher); // { IsBackground = true }; + _threadRunning = true; + _watcherThread.Start(); + } + } + public Logger(string modName = "Default") { - _logQueue = new Queue(); + SetupStatic(); _logFile = GetPath(modName); - _watcherThread = new Thread(QueueWatcher) {IsBackground = true}; - _threadRunning = true; - Start(); + _logFile.Create().Close(); } - - public Logger(IPlugin plugin) { - _logQueue = new Queue(); + + public Logger(IPlugin plugin) + { + SetupStatic(); _logFile = GetPath(plugin); - _watcherThread = new Thread(QueueWatcher) {IsBackground = true}; - _threadRunning = true; - Start(); + _logFile.Create().Close(); } public void Log(string msg) { if(!_watcherThread.IsAlive) throw new Exception("Logger is Closed!"); - _logQueue.Enqueue(new logMessage($"[LOG @ {DateTime.Now:HH:mm:ss} | {ModName}] {msg}", WarningLevel.Log)); + //_logQueue.Add(new logMessage($"[LOG @ {DateTime.Now:HH:mm:ss} | {ModName}] {msg}", WarningLevel.Log)); + _logQueue.Add(new logMessage(msg, this, DateTime.Now, WarningLevel.Log)); } public void Error(string msg) { if(!_watcherThread.IsAlive) throw new Exception("Logger is Closed!"); - _logQueue.Enqueue(new logMessage($"[ERROR @ {DateTime.Now:HH:mm:ss} | {ModName}] {msg}", WarningLevel.Error)); + //_logQueue.Add(new logMessage($"[ERROR @ {DateTime.Now:HH:mm:ss} | {ModName}] {msg}", WarningLevel.Error)); + _logQueue.Add(new logMessage(msg, this, DateTime.Now, WarningLevel.Error)); } public void Exception(string msg) { if(!_watcherThread.IsAlive) throw new Exception("Logger is Closed!"); - _logQueue.Enqueue(new logMessage($"[EXCEPTION @ {DateTime.Now:HH:mm:ss} | {ModName}] {msg}", WarningLevel.Exception)); + //_logQueue.Add(new logMessage($"[EXCEPTION @ {DateTime.Now:HH:mm:ss} | {ModName}] {msg}", WarningLevel.Exception)); + _logQueue.Add(new logMessage(msg, this, DateTime.Now, WarningLevel.Exception)); } public void Warning(string msg) { if(!_watcherThread.IsAlive) throw new Exception("Logger is Closed!"); - _logQueue.Enqueue(new logMessage($"[WARNING @ {DateTime.Now:HH:mm:ss} | {ModName}] {msg}", WarningLevel.Warning)); + //_logQueue.Add(new logMessage($"[WARNING @ {DateTime.Now:HH:mm:ss} | {ModName}] {msg}", WarningLevel.Warning)); + _logQueue.Add(new logMessage(msg, this, DateTime.Now, WarningLevel.Warning)); } - void QueueWatcher() { - _logFile.Create().Close(); - while (_threadRunning) { - if (_logQueue.Count > 0) { - _watcherThread.IsBackground = false; - using (var f = _logFile.AppendText()) { - while (_logQueue.Count > 0) { - var d = _logQueue.Dequeue(); - if (d.Message == oldLog.Message) return; - oldLog = d; - f.WriteLine(d.Message); - Console.ForegroundColor = GetConsoleColour(d.WarningLevel); - Console.WriteLine(d.Message); - Console.ResetColor(); - } - } + static void QueueWatcher() { + //StreamWriter wstream = null; + Dictionary wstreams = new Dictionary(); + while (_threadRunning && _logQueue.TryTake(out logMessage message, Timeout.Infinite)) + { + string msg = string.Format(logMessage.logFormat, message.Message, message.Log.ModName, message.Time, message.WarningLevel); + + wstreams[message.Log.ModName] = message.Log._logFile.AppendText(); + wstreams[message.Log.ModName].WriteLine(msg); + Console.ForegroundColor = GetConsoleColour(message.WarningLevel); + Console.WriteLine(message.Message); + Console.ResetColor(); - _watcherThread.IsBackground = true; + if (_logQueue.Count == 0) + { // no more messages + foreach (var kvp in wstreams) + { + if (kvp.Value == null) continue; + kvp.Value.Dispose(); + wstreams[kvp.Key] = null; + } } - Thread.Sleep(5); } - } - void Start() => _watcherThread.Start(); + foreach (var kvp in wstreams) + { + if (kvp.Value == null) continue; + kvp.Value.Dispose(); + } + } - public void Stop() { + public static void Stop() { _threadRunning = false; _watcherThread.Join(); } - ConsoleColor GetConsoleColour(WarningLevel level) { + static ConsoleColor GetConsoleColour(WarningLevel level) { switch (level) { case WarningLevel.Log: return ConsoleColor.Green; @@ -120,7 +144,7 @@ namespace IllusionPlugin { } } - public static class DebugExtensions { + public static class LoggerExtensions { public static Logger GetLogger(this IPlugin plugin) { return new Logger(plugin); } diff --git a/IllusionPlugin/obj/Debug/IllusionPlugin.csproj.CoreCompileInputs.cache b/IllusionPlugin/obj/Debug/IllusionPlugin.csproj.CoreCompileInputs.cache index 77da6a5d..907a8434 100644 --- a/IllusionPlugin/obj/Debug/IllusionPlugin.csproj.CoreCompileInputs.cache +++ b/IllusionPlugin/obj/Debug/IllusionPlugin.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -de4d7d5be2f255b6eb5fe16bec001496ad0c8963 +3916871581af94648b18c3cc477ec3f5b61ffa03