fix: don't crash in hook

This commit is contained in:
Anna 2020-12-10 22:35:31 -05:00
parent 28051de60d
commit 6273fec0e0
2 changed files with 26 additions and 8 deletions

View File

@ -25,6 +25,7 @@ namespace GoodMemory {
Cards = 3_357, // cards Cards = 3_357, // cards
GathererBooks = 4_107, // gatherer books GathererBooks = 4_107, // gatherer books
OrchestrionRolls = 5_845, // orchestrion rolls OrchestrionRolls = 5_845, // orchestrion rolls
FieldNotes = 19_743, // bozjan field notes
FashionAccessories = 20_086, // fashion accessories FashionAccessories = 20_086, // fashion accessories
} }
} }

View File

@ -2,6 +2,7 @@
using Dalamud.Plugin; using Dalamud.Plugin;
using Lumina.Excel.GeneratedSheets; using Lumina.Excel.GeneratedSheets;
using System; using System;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
@ -47,33 +48,37 @@ namespace GoodMemory {
if (tooltipPtr == IntPtr.Zero) { if (tooltipPtr == IntPtr.Zero) {
throw new ApplicationException("Could not set up tooltip hook because of null pointer"); throw new ApplicationException("Could not set up tooltip hook because of null pointer");
} }
unsafe { unsafe {
this.tooltipHook = new Hook<TooltipDelegate>(tooltipPtr, new TooltipDelegate(this.OnTooltip)); this.tooltipHook = new Hook<TooltipDelegate>(tooltipPtr, new TooltipDelegate(this.OnTooltip));
} }
this.tooltipHook.Enable(); this.tooltipHook.Enable();
} }
private unsafe IntPtr OnTooltip(IntPtr a1, uint** a2, byte*** a3) { private unsafe void TooltipLogic(IntPtr a1, uint** a2, byte*** a3) {
// this can be replaced with a mid-func hook when reloaded hooks is in dalamud // this can be replaced with a mid-func hook when reloaded hooks is in dalamud
// but for now, do the same logic the func does and replace the text after // but for now, do the same logic the func does and replace the text after
uint* v3 = *(a2 + 4); uint* v3 = *(a2 + 4);
uint v9 = *(v3 + 4); uint v9 = *(v3 + 4);
if ((v9 & 2) == 0) { if ((v9 & 2) == 0) {
goto Return; return;
} }
ulong itemId = this.Interface.Framework.Gui.HoveredItem; ulong itemId = this.Interface.Framework.Gui.HoveredItem;
if (itemId > 2_000_000) { if (itemId > 2_000_000) {
goto Return; return;
} else if (itemId > 1_000_000) { }
if (itemId > 1_000_000) {
itemId -= 1_000_000; itemId -= 1_000_000;
} }
Item item = this.Interface.Data.GetExcelSheet<Item>().GetRow((uint)itemId); Item item = this.Interface.Data.GetExcelSheet<Item>().GetRow((uint)itemId);
if (item == null) { if (item == null) {
goto Return; return;
} }
// get the pointer to the text // get the pointer to the text
@ -83,7 +88,7 @@ namespace GoodMemory {
// work around function being called twice // work around function being called twice
if (start == (byte*)this.alloc) { if (start == (byte*)this.alloc) {
goto Return; return;
} }
string overwrite; string overwrite;
@ -104,6 +109,8 @@ namespace GoodMemory {
continue; continue;
} }
Debug.Assert(resultAction != null, nameof(resultAction) + " != null");
uint orchId = resultAction.Data[0]; uint orchId = resultAction.Data[0];
Orchestrion orch = this.Interface.Data.GetExcelSheet<Orchestrion>().GetRow(orchId); Orchestrion orch = this.Interface.Data.GetExcelSheet<Orchestrion>().GetRow(orchId);
if (orch == null) { if (orch == null) {
@ -116,7 +123,7 @@ namespace GoodMemory {
ItemAction action = item.ItemAction?.Value; ItemAction action = item.ItemAction?.Value;
if (!ActionTypeExt.IsValidAction(action)) { if (!ActionTypeExt.IsValidAction(action)) {
goto Return; return;
} }
// get the text // get the text
@ -131,8 +138,15 @@ namespace GoodMemory {
// overwrite the original pointer with our own // overwrite the original pointer with our own
*startPtr = (byte*)this.alloc; *startPtr = (byte*)this.alloc;
}
private unsafe IntPtr OnTooltip(IntPtr a1, uint** a2, byte*** a3) {
try {
this.TooltipLogic(a1, a2, a3);
} catch (Exception ex) {
PluginLog.Error($"Could not modify tooltip:\n{ex.Message}\n{ex.StackTrace}");
}
Return:
return this.tooltipHook.Original(a1, a2, a3); return this.tooltipHook.Original(a1, a2, a3);
} }
@ -194,8 +208,10 @@ namespace GoodMemory {
if (b == 0) { if (b == 0) {
break; break;
} }
offset += 1; offset += 1;
} }
return Encoding.UTF8.GetString(ptr, offset); return Encoding.UTF8.GetString(ptr, offset);
} }
@ -204,6 +220,7 @@ namespace GoodMemory {
for (int i = 0; i < bytes.Length; i++) { for (int i = 0; i < bytes.Length; i++) {
*(dst + i) = bytes[i]; *(dst + i) = bytes[i];
} }
if (finalise) { if (finalise) {
*(dst + bytes.Length) = 0; *(dst + bytes.Length) = 0;
} }