Skip to content

Commit

Permalink
Fix a long-standing icon bug
Browse files Browse the repository at this point in the history
- Also switch some struct things around
  • Loading branch information
KazWolfe committed Oct 3, 2023
1 parent a388fe0 commit cca5a77
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
16 changes: 7 additions & 9 deletions FFXIVPlugin/Game/Managers/HotbarManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ public static unsafe void PulseHotbarSlot(int hotbarId, int slotId) {

// Handle the main hotbar, which is a bit interesting as it can behave oddly at times.
var mainBarName = isCrossHotbar ? "_ActionCross" : "_ActionBar";
var mainBarPtr = Injections.GameGui.GetAddonByName(mainBarName);
var mainBar = (AddonActionBarBase*) Injections.GameGui.GetAddonByName(mainBarName);

if (mainBarPtr != nint.Zero) {
var activeHotbarId = *(byte*) (mainBarPtr + 0x23C); // offset to RaptureHotbarId

if (activeHotbarId == hotbarId) {
SafePulseBar((AddonActionBarBase*) mainBarPtr, slotId);
if (mainBar != null) {
if (mainBar->RaptureHotbarId == hotbarId) {
SafePulseBar(mainBar, slotId);
}
} else {
Injections.PluginLog.Debug($"Couldn't find main hotbar addon {mainBarName}!");
Expand All @@ -54,10 +52,10 @@ public static unsafe void PulseHotbarSlot(int hotbarId, int slotId) {
// And handle any extra visible normal hotbars
if (!isCrossHotbar) {
var actionBarName = $"_ActionBar{hotbarId:00}";
var actionBarPtr = Injections.GameGui.GetAddonByName(actionBarName);
var actionBar = (AddonActionBarBase*) Injections.GameGui.GetAddonByName(actionBarName);

if (actionBarPtr != nint.Zero) {
SafePulseBar((AddonActionBarBase*) actionBarPtr, slotId);
if (actionBar != null) {
SafePulseBar(actionBar, slotId);
} else {
Injections.PluginLog.Debug($"Couldn't find hotbar addon {actionBarName}");
}
Expand Down
23 changes: 15 additions & 8 deletions FFXIVPlugin/Game/Managers/IconManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,30 @@ namespace XIVDeck.FFXIVPlugin.Game.Managers;
public static class IconManager {
private const string IconFileFormat = "ui/icon/{0:D3}000/{1}{2:D6}{3}.tex";

private static string GetIconPath(string lang, int iconId, bool hq = false, bool highres = false, bool forceOriginal = false) {
private static string GetIconPath(string lang, int iconId, bool highres = false, bool forceOriginal = false) {
var useHqIcon = false;

if (iconId > 1_000_000) {
useHqIcon = true;
iconId -= 1_000_000;
}

var path = string.Format(IconFileFormat,
iconId / 1000, (hq ? "hq/" : "") + lang, iconId, highres ? "_hr1" : "");
iconId / 1000, (useHqIcon ? "hq/" : "") + lang, iconId, highres ? "_hr1" : "");

if (PenumbraIPC.Instance is {Enabled: true} && !forceOriginal && XIVDeckPlugin.Instance.Configuration.UsePenumbraIPC)
path = PenumbraIPC.Instance.ResolvePenumbraPath(path);

return path;
}

public static TexFile? GetIcon(string lang, int iconId, bool hq = false, bool highres = false) {
public static TexFile? GetIcon(string lang, int iconId, bool highres = false) {
TexFile? texFile;

if (lang.Length > 0 && !lang.EndsWith("/"))
lang += "/";

var texPath = GetIconPath(lang, iconId, hq, true);
var texPath = GetIconPath(lang, iconId, true);

if (Path.IsPathRooted(texPath)) {
Injections.PluginLog.Verbose($"Using on-disk asset {texPath}");
Expand All @@ -47,17 +54,17 @@ private static string GetIconPath(string lang, int iconId, bool hq = false, bool
switch (texFile) {
case null when lang.Length > 0:
Injections.PluginLog.Debug($"Couldn't get lang-specific icon for {texPath}, falling back to no-lang");
return GetIcon(string.Empty, iconId, hq, true);
return GetIcon(string.Empty, iconId, true);
case null when highres:
Injections.PluginLog.Debug($"Couldn't get highres icon for {texPath}, falling back to lowres");
return GetIcon(lang, iconId, hq);
return GetIcon(lang, iconId);
default:
return texFile;
}
}

public static string GetIconAsPngString(int iconId, bool hq = false) {
var icon = GetIcon("", iconId, hq, true) ?? GetIcon("", 0, hq, true)!;
public static string GetIconAsPngString(int iconId) {
var icon = GetIcon("", iconId, true) ?? GetIcon("", 0, true)!;

return "data:image/png;base64," + Convert.ToBase64String(icon.GetImage().ConvertToPng());
}
Expand Down
2 changes: 1 addition & 1 deletion FFXIVPlugin/Server/Controllers/HotbarController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public unsafe SerializableHotbarSlot GetHotbarSlot(int hotbarId, int slotId) {
IconId = iconId,
SlotType = hotbarItem->CommandType,
CommandId = (int) hotbarItem->CommandId,
IconData = IconManager.GetIconAsPngString(iconId % 1000000, iconId >= 1000000)
IconData = IconManager.GetIconAsPngString(iconId)
};

}
Expand Down
4 changes: 2 additions & 2 deletions FFXIVPlugin/Server/Controllers/IconController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace XIVDeck.FFXIVPlugin.Server.Controllers;
public class IconController : WebApiController {

[Route(HttpVerbs.Get, "/{iconId}")]
public async Task GetIcon(int iconId, [QueryField] bool hq = false) {
public async Task GetIcon(int iconId) {
this.HttpContext.Response.ContentType = "image/png";

var icon = IconManager.GetIcon("", iconId, hq, true);
var icon = IconManager.GetIcon("", iconId, true);

if (icon == null)
throw HttpException.NotFound($"Icon {iconId} was not found.");
Expand Down

0 comments on commit cca5a77

Please sign in to comment.