Skip to content

Commit

Permalink
Back up configs from previous YAMDCC versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Sparronator9999 committed Jan 17, 2025
1 parent 14966ec commit 2461f20
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
27 changes: 20 additions & 7 deletions YAMDCC.Updater/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ private static int Main(string[] args)
Utils.ShowError("Could not set pre-release setting!");
return 1;
case "--install":
// args: --install <oldPath> <updatePath> <destPath>
if (args.Length >= 4)
// args: --install <oldPath> <updatePath> <destPath> <confPath>
if (args.Length >= 5)
{
InstallUpdate(args[1], args[2], args[3]);
InstallUpdate(args[1], args[2], args[3], args[4]);
}
return 0;
}
Expand All @@ -82,7 +82,7 @@ private static int Main(string[] args)
}

private static void InstallUpdate(
string oldPath, string updatePath, string destPath)
string oldPath, string updatePath, string destPath, string confPath)
{
ProgressDialog<bool> dlg = new("Installing YAMDCC update...", () =>
{
Expand Down Expand Up @@ -123,7 +123,8 @@ private static void InstallUpdate(
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
if (dir.FullName != oldPath &&
if (dir.FullName != confPath &&
dir.FullName != oldPath &&
dir.FullName != updatePath)
{
dir.Delete(true);
Expand All @@ -138,7 +139,19 @@ private static void InstallUpdate(
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
dir.MoveTo(Path.Combine(destPath, dir.Name));
if (dir.Name == "Configs")
{
// copy new configs individually since we can't
// merge directories without extra work
foreach (FileInfo fi in dir.GetFiles())
{
fi.MoveTo(Path.Combine(confPath, fi.Name));
}
}
else
{
dir.MoveTo(Path.Combine(destPath, dir.Name));
}
}

// restart the YAMDCC service if it was running before the update
Expand All @@ -149,7 +162,7 @@ private static void InstallUpdate(

// cleanup :)
// (note: does not delete "Old" folder that we should be running from)
Directory.Delete(updatePath);
Directory.Delete(updatePath, true);

return true;
});
Expand Down
25 changes: 23 additions & 2 deletions YAMDCC.Updater/UpdateForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ internal sealed partial class UpdateForm : Form
private static readonly string DownloadPath = Path.GetFullPath("YAMDCC-Update.zip");
private static readonly string UpdatePath = Path.GetFullPath("Update");
private static readonly string OldPath = Path.GetFullPath("Old");
private static readonly string ConfPath = Path.GetFullPath("Configs");

public UpdateForm(Release release = null, bool autoUpdate = false)
{
Expand Down Expand Up @@ -146,6 +147,25 @@ await Updater.DownloadUpdateAsync(
// make a copy of the old YAMDCC installation
CopyDir(new DirectoryInfo(TargetPath), new DirectoryInfo(OldPath));

// backup existing YAMDCC configs so they don't
// get deleted when installing the new update
DirectoryInfo confDI = new(ConfPath);
foreach (FileInfo fi in confDI.GetFiles())
{
string name = Path.GetFileNameWithoutExtension(fi.Name);

// config is a backup from previous update; ignore
if (name.Contains("-backup-"))
{
continue;
}

// backup the config, just in case :)
string path = Path.Combine(fi.DirectoryName, name);
string date = $"{DateTime.Now:s}".Replace(':', '-');
fi.MoveTo($"{path}-backup-{date}{fi.Extension}");
}

// actually install the update
InstallUpdate();
return;
Expand Down Expand Up @@ -320,7 +340,7 @@ private void InstallUpdate()
// run the installer from a different location so we can
// clean the old directory
Utils.RunCmd(Path.Combine(OldPath, ExeName),
$"--install \"{OldPath}\" \"{UpdatePath}\" \"{TargetPath}\"", false);
$"--install \"{OldPath}\" \"{UpdatePath}\" \"{TargetPath}\" \"{ConfPath}\"", false);
Close();
}
catch (Win32Exception ex)
Expand Down Expand Up @@ -385,7 +405,8 @@ internal static void CopyDir(DirectoryInfo src, DirectoryInfo dest)
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in src.GetDirectories())
{
if (diSourceSubDir.FullName != OldPath &&
if (diSourceSubDir.FullName != ConfPath &&
diSourceSubDir.FullName != OldPath &&
diSourceSubDir.FullName != UpdatePath)
{
DirectoryInfo nextTargetSubDir =
Expand Down

0 comments on commit 2461f20

Please sign in to comment.