From 95d7ba8935ac7214805147e694353206a56bddb7 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 16 Apr 2022 14:07:09 -0400 Subject: move case-insensitive path lookup into toolkit for reuse --- .../Utilities/CaseInsensitivePathLookup.cs | 137 +++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/SMAPI.Toolkit/Utilities/CaseInsensitivePathLookup.cs (limited to 'src/SMAPI.Toolkit/Utilities/CaseInsensitivePathLookup.cs') diff --git a/src/SMAPI.Toolkit/Utilities/CaseInsensitivePathLookup.cs b/src/SMAPI.Toolkit/Utilities/CaseInsensitivePathLookup.cs new file mode 100644 index 00000000..2e149e3c --- /dev/null +++ b/src/SMAPI.Toolkit/Utilities/CaseInsensitivePathLookup.cs @@ -0,0 +1,137 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace StardewModdingAPI.Toolkit.Utilities +{ + /// Provides an API for case-insensitive relative path lookups within a root directory. + internal class CaseInsensitivePathLookup + { + /********* + ** 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 path caches by root path. + private static readonly Dictionary CachedRoots = new(StringComparer.OrdinalIgnoreCase); + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The root directory path for relative paths. + public CaseInsensitivePathLookup(string rootPath) + { + this.RootPath = rootPath; + this.RelativePathCache = new(this.GetRelativePathCache); + } + + /// Get the exact capitalization for a given relative file path. + /// The relative path. + /// Returns the resolved path in file path format, else the normalized . + public string GetFilePath(string relativePath) + { + return this.GetImpl(PathUtilities.NormalizePath(relativePath)); + } + + /// Get the exact capitalization for a given asset name. + /// The relative path. + /// Returns the resolved path in asset name format, else the normalized . + public string GetAssetName(string relativePath) + { + return this.GetImpl(PathUtilities.NormalizeAssetName(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. + 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.CacheRawPath(this.RelativePathCache.Value, 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) + { + rootPath = PathUtilities.NormalizePath(rootPath); + + if (!CaseInsensitivePathLookup.CachedRoots.TryGetValue(rootPath, out CaseInsensitivePathLookup? cache)) + CaseInsensitivePathLookup.CachedRoots[rootPath] = cache = new CaseInsensitivePathLookup(rootPath); + + return cache; + } + + + /********* + ** 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 ). + private Dictionary GetRelativePathCache() + { + Dictionary cache = new(StringComparer.OrdinalIgnoreCase); + + foreach (string path in Directory.EnumerateFiles(this.RootPath, "*", SearchOption.AllDirectories)) + { + string relativePath = path.Substring(this.RootPath.Length + 1); + + this.CacheRawPath(cache, 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; + } + } +} -- cgit From f93c41f55c199293b4b8e00fc38ab89d24837f03 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 16 Apr 2022 14:28:20 -0400 Subject: make manifest.json filename case-insensitive --- src/SMAPI.Toolkit/Framework/ModScanning/ModScanner.cs | 16 ++++++++++++++-- src/SMAPI.Toolkit/Utilities/CaseInsensitivePathLookup.cs | 10 ++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) (limited to 'src/SMAPI.Toolkit/Utilities/CaseInsensitivePathLookup.cs') diff --git a/src/SMAPI.Toolkit/Framework/ModScanning/ModScanner.cs b/src/SMAPI.Toolkit/Framework/ModScanning/ModScanner.cs index 12333c4e..84329f63 100644 --- a/src/SMAPI.Toolkit/Framework/ModScanning/ModScanner.cs +++ b/src/SMAPI.Toolkit/Framework/ModScanning/ModScanner.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text.RegularExpressions; using StardewModdingAPI.Toolkit.Serialization; using StardewModdingAPI.Toolkit.Serialization.Models; +using StardewModdingAPI.Toolkit.Utilities; namespace StardewModdingAPI.Toolkit.Framework.ModScanning { @@ -251,8 +252,19 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning { while (true) { - // check for manifest in current folder - FileInfo file = new(Path.Combine(folder.FullName, "manifest.json")); + // check for conventional manifest in current folder + const string defaultName = "manifest.json"; + FileInfo file = new(Path.Combine(folder.FullName, defaultName)); + if (file.Exists) + return file; + + // check for manifest with incorrect capitalization + { + CaseInsensitivePathLookup pathLookup = new(folder.FullName, SearchOption.TopDirectoryOnly); // don't use GetCachedFor, since we only need it temporarily + string realName = pathLookup.GetFilePath(defaultName); + if (realName != defaultName) + file = new(Path.Combine(folder.FullName, realName)); + } if (file.Exists) return file; diff --git a/src/SMAPI.Toolkit/Utilities/CaseInsensitivePathLookup.cs b/src/SMAPI.Toolkit/Utilities/CaseInsensitivePathLookup.cs index 2e149e3c..12fad008 100644 --- a/src/SMAPI.Toolkit/Utilities/CaseInsensitivePathLookup.cs +++ b/src/SMAPI.Toolkit/Utilities/CaseInsensitivePathLookup.cs @@ -25,10 +25,11 @@ namespace StardewModdingAPI.Toolkit.Utilities *********/ /// Construct an instance. /// The root directory path for relative paths. - public CaseInsensitivePathLookup(string rootPath) + /// Which directories to scan from the root. + public CaseInsensitivePathLookup(string rootPath, SearchOption searchOption = SearchOption.AllDirectories) { this.RootPath = rootPath; - this.RelativePathCache = new(this.GetRelativePathCache); + this.RelativePathCache = new(() => this.GetRelativePathCache(searchOption)); } /// Get the exact capitalization for a given relative file path. @@ -108,11 +109,12 @@ namespace StardewModdingAPI.Toolkit.Utilities } /// Get a case-insensitive lookup of file paths (see ). - private Dictionary GetRelativePathCache() + /// 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.AllDirectories)) + foreach (string path in Directory.EnumerateFiles(this.RootPath, "*", searchOption)) { string relativePath = path.Substring(this.RootPath.Length + 1); -- cgit