WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   About add-ons optimization (https://www.wowinterface.com/forums/showthread.php?t=47694)

lightspark 01-16-18 06:04 AM

Quote:

Originally Posted by Resike (Post 326512)
What do you mean by pre-hooking? Of course if your hook is wrong it's not gonna work. Or if a hook does not actually hook than don't call it a hook.

Lua Code:
  1. local oldBlizzFunc = BlizzFunc
  2.  
  3. function BlizzFunc(...)
  4.     -- your stuff here
  5.  
  6.     oldBlizzFunc(...)
  7. end

People either do this thingy or replace Blizz function entirely to prevent it from doing something, quite common for bag addons.

In this case, if you create an upvalue for a function before some other addon redefines it, you'll be calling original function.

It's not a proper hook, that's why I wrote it in quotes.

Resike 01-16-18 12:38 PM

Quote:

Originally Posted by lightspark (Post 326515)
Lua Code:
  1. local oldBlizzFunc = BlizzFunc
  2.  
  3. function BlizzFunc(...)
  4.     -- your stuff here
  5.  
  6.     oldBlizzFunc(...)
  7. end

People either do this thingy or replace Blizz function entirely to prevent it from doing something, quite common for bag addons.

In this case, if you create an upvalue for a function before some other addon redefines it, you'll be calling original function.

It's not a proper hook, that's why I wrote it in quotes.

Well if you do this, you are already cutting the tree under yourself.

Seerah 01-16-18 12:40 PM

Sometimes a pre-hook is necessary in order to alter values going into the Blizzard function, etc.

Lombra 01-16-18 02:28 PM

Quote:

Originally Posted by Resike (Post 326513)
It makes no sense, you are saying this hook actually killing the functionality of the SetCVar calls? It also works as a described and prints the value regardless how and where do you upvalue it.

No, I'm saying a new function is defined. You may verify using this:

Code:

/run local a = SetCVar hooksecurefunc("SetCVar", function(name, value) if name == "Sound_EnableMusic" then print(name, value) end end) print(a == SetCVar)
My code does not print anything on login for me, nor does this macro: (don't use together with the addon code as it will upvalue the already hooked function)

Code:

/run local a = SetCVar hooksecurefunc("SetCVar", function(name, value) if name == "Sound_EnableMusic" then print(name, value) end end) a("Sound_EnableMusic", 1)
And regardless, the point still stands for insecure hooks, and whether those are a good idea or not wasn't the point.

Banknorris 01-16-18 06:52 PM

Quote:

Originally Posted by Resike (Post 326500)
Except you are not hooking a function call, but a call for it's memory pointer's reference.

You can try it yourself:

Lua Code:
  1. hooksecurefunc("SetCVar", function(name, value)
  2.     if name == "Sound_EnableMusic" then
  3.         print(name, value)
  4.     end
  5. end)
  6.  
  7. local SetCVar = SetCVar
  8.  
  9. SetCVar("Sound_EnableMusic", 1)

You can do this in the other way around, it won't change a thing:

Lua Code:
  1. local SetCVar = SetCVar
  2.  
  3. hooksecurefunc("SetCVar", function(name, value)
  4.     if name == "Sound_EnableMusic" then
  5.         print(name, value)
  6.     end
  7. end)
  8.  
  9. _G.SetCVar("Sound_EnableMusic", 1)

What would happen if in the last line of the second code you have used SetCVar("Sound_EnableMusic", 1) instead of _G.SetCVar("Sound_EnableMusic", 1)? Would it still print anything? If not then that is exactly the point: calling a local reference to a global function when the attribution of the local is made BEFORE you hook the global function will result in the hook (by that I mean the function with print(name,value)) not being called. But of course the hooked function (SetCVar) will run normally.

That was exactly what Semlar said. If in my addon I don't call _G.SetCVar but a local version of it and your addon (which wants to track SetCVar calls by hooksecurefunc'ing it) loads after mine, then you will not be able to see my SetCVar calls. Hence the advice to use _G.SetCVar (implicitally, by just not defining a local version of it) that is just a little bit slower but don't interfere with hooks.

Resike 01-17-18 02:34 PM

Quote:

Originally Posted by Seerah (Post 326518)
Sometimes a pre-hook is necessary in order to alter values going into the Blizzard function, etc.

Why would you do that instead of creating a custom function/hook for yourself only?

Resike 01-17-18 02:42 PM

Quote:

Originally Posted by Banknorris (Post 326523)
What would happen if in the last line of the second code you have used SetCVar("Sound_EnableMusic", 1) instead of _G.SetCVar("Sound_EnableMusic", 1)? Would it still print anything? If not then that is exactly the point, calling a local reference to a global function when the attribution of the local is made BEFORE you hook the global function will result in the hook (by that I mean the function with print(name,value) not being called. But of course the hooked function (SetCVar) will run normally.

That was exactly what Semlar said. If in my addon I don't call _G.SetCVar but a local version of it and your addon (which wants to track SetCVar calls by hooksecurefunc'ing it) loads after mine, then you will not be able to see my SetCVar calls. Hence the advice to use _G.SetCVar (implicitally, by just not defining a local version of it) that is just a little bit slower but don't interfere with hooks.

Still works. Actually it only works with the _G.

So this indeed does not seems to work, i might have been wrong about this, since i doubt it would be the post-patch changes:

Lua Code:
  1. local SetCVar = SetCVar
  2.  
  3. hooksecurefunc("SetCVar", function(name, value)
  4.     if name == "Sound_EnableMusic" then
  5.         print(name, value)
  6.     end
  7. end)
  8.  
  9. SetCVar("Sound_EnableMusic", 1)

Seerah 01-17-18 07:55 PM

Quote:

Originally Posted by Resike (Post 326532)
Why would you do that instead of creating a custom function/hook for yourself only?

For example...

A chat addon that alters/adds to messages before printing them in the chat frame. You have to catch the string first, edit the it, then continue to pass it through the frame's AddMessage method.

This is how addons add links to things (quests, etc.) in your chat frame. Add time stamps. Color names by class. Filter unwanted messages.


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

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