From dffebdf7eda6e68045c6e3c99d868507ea06a3dc Mon Sep 17 00:00:00 2001 From: nullium21 Date: Wed, 26 Oct 2022 13:31:32 +0300 Subject: [PATCH 1/5] Only use 0-9A-Za-z-_()[] characters in filenames --- osu.Game/Extensions/ModelExtensions.cs | 31 ++++++++++++++++++-------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/osu.Game/Extensions/ModelExtensions.cs b/osu.Game/Extensions/ModelExtensions.cs index b10071bb45..51bea02726 100644 --- a/osu.Game/Extensions/ModelExtensions.cs +++ b/osu.Game/Extensions/ModelExtensions.cs @@ -2,7 +2,6 @@ // See the LICENCE file in the repository root for full licence text. using System.IO; -using System.Linq; using osu.Game.Beatmaps; using osu.Game.Database; using osu.Game.IO; @@ -137,20 +136,34 @@ namespace osu.Game.Extensions return instance.OnlineID.Equals(other.OnlineID); } - private static readonly char[] invalid_filename_characters = Path.GetInvalidFileNameChars() - // Backslash is added to avoid issues when exporting to zip. - // See SharpCompress filename normalisation https://github.com/adamhathcock/sharpcompress/blob/a1e7c0068db814c9aa78d86a94ccd1c761af74bd/src/SharpCompress/Writers/Zip/ZipWriter.cs#L143. - .Append('\\') - .ToArray(); + private static bool isValidFilenameChar(this char ch) + { + if (ch >= '0' && ch <= '9') + return true; + + if (ch >= 'A' && ch <= 'Z') + return true; + + if (ch >= 'a' && ch <= 'z') + return true; + + return "-_()[]".Contains(ch); + } /// /// Get a valid filename for use inside a zip file. Avoids backslashes being incorrectly converted to directories. /// public static string GetValidArchiveContentFilename(this string filename) { - foreach (char c in invalid_filename_characters) - filename = filename.Replace(c, '_'); - return filename; + char[] resultData = filename.ToCharArray(); + + for (int i = 0; i < resultData.Length; i++) + { + if (!isValidFilenameChar(resultData[i])) + resultData[i] = '_'; + } + + return new string(resultData); } } } From 68d1febe6e9b5939f1f32a112b4c81f1567b1771 Mon Sep 17 00:00:00 2001 From: nullium21 Date: Thu, 27 Oct 2022 10:33:50 +0300 Subject: [PATCH 2/5] Use a regex for matching invalid characters instead What the regex does is it matches everything except alphanumeric and [_()[] -], excluding EOL since regex101 seems to also match it, and we probably don't want that to happen. --- osu.Game/Extensions/ModelExtensions.cs | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/osu.Game/Extensions/ModelExtensions.cs b/osu.Game/Extensions/ModelExtensions.cs index 51bea02726..8acc079805 100644 --- a/osu.Game/Extensions/ModelExtensions.cs +++ b/osu.Game/Extensions/ModelExtensions.cs @@ -2,6 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using System.IO; +using System.Text.RegularExpressions; using osu.Game.Beatmaps; using osu.Game.Database; using osu.Game.IO; @@ -136,34 +137,14 @@ namespace osu.Game.Extensions return instance.OnlineID.Equals(other.OnlineID); } - private static bool isValidFilenameChar(this char ch) - { - if (ch >= '0' && ch <= '9') - return true; - - if (ch >= 'A' && ch <= 'Z') - return true; - - if (ch >= 'a' && ch <= 'z') - return true; - - return "-_()[]".Contains(ch); - } + private static Regex invalid_filename_chars = new(@"(?!$)[^A-Za-z0-9_()[\] -]", RegexOptions.Compiled); /// /// Get a valid filename for use inside a zip file. Avoids backslashes being incorrectly converted to directories. /// public static string GetValidArchiveContentFilename(this string filename) { - char[] resultData = filename.ToCharArray(); - - for (int i = 0; i < resultData.Length; i++) - { - if (!isValidFilenameChar(resultData[i])) - resultData[i] = '_'; - } - - return new string(resultData); + return invalid_filename_chars.Replace(filename, "_"); } } } From c1e9398c19d3e74850c9f7fe546d8fefdabefa2f Mon Sep 17 00:00:00 2001 From: nullium21 Date: Fri, 28 Oct 2022 09:19:34 +0300 Subject: [PATCH 3/5] Fix regex by allowing file extensions + typed new Apparently, the _whole_ filename was checked with GetValidArchiveContentFilename, not just the stem. --- osu.Game/Extensions/ModelExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Extensions/ModelExtensions.cs b/osu.Game/Extensions/ModelExtensions.cs index 8acc079805..73a87c7003 100644 --- a/osu.Game/Extensions/ModelExtensions.cs +++ b/osu.Game/Extensions/ModelExtensions.cs @@ -137,7 +137,7 @@ namespace osu.Game.Extensions return instance.OnlineID.Equals(other.OnlineID); } - private static Regex invalid_filename_chars = new(@"(?!$)[^A-Za-z0-9_()[\] -]", RegexOptions.Compiled); + private static Regex invalid_filename_chars = new Regex(@"(?!$)[^A-Za-z0-9_()[\]. -]", RegexOptions.Compiled); /// /// Get a valid filename for use inside a zip file. Avoids backslashes being incorrectly converted to directories. From 6901ed997d353628005b524ad553d7eed9c83cde Mon Sep 17 00:00:00 2001 From: nullium21 Date: Fri, 28 Oct 2022 09:21:47 +0300 Subject: [PATCH 4/5] One more little change Damn, i forgor. Sorry, CI runners. --- osu.Game/Extensions/ModelExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Extensions/ModelExtensions.cs b/osu.Game/Extensions/ModelExtensions.cs index 73a87c7003..f8db5f4e88 100644 --- a/osu.Game/Extensions/ModelExtensions.cs +++ b/osu.Game/Extensions/ModelExtensions.cs @@ -137,7 +137,7 @@ namespace osu.Game.Extensions return instance.OnlineID.Equals(other.OnlineID); } - private static Regex invalid_filename_chars = new Regex(@"(?!$)[^A-Za-z0-9_()[\]. -]", RegexOptions.Compiled); + private static readonly Regex invalid_filename_chars = new Regex(@"(?!$)[^A-Za-z0-9_()[\]. \-]", RegexOptions.Compiled); /// /// Get a valid filename for use inside a zip file. Avoids backslashes being incorrectly converted to directories. From 66ed77ac91f7a9ab4cd6297833fba3f52a044bf5 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 28 Oct 2022 18:04:28 +0900 Subject: [PATCH 5/5] Rename and add documentation to function --- osu.Game/Beatmaps/BeatmapManager.cs | 2 +- osu.Game/Database/LegacyExporter.cs | 2 +- osu.Game/Extensions/ModelExtensions.cs | 16 +++++++++------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/osu.Game/Beatmaps/BeatmapManager.cs b/osu.Game/Beatmaps/BeatmapManager.cs index befc56d244..965cc43815 100644 --- a/osu.Game/Beatmaps/BeatmapManager.cs +++ b/osu.Game/Beatmaps/BeatmapManager.cs @@ -340,7 +340,7 @@ namespace osu.Game.Beatmaps static string createBeatmapFilenameFromMetadata(BeatmapInfo beatmapInfo) { var metadata = beatmapInfo.Metadata; - return $"{metadata.Artist} - {metadata.Title} ({metadata.Author.Username}) [{beatmapInfo.DifficultyName}].osu".GetValidArchiveContentFilename(); + return $"{metadata.Artist} - {metadata.Title} ({metadata.Author.Username}) [{beatmapInfo.DifficultyName}].osu".GetValidFilename(); } } diff --git a/osu.Game/Database/LegacyExporter.cs b/osu.Game/Database/LegacyExporter.cs index d9fdc40abc..16d7441dde 100644 --- a/osu.Game/Database/LegacyExporter.cs +++ b/osu.Game/Database/LegacyExporter.cs @@ -37,7 +37,7 @@ namespace osu.Game.Database /// The item to export. public void Export(TModel item) { - string filename = $"{item.GetDisplayString().GetValidArchiveContentFilename()}{FileExtension}"; + string filename = $"{item.GetDisplayString().GetValidFilename()}{FileExtension}"; using (var stream = exportStorage.CreateFileSafely(filename)) ExportModelTo(item, stream); diff --git a/osu.Game/Extensions/ModelExtensions.cs b/osu.Game/Extensions/ModelExtensions.cs index f8db5f4e88..efb3c4d633 100644 --- a/osu.Game/Extensions/ModelExtensions.cs +++ b/osu.Game/Extensions/ModelExtensions.cs @@ -15,6 +15,8 @@ namespace osu.Game.Extensions { public static class ModelExtensions { + private static readonly Regex invalid_filename_chars = new Regex(@"(?!$)[^A-Za-z0-9_()[\]. \-]", RegexOptions.Compiled); + /// /// Get the relative path in osu! storage for this file. /// @@ -137,14 +139,14 @@ namespace osu.Game.Extensions return instance.OnlineID.Equals(other.OnlineID); } - private static readonly Regex invalid_filename_chars = new Regex(@"(?!$)[^A-Za-z0-9_()[\]. \-]", RegexOptions.Compiled); - /// - /// Get a valid filename for use inside a zip file. Avoids backslashes being incorrectly converted to directories. + /// Create a valid filename which should work across all platforms. /// - public static string GetValidArchiveContentFilename(this string filename) - { - return invalid_filename_chars.Replace(filename, "_"); - } + /// + /// This function replaces all characters not included in a very pessimistic list which should be compatible + /// across all operating systems. We are using this in place of as + /// that function does not have per-platform considerations (and is only made to work on windows). + /// + public static string GetValidFilename(this string filename) => invalid_filename_chars.Replace(filename, "_"); } }