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 *********/ /// Normalizes an asset key to match the cache key. protected readonly Func GetNormalizedPath; /// The backing field for . private IAssetName? NameWithoutLocaleImpl; /********* ** Accessors *********/ /// public string? Locale { get; } /// public IAssetName Name { get; } /// public IAssetName NameWithoutLocale => this.NameWithoutLocaleImpl ??= this.Name.GetBaseAssetName(); /// public Type DataType { get; } #if SMAPI_DEPRECATED /// [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 *********/ /// Construct an instance. /// The content's locale code, if the content is localized. /// The asset name being read. /// The content type being read. /// Normalizes an asset key to match the cache key. public AssetInfo(string? locale, IAssetName assetName, Type type, Func getNormalizedPath) { this.Locale = locale; this.Name = assetName; this.DataType = type; this.GetNormalizedPath = getNormalizedPath; } #if SMAPI_DEPRECATED /// [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 *********/ /// Get a human-readable type name. /// The type to name. 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!; } } }