using System; using System.IO; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using StardewModdingAPI.Framework.Serialisation; using StardewModdingAPI.Toolkit.Utilities; using xTile; namespace StardewModdingAPI.Framework { /// Manages access to a content pack's metadata and files. internal class ContentPack : IContentPack { /********* ** Properties *********/ /// Provides an API for loading content assets. private readonly IContentHelper Content; /// Encapsulates SMAPI's JSON file parsing. private readonly JsonHelper JsonHelper; /********* ** Accessors *********/ /// The full path to the content pack's folder. public string DirectoryPath { get; } /// The content pack's manifest. public IManifest Manifest { get; } /********* ** Public methods *********/ /// Construct an instance. /// The full path to the content pack's folder. /// The content pack's manifest. /// Provides an API for loading content assets. /// Encapsulates SMAPI's JSON file parsing. public ContentPack(string directoryPath, IManifest manifest, IContentHelper content, JsonHelper jsonHelper) { this.DirectoryPath = directoryPath; this.Manifest = manifest; this.Content = content; this.JsonHelper = jsonHelper; } /// Read a JSON file from the content pack folder. /// The model type. /// The file path relative to the contnet directory. /// Returns the deserialised model, or null if the file doesn't exist or is empty. public TModel ReadJsonFile(string path) where TModel : class { path = Path.Combine(this.DirectoryPath, PathUtilities.NormalisePathSeparators(path)); return this.JsonHelper.ReadJsonFile(path); } /// Load content from the content pack folder (if not already cached), and return it. When loading a .png file, this must be called outside the game's draw loop. /// The expected data type. The main supported types are , , and dictionaries; other types may be supported by the game's content pipeline. /// The local path to a content file relative to the content pack folder. /// The is empty or contains invalid characters. /// The content asset couldn't be loaded (e.g. because it doesn't exist). public T LoadAsset(string key) { return this.Content.Load(key, ContentSource.ModFolder); } /// Get the underlying key in the game's content cache for an asset. This can be used to load custom map tilesheets, but should be avoided when you can use the content API instead. This does not validate whether the asset exists. /// The the local path to a content file relative to the content pack folder. /// The is empty or contains invalid characters. public string GetActualAssetKey(string key) { return this.Content.GetActualAssetKey(key, ContentSource.ModFolder); } } }