From a2190df08cc3f1b4a8dcb394056d65921d10702e Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 18 Feb 2022 15:39:49 -0500 Subject: add AssetName to encapsulate asset name handling (#766) --- .../ContentManagers/BaseContentManager.cs | 51 +----------- .../ContentManagers/GameContentManager.cs | 94 +++++++++------------- .../Framework/ContentManagers/IContentManager.cs | 3 - .../Framework/ContentManagers/ModContentManager.cs | 26 +++--- 4 files changed, 54 insertions(+), 120 deletions(-) (limited to 'src/SMAPI/Framework/ContentManagers') diff --git a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs index 5645c0fa..be892b33 100644 --- a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs @@ -160,14 +160,6 @@ namespace StardewModdingAPI.Framework.ContentManagers return this.IsNormalizedKeyLoaded(assetName, language); } - /// - public IEnumerable GetAssetKeys() - { - return this.Cache.Keys - .Select(this.GetAssetName) - .Distinct(); - } - /**** ** Cache invalidation ****/ @@ -177,13 +169,13 @@ namespace StardewModdingAPI.Framework.ContentManagers IDictionary removeAssets = new Dictionary(StringComparer.OrdinalIgnoreCase); this.Cache.Remove((key, asset) => { - this.ParseCacheKey(key, out string assetName, out _); + string baseAssetName = this.Coordinator.ParseAssetName(key).BaseName; // check if asset should be removed - bool remove = removeAssets.ContainsKey(assetName); - if (!remove && predicate(assetName, asset.GetType())) + bool remove = removeAssets.ContainsKey(baseAssetName); + if (!remove && predicate(baseAssetName, asset.GetType())) { - removeAssets[assetName] = asset; + removeAssets[baseAssetName] = asset; remove = true; } @@ -275,44 +267,9 @@ namespace StardewModdingAPI.Framework.ContentManagers this.BaseDisposableReferences.Clear(); } - /// Parse a cache key into its component parts. - /// The input cache key. - /// The original asset name. - /// The asset locale code (or null if not localized). - protected void ParseCacheKey(string cacheKey, out string assetName, out string localeCode) - { - // handle localized key - if (!string.IsNullOrWhiteSpace(cacheKey)) - { - int lastSepIndex = cacheKey.LastIndexOf(".", StringComparison.Ordinal); - if (lastSepIndex >= 0) - { - string suffix = cacheKey.Substring(lastSepIndex + 1, cacheKey.Length - lastSepIndex - 1); - if (this.Coordinator.TryGetLanguageEnum(suffix, out _)) - { - assetName = cacheKey.Substring(0, lastSepIndex); - localeCode = cacheKey.Substring(lastSepIndex + 1, cacheKey.Length - lastSepIndex - 1); - return; - } - } - } - - // handle simple key - assetName = cacheKey; - localeCode = null; - } - /// Get whether an asset has already been loaded. /// The normalized asset name. /// The language to check. protected abstract bool IsNormalizedKeyLoaded(string normalizedAssetName, LanguageCode language); - - /// Get the asset name from a cache key. - /// The input cache key. - private string GetAssetName(string cacheKey) - { - this.ParseCacheKey(cacheKey, out string assetName, out string _); - return assetName; - } } } diff --git a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs index ab198076..0ca9e277 100644 --- a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs @@ -73,46 +73,46 @@ namespace StardewModdingAPI.Framework.ContentManagers } // normalize asset name - assetName = this.AssertAndNormalizeAssetName(assetName); - if (this.TryParseExplicitLanguageAssetKey(assetName, out string newAssetName, out LanguageCode newLanguage)) - return this.Load(newAssetName, newLanguage, useCache); + IAssetName parsedName = this.Coordinator.ParseAssetName(assetName); + if (parsedName.LanguageCode.HasValue) + return this.Load(parsedName.BaseName, parsedName.LanguageCode.Value, useCache); // get from cache - if (useCache && this.IsLoaded(assetName, language)) - return this.RawLoad(assetName, language, useCache: true); + if (useCache && this.IsLoaded(parsedName.Name, language)) + return this.RawLoad(parsedName.Name, language, useCache: true); // get managed asset - if (this.Coordinator.TryParseManagedAssetKey(assetName, out string contentManagerID, out string relativePath)) + if (this.Coordinator.TryParseManagedAssetKey(parsedName.Name, out string contentManagerID, out string relativePath)) { T managedAsset = this.Coordinator.LoadManagedAsset(contentManagerID, relativePath); - this.TrackAsset(assetName, managedAsset, language, useCache); + this.TrackAsset(parsedName.Name, managedAsset, language, useCache); return managedAsset; } // load asset T data; - if (this.AssetsBeingLoaded.Contains(assetName)) + if (this.AssetsBeingLoaded.Contains(parsedName.Name)) { - this.Monitor.Log($"Broke loop while loading asset '{assetName}'.", LogLevel.Warn); + this.Monitor.Log($"Broke loop while loading asset '{parsedName.Name}'.", LogLevel.Warn); this.Monitor.Log($"Bypassing mod loaders for this asset. Stack trace:\n{Environment.StackTrace}"); - data = this.RawLoad(assetName, language, useCache); + data = this.RawLoad(parsedName.Name, language, useCache); } else { - data = this.AssetsBeingLoaded.Track(assetName, () => + data = this.AssetsBeingLoaded.Track(parsedName.Name, () => { string locale = this.GetLocale(language); - IAssetInfo info = new AssetInfo(locale, assetName, typeof(T), this.AssertAndNormalizeAssetName); + IAssetInfo info = new AssetInfo(locale, parsedName, typeof(T), this.AssertAndNormalizeAssetName); IAssetData asset = this.ApplyLoader(info) - ?? new AssetDataForObject(info, this.RawLoad(assetName, language, useCache), this.AssertAndNormalizeAssetName); + ?? new AssetDataForObject(info, this.RawLoad(parsedName.Name, language, useCache), this.AssertAndNormalizeAssetName); asset = this.ApplyEditors(info, asset); return (T)asset.Data; }); } // update cache & return data - this.TrackAsset(assetName, data, language, useCache); + this.TrackAsset(parsedName.Name, data, language, useCache); return data; } @@ -124,13 +124,16 @@ namespace StardewModdingAPI.Framework.ContentManagers // find assets for which a translatable version was loaded HashSet removeAssetNames = new HashSet(StringComparer.OrdinalIgnoreCase); foreach (string key in this.LocalizedAssetNames.Where(p => p.Key != p.Value).Select(p => p.Key)) - removeAssetNames.Add(this.TryParseExplicitLanguageAssetKey(key, out string assetName, out _) ? assetName : key); + { + IAssetName assetName = this.Coordinator.ParseAssetName(key); + removeAssetNames.Add(assetName.BaseName); + } // invalidate translatable assets string[] invalidated = this .InvalidateCache((key, type) => removeAssetNames.Contains(key) - || (this.TryParseExplicitLanguageAssetKey(key, out string assetName, out _) && removeAssetNames.Contains(assetName)) + || removeAssetNames.Contains(this.Coordinator.ParseAssetName(key).BaseName) ) .Select(p => p.Key) .OrderBy(p => p, StringComparer.OrdinalIgnoreCase) @@ -168,9 +171,10 @@ namespace StardewModdingAPI.Framework.ContentManagers { // handle explicit language in asset name { - if (this.TryParseExplicitLanguageAssetKey(assetName, out string newAssetName, out LanguageCode newLanguage)) + IAssetName parsedName = this.Coordinator.ParseAssetName(assetName); + if (parsedName.LanguageCode.HasValue) { - this.TrackAsset(newAssetName, value, newLanguage, useCache); + this.TrackAsset(parsedName.BaseName, value, parsedName.LanguageCode.Value, useCache); return; } } @@ -238,30 +242,6 @@ namespace StardewModdingAPI.Framework.ContentManagers } } - /// Parse an asset key that contains an explicit language into its asset name and language, if applicable. - /// The asset key to parse. - /// The asset name without the language code. - /// The language code removed from the asset name. - /// Returns whether the asset key contains an explicit language and was successfully parsed. - private bool TryParseExplicitLanguageAssetKey(string rawAsset, out string assetName, out LanguageCode language) - { - if (string.IsNullOrWhiteSpace(rawAsset)) - throw new SContentLoadException("The asset key is empty."); - - // extract language code - int splitIndex = rawAsset.LastIndexOf('.'); - if (splitIndex != -1 && this.Coordinator.TryGetLanguageEnum(rawAsset.Substring(splitIndex + 1), out language)) - { - assetName = rawAsset.Substring(0, splitIndex); - return true; - } - - // no explicit language code found - assetName = rawAsset; - language = this.Language; - return false; - } - /// Load the initial asset from the registered . /// The basic asset metadata. /// Returns the loaded asset metadata, or null if no loader matched. @@ -277,7 +257,7 @@ namespace StardewModdingAPI.Framework.ContentManagers } catch (Exception ex) { - entry.Mod.LogAsMod($"Mod failed when checking whether it could load asset '{info.AssetName}', and will be ignored. Error details:\n{ex.GetLogSummary()}", LogLevel.Error); + entry.Mod.LogAsMod($"Mod failed when checking whether it could load asset '{info.Name}', and will be ignored. Error details:\n{ex.GetLogSummary()}", LogLevel.Error); return false; } }) @@ -289,7 +269,7 @@ namespace StardewModdingAPI.Framework.ContentManagers if (loaders.Length > 1) { string[] loaderNames = loaders.Select(p => p.Mod.DisplayName).ToArray(); - this.Monitor.Log($"Multiple mods want to provide the '{info.AssetName}' asset ({string.Join(", ", loaderNames)}), but an asset can't be loaded multiple times. SMAPI will use the default asset instead; uninstall one of the mods to fix this. (Message for modders: you should usually use {typeof(IAssetEditor)} instead to avoid conflicts.)", LogLevel.Warn); + this.Monitor.Log($"Multiple mods want to provide the '{info.Name}' asset ({string.Join(", ", loaderNames)}), but an asset can't be loaded multiple times. SMAPI will use the default asset instead; uninstall one of the mods to fix this. (Message for modders: you should usually use {typeof(IAssetEditor)} instead to avoid conflicts.)", LogLevel.Warn); return null; } @@ -300,11 +280,11 @@ namespace StardewModdingAPI.Framework.ContentManagers try { data = loader.Load(info); - this.Monitor.Log($"{mod.DisplayName} loaded asset '{info.AssetName}'.", LogLevel.Trace); + this.Monitor.Log($"{mod.DisplayName} loaded asset '{info.Name}'.", LogLevel.Trace); } catch (Exception ex) { - mod.LogAsMod($"Mod crashed when loading asset '{info.AssetName}'. SMAPI will use the default asset instead. Error details:\n{ex.GetLogSummary()}", LogLevel.Error); + mod.LogAsMod($"Mod crashed when loading asset '{info.Name}'. SMAPI will use the default asset instead. Error details:\n{ex.GetLogSummary()}", LogLevel.Error); return null; } @@ -349,7 +329,7 @@ namespace StardewModdingAPI.Framework.ContentManagers } catch (Exception ex) { - mod.LogAsMod($"Mod crashed when checking whether it could edit asset '{info.AssetName}', and will be ignored. Error details:\n{ex.GetLogSummary()}", LogLevel.Error); + mod.LogAsMod($"Mod crashed when checking whether it could edit asset '{info.Name}', and will be ignored. Error details:\n{ex.GetLogSummary()}", LogLevel.Error); continue; } @@ -358,22 +338,22 @@ namespace StardewModdingAPI.Framework.ContentManagers try { editor.Edit(asset); - this.Monitor.Log($"{mod.DisplayName} edited {info.AssetName}."); + this.Monitor.Log($"{mod.DisplayName} edited {info.Name}."); } catch (Exception ex) { - mod.LogAsMod($"Mod crashed when editing asset '{info.AssetName}', which may cause errors in-game. Error details:\n{ex.GetLogSummary()}", LogLevel.Error); + mod.LogAsMod($"Mod crashed when editing asset '{info.Name}', which may cause errors in-game. Error details:\n{ex.GetLogSummary()}", LogLevel.Error); } // validate edit if (asset.Data == null) { - mod.LogAsMod($"Mod incorrectly set asset '{info.AssetName}' to a null value; ignoring override.", LogLevel.Warn); + mod.LogAsMod($"Mod incorrectly set asset '{info.Name}' to a null value; ignoring override.", LogLevel.Warn); asset = GetNewData(prevAsset); } else if (!(asset.Data is T)) { - mod.LogAsMod($"Mod incorrectly set asset '{asset.AssetName}' to incompatible type '{asset.Data.GetType()}', expected '{typeof(T)}'; ignoring override.", LogLevel.Warn); + mod.LogAsMod($"Mod incorrectly set asset '{asset.Name}' to incompatible type '{asset.Data.GetType()}', expected '{typeof(T)}'; ignoring override.", LogLevel.Warn); asset = GetNewData(prevAsset); } } @@ -393,21 +373,21 @@ namespace StardewModdingAPI.Framework.ContentManagers // can't load a null asset if (data == null) { - mod.LogAsMod($"SMAPI blocked asset replacement for '{info.AssetName}': mod incorrectly set asset to a null value.", LogLevel.Error); + mod.LogAsMod($"SMAPI blocked asset replacement for '{info.Name}': mod incorrectly set asset to a null value.", LogLevel.Error); return false; } // when replacing a map, the vanilla tilesheets must have the same order and IDs if (data is Map loadedMap) { - TilesheetReference[] vanillaTilesheetRefs = this.Coordinator.GetVanillaTilesheetIds(info.AssetName); + TilesheetReference[] vanillaTilesheetRefs = this.Coordinator.GetVanillaTilesheetIds(info.Name.Name); foreach (TilesheetReference vanillaSheet in vanillaTilesheetRefs) { // add missing tilesheet if (loadedMap.GetTileSheet(vanillaSheet.Id) == null) { mod.Monitor.LogOnce("SMAPI fixed maps loaded by this mod to prevent errors. See the log file for details.", LogLevel.Warn); - this.Monitor.Log($"Fixed broken map replacement: {mod.DisplayName} loaded '{info.AssetName}' without a required tilesheet (id: {vanillaSheet.Id}, source: {vanillaSheet.ImageSource})."); + this.Monitor.Log($"Fixed broken map replacement: {mod.DisplayName} loaded '{info.Name}' without a required tilesheet (id: {vanillaSheet.Id}, source: {vanillaSheet.ImageSource})."); loadedMap.AddTileSheet(new TileSheet(vanillaSheet.Id, loadedMap, vanillaSheet.ImageSource, vanillaSheet.SheetSize, vanillaSheet.TileSize)); } @@ -417,17 +397,17 @@ namespace StardewModdingAPI.Framework.ContentManagers { // only show warning if not farm map // This is temporary: mods shouldn't do this for any vanilla map, but these are the ones we know will crash. Showing a warning for others instead gives modders time to update their mods, while still simplifying troubleshooting. - bool isFarmMap = info.AssetNameEquals("Maps/Farm") || info.AssetNameEquals("Maps/Farm_Combat") || info.AssetNameEquals("Maps/Farm_Fishing") || info.AssetNameEquals("Maps/Farm_Foraging") || info.AssetNameEquals("Maps/Farm_FourCorners") || info.AssetNameEquals("Maps/Farm_Island") || info.AssetNameEquals("Maps/Farm_Mining"); + bool isFarmMap = info.Name.IsEquivalentTo("Maps/Farm") || info.Name.IsEquivalentTo("Maps/Farm_Combat") || info.Name.IsEquivalentTo("Maps/Farm_Fishing") || info.Name.IsEquivalentTo("Maps/Farm_Foraging") || info.Name.IsEquivalentTo("Maps/Farm_FourCorners") || info.Name.IsEquivalentTo("Maps/Farm_Island") || info.Name.IsEquivalentTo("Maps/Farm_Mining"); string reason = $"mod reordered the original tilesheets, which {(isFarmMap ? "would cause a crash" : "often causes crashes")}.\nTechnical details for mod author: Expected order: {string.Join(", ", vanillaTilesheetRefs.Select(p => p.Id))}. See https://stardewvalleywiki.com/Modding:Maps#Tilesheet_order for help."; SCore.DeprecationManager.PlaceholderWarn("3.8.2", DeprecationLevel.PendingRemoval); if (isFarmMap) { - mod.LogAsMod($"SMAPI blocked '{info.AssetName}' map load: {reason}", LogLevel.Error); + mod.LogAsMod($"SMAPI blocked '{info.Name}' map load: {reason}", LogLevel.Error); return false; } - mod.LogAsMod($"SMAPI found an issue with '{info.AssetName}' map load: {reason}", LogLevel.Warn); + mod.LogAsMod($"SMAPI found an issue with '{info.Name}' map load: {reason}", LogLevel.Warn); } } } diff --git a/src/SMAPI/Framework/ContentManagers/IContentManager.cs b/src/SMAPI/Framework/ContentManagers/IContentManager.cs index d7963305..ba7dbc06 100644 --- a/src/SMAPI/Framework/ContentManagers/IContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/IContentManager.cs @@ -58,9 +58,6 @@ namespace StardewModdingAPI.Framework.ContentManagers /// The language. bool IsLoaded(string assetName, LocalizedContentManager.LanguageCode language); - /// Get the cached asset keys. - IEnumerable GetAssetKeys(); - /// Purge matched assets from the cache. /// Matches the asset keys to invalidate. /// Whether to dispose invalidated assets. This should only be true when they're being invalidated as part of a dispose, to avoid crashing the game. diff --git a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs index beb90a5d..21f88d47 100644 --- a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs @@ -80,7 +80,7 @@ namespace StardewModdingAPI.Framework.ContentManagers { // normalize key bool isXnbFile = Path.GetExtension(assetName).ToLower() == ".xnb"; - assetName = this.AssertAndNormalizeAssetName(assetName); + IAssetName parsedName = this.Coordinator.ParseAssetName(assetName); // disable caching // This is necessary to avoid assets being shared between content managers, which can @@ -97,21 +97,21 @@ namespace StardewModdingAPI.Framework.ContentManagers // resolve managed asset key { - if (this.Coordinator.TryParseManagedAssetKey(assetName, out string contentManagerID, out string relativePath)) + if (this.Coordinator.TryParseManagedAssetKey(parsedName.Name, out string contentManagerID, out string relativePath)) { if (contentManagerID != this.Name) - throw new SContentLoadException($"Can't load managed asset key '{assetName}' through content manager '{this.Name}' for a different mod."); - assetName = relativePath; + throw new SContentLoadException($"Can't load managed asset key '{parsedName}' through content manager '{this.Name}' for a different mod."); + parsedName = this.Coordinator.ParseAssetName(relativePath); } } // get local asset - SContentLoadException GetContentError(string reasonPhrase) => new SContentLoadException($"Failed loading asset '{assetName}' from {this.Name}: {reasonPhrase}"); + SContentLoadException GetContentError(string reasonPhrase) => new SContentLoadException($"Failed loading asset '{parsedName}' from {this.Name}: {reasonPhrase}"); T asset; try { // get file - FileInfo file = this.GetModFile(isXnbFile ? $"{assetName}.xnb" : assetName); // .xnb extension is stripped from asset names passed to the content manager + FileInfo file = this.GetModFile(isXnbFile ? $"{parsedName}.xnb" : parsedName.Name); // .xnb extension is stripped from asset names passed to the content manager if (!file.Exists) throw GetContentError("the specified path doesn't exist."); @@ -121,11 +121,11 @@ namespace StardewModdingAPI.Framework.ContentManagers // XNB file case ".xnb": { - asset = this.RawLoad(assetName, useCache: false); + asset = this.RawLoad(parsedName.Name, useCache: false); if (asset is Map map) { - map.assetPath = assetName; - this.FixTilesheetPaths(map, relativeMapPath: assetName, fixEagerPathPrefixes: true); + map.assetPath = parsedName.Name; + this.FixTilesheetPaths(map, relativeMapPath: parsedName.Name, fixEagerPathPrefixes: true); } } break; @@ -173,8 +173,8 @@ namespace StardewModdingAPI.Framework.ContentManagers // fetch & cache FormatManager formatManager = FormatManager.Instance; Map map = formatManager.LoadMap(file.FullName); - map.assetPath = assetName; - this.FixTilesheetPaths(map, relativeMapPath: assetName, fixEagerPathPrefixes: false); + map.assetPath = parsedName.Name; + this.FixTilesheetPaths(map, relativeMapPath: parsedName.Name, fixEagerPathPrefixes: false); asset = (T)(object)map; } break; @@ -185,11 +185,11 @@ namespace StardewModdingAPI.Framework.ContentManagers } catch (Exception ex) when (!(ex is SContentLoadException)) { - throw new SContentLoadException($"The content manager failed loading content asset '{assetName}' from {this.Name}.", ex); + throw new SContentLoadException($"The content manager failed loading content asset '{parsedName}' from {this.Name}.", ex); } // track & return asset - this.TrackAsset(assetName, asset, language, useCache); + this.TrackAsset(parsedName.Name, asset, language, useCache); return asset; } -- cgit From 2d52681b1034d314d8d56c561a5aa72e54e34576 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 19 Feb 2022 11:07:26 -0500 Subject: add Constants.GamePath & deprecate Constants.ExecutionPath --- src/SMAPI/Framework/ContentManagers/BaseContentManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/SMAPI/Framework/ContentManagers') diff --git a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs index be892b33..26f0921d 100644 --- a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs @@ -56,7 +56,7 @@ namespace StardewModdingAPI.Framework.ContentManagers public LanguageCode Language => this.GetCurrentLanguage(); /// - public string FullRootDirectory => Path.Combine(Constants.ExecutionPath, this.RootDirectory); + public string FullRootDirectory => Path.Combine(Constants.GamePath, this.RootDirectory); /// public bool IsNamespaced { get; } -- cgit From 6fc3be52bc2faaa85bea2febe6953fb5b51dac3a Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 22 Feb 2022 17:38:59 -0500 Subject: avoid SetData when premultiplying texture with no semi-transparency --- src/SMAPI/Framework/ContentManagers/ModContentManager.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src/SMAPI/Framework/ContentManagers') diff --git a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs index 21f88d47..50ea6e61 100644 --- a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs @@ -252,16 +252,20 @@ namespace StardewModdingAPI.Framework.ContentManagers // premultiply pixels Color[] data = new Color[texture.Width * texture.Height]; texture.GetData(data); + bool changed = false; for (int i = 0; i < data.Length; i++) { - var pixel = data[i]; - if (pixel.A == byte.MinValue || pixel.A == byte.MaxValue) + Color pixel = data[i]; + if (pixel.A is (byte.MinValue or byte.MaxValue)) continue; // no need to change fully transparent/opaque pixels data[i] = new Color(pixel.R * pixel.A / byte.MaxValue, pixel.G * pixel.A / byte.MaxValue, pixel.B * pixel.A / byte.MaxValue, pixel.A); // slower version: Color.FromNonPremultiplied(data[i].ToVector4()) + changed = true; } - texture.SetData(data); + if (changed) + texture.SetData(data); + return texture; } -- cgit From 2f279708b3529020ac0630fe96b8dd456e9cb509 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 4 Mar 2022 20:21:14 -0500 Subject: fix regression with mod XNB files in the content pipeline (#766) --- src/SMAPI/Framework/ContentManagers/ModContentManager.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/SMAPI/Framework/ContentManagers') diff --git a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs index 50ea6e61..2c47f14c 100644 --- a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs @@ -79,7 +79,6 @@ namespace StardewModdingAPI.Framework.ContentManagers public override T Load(string assetName, LanguageCode language, bool useCache) { // normalize key - bool isXnbFile = Path.GetExtension(assetName).ToLower() == ".xnb"; IAssetName parsedName = this.Coordinator.ParseAssetName(assetName); // disable caching @@ -111,7 +110,7 @@ namespace StardewModdingAPI.Framework.ContentManagers try { // get file - FileInfo file = this.GetModFile(isXnbFile ? $"{parsedName}.xnb" : parsedName.Name); // .xnb extension is stripped from asset names passed to the content manager + FileInfo file = this.GetModFile(parsedName.Name); if (!file.Exists) throw GetContentError("the specified path doesn't exist."); @@ -121,11 +120,16 @@ namespace StardewModdingAPI.Framework.ContentManagers // XNB file case ".xnb": { - asset = this.RawLoad(parsedName.Name, useCache: false); + // the underlying content manager adds a .xnb extension implicitly, so + // we need to strip it here to avoid trying to load a '.xnb.xnb' file. + string loadName = parsedName.Name[..^".xnb".Length]; + + // load asset + asset = this.RawLoad(loadName, useCache: false); if (asset is Map map) { - map.assetPath = parsedName.Name; - this.FixTilesheetPaths(map, relativeMapPath: parsedName.Name, fixEagerPathPrefixes: true); + map.assetPath = loadName; + this.FixTilesheetPaths(map, relativeMapPath: loadName, fixEagerPathPrefixes: true); } } break; -- cgit From b0d8b23c2c53ea3aafd60b0597a7562ac1708a42 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 5 Mar 2022 15:31:06 -0500 Subject: migrate more internal code to IAssetName (#766) --- .../ContentManagers/BaseContentManager.cs | 54 ++++++------- .../ContentManagers/GameContentManager.cs | 88 +++++++++++----------- .../GameContentManagerForAssetPropagation.cs | 2 +- .../Framework/ContentManagers/IContentManager.cs | 12 +-- .../Framework/ContentManagers/ModContentManager.cs | 64 ++++++++-------- 5 files changed, 101 insertions(+), 119 deletions(-) (limited to 'src/SMAPI/Framework/ContentManagers') diff --git a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs index 26f0921d..3efc33bb 100644 --- a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs @@ -93,36 +93,30 @@ namespace StardewModdingAPI.Framework.ContentManagers } /// - public override T Load(string assetName) + [Obsolete("This method is implemented for the base game and should not be used directly. To load an asset from the underlying content manager directly, use " + nameof(BaseContentManager.RawLoad) + " instead.")] + public override T LoadBase(string assetName) { - return this.Load(assetName, this.Language, useCache: true); + return this.Load(assetName, LanguageCode.en); } /// - public override T Load(string assetName, LanguageCode language) + public override T Load(string assetName) { - return this.Load(assetName, language, useCache: true); + return this.Load(assetName, this.Language); } /// - public abstract T Load(string assetName, LocalizedContentManager.LanguageCode language, bool useCache); - - /// - [Obsolete("This method is implemented for the base game and should not be used directly. To load an asset from the underlying content manager directly, use " + nameof(BaseContentManager.RawLoad) + " instead.")] - public override T LoadBase(string assetName) + public override T Load(string assetName, LanguageCode language) { - return this.Load(assetName, LanguageCode.en, useCache: true); + IAssetName parsedName = this.Coordinator.ParseAssetName(assetName); + return this.Load(parsedName, language, useCache: true); } /// - public virtual void OnLocaleChanged() { } + public abstract T Load(IAssetName assetName, LanguageCode language, bool useCache); /// - [Pure] - public string NormalizePathSeparators(string path) - { - return this.Cache.NormalizePathSeparators(path); - } + public virtual void OnLocaleChanged() { } /// [SuppressMessage("ReSharper", "ParameterOnlyUsedForPreconditionCheck.Local", Justification = "Parameter is only used for assertion checks by design.")] @@ -154,11 +148,7 @@ namespace StardewModdingAPI.Framework.ContentManagers } /// - public bool IsLoaded(string assetName, LanguageCode language) - { - assetName = this.Cache.NormalizeKey(assetName); - return this.IsNormalizedKeyLoaded(assetName, language); - } + public abstract bool IsLoaded(IAssetName assetName, LanguageCode language); /**** ** Cache invalidation @@ -233,6 +223,14 @@ namespace StardewModdingAPI.Framework.ContentManagers /********* ** Private methods *********/ + /// Normalize path separators in a file path. For asset keys, see instead. + /// The file path to normalize. + [Pure] + protected string NormalizePathSeparators(string path) + { + return this.Cache.NormalizePathSeparators(path); + } + /// Load an asset file directly from the underlying content manager. /// The type of asset to load. /// The normalized asset key. @@ -250,26 +248,18 @@ namespace StardewModdingAPI.Framework.ContentManagers /// The asset value. /// The language code for which to inject the asset. /// Whether to save the asset to the asset cache. - protected virtual void TrackAsset(string assetName, T value, LanguageCode language, bool useCache) + protected virtual void TrackAsset(IAssetName assetName, T value, LanguageCode language, bool useCache) { // track asset key if (value is Texture2D texture) - texture.Name = assetName; + texture.Name = assetName.Name; // cache asset if (useCache) - { - assetName = this.AssertAndNormalizeAssetName(assetName); - this.Cache[assetName] = value; - } + this.Cache[assetName.Name] = value; // avoid hard disposable references; see remarks on the field this.BaseDisposableReferences.Clear(); } - - /// Get whether an asset has already been loaded. - /// The normalized asset name. - /// The language to check. - protected abstract bool IsNormalizedKeyLoaded(string normalizedAssetName, LanguageCode language); } } diff --git a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs index 0ca9e277..9f686f97 100644 --- a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs @@ -63,7 +63,7 @@ namespace StardewModdingAPI.Framework.ContentManagers } /// - public override T Load(string assetName, LocalizedContentManager.LanguageCode language, bool useCache) + public override T Load(IAssetName assetName, LanguageCode language, bool useCache) { // raise first-load callback if (GameContentManager.IsFirstLoad) @@ -73,49 +73,62 @@ namespace StardewModdingAPI.Framework.ContentManagers } // normalize asset name - IAssetName parsedName = this.Coordinator.ParseAssetName(assetName); - if (parsedName.LanguageCode.HasValue) - return this.Load(parsedName.BaseName, parsedName.LanguageCode.Value, useCache); + if (assetName.LanguageCode.HasValue) + return this.Load(this.Coordinator.ParseAssetName(assetName.BaseName), assetName.LanguageCode.Value, useCache); // get from cache - if (useCache && this.IsLoaded(parsedName.Name, language)) - return this.RawLoad(parsedName.Name, language, useCache: true); + if (useCache && this.IsLoaded(assetName, language)) + return this.RawLoad(assetName, language, useCache: true); // get managed asset - if (this.Coordinator.TryParseManagedAssetKey(parsedName.Name, out string contentManagerID, out string relativePath)) + if (this.Coordinator.TryParseManagedAssetKey(assetName.Name, out string contentManagerID, out IAssetName relativePath)) { T managedAsset = this.Coordinator.LoadManagedAsset(contentManagerID, relativePath); - this.TrackAsset(parsedName.Name, managedAsset, language, useCache); + this.TrackAsset(assetName, managedAsset, language, useCache); return managedAsset; } // load asset T data; - if (this.AssetsBeingLoaded.Contains(parsedName.Name)) + if (this.AssetsBeingLoaded.Contains(assetName.Name)) { - this.Monitor.Log($"Broke loop while loading asset '{parsedName.Name}'.", LogLevel.Warn); + this.Monitor.Log($"Broke loop while loading asset '{assetName}'.", LogLevel.Warn); this.Monitor.Log($"Bypassing mod loaders for this asset. Stack trace:\n{Environment.StackTrace}"); - data = this.RawLoad(parsedName.Name, language, useCache); + data = this.RawLoad(assetName, language, useCache); } else { - data = this.AssetsBeingLoaded.Track(parsedName.Name, () => + data = this.AssetsBeingLoaded.Track(assetName.Name, () => { string locale = this.GetLocale(language); - IAssetInfo info = new AssetInfo(locale, parsedName, typeof(T), this.AssertAndNormalizeAssetName); + IAssetInfo info = new AssetInfo(locale, assetName, typeof(T), this.AssertAndNormalizeAssetName); IAssetData asset = this.ApplyLoader(info) - ?? new AssetDataForObject(info, this.RawLoad(parsedName.Name, language, useCache), this.AssertAndNormalizeAssetName); + ?? new AssetDataForObject(info, this.RawLoad(assetName, language, useCache), this.AssertAndNormalizeAssetName); asset = this.ApplyEditors(info, asset); return (T)asset.Data; }); } // update cache & return data - this.TrackAsset(parsedName.Name, data, language, useCache); + this.TrackAsset(assetName, data, language, useCache); return data; } + /// + public override bool IsLoaded(IAssetName assetName, LanguageCode language) + { + string cachedKey = null; + bool localized = + language != LanguageCode.en + && !this.Coordinator.IsManagedAssetKey(assetName) + && this.LocalizedAssetNames.TryGetValue(assetName.Name, out cachedKey); + + return localized + ? this.Cache.ContainsKey(cachedKey) + : this.Cache.ContainsKey(assetName.Name); + } + /// public override void OnLocaleChanged() { @@ -153,28 +166,13 @@ namespace StardewModdingAPI.Framework.ContentManagers ** Private methods *********/ /// - protected override bool IsNormalizedKeyLoaded(string normalizedAssetName, LanguageCode language) - { - string cachedKey = null; - bool localized = - language != LocalizedContentManager.LanguageCode.en - && !this.Coordinator.IsManagedAssetKey(normalizedAssetName) - && this.LocalizedAssetNames.TryGetValue(normalizedAssetName, out cachedKey); - - return localized - ? this.Cache.ContainsKey(cachedKey) - : this.Cache.ContainsKey(normalizedAssetName); - } - - /// - protected override void TrackAsset(string assetName, T value, LanguageCode language, bool useCache) + protected override void TrackAsset(IAssetName assetName, T value, LanguageCode language, bool useCache) { // handle explicit language in asset name { - IAssetName parsedName = this.Coordinator.ParseAssetName(assetName); - if (parsedName.LanguageCode.HasValue) + if (assetName.LanguageCode.HasValue) { - this.TrackAsset(parsedName.BaseName, value, parsedName.LanguageCode.Value, useCache); + this.TrackAsset(this.Coordinator.ParseAssetName(assetName.BaseName), value, assetName.LanguageCode.Value, useCache); return; } } @@ -188,16 +186,16 @@ namespace StardewModdingAPI.Framework.ContentManagers // doesn't change the instance stored in the cache, e.g. using `asset.ReplaceWith`. if (useCache) { - string translatedKey = $"{assetName}.{this.GetLocale(language)}"; + IAssetName translatedKey = new AssetName(assetName.Name, this.GetLocale(language), language); base.TrackAsset(assetName, value, language, useCache: true); - if (this.Cache.ContainsKey(translatedKey)) + if (this.Cache.ContainsKey(translatedKey.Name)) base.TrackAsset(translatedKey, value, language, useCache: true); // track whether the injected asset is translatable for is-loaded lookups - if (this.Cache.ContainsKey(translatedKey)) - this.LocalizedAssetNames[assetName] = translatedKey; - else if (this.Cache.ContainsKey(assetName)) - this.LocalizedAssetNames[assetName] = assetName; + if (this.Cache.ContainsKey(translatedKey.Name)) + this.LocalizedAssetNames[assetName.Name] = translatedKey.Name; + else if (this.Cache.ContainsKey(assetName.Name)) + this.LocalizedAssetNames[assetName.Name] = assetName.Name; else this.Monitor.Log($"Asset '{assetName}' could not be found in the cache immediately after injection.", LogLevel.Error); } @@ -209,32 +207,32 @@ namespace StardewModdingAPI.Framework.ContentManagers /// The language code for which to load content. /// Whether to read/write the loaded asset to the asset cache. /// Derived from . - private T RawLoad(string assetName, LanguageCode language, bool useCache) + private T RawLoad(IAssetName assetName, LanguageCode language, bool useCache) { try { // use cached key - if (language == this.Language && this.LocalizedAssetNames.TryGetValue(assetName, out string cachedKey)) + if (language == this.Language && this.LocalizedAssetNames.TryGetValue(assetName.Name, out string cachedKey)) return base.RawLoad(cachedKey, useCache); // try translated key - if (language != LocalizedContentManager.LanguageCode.en) + if (language != LanguageCode.en) { string translatedKey = $"{assetName}.{this.GetLocale(language)}"; try { T obj = base.RawLoad(translatedKey, useCache); - this.LocalizedAssetNames[assetName] = translatedKey; + this.LocalizedAssetNames[assetName.Name] = translatedKey; return obj; } catch (ContentLoadException) { - this.LocalizedAssetNames[assetName] = assetName; + this.LocalizedAssetNames[assetName.Name] = assetName.Name; } } // try base asset - return base.RawLoad(assetName, useCache); + return base.RawLoad(assetName.Name, useCache); } catch (ContentLoadException ex) when (ex.InnerException is FileNotFoundException innerEx && innerEx.InnerException == null) { diff --git a/src/SMAPI/Framework/ContentManagers/GameContentManagerForAssetPropagation.cs b/src/SMAPI/Framework/ContentManagers/GameContentManagerForAssetPropagation.cs index 61683ce6..206ece30 100644 --- a/src/SMAPI/Framework/ContentManagers/GameContentManagerForAssetPropagation.cs +++ b/src/SMAPI/Framework/ContentManagers/GameContentManagerForAssetPropagation.cs @@ -25,7 +25,7 @@ namespace StardewModdingAPI.Framework.ContentManagers : base(name, serviceProvider, rootDirectory, currentCulture, coordinator, monitor, reflection, onDisposing, onLoadingFirstAsset, aggressiveMemoryOptimizations) { } /// - public override T Load(string assetName, LanguageCode language, bool useCache) + public override T Load(IAssetName assetName, LanguageCode language, bool useCache) { T data = base.Load(assetName, language, useCache); diff --git a/src/SMAPI/Framework/ContentManagers/IContentManager.cs b/src/SMAPI/Framework/ContentManagers/IContentManager.cs index ba7dbc06..fe0519b6 100644 --- a/src/SMAPI/Framework/ContentManagers/IContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/IContentManager.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics.Contracts; using Microsoft.Xna.Framework.Content; using StardewModdingAPI.Framework.Exceptions; using StardewValley; @@ -31,15 +30,10 @@ namespace StardewModdingAPI.Framework.ContentManagers *********/ /// Load an asset that has been processed by the content pipeline. /// The type of asset to load. - /// The asset path relative to the loader root directory, not including the .xnb extension. + /// The asset name relative to the loader root directory. /// The language code for which to load content. /// Whether to read/write the loaded asset to the asset cache. - T Load(string assetName, LocalizedContentManager.LanguageCode language, bool useCache); - - /// Normalize path separators in a file path. For asset keys, see instead. - /// The file path to normalize. - [Pure] - string NormalizePathSeparators(string path); + T Load(IAssetName assetName, LocalizedContentManager.LanguageCode language, bool useCache); /// Assert that the given key has a valid format and return a normalized form consistent with the underlying cache. /// The asset key to check. @@ -56,7 +50,7 @@ namespace StardewModdingAPI.Framework.ContentManagers /// Get whether the content manager has already loaded and cached the given asset. /// The asset path relative to the loader root directory, not including the .xnb extension. /// The language. - bool IsLoaded(string assetName, LocalizedContentManager.LanguageCode language); + bool IsLoaded(IAssetName assetName, LocalizedContentManager.LanguageCode language); /// Purge matched assets from the cache. /// Matches the asset keys to invalidate. diff --git a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs index 2c47f14c..267146ad 100644 --- a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs @@ -66,21 +66,19 @@ namespace StardewModdingAPI.Framework.ContentManagers /// public override T Load(string assetName) { - return this.Load(assetName, this.DefaultLanguage, useCache: false); + return this.Load(assetName, this.DefaultLanguage); } /// public override T Load(string assetName, LanguageCode language) { - return this.Load(assetName, language, useCache: false); + IAssetName parsedName = this.Coordinator.ParseAssetName(assetName); + return this.Load(parsedName, language, useCache: false); } /// - public override T Load(string assetName, LanguageCode language, bool useCache) + public override T Load(IAssetName assetName, LanguageCode language, bool useCache) { - // normalize key - IAssetName parsedName = this.Coordinator.ParseAssetName(assetName); - // disable caching // This is necessary to avoid assets being shared between content managers, which can // cause changes to an asset through one content manager affecting the same asset in @@ -96,21 +94,21 @@ namespace StardewModdingAPI.Framework.ContentManagers // resolve managed asset key { - if (this.Coordinator.TryParseManagedAssetKey(parsedName.Name, out string contentManagerID, out string relativePath)) + if (this.Coordinator.TryParseManagedAssetKey(assetName.Name, out string contentManagerID, out IAssetName relativePath)) { if (contentManagerID != this.Name) - throw new SContentLoadException($"Can't load managed asset key '{parsedName}' through content manager '{this.Name}' for a different mod."); - parsedName = this.Coordinator.ParseAssetName(relativePath); + throw new SContentLoadException($"Can't load managed asset key '{assetName}' through content manager '{this.Name}' for a different mod."); + assetName = relativePath; } } // get local asset - SContentLoadException GetContentError(string reasonPhrase) => new SContentLoadException($"Failed loading asset '{parsedName}' from {this.Name}: {reasonPhrase}"); + SContentLoadException GetContentError(string reasonPhrase) => new($"Failed loading asset '{assetName}' from {this.Name}: {reasonPhrase}"); T asset; try { // get file - FileInfo file = this.GetModFile(parsedName.Name); + FileInfo file = this.GetModFile(assetName.Name); if (!file.Exists) throw GetContentError("the specified path doesn't exist."); @@ -122,7 +120,7 @@ namespace StardewModdingAPI.Framework.ContentManagers { // the underlying content manager adds a .xnb extension implicitly, so // we need to strip it here to avoid trying to load a '.xnb.xnb' file. - string loadName = parsedName.Name[..^".xnb".Length]; + string loadName = assetName.Name[..^".xnb".Length]; // load asset asset = this.RawLoad(loadName, useCache: false); @@ -177,8 +175,8 @@ namespace StardewModdingAPI.Framework.ContentManagers // fetch & cache FormatManager formatManager = FormatManager.Instance; Map map = formatManager.LoadMap(file.FullName); - map.assetPath = parsedName.Name; - this.FixTilesheetPaths(map, relativeMapPath: parsedName.Name, fixEagerPathPrefixes: false); + map.assetPath = assetName.Name; + this.FixTilesheetPaths(map, relativeMapPath: assetName.Name, fixEagerPathPrefixes: false); asset = (T)(object)map; } break; @@ -189,14 +187,20 @@ namespace StardewModdingAPI.Framework.ContentManagers } catch (Exception ex) when (!(ex is SContentLoadException)) { - throw new SContentLoadException($"The content manager failed loading content asset '{parsedName}' from {this.Name}.", ex); + throw new SContentLoadException($"The content manager failed loading content asset '{assetName}' from {this.Name}.", ex); } // track & return asset - this.TrackAsset(parsedName.Name, asset, language, useCache); + this.TrackAsset(assetName, asset, language, useCache); return asset; } + /// + public override bool IsLoaded(IAssetName assetName, LanguageCode language) + { + return this.Cache.ContainsKey(assetName.Name); + } + /// public override LocalizedContentManager CreateTemporary() { @@ -206,23 +210,19 @@ namespace StardewModdingAPI.Framework.ContentManagers /// Get the underlying key in the game's content cache for an asset. This does not validate whether the asset exists. /// The local path to a content file relative to the mod folder. /// The is empty or contains invalid characters. - public string GetInternalAssetKey(string key) + public IAssetName GetInternalAssetKey(string key) { FileInfo file = this.GetModFile(key); string relativePath = PathUtilities.GetRelativePath(this.RootDirectory, file.FullName); - return Path.Combine(this.Name, relativePath); + string internalKey = Path.Combine(this.Name, relativePath); + + return this.Coordinator.ParseAssetName(internalKey); } /********* ** Private methods *********/ - /// - protected override bool IsNormalizedKeyLoaded(string normalizedAssetName, LanguageCode language) - { - return this.Cache.ContainsKey(normalizedAssetName); - } - /// Get a file from the mod folder. /// The asset path relative to the content folder. private FileInfo GetModFile(string path) @@ -304,15 +304,15 @@ namespace StardewModdingAPI.Framework.ContentManagers // load best match try { - if (!this.TryGetTilesheetAssetName(relativeMapFolder, imageSource, out string assetName, out string error)) + if (!this.TryGetTilesheetAssetName(relativeMapFolder, imageSource, out IAssetName assetName, out string error)) throw new SContentLoadException($"{errorPrefix} {error}"); - if (assetName != tilesheet.ImageSource) + if (!assetName.IsEquivalentTo(tilesheet.ImageSource)) this.Monitor.VerboseLog($" Mapped tilesheet '{tilesheet.ImageSource}' to '{assetName}'."); - tilesheet.ImageSource = assetName; + tilesheet.ImageSource = assetName.Name; } - catch (Exception ex) when (!(ex is SContentLoadException)) + catch (Exception ex) when (ex is not SContentLoadException) { throw new SContentLoadException($"{errorPrefix} The tilesheet couldn't be loaded.", ex); } @@ -326,7 +326,7 @@ namespace StardewModdingAPI.Framework.ContentManagers /// A message indicating why the file couldn't be loaded. /// Returns whether the asset name was found. /// See remarks on . - private bool TryGetTilesheetAssetName(string modRelativeMapFolder, string relativePath, out string assetName, out string error) + private bool TryGetTilesheetAssetName(string modRelativeMapFolder, string relativePath, out IAssetName assetName, out string error) { assetName = null; error = null; @@ -334,7 +334,7 @@ namespace StardewModdingAPI.Framework.ContentManagers // nothing to do if (string.IsNullOrWhiteSpace(relativePath)) { - assetName = relativePath; + assetName = null; return true; } @@ -358,7 +358,7 @@ namespace StardewModdingAPI.Framework.ContentManagers } // get from game assets - string contentKey = this.GetContentKeyForTilesheetImageSource(relativePath); + IAssetName contentKey = this.Coordinator.ParseAssetName(this.GetContentKeyForTilesheetImageSource(relativePath)); try { this.GameContentManager.Load(contentKey, this.Language, useCache: true); // no need to bypass cache here, since we're not storing the asset @@ -374,7 +374,7 @@ namespace StardewModdingAPI.Framework.ContentManagers // if the content file doesn't exist, that doesn't mean the error here is a // content-not-found error. Unfortunately XNA doesn't provide a good way to // detect the error type. - if (this.GetContentFolderFileExists(contentKey)) + if (this.GetContentFolderFileExists(contentKey.Name)) throw; } -- cgit From b68b301b712a0047a7d6f24aa2fb776e19f8086d Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 5 Mar 2022 15:38:05 -0500 Subject: add DoesAssetExist to support the upcoming Stardew Valley 1.6 (#766) --- .../ContentManagers/BaseContentManager.cs | 6 +++ .../ContentManagers/GameContentManager.cs | 58 ++++++++++++++++------ .../Framework/ContentManagers/IContentManager.cs | 4 ++ .../Framework/ContentManagers/ModContentManager.cs | 10 ++++ 4 files changed, 64 insertions(+), 14 deletions(-) (limited to 'src/SMAPI/Framework/ContentManagers') diff --git a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs index 3efc33bb..030c60a7 100644 --- a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs @@ -92,6 +92,12 @@ namespace StardewModdingAPI.Framework.ContentManagers this.BaseDisposableReferences = reflection.GetField>(this, "disposableAssets").GetValue(); } + /// + public virtual bool DoesAssetExist(IAssetName assetName) + { + return this.Cache.ContainsKey(assetName.Name); + } + /// [Obsolete("This method is implemented for the base game and should not be used directly. To load an asset from the underlying content manager directly, use " + nameof(BaseContentManager.RawLoad) + " instead.")] public override T LoadBase(string assetName) diff --git a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs index 9f686f97..e7fb0c5f 100644 --- a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs @@ -62,6 +62,29 @@ namespace StardewModdingAPI.Framework.ContentManagers this.OnLoadingFirstAsset = onLoadingFirstAsset; } + /// + public override bool DoesAssetExist(IAssetName assetName) + { + if (base.DoesAssetExist(assetName)) + return true; + + // managed asset + if (this.Coordinator.TryParseManagedAssetKey(assetName.Name, out string contentManagerID, out IAssetName relativePath)) + return this.Coordinator.DoesManagedAssetExist(contentManagerID, relativePath); + + // else check for loaders + string locale = this.GetLocale(); + IAssetInfo info = new AssetInfo(locale, assetName, typeof(object), this.AssertAndNormalizeAssetName); + ModLinked[] loaders = this.GetLoaders(info).ToArray(); + if (loaders.Length > 1) + { + string[] loaderNames = loaders.Select(p => p.Mod.DisplayName).ToArray(); + this.Monitor.Log($"Multiple mods want to provide the '{info.Name}' asset ({stri