diff --git a/Pilz.Dalamud/Nameplates/NameplateHooks.cs b/Pilz.Dalamud/Nameplates/NameplateHooks.cs index 867e3bf..4cd4639 100644 --- a/Pilz.Dalamud/Nameplates/NameplateHooks.cs +++ b/Pilz.Dalamud/Nameplates/NameplateHooks.cs @@ -33,6 +33,8 @@ namespace Pilz.Dalamud.Nameplates private Hook? hook_AddonNamePlate_SetPlayerNameplateDetour = null; private unsafe delegate IntPtr AddonNamePlate_SetPlayerNameplateDetour(IntPtr playerNameplateObjectPtr, bool isTitleAboveName, bool isTitleVisible, IntPtr titlePtr, IntPtr namePtr, IntPtr freeCompanyPtr, IntPtr prefix, int iconId); + private bool allowHookHandling = false; + /// /// Defines if all hooks are enabled. If this is false, then there might be something wrong or the class already has been disposed. /// @@ -64,6 +66,7 @@ namespace Pilz.Dalamud.Nameplates public void Dispose() { Unhook(); + GC.SuppressFinalize(this); } /// @@ -71,7 +74,9 @@ namespace Pilz.Dalamud.Nameplates /// internal void Initialize() { - hook_AddonNamePlate_SetPlayerNameplateDetour?.Enable(); + if (!IsHookEnabled(hook_AddonNamePlate_SetPlayerNameplateDetour)) + hook_AddonNamePlate_SetPlayerNameplateDetour?.Enable(); + allowHookHandling = true; } /// @@ -79,7 +84,9 @@ namespace Pilz.Dalamud.Nameplates /// internal void Unhook() { - hook_AddonNamePlate_SetPlayerNameplateDetour?.Disable(); + if (allowHookHandling && IsHookEnabled(hook_AddonNamePlate_SetPlayerNameplateDetour)) + hook_AddonNamePlate_SetPlayerNameplateDetour?.Disable(); + allowHookHandling = false; } private static bool IsHookEnabled(Hook hook) where T : Delegate @@ -91,7 +98,7 @@ namespace Pilz.Dalamud.Nameplates { var result = IntPtr.Zero; - if (IsHookEnabled(hook_AddonNamePlate_SetPlayerNameplateDetour)) + if (allowHookHandling && IsHookEnabled(hook_AddonNamePlate_SetPlayerNameplateDetour)) { var eventArgs = new AddonNamePlate_SetPlayerNameEventArgs { diff --git a/Pilz.Dalamud/Nameplates/NameplateManager.cs b/Pilz.Dalamud/Nameplates/NameplateManager.cs index 8fbd04a..acf4cfb 100644 --- a/Pilz.Dalamud/Nameplates/NameplateManager.cs +++ b/Pilz.Dalamud/Nameplates/NameplateManager.cs @@ -36,6 +36,7 @@ namespace Pilz.Dalamud.Nameplates public void Dispose() { Hooks?.Dispose(); + GC.SuppressFinalize(this); } public static T? GetNameplateGameObject(SafeNameplateObject namePlateObject) where T : GameObject @@ -49,19 +50,17 @@ namespace Pilz.Dalamud.Nameplates var nameplateAddonPtr = PluginServices.GameGui.GetAddonByName("NamePlate", 1); var nameplateObjectArrayPtrPtr = nameplateAddonPtr + Marshal.OffsetOf(typeof(AddonNamePlate), nameof(AddonNamePlate.NamePlateObjectArray)).ToInt32(); var nameplateObjectArrayPtr = Marshal.ReadIntPtr(nameplateObjectArrayPtrPtr); + if (nameplateObjectArrayPtr == IntPtr.Zero) - { return null; - } // Determine the index of the nameplate object within the nameplate object array var namePlateObjectSize = Marshal.SizeOf(typeof(AddonNamePlate.NamePlateObject)); var namePlateObjectPtr0 = nameplateObjectArrayPtr + namePlateObjectSize * 0; var namePlateIndex = (nameplateObjectPtr.ToInt64() - namePlateObjectPtr0.ToInt64()) / namePlateObjectSize; + if (namePlateIndex < 0 || namePlateIndex >= AddonNamePlate.NumNamePlateObjects) - { return null; - } // Get the nameplate info array IntPtr nameplateInfoArrayPtr = IntPtr.Zero;