diff options
Diffstat (limited to 'src/SMAPI/Framework/ContentManagers')
5 files changed, 174 insertions, 134 deletions
diff --git a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs index 26f0921d..030c60a7 100644 --- a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs @@ -93,36 +93,36 @@ namespace StardewModdingAPI.Framework.ContentManagers } /// <inheritdoc /> - public override T Load<T>(string assetName) + public virtual bool DoesAssetExist(IAssetName assetName) { - return this.Load<T>(assetName, this.Language, useCache: true); + return this.Cache.ContainsKey(assetName.Name); } /// <inheritdoc /> - public override T Load<T>(string assetName, LanguageCode language) + [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<T>(string assetName) { - return this.Load<T>(assetName, language, useCache: true); + return this.Load<T>(assetName, LanguageCode.en); } /// <inheritdoc /> - public abstract T Load<T>(string assetName, LocalizedContentManager.LanguageCode language, bool useCache); + public override T Load<T>(string assetName) + { + return this.Load<T>(assetName, this.Language); + } /// <inheritdoc /> - [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<T>(string assetName) + public override T Load<T>(string assetName, LanguageCode language) { - return this.Load<T>(assetName, LanguageCode.en, useCache: true); + IAssetName parsedName = this.Coordinator.ParseAssetName(assetName); + return this.Load<T>(parsedName, language, useCache: true); } /// <inheritdoc /> - public virtual void OnLocaleChanged() { } + public abstract T Load<T>(IAssetName assetName, LanguageCode language, bool useCache); /// <inheritdoc /> - [Pure] - public string NormalizePathSeparators(string path) - { - return this.Cache.NormalizePathSeparators(path); - } + public virtual void OnLocaleChanged() { } /// <inheritdoc /> [SuppressMessage("ReSharper", "ParameterOnlyUsedForPreconditionCheck.Local", Justification = "Parameter is only used for assertion checks by design.")] @@ -154,11 +154,7 @@ namespace StardewModdingAPI.Framework.ContentManagers } /// <inheritdoc /> - 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 +229,14 @@ namespace StardewModdingAPI.Framework.ContentManagers /********* ** Private methods *********/ + /// <summary>Normalize path separators in a file path. For asset keys, see <see cref="AssertAndNormalizeAssetName"/> instead.</summary> + /// <param name="path">The file path to normalize.</param> + [Pure] + protected string NormalizePathSeparators(string path) + { + return this.Cache.NormalizePathSeparators(path); + } + /// <summary>Load an asset file directly from the underlying content manager.</summary> /// <typeparam name="T">The type of asset to load.</typeparam> /// <param name="assetName">The normalized asset key.</param> @@ -250,26 +254,18 @@ namespace StardewModdingAPI.Framework.ContentManagers /// <param name="value">The asset value.</param> /// <param name="language">The language code for which to inject the asset.</param> /// <param name="useCache">Whether to save the asset to the asset cache.</param> - protected virtual void TrackAsset<T>(string assetName, T value, LanguageCode language, bool useCache) + protected virtual void TrackAsset<T>(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(); } - - /// <summary>Get whether an asset has already been loaded.</summary> - /// <param name="normalizedAssetName">The normalized asset name.</param> - /// <param name="language">The language to check.</param> - 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..9b8125ad 100644 --- a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs @@ -63,7 +63,34 @@ namespace StardewModdingAPI.Framework.ContentManagers } /// <inheritdoc /> - public override T Load<T>(string assetName, LocalizedContentManager.LanguageCode language, bool useCache) + public override bool DoesAssetExist(IAssetName assetName) + { + if (base.DoesAssetExist(assetName)) + return true; + + // vanilla asset + if (File.Exists(Path.Combine(this.RootDirectory, $"{assetName.Name}.xnb"))) + return true; + + // managed asset + if (this.Coordinator.TryParseManagedAssetKey(assetName.Name, out string contentManagerID, out IAssetName relativePath)) + return this.Coordinator.DoesManagedAssetExist(contentManagerID, relativePath); + + // custom asset from a loader + string locale = this.GetLocale(); + IAssetInfo info = new AssetInfo(locale, assetName, typeof(object), this.AssertAndNormalizeAssetName); + ModLinked<IAssetLoader>[] loaders = this.GetLoaders<object>(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 ({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 loaders.Length == 1; + } + + /// <inheritdoc /> + public override T Load<T>(IAssetName assetName, LanguageCode language, bool useCache) { // raise first-load callback if (GameContentManager.IsFirstLoad) @@ -73,50 +100,63 @@ namespace StardewModdingAPI.Framework.ContentManagers } // normalize asset name - IAssetName parsedName = this.Coordinator.ParseAssetName(assetName); - if (parsedName.LanguageCode.HasValue) - return this.Load<T>(parsedName.BaseName, parsedName.LanguageCode.Value, useCache); + if (assetName.LanguageCode.HasValue) + return this.Load<T>(this.Coordinator.ParseAssetName(assetName.BaseName), assetName.LanguageCode.Value, useCache); // get from cache - if (useCache && this.IsLoaded(parsedName.Name, language)) - return this.RawLoad<T>(parsedName.Name, language, useCache: true); + if (useCache && this.IsLoaded(assetName, language)) + return this.RawLoad<T>(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<T>(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<T>(parsedName.Name, language, useCache); + data = this.RawLoad<T>(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<T>(info) - ?? new AssetDataForObject(info, this.RawLoad<T>(parsedName.Name, language, useCache), this.AssertAndNormalizeAssetName); + ?? new AssetDataForObject(info, this.RawLoad<T>(assetName, language, useCache), this.AssertAndNormalizeAssetName); asset = this.ApplyEditors<T>(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; } /// <inheritdoc /> + 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); + } + + /// <inheritdoc /> public override void OnLocaleChanged() { base.OnLocaleChanged(); @@ -153,28 +193,13 @@ namespace StardewModdingAPI.Framework.ContentManagers ** Private methods *********/ /// <inheritdoc /> - 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); - } - - /// <inheritdoc /> - protected override void TrackAsset<T>(string assetName, T value, LanguageCode language, bool useCache) + protected override void TrackAsset<T>(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 +213,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 +234,32 @@ namespace StardewModdingAPI.Framework.ContentManagers /// <param name="language">The language code for which to load content.</param> /// <param name="useCache">Whether to read/write the loaded asset to the asset cache.</param> /// <remarks>Derived from <see cref="LocalizedContentManager.Load{T}(string, LocalizedContentManager.LanguageCode)"/>.</remarks> - private T RawLoad<T>(string assetName, LanguageCode language, bool useCache) + private T RawLoad<T>(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<T>(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<T>(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<T>(assetName, useCache); + return base.RawLoad<T>(assetName.Name, useCache); } catch (ContentLoadException ex) when (ex.InnerException is FileNotFoundException innerEx && innerEx.InnerException == null) { @@ -248,20 +273,7 @@ namespace StardewModdingAPI.Framework.ContentManagers private IAssetData ApplyLoader<T>(IAssetInfo info) { // find matching loaders - var loaders = this.Loaders - .Where(entry => - { - try - { - return entry.Data.CanLoad<T>(info); - } - catch (Exception ex) - { - 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; - } - }) - .ToArray(); + var loaders = this.GetLoaders<T>(info).ToArray(); // validate loaders if (!loaders.Any()) @@ -362,6 +374,26 @@ namespace StardewModdingAPI.Framework.ContentManagers return asset; } + /// <summary>Get the asset loaders which handle the asset.</summary> + /// <typeparam name="T">The asset type.</typeparam> + /// <param name="info">The basic asset metadata.</param> + private IEnumerable<ModLinked<IAssetLoader>> GetLoaders<T>(IAssetInfo info) + { + return this.Loaders + .Where(entry => + { + try + { + return entry.Data.CanLoad<T>(info); + } + catch (Exception ex) + { + 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; + } + }); + } + /// <summary>Validate that an asset loaded by a mod is valid and won't cause issues, and fix issues if possible.</summary> /// <typeparam name="T">The asset type.</typeparam> /// <param name="info">The basic asset metadata.</param> 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) { } /// <inheritdoc /> - public override T Load<T>(string assetName, LanguageCode language, bool useCache) + public override T Load<T>(IAssetName assetName, LanguageCode language, bool useCache) { T data = base.Load<T>(assetName, language, useCache); diff --git a/src/SMAPI/Framework/ContentManagers/IContentManager.cs b/src/SMAPI/Framework/ContentManagers/IContentManager.cs index ba7dbc06..6d71472f 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; @@ -29,17 +28,16 @@ namespace StardewModdingAPI.Framework.ContentManagers /********* ** Methods *********/ + /// <summary>Get whether an asset exists and can be loaded.</summary> + /// <param name="assetName">The normalized asset name.</param> + bool DoesAssetExist(IAssetName assetName); + /// <summary>Load an asset that has been processed by the content pipeline.</summary> /// <typeparam name="T">The type of asset to load.</typeparam> - /// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param> + /// <param name="assetName">The asset name relative to the loader root directory.</param> /// <param name="language">The language code for which to load content.</param> /// <param name="useCache">Whether to read/write the loaded asset to the asset cache.</param> - T Load<T>(string assetName, LocalizedContentManager.LanguageCode language, bool useCache); - - /// <summary>Normalize path separators in a file path. For asset keys, see <see cref="AssertAndNormalizeAssetName"/> instead.</summary> - /// <param name="path">The file path to normalize.</param> - [Pure] - string NormalizePathSeparators(string path); + T Load<T>(IAssetName assetName, LocalizedContentManager.LanguageCode language, bool useCache); /// <summary>Assert that the given key has a valid format and return a normalized form consistent with the underlying cache.</summary> /// <param name="assetName">The asset key to check.</param> @@ -56,7 +54,7 @@ namespace StardewModdingAPI.Framework.ContentManagers /// <summary>Get whether the content manager has already loaded and cached the given asset.</summary> /// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param> /// <param name="language">The language.</param> - bool IsLoaded(string assetName, LocalizedContentManager.LanguageCode language); + bool IsLoaded(IAssetName assetName, LocalizedContentManager.LanguageCode language); /// <summary>Purge matched assets from the cache.</summary> /// <param name="predicate">Matches the asset keys to invalidate.</param> diff --git a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs index 50ea6e61..90836fcf 100644 --- a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs @@ -64,24 +64,31 @@ namespace StardewModdingAPI.Framework.ContentManagers } /// <inheritdoc /> - public override T Load<T>(string assetName) + public override bool DoesAssetExist(IAssetName assetName) { - return this.Load<T>(assetName, this.DefaultLanguage, useCache: false); + if (base.DoesAssetExist(assetName)) + return true; + + FileInfo file = this.GetModFile(assetName.Name); + return file.Exists; } /// <inheritdoc /> - public override T Load<T>(string assetName, LanguageCode language) + public override T Load<T>(string assetName) { - return this.Load<T>(assetName, language, useCache: false); + return this.Load<T>(assetName, this.DefaultLanguage); } /// <inheritdoc /> - public override T Load<T>(string assetName, LanguageCode language, bool useCache) + public override T Load<T>(string assetName, LanguageCode language) { - // normalize key - bool isXnbFile = Path.GetExtension(assetName).ToLower() == ".xnb"; IAssetName parsedName = this.Coordinator.ParseAssetName(assetName); + return this.Load<T>(parsedName, language, useCache: false); + } + /// <inheritdoc /> + public override T Load<T>(IAssetName assetName, LanguageCode language, bool useCache) + { // 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 @@ -97,21 +104,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(isXnbFile ? $"{parsedName}.xnb" : parsedName.Name); // .xnb extension is stripped from asset names passed to the content manager + FileInfo file = this.GetModFile(assetName.Name); if (!file.Exists) throw GetContentError("the specified path doesn't exist."); @@ -121,11 +128,16 @@ namespace StardewModdingAPI.Framework.ContentManagers // XNB file case ".xnb": { - asset = this.RawLoad<T>(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 = assetName.Name[..^".xnb".Length]; + + // load asset + asset = this.RawLoad<T>(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; @@ -173,8 +185,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; @@ -185,15 +197,21 @@ 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; } /// <inheritdoc /> + public override bool IsLoaded(IAssetName assetName, LanguageCode language) + { + return this.Cache.ContainsKey(assetName.Name); + } + + /// <inheritdoc /> public override LocalizedContentManager CreateTemporary() { throw new NotSupportedException("Can't create a temporary mod content manager."); @@ -202,23 +220,19 @@ namespace StardewModdingAPI.Framework.ContentManagers /// <summary>Get the underlying key in the game's content cache for an asset. This does not validate whether the asset exists.</summary> /// <param name="key">The local path to a content file relative to the mod folder.</param> /// <exception cref="ArgumentException">The <paramref name="key"/> is empty or contains invalid characters.</exception> - 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 *********/ - /// <inheritdoc /> - protected override bool IsNormalizedKeyLoaded(string normalizedAssetName, LanguageCode language) - { - return this.Cache.ContainsKey(normalizedAssetName); - } - /// <summary>Get a file from the mod folder.</summary> /// <param name="path">The asset path relative to the content folder.</param> private FileInfo GetModFile(string path) @@ -300,15 +314,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); } @@ -322,7 +336,7 @@ namespace StardewModdingAPI.Framework.ContentManagers /// <param name="error">A message indicating why the file couldn't be loaded.</param> /// <returns>Returns whether the asset name was found.</returns> /// <remarks>See remarks on <see cref="FixTilesheetPaths"/>.</remarks> - 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; @@ -330,7 +344,7 @@ namespace StardewModdingAPI.Framework.ContentManagers // nothing to do if (string.IsNullOrWhiteSpace(relativePath)) { - assetName = relativePath; + assetName = null; return true; } @@ -354,7 +368,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<Texture2D>(contentKey, this.Language, useCache: true); // no need to bypass cache here, since we're not storing the asset @@ -370,7 +384,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; } |