From cd2a44fff7eb55428865cb94eb2b8881cd5a9a28 Mon Sep 17 00:00:00 2001 From: Eddie Zato Date: Sun, 18 Feb 2018 22:33:12 +0700 Subject: [PATCH] fix timer GC, improve quantity handler --- DS3SavesBackup/Program.cs | 48 ++++++++++------------- DS3SavesBackup/Properties/AssemblyInfo.cs | 4 +- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/DS3SavesBackup/Program.cs b/DS3SavesBackup/Program.cs index 4f7cf73..2227f66 100644 --- a/DS3SavesBackup/Program.cs +++ b/DS3SavesBackup/Program.cs @@ -1,10 +1,8 @@ using System; -using System.Collections.Generic; using System.Configuration; using System.Diagnostics; using System.IO; using System.IO.Compression; -using System.Linq; using System.Threading; namespace DS3SavesBackup @@ -12,6 +10,10 @@ namespace DS3SavesBackup class Program { private static string[] localized; + private static string savesPath, backupPath; + private static int interval, maxBackup; + + private static Timer mTimer; private static string nowToString() { @@ -40,45 +42,44 @@ private static void saveConfig(string appKey, string appValue) configuration.Save(ConfigurationSaveMode.Modified); } - private static void mainLoop(string saves_path, string backup_path, int maxbkup, List backupList) + private static void mainLoop() { Process[] ds3Process = Process.GetProcessesByName("DarkSoulsIII"); if (ds3Process.Length > 0) { - if (Directory.GetFiles(saves_path).Length == 0) - { - messWrite(localized[10], true); - } - else + if (Directory.GetFiles(savesPath).Length > 0) { messWrite(localized[6] + "...", false); - if (backupList.Count == maxbkup) + + string[] backupFiles = Directory.GetFiles(backupPath, "ds3*.zip", SearchOption.TopDirectoryOnly); + if (backupFiles.Length >= maxBackup) { try { - File.Delete(backupList.First()); + for (int i = 0; i < backupFiles.Length - maxBackup + 1; i++) + File.Delete(backupFiles[i]); } catch (Exception e) { messWrite(e.ToString(), true); - Environment.Exit(0); } - backupList.RemoveAt(0); - } - string backup = Path.Combine(backup_path, "ds3_" + nowToString() + ".zip"); - backupList.Add(backup); + } + string backup = Path.Combine(backupPath, "ds3_" + nowToString() + ".zip"); try { - ZipFile.CreateFromDirectory(saves_path, backup, CompressionLevel.Fastest, false); + ZipFile.CreateFromDirectory(savesPath, backup, CompressionLevel.Fastest, false); } catch (Exception e) { messWrite(e.ToString(), true); - Environment.Exit(0); } Console.WriteLine(" " + localized[7] + ": " + Path.GetFileName(backup)); } + else + { + messWrite(localized[10], true); + } } else { @@ -130,10 +131,7 @@ static void Main(string[] args) Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine(localized[0] + "\n"); Console.ResetColor(); - - string savesPath, backupPath; - int interval, maxBackup; - + try { interval = Convert.ToInt32(ConfigurationManager.AppSettings["auto_backup_interval_min"]); @@ -207,13 +205,7 @@ static void Main(string[] args) Console.WriteLine(localized[5] + ": " + maxBackup.ToString() + "\n", true); Console.ResetColor(); - List backupList = new List(); - foreach (string filename in Directory.GetFiles(backupPath, "ds3*.zip", SearchOption.TopDirectoryOnly)) - { - backupList.Add(filename); - } - - Timer mTimer = new Timer((e) => { mainLoop(savesPath, backupPath, maxBackup, backupList); }, null, 0, interval * 1000 * 60); + mTimer = new Timer((z) => { mainLoop(); }, null, 0, interval * 60000); while (Console.ReadKey(true).Key != ConsoleKey.Q) { } } } diff --git a/DS3SavesBackup/Properties/AssemblyInfo.cs b/DS3SavesBackup/Properties/AssemblyInfo.cs index 30c55b1..ba476d1 100644 --- a/DS3SavesBackup/Properties/AssemblyInfo.cs +++ b/DS3SavesBackup/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("1.0.1.0")] +[assembly: AssemblyFileVersion("1.0.1.0")]