ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Prehooking DestroyAllJunk (https://www.esoui.com/forums/showthread.php?t=10130)

static_recharge 04/04/22 05:35 AM

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!

Baertram 04/04/22 07:46 AM

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.

static_recharge 04/04/22 09:00 AM

Quote:

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

Baertram 04/04/22 11:04 AM

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. }

static_recharge 04/04/22 11:57 AM

This works beautifully, thank you for diving into it with me!

sirinsidiator 04/04/22 12:52 PM

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.

static_recharge 04/04/22 03:34 PM

Thanks for the explanation, this makes a lot more sense now.


All times are GMT -6. The time now is 09:57 AM.

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