Quantcast About add-ons optimization - Page 4 - WoWInterface
Thread Tools Display Modes
01-16-18, 06:04 AM   #61
lightspark
A Frostmaul Preserver
 
lightspark's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2012
Posts: 272
Originally Posted by Resike View Post
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.
__________________

Last edited by lightspark : 01-16-18 at 06:09 AM.
  Reply With Quote
01-16-18, 12:38 PM   #62
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,260
Originally Posted by lightspark View Post
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.
  Reply With Quote
01-16-18, 12:40 PM   #63
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,435
Sometimes a pre-hook is necessary in order to alter values going into the Blizzard function, etc.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
01-16-18, 02:28 PM   #64
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
Originally Posted by Resike View Post
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.
__________________
Grab your sword and fight the Horde!
  Reply With Quote
01-16-18, 06:52 PM   #65
Banknorris
A Flamescale Wyrmkin
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 146
Originally Posted by Resike View Post
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.
__________________
"In this world nothing can be said to be certain, except death and taxes." - Benjamin Franklin

Last edited by Banknorris : 01-17-18 at 03:06 PM.
  Reply With Quote
01-17-18, 02:34 PM   #66
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,260
Originally Posted by Seerah View Post
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?

Last edited by Resike : 01-17-18 at 02:41 PM.
  Reply With Quote
01-17-18, 02:42 PM   #67
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,260
Originally Posted by Banknorris View Post
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)

Last edited by Resike : 01-17-18 at 02:57 PM.
  Reply With Quote
01-17-18, 07:55 PM   #68
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,435
Originally Posted by Resike View Post
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.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » About add-ons optimization

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