From 4d57e3c150b8d0d3e66933e6253b831758ece70a Mon Sep 17 00:00:00 2001 From: Anna Date: Tue, 4 Aug 2020 21:03:26 -0400 Subject: [PATCH] refactor: use Dalamud API for targeting --- Peeping Tom/Peeping Tom.csproj | 1 + Peeping Tom/PluginUI.cs | 57 +++++++++++++--------------------- Peeping Tom/Util.cs | 21 +++++++++++++ 3 files changed, 43 insertions(+), 36 deletions(-) create mode 100644 Peeping Tom/Util.cs diff --git a/Peeping Tom/Peeping Tom.csproj b/Peeping Tom/Peeping Tom.csproj index e42027a..bf9d148 100644 --- a/Peeping Tom/Peeping Tom.csproj +++ b/Peeping Tom/Peeping Tom.csproj @@ -71,6 +71,7 @@ True Resources.resx + diff --git a/Peeping Tom/PluginUI.cs b/Peeping Tom/PluginUI.cs index 87a9071..e727b92 100644 --- a/Peeping Tom/PluginUI.cs +++ b/Peeping Tom/PluginUI.cs @@ -21,7 +21,7 @@ namespace PeepingTom { private readonly DalamudPluginInterface pi; private readonly List previousTargeters = new List(); - private IntPtr? previousFocus = null; + private Optional previousFocus = new Optional(); private long soundLastPlayed = 0; private int lastTargetAmount = 0; @@ -323,7 +323,7 @@ namespace PeepingTom { // .Skip(1) // .Select(actor => actor as PlayerCharacter) // .Take(2)) { - // this.AddEntry(p); + // this.AddEntry(new Targeter(p), p, ref anyHovered); //} foreach (PlayerCharacter targeter in targeting) { if (this.config.KeepHistory) { @@ -333,7 +333,7 @@ namespace PeepingTom { } this.previousTargeters.Insert(0, new Targeter(targeter)); } - this.AddEntry(new Targeter(targeter), targeter.Address, ref anyHovered); + this.AddEntry(new Targeter(targeter), targeter, ref anyHovered); } if (this.config.KeepHistory) { // only keep the configured number of previous targeters (ignoring ones that are currently targeting) @@ -352,38 +352,38 @@ namespace PeepingTom { } ImGui.ListBoxFooter(); } - if (this.config.FocusTargetOnHover && !anyHovered && this.previousFocus != null) { - // old focus target still here - if (this.pi.ClientState.Actors.Any(actor => actor.Address == this.previousFocus)) { - this.FocusTarget((IntPtr)this.previousFocus); + if (this.config.FocusTargetOnHover && !anyHovered && this.previousFocus.Get(out Actor previousFocus)) { + if (previousFocus == null) { + this.pi.ClientState.Targets.SetFocusTarget(null); } else { - this.FocusTarget(IntPtr.Zero); + Actor actor = this.pi.ClientState.Actors.FirstOrDefault(a => a.ActorId == previousFocus.ActorId); + // either target the actor if still present or target nothing + this.pi.ClientState.Targets.SetFocusTarget(actor); } - this.previousFocus = null; + this.previousFocus = new Optional(); } ImGui.Text("Click to link or right click to target."); ImGui.End(); } } - private void AddEntry(Targeter targeter, IntPtr? address, ref bool anyHovered, ImGuiSelectableFlags flags = ImGuiSelectableFlags.None) { + private void AddEntry(Targeter targeter, Actor actor, ref bool anyHovered, ImGuiSelectableFlags flags = ImGuiSelectableFlags.None) { ImGui.Selectable(targeter.Name, false, flags); bool hover = ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled); anyHovered |= hover; bool left = hover && ImGui.IsMouseClicked(0); bool right = hover && ImGui.IsMouseClicked(1); - if (address == null) { - address = this.pi.ClientState.Actors - .Where(actor => actor.ActorId == targeter.ActorId) - .FirstOrDefault() - ?.Address; + if (actor == null) { + actor = this.pi.ClientState.Actors + .Where(a => a.ActorId == targeter.ActorId) + .FirstOrDefault(); } - if (this.config.FocusTargetOnHover && hover && address != null) { - if (this.previousFocus == null) { - this.previousFocus = this.GetRawFocusTarget(); + if (this.config.FocusTargetOnHover && hover && actor != null) { + if (!this.previousFocus.Present) { + this.previousFocus = new Optional(this.pi.ClientState.Targets.FocusTarget); } - this.FocusTarget(address.Value); + this.pi.ClientState.Targets.SetFocusTarget(actor); } if (left) { @@ -392,8 +392,8 @@ namespace PeepingTom { this.pi.Framework.Gui.Chat.PrintChat(new XivChatEntry { MessageBytes = new SeString(payloads).Encode() }); - } else if (right && address != null) { - this.Target(address.Value); + } else if (right && actor != null) { + this.pi.ClientState.Targets.SetCurrentTarget(actor); } } @@ -444,21 +444,6 @@ namespace PeepingTom { .FirstOrDefault(); } - private void Target(IntPtr actor) { - IntPtr targetPtr = this.pi.TargetModuleScanner.ResolveRelativeAddress(this.pi.TargetModuleScanner.Module.BaseAddress, 0x1c64150); - Marshal.WriteIntPtr(targetPtr, actor); - } - - private void FocusTarget(IntPtr ptr) { - IntPtr targetPtr = this.pi.TargetModuleScanner.ResolveRelativeAddress(this.pi.TargetModuleScanner.Module.BaseAddress, 0x1c641c8); - Marshal.WriteIntPtr(targetPtr, ptr); - } - - private IntPtr GetRawFocusTarget() { - IntPtr targetPtr = this.pi.TargetModuleScanner.ResolveRelativeAddress(this.pi.TargetModuleScanner.Module.BaseAddress, 0x1c641c8); - return Marshal.ReadIntPtr(targetPtr); - } - private bool CanPlaySound() { if (this.soundLastPlayed == 0) { return true; diff --git a/Peeping Tom/Util.cs b/Peeping Tom/Util.cs new file mode 100644 index 0000000..007ccfb --- /dev/null +++ b/Peeping Tom/Util.cs @@ -0,0 +1,21 @@ +namespace PeepingTom { + [System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1716:Identifiers should not match keywords")] + public class Optional { + public bool Present { get; private set; } + private readonly T value; + + public Optional(T value) { + this.value = value; + this.Present = true; + } + + public Optional() { + this.Present = false; + } + + public bool Get(out T value) { + value = this.value; + return this.Present; + } + } +}