Skip to content

Commit

Permalink
Prevent duplicate toasts, fix drawing logic crash
Browse files Browse the repository at this point in the history
Changed Draw method in OverlayWindow.cs to synchronous and added null check for drawList.NativePtr to prevent crashes.

Updated RSCommands_StateSpecialCommand.cs to prevent duplicate toast messages by storing the last message and checking against it before displaying a new one. Simplified and optimized DrawingHighlightHotbar.cs by removing unnecessary Union calls and refactoring GetAddons method. Cleaned up IDrawing2D interface and added null checks in ImageDrawing struct's Draw method. Refactored GetDrawingOrder method for better readability.
  • Loading branch information
LTS-FFXIV committed Jan 11, 2025
1 parent b277aad commit de10765
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
11 changes: 9 additions & 2 deletions RotationSolver/Commands/RSCommands_StateSpecialCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ public static partial class RSCommands

internal static string EntryString => $"{_stateString}{(DataCenter.SpecialTimeLeft < 0 ? string.Empty : $" - {_specialString}: {DataCenter.SpecialTimeLeft:F2}s")}";

private static string _lastToastMessage = string.Empty;

private static void UpdateToast()
{
if (!Service.Config.ShowInfoOnToast) return;

Svc.Toasts.ShowQuest($" {EntryString}", new Dalamud.Game.Gui.Toast.QuestToastOptions
string currentMessage = $" {EntryString}";
if (currentMessage == _lastToastMessage) return;

Svc.Toasts.ShowQuest(currentMessage, new Dalamud.Game.Gui.Toast.QuestToastOptions
{
IconId = 101,
});

_lastToastMessage = currentMessage;
}

public static unsafe void DoStateCommandType(StateCommandType stateType, int index = -1) => DoOneCommandType((type, role) => type.ToStateString(role), role =>
Expand Down Expand Up @@ -110,4 +117,4 @@ private static void DoOneCommandType<T>(Func<T, JobRole, string> sayout, Func<Jo
if (Service.Config.SayOutStateChanged) SpeechHelper.Speak(sayout(type, role));
}
}
}
}
38 changes: 21 additions & 17 deletions RotationSolver/UI/HighlightTeachingMode/DrawingHighlightHotbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ private protected override unsafe IEnumerable<IDrawing2D> To2D()
var result = new List<IDrawing2D>();

var hotBarIndex = 0;
foreach (var intPtr in GetAddons<AddonActionBar>()
.Union(GetAddons<AddonActionBarX>())
.Union(GetAddons<AddonActionCross>())
.Union(GetAddons<AddonActionDoubleCrossBase>()))
foreach (var intPtr in GetAddons<AddonActionBar>())
{
var actionBar = (AddonActionBarBase*)intPtr;
if (actionBar != null && IsVisible(actionBar->AtkUnitBase))
Expand Down Expand Up @@ -142,9 +139,16 @@ private static unsafe IEnumerable<nint> GetAddons<T>() where T : struct
{
if (typeof(T).GetCustomAttribute<AddonAttribute>() is not AddonAttribute on) return Array.Empty<nint>();

return on.AddonIdentifiers
.Select(str => Svc.GameGui.GetAddonByName(str, 1))
.Where(ptr => ptr != nint.Zero);
var result = new List<nint>();
foreach (var str in on.AddonIdentifiers)
{
var ptr = Svc.GameGui.GetAddonByName(str, 1);
if (ptr != nint.Zero)
{
result.Add(ptr);
}
}
return result;
}

private static unsafe bool IsVisible(AtkUnitBase unit)
Expand Down Expand Up @@ -239,12 +243,6 @@ public interface IDrawing2D
void Draw();
}

/// <summary> Drawing the image. </summary>
/// <remarks> </remarks>
/// <param name="texture"> </param>
/// <param name="pt1"> </param>
/// <param name="pt2"> </param>
/// <param name="col"> </param>
public readonly struct ImageDrawing(IDalamudTextureWrap texture, Vector2 pt1, Vector2 pt2, uint col = uint.MaxValue) : IDrawing2D
{
/// <summary> </summary>
Expand All @@ -262,13 +260,19 @@ public ImageDrawing(IDalamudTextureWrap texture, Vector2 pt1, Vector2 pt2,
_uv2 = uv2;
}

/// <summary> Draw on the <seealso cref="ImGui" /> </summary>
public void Draw()
public unsafe void Draw()
{
ImGui.GetWindowDrawList().AddImage(_texture.ImGuiHandle, _pt1, _pt2, _uv1, _uv2, _col);
if (_texture == null)
return;

var drawList = ImGui.GetWindowDrawList();
if (drawList.NativePtr == null)
return;

drawList.AddImage(_texture.ImGuiHandle, _pt1, _pt2, _uv1, _uv2, _col);
}

private readonly uint _col = col;
private readonly Vector2 _pt1 = pt1, _pt2 = pt2, _uv1 = default, _uv2 = Vector2.One;
private readonly IDalamudTextureWrap _texture = texture;
}
}
26 changes: 16 additions & 10 deletions RotationSolver/UI/OverlayWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using Dalamud.Interface.Windowing;
using ECommons.DalamudServices;
using RotationSolver.UI.HighlightTeachingMode;
using System.Linq;
using System.Threading.Tasks;

namespace RotationSolver.UI;

Expand Down Expand Up @@ -37,7 +35,7 @@ public override void PreDraw()
base.PreDraw();
}

public override async void Draw()
public unsafe override void Draw()
{
if (!HotbarHighlightManager.Enable || Svc.ClientState == null || Svc.ClientState.LocalPlayer == null)
return;
Expand All @@ -46,10 +44,17 @@ public override async void Draw()

try
{
await UpdateDrawingElementsAsync();
UpdateDrawingElementsAsync().GetAwaiter().GetResult();

if (HotbarHighlightManager._drawingElements2D != null)
{
var drawList = ImGui.GetWindowDrawList();
if (drawList.NativePtr == null)
{
Svc.Log.Warning($"{nameof(OverlayWindow)}: Window draw list is null.");
return;
}

foreach (var item in HotbarHighlightManager._drawingElements2D.OrderBy(GetDrawingOrder))
{
item.Draw();
Expand All @@ -72,13 +77,14 @@ private async Task UpdateDrawingElementsAsync()

private int GetDrawingOrder(object drawing)
{
if (drawing is PolylineDrawing poly)
{
return poly._thickness == 0 ? 0 : 1;
}
else
switch (drawing)
{
return 2;
case PolylineDrawing poly:
return poly._thickness == 0 ? 0 : 1;
case ImageDrawing:
return 1;
default:
return 2;
}
}

Expand Down

0 comments on commit de10765

Please sign in to comment.