1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
#nullable disable
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using StardewValley;
namespace StardewModdingAPI.Mods.SaveBackup
{
/// <summary>The main entry point for the mod.</summary>
public class ModEntry : Mod
{
/*********
** Fields
*********/
/// <summary>The number of backups to keep.</summary>
private readonly int BackupsToKeep = 10;
/// <summary>The absolute path to the folder in which to store save backups.</summary>
private readonly string BackupFolder = Path.Combine(Constants.GamePath, "save-backups");
/// <summary>A unique label for the save backup to create.</summary>
private readonly string BackupLabel = $"{DateTime.UtcNow:yyyy-MM-dd} - SMAPI {Constants.ApiVersion} with Stardew Valley {Game1.version}";
/// <summary>The name of the save archive to create.</summary>
private string FileName => $"{this.BackupLabel}.zip";
/*********
** Public methods
*********/
/// <summary>The mod entry point, called after the mod is first loaded.</summary>
/// <param name="helper">Provides simplified APIs for writing mods.</param>
public override void Entry(IModHelper helper)
{
try
{
// init backup folder
DirectoryInfo backupFolder = new(this.BackupFolder);
backupFolder.Create();
// back up & prune saves
Task
.Run(() => this.CreateBackup(backupFolder))
.ContinueWith(_ => this.PruneBackups(backupFolder, this.BackupsToKeep));
}
catch (Exception ex)
{
this.Monitor.Log($"Error backing up saves: {ex}", LogLevel.Error);
}
}
/*********
** Private methods
*********/
/// <summary>Back up the current saves.</summary>
/// <param name="backupFolder">The folder containing save backups.</param>
private void CreateBackup(DirectoryInfo backupFolder)
{
try
{
// get target path
FileInfo targetFile = new(Path.Combine(backupFolder.FullName, this.FileName));
DirectoryInfo fallbackDir = new(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)
DirectoryInfo savesDir = new(Constants.SavesPath);
if (!this.RecursiveCopy(savesDir, fallbackDir, entry => this.MatchSaveFolders(savesDir, entry), copyRoot: false))
{
this.Monitor.Log("No saves found.");
return;
}
// compress backup if possible
if (!this.TryCompress(fallbackDir.FullName, targetFile, out Exception compressError))
{
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);
}
}
catch (Exception ex)
{
this.Monitor.Log("Couldn't back up saves (see log file for details).", LogLevel.Warn);
this.Monitor.Log(ex.ToString());
}
}
/// <summary>Remove old backups if we've exceeded the limit.</summary>
/// <param name="backupFolder">The folder containing save backups.</param>
/// <param name="backupsToKeep">The number of backups to keep.</param>
private void PruneBackups(DirectoryInfo backupFolder, int backupsToKeep)
{
try
{
var oldBackups = backupFolder
.GetFileSystemInfos()
.OrderByDescending(p => p.CreationTimeUtc)
.Skip(backupsToKeep);
foreach (FileSystemInfo entry in oldBackups)
{
try
{
this.Monitor.Log($"Deleting {entry.Name}...");
if (entry is DirectoryInfo folder)
folder.Delete(recursive: true);
else
entry.Delete();
}
catch (Exception ex)
{
this.Monitor.Log($"Error deleting old save backup '{entry.Name}': {ex}", LogLevel.Error);
}
}
}
catch (Exception ex)
{
this.Monitor.Log("Couldn't remove old backups (see log file for details).", LogLevel.Warn);
this.Monitor.Log(ex.ToString());
}
}
/// <summary>Create a zip using the best available method.</summary>
/// <param name="sourcePath">The file or directory path to zip.</param>
/// <param name="destination">The destination file to create.</param>
/// <param name="error">The error which occurred trying to compress, if applicable. This is <see cref="NotSupportedException"/> if compression isn't supported on this platform.</param>
/// <returns>Returns whether compression succeeded.</returns>
private bool TryCompress(string sourcePath, FileInfo destination, out Exception error)
{
try
{
if (Constants.TargetPlatform == GamePlatform.Mac)
this.CompressUsingMacProcess(sourcePath, destination); // due to limitations with the bundled Mono on macOS, we can't reference System.IO.Compression
else
this.CompressUsingNetFramework(sourcePath, destination);
error = null;
return true;
}
catch (Exception ex)
{
error = ex;
return false;
}
}
/// <summary>Create a zip using the .NET compression library.</summary>
/// <param name="sourcePath">The file or directory path to zip.</param>
/// <param name="destination">The destination file to create.</param>
/// <exception cref="NotSupportedException">The compression libraries aren't available on this system.</exception>
private void CompressUsingNetFramework(string sourcePath, FileInfo destination)
{
// get compress method
MethodInfo createFromDirectory;
try
{
// create compressed backup
Assembly coreAssembly = Assembly.Load("System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
Assembly fsAssembly = Assembly.Load("System.IO.Compression.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
Type compressionLevelType = coreAssembly.GetType("System.IO.Compression.CompressionLevel") ?? throw new InvalidOperationException("Can't load CompressionLevel type.");
Type zipFileType = fsAssembly.GetType("System.IO.Compression.ZipFile") ?? throw new InvalidOperationException("Can't load ZipFile type.");
createFromDirectory = zipFileType.GetMethod("CreateFromDirectory", new[] { typeof(string), typeof(string), compressionLevelType, typeof(bool) }) ?? throw new InvalidOperationException("Can't load ZipFile.CreateFromDirectory method.");
}
catch (Exception ex)
{
throw new NotSupportedException("Couldn't load the .NET compression libraries on this system.", ex);
}
// compress file
createFromDirectory.Invoke(null, new object[] { sourcePath, destination.FullName, CompressionLevel.Fastest, false });
}
/// <summary>Create a zip using a process command on macOS.</summary>
/// <param name="sourcePath">The file or directory path to zip.</param>
/// <param name="destination">The destination file to create.</param>
private void CompressUsingMacProcess(string sourcePath, FileInfo destination)
{
DirectoryInfo saveFolder = new(sourcePath);
ProcessStartInfo startInfo = new()
{
FileName = "zip",
Arguments = $"-rq \"{destination.FullName}\" \"{saveFolder.Name}\" -x \"*.DS_Store\" -x \"__MACOSX\"",
WorkingDirectory = $"{saveFolder.FullName}/../",
CreateNoWindow = true
};
new Process { StartInfo = startInfo }.Start();
}
/// <summary>Recursively copy a directory or file.</summary>
/// <param name="source">The file or folder to copy.</param>
/// <param name="targetFolder">The folder to copy into.</param>
/// <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>
/// <returns>Returns whether any files were copied.</returns>
private bool RecursiveCopy(FileSystemInfo source, DirectoryInfo targetFolder, Func<FileSystemInfo, bool> filter, bool copyRoot = true)
{
if (!source.Exists || filter?.Invoke(source) == false)
return false;
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())
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>
/// <param name="savesFolder">The folder containing save folders.</param>
/// <param name="entry">The current entry to check under <paramref name="savesFolder"/>.</param>
private bool MatchSaveFolders(DirectoryInfo savesFolder, FileSystemInfo entry)
{
// only need to filter top-level entries
string parentPath = (entry as FileInfo)?.DirectoryName ?? (entry as DirectoryInfo)?.Parent?.FullName;
if (parentPath != savesFolder.FullName)
return true;
// match folders with Name_ID format
return
entry is DirectoryInfo
&& ulong.TryParse(entry.Name.Split('_').Last(), out _);
}
}
}
|