Merge pull request #76 from fmauNeko/add_get_icon_helper

Add GetIcon helper
This commit is contained in:
goaaats 2020-04-09 17:57:42 +09:00 committed by GitHub
commit e16b371ac3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 89 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,59 @@ 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(this.language, 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(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);
}
/// <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,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;
}
}
}