move case-insensitive path lookup into toolkit for reuse

This commit is contained in:
Jesse Plamondon-Willard 2022-04-16 14:07:09 -04:00
parent 1974324c43
commit 95d7ba8935
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
7 changed files with 24 additions and 24 deletions

View File

@ -2,10 +2,10 @@ using System;
using System.Collections.Generic;
using System.IO;
namespace StardewModdingAPI.Utilities
namespace StardewModdingAPI.Toolkit.Utilities
{
/// <summary>Provides an API for case-insensitive relative path lookups within a root directory.</summary>
internal class CaseInsensitivePathCache
internal class CaseInsensitivePathLookup
{
/*********
** Fields
@ -17,7 +17,7 @@ namespace StardewModdingAPI.Utilities
private readonly Lazy<Dictionary<string, string>> RelativePathCache;
/// <summary>The case-insensitive path caches by root path.</summary>
private static readonly Dictionary<string, CaseInsensitivePathCache> CachesByRootPath = new(StringComparer.OrdinalIgnoreCase);
private static readonly Dictionary<string, CaseInsensitivePathLookup> CachedRoots = new(StringComparer.OrdinalIgnoreCase);
/*********
@ -25,7 +25,7 @@ namespace StardewModdingAPI.Utilities
*********/
/// <summary>Construct an instance.</summary>
/// <param name="rootPath">The root directory path for relative paths.</param>
public CaseInsensitivePathCache(string rootPath)
public CaseInsensitivePathLookup(string rootPath)
{
this.RootPath = rootPath;
this.RelativePathCache = new(this.GetRelativePathCache);
@ -70,12 +70,12 @@ namespace StardewModdingAPI.Utilities
/// <summary>Get a cached dictionary of relative paths within a root path, for case-insensitive file lookups.</summary>
/// <param name="rootPath">The root path to scan.</param>
public static CaseInsensitivePathCache GetFor(string rootPath)
public static CaseInsensitivePathLookup GetCachedFor(string rootPath)
{
rootPath = PathUtilities.NormalizePath(rootPath);
if (!CaseInsensitivePathCache.CachesByRootPath.TryGetValue(rootPath, out CaseInsensitivePathCache? cache))
CaseInsensitivePathCache.CachesByRootPath[rootPath] = cache = new CaseInsensitivePathCache(rootPath);
if (!CaseInsensitivePathLookup.CachedRoots.TryGetValue(rootPath, out CaseInsensitivePathLookup? cache))
CaseInsensitivePathLookup.CachedRoots[rootPath] = cache = new CaseInsensitivePathLookup(rootPath);
return cache;
}

View File

@ -15,7 +15,7 @@ using StardewModdingAPI.Framework.Utilities;
using StardewModdingAPI.Internal;
using StardewModdingAPI.Metadata;
using StardewModdingAPI.Toolkit.Serialization;
using StardewModdingAPI.Utilities;
using StardewModdingAPI.Toolkit.Utilities;
using StardewValley;
using StardewValley.GameData;
using xTile;
@ -208,7 +208,7 @@ namespace StardewModdingAPI.Framework
jsonHelper: this.JsonHelper,
onDisposing: this.OnDisposing,
aggressiveMemoryOptimizations: this.AggressiveMemoryOptimizations,
relativePathCache: CaseInsensitivePathCache.GetFor(rootDirectory)
relativePathCache: CaseInsensitivePathLookup.GetCachedFor(rootDirectory)
);
this.ContentManagers.Add(manager);
return manager;

View File

@ -9,7 +9,7 @@ using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI.Framework.Exceptions;
using StardewModdingAPI.Framework.Reflection;
using StardewModdingAPI.Toolkit.Serialization;
using StardewModdingAPI.Utilities;
using StardewModdingAPI.Toolkit.Utilities;
using StardewValley;
using xTile;
using xTile.Format;
@ -33,7 +33,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
private readonly IContentManager GameContentManager;
/// <summary>A case-insensitive lookup of relative paths within the <see cref="ContentManager.RootDirectory"/>.</summary>
private readonly CaseInsensitivePathCache RelativePathCache;
private readonly CaseInsensitivePathLookup RelativePathCache;
/// <summary>If a map tilesheet's image source has no file extensions, the file extensions to check for in the local mod folder.</summary>
private readonly string[] LocalTilesheetExtensions = { ".png", ".xnb" };
@ -56,7 +56,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
/// <param name="onDisposing">A callback to invoke when the content manager is being disposed.</param>
/// <param name="aggressiveMemoryOptimizations">Whether to enable more aggressive memory optimizations.</param>
/// <param name="relativePathCache">A case-insensitive lookup of relative paths within the <paramref name="rootDirectory"/>.</param>
public ModContentManager(string name, IContentManager gameContentManager, IServiceProvider serviceProvider, string modName, string rootDirectory, CultureInfo currentCulture, ContentCoordinator coordinator, IMonitor monitor, Reflector reflection, JsonHelper jsonHelper, Action<BaseContentManager> onDisposing, bool aggressiveMemoryOptimizations, CaseInsensitivePathCache relativePathCache)
public ModContentManager(string name, IContentManager gameContentManager, IServiceProvider serviceProvider, string modName, string rootDirectory, CultureInfo currentCulture, ContentCoordinator coordinator, IMonitor monitor, Reflector reflection, JsonHelper jsonHelper, Action<BaseContentManager> onDisposing, bool aggressiveMemoryOptimizations, CaseInsensitivePathLookup relativePathCache)
: base(name, serviceProvider, rootDirectory, currentCulture, coordinator, monitor, reflection, onDisposing, isNamespaced: true, aggressiveMemoryOptimizations: aggressiveMemoryOptimizations)
{
this.GameContentManager = gameContentManager;

View File

@ -2,7 +2,7 @@ using System;
using System.IO;
using StardewModdingAPI.Framework.ModHelpers;
using StardewModdingAPI.Toolkit.Serialization;
using StardewModdingAPI.Utilities;
using StardewModdingAPI.Toolkit.Utilities;
namespace StardewModdingAPI.Framework
{
@ -16,7 +16,7 @@ namespace StardewModdingAPI.Framework
private readonly JsonHelper JsonHelper;
/// <summary>A case-insensitive lookup of relative paths within the <see cref="DirectoryPath"/>.</summary>
private readonly CaseInsensitivePathCache RelativePathCache;
private readonly CaseInsensitivePathLookup RelativePathCache;
/*********
@ -48,7 +48,7 @@ namespace StardewModdingAPI.Framework
/// <param name="translation">Provides translations stored in the content pack's <c>i18n</c> folder.</param>
/// <param name="jsonHelper">Encapsulates SMAPI's JSON file parsing.</param>
/// <param name="relativePathCache">A case-insensitive lookup of relative paths within the <paramref name="directoryPath"/>.</param>
public ContentPack(string directoryPath, IManifest manifest, IModContentHelper content, TranslationHelper translation, JsonHelper jsonHelper, CaseInsensitivePathCache relativePathCache)
public ContentPack(string directoryPath, IManifest manifest, IModContentHelper content, TranslationHelper translation, JsonHelper jsonHelper, CaseInsensitivePathLookup relativePathCache)
{
this.DirectoryPath = directoryPath;
this.Manifest = manifest;

View File

@ -4,7 +4,7 @@ using StardewModdingAPI.Framework.Content;
using StardewModdingAPI.Framework.ContentManagers;
using StardewModdingAPI.Framework.Exceptions;
using StardewModdingAPI.Framework.Reflection;
using StardewModdingAPI.Utilities;
using StardewModdingAPI.Toolkit.Utilities;
namespace StardewModdingAPI.Framework.ModHelpers
{
@ -24,7 +24,7 @@ namespace StardewModdingAPI.Framework.ModHelpers
private readonly string ModName;
/// <summary>A case-insensitive lookup of relative paths within the <see cref="ContentManager.RootDirectory"/>.</summary>
private readonly CaseInsensitivePathCache RelativePathCache;
private readonly CaseInsensitivePathLookup RelativePathCache;
/// <summary>Simplifies access to private code.</summary>
private readonly Reflector Reflection;
@ -41,7 +41,7 @@ namespace StardewModdingAPI.Framework.ModHelpers
/// <param name="gameContentManager">The game content manager used for map tilesheets not provided by the mod.</param>
/// <param name="relativePathCache">A case-insensitive lookup of relative paths within the <paramref name="relativePathCache"/>.</param>
/// <param name="reflection">Simplifies access to private code.</param>
public ModContentHelper(ContentCoordinator contentCore, string modFolderPath, IModMetadata mod, string modName, IContentManager gameContentManager, CaseInsensitivePathCache relativePathCache, Reflector reflection)
public ModContentHelper(ContentCoordinator contentCore, string modFolderPath, IModMetadata mod, string modName, IContentManager gameContentManager, CaseInsensitivePathLookup relativePathCache, Reflector reflection)
: base(mod)
{
string managedAssetPrefix = contentCore.GetManagedAssetPrefix(mod.Manifest.UniqueID);

View File

@ -8,7 +8,7 @@ using StardewModdingAPI.Toolkit.Framework.ModData;
using StardewModdingAPI.Toolkit.Framework.ModScanning;
using StardewModdingAPI.Toolkit.Framework.UpdateData;
using StardewModdingAPI.Toolkit.Serialization.Models;
using StardewModdingAPI.Utilities;
using StardewModdingAPI.Toolkit.Utilities;
namespace StardewModdingAPI.Framework.ModLoading
{
@ -141,7 +141,7 @@ namespace StardewModdingAPI.Framework.ModLoading
}
// file doesn't exist
string fileName = CaseInsensitivePathCache.GetFor(mod.DirectoryPath).GetFilePath(mod.Manifest.EntryDll!);
string fileName = CaseInsensitivePathLookup.GetCachedFor(mod.DirectoryPath).GetFilePath(mod.Manifest.EntryDll!);
if (!File.Exists(Path.Combine(mod.DirectoryPath, fileName)))
{
mod.SetStatus(ModMetadataStatus.Failed, ModFailReason.InvalidManifest, $"its DLL '{mod.Manifest.EntryDll}' doesn't exist.");

View File

@ -1748,7 +1748,7 @@ namespace StardewModdingAPI.Framework
if (mod.IsContentPack)
{
IMonitor monitor = this.LogManager.GetMonitor(mod.DisplayName);
CaseInsensitivePathCache relativePathCache = CaseInsensitivePathCache.GetFor(mod.DirectoryPath);
CaseInsensitivePathLookup relativePathCache = CaseInsensitivePathLookup.GetCachedFor(mod.DirectoryPath);
GameContentHelper gameContentHelper = new(this.ContentCore, mod, mod.DisplayName, monitor, this.Reflection);
IModContentHelper modContentHelper = new ModContentHelper(this.ContentCore, mod.DirectoryPath, mod, mod.DisplayName, gameContentHelper.GetUnderlyingContentManager(), relativePathCache, this.Reflection);
TranslationHelper translationHelper = new(mod, contentCore.GetLocale(), contentCore.Language);
@ -1767,7 +1767,7 @@ namespace StardewModdingAPI.Framework
// get mod info
string assemblyPath = Path.Combine(
mod.DirectoryPath,
CaseInsensitivePathCache.GetFor(mod.DirectoryPath).GetFilePath(manifest.EntryDll!)
CaseInsensitivePathLookup.GetCachedFor(mod.DirectoryPath).GetFilePath(manifest.EntryDll!)
);
// load mod
@ -1833,7 +1833,7 @@ namespace StardewModdingAPI.Framework
{
IMonitor packMonitor = this.LogManager.GetMonitor(packManifest.Name);
CaseInsensitivePathCache relativePathCache = CaseInsensitivePathCache.GetFor(packDirPath);
CaseInsensitivePathLookup relativePathCache = CaseInsensitivePathLookup.GetCachedFor(packDirPath);
GameContentHelper gameContentHelper = new(contentCore, mod, packManifest.Name, packMonitor, this.Reflection);
IModContentHelper packContentHelper = new ModContentHelper(contentCore, packDirPath, mod, packManifest.Name, gameContentHelper.GetUnderlyingContentManager(), relativePathCache, this.Reflection);
@ -1847,7 +1847,7 @@ namespace StardewModdingAPI.Framework
IModEvents events = new ModEvents(mod, this.EventManager);
ICommandHelper commandHelper = new CommandHelper(mod, this.CommandManager);
CaseInsensitivePathCache relativePathCache = CaseInsensitivePathCache.GetFor(mod.DirectoryPath);
CaseInsensitivePathLookup relativePathCache = CaseInsensitivePathLookup.GetCachedFor(mod.DirectoryPath);
#pragma warning disable CS0612 // deprecated code
ContentHelper contentHelper = new(contentCore, mod.DirectoryPath, mod, monitor, this.Reflection);
#pragma warning restore CS0612