ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Tutorials & Other Helpful Info (https://www.esoui.com/forums/forumdisplay.php?f=172)
-   -   PreHooking protected functions (https://www.esoui.com/forums/showthread.php?t=6266)

Ayantir 05/07/16 03:22 AM

PreHooking protected functions
 
Even if it's really simple, it's not in the esoui files, so ..

Lua Code:
  1. --[[
  2. ZO_PreHookProtected is a rewrite of ZO_PreHook. It accepts Protected functions.
  3. IsProtectedFunction(existingFunctionName) will still return true even if existingFunctionName is not called
  4. Calling existingFunctionName() will work, it will call your function and depending on your code will run the prehooked function
  5. Calling CallSecureProtected("existingFunctionName", arg1) will work, it will call the original function only. You won't be able to "fake" other addons if they're correctly written, but mainly ESOUI code.
  6. Don't hook a non-protected function with this PreHook or game won't load
  7. ]]--
  8. function ZO_PreHookProtected(objectTable, existingFunctionName, hookFunction)
  9.     if(type(objectTable) == "string") then
  10.         hookFunction = existingFunctionName
  11.         existingFunctionName = objectTable
  12.         objectTable = _G
  13.     end
  14.  
  15.     local newFn = function(...)
  16.         if(not hookFunction(...)) then
  17.        
  18.             if IsProtectedFunction(existingFunctionName) then
  19.                 return CallSecureProtected(existingFunctionName, ...)
  20.             end
  21.            
  22.         end
  23.     end
  24.  
  25.     objectTable[existingFunctionName] = newFn
  26.  
  27. end

Doing this will mainly prehook ESOUI code and your own addon, so consider that the esoui code may not run because of another addon doing something else instead.

haggen 05/07/16 08:04 PM

Could you provide an example using this hook? Thanks!!

Baertram 05/08/16 04:58 AM

I think you can use it as ZO_PreHook variant but ONLY for functions like UseItem() now.

Quote:

Don't hook a non-protected function with this PreHook or game won't load
If you return true inside your PreHook UseItem won't be executed if I understand it correctly?

Lua Code:
  1. ZO_PreHookProtected('UseItem', MyUseItemFunction)
  2.  
  3. local function MyUseItemFunction(..)
  4.    if doNotUseItem then
  5.       return true
  6.    end
  7. end

Ayantir 05/08/16 05:06 AM

That's it, just like PreHook.

Main goal is to add a backup of the prehooked func inside your hooking function to change its parameters. My first need was to redirect moves for crafting bag.


All times are GMT -6. The time now is 07:08 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI