XivCommon/XivCommon/Functions/PartyFinder.cs

135 lines
5.0 KiB
C#
Raw Normal View History

2021-04-06 08:41:38 +00:00
using System;
using System.Collections.Generic;
2021-04-06 08:41:38 +00:00
using System.Runtime.InteropServices;
using Dalamud.Game;
using Dalamud.Game.Internal.Gui;
using Dalamud.Game.Internal.Gui.Structs;
2021-04-06 08:41:38 +00:00
using Dalamud.Hooking;
using Dalamud.Plugin;
2021-04-06 08:41:38 +00:00
namespace XivCommon.Functions {
/// <summary>
/// A class containing Party Finder functionality
/// </summary>
2021-04-06 08:41:38 +00:00
public class PartyFinder : IDisposable {
private delegate byte RequestPartyFinderListingsDelegate(IntPtr agent, byte categoryIdx);
private delegate IntPtr JoinPfDelegate(IntPtr manager, IntPtr a2, int a3, IntPtr packetData, uint a5);
2021-04-06 08:41:38 +00:00
private RequestPartyFinderListingsDelegate RequestPartyFinderListings { get; }
2021-04-11 13:24:56 +00:00
private Hook<RequestPartyFinderListingsDelegate>? RequestPfListingsHook { get; }
private Hook<JoinPfDelegate>? JoinPfHook { get; }
/// <summary>
/// The delegate for party join events.
/// </summary>
public delegate void JoinPfEventDelegate(PartyFinderListing listing);
2021-04-06 08:41:38 +00:00
/// <summary>
/// <para>
/// The event that is fired when the player joins a party via Party Finder.
/// </para>
/// <para>
/// Requires the <see cref="Hooks.PartyFinder"/> hook to be enabled.
/// </para>
/// </summary>
public event JoinPfEventDelegate? JoinParty;
private PartyFinderGui PartyFinderGui { get; }
2021-04-11 13:24:56 +00:00
private bool Enabled { get; }
2021-04-06 08:41:38 +00:00
private IntPtr PartyFinderAgent { get; set; } = IntPtr.Zero;
private Dictionary<uint, PartyFinderListing> Listings { get; } = new();
private int LastBatch { get; set; } = -1;
internal PartyFinder(SigScanner scanner, PartyFinderGui partyFinderGui, bool hook) {
this.PartyFinderGui = partyFinderGui;
2021-04-06 08:41:38 +00:00
var requestPfPtr = scanner.ScanText("48 89 5C 24 ?? 48 89 74 24 ?? 57 48 83 EC 40 0F 10 81 ?? ?? ?? ??");
this.RequestPartyFinderListings = Marshal.GetDelegateForFunctionPointer<RequestPartyFinderListingsDelegate>(requestPfPtr);
2021-04-11 13:24:56 +00:00
this.Enabled = hook;
if (!hook) {
return;
}
2021-04-06 08:41:38 +00:00
this.RequestPfListingsHook = new Hook<RequestPartyFinderListingsDelegate>(requestPfPtr, new RequestPartyFinderListingsDelegate(this.OnRequestPartyFinderListings));
this.RequestPfListingsHook.Enable();
this.PartyFinderGui.ReceiveListing += this.ReceiveListing;
var joinPtr = scanner.ScanText("E8 ?? ?? ?? ?? 0F B7 47 28");
this.JoinPfHook = new Hook<JoinPfDelegate>(joinPtr, new JoinPfDelegate(this.JoinPfDetour));
this.JoinPfHook.Enable();
2021-04-06 08:41:38 +00:00
}
/// <inheritdoc />
2021-04-06 08:41:38 +00:00
public void Dispose() {
this.PartyFinderGui.ReceiveListing -= this.ReceiveListing;
2021-04-11 13:24:56 +00:00
this.RequestPfListingsHook?.Dispose();
this.JoinPfHook?.Dispose();
}
private void ReceiveListing(PartyFinderListing listing, PartyFinderListingEventArgs args) {
if (args.BatchNumber != this.LastBatch) {
this.Listings.Clear();
}
this.LastBatch = args.BatchNumber;
this.Listings[listing.Id] = listing;
2021-04-06 08:41:38 +00:00
}
private byte OnRequestPartyFinderListings(IntPtr agent, byte categoryIdx) {
this.PartyFinderAgent = agent;
2021-04-11 13:24:56 +00:00
return this.RequestPfListingsHook!.Original(agent, categoryIdx);
2021-04-06 08:41:38 +00:00
}
private IntPtr JoinPfDetour(IntPtr manager, IntPtr a2, int a3, IntPtr packetData, uint a5) {
// Updated: 5.5
const int idOffset = -0x20;
var ret = this.JoinPfHook!.Original(manager, a2, a3, packetData, a5);
try {
2021-04-22 18:53:33 +00:00
if (packetData != IntPtr.Zero) {
var id = (uint) Marshal.ReadInt32(packetData + idOffset);
if (this.Listings.TryGetValue(id, out var listing)) {
this.JoinParty?.Invoke(listing);
}
}
} catch (Exception ex) {
PluginLog.LogError(ex, "Exception in PF join detour");
}
return ret;
}
2021-04-11 13:42:59 +00:00
/// <summary>
/// <para>
/// Refresh the Party Finder listings. This does not open the Party Finder.
/// </para>
/// <para>
/// This maintains the currently selected category.
/// </para>
/// </summary>
/// <exception cref="InvalidOperationException">If the <see cref="Hooks.PartyFinder"/> hook is not enabled</exception>
2021-04-06 08:41:38 +00:00
public void RefreshListings() {
2021-04-11 13:24:56 +00:00
if (!this.Enabled) {
throw new InvalidOperationException("PartyFinder hooks are not enabled");
}
2021-04-13 09:33:38 +00:00
// Updated 5.5
2021-04-06 08:41:38 +00:00
const int categoryOffset = 10_655;
if (this.PartyFinderAgent == IntPtr.Zero) {
return;
}
var categoryIdx = Marshal.ReadByte(this.PartyFinderAgent + categoryOffset);
this.RequestPartyFinderListings(this.PartyFinderAgent, categoryIdx);
}
}
}