diff --git a/Dalamud/Data/DataManager.cs b/Dalamud/Data/DataManager.cs index 239cfccee..edc46ac23 100644 --- a/Dalamud/Data/DataManager.cs +++ b/Dalamud/Data/DataManager.cs @@ -6,7 +6,9 @@ using System.IO; using System.Net; using System.Net.Http; using System.Threading.Tasks; +using Dalamud.Data.LuminaExtensions; using Lumina.Data; +using Lumina.Data.Files; using Lumina.Excel; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -41,6 +43,8 @@ namespace Dalamud.Data private ClientLanguage language; + private const string IconFileFormat = "ui/icon/{0:D3}000/{1}{2:D6}.tex"; + public DataManager(ClientLanguage language) { // Set up default values so plugins do not null-reference when data is being loaded. @@ -131,6 +135,59 @@ namespace Dalamud.Data return this.gameData.Repositories.TryGetValue(filePath.Repository, out repository) ? repository.GetFile(filePath.Category, filePath) : default(T); } + /// + /// Get a containing the icon with the given ID. + /// + /// The icon ID. + /// The containing the icon. + public TexFile GetIcon(int iconId) + { + return GetIcon(this.language, iconId); + } + + /// + /// Get a containing the icon with the given ID, of the given language. + /// + /// The requested language. + /// The icon ID. + /// The containing the icon. + public TexFile GetIcon(ClientLanguage iconLanguage, int iconId) + { + var type = iconLanguage switch { + ClientLanguage.Japanese => "ja/", + ClientLanguage.English => "en/", + ClientLanguage.German => "de/", + ClientLanguage.French => "fr/", + _ => throw new ArgumentOutOfRangeException(nameof(this.language), + "Unknown Language: " + this.language) + }; + + return GetIcon(type, iconId); + } + + /// + /// Get a containing the icon with the given ID, of the given type. + /// + /// The type of the icon (e.g. 'hq' to get the HQ variant of an item icon). + /// The icon ID. + /// The containing the icon. + public TexFile GetIcon(string type, int iconId) + { + type ??= string.Empty; + if (type.Length > 0 && !type.EndsWith("/")) + type += "/"; + + var filePath = string.Format(IconFileFormat, iconId / 1000, type, iconId); + var file = this.GetFile(filePath); + + if (file != default(TexFile) || type.Length <= 0) return file; + + // Couldn't get specific type, try for generic version. + filePath = string.Format(IconFileFormat, iconId / 1000, string.Empty, iconId); + file = this.GetFile(filePath); + return file; + } + #endregion } } diff --git a/Dalamud/Data/LuminaExtensions/TexFileExtensions.cs b/Dalamud/Data/LuminaExtensions/TexFileExtensions.cs new file mode 100644 index 000000000..d2bb7b736 --- /dev/null +++ b/Dalamud/Data/LuminaExtensions/TexFileExtensions.cs @@ -0,0 +1,32 @@ +using ImGuiScene; +using Lumina.Data.Files; + +namespace Dalamud.Data.LuminaExtensions +{ + /// + /// Extensions to . + /// + public static class TexFileExtensions + { + /// + /// Returns the image data formatted for . + /// + /// The TexFile to format. + /// The formatted image data. + public static byte[] GetRgbaImageData(this TexFile texFile) + { + var imageData = texFile.ImageData; + var dst = new byte[imageData.Length]; + + for (var i = 0; i < dst.Length; i += 4) + { + dst[i] = imageData[i + 2]; + dst[i + 1] = imageData[i + 1]; + dst[i + 2] = imageData[i]; + dst[i + 3] = imageData[i + 3]; + } + + return dst; + } + } +}