World Save Back and Restore Changes

1. added the SaveGames folder to the to the backup zip file.
2. now restores the SaveGames folder.
This commit is contained in:
Brett Hewitson 2022-05-02 11:32:06 +10:00
parent aa5c135be7
commit a5e749963a
19 changed files with 408 additions and 96 deletions

View file

@ -1925,8 +1925,8 @@ namespace ServerManagerTool.Lib
if (Directory.Exists(saveFolder))
{
// make a backup of the current world file.
var worldBackupFile = GetServerWorldBackupFile();
if (File.Exists(worldBackupFile))
var worldFile = GetServerWorldBackupFile();
if (File.Exists(worldFile))
{
try
{
@ -1944,8 +1944,6 @@ namespace ServerManagerTool.Lib
if (File.Exists(backupFile))
File.Delete(backupFile);
var files = new List<string>();
var comment = new StringBuilder();
comment.AppendLine($"Windows Platform: {Environment.OSVersion.Platform}");
comment.AppendLine($"Windows Version: {Environment.OSVersion.VersionString}");
@ -1956,9 +1954,24 @@ namespace ServerManagerTool.Lib
comment.AppendLine($"Profile Name: {_profile.ProfileName}");
comment.AppendLine($"Process: {ServerProcess}");
ZipUtils.ZipAFile(backupFile, worldFileName, worldBackupFile, comment.ToString());
if (files.Count > 0)
ZipUtils.UpdateFiles(backupFile, files, null, false, "");
var saveFolderInfo = new DirectoryInfo(saveFolder);
// backup the world save file
ZipUtils.ZipAFile(backupFile, worldFileName, worldFile, comment.ToString());
// backup the save games files
var saveGamesFolder = GetServerSaveGamesFolder();
if (Directory.Exists(saveGamesFolder))
{
var saveGamesFolderInfo = new DirectoryInfo(saveGamesFolder);
var saveGamesFileFilter = $"*";
var saveGamesFiles = saveGamesFolderInfo.GetFiles(saveGamesFileFilter, SearchOption.AllDirectories);
foreach (var file in saveGamesFiles)
{
ZipUtils.ZipAFile(backupFile, file.FullName.Replace(saveGamesFolder, Config.Default.SaveGamesRelativePath), file.FullName);
}
}
LogProfileMessage($"Backed up world files - {saveFolder}");
LogProfileMessage($"Backup file created - {backupFile}");
@ -1986,12 +1999,12 @@ namespace ServerManagerTool.Lib
}
else
{
LogProfileMessage($"Server save file does not exist or could not be found '{worldBackupFile}'.");
LogProfileMessage($"Server save file does not exist or could not be found '{worldFile}'.");
LogProfileMessage($"Backup not performed.");
emailMessage?.AppendLine();
emailMessage?.AppendLine($"Server save file does not exist or could not be found.");
emailMessage?.AppendLine(worldBackupFile);
emailMessage?.AppendLine(worldFile);
emailMessage?.AppendLine();
emailMessage?.AppendLine("Backup not performed.");
@ -2289,6 +2302,8 @@ namespace ServerManagerTool.Lib
private string GetServerSaveFolder() => IOUtils.NormalizePath(Path.Combine(_profile.InstallDirectory, Config.Default.SavedFilesRelativePath));
private string GetServerSaveGamesFolder() => IOUtils.NormalizePath(ServerProfile.GetProfileSaveGamesPath(_profile.InstallDirectory));
private string GetServerVersionFile() => IOUtils.NormalizePath(Path.Combine(_profile.InstallDirectory, Config.Default.ServerBinaryRelativePath, Config.Default.ServerExeFile));
public static Version GetServerVersion(string versionFile)

View file

@ -1320,7 +1320,7 @@ namespace ServerManagerTool.Lib
return JsonUtils.Serialize<ServerProfile>(this);
}
public int RestoreSaveFiles(string restoreFile, bool isArchiveFile)
public int RestoreSaveFiles(string restoreFile, bool isArchiveFile, bool restoreAll)
{
if (string.IsNullOrWhiteSpace(restoreFile) || !File.Exists(restoreFile))
{
@ -1336,6 +1336,7 @@ namespace ServerManagerTool.Lib
}
var worldFileName = ServerMapSaveFileName;
var saveGamesFolder = GetProfileSaveGamesPath(this);
// check if the archive file contains the world save file at minimum
if (isArchiveFile)
@ -1365,12 +1366,23 @@ namespace ServerManagerTool.Lib
if (isArchiveFile)
{
// create a list of files to be deleted
var files = new List<string>();
// add the current world save file
files.Add(worldFile);
var directories = new List<string>();
var files = new List<string>
{
worldFile,
};
// add the world save support files
files.AddRange(Directory.GetFiles(saveFolder, $"{worldFileName}-*"));
if (restoreAll)
{
if (Directory.Exists(saveGamesFolder) && ZipUtils.DoesFolderExist(restoreFile, Config.Default.SaveGamesRelativePath))
{
directories.Add(saveGamesFolder);
}
}
// delete the selected files
foreach (var file in files)
{
@ -1384,8 +1396,33 @@ namespace ServerManagerTool.Lib
}
}
// restore the files from the backup
restoredFileCount = ZipUtils.ExtractAFile(restoreFile, worldFileName, saveFolder);
foreach (var directory in directories)
{
try
{
Directory.Delete(directory, true);
}
catch
{
// if unable to delete, do not bother
}
}
if (restoreAll)
{
// restore the files from the backup
restoredFileCount += ZipUtils.ExtractFiles(restoreFile, saveFolder, sourceFolder: "", recurseFolders: false);
if (ZipUtils.DoesFolderExist(restoreFile, Config.Default.SaveGamesRelativePath))
{
var rootSaveFolder = Path.GetDirectoryName(saveGamesFolder);
restoredFileCount += ZipUtils.ExtractFiles(restoreFile, rootSaveFolder, Config.Default.SaveGamesRelativePath, recurseFolders: true);
}
}
else
{
restoredFileCount += ZipUtils.ExtractAFile(restoreFile, worldFileName, saveFolder);
}
}
else
{
@ -1768,6 +1805,16 @@ namespace ServerManagerTool.Lib
return ModUtils.GetMapName(serverMap);
}
public static string GetProfileSaveGamesPath(ServerProfile profile)
{
return GetProfileSaveGamesPath(profile?.InstallDirectory);
}
public static string GetProfileSaveGamesPath(string installDirectory)
{
return Path.Combine(installDirectory ?? string.Empty, Config.Default.SavedRelativePath, Config.Default.SaveGamesRelativePath);
}
public static string GetProfileSavePath(ServerProfile profile)
{
return GetProfileSavePath(profile?.InstallDirectory);