refactor: use new Dalamud PF event
This commit is contained in:
parent
02d5767c43
commit
8b61617997
|
@ -3,7 +3,8 @@ using Dalamud.Game.Chat.SeStringHandling;
|
|||
using Dalamud.Hooking;
|
||||
using Dalamud.Plugin;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using Dalamud.Game.Internal.Gui;
|
||||
using Dalamud.Game.Internal.Gui.Structs;
|
||||
using NoSoliciting.Ml;
|
||||
|
||||
namespace NoSoliciting {
|
||||
|
@ -42,10 +43,6 @@ namespace NoSoliciting {
|
|||
private Plugin Plugin { get; }
|
||||
private bool _clearOnNext;
|
||||
|
||||
private delegate void HandlePfPacketDelegate(IntPtr param1, IntPtr param2);
|
||||
|
||||
private readonly Hook<HandlePfPacketDelegate>? _handlePacketHook;
|
||||
|
||||
private delegate IntPtr HandlePfSummaryDelegate(IntPtr param1, IntPtr param2, byte param3);
|
||||
|
||||
private readonly Hook<HandlePfSummaryDelegate>? _handleSummaryHook;
|
||||
|
@ -56,38 +53,80 @@ namespace NoSoliciting {
|
|||
this.Plugin = plugin ?? throw new ArgumentNullException(nameof(plugin), "Plugin cannot be null");
|
||||
|
||||
this.Plugin.Interface.Framework.Gui.Chat.OnCheckMessageHandled += this.OnChat;
|
||||
this.Plugin.Interface.Framework.Gui.PartyFinder.ReceiveListing += this.OnListing;
|
||||
|
||||
var listingPtr = this.Plugin.Interface.TargetModuleScanner.ScanText("40 53 41 57 48 83 EC 28 48 8B D9");
|
||||
var summaryPtr = this.Plugin.Interface.TargetModuleScanner.ScanText("48 89 5C 24 ?? 48 89 74 24 ?? 57 48 83 EC 20 48 8B FA 48 8B F1 45 84 C0 74 ?? 0F B7 0A");
|
||||
|
||||
this._handlePacketHook = new Hook<HandlePfPacketDelegate>(listingPtr, new HandlePfPacketDelegate(this.TransformPfPacket));
|
||||
this._handlePacketHook.Enable();
|
||||
|
||||
this._handleSummaryHook = new Hook<HandlePfSummaryDelegate>(summaryPtr, new HandlePfSummaryDelegate(this.HandleSummary));
|
||||
this._handleSummaryHook.Enable();
|
||||
}
|
||||
|
||||
public void OnChat(XivChatType type, uint senderId, ref SeString sender, ref SeString message, ref bool isHandled) {
|
||||
private void Dispose(bool disposing) {
|
||||
if (this._disposedValue) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (disposing) {
|
||||
this.Plugin.Interface.Framework.Gui.Chat.OnCheckMessageHandled -= this.OnChat;
|
||||
this.Plugin.Interface.Framework.Gui.PartyFinder.ReceiveListing -= this.OnListing;
|
||||
this._handleSummaryHook?.Dispose();
|
||||
}
|
||||
|
||||
this._disposedValue = true;
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
||||
this.Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
private void OnChat(XivChatType type, uint senderId, ref SeString sender, ref SeString message, ref bool isHandled) {
|
||||
isHandled = isHandled || this.FilterMessage(type, sender, message);
|
||||
}
|
||||
|
||||
private void TransformPfPacket(IntPtr param1, IntPtr data) {
|
||||
if (data == IntPtr.Zero) {
|
||||
goto Return;
|
||||
}
|
||||
|
||||
private void OnListing(PartyFinderListing listing, PartyFinderListingEventArgs args) {
|
||||
try {
|
||||
if (this._clearOnNext) {
|
||||
this.Plugin.ClearPartyFinderHistory();
|
||||
this._clearOnNext = false;
|
||||
}
|
||||
|
||||
string? reason = null;
|
||||
uint? version = null;
|
||||
if (this.Plugin.MlReady) {
|
||||
this.MlTransformPfPacket(data);
|
||||
version = this.Plugin.MlFilter!.Version;
|
||||
reason = this.MlListingFilterReason(listing);
|
||||
} else if (this.Plugin.DefsReady) {
|
||||
this.DefsTransformPfPacket(data);
|
||||
version = this.Plugin.Definitions!.Version;
|
||||
reason = this.DefsListingFilterReason(listing);
|
||||
}
|
||||
|
||||
if (version == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.Plugin.AddPartyFinderHistory(new Message(
|
||||
version.Value,
|
||||
ChatType.None,
|
||||
listing.Name,
|
||||
listing.Description,
|
||||
this.Plugin.MlReady,
|
||||
reason
|
||||
));
|
||||
|
||||
if (reason == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
args.Visible = false;
|
||||
|
||||
if (this.Plugin.Config.LogFilteredPfs) {
|
||||
PluginLog.Log($"Filtered PF listing from {listing.Name.TextValue} ({reason}): {listing.Description.TextValue}");
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
PluginLog.LogError($"Error in PF hook: {ex}");
|
||||
PluginLog.LogError($"Error in PF listing event: {ex}");
|
||||
}
|
||||
|
||||
Return:
|
||||
this._handlePacketHook!.Original(param1, data);
|
||||
}
|
||||
|
||||
private bool FilterMessage(XivChatType type, SeString sender, SeString message) {
|
||||
|
@ -121,8 +160,8 @@ namespace NoSoliciting {
|
|||
|
||||
// step 1. check for custom filters if enabled
|
||||
var filter = this.Plugin.Config.CustomChatFilter
|
||||
&& Chat.MatchesCustomFilters(text, this.Plugin.Config)
|
||||
&& SetReason(out reason, "custom");
|
||||
&& Chat.MatchesCustomFilters(text, this.Plugin.Config)
|
||||
&& SetReason(out reason, "custom");
|
||||
|
||||
// only look at ml if message >= min words
|
||||
if (!filter && text.Trim().Split(' ').Length >= MinWords) {
|
||||
|
@ -131,8 +170,8 @@ namespace NoSoliciting {
|
|||
|
||||
// step 2a. only filter if configured to act on this channel
|
||||
filter = category != MessageCategory.Normal
|
||||
&& this.Plugin.Config.MlEnabledOn(category, chatType)
|
||||
&& SetReason(out reason, category.Name());
|
||||
&& this.Plugin.Config.MlEnabledOn(category, chatType)
|
||||
&& SetReason(out reason, category.Name());
|
||||
}
|
||||
|
||||
this.Plugin.AddMessageHistory(new Message(
|
||||
|
@ -189,181 +228,65 @@ namespace NoSoliciting {
|
|||
return filter;
|
||||
}
|
||||
|
||||
private void MlTransformPfPacket(IntPtr data) {
|
||||
private string? MlListingFilterReason(PartyFinderListing listing) {
|
||||
if (this.Plugin.MlFilter == null) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (this._clearOnNext) {
|
||||
this.Plugin.ClearPartyFinderHistory();
|
||||
this._clearOnNext = false;
|
||||
// ignore private listings if configured
|
||||
if (!this.Plugin.Config.ConsiderPrivatePfs && listing[SearchAreaFlags.Private]) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var dataPtr = data + 0x10;
|
||||
var desc = listing.Description.TextValue;
|
||||
|
||||
// parse the packet into a struct
|
||||
var packet = Marshal.PtrToStructure<PfPacket>(dataPtr);
|
||||
|
||||
for (var i = 0; i < packet.listings.Length; i++) {
|
||||
var listing = packet.listings[i];
|
||||
|
||||
// only look at listings that aren't null
|
||||
if (listing.IsNull()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// ignore private listings if configured
|
||||
if (!this.Plugin.Config.ConsiderPrivatePfs && (listing.searchArea & (1 << 1)) != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var rawName = listing.Name(this.Plugin.Interface.SeStringManager);
|
||||
var rawDesc = listing.Description(this.Plugin.Interface.SeStringManager);
|
||||
|
||||
var name = rawName.TextValue;
|
||||
var desc = rawDesc.TextValue;
|
||||
|
||||
string? reason = null;
|
||||
|
||||
// step 1. check if pf has an item level that's too high
|
||||
var filter = this.Plugin.Config.FilterHugeItemLevelPFs
|
||||
&& listing.minimumItemLevel > FilterUtil.MaxItemLevelAttainable(this.Plugin.Interface.Data)
|
||||
&& SetReason(out reason, "ilvl");
|
||||
|
||||
// step 2. check custom filters
|
||||
filter = filter || this.Plugin.Config.CustomPFFilter
|
||||
&& PartyFinder.MatchesCustomFilters(desc, this.Plugin.Config)
|
||||
&& SetReason(out reason, "custom");
|
||||
|
||||
// only look at ml for pfs >= min words
|
||||
if (!filter && desc.Trim().Split(' ').Length >= MinWords) {
|
||||
// step 3. check the model's prediction
|
||||
var category = this.Plugin.MlFilter.ClassifyMessage((ushort) ChatType.None, desc);
|
||||
|
||||
// step 3a. filter the message if configured to do so
|
||||
filter = category != MessageCategory.Normal
|
||||
&& this.Plugin.Config.MlEnabledOn(category, ChatType.None)
|
||||
&& SetReason(out reason, category.Name());
|
||||
}
|
||||
|
||||
this.Plugin.AddPartyFinderHistory(new Message(
|
||||
this.Plugin.MlFilter.Version,
|
||||
ChatType.None,
|
||||
rawName,
|
||||
rawDesc,
|
||||
true,
|
||||
reason
|
||||
));
|
||||
|
||||
if (!filter) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// replace the listing with an empty one
|
||||
packet.listings[i] = new PfListing();
|
||||
|
||||
if (this.Plugin.Config.LogFilteredPfs) {
|
||||
PluginLog.Log($"Filtered PF listing from {name} ({reason}): {desc}");
|
||||
}
|
||||
// step 1. check if pf has an item level that's too high
|
||||
if (this.Plugin.Config.FilterHugeItemLevelPFs && listing.MinimumItemLevel > FilterUtil.MaxItemLevelAttainable(this.Plugin.Interface.Data)) {
|
||||
return "ilvl";
|
||||
}
|
||||
|
||||
// get some memory for writing to
|
||||
var newPacket = new byte[PacketInfo.PacketSize];
|
||||
var pinnedArray = GCHandle.Alloc(newPacket, GCHandleType.Pinned);
|
||||
var pointer = pinnedArray.AddrOfPinnedObject();
|
||||
// step 2. check custom filters
|
||||
if (this.Plugin.Config.CustomPFFilter && PartyFinder.MatchesCustomFilters(desc, this.Plugin.Config)) {
|
||||
return "custom";
|
||||
}
|
||||
|
||||
// write our struct into the memory (doing this directly crashes the game)
|
||||
Marshal.StructureToPtr(packet, pointer, false);
|
||||
// only look at ml for pfs >= min words
|
||||
if (desc.Trim().Split(' ').Length < MinWords) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// copy our new memory over the game's
|
||||
Marshal.Copy(newPacket, 0, dataPtr, PacketInfo.PacketSize);
|
||||
var category = this.Plugin.MlFilter.ClassifyMessage((ushort) ChatType.None, desc);
|
||||
|
||||
// free memory
|
||||
pinnedArray.Free();
|
||||
if (category != MessageCategory.Normal && this.Plugin.Config.MlEnabledOn(category, ChatType.None)) {
|
||||
return category.Name();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void DefsTransformPfPacket(IntPtr data) {
|
||||
private string? DefsListingFilterReason(PartyFinderListing listing) {
|
||||
if (this.Plugin.Definitions == null) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (this._clearOnNext) {
|
||||
this.Plugin.ClearPartyFinderHistory();
|
||||
this._clearOnNext = false;
|
||||
var desc = listing.Description.TextValue;
|
||||
|
||||
if (this.Plugin.Config.FilterHugeItemLevelPFs && listing.MinimumItemLevel > FilterUtil.MaxItemLevelAttainable(this.Plugin.Interface.Data)) {
|
||||
return "ilvl";
|
||||
}
|
||||
|
||||
var dataPtr = data + 0x10;
|
||||
|
||||
// parse the packet into a struct
|
||||
var packet = Marshal.PtrToStructure<PfPacket>(dataPtr);
|
||||
|
||||
for (var i = 0; i < packet.listings.Length; i++) {
|
||||
var listing = packet.listings[i];
|
||||
|
||||
// only look at listings that aren't null
|
||||
if (listing.IsNull()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var rawName = listing.Name(this.Plugin.Interface.SeStringManager);
|
||||
var rawDesc = listing.Description(this.Plugin.Interface.SeStringManager);
|
||||
|
||||
var name = rawName.TextValue;
|
||||
var desc = rawDesc.TextValue;
|
||||
|
||||
string? reason = null;
|
||||
var filter = false;
|
||||
|
||||
filter = filter || this.Plugin.Config.FilterHugeItemLevelPFs
|
||||
&& listing.minimumItemLevel > FilterUtil.MaxItemLevelAttainable(this.Plugin.Interface.Data)
|
||||
&& SetReason(out reason, "ilvl");
|
||||
|
||||
foreach (var def in this.Plugin.Definitions.PartyFinder.Values) {
|
||||
filter = filter || this.Plugin.Config.FilterStatus.TryGetValue(def.Id, out var enabled)
|
||||
&& enabled
|
||||
&& def.Matches(XivChatType.None, desc)
|
||||
&& SetReason(out reason, def.Id);
|
||||
}
|
||||
|
||||
// check for custom filters if enabled
|
||||
filter = filter || this.Plugin.Config.CustomPFFilter
|
||||
&& PartyFinder.MatchesCustomFilters(desc, this.Plugin.Config)
|
||||
&& SetReason(out reason, "custom");
|
||||
|
||||
this.Plugin.AddPartyFinderHistory(new Message(
|
||||
this.Plugin.Definitions.Version,
|
||||
ChatType.None,
|
||||
rawName,
|
||||
rawDesc,
|
||||
false,
|
||||
reason
|
||||
));
|
||||
|
||||
if (!filter) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// replace the listing with an empty one
|
||||
packet.listings[i] = new PfListing();
|
||||
|
||||
if (this.Plugin.Config.LogFilteredPfs) {
|
||||
PluginLog.Log($"Filtered PF listing from {name} ({reason}): {desc}");
|
||||
foreach (var def in this.Plugin.Definitions.PartyFinder.Values) {
|
||||
if (this.Plugin.Config.FilterStatus.TryGetValue(def.Id, out var enabled) && enabled && def.Matches(XivChatType.None, desc)) {
|
||||
return def.Id;
|
||||
}
|
||||
}
|
||||
|
||||
// get some memory for writing to
|
||||
var newPacket = new byte[PacketInfo.PacketSize];
|
||||
var pinnedArray = GCHandle.Alloc(newPacket, GCHandleType.Pinned);
|
||||
var pointer = pinnedArray.AddrOfPinnedObject();
|
||||
// check for custom filters if enabled
|
||||
if (this.Plugin.Config.CustomPFFilter && PartyFinder.MatchesCustomFilters(desc, this.Plugin.Config)) {
|
||||
return "custom";
|
||||
}
|
||||
|
||||
// write our struct into the memory (doing this directly crashes the game)
|
||||
Marshal.StructureToPtr(packet, pointer, false);
|
||||
|
||||
// copy our new memory over the game's
|
||||
Marshal.Copy(newPacket, 0, dataPtr, PacketInfo.PacketSize);
|
||||
|
||||
// free memory
|
||||
pinnedArray.Free();
|
||||
return null;
|
||||
}
|
||||
|
||||
private IntPtr HandleSummary(IntPtr param1, IntPtr param2, byte param3) {
|
||||
|
@ -376,25 +299,5 @@ namespace NoSoliciting {
|
|||
reason = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing) {
|
||||
if (this._disposedValue) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (disposing) {
|
||||
this.Plugin.Interface.Framework.Gui.Chat.OnCheckMessageHandled -= this.OnChat;
|
||||
this._handlePacketHook?.Dispose();
|
||||
this._handleSummaryHook?.Dispose();
|
||||
}
|
||||
|
||||
this._disposedValue = true;
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
||||
this.Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
<PlatformTarget>x64</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Dalamud, Version=5.2.3.3, Culture=neutral, PublicKeyToken=null">
|
||||
<Reference Include="Dalamud, Version=5.2.3.5, Culture=neutral, PublicKeyToken=null">
|
||||
<HintPath>$(AppData)\XIVLauncher\addon\Hooks\dev\Dalamud.dll</HintPath>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
|
|
Loading…
Reference in New Issue