Add GetIcon helper

This commit is contained in:
Florian Maunier 2020-04-09 09:23:17 +02:00
parent c9d7846d4c
commit 3bfc831167
No known key found for this signature in database
GPG key ID: 3919060DDEE2A555
3 changed files with 116 additions and 0 deletions

View file

@ -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,53 @@ namespace Dalamud.Data
return this.gameData.Repositories.TryGetValue(filePath.Repository, out repository) ? repository.GetFile<T>(filePath.Category, filePath) : default(T);
}
/// <summary>
/// Get a <see cref="TexFile"/> containing the icon with the given ID.
/// </summary>
/// <param name="iconId">The icon ID.</param>
/// <returns>The <see cref="TexFile"/> containing the icon.</returns>
public TexFile GetIcon(int iconId)
{
return GetIcon(string.Empty, iconId);
}
/// <summary>
/// Get a <see cref="TexFile"/> containing the icon with the given ID, of the given language.
/// </summary>
/// <param name="iconLanguage">The requested language.</param>
/// <param name="iconId">The icon ID.</param>
/// <returns>The <see cref="TexFile"/> containing the icon.</returns>
public TexFile GetIcon(Language iconLanguage, int iconId)
{
var type = iconLanguage.GetCode();
if (type.Length > 0)
type += "/";
return GetIcon(type, iconId);
}
/// <summary>
/// Get a <see cref="TexFile"/> containing the icon with the given ID, of the given type.
/// </summary>
/// <param name="type">The type of the icon (e.g. 'hq' to get the HQ variant of an item icon).</param>
/// <param name="iconId">The icon ID.</param>
/// <returns>The <see cref="TexFile"/> containing the icon.</returns>
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<TexFile>(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<TexFile>(filePath);
return file;
}
#endregion
}
}

View file

@ -0,0 +1,33 @@
using System.Collections.Generic;
using Lumina.Data;
namespace Dalamud.Data.LuminaExtensions
{
/// <summary>
/// Extensions to <see cref="Language"/>.
/// </summary>
public static class LanguageExtensions
{
private static readonly Dictionary<Language, string> LangToCode = new Dictionary<Language, string>()
{
{ Language.None, "" },
{ Language.Japanese, "ja" },
{ Language.English, "en" },
{ Language.German, "de" },
{ Language.French, "fr" },
{ Language.ChineseSimplified, "chs" },
{ Language.ChineseTraditional, "cht" },
{ Language.Korean, "ko" },
};
/// <summary>
/// Return the language code for a <see cref="Language"/>.
/// </summary>
/// <param name="language">The Language.</param>
/// <returns>The language code.</returns>
public static string GetCode(this Language language)
{
return LangToCode[language];
}
}
}

View file

@ -0,0 +1,32 @@
using ImGuiScene;
using Lumina.Data.Files;
namespace Dalamud.Data.LuminaExtensions
{
/// <summary>
/// Extensions to <see cref="TexFile"/>.
/// </summary>
public static class TexFileExtensions
{
/// <summary>
/// Returns the image data formatted for <see cref="RawDX11Scene.LoadImageRaw"/>.
/// </summary>
/// <param name="texFile">The TexFile to format.</param>
/// <returns>The formatted image data.</returns>
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;
}
}
}