using System; using System.Collections.Generic; using Microsoft.Xna.Framework.Graphics; namespace StardewModdingAPI.Framework.Content { internal class AssetInfo : IAssetInfo { /********* ** Fields *********/ /// Normalizes an asset key to match the cache key. protected readonly Func GetNormalizedPath; /********* ** Accessors *********/ /// public string Locale { get; } /// public string AssetName { get; } /// public Type DataType { get; } /********* ** Public methods *********/ /// Construct an instance. /// The content's locale code, if the content is localized. /// The normalized asset name being read. /// The content type being read. /// Normalizes an asset key to match the cache key. public AssetInfo(string locale, string assetName, Type type, Func getNormalizedPath) { this.Locale = locale; this.AssetName = assetName; this.DataType = type; this.GetNormalizedPath = getNormalizedPath; } /// public bool AssetNameEquals(string path) { path = this.GetNormalizedPath(path); return this.AssetName.Equals(path, StringComparison.OrdinalIgnoreCase); } /********* ** 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; } } }