summaryrefslogtreecommitdiff
path: root/src/SMAPI.Mods.SaveBackup
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-02-22 12:03:39 -0500
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-02-22 12:03:39 -0500
commit66079f2253a0c81bb33c1fee848a6cd2222d43d9 (patch)
treeca5fdd5f27c24f8d90665cb369e50f4580ce24fc /src/SMAPI.Mods.SaveBackup
parentc8d627cdf2ae3126584ec2500877ff19987db17f (diff)
parent585b23797e262073e0738069eff61e90819216b7 (diff)
downloadSMAPI-66079f2253a0c81bb33c1fee848a6cd2222d43d9.tar.gz
SMAPI-66079f2253a0c81bb33c1fee848a6cd2222d43d9.tar.bz2
SMAPI-66079f2253a0c81bb33c1fee848a6cd2222d43d9.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI.Mods.SaveBackup')
-rw-r--r--src/SMAPI.Mods.SaveBackup/ModEntry.cs46
-rw-r--r--src/SMAPI.Mods.SaveBackup/manifest.json4
2 files changed, 31 insertions, 19 deletions
diff --git a/src/SMAPI.Mods.SaveBackup/ModEntry.cs b/src/SMAPI.Mods.SaveBackup/ModEntry.cs
index 8b139d8f..b8d3be1c 100644
--- a/src/SMAPI.Mods.SaveBackup/ModEntry.cs
+++ b/src/SMAPI.Mods.SaveBackup/ModEntry.cs
@@ -66,29 +66,37 @@ namespace StardewModdingAPI.Mods.SaveBackup
FileInfo targetFile = new FileInfo(Path.Combine(backupFolder.FullName, this.FileName));
DirectoryInfo fallbackDir = new DirectoryInfo(Path.Combine(backupFolder.FullName, this.BackupLabel));
if (targetFile.Exists || fallbackDir.Exists)
+ {
+ this.Monitor.Log("Already backed up today.");
return;
+ }
// copy saves to fallback directory (ignore non-save files/folders)
- this.Monitor.Log($"Backing up saves to {fallbackDir.FullName}...", LogLevel.Trace);
DirectoryInfo savesDir = new DirectoryInfo(Constants.SavesPath);
- this.RecursiveCopy(savesDir, fallbackDir, entry => this.MatchSaveFolders(savesDir, entry), copyRoot: false);
+ if (!this.RecursiveCopy(savesDir, fallbackDir, entry => this.MatchSaveFolders(savesDir, entry), copyRoot: false))
+ {
+ this.Monitor.Log("No saves found.");
+ return;
+ }
// compress backup if possible
- this.Monitor.Log("Compressing backup if possible...", LogLevel.Trace);
if (!this.TryCompress(fallbackDir.FullName, targetFile, out Exception compressError))
{
- if (Constants.TargetPlatform != GamePlatform.Android) // expected to fail on Android
- this.Monitor.Log($"Couldn't compress backup, leaving it uncompressed.\n{compressError}", LogLevel.Trace);
+ this.Monitor.Log(Constants.TargetPlatform != GamePlatform.Android
+ ? $"Backed up to {fallbackDir.FullName}." // expected to fail on Android
+ : $"Backed up to {fallbackDir.FullName}. Couldn't compress backup:\n{compressError}"
+ );
}
else
+ {
+ this.Monitor.Log($"Backed up to {targetFile.FullName}.");
fallbackDir.Delete(recursive: true);
-
- this.Monitor.Log("Backup done!", LogLevel.Trace);
+ }
}
catch (Exception ex)
{
- this.Monitor.Log("Couldn't back up save files (see log file for details).", LogLevel.Warn);
- this.Monitor.Log(ex.ToString(), LogLevel.Trace);
+ this.Monitor.Log("Couldn't back up saves (see log file for details).", LogLevel.Warn);
+ this.Monitor.Log(ex.ToString());
}
}
@@ -108,7 +116,7 @@ namespace StardewModdingAPI.Mods.SaveBackup
{
try
{
- this.Monitor.Log($"Deleting {entry.Name}...", LogLevel.Trace);
+ this.Monitor.Log($"Deleting {entry.Name}...");
if (entry is DirectoryInfo folder)
folder.Delete(recursive: true);
else
@@ -123,7 +131,7 @@ namespace StardewModdingAPI.Mods.SaveBackup
catch (Exception ex)
{
this.Monitor.Log("Couldn't remove old backups (see log file for details).", LogLevel.Warn);
- this.Monitor.Log(ex.ToString(), LogLevel.Trace);
+ this.Monitor.Log(ex.ToString());
}
}
@@ -199,29 +207,33 @@ namespace StardewModdingAPI.Mods.SaveBackup
/// <param name="copyRoot">Whether to copy the root folder itself, or <c>false</c> to only copy its contents.</param>
/// <param name="filter">A filter which matches the files or directories to copy, or <c>null</c> to copy everything.</param>
/// <remarks>Derived from the SMAPI installer code.</remarks>
- private void RecursiveCopy(FileSystemInfo source, DirectoryInfo targetFolder, Func<FileSystemInfo, bool> filter, bool copyRoot = true)
+ /// <returns>Returns whether any files were copied.</returns>
+ private bool RecursiveCopy(FileSystemInfo source, DirectoryInfo targetFolder, Func<FileSystemInfo, bool> filter, bool copyRoot = true)
{
- if (!targetFolder.Exists)
- targetFolder.Create();
+ if (!source.Exists || filter?.Invoke(source) == false)
+ return false;
- if (filter?.Invoke(source) == false)
- return;
+ bool anyCopied = false;
switch (source)
{
case FileInfo sourceFile:
+ targetFolder.Create();
sourceFile.CopyTo(Path.Combine(targetFolder.FullName, sourceFile.Name));
+ anyCopied = true;
break;
case DirectoryInfo sourceDir:
DirectoryInfo targetSubfolder = copyRoot ? new DirectoryInfo(Path.Combine(targetFolder.FullName, sourceDir.Name)) : targetFolder;
foreach (var entry in sourceDir.EnumerateFileSystemInfos())
- this.RecursiveCopy(entry, targetSubfolder, filter);
+ anyCopied = this.RecursiveCopy(entry, targetSubfolder, filter) || anyCopied;
break;
default:
throw new NotSupportedException($"Unknown filesystem info type '{source.GetType().FullName}'.");
}
+
+ return anyCopied;
}
/// <summary>A copy filter which matches save folders.</summary>
diff --git a/src/SMAPI.Mods.SaveBackup/manifest.json b/src/SMAPI.Mods.SaveBackup/manifest.json
index 74256013..4559d1b0 100644
--- a/src/SMAPI.Mods.SaveBackup/manifest.json
+++ b/src/SMAPI.Mods.SaveBackup/manifest.json
@@ -1,9 +1,9 @@
{
"Name": "Save Backup",
"Author": "SMAPI",
- "Version": "3.2.0",
+ "Version": "3.3.0",
"Description": "Automatically backs up all your saves once per day into its folder.",
"UniqueID": "SMAPI.SaveBackup",
"EntryDll": "SaveBackup.dll",
- "MinimumApiVersion": "3.2.0"
+ "MinimumApiVersion": "3.3.0"
}