2023-10-13 09:38:52 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Dalamud.Interface.Internal;
|
2024-07-09 21:01:17 +00:00
|
|
|
|
using Dalamud.Interface.Textures;
|
|
|
|
|
using Dalamud.Interface.Textures.TextureWraps;
|
2023-10-13 09:38:52 +00:00
|
|
|
|
using Dalamud.Logging;
|
|
|
|
|
using Dalamud.Utility;
|
|
|
|
|
using ImGuiScene;
|
|
|
|
|
|
|
|
|
|
namespace KamiLib.Caching;
|
|
|
|
|
|
|
|
|
|
public class IconCache : IDisposable
|
|
|
|
|
{
|
2024-07-09 21:01:17 +00:00
|
|
|
|
private readonly Dictionary<uint, ISharedImmediateTexture?> iconTextures = new();
|
2023-10-13 09:38:52 +00:00
|
|
|
|
|
|
|
|
|
private const string IconFilePath = "ui/icon/{0:D3}000/{1:D6}_hr1.tex";
|
|
|
|
|
|
|
|
|
|
private static IconCache? _instance;
|
|
|
|
|
public static IconCache Instance => _instance ??= new IconCache();
|
|
|
|
|
|
|
|
|
|
public static void Cleanup()
|
|
|
|
|
{
|
|
|
|
|
_instance?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
iconTextures.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadIconTexture(uint iconId)
|
|
|
|
|
{
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var path = IconFilePath.Format(iconId / 1000, iconId);
|
2024-07-09 21:01:17 +00:00
|
|
|
|
var tex = Service.TextureProvider.GetFromGame(path);
|
2023-10-13 09:38:52 +00:00
|
|
|
|
|
2024-07-09 21:01:17 +00:00
|
|
|
|
iconTextures[iconId] = tex;
|
|
|
|
|
}
|
2023-10-13 09:38:52 +00:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-07-09 21:01:17 +00:00
|
|
|
|
//PluginLog.LogError($"Failed loading texture for icon {iconId} - {ex.Message}");
|
2023-10-13 09:38:52 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-09 21:01:17 +00:00
|
|
|
|
public ISharedImmediateTexture? GetIcon(uint iconId)
|
2023-10-13 09:38:52 +00:00
|
|
|
|
{
|
|
|
|
|
if (iconTextures.TryGetValue(iconId, out var value)) return value;
|
|
|
|
|
|
|
|
|
|
iconTextures.Add(iconId, null);
|
|
|
|
|
LoadIconTexture(iconId);
|
|
|
|
|
|
|
|
|
|
return iconTextures[iconId];
|
|
|
|
|
}
|
|
|
|
|
}
|