From 143ce3f2feaf69155bdb40c9ae8e332bc93e37de Mon Sep 17 00:00:00 2001 From: p0358 Date: Tue, 1 Nov 2022 23:36:07 +0100 Subject: [PATCH] Dump unsent pulses on quit and restore on next launch (closes #24) --- CodeStats/CodeStatsPackage.cs | 39 ++++++++++++++++++++++++++++++++++- CodeStats/ConfigFile.cs | 9 ++++++++ CodeStats/Pulse.cs | 6 ++++++ HISTORY.rst | 7 +++++++ 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/CodeStats/CodeStatsPackage.cs b/CodeStats/CodeStatsPackage.cs index c4734c2..ced3781 100644 --- a/CodeStats/CodeStatsPackage.cs +++ b/CodeStats/CodeStatsPackage.cs @@ -165,6 +165,34 @@ private static void InitializeAsync() ReportStats(); } + // Restore pulses previously dumped to file (if we had to shut down before being able to pulse them) + Task.Run(() => + { + try + { + var path = ConfigFile.GetUnsavedPulsesFilePath(); + if (File.Exists(path)) + { + var json = File.ReadAllText(path); + + var serializer = new JavaScriptSerializer(); + var pulses = serializer.Deserialize>(json); + foreach (var pulse in pulses) + { + Logger.Info("Restoring dumped pulse: " + serializer.Serialize(pulse)); + pulseQueue.Enqueue(pulse); + } + + UpdateStatusbar(); + File.Delete(path); + } + } + catch (Exception ex) + { + Logger.Error("Error trying to restore dumped pulses from file!", ex); + } + }); + Logger.Info(string.Format("Finished initializing Code::Stats v{0}", Constants.PluginVersion)); } catch (Exception ex) @@ -976,15 +1004,24 @@ internal static void PluginCleanUp() Logger.Debug("Dequeueing remaining queued pulses..."); var jsonSerializer = new JavaScriptSerializer(); Pulse result; + var unsavedPulses = new List(); while (pulseQueue.TryDequeue(out result)) { if (!result.isEmpty()) { + unsavedPulses.Add(result); string json = jsonSerializer.Serialize(result); Logger.Info("Unsaved pulse: " + json); - // TODO: dump them to file and restore } } + if (unsavedPulses.Count > 0) + { + // dump them to file + var path = ConfigFile.GetUnsavedPulsesFilePath(); + Logger.Info("Dumping " + unsavedPulses.Count + " unsaved pulses to file at: " + path); + string json = jsonSerializer.Serialize(unsavedPulses); + File.WriteAllText(path, json); + } Logger.Info("Plugin cleanup on shutdown finished"); Logger.FlushEverything(); diff --git a/CodeStats/ConfigFile.cs b/CodeStats/ConfigFile.cs index 77b90e9..4901472 100644 --- a/CodeStats/ConfigFile.cs +++ b/CodeStats/ConfigFile.cs @@ -181,5 +181,14 @@ public static string GetCustomExtensionMappingFilePath() return Path.Combine(configFilePath, "CodeStatsCustomExtensionMapping.json"); } + + public static string GetUnsavedPulsesFilePath() + { + StringBuilder sbConfigFilePath = new StringBuilder(Win32.MAX_PATH); + Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETPLUGINSCONFIGDIR, Win32.MAX_PATH, sbConfigFilePath); + string configFilePath = sbConfigFilePath.ToString(); + + return Path.Combine(configFilePath, "CodeStatsPendingPulses.json"); + } } } \ No newline at end of file diff --git a/CodeStats/Pulse.cs b/CodeStats/Pulse.cs index b41d45d..65d1494 100644 --- a/CodeStats/Pulse.cs +++ b/CodeStats/Pulse.cs @@ -8,6 +8,12 @@ internal class XpObj public string language { get; set; } public int xp { get; set; } + public XpObj() + { + language = "Plain text"; + xp = 0; + } + public XpObj(string lang) { language = lang; diff --git a/HISTORY.rst b/HISTORY.rst index 390081b..efe9e50 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -3,6 +3,13 @@ History ------- +1.1.1 (2022-11-01) +++++++++++++++++++ + +- Fixed version check message claiming your version is outdated if it was in fact newer (it was checking for simple equality) +- Pulses that were not possible to be sent (for example due to connection issues) will now be dumped to disk on Notepad++ close and later restored, instead of being discarded + + 1.1.0 (2022-10-20) ++++++++++++++++++