Thread Tools Display Modes
04/04/22, 05:35 AM   #1
static_recharge
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 32
Prehooking DestroyAllJunk

I'm having an issue with Prehooking the DestroyAllJunk() function. I'm fairly new to Prehooking but I followed a few examples and can't seem to get it to work. I want to grab some info about the items before they are all destroyed. Here is my code, right now it's just a message to chat to make sure it works and nothing happens. I have return set to true so that the original function doesn't run just for testing purposes but it runs anyway.

Code:
ZO_PreHook("DestroyAllJunk", function() d("test") return true end)
That line is in my Initialization function. Any help always appreciated!
  Reply With Quote
04/04/22, 07:46 AM   #2
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,912
Did you disable all other addons for testing, just to make sure it's nothing any other lib or addon interferes with?
Do you get any error message as this happens?

Your code looks okay to me and the return true should prevent the original function to start.
But maybe the function DestroyAllJunk is global but potected, so you cannot hook them. You should get an error message then I think (or it will just fail silently).

Try to use IsProtectedFunction(functionName) to check that.
  Reply With Quote
04/04/22, 09:00 AM   #3
static_recharge
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 32
Originally Posted by Baertram View Post
Did you disable all other addons for testing, just to make sure it's nothing any other lib or addon interferes with?
Do you get any error message as this happens?

Your code looks okay to me and the return true should prevent the original function to start.
But maybe the function DestroyAllJunk is global but potected, so you cannot hook them. You should get an error message then I think (or it will just fail silently).

Try to use IsProtectedFunction(functionName) to check that.
When I run the function from chat:
Code:
/script DestroyAllJunk()
My test message shows up. When I run it from the UI by hitting the hotkey while having at least one item on the junk tab it opens up the dialogue to confirm and then runs the function, as in it doesn't print my message and instead deletes the junk.

Edit: Yes I have only my new add-on running, nothing else.
  Reply With Quote
04/04/22, 11:04 AM   #4
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,912
I could not believe it and tried my self and you are right.

Checked which dialog it is that is shown there, via using merTorchbug or zgoo's /tbm (/tbug mouse) or /zgoo mouse above the dialog.

It may be necessary to remove the modal overlay so you are able to use the mouse properly there.
It should be :
/script ZO_Dialog1ModalUnderlay:SetHidden(true)

This "Are you sure you want to destroy..." dialog uses the string constant 5224 which is SI_DESTROY_ALL_JUNK
and is used in the dialog as mainText



Code:
ESO_Dialogs["DESTROY_ALL_JUNK"] =
{
    title =
    {
        text = SI_PROMPT_TITLE_DESTROY_ITEMS,
    },
    mainText = 
    {
        text = SI_DESTROY_ALL_JUNK,
    },
    buttons =
    {
        [1] =
        {
            text =      SI_DESTROY_ALL_JUNK_CONFIRM,
            callback =  DestroyAllJunk,
            clickSound = SOUNDS.INVENTORY_DESTROY_JUNK,
        },
        [2] =
        {
            text =       SI_DIALOG_DECLINE,
        },
    },
}
The function seems to be used in there yes.
I've added my own prehook in an addon at event_add_on_loaded:

Code:
ZO_PreHook("DestroyAllJunk", function()
                d("DestroyAllJunk -> Aborting now")
                
                return true
            end)
And If I use the keybind to open the dialog and click on the "ok" button or use the keybind of that button it shows in my chat:
nothing and it just destroys it as you said.

So yeah, seems to be either a bug or a feature

I've asked ZOsDanBatson if he knows whats going on here.
For now you can fix this by "overwriting" the acceptCallback function of that dialog with your own func which does the checks and then calls DestroyAllJunk I'd say.
At event_player_activated do once:

Lua Code:
  1. ESO_Dialogs["DESTROY_ALL_JUNK"] =
  2. {
  3.     title =
  4.     {
  5.         text = SI_PROMPT_TITLE_DESTROY_ITEMS,
  6.     },
  7.     mainText =
  8.     {
  9.         text = SI_DESTROY_ALL_JUNK,
  10.     },
  11.     buttons =
  12.     {
  13.         [1] =
  14.         {
  15.             text =      SI_DESTROY_ALL_JUNK_CONFIRM,
  16.             callback =  function()
  17.                  if not yourAddon.doNotDestroyAllJunk then
  18.                     DestroyAllJunk()
  19.                  else
  20.                    --Do other stuff or just let the dialog close
  21.                  end
  22.             end,
  23.  
  24.             clickSound = SOUNDS.INVENTORY_DESTROY_JUNK,
  25.         },
  26.         [2] =
  27.         {
  28.             text =       SI_DIALOG_DECLINE,
  29.         },
  30.     },
  31. }

Last edited by Baertram : 04/04/22 at 11:08 AM.
  Reply With Quote
04/04/22, 11:57 AM   #5
static_recharge
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 32
This works beautifully, thank you for diving into it with me!
  Reply With Quote
04/04/22, 12:52 PM   #6
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,566
That behaviour is to be expected.
The reference to DestroyAllJunk is stored at the time the dialog is defined, so anything you do to that function afterwards (like adding a hook, which essentially replaces DestroyAllJunk) doesn't apply to the callback of the dialog. ZOS would have to change the line
Lua Code:
  1. callback =  DestroyAllJunk,
to
Lua Code:
  1. callback = function() DestroyAllJunk() end,
so it gets the hooked DestroyAllJunk function at the time the callback is run.
Otherwise you have to manually replace the callback after you added the hook like Baertram said.
  Reply With Quote
04/04/22, 03:34 PM   #7
static_recharge
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 32
Thanks for the explanation, this makes a lot more sense now.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Prehooking DestroyAllJunk

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