summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Content/AssetData.cs
blob: 0a86f54d86c5bc67f108b6311401d2996bb9fc2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;

namespace StardewModdingAPI.Framework.Content
{
    /// <summary>Base implementation for a content helper which encapsulates access and changes to content being read from a data file.</summary>
    /// <typeparam name="TValue">The interface value type.</typeparam>
    internal class AssetData<TValue> : AssetInfo, IAssetData<TValue>
    {
        /*********
        ** Properties
        *********/
        /// <summary>A callback to invoke when the data is replaced (if any).</summary>
        private readonly Action<TValue> OnDataReplaced;


        /*********
        ** Accessors
        *********/
        /// <summary>The content data being read.</summary>
        public TValue Data { get; protected set; }


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="locale">The content's locale code, if the content is localised.</param>
        /// <param name="assetName">The normalised asset name being read.</param>
        /// <param name="data">The content data being read.</param>
        /// <param name="getNormalisedPath">Normalises an asset key to match the cache key.</param>
        /// <param name="onDataReplaced">A callback to invoke when the data is replaced (if any).</param>
        public AssetData(string locale, string assetName, TValue data, Func<string, string> getNormalisedPath, Action<TValue> onDataReplaced)
            : base(locale, assetName, data.GetType(), getNormalisedPath)
        {
            this.Data = data;
            this.OnDataReplaced = onDataReplaced;
        }

        /// <summary>Replace the entire content value with the given value. This is generally not recommended, since it may break compatibility with other mods or different versions of the game.</summary>
        /// <param name="value">The new content value.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="value"/> is null.</exception>
        /// <exception cref="InvalidCastException">The <paramref name="value"/>'s type is not compatible with the loaded asset's type.</exception>
        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);
        }
    }
}