CurrencyAlertClassic/KamiLib/Caching/IconCache.cs

60 lines
1.5 KiB
C#
Raw Normal View History

2023-10-13 09:38:52 +00:00
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Dalamud.Interface.Internal;
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
{
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);
var tex = Service.TextureProvider.GetFromGame(path);
2023-10-13 09:38:52 +00:00
iconTextures[iconId] = tex;
}
2023-10-13 09:38:52 +00:00
catch (Exception ex)
{
//PluginLog.LogError($"Failed loading texture for icon {iconId} - {ex.Message}");
2023-10-13 09:38:52 +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];
}
}