using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI.Framework.Reflection;
using xTile;
namespace StardewModdingAPI.Framework.Content
{
/// Encapsulates access and changes to content being read from a data file.
internal class AssetDataForObject : AssetData, IAssetData
{
/*********
** Fields
*********/
/// Simplifies access to private code.
private readonly Reflector Reflection;
/*********
** 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.
/// Simplifies access to private code.
/// A callback to invoke when the data is replaced (if any).
public AssetDataForObject(string? locale, IAssetName assetName, object data, Func getNormalizedPath, Reflector reflection, Action? onDataReplaced = null)
: base(locale, assetName, data, getNormalizedPath, onDataReplaced)
{
this.Reflection = reflection;
}
/// Construct an instance.
/// The asset metadata.
/// The content data being read.
/// Normalizes an asset key to match the cache key.
/// Simplifies access to private code.
/// A callback to invoke when the data is replaced (if any).
public AssetDataForObject(IAssetInfo info, object data, Func getNormalizedPath, Reflector reflection, Action? onDataReplaced = null)
: this(info.Locale, info.Name, data, getNormalizedPath, reflection, onDataReplaced) { }
///
public IAssetDataForDictionary AsDictionary()
{
return new AssetDataForDictionary(this.Locale, this.Name, this.GetData>(), this.GetNormalizedPath, this.ReplaceWith);
}
///
public IAssetDataForImage AsImage()
{
return new AssetDataForImage(this.Locale, this.Name, this.GetData(), this.GetNormalizedPath, this.ReplaceWith);
}
///
public IAssetDataForMap AsMap()
{
return new AssetDataForMap(this.Locale, this.Name, this.GetData(), this.GetNormalizedPath, this.ReplaceWith, this.Reflection);
}
///
public TData GetData()
{
if (this.Data is not TData data)
throw new InvalidCastException($"The content data of type {this.Data.GetType().FullName} can't be converted to the requested {typeof(TData).FullName}.");
return data;
}
}
}