using System;
namespace StardewModdingAPI.Framework.Content
{
/// Base implementation for a content helper which encapsulates access and changes to content being read from a data file.
/// The interface value type.
internal class AssetData : AssetInfo, IAssetData
{
/*********
** Fields
*********/
/// A callback to invoke when the data is replaced (if any).
private readonly Action OnDataReplaced;
/*********
** Accessors
*********/
///
public TValue Data { get; protected set; }
/*********
** Public methods
*********/
/// Construct an instance.
/// The content's locale code, if the content is localized.
/// The asset name being read.
/// The content data being read.
/// Normalizes an asset key to match the cache key.
/// A callback to invoke when the data is replaced (if any).
public AssetData(string locale, IAssetName assetName, TValue data, Func getNormalizedPath, Action onDataReplaced)
: base(locale, assetName, data.GetType(), getNormalizedPath)
{
this.Data = data;
this.OnDataReplaced = onDataReplaced;
}
///
public void ReplaceWith(TValue value)
{
if (value == null)
throw new ArgumentNullException(nameof(value), "Can't set a loaded asset to a null value.");
if (!this.DataType.IsInstanceOfType(value))
throw new InvalidCastException($"Can't replace loaded asset of type {this.GetFriendlyTypeName(this.DataType)} with value of type {this.GetFriendlyTypeName(value.GetType())}. The new type must be compatible to prevent game errors.");
this.Data = value;
this.OnDataReplaced?.Invoke(value);
}
}
}