ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   General Authoring Discussion (https://www.esoui.com/forums/forumdisplay.php?f=174)
-   -   Secure Protected Function "UseItem" (https://www.esoui.com/forums/showthread.php?t=9994)

@DeadSoon 11/23/21 11:09 AM

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

Shinni 11/23/21 12:05 PM

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

Baertram 11/24/21 01:20 PM

Quote:

Originally Posted by Shinni (Post 44968)
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.

@DeadSoon 11/27/21 10:43 AM

Quote:

Originally Posted by Baertram (Post 44969)
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


All times are GMT -6. The time now is 05:19 AM.

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