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