using System; using System.Globalization; using Microsoft.Xna.Framework.Graphics; using StardewModdingAPI.Framework.Reflection; using StardewValley; namespace StardewModdingAPI.Framework.ContentManagers { /// An extension of specifically optimized for asset propagation. /// This avoids sharing an asset cache with or mods, so that assets can be safely disposed when the vanilla game no longer references them. internal class GameContentManagerForAssetPropagation : GameContentManager { /********* ** Fields *********/ /// A unique value used in to identify assets loaded through this instance. private readonly string Tag = $"Pathoschild.SMAPI/LoadedBy:{nameof(GameContentManagerForAssetPropagation)}"; /********* ** Public methods *********/ /// public GameContentManagerForAssetPropagation(string name, IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, ContentCoordinator coordinator, IMonitor monitor, Reflector reflection, Action onDisposing, Action onLoadingFirstAsset, Action onAssetLoaded) : base(name, serviceProvider, rootDirectory, currentCulture, coordinator, monitor, reflection, onDisposing, onLoadingFirstAsset, onAssetLoaded) { } /// public override T LoadExact(IAssetName assetName, bool useCache) { T data = base.LoadExact(assetName, useCache); if (data is Texture2D texture) texture.Tag = this.Tag; return data; } /// Get whether a texture was loaded by this content manager. /// The texture to check. public bool IsResponsibleFor(Texture2D? texture) { return texture?.Tag is string tag && tag.Contains(this.Tag); } } }