diff --git a/Ninjacrab.PersistentWindows.Solution/Common/Common.csproj b/Ninjacrab.PersistentWindows.Solution/Common/Common.csproj index 7d1ee22..2f3534a 100644 --- a/Ninjacrab.PersistentWindows.Solution/Common/Common.csproj +++ b/Ninjacrab.PersistentWindows.Solution/Common/Common.csproj @@ -36,6 +36,7 @@ + diff --git a/Ninjacrab.PersistentWindows.Solution/Common/PersistentWindowProcessor.cs b/Ninjacrab.PersistentWindows.Solution/Common/PersistentWindowProcessor.cs index 499b3fc..fc83f98 100644 --- a/Ninjacrab.PersistentWindows.Solution/Common/PersistentWindowProcessor.cs +++ b/Ninjacrab.PersistentWindows.Solution/Common/PersistentWindowProcessor.cs @@ -9,6 +9,8 @@ using System.Threading; using System.Diagnostics; using System.Reflection; using Microsoft.Win32; +using System.Xml; +using System.Runtime.Serialization; using LiteDB; @@ -135,6 +137,9 @@ namespace PersistentWindows.Common "chrome", "firefox", "msedge", "vivaldi", "opera", "brave", "360ChromeX" }; + private string windowPosDataFile = "window_pos.xml"; + private string snapshotTimeFile = "snapshot_time.xml"; + private HashSet ignoreProcess = new HashSet(); private HashSet debugProcess = new HashSet(); private HashSet debugWindows = new HashSet(); @@ -197,6 +202,59 @@ namespace PersistentWindows.Common ; } #endif + + public void WriteDataDump() + { + DataContractSerializer dcs = new DataContractSerializer(typeof(Dictionary>>)); + StringBuilder sb = new StringBuilder(); + using (XmlWriter xw = XmlWriter.Create(sb)) + { + dcs.WriteObject(xw, monitorApplications); + } + string xml = sb.ToString(); + File.WriteAllText(Path.Combine(appDataFolder, windowPosDataFile), xml, Encoding.Unicode); + + DataContractSerializer dcs2 = new DataContractSerializer(typeof(Dictionary>)); + StringBuilder sb2 = new StringBuilder(); + using (XmlWriter xw = XmlWriter.Create(sb2)) + { + dcs2.WriteObject(xw, snapshotTakenTime); + } + string xml2 = sb2.ToString(); + File.WriteAllText(Path.Combine(appDataFolder, snapshotTimeFile), xml2, Encoding.Unicode); + } + + private void ReadDataDump() + { + DataContractSerializer dcs = new DataContractSerializer(typeof(Dictionary>>)); + using (FileStream fs = File.OpenRead(Path.Combine(appDataFolder, windowPosDataFile))) + using (XmlReader xr = XmlReader.Create(fs)) + { + monitorApplications = (Dictionary>>)dcs.ReadObject(xr); + } + File.Delete(Path.Combine(appDataFolder, windowPosDataFile)); + + DataContractSerializer dcs2 = new DataContractSerializer(typeof(Dictionary>)); + using (FileStream fs = File.OpenRead(Path.Combine(appDataFolder, snapshotTimeFile))) + using (XmlReader xr = XmlReader.Create(fs)) + { + snapshotTakenTime = (Dictionary>)dcs2.ReadObject(xr); + } + File.Delete(Path.Combine(appDataFolder, snapshotTimeFile)); + } + + private void ReadDataDumpSafe() + { + try + { + ReadDataDump(); + } + catch (Exception e) + { + Log.Error(e.ToString()); + } + } + private void CleanupDisplayRegKey(string key) { if (key.Contains("__")) @@ -312,6 +370,7 @@ namespace PersistentWindows.Common } } + ReadDataDumpSafe(); curDisplayKey = GetDisplayKey(); CaptureNewDisplayConfig(curDisplayKey); @@ -795,6 +854,8 @@ namespace PersistentWindows.Common initialized = true; remoteSession = System.Windows.Forms.SystemInformation.TerminalServerSession; + bool sshot_exist = snapshotTakenTime.ContainsKey(curDisplayKey); + enableRestoreSnapshotMenu(sshot_exist); Log.Event($"Display config is {curDisplayKey}"); using (var persistDB = new LiteDatabase(persistDbName)) { diff --git a/Ninjacrab.PersistentWindows.Solution/SystrayShell/Program.cs b/Ninjacrab.PersistentWindows.Solution/SystrayShell/Program.cs index 1104c88..6eb3f4c 100644 --- a/Ninjacrab.PersistentWindows.Solution/SystrayShell/Program.cs +++ b/Ninjacrab.PersistentWindows.Solution/SystrayShell/Program.cs @@ -774,5 +774,16 @@ namespace PersistentWindows.SystrayShell Log.Error(format, args); } + public static void WriteDataDump() + { + try + { + pwp.WriteDataDump(); + } + catch (Exception e) + { + Log.Error(e.ToString()); + } + } } } diff --git a/Ninjacrab.PersistentWindows.Solution/SystrayShell/SystrayForm.cs b/Ninjacrab.PersistentWindows.Solution/SystrayShell/SystrayForm.cs index 120c2d8..5deeff2 100644 --- a/Ninjacrab.PersistentWindows.Solution/SystrayShell/SystrayForm.cs +++ b/Ninjacrab.PersistentWindows.Solution/SystrayShell/SystrayForm.cs @@ -172,7 +172,6 @@ namespace PersistentWindows.SystrayShell ctrlKeyPressed = 0; shiftKeyPressed = 0; altKeyPressed = 0; - } //private void TimerEventProcessor(Object myObject, EventArgs myEventArgs) @@ -281,6 +280,8 @@ namespace PersistentWindows.SystrayShell private void Exit() { + Program.WriteDataDump(); + #if DEBUG this.notifyIconMain.Visible = false; #endif @@ -288,8 +289,11 @@ namespace PersistentWindows.SystrayShell Log.Exit(); Application.Exit(); } + private void Upgrade() { + Program.WriteDataDump(); + string batFile = Path.Combine(Program.AppdataFolder, "pw_upgrade.bat"); Process.Start(batFile); Exit();