summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Content/AssetInfo.cs
blob: af0003008b67c1475231e71667efc1fcf2a39187 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
#if SMAPI_DEPRECATED
using StardewModdingAPI.Framework.Deprecations;
#endif

namespace StardewModdingAPI.Framework.Content
{
    internal class AssetInfo : IAssetInfo
    {
        /*********
        ** Fields
        *********/
        /// <summary>Normalizes an asset key to match the cache key.</summary>
        protected readonly Func<string, string> GetNormalizedPath;

        /// <summary>The backing field for <see cref="NameWithoutLocale"/>.</summary>
        private IAssetName? NameWithoutLocaleImpl;


        /*********
        ** Accessors
        *********/
        /// <inheritdoc />
        public string? Locale { get; }

        /// <inheritdoc />
        public IAssetName Name { get; }

        /// <inheritdoc />
        public IAssetName NameWithoutLocale => this.NameWithoutLocaleImpl ??= this.Name.GetBaseAssetName();

        /// <inheritdoc />
        public Type DataType { get; }

#if SMAPI_DEPRECATED
        /// <inheritdoc />
        [Obsolete($"Use {nameof(AssetInfo.Name)} or {nameof(AssetInfo.NameWithoutLocale)} instead. This property will be removed in SMAPI 4.0.0.")]
        public string AssetName
        {
            get
            {
                SCore.DeprecationManager.Warn(
                    source: null,
                    nounPhrase: $"{nameof(IAssetInfo)}.{nameof(IAssetInfo.AssetName)}",
                    version: "3.14.0",
                    severity: DeprecationLevel.Info,
                    unlessStackIncludes: new[]
                    {
                        $"{typeof(AssetInterceptorChange).FullName}.{nameof(AssetInterceptorChange.CanIntercept)}",
                        $"{typeof(ContentCoordinator).FullName}.{nameof(ContentCoordinator.GetAssetOperations)}"
                    }
                );

                return this.NameWithoutLocale.Name;
            }
        }
#endif


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="locale">The content's locale code, if the content is localized.</param>
        /// <param name="assetName">The asset name being read.</param>
        /// <param name="type">The content type being read.</param>
        /// <param name="getNormalizedPath">Normalizes an asset key to match the cache key.</param>
        public AssetInfo(string? locale, IAssetName assetName, Type type, Func<string, string> getNormalizedPath)
        {
            this.Locale = locale;
            this.Name = assetName;
            this.DataType = type;
            this.GetNormalizedPath = getNormalizedPath;
        }

#if SMAPI_DEPRECATED
        /// <inheritdoc />
        [Obsolete($"Use {nameof(Name)}.{nameof(IAssetName.IsEquivalentTo)} or {nameof(AssetInfo.NameWithoutLocale)}.{nameof(IAssetName.IsEquivalentTo)} instead. This method will be removed in SMAPI 4.0.0.")]
        public bool AssetNameEquals(string path)
        {
            SCore.DeprecationManager.Warn(
                source: null,
                nounPhrase: $"{nameof(IAssetInfo)}.{nameof(IAssetInfo.AssetNameEquals)}",
                version: "3.14.0",
                severity: DeprecationLevel.Info,
                unlessStackIncludes: new[]
                {
                    $"{typeof(AssetInterceptorChange).FullName}.{nameof(AssetInterceptorChange.CanIntercept)}",
                    $"{typeof(ContentCoordinator).FullName}.{nameof(ContentCoordinator.GetAssetOperations)}"
                }
            );


            return this.NameWithoutLocale.IsEquivalentTo(path);
        }
#endif


        /*********
        ** Protected methods
        *********/
        /// <summary>Get a human-readable type name.</summary>
        /// <param name="type">The type to name.</param>
        protected string GetFriendlyTypeName(Type type)
        {
            // dictionary
            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary<,>))
            {
                Type[] genericArgs = type.GetGenericArguments();
                return $"Dictionary<{this.GetFriendlyTypeName(genericArgs[0])}, {this.GetFriendlyTypeName(genericArgs[1])}>";
            }

            // texture
            if (type == typeof(Texture2D))
                return type.Name;

            // native type
            if (type == typeof(int))
                return "int";
            if (type == typeof(string))
                return "string";

            // default
            return type.FullName!;
        }
    }
}