From 9388b5b630198d604ebe0a3b2c0bdfab09981d6e Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Sun, 27 Aug 2023 23:22:44 -0500 Subject: [PATCH] Don't try to upload games if no save path definition file exists --- .../Services/GameSaveService.cs | 127 +++++++++--------- 1 file changed, 65 insertions(+), 62 deletions(-) diff --git a/LANCommander.Playnite.Extension/Services/GameSaveService.cs b/LANCommander.Playnite.Extension/Services/GameSaveService.cs index bf9cbe5..a650e5c 100644 --- a/LANCommander.Playnite.Extension/Services/GameSaveService.cs +++ b/LANCommander.Playnite.Extension/Services/GameSaveService.cs @@ -168,81 +168,84 @@ namespace LANCommander.PlaynitePlugin.Services var manifest = deserializer.Deserialize(File.ReadAllText(manifestPath)); var temp = Path.GetTempFileName(); - using (var archive = ZipArchive.Create()) + if (manifest.SavePaths != null && manifest.SavePaths.Count() > 0) { - archive.DeflateCompressionLevel = SharpCompress.Compressors.Deflate.CompressionLevel.BestCompression; - - #region Add files from defined paths - foreach (var savePath in manifest.SavePaths.Where(sp => sp.Type == "File")) + using (var archive = ZipArchive.Create()) { - var localPath = Environment.ExpandEnvironmentVariables(savePath.Path.Replace('/', '\\').Replace("{InstallDir}", game.InstallDirectory)); + archive.DeflateCompressionLevel = SharpCompress.Compressors.Deflate.CompressionLevel.BestCompression; - if (Directory.Exists(localPath)) + #region Add files from defined paths + foreach (var savePath in manifest.SavePaths.Where(sp => sp.Type == "File")) { - AddDirectoryToZip(archive, localPath, localPath, savePath.Id); + var localPath = Environment.ExpandEnvironmentVariables(savePath.Path.Replace('/', '\\').Replace("{InstallDir}", game.InstallDirectory)); + + if (Directory.Exists(localPath)) + { + AddDirectoryToZip(archive, localPath, localPath, savePath.Id); + } + else if (File.Exists(localPath)) + { + archive.AddEntry(Path.Combine(savePath.Id.ToString(), savePath.Path.Replace("{InstallDir}/", "")), localPath); + } } - else if (File.Exists(localPath)) + #endregion + + #region Add files from defined paths + foreach (var savePath in manifest.SavePaths.Where(sp => sp.Type == "File")) { - archive.AddEntry(Path.Combine(savePath.Id.ToString(), savePath.Path.Replace("{InstallDir}/", "")), localPath); + var localPath = Environment.ExpandEnvironmentVariables(savePath.Path.Replace('/', '\\').Replace("{InstallDir}", game.InstallDirectory)); + + if (Directory.Exists(localPath)) + { + AddDirectoryToZip(archive, localPath, localPath, savePath.Id); + } + else if (File.Exists(localPath)) + { + archive.AddEntry(Path.Combine(savePath.Id.ToString(), savePath.Path.Replace("{InstallDir}/", "")), localPath); + } } - } - #endregion + #endregion - #region Add files from defined paths - foreach (var savePath in manifest.SavePaths.Where(sp => sp.Type == "File")) - { - var localPath = Environment.ExpandEnvironmentVariables(savePath.Path.Replace('/', '\\').Replace("{InstallDir}", game.InstallDirectory)); - - if (Directory.Exists(localPath)) + #region Export registry keys + if (manifest.SavePaths.Any(sp => sp.Type == "Registry")) { - AddDirectoryToZip(archive, localPath, localPath, savePath.Id); + List tempRegFiles = new List(); + + var exportCommand = new StringBuilder(); + + foreach (var savePath in manifest.SavePaths.Where(sp => sp.Type == "Registry")) + { + var tempRegFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".reg"); + + exportCommand.AppendLine($"reg.exe export \"{savePath.Path.Replace(":\\", "\\")}\" \"{tempRegFile}\""); + tempRegFiles.Add(tempRegFile); + } + + PowerShellRuntime.RunCommand(exportCommand.ToString()); + + var exportFile = new StringBuilder(); + + foreach (var tempRegFile in tempRegFiles) + { + exportFile.AppendLine(File.ReadAllText(tempRegFile)); + File.Delete(tempRegFile); + } + + archive.AddEntry("_registry.reg", new MemoryStream(Encoding.UTF8.GetBytes(exportFile.ToString())), true); } - else if (File.Exists(localPath)) + #endregion + + archive.AddEntry("_manifest.yml", manifestPath); + + using (var ms = new MemoryStream()) { - archive.AddEntry(Path.Combine(savePath.Id.ToString(), savePath.Path.Replace("{InstallDir}/", "")), localPath); + archive.SaveTo(ms); + + ms.Seek(0, SeekOrigin.Begin); + + var save = LANCommander.UploadSave(game.GameId, ms.ToArray()); } } - #endregion - - #region Export registry keys - if (manifest.SavePaths.Any(sp => sp.Type == "Registry")) - { - List tempRegFiles = new List(); - - var exportCommand = new StringBuilder(); - - foreach (var savePath in manifest.SavePaths.Where(sp => sp.Type == "Registry")) - { - var tempRegFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".reg"); - - exportCommand.AppendLine($"reg.exe export \"{savePath.Path.Replace(":\\", "\\")}\" \"{tempRegFile}\""); - tempRegFiles.Add(tempRegFile); - } - - PowerShellRuntime.RunCommand(exportCommand.ToString()); - - var exportFile = new StringBuilder(); - - foreach (var tempRegFile in tempRegFiles) - { - exportFile.AppendLine(File.ReadAllText(tempRegFile)); - File.Delete(tempRegFile); - } - - archive.AddEntry("_registry.reg", new MemoryStream(Encoding.UTF8.GetBytes(exportFile.ToString())), true); - } - #endregion - - archive.AddEntry("_manifest.yml", manifestPath); - - using (var ms = new MemoryStream()) - { - archive.SaveTo(ms); - - ms.Seek(0, SeekOrigin.Begin); - - var save = LANCommander.UploadSave(game.GameId, ms.ToArray()); - } } } }