From 0e5982bf9c21acd0429f05a0a608303f72b7b470 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 8 Oct 2017 02:22:12 -0400 Subject: [PATCH] escape invalid characters in release zip paths --- .../Tasks/CreateModReleaseZip.cs | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/SMAPI.ModBuildConfig/Tasks/CreateModReleaseZip.cs b/src/SMAPI.ModBuildConfig/Tasks/CreateModReleaseZip.cs index 26f2ed44..dd89d6b8 100644 --- a/src/SMAPI.ModBuildConfig/Tasks/CreateModReleaseZip.cs +++ b/src/SMAPI.ModBuildConfig/Tasks/CreateModReleaseZip.cs @@ -45,18 +45,17 @@ namespace StardewModdingAPI.ModBuildConfig.Tasks { try { - // create output path if needed - Directory.CreateDirectory(this.OutputFolderPath); - - // get zip filename - string fileName = $"{this.ModName}-{this.GetManifestVersion()}.zip"; + // get names + string zipName = this.EscapeInvalidFilenameCharacters($"{this.ModName}-{this.GetManifestVersion()}.zip"); + string folderName = this.EscapeInvalidFilenameCharacters(this.ModName); + string zipPath = Path.Combine(this.OutputFolderPath, zipName); // clear old zip file if present - string zipPath = Path.Combine(this.OutputFolderPath, fileName); if (File.Exists(zipPath)) File.Delete(zipPath); // create zip file + Directory.CreateDirectory(this.OutputFolderPath); using (Stream zipStream = new FileStream(zipPath, FileMode.Create, FileAccess.Write)) using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create)) { @@ -64,7 +63,7 @@ namespace StardewModdingAPI.ModBuildConfig.Tasks { // get file info string filePath = file.ItemSpec; - string entryName = this.ModName + '/' + file.GetMetadata("RecursiveDir") + file.GetMetadata("Filename") + file.GetMetadata("Extension"); + string entryName = folderName + '/' + file.GetMetadata("RecursiveDir") + file.GetMetadata("Filename") + file.GetMetadata("Extension"); if (new FileInfo(filePath).Directory.Name.Equals("i18n", StringComparison.InvariantCultureIgnoreCase)) entryName = Path.Combine("i18n", entryName); @@ -138,5 +137,14 @@ namespace StardewModdingAPI.ModBuildConfig.Tasks IDictionary data = (IDictionary)new JavaScriptSerializer().DeserializeObject(json); return MakeCaseInsensitive(data); } + + /// Get a copy of a filename with all invalid filename characters substituted. + /// The filename. + private string EscapeInvalidFilenameCharacters(string name) + { + foreach (char invalidChar in Path.GetInvalidFileNameChars()) + name = name.Replace(invalidChar, '.'); + return name; + } } }