diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index f8e76f7..8accd3d 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -296,11 +296,14 @@ private void loadBackups() string[] worldBackups = Directory.GetDirectories(backupDirPath + "\\worlds"); foreach (string w in worldBackups) { - string name = w.Replace($@"{backupDirPath}\worlds", ""); + //string name = w.Replace($@"{backupDirPath}\worlds", ""); string[] backupDirs = Directory.GetDirectories(w); foreach (string backupDir in backupDirs) { - SaveBackup backup = new SaveBackup($@"{backupDir}\{name}.db"); + string[] files = System.IO.Directory.GetFiles(backupDir, "*.db"); + if (files.Length < 1) continue; + var name = new FileInfo(files[0]).Name; + SaveBackup backup = new SaveBackup($@"{backupDir}\{name}"); if (backupWorldNames.ContainsKey(backup.SaveDate.Ticks)) { backup.Label = backupWorldNames[backup.SaveDate.Ticks]; @@ -321,11 +324,14 @@ private void loadBackups() string[] charBackups = Directory.GetDirectories($@"{backupDirPath}\characters"); foreach (string c in charBackups) { - string name = c.Replace($@"{backupDirPath}\characters", ""); + //string name = c.Replace($@"{backupDirPath}\characters", ""); string[] backupDirs = Directory.GetDirectories(c); foreach (string backupDir in backupDirs) { - SaveBackup backup = new SaveBackup($@"{backupDir}\{name}.fch"); + string[] files = System.IO.Directory.GetFiles(backupDir, "*.fch"); + if (files.Length < 1) continue; + var name = new FileInfo(files[0]).Name; + SaveBackup backup = new SaveBackup($@"{backupDir}\{name}"); if (backupCharNames.ContainsKey(backup.SaveDate.Ticks)) { backup.Label = backupCharNames[backup.SaveDate.Ticks]; @@ -343,10 +349,6 @@ private void loadBackups() //listBackups.Sort(); listBackups = listBackups.OrderByDescending(x => x.SaveDate).ToList(); dataBackups.ItemsSource = listBackups; - if (listBackups.Count > 0) - { - //logMessage("Last backup save date: " + listBackups[listBackups.Count - 1].SaveDate.ToString()); - } } catch (Exception ex) { @@ -1000,14 +1002,6 @@ private void BtnBackupFolder_Click(object sender, RoutedEventArgs e) List backupFolders = Directory.GetDirectories(backupDirPath).ToList(); foreach (string file in backupFolders) { - /*string subFolderName = file.Substring(file.LastIndexOf(@"\")); - Directory.CreateDirectory(folderName + subFolderName); - Directory.SetCreationTime(folderName + subFolderName, Directory.GetCreationTime(file)); - Directory.SetLastWriteTime(folderName + subFolderName, Directory.GetCreationTime(file)); - foreach (string filename in Directory.GetFiles(file)) - { - File.Copy(filename, filename.Replace(backupDirPath, folderName)); - }*/ Directory.Delete(file, true); } } @@ -1091,14 +1085,6 @@ private void btnFtpImport_Click(object sender, RoutedEventArgs e) syncDirectoriesAsync(); } } - // Only do something if user clicks OK - /*if (GetFtpSettings()) - { - if (ftpDirectorySync == null) - { - syncDirectoriesAsync(); - } - }*/ } private void syncDirectoriesAsync() @@ -1287,10 +1273,6 @@ private void menuSavePathAdd_Click(object sender, RoutedEventArgs e) } if (!Directory.Exists($@"{folderName}\worlds")) { - /*ModernMessageBox mmbWarn = new ModernMessageBox(this); - mmbWarn.Show("Please select the folder where your Valheim save files are located. This folder should contain both a \"worlds\" and a \"characters\" folder.", - "Invalid Folder", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK); - return;*/ Directory.CreateDirectory($@"{folderName}\worlds"); logMessage($"{folderName} did not contain a \"worlds\" folder, so it may not be a valid save location."); } @@ -1300,8 +1282,6 @@ private void menuSavePathAdd_Click(object sender, RoutedEventArgs e) } lstSaveFolders.Items.Add(folderName); lblSaveFolders.Content = "Save Folders"; - //var watcher = new SaveWatcher(folderName); - //watcher.LogMessage += SaveWatcher_LogMessage; AddToSaveWatchers(folderName); Properties.Settings.Default.SaveFolders.Add(folderName); @@ -1335,10 +1315,6 @@ private void menuSavePathEdit_Click(object sender, RoutedEventArgs e) } if (!Directory.Exists($@"{folderName}\worlds")) { - /*ModernMessageBox mmbWarn = new ModernMessageBox(this); - mmbWarn.Show("Please select the folder where your Valheim save files are located. This folder should contain both a \"worlds\" and a \"characters\" folder.", - "Invalid Folder", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK); - return;*/ Directory.CreateDirectory($@"{folderName}\worlds"); logMessage($"{folderName} did not contain a \"worlds\" folder, so it may not be a valid save location."); } @@ -1356,8 +1332,6 @@ private void menuSavePathEdit_Click(object sender, RoutedEventArgs e) break; } } - //var watcher = new SaveWatcher(folderName); - //watcher.LogMessage += SaveWatcher_LogMessage; AddToSaveWatchers(folderName); Properties.Settings.Default.SaveFolders.Remove(saveDirPath); Properties.Settings.Default.SaveFolders.Add(folderName); @@ -1401,13 +1375,6 @@ private void menuSavePathRemove_Click(object sender, RoutedEventArgs e) Properties.Settings.Default.Save(); } - private void MenuRestorePath_Click(object sender, RoutedEventArgs e) - { - SaveBackup selectedBackup = (SaveBackup)dataBackups.SelectedItem; - MenuItem menu = (MenuItem)sender; - restoreBackup(selectedBackup, menu.Tag.ToString()); - } - private void menuBackups_Opened(object sender, RoutedEventArgs e) { if (dataBackups.SelectedIndex == -1) diff --git a/SaveBackup.cs b/SaveBackup.cs index 12acd48..4eda710 100644 --- a/SaveBackup.cs +++ b/SaveBackup.cs @@ -12,49 +12,47 @@ namespace ValheimSaveShield { public class SaveBackup : IEditableObject, IComparable { - struct SaveData + struct BackupData { - internal string savePath; + internal string backupPath; internal string label; //internal string type; - internal DateTime date; internal bool keep; } public event EventHandler Updated; - private SaveData saveData; - private SaveData backupData; + private BackupData backupData; + private BackupData backupDataBackup; private bool inTxn = false; public string Label { get { - if (this.saveData.label == "" || this.saveData.label == null) + if (this.backupData.label == "" || this.backupData.label == null) { return this.DefaultLabel; } else { - return this.saveData.label; + return this.backupData.label; } } set { if (value == "" || value == null) { - this.saveData.label = this.DefaultLabel; + this.backupData.label = this.DefaultLabel; } else { - this.saveData.label = value; + this.backupData.label = value; } - //OnUpdated(new UpdatedEventArgs("Name")); } } public string Name { get { - var fileName = new FileInfo(this.saveData.savePath).Name; + var fileName = new FileInfo(this.backupData.backupPath).Name; var parts = new ArrayList(FileName.Split('.')); parts.RemoveAt(parts.Count - 1); return string.Join(".", parts.ToArray()).Trim(); @@ -65,7 +63,7 @@ public string Type { get { - if (new FileInfo(this.saveData.savePath).Directory.FullName.StartsWith($@"{Properties.Settings.Default.BackupFolder}\worlds\")) + if (new FileInfo(this.backupData.backupPath).Directory.FullName.StartsWith($@"{Properties.Settings.Default.BackupFolder}\worlds\")) { return "World"; } @@ -73,69 +71,52 @@ public string Type { return "Character"; } - //return this.saveData.type; } - /*set - { - this.saveData.type = value; - }*/ } public string DefaultLabel { get { - return this.Name + " " + Math.Abs(this.saveData.date.Ticks % 10000); + return this.Name + " " + Math.Abs(this.SaveDate.Ticks % 10000); } } public DateTime SaveDate { get { - return this.saveData.date; - } - set - { - this.saveData.date = value; - //OnUpdated(new UpdatedEventArgs("SaveDate")); + return File.GetLastWriteTime(this.FullPath); } } public bool Keep { get { - return this.saveData.keep; + return this.backupData.keep; } set { - this.saveData.keep = value; - //OnUpdated(new UpdatedEventArgs("Keep")); + this.backupData.keep = value; } } public bool Active { get { - //string activePath = this.ActivePath; foreach (var activePath in ActivePaths) { - if (File.Exists(activePath) && File.GetLastWriteTime(activePath).Ticks == this.SaveDateTime.Ticks) + if (File.Exists(activePath) && File.GetLastWriteTime(activePath).Ticks == this.SaveDate.Ticks) { return true; } } return false; - }/* - set - { - this.saveData.active = value; - //OnUpdated(new UpdatedEventArgs("Active")); - }*/ + } } public string FileName { get { - return new FileInfo(this.saveData.savePath).Name; + return new FileInfo(this.backupData.backupPath).Name; } } @@ -143,7 +124,7 @@ public string FullPath { get { - return this.saveData.savePath; + return this.backupData.backupPath; } } @@ -168,25 +149,15 @@ public List ActivePaths } } - private DateTime SaveDateTime - { - get - { - return File.GetLastWriteTime(this.saveData.savePath); - } - } - - //public SaveBackup(DateTime saveDate) - public SaveBackup(string savePath) + public SaveBackup(string backupPath) { - if (this.saveData.Equals(default(SaveData))) + if (this.backupData.Equals(default(BackupData))) { - //this.saveData = new SaveData(); - this.saveData.label = ""; + //this.backupData = new SaveData(); + this.backupData.label = ""; } - this.saveData.savePath = savePath; - this.saveData.date = this.SaveDateTime; - this.saveData.keep = false; + this.backupData.backupPath = backupPath; + this.backupData.keep = false; } @@ -227,7 +198,7 @@ void IEditableObject.BeginEdit() { if (!inTxn) { - this.backupData = saveData; + this.backupDataBackup = backupData; inTxn = true; } } @@ -236,7 +207,7 @@ void IEditableObject.CancelEdit() { if (inTxn) { - this.saveData = backupData; + this.backupData = backupDataBackup; inTxn = false; } } @@ -245,23 +216,15 @@ void IEditableObject.EndEdit() { if (inTxn) { - if (backupData.label !=saveData.label) + if (backupDataBackup.label != backupData.label) { OnUpdated(new UpdatedEventArgs("Label")); } - if (backupData.date != saveData.date) - { - OnUpdated(new UpdatedEventArgs("SaveDate")); - } - if (backupData.keep != saveData.keep) + if (backupDataBackup.keep != backupData.keep) { OnUpdated(new UpdatedEventArgs("Keep")); } - /*if (!backupData.active.Equals(saveData.active)) - { - OnUpdated(new UpdatedEventArgs("Active")); - }*/ - backupData = new SaveData(); + backupDataBackup = new BackupData(); inTxn = false; } } diff --git a/SaveFile.cs b/SaveFile.cs index 9031d3c..2549780 100644 --- a/SaveFile.cs +++ b/SaveFile.cs @@ -72,7 +72,7 @@ public string BackupsPath } } - public string BackupPath + public string FullBackupPath { get { @@ -92,7 +92,7 @@ public bool BackedUp { get { - return File.Exists(this.BackupPath); + return File.Exists(this.FullBackupPath); } } @@ -143,7 +143,7 @@ public SaveBackup PerformBackup() { Directory.CreateDirectory(backupFolder); } - File.Copy(this.FullPath, this.BackupPath, true); + File.Copy(this.FullPath, this.FullBackupPath, true); if (this.Type.Equals("World")) { FileInfo info = new FileInfo(this.FullPath); @@ -160,7 +160,7 @@ public SaveBackup PerformBackup() } } } - return new SaveBackup(this.BackupPath); + return new SaveBackup(this.FullBackupPath); } catch (IOException ex) {