Dalamud icon indicating copy to clipboard operation
Dalamud copied to clipboard

Something is wrong with Signature attribute when used with Hook?

Open Limiana opened this issue 1 year ago • 1 comments

delegate long OnEmoteFuncDelegate(IntPtr a1, IntPtr source, ushort emoteId, long targetId, long a5);
[Signature("E8 ?? ?? ?? ?? 48 8D 8F ?? ?? ?? ?? 4C 8B CE", DetourName = "OnEmoteFuncDetour")]
Hook<OnEmoteFuncDelegate> OnEmoteFuncHook;

long OnEmoteFuncDetour(IntPtr a1, IntPtr source, ushort emoteId, long targetId, long a5)
{
    try
    {
        Svc.Chat.Print($"{source:X16}");
        var gameObject = Svc.Objects.CreateObjectReference(source);
        var emoteName = Svc.Data.GetExcelSheet<Emote>().GetRow(emoteId).Name;
        var target = Svc.Objects.FirstOrDefault(x => ((GameObject*)x.Address)->GetObjectID() == targetId);
        Svc.Chat.Print($">> {gameObject.Name} used emote {emoteName}" + (target != null?$" on {target.Name}":""));
    }
    catch(Exception e)
    {
        Svc.Chat.Print($"{e.Message}\n{e.StackTrace}");
    }
    return OnEmoteFuncHook.Original(a1, source, emoteId, targetId, a5);
}

///

public TestPlugin(DalamudPluginInterface pluginInterface)
{
    OnEmoteFuncHook.Enable();
}

code like this causes stack overflow:

Repeat 2 times:
--------------------------------
   at System.ModuleHandle.ResolveMethod(System.Runtime.CompilerServices.QCallModule, Int32, IntPtr*, Int32, IntPtr*, Int32)
--------------------------------
   at System.ModuleHandle.ResolveMethodHandleInternalCore(System.Reflection.RuntimeModule, Int32, IntPtr[], Int32, IntPtr[], Int32)
   at System.ModuleHandle.ResolveMethodHandleInternal(System.Reflection.RuntimeModule, Int32, System.RuntimeTypeHandle[], System.RuntimeTypeHandle[])
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(System.Reflection.MetadataToken, System.Reflection.MetadataImport ByRef, System.Reflection.RuntimeModule, System.Reflection.MetadataToken, System.RuntimeType, Boolean, ListBuilder`1<System.Object> ByRef, System.RuntimeType ByRef, System.IRuntimeMethodInfo ByRef, Boolean ByRef)
   at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1<System.Object> ByRef, System.Reflection.RuntimeModule, Int32, System.RuntimeType, Boolean, ListBuilder`1<System.Object>)
   at System.Reflection.CustomAttribute.GetCustomAttributes(System.RuntimeType, System.RuntimeType, Boolean)
   at System.Attribute.GetCustomAttributes(System.Reflection.MemberInfo, System.Type, Boolean)
   at System.Attribute.GetCustomAttribute(System.Reflection.MemberInfo, System.Type, Boolean)
   at System.Reflection.CustomAttributeExtensions.GetCustomAttribute[[System.__Canon, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Reflection.MemberInfo)
   at Lumina.Excel.ExcelModule.GetSheet[[System.__Canon, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](Lumina.Data.Language)
   at TestPlugin.TestPlugin.OnEmoteFuncDetour(IntPtr, IntPtr, UInt16, Int64, Int64)
   at TestPlugin.TestPlugin.OnEmoteFuncDetour(IntPtr, IntPtr, UInt16, Int64, Int64)
...................
   at TestPlugin.TestPlugin.OnEmoteFuncDetour(IntPtr, IntPtr, UInt16, Int64, Int64)
   at TestPlugin.TestPlugin.OnEmoteFuncDetour(IntPtr, IntPtr, UInt16, Int64, Int64)
   at Dalamud.Game.Network.GameNetwork.ProcessZonePacketDownDetour(IntPtr, UInt32, IntPtr)
   at Dalamud.Game.Framework.HandleFrameworkUpdate(IntPtr)

While:

delegate long OnEmoteFuncDelegate(IntPtr a1, IntPtr source, ushort emoteId, long targetId, long a5);
Hook<OnEmoteFuncDelegate> OnEmoteFuncHook;

// ...

public TestPlugin(DalamudPluginInterface pluginInterface)
{
    OnEmoteFuncHook = Hook<OnEmoteFuncDelegate>.FromAddress(Svc.SigScanner.ScanText("E8 ?? ?? ?? ?? 48 8D 8F ?? ?? ?? ?? 4C 8B CE"), OnEmoteFuncDetour);
    OnEmoteFuncHook.Enable();
}

works fine

Limiana avatar Jul 26 '22 12:07 Limiana

Update: seems to be happening only when signature attribute used in main plugin's class https://github.com/Eternita-S/MinimalTestPlugin

Limiana avatar Aug 06 '22 23:08 Limiana