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 --- src/SMAPI/Framework/ContentCoordinator.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/SMAPI/Framework/ContentCoordinator.cs') diff --git a/src/SMAPI/Framework/ContentCoordinator.cs b/src/SMAPI/Framework/ContentCoordinator.cs index 4f52d57e..02b753ba 100644 --- a/src/SMAPI/Framework/ContentCoordinator.cs +++ b/src/SMAPI/Framework/ContentCoordinator.cs @@ -32,8 +32,8 @@ namespace StardewModdingAPI.Framework /// An asset key prefix for assets from SMAPI mod folders. private readonly string ManagedPrefix = "SMAPI"; - /// Get a file path lookup for the given directory. - private readonly Func GetFilePathLookup; + /// Get a file lookup for the given directory. + private readonly Func GetFileLookup; /// Encapsulates monitoring and logging. private readonly IMonitor Monitor; @@ -123,12 +123,12 @@ namespace StardewModdingAPI.Framework /// Encapsulates SMAPI's JSON file parsing. /// A callback to invoke the first time *any* game content manager loads an asset. /// A callback to invoke when an asset is fully loaded. - /// Get a file path lookup for the given directory. + /// Get a file lookup for the given directory. /// A callback to invoke when any asset names have been invalidated from the cache. /// Get the load/edit operations to apply to an asset by querying registered event handlers. - public ContentCoordinator(IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, IMonitor monitor, Reflector reflection, JsonHelper jsonHelper, Action onLoadingFirstAsset, Action onAssetLoaded, Func getFilePathLookup, Action> onAssetsInvalidated, Func> requestAssetOperations) + public ContentCoordinator(IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, IMonitor monitor, Reflector reflection, JsonHelper jsonHelper, Action onLoadingFirstAsset, Action onAssetLoaded, Func getFileLookup, Action> onAssetsInvalidated, Func> requestAssetOperations) { - this.GetFilePathLookup = getFilePathLookup; + this.GetFileLookup = getFileLookup; this.Monitor = monitor ?? throw new ArgumentNullException(nameof(monitor)); this.Reflection = reflection; this.JsonHelper = jsonHelper; @@ -200,7 +200,7 @@ namespace StardewModdingAPI.Framework reflection: this.Reflection, jsonHelper: this.JsonHelper, onDisposing: this.OnDisposing, - relativePathLookup: this.GetFilePathLookup(rootDirectory) + fileLookup: this.GetFileLookup(rootDirectory) ); this.ContentManagers.Add(manager); return manager; -- cgit From 26f95bca63e199cda3b3de1fa71eacab3110d17b Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 8 May 2022 18:22:35 -0400 Subject: optimize case where there's no legacy IAssetLoader/IAssetEditor instances --- src/SMAPI/Framework/ContentCoordinator.cs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'src/SMAPI/Framework/ContentCoordinator.cs') diff --git a/src/SMAPI/Framework/ContentCoordinator.cs b/src/SMAPI/Framework/ContentCoordinator.cs index 02b753ba..f7cab23d 100644 --- a/src/SMAPI/Framework/ContentCoordinator.cs +++ b/src/SMAPI/Framework/ContentCoordinator.cs @@ -79,14 +79,14 @@ namespace StardewModdingAPI.Framework private Lazy> LocaleCodes; /// The cached asset load/edit operations to apply, indexed by asset name. - private readonly TickCacheDictionary AssetOperationsByKey = new(); + private readonly TickCacheDictionary> AssetOperationsByKey = new(); /// A cache of asset operation groups created for legacy implementations. - [Obsolete] + [Obsolete("This only exists to support legacy code and will be removed in SMAPI 4.0.0.")] private readonly Dictionary> LegacyLoaderCache = new(ReferenceEqualityComparer.Instance); /// A cache of asset operation groups created for legacy implementations. - [Obsolete] + [Obsolete("This only exists to support legacy code and will be removed in SMAPI 4.0.0.")] private readonly Dictionary> LegacyEditorCache = new(ReferenceEqualityComparer.Instance); @@ -100,11 +100,11 @@ namespace StardewModdingAPI.Framework public LocalizedContentManager.LanguageCode Language => this.MainContentManager.Language; /// Interceptors which provide the initial versions of matching assets. - [Obsolete] + [Obsolete("This only exists to support legacy code and will be removed in SMAPI 4.0.0.")] public IList> Loaders { get; } = new List>(); /// Interceptors which edit matching assets after they're loaded. - [Obsolete] + [Obsolete("This only exists to support legacy code and will be removed in SMAPI 4.0.0.")] public IList> Editors { get; } = new List>(); /// The absolute path to the . @@ -449,12 +449,16 @@ namespace StardewModdingAPI.Framework /// Get the asset load and edit operations to apply to a given asset if it's (re)loaded now. /// The asset type. /// The asset info to load or edit. - public IEnumerable GetAssetOperations(IAssetInfo info) + public IList GetAssetOperations(IAssetInfo info) where T : notnull { return this.AssetOperationsByKey.GetOrSet( info.Name, - () => this.GetAssetOperationsWithoutCache(info).ToArray() +#pragma warning disable CS0612, CS0618 // deprecated code + () => this.Editors.Count > 0 || this.Loaders.Count > 0 + ? this.GetAssetOperationsIncludingLegacyWithoutCache(info).ToArray() +#pragma warning restore CS0612, CS0618 + : this.RequestAssetOperations(info) ); } @@ -580,7 +584,8 @@ namespace StardewModdingAPI.Framework /// Get the asset load and edit operations to apply to a given asset if it's (re)loaded now, ignoring the cache. /// The asset type. /// The asset info to load or edit. - private IEnumerable GetAssetOperationsWithoutCache(IAssetInfo info) + [Obsolete("This only exists to support legacy code and will be removed in SMAPI 4.0.0.")] + private IEnumerable GetAssetOperationsIncludingLegacyWithoutCache(IAssetInfo info) where T : notnull { IAssetInfo legacyInfo = this.GetLegacyAssetInfo(info); @@ -590,7 +595,6 @@ namespace StardewModdingAPI.Framework yield return group; // legacy load operations -#pragma warning disable CS0612, CS0618 // deprecated code foreach (ModLinked loader in this.Loaders) { // check if loader applies @@ -686,7 +690,6 @@ namespace StardewModdingAPI.Framework ) ); } -#pragma warning restore CS0612, CS0618 } /// Get a cached asset operation group for a legacy or instance, creating it if needed. -- cgit From f8f8b237996a1c883d43e539a379bf713a5fd7be Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 8 May 2022 18:50:07 -0400 Subject: use records for asset edit operations --- src/SMAPI/Framework/ContentCoordinator.cs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'src/SMAPI/Framework/ContentCoordinator.cs') diff --git a/src/SMAPI/Framework/ContentCoordinator.cs b/src/SMAPI/Framework/ContentCoordinator.cs index f7cab23d..6702f5e6 100644 --- a/src/SMAPI/Framework/ContentCoordinator.cs +++ b/src/SMAPI/Framework/ContentCoordinator.cs @@ -615,19 +615,19 @@ namespace StardewModdingAPI.Framework editor: loader.Data, dataType: info.DataType, createGroup: () => new AssetOperationGroup( - mod: loader.Mod, - loadOperations: new[] + Mod: loader.Mod, + LoadOperations: new[] { new AssetLoadOperation( - mod: loader.Mod, - priority: AssetLoadPriority.Exclusive, - onBehalfOf: null, - getData: assetInfo => loader.Data.Load( + Mod: loader.Mod, + OnBehalfOf: null, + Priority: AssetLoadPriority.Exclusive, + GetData: assetInfo => loader.Data.Load( this.GetLegacyAssetInfo(assetInfo) ) ) }, - editOperations: Array.Empty() + EditOperations: Array.Empty() ) ); } @@ -674,15 +674,15 @@ namespace StardewModdingAPI.Framework editor: editor.Data, dataType: info.DataType, createGroup: () => new AssetOperationGroup( - mod: editor.Mod, - loadOperations: Array.Empty(), - editOperations: new[] + Mod: editor.Mod, + LoadOperations: Array.Empty(), + EditOperations: new[] { new AssetEditOperation( - mod: editor.Mod, - priority: priority, - onBehalfOf: null, - applyEdit: assetData => editor.Data.Edit( + Mod: editor.Mod, + OnBehalfOf: null, + Priority: priority, + ApplyEdit: assetData => editor.Data.Edit( this.GetLegacyAssetData(assetData) ) ) -- cgit