Skip to content

Commit

Permalink
Added option to restore worlds to the remote FTP sync folder.
Browse files Browse the repository at this point in the history
  • Loading branch information
Razzmatazzz committed Mar 6, 2021
1 parent 50cbc0c commit 2d10229
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 3 deletions.
39 changes: 38 additions & 1 deletion MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand All @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
22 changes: 22 additions & 0 deletions SaveBackup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
39 changes: 39 additions & 0 deletions SynchronizeDirectories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}

0 comments on commit 2d10229

Please sign in to comment.