From 82779bcc72e76b9aec9947b947847559b629cc6b Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Thu, 30 Nov 2023 18:37:23 -0600 Subject: [PATCH] Restore files from save archive based on entries in manifest --- LANCommander.SDK/GameSaveManager.cs | 58 +++++++++++++++-------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/LANCommander.SDK/GameSaveManager.cs b/LANCommander.SDK/GameSaveManager.cs index 1b4fe28..228c209 100644 --- a/LANCommander.SDK/GameSaveManager.cs +++ b/LANCommander.SDK/GameSaveManager.cs @@ -67,43 +67,45 @@ namespace LANCommander.SDK bool inInstallDir = savePath.Path.StartsWith("{InstallDir}"); string tempSavePath = Path.Combine(tempLocation, savePath.Id.ToString()); - var tempSavePathFile = Path.Combine(tempSavePath, savePath.Path.Replace('/', Path.DirectorySeparatorChar).Replace("{InstallDir}" + Path.DirectorySeparatorChar, "")); - - destination = Environment.ExpandEnvironmentVariables(savePath.Path.Replace('/', Path.DirectorySeparatorChar).Replace("{InstallDir}", installDirectory)); - - if (File.Exists(tempSavePathFile)) + foreach (var entry in savePath.Entries) { - // Is file, move file - if (File.Exists(destination)) - File.Delete(destination); + var tempSavePathFile = Path.Combine(tempSavePath, entry.ArchivePath); - File.Move(tempSavePathFile, destination); - } - else if (Directory.Exists(tempSavePath)) - { - var files = Directory.GetFiles(tempSavePath, "*", SearchOption.AllDirectories); + destination = Environment.ExpandEnvironmentVariables(entry.ActualPath).Replace("{InstallDir}", installDirectory); - foreach (var file in files) + if (File.Exists(tempSavePathFile)) { - if (inInstallDir) + if (File.Exists(destination)) + File.Delete(destination); + + File.Move(tempSavePathFile, destination); + } + else if (Directory.Exists(tempSavePath)) + { + var files = Directory.GetFiles(tempSavePath, "*", SearchOption.AllDirectories); + + foreach (var file in files) { - // Files are in the game's install directory. Move them there from the save path. - destination = file.Replace(tempSavePath, savePath.Path.Replace('/', Path.DirectorySeparatorChar).TrimEnd(Path.DirectorySeparatorChar).Replace("{InstallDir}", installDirectory)); + if (inInstallDir) + { + // Files are in the game's install directory. Move them there from the save path. + destination = file.Replace(tempSavePath, savePath.Path.Replace('/', Path.DirectorySeparatorChar).TrimEnd(Path.DirectorySeparatorChar).Replace("{InstallDir}", installDirectory)); - if (File.Exists(destination)) - File.Delete(destination); + if (File.Exists(destination)) + File.Delete(destination); - File.Move(file, destination); - } - else - { - // Specified path is probably an absolute path, maybe with environment variables. - destination = Environment.ExpandEnvironmentVariables(file.Replace(tempSavePathFile, savePath.Path.Replace('/', Path.DirectorySeparatorChar))); + File.Move(file, destination); + } + else + { + // Specified path is probably an absolute path, maybe with environment variables. + destination = Environment.ExpandEnvironmentVariables(file.Replace(tempSavePathFile, savePath.Path.Replace('/', Path.DirectorySeparatorChar))); - if (File.Exists(destination)) - File.Delete(destination); + if (File.Exists(destination)) + File.Delete(destination); - File.Move(file, destination); + File.Move(file, destination); + } } } }