using System; using System.Globalization; using System.IO; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using StardewModdingAPI.Framework.Exceptions; using StardewModdingAPI.Framework.Reflection; using StardewModdingAPI.Toolkit.Serialisation; using StardewValley; namespace StardewModdingAPI.Framework.ContentManagers { /// A content manager which handles reading files from a SMAPI mod folder with support for unpacked files. internal class ModContentManager : BaseContentManager { /********* ** Properties *********/ /// Encapsulates SMAPI's JSON file parsing. private readonly JsonHelper JsonHelper; /********* ** Public methods *********/ /// Construct an instance. /// A name for the mod manager. Not guaranteed to be unique. /// The service provider to use to locate services. /// The root directory to search for content. /// The current culture for which to localise content. /// The central coordinator which manages content managers. /// Encapsulates monitoring and logging. /// Simplifies access to private code. /// Encapsulates SMAPI's JSON file parsing. /// A callback to invoke when the content manager is being disposed. public ModContentManager(string name, IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, ContentCoordinator coordinator, IMonitor monitor, Reflector reflection, JsonHelper jsonHelper, Action onDisposing) : base(name, serviceProvider, rootDirectory, currentCulture, coordinator, monitor, reflection, onDisposing, isModFolder: true) { this.JsonHelper = jsonHelper; } /// 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 language code for which to load content. public override T Load(string assetName, LanguageCode language) { assetName = this.AssertAndNormaliseAssetName(assetName); // get from cache if (this.IsLoaded(assetName)) return base.Load(assetName, language); // get managed asset if (this.Coordinator.TryParseManagedAssetKey(assetName, out string contentManagerID, out string relativePath)) { if (contentManagerID != this.Name) { T data = this.Coordinator.LoadAndCloneManagedAsset(assetName, contentManagerID, relativePath, language); this.Inject(assetName, data); return data; } return this.LoadManagedAsset(assetName, contentManagerID, relativePath, language); } throw new NotSupportedException("Can't load content folder asset from a mod content manager."); } /// Create a new content manager for temporary use. public override LocalizedContentManager CreateTemporary() { throw new NotSupportedException("Can't create a temporary mod content manager."); } /********* ** Private methods *********/ /// Get whether an asset has already been loaded. /// The normalised asset name. protected override bool IsNormalisedKeyLoaded(string normalisedAssetName) { return this.Cache.ContainsKey(normalisedAssetName); } /// Load a managed mod asset. /// The type of asset to load. /// The internal asset key. /// The unique name for the content manager which should load this asset. /// The relative path within the mod folder. /// The language code for which to load content. private T LoadManagedAsset(string internalKey, string contentManagerID, string relativePath, LanguageCode language) { SContentLoadException GetContentError(string reasonPhrase) => new SContentLoadException($"Failed loading asset '{relativePath}' from {contentManagerID}: {reasonPhrase}"); try { // get file FileInfo file = this.GetModFile(relativePath); if (!file.Exists) throw GetContentError("the specified path doesn't exist."); // load content switch (file.Extension.ToLower()) { // XNB file case ".xnb": return base.Load(relativePath, language); // unpacked data case ".json": { if (!this.JsonHelper.ReadJsonFileIfExists(file.FullName, out T data)) throw GetContentError("the JSON file is invalid."); // should never happen since we check for file existence above return data; } // unpacked image case ".png": // validate if (typeof(T) != typeof(Texture2D)) throw GetContentError($"can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(Texture2D)}'."); // fetch & cache using (FileStream stream = File.OpenRead(file.FullName)) { Texture2D texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, stream); texture = this.PremultiplyTransparency(texture); this.Inject(internalKey, texture); return (T)(object)texture; } // unpacked map case ".tbin": throw GetContentError($"can't read unpacked map file directly from the underlying content manager. It must be loaded through the mod's {typeof(IModHelper)}.{nameof(IModHelper.Content)} helper."); default: throw GetContentError($"unknown file extension '{file.Extension}'; must be one of '.png', '.tbin', or '.xnb'."); } } catch (Exception ex) when (!(ex is SContentLoadException)) { if (ex.GetInnermostException() is DllNotFoundException dllEx && dllEx.Message == "libgdiplus.dylib") throw GetContentError("couldn't find libgdiplus, which is needed to load mod images. Make sure Mono is installed and you're running the game through the normal launcher."); throw new SContentLoadException($"The content manager failed loading content asset '{relativePath}' from {contentManagerID}.", ex); } } /// Get a file from the mod folder. /// The asset path relative to the content folder. private FileInfo GetModFile(string path) { // try exact match FileInfo file = new FileInfo(Path.Combine(this.FullRootDirectory, path)); // try with default extension if (!file.Exists && file.Extension.ToLower() != ".xnb") { FileInfo result = new FileInfo(file.FullName + ".xnb"); if (result.Exists) file = result; } return file; } /// Premultiply a texture's alpha values to avoid transparency issues in the game. This is only possible if the game isn't currently drawing. /// The texture to premultiply. /// Returns a premultiplied texture. /// Based on code by Layoric. private Texture2D PremultiplyTransparency(Texture2D texture) { // validate if (Context.IsInDrawLoop) throw new NotSupportedException("Can't load a PNG file while the game is drawing to the screen. Make sure you load content outside the draw loop."); // process texture SpriteBatch spriteBatch = Game1.spriteBatch; GraphicsDevice gpu = Game1.graphics.GraphicsDevice; using (RenderTarget2D renderTarget = new RenderTarget2D(Game1.graphics.GraphicsDevice, texture.Width, texture.Height)) { // create blank render target to premultiply gpu.SetRenderTarget(renderTarget); gpu.Clear(Color.Black); // multiply each color by the source alpha, and write just the color values into the final texture spriteBatch.Begin(SpriteSortMode.Immediate, new BlendState { ColorDestinationBlend = Blend.Zero, ColorWriteChannels = ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue, AlphaDestinationBlend = Blend.Zero, AlphaSourceBlend = Blend.SourceAlpha, ColorSourceBlend = Blend.SourceAlpha }); spriteBatch.Draw(texture, texture.Bounds, Color.White); spriteBatch.End(); // copy the alpha values from the source texture into the final one without multiplying them spriteBatch.Begin(SpriteSortMode.Immediate, new BlendState { ColorWriteChannels = ColorWriteChannels.Alpha, AlphaDestinationBlend = Blend.Zero, ColorDestinationBlend = Blend.Zero, AlphaSourceBlend = Blend.One, ColorSourceBlend = Blend.One }); spriteBatch.Draw(texture, texture.Bounds, Color.White); spriteBatch.End(); // release GPU gpu.SetRenderTarget(null); // extract premultiplied data Color[] data = new Color[texture.Width * texture.Height]; renderTarget.GetData(data); // unset texture from GPU to regain control gpu.Textures[0] = null; // update texture with premultiplied data texture.SetData(data); } return texture; } } }