using System; using System.Collections.Generic; using Microsoft.Xna.Framework.Graphics; namespace StardewModdingAPI.Framework.Content { /// Encapsulates access and changes to content being read from a data file. internal class AssetDataForObject : AssetData, IAssetData { /********* ** Public methods *********/ /// Construct an instance. /// The content's locale code, if the content is localised. /// The normalised asset name being read. /// The content data being read. /// Normalises an asset key to match the cache key. public AssetDataForObject(string locale, string assetName, object data, Func getNormalisedPath) : base(locale, assetName, data, getNormalisedPath, onDataReplaced: null) { } /// Construct an instance. /// The asset metadata. /// The content data being read. /// Normalises an asset key to match the cache key. public AssetDataForObject(IAssetInfo info, object data, Func getNormalisedPath) : this(info.Locale, info.AssetName, data, getNormalisedPath) { } /// Get a helper to manipulate the data as a dictionary. /// The expected dictionary key. /// The expected dictionary balue. /// The content being read isn't a dictionary. public IAssetDataForDictionary AsDictionary() { return new AssetDataForDictionary(this.Locale, this.AssetName, this.GetData>(), this.GetNormalisedPath, this.ReplaceWith); } /// Get a helper to manipulate the data as an image. /// The content being read isn't an image. public IAssetDataForImage AsImage() { return new AssetDataForImage(this.Locale, this.AssetName, this.GetData(), this.GetNormalisedPath, this.ReplaceWith); } /// Get the data as a given type. /// The expected data type. /// The data can't be converted to . public TData GetData() { if (!(this.Data is TData)) throw new InvalidCastException($"The content data of type {this.Data.GetType().FullName} can't be converted to the requested {typeof(TData).FullName}."); return (TData)this.Data; } } }