Thread Tools Display Modes
11/23/21, 11:09 AM   #1
@DeadSoon
AddOn Author - Click to view addons
Join Date: Sep 2018
Posts: 22
Secure Protected Function "UseItem"

Hello,

I know there are a lot of posts about the topic of protected functions. However, I have a question on a concrete example/issue what I do not understand:

I want to intersect the use of an item in special cases (if the player clicks on an item in the inventory). Thefore, I have a prehook on the protected function "UseItem", following the approach of "ZO_PreHookProtected" from here. My pre-hooking function returns true in the case that I want to intersect/prevent the "UseItem" call, in all other cases it returns false (pass execution).

Because UseItem is a proteced function, this works fine for all cases out of combat.


Now the issue:
Without prehook in place, it is possible to use items even in combat (open inventory and click on an item!). Although, I know that "UseItem" is called and it shouldnt because it is protected. However, it seems that ZOS has built its own exception.

With the prehook in place, it is no longer possible to use items from the inventory while in combat. I can observe that my prehook is called and even if my function returns false to let the function pass without modifying anything, the item is not be used. Simply, nothing happens.

Now my question:
  1. Can anyone explain this behavior to me?
  2. Even if ZOS has its own exception in place, why does my pre-hooking function breaks the "UseItem" call in combat?
  3. Do you have an idea, how I can keep my prehook for UseItem (in practice: whenever the player tries to use an item from the inventory) but at the same time fix the issue, that the player cannot use items in combat any longer?

Thank you so much in advance!
Best regards
  Reply With Quote
11/23/21, 12:05 PM   #2
Shinni
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 167
Protected functions are only allowed to be called by addons when out of combat.
This verification of the combat state is done inside "CallSecureProtected". Protected functions must be called via CallSecureProtected and if the player is currently in combat, the function call will be blocked.
Since ZO_PreHookProtected calls CallSecureProtected, it is no longer possible to use the hooked function while in combat.

To fix this, you could hook IsItemUsable instead so that it will block the execution of UseItem here:
https://github.com/esoui/esoui/blob/...slot.lua#L1129

Last edited by Shinni : 11/23/21 at 12:09 PM.
  Reply With Quote
11/24/21, 01:20 PM   #3
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,912
Originally Posted by Shinni View Post
To fix this, you could hook IsItemUsable instead so that it will block the execution of UseItem here:
https://github.com/esoui/esoui/blob/...slot.lua#L1129
Depending on when you will prehook inventory related stuff and functions it may cause other error messages as the inventory code get's tainted, (via addon inserted code) before it was fully build.
It will show you lua errors like "'...from insecure code. The callstack became untrusted ...".

I was able to circumvent this for most cases by hooking into functions and handlers that get used in the inventory surrounding at EVENT_PLAYER_ACTIVATED instead of EVENT_ADD_ON_LOADED. But even then it could happen.

Btw: Another idea would be to hook into the inventory slot actions (LibCustomMenu should be able to help here) and prevent the addition of the "Use" action in total! This will remove the keybind for it as well + the context menu entry will be gone then.
I'm using this within FCOItemSaver e.g for protected items to remove the "Destroy", "Extract" and other actions for marked (protected) items.
Downside is: You'd also have to hook into the inventory row's eventHandler for "OnDoubleClick" to prevent the usage of the item via double click.

Last edited by Baertram : 11/24/21 at 01:26 PM.
  Reply With Quote
11/27/21, 10:43 AM   #4
@DeadSoon
AddOn Author - Click to view addons
Join Date: Sep 2018
Posts: 22
Originally Posted by Baertram View Post
Btw: Another idea would be to hook into the inventory slot actions (LibCustomMenu should be able to help here) and prevent the addition of the "Use" action in total! This will remove the keybind for it as well + the context menu entry will be gone then.
I'm using this within FCOItemSaver e.g for protected items to remove the "Destroy", "Extract" and other actions for marked (protected) items.
Downside is: You'd also have to hook into the inventory row's eventHandler for "OnDoubleClick" to prevent the usage of the item via double click.
Thank you both for the ideas and information. Hooking into the slot actions sounds good and should not effect the following "protected code". I will try it out.

Thanks again and best regards
  Reply With Quote

ESOUI » Developer Discussions » General Authoring Discussion » Secure Protected Function "UseItem"

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off