Skip to content

Commit

Permalink
Bug fixes related FTP sync.
Browse files Browse the repository at this point in the history
  • Loading branch information
Razzmatazzz committed Mar 23, 2021
1 parent 7574990 commit 3e53a79
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 42 deletions.
5 changes: 3 additions & 2 deletions FtpSettingsWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ public FtpSettingsWindow()

private void btnSave_Click(object sender, RoutedEventArgs e)
{
if (txtWorldsPath.Text.StartsWith("/"))
if (!txtWorldsPath.Text.StartsWith("/"))
{
txtWorldsPath.Text = txtWorldsPath.Text.Remove(0, 1);
//txtWorldsPath.Text = txtWorldsPath.Text.Remove(0, 1);
txtWorldsPath.Text = "/"+txtWorldsPath.Text;
}

if (txtWorldsPath.Text.EndsWith("/"))
Expand Down
20 changes: 12 additions & 8 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,14 @@ public MainWindow()
if (Properties.Settings.Default.UpgradeRequired)
{
Properties.Settings.Default.Upgrade();
logMessage($"Previous backup folder: {Properties.Settings.Default.GetPreviousVersion("BackupFolder")}");
//logMessage($"Previous backup folder: {Properties.Settings.Default.GetPreviousVersion("BackupFolder")}");
Properties.Settings.Default.UpgradeRequired = false;
if (!Properties.Settings.Default.FtpFilePath.StartsWith("/"))
{
Properties.Settings.Default.FtpFilePath = "/" + Properties.Settings.Default.FtpFilePath;
}
Properties.Settings.Default.Save();
logMessage($"Current backup folder: {Properties.Settings.Default.BackupFolder}");
//logMessage($"Current backup folder: {Properties.Settings.Default.BackupFolder}");
}
Width = Properties.Settings.Default.MainWindowWidth;
Height = Properties.Settings.Default.MainWindowHeight;
Expand Down Expand Up @@ -622,10 +626,10 @@ private void OnSaveFileChanged(object source, FileSystemEventArgs e)
{
try
{
if (e.FullPath.EndsWith(".old")) return;
if (Properties.Settings.Default.AutoBackup)
if (e.FullPath.EndsWith(".old") || !Properties.Settings.Default.AutoBackup) return;
SaveFile save = new SaveFile(e.FullPath);
if (!save.BackedUp)
{
SaveFile save = new SaveFile(e.FullPath);
if (!saveTimers.ContainsKey(e.FullPath))
{
var saveTimer = new SaveTimer(save);
Expand Down Expand Up @@ -670,7 +674,7 @@ private void OnSaveTimerElapsed(Object source, ElapsedEventArgs e)
{
this.IsBackupCurrent = false;
dataBackups.Items.Refresh();
TimeSpan span = (timer.Save.BackupDueTime - DateTime.Now);
TimeSpan span = (timer.Save.BackupDueTime - timer.Save.SaveTime);
logMessage($"Save change detected, but {span.Minutes + Math.Round(span.Seconds / 60.0, 2)} minutes left until next backup is due.");
});
}
Expand Down Expand Up @@ -1185,10 +1189,10 @@ private void syncDirectoriesAsync()
}

System.Diagnostics.Debug.WriteLine("re-syncing");
int syncstatus = SynchronizeDirectories.remoteSync(
int syncstatus = SynchronizeDirectories.downloadWorlds(
Properties.Settings.Default.FtpIpAddress,
Properties.Settings.Default.FtpPort,
'/' + Properties.Settings.Default.FtpFilePath,
Properties.Settings.Default.FtpFilePath,
Properties.Settings.Default.FtpSaveDest + "\\worlds",
Properties.Settings.Default.FtpUsername,
Properties.Settings.Default.FtpPassword,
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.9.0")]
[assembly: AssemblyFileVersion("0.4.9.0")]
[assembly: AssemblyVersion("0.4.10.0")]
[assembly: AssemblyFileVersion("0.4.10.0")]
11 changes: 9 additions & 2 deletions SaveFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ public string FileName
return new FileInfo(this.filePath).Name;
}
}
public DateTime SaveTime
{
get
{
return new FileInfo(filePath).LastWriteTime;
}
}

public string FullPath
{
Expand Down Expand Up @@ -100,8 +107,8 @@ public bool NeedsBackedUp
{
get
{
DateTime newBackupTime = this.BackupDueTime;
if (DateTime.Compare(DateTime.Now, newBackupTime) >= 0)
//DateTime newBackupTime = this.BackupDueTime;
if (DateTime.Compare(SaveTime, this.BackupDueTime) >= 0)
{
return true;
}
Expand Down
27 changes: 0 additions & 27 deletions SaveTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,9 @@ class SaveTimer : Timer
{
public SaveFile Save { get; set; }

//new public event EventHandler<SaveTimerElapsedEventArgs> Elapsed;

public SaveTimer(SaveFile saveFile) : base()
{
Save = saveFile;
//base.Elapsed += Parent_Elapsed;
}

/*private void Parent_Elapsed(object sender, ElapsedEventArgs e)
{
OnElapsed(new SaveTimerElapsedEventArgs(this.Save, e.SignalTime));
}
private void OnElapsed(SaveTimerElapsedEventArgs args)
{
EventHandler<SaveTimerElapsedEventArgs> handler = Elapsed;
if (null != handler) handler(this, args);
}*/
}

/*class SaveTimerElapsedEventArgs : EventArgs
{
private readonly SaveFile _save;
private readonly DateTime _signalTime;
public SaveFile Save { get { return _save; } }
public DateTime SignalTime { get { return _signalTime; } }
public SaveTimerElapsedEventArgs(SaveFile save, DateTime signaltime)
{
_save = save;
_signalTime = signaltime;
}
}*/
}
53 changes: 52 additions & 1 deletion SynchronizeDirectories.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using WinSCP;

namespace ValheimSaveShield
Expand Down Expand Up @@ -35,7 +36,7 @@ public static int remoteSync(string hostUrl, string port, string hostDirectory,

// Connect
session.Open(sessionOptions);

// Synchronize files
SynchronizationResult synchronizationResult;
synchronizationResult =
Expand All @@ -57,6 +58,56 @@ public static int remoteSync(string hostUrl, string port, string hostDirectory,
}
}

public static int downloadWorlds(string hostUrl, string port, string hostDirectory, string localDirectory, 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);

//download
//transfer .fwl files first since they don't trigger a backup
var transferResult = session.GetFilesToDirectory(hostDirectory, localDirectory, "*.fwl");
transferResult.Check();
//transfer .db files next, but under a different file extension to delay triggering a backup
transferResult = session.GetFiles($"{hostDirectory}/*.db", $@"{localDirectory}\*.dbftp");
transferResult.Check();
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
//rename finished .db downloads to trigger a backup
var dbName = transfer.Destination.Replace(".dbftp", ".db");
if (File.Exists(dbName)) File.Delete(dbName);
var fi = new FileInfo(transfer.Destination);
fi.MoveTo(fi.FullName.Replace(transfer.Destination, dbName));
}
}

return 0;
}
catch (Exception e)
{
_lasterror = e;
System.Diagnostics.Debug.WriteLine("Error: {0}", e);
return 1;
}
}

private static void FileTransferred(object sender, TransferEventArgs e)
{
if (e.Error == null)
Expand Down

0 comments on commit 3e53a79

Please sign in to comment.