From 3db035312641b629aaa5517569d0d30cf71bac29 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 7 May 2022 23:12:33 -0400 Subject: simplify and rewrite case-insensitive file path feature --- .../PathLookups/CaseInsensitivePathLookup.cs | 78 +++++++--------------- .../Utilities/PathLookups/IFilePathLookup.cs | 18 ++--- .../Utilities/PathLookups/MinimalPathLookup.cs | 39 ++++++++--- 3 files changed, 61 insertions(+), 74 deletions(-) (limited to 'src/SMAPI.Toolkit/Utilities') diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs index 9cc00737..496d54c3 100644 --- a/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs +++ b/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs @@ -4,8 +4,8 @@ using System.IO; namespace StardewModdingAPI.Toolkit.Utilities.PathLookups { - /// An API for case-insensitive relative path lookups within a root directory. - internal class CaseInsensitivePathLookup : IFilePathLookup + /// An API for case-insensitive file lookups within a root directory. + internal class CaseInsensitiveFileLookup : IFileLookup { /********* ** Fields @@ -16,8 +16,8 @@ namespace StardewModdingAPI.Toolkit.Utilities.PathLookups /// A case-insensitive lookup of file paths within the . Each path is listed in both file path and asset name format, so it's usable in both contexts without needing to re-parse paths. private readonly Lazy> RelativePathCache; - /// The case-insensitive path caches by root path. - private static readonly Dictionary CachedRoots = new(StringComparer.OrdinalIgnoreCase); + /// The case-insensitive file lookups by root path. + private static readonly Dictionary CachedRoots = new(StringComparer.OrdinalIgnoreCase); /********* @@ -26,22 +26,28 @@ namespace StardewModdingAPI.Toolkit.Utilities.PathLookups /// Construct an instance. /// The root directory path for relative paths. /// Which directories to scan from the root. - public CaseInsensitivePathLookup(string rootPath, SearchOption searchOption = SearchOption.AllDirectories) + public CaseInsensitiveFileLookup(string rootPath, SearchOption searchOption = SearchOption.AllDirectories) { - this.RootPath = rootPath; + this.RootPath = PathUtilities.NormalizePath(rootPath); this.RelativePathCache = new(() => this.GetRelativePathCache(searchOption)); } /// - public string GetFilePath(string relativePath) + public FileInfo GetFile(string relativePath) { - return this.GetImpl(PathUtilities.NormalizePath(relativePath)); - } + // invalid path + if (string.IsNullOrWhiteSpace(relativePath)) + throw new InvalidOperationException("Can't get a file from an empty relative path."); - /// - public string GetAssetName(string relativePath) - { - return this.GetImpl(PathUtilities.NormalizeAssetName(relativePath)); + // already cached + if (this.RelativePathCache.Value.TryGetValue(relativePath, out string? resolved)) + return new(Path.Combine(this.RootPath, resolved)); + + // keep capitalization as-is + FileInfo file = new(Path.Combine(this.RootPath, relativePath)); + if (file.Exists) + this.RelativePathCache.Value[relativePath] = relativePath; + return file; } /// @@ -61,17 +67,17 @@ namespace StardewModdingAPI.Toolkit.Utilities.PathLookups throw new InvalidOperationException($"Can't add relative path '{relativePath}' to the case-insensitive cache for '{this.RootPath}' because that file doesn't exist."); // cache path - this.CacheRawPath(this.RelativePathCache.Value, relativePath); + this.RelativePathCache.Value[relativePath] = relativePath; } /// Get a cached dictionary of relative paths within a root path, for case-insensitive file lookups. /// The root path to scan. - public static CaseInsensitivePathLookup GetCachedFor(string rootPath) + public static CaseInsensitiveFileLookup GetCachedFor(string rootPath) { rootPath = PathUtilities.NormalizePath(rootPath); - if (!CaseInsensitivePathLookup.CachedRoots.TryGetValue(rootPath, out CaseInsensitivePathLookup? cache)) - CaseInsensitivePathLookup.CachedRoots[rootPath] = cache = new CaseInsensitivePathLookup(rootPath); + if (!CaseInsensitiveFileLookup.CachedRoots.TryGetValue(rootPath, out CaseInsensitiveFileLookup? cache)) + CaseInsensitiveFileLookup.CachedRoots[rootPath] = cache = new CaseInsensitiveFileLookup(rootPath); return cache; } @@ -80,29 +86,6 @@ namespace StardewModdingAPI.Toolkit.Utilities.PathLookups /********* ** Private methods *********/ - /// Get the exact capitalization for a given relative path. - /// The relative path. This must already be normalized into asset name or file path format (i.e. using or respectively). - /// Returns the resolved path in the same format if found, else returns the path as-is. - private string GetImpl(string relativePath) - { - // invalid path - if (string.IsNullOrWhiteSpace(relativePath)) - return relativePath; - - // already cached - if (this.RelativePathCache.Value.TryGetValue(relativePath, out string? resolved)) - return resolved; - - // keep capitalization as-is - if (File.Exists(Path.Combine(this.RootPath, relativePath))) - { - // file exists but isn't cached for some reason - // cache it now so any later references to it are case-insensitive - this.CacheRawPath(this.RelativePathCache.Value, relativePath); - } - return relativePath; - } - /// Get a case-insensitive lookup of file paths (see ). /// Which directories to scan from the root. private Dictionary GetRelativePathCache(SearchOption searchOption) @@ -112,23 +95,10 @@ namespace StardewModdingAPI.Toolkit.Utilities.PathLookups foreach (string path in Directory.EnumerateFiles(this.RootPath, "*", searchOption)) { string relativePath = path.Substring(this.RootPath.Length + 1); - - this.CacheRawPath(cache, relativePath); + cache[relativePath] = relativePath; } return cache; } - - /// Add a raw relative path to the cache. - /// The cache to update. - /// The relative path to cache, with its exact filesystem capitalization. - private void CacheRawPath(IDictionary cache, string relativePath) - { - string filePath = PathUtilities.NormalizePath(relativePath); - string assetName = PathUtilities.NormalizeAssetName(relativePath); - - cache[filePath] = filePath; - cache[assetName] = assetName; - } } } diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/IFilePathLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/IFilePathLookup.cs index 678e1383..d43b5141 100644 --- a/src/SMAPI.Toolkit/Utilities/PathLookups/IFilePathLookup.cs +++ b/src/SMAPI.Toolkit/Utilities/PathLookups/IFilePathLookup.cs @@ -1,20 +1,16 @@ +using System.IO; + namespace StardewModdingAPI.Toolkit.Utilities.PathLookups { - /// An API for relative path lookups within a root directory. - internal interface IFilePathLookup + /// An API for file lookups within a root directory. + internal interface IFileLookup { - /// Get the actual path for a given relative file path. - /// The relative path. - /// Returns the resolved path in file path format, else the normalized . - string GetFilePath(string relativePath); - - /// Get the actual path for a given asset name. + /// Get the file for a given relative file path, if it exists. /// The relative path. - /// Returns the resolved path in asset name format, else the normalized . - string GetAssetName(string relativePath); + FileInfo GetFile(string relativePath); /// Add a relative path that was just created by a SMAPI API. - /// The relative path. This must already be normalized in asset name or file path format. + /// The relative path. void Add(string relativePath); } } diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs index 2cf14704..414b569b 100644 --- a/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs +++ b/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs @@ -1,31 +1,52 @@ +using System.Collections.Generic; +using System.IO; + namespace StardewModdingAPI.Toolkit.Utilities.PathLookups { - /// An API for relative path lookups within a root directory with minimal preprocessing. - internal class MinimalPathLookup : IFilePathLookup + /// An API for file lookups within a root directory with minimal preprocessing. + internal class MinimalFileLookup : IFileLookup { /********* ** Accessors *********/ - /// A singleton instance for reuse. - public static readonly MinimalPathLookup Instance = new(); + /// The file lookups by root path. + private static readonly Dictionary CachedRoots = new(); + + /// The root directory path for relative paths. + private readonly string RootPath; /********* ** Public methods *********/ - /// - public string GetFilePath(string relativePath) + /// Construct an instance. + /// The root directory path for relative paths. + public MinimalFileLookup(string rootPath) { - return PathUtilities.NormalizePath(relativePath); + this.RootPath = rootPath; } /// - public string GetAssetName(string relativePath) + public FileInfo GetFile(string relativePath) { - return PathUtilities.NormalizeAssetName(relativePath); + return new( + Path.Combine(this.RootPath, PathUtilities.NormalizePath(relativePath)) + ); } /// public void Add(string relativePath) { } + + /// Get a cached dictionary of relative paths within a root path, for case-insensitive file lookups. + /// The root path to scan. + public static MinimalFileLookup GetCachedFor(string rootPath) + { + rootPath = PathUtilities.NormalizePath(rootPath); + + if (!MinimalFileLookup.CachedRoots.TryGetValue(rootPath, out MinimalFileLookup? lookup)) + MinimalFileLookup.CachedRoots[rootPath] = lookup = new MinimalFileLookup(rootPath); + + return lookup; + } } } -- cgit From ecdda9b07798df516d8fd91d9c388b4be5f27230 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 7 May 2022 23:13:59 -0400 Subject: update filenames for case-insensitive path rewrite --- .../PathLookups/CaseInsensitiveFileLookup.cs | 104 +++++++++++++++++++++ .../PathLookups/CaseInsensitivePathLookup.cs | 104 --------------------- .../Utilities/PathLookups/IFileLookup.cs | 16 ++++ .../Utilities/PathLookups/IFilePathLookup.cs | 16 ---- .../Utilities/PathLookups/MinimalFileLookup.cs | 52 +++++++++++ .../Utilities/PathLookups/MinimalPathLookup.cs | 52 ----------- 6 files changed, 172 insertions(+), 172 deletions(-) create mode 100644 src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitiveFileLookup.cs delete mode 100644 src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs create mode 100644 src/SMAPI.Toolkit/Utilities/PathLookups/IFileLookup.cs delete mode 100644 src/SMAPI.Toolkit/Utilities/PathLookups/IFilePathLookup.cs create mode 100644 src/SMAPI.Toolkit/Utilities/PathLookups/MinimalFileLookup.cs delete mode 100644 src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs (limited to 'src/SMAPI.Toolkit/Utilities') diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitiveFileLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitiveFileLookup.cs new file mode 100644 index 00000000..496d54c3 --- /dev/null +++ b/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitiveFileLookup.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace StardewModdingAPI.Toolkit.Utilities.PathLookups +{ + /// An API for case-insensitive file lookups within a root directory. + internal class CaseInsensitiveFileLookup : IFileLookup + { + /********* + ** Fields + *********/ + /// The root directory path for relative paths. + private readonly string RootPath; + + /// A case-insensitive lookup of file paths within the . Each path is listed in both file path and asset name format, so it's usable in both contexts without needing to re-parse paths. + private readonly Lazy> RelativePathCache; + + /// The case-insensitive file lookups by root path. + private static readonly Dictionary CachedRoots = new(StringComparer.OrdinalIgnoreCase); + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The root directory path for relative paths. + /// Which directories to scan from the root. + public CaseInsensitiveFileLookup(string rootPath, SearchOption searchOption = SearchOption.AllDirectories) + { + this.RootPath = PathUtilities.NormalizePath(rootPath); + this.RelativePathCache = new(() => this.GetRelativePathCache(searchOption)); + } + + /// + public FileInfo GetFile(string relativePath) + { + // invalid path + if (string.IsNullOrWhiteSpace(relativePath)) + throw new InvalidOperationException("Can't get a file from an empty relative path."); + + // already cached + if (this.RelativePathCache.Value.TryGetValue(relativePath, out string? resolved)) + return new(Path.Combine(this.RootPath, resolved)); + + // keep capitalization as-is + FileInfo file = new(Path.Combine(this.RootPath, relativePath)); + if (file.Exists) + this.RelativePathCache.Value[relativePath] = relativePath; + return file; + } + + /// + public void Add(string relativePath) + { + // skip if cache isn't created yet (no need to add files manually in that case) + if (!this.RelativePathCache.IsValueCreated) + return; + + // skip if already cached + if (this.RelativePathCache.Value.ContainsKey(relativePath)) + return; + + // make sure path exists + relativePath = PathUtilities.NormalizePath(relativePath); + if (!File.Exists(Path.Combine(this.RootPath, relativePath))) + throw new InvalidOperationException($"Can't add relative path '{relativePath}' to the case-insensitive cache for '{this.RootPath}' because that file doesn't exist."); + + // cache path + this.RelativePathCache.Value[relativePath] = relativePath; + } + + /// Get a cached dictionary of relative paths within a root path, for case-insensitive file lookups. + /// The root path to scan. + public static CaseInsensitiveFileLookup GetCachedFor(string rootPath) + { + rootPath = PathUtilities.NormalizePath(rootPath); + + if (!CaseInsensitiveFileLookup.CachedRoots.TryGetValue(rootPath, out CaseInsensitiveFileLookup? cache)) + CaseInsensitiveFileLookup.CachedRoots[rootPath] = cache = new CaseInsensitiveFileLookup(rootPath); + + return cache; + } + + + /********* + ** Private methods + *********/ + /// Get a case-insensitive lookup of file paths (see ). + /// Which directories to scan from the root. + private Dictionary GetRelativePathCache(SearchOption searchOption) + { + Dictionary cache = new(StringComparer.OrdinalIgnoreCase); + + foreach (string path in Directory.EnumerateFiles(this.RootPath, "*", searchOption)) + { + string relativePath = path.Substring(this.RootPath.Length + 1); + cache[relativePath] = relativePath; + } + + return cache; + } + } +} diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs deleted file mode 100644 index 496d54c3..00000000 --- a/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs +++ /dev/null @@ -1,104 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; - -namespace StardewModdingAPI.Toolkit.Utilities.PathLookups -{ - /// An API for case-insensitive file lookups within a root directory. - internal class CaseInsensitiveFileLookup : IFileLookup - { - /********* - ** Fields - *********/ - /// The root directory path for relative paths. - private readonly string RootPath; - - /// A case-insensitive lookup of file paths within the . Each path is listed in both file path and asset name format, so it's usable in both contexts without needing to re-parse paths. - private readonly Lazy> RelativePathCache; - - /// The case-insensitive file lookups by root path. - private static readonly Dictionary CachedRoots = new(StringComparer.OrdinalIgnoreCase); - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The root directory path for relative paths. - /// Which directories to scan from the root. - public CaseInsensitiveFileLookup(string rootPath, SearchOption searchOption = SearchOption.AllDirectories) - { - this.RootPath = PathUtilities.NormalizePath(rootPath); - this.RelativePathCache = new(() => this.GetRelativePathCache(searchOption)); - } - - /// - public FileInfo GetFile(string relativePath) - { - // invalid path - if (string.IsNullOrWhiteSpace(relativePath)) - throw new InvalidOperationException("Can't get a file from an empty relative path."); - - // already cached - if (this.RelativePathCache.Value.TryGetValue(relativePath, out string? resolved)) - return new(Path.Combine(this.RootPath, resolved)); - - // keep capitalization as-is - FileInfo file = new(Path.Combine(this.RootPath, relativePath)); - if (file.Exists) - this.RelativePathCache.Value[relativePath] = relativePath; - return file; - } - - /// - public void Add(string relativePath) - { - // skip if cache isn't created yet (no need to add files manually in that case) - if (!this.RelativePathCache.IsValueCreated) - return; - - // skip if already cached - if (this.RelativePathCache.Value.ContainsKey(relativePath)) - return; - - // make sure path exists - relativePath = PathUtilities.NormalizePath(relativePath); - if (!File.Exists(Path.Combine(this.RootPath, relativePath))) - throw new InvalidOperationException($"Can't add relative path '{relativePath}' to the case-insensitive cache for '{this.RootPath}' because that file doesn't exist."); - - // cache path - this.RelativePathCache.Value[relativePath] = relativePath; - } - - /// Get a cached dictionary of relative paths within a root path, for case-insensitive file lookups. - /// The root path to scan. - public static CaseInsensitiveFileLookup GetCachedFor(string rootPath) - { - rootPath = PathUtilities.NormalizePath(rootPath); - - if (!CaseInsensitiveFileLookup.CachedRoots.TryGetValue(rootPath, out CaseInsensitiveFileLookup? cache)) - CaseInsensitiveFileLookup.CachedRoots[rootPath] = cache = new CaseInsensitiveFileLookup(rootPath); - - return cache; - } - - - /********* - ** Private methods - *********/ - /// Get a case-insensitive lookup of file paths (see ). - /// Which directories to scan from the root. - private Dictionary GetRelativePathCache(SearchOption searchOption) - { - Dictionary cache = new(StringComparer.OrdinalIgnoreCase); - - foreach (string path in Directory.EnumerateFiles(this.RootPath, "*", searchOption)) - { - string relativePath = path.Substring(this.RootPath.Length + 1); - cache[relativePath] = relativePath; - } - - return cache; - } - } -} diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/IFileLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/IFileLookup.cs new file mode 100644 index 00000000..d43b5141 --- /dev/null +++ b/src/SMAPI.Toolkit/Utilities/PathLookups/IFileLookup.cs @@ -0,0 +1,16 @@ +using System.IO; + +namespace StardewModdingAPI.Toolkit.Utilities.PathLookups +{ + /// An API for file lookups within a root directory. + internal interface IFileLookup + { + /// Get the file for a given relative file path, if it exists. + /// The relative path. + FileInfo GetFile(string relativePath); + + /// Add a relative path that was just created by a SMAPI API. + /// The relative path. + void Add(string relativePath); + } +} diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/IFilePathLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/IFilePathLookup.cs deleted file mode 100644 index d43b5141..00000000 --- a/src/SMAPI.Toolkit/Utilities/PathLookups/IFilePathLookup.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.IO; - -namespace StardewModdingAPI.Toolkit.Utilities.PathLookups -{ - /// An API for file lookups within a root directory. - internal interface IFileLookup - { - /// Get the file for a given relative file path, if it exists. - /// The relative path. - FileInfo GetFile(string relativePath); - - /// Add a relative path that was just created by a SMAPI API. - /// The relative path. - void Add(string relativePath); - } -} diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalFileLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalFileLookup.cs new file mode 100644 index 00000000..414b569b --- /dev/null +++ b/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalFileLookup.cs @@ -0,0 +1,52 @@ +using System.Collections.Generic; +using System.IO; + +namespace StardewModdingAPI.Toolkit.Utilities.PathLookups +{ + /// An API for file lookups within a root directory with minimal preprocessing. + internal class MinimalFileLookup : IFileLookup + { + /********* + ** Accessors + *********/ + /// The file lookups by root path. + private static readonly Dictionary CachedRoots = new(); + + /// The root directory path for relative paths. + private readonly string RootPath; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The root directory path for relative paths. + public MinimalFileLookup(string rootPath) + { + this.RootPath = rootPath; + } + + /// + public FileInfo GetFile(string relativePath) + { + return new( + Path.Combine(this.RootPath, PathUtilities.NormalizePath(relativePath)) + ); + } + + /// + public void Add(string relativePath) { } + + /// Get a cached dictionary of relative paths within a root path, for case-insensitive file lookups. + /// The root path to scan. + public static MinimalFileLookup GetCachedFor(string rootPath) + { + rootPath = PathUtilities.NormalizePath(rootPath); + + if (!MinimalFileLookup.CachedRoots.TryGetValue(rootPath, out MinimalFileLookup? lookup)) + MinimalFileLookup.CachedRoots[rootPath] = lookup = new MinimalFileLookup(rootPath); + + return lookup; + } + } +} diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs deleted file mode 100644 index 414b569b..00000000 --- a/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System.Collections.Generic; -using System.IO; - -namespace StardewModdingAPI.Toolkit.Utilities.PathLookups -{ - /// An API for file lookups within a root directory with minimal preprocessing. - internal class MinimalFileLookup : IFileLookup - { - /********* - ** Accessors - *********/ - /// The file lookups by root path. - private static readonly Dictionary CachedRoots = new(); - - /// The root directory path for relative paths. - private readonly string RootPath; - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The root directory path for relative paths. - public MinimalFileLookup(string rootPath) - { - this.RootPath = rootPath; - } - - /// - public FileInfo GetFile(string relativePath) - { - return new( - Path.Combine(this.RootPath, PathUtilities.NormalizePath(relativePath)) - ); - } - - /// - public void Add(string relativePath) { } - - /// Get a cached dictionary of relative paths within a root path, for case-insensitive file lookups. - /// The root path to scan. - public static MinimalFileLookup GetCachedFor(string rootPath) - { - rootPath = PathUtilities.NormalizePath(rootPath); - - if (!MinimalFileLookup.CachedRoots.TryGetValue(rootPath, out MinimalFileLookup? lookup)) - MinimalFileLookup.CachedRoots[rootPath] = lookup = new MinimalFileLookup(rootPath); - - return lookup; - } - } -} -- cgit