Talk:Sea.util.unhook
Back to page
There is a bug in the unhook function which prevents unhooking of previously hooked functions. Basically the code in unhook which was copied from hook will exit if it finds that the passed new function is already hooked. This is good for the hook function, but the opposite of what needs to happen for the unhook function. I modified the code to fix this so that it will only try to unhook if the function was already hooked (as opposed to the opposite). Here's the complete fixed unhook function.
Sea.util.unhook = function ( orig, new, hooktype, scriptElementName )
if(not hooktype) then
hooktype = "before";
end
local compoundOrig = orig;
if (scriptElementName) then
compoundOrig = orig.."."..scriptElementName;
end
Sea.io.dprintfc((SEA_HOOKS_DEBUG and (SEA_HOOKS_DEBUG_VERBOSE==compoundOrig)), nil, NORMAL_FONT_COLOR, "SeaHooks Progress: Unhooking ", orig, " to ", new, ", hooktype ", hooktype, ", scriptElementName ", scriptElementName);
local newFunc = new;
if ( type(new) ~= "function" ) then
newFunc = Sea.util.getValue(new);
end
local hookObj = Sea.util.Hooks[compoundOrig];
if(not hookObj) then
hookObj = SeaHooks_hookInit(orig, compoundOrig, scriptElementName);
Sea.io.dprintfc((SEA_HOOKS_DEBUG and (SEA_HOOKS_DEBUG_VERBOSE==compoundOrig)), nil, NORMAL_FONT_COLOR, "SeaHooks Progress: '", compoundOrig, "' not hooked with '", new, "' skipping.");
return;
else
local foundIt = false;
for key,value in hookObj[hooktype] do
-- NOTE THIS SHOULD BE VALUE! VALUE! *NOT* KEY! (checking if the functions are the same, even if the names are different)
-- If the function is found it will be unhooked
if(value == newFunc) then
foundIt = true;
break;
--exit loop since found hook
end
end
if (foundIt == false) then
Sea.io.dprintfc((SEA_HOOKS_DEBUG and (SEA_HOOKS_DEBUG_VERBOSE==compoundOrig)), nil, NORMAL_FONT_COLOR, "SeaHooks Progress: '", compoundOrig, "' not hooked with '", new, "' skipping.");
--hooked function not found so nothing to do
return;
end
end
local info = hookObj[hooktype]; --Sea.util.Hooks[compoundOrig][hooktype]
for key,value in info do
if (type(value) == "function") and (value == newFunc) then
info[key] = nil;
local embeddedTable = info[key.."Info"];
if (type(embeddedTable) == "table") then
embeddedTable.parent = nil;
embeddedTable.name = nil;
end
info[key.."Info"] = embeddedTable;
hookObj[hooktype] = info;
hookObj.count = hookObj.count - 1; --decrement hook counter
Sea.util.Hooks[compoundOrig] = hookObj --repackage all hook types
Sea.io.dprintfc((SEA_HOOKS_DEBUG and (SEA_HOOKS_DEBUG_VERBOSE==compoundOrig)), nil, NORMAL_FONT_COLOR, "SeaHooks Progress: Found and unhooked '", new, "' from '", compoundOrig, "'.");
return;
end
end
-- No Complete Unhooking - Incompatible with Frame Script Element Hooks - Also liable to erase function hooks loaded after the first hook.
Sea.util.Hooks[compoundOrig] = hookObj --repackage all hook types
end