From 2d10229e4b2ad1ce45e69283487e72db6f703dad Mon Sep 17 00:00:00 2001 From: Razzmatazz Date: Sat, 6 Mar 2021 08:27:33 -0600 Subject: [PATCH] Added option to restore worlds to the remote FTP sync folder. --- MainWindow.xaml.cs | 39 +++++++++++++++++++++++++++++++++++++- Properties/AssemblyInfo.cs | 4 ++-- SaveBackup.cs | 22 +++++++++++++++++++++ SynchronizeDirectories.cs | 39 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 3 deletions(-) diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 608c2c3..d79b16f 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -1087,6 +1087,17 @@ private void btnFtpImport_Click(object sender, RoutedEventArgs e) } } + private bool ftpSyncEnabled() + { + return !(Properties.Settings.Default.FtpIpAddress.Length == 0 + || Properties.Settings.Default.FtpPort.Length == 0 + || Properties.Settings.Default.FtpFilePath.Length == 0 + || Properties.Settings.Default.FtpSaveDest.Length == 0 + || Properties.Settings.Default.FtpUsername.Length == 0 + || Properties.Settings.Default.FtpPassword.Length == 0 + ); + } + private void syncDirectoriesAsync() { @@ -1388,7 +1399,7 @@ private void menuBackups_Opened(object sender, RoutedEventArgs e) SaveBackup selectedBackup = (SaveBackup)dataBackups.SelectedItem; menuBackupsRestore.Click -= menuBackupsRestore_Click; menuBackupsRestore.Items.Clear(); - if (Properties.Settings.Default.SaveFolders.Count < 2) + if (Properties.Settings.Default.SaveFolders.Count < 2 && (!ftpSyncEnabled() || selectedBackup.Type != "World")) { if (!File.Exists(selectedBackup.ActivePaths.First()) || File.GetLastWriteTime(selectedBackup.ActivePaths.First()) != selectedBackup.SaveDate) { @@ -1417,6 +1428,14 @@ private void menuBackups_Opened(object sender, RoutedEventArgs e) menuBackupsRestore.Items.Add(menu); } } + if (ftpSyncEnabled() && selectedBackup.Type == "World") + { + MenuItem menu = new MenuItem(); + menu.Header = "ftp://" + Properties.Settings.Default.FtpIpAddress + ":" + Properties.Settings.Default.FtpPort + "/" + Properties.Settings.Default.FtpFilePath; + menu.ToolTip = "Restore to the remote FTP import location"; + menu.Click += menuFtpRestore_Click; + menuBackupsRestore.Items.Add(menu); + } if (menuBackupsRestore.Items.Count > 0) { menuBackupsRestore.IsEnabled = true; @@ -1438,6 +1457,24 @@ private void menuBackups_Opened(object sender, RoutedEventArgs e) } } + private void menuFtpRestore_Click(object sender, RoutedEventArgs e) + { + new Thread(() => + { + Thread.CurrentThread.IsBackground = true; + try + { + SaveBackup selectedBackup = (SaveBackup)dataBackups.SelectedItem; + selectedBackup.RestoreFtp(); + logMessage($"{selectedBackup.Name} backup restored to {"ftp://" + Properties.Settings.Default.FtpIpAddress + ":" + Properties.Settings.Default.FtpPort + "/" + Properties.Settings.Default.FtpFilePath}!", LogType.Success); + } + catch (Exception ex) + { + logMessage($"Error restoring save to FTP: {ex.Message}", LogType.Error); + } + }).Start(); + } + private void menuBackupsRestore_Click(object sender, RoutedEventArgs e) { if (isValheimRunning()) diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index b34e7f4..2ee4e87 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -51,5 +51,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("0.4.7.0")] -[assembly: AssemblyFileVersion("0.4.7.0")] +[assembly: AssemblyVersion("0.4.8.0")] +[assembly: AssemblyFileVersion("0.4.8.0")] diff --git a/SaveBackup.cs b/SaveBackup.cs index 4eda710..4d0108d 100644 --- a/SaveBackup.cs +++ b/SaveBackup.cs @@ -192,6 +192,28 @@ public void Restore(string path) } } } + public void RestoreFtp() + { + if (this.Type != "World") + { + throw new Exception("You can only restore world saves to the FTP location"); + } + SynchronizeDirectories.uploadFile(Properties.Settings.Default.FtpIpAddress, Properties.Settings.Default.FtpPort, Properties.Settings.Default.FtpFilePath, this.FullPath, Properties.Settings.Default.FtpUsername, Properties.Settings.Default.FtpPassword, (WinSCP.FtpMode)Properties.Settings.Default.FtpMode); + if (this.Type == "World") + { + FileInfo info = new FileInfo(this.FullPath); + string sourcefwl = info.DirectoryName + "\\" + this.Name + ".fwl"; + SynchronizeDirectories.uploadFile(Properties.Settings.Default.FtpIpAddress, Properties.Settings.Default.FtpPort, Properties.Settings.Default.FtpFilePath, sourcefwl, Properties.Settings.Default.FtpUsername, Properties.Settings.Default.FtpPassword, (WinSCP.FtpMode)Properties.Settings.Default.FtpMode); + foreach (var ext in Properties.Settings.Default.WorldFileExtensions) + { + string sourcefile = info.DirectoryName + "\\" + this.Name + ext; + if (File.Exists(sourcefile)) + { + SynchronizeDirectories.uploadFile(Properties.Settings.Default.FtpIpAddress, Properties.Settings.Default.FtpPort, Properties.Settings.Default.FtpFilePath, sourcefile, Properties.Settings.Default.FtpUsername, Properties.Settings.Default.FtpPassword, (WinSCP.FtpMode)Properties.Settings.Default.FtpMode); + } + } + } + } // Implements IEditableObject void IEditableObject.BeginEdit() diff --git a/SynchronizeDirectories.cs b/SynchronizeDirectories.cs index 69ba235..67f4ab9 100644 --- a/SynchronizeDirectories.cs +++ b/SynchronizeDirectories.cs @@ -106,5 +106,44 @@ private static void FileTransferred(object sender, TransferEventArgs e) "Timestamp of {0} kept with its default (current time)", e.Destination); } } + + public static void uploadFile(string hostUrl, string port, string hostDirectory, string localFile, string userName, string password, FtpMode ftpMode) + { + try + { + // Setup session options + SessionOptions sessionOptions = new SessionOptions + { + Protocol = Protocol.Ftp, + HostName = hostUrl, + PortNumber = Int32.Parse(port), + UserName = userName, + Password = password, + FtpMode = ftpMode + }; + + using (Session session = new Session()) + { + // Will continuously report progress of synchronization + session.FileTransferred += FileTransferred; + + // Connect + session.Open(sessionOptions); + + // Upload + var uploadResult = session.PutFileToDirectory(localFile, hostDirectory); + + // Throw on any error + if (uploadResult.Error != null) + { + throw uploadResult.Error; + } + } + } + catch (Exception e) + { + throw e; + } + } } } \ No newline at end of file