Thread Tools Display Modes
02-03-12, 10:17 PM   #1
unlimit
Lookin' Good
 
unlimit's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 484
Adding new things to global functions?

lua Code:
  1. local thelist = {};
  2. thelist["am_i"] = true;
  3. thelist["in_the"] = true;
  4. thelist["list"] = true;
  5.  
  6. function IntheList(name)
  7.     return thelist[name];
  8. end

Let's say I have two addons. I would like to, from my addon, be able to add my words into "thelist", but it's a local variable. However, IntheList(name) is a global function, is there anyway override the global function from MY addon and make it return "thelist[name] AND theotherlist[name];"?, or manually (without editing the original addon) to add my words into thelist?

qts;dfr (question too stupid, didn't finish reading): Is there a way to make IntheList(name) return from anything OTHER than just thelist[name] without editing the original addon?
__________________


kúdan: im playing pantheon
JRCapablanca: no youre not
** Pantheon has been Banned. **

Last edited by unlimit : 02-03-12 at 10:22 PM.
  Reply With Quote
02-03-12, 10:27 PM   #2
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Sure. Since it's a global function, you can either completely replace it or hook it.
__________________
"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
02-03-12, 11:15 PM   #3
unlimit
Lookin' Good
 
unlimit's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 484
Hooking seems to be what I'm after, but I honestly have no idea on how that works. I've tried looking up examples of Hooksecurefunc, but it's like reading Chinese in Spanish to me.

lua Code:
  1. hooksecurefunc("IntheList()", function()
  2.     return theotherlist[name]; end
  3. );

doesn't seem to work.

Code:
Interface\AddOns\TrueAui\test.lua:24: hooksecurefunc(): InTheList() is not a function

Stack trace:
-----------
Interface\AddOns\TrueAui\test.lua:24: in main chunk
__________________


kúdan: im playing pantheon
JRCapablanca: no youre not
** Pantheon has been Banned. **

Last edited by unlimit : 02-03-12 at 11:25 PM.
  Reply With Quote
02-04-12, 12:05 AM   #4
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Get rid of the parentheses. They're not a part of your function name.
__________________
"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
02-04-12, 12:10 AM   #5
unlimit
Lookin' Good
 
unlimit's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 484
Yeah, I tried that, but it just ends up throwing out the same error. I definitely know that function is there, but I'm thinking it just hates me. C_C

Code:
Interface\AddOns\TrueAui\test.lua:6: hooksecurefunc(): RDXDB.IsProtectedPkg is not a function

Stack trace:
-----------
Interface\AddOns\TrueAui\test.lua:6: in main chunk
HOW DARE YOU GIVE ME THIS ERROR, WOW-CLIENT. YOU. LIE. TO. ME.



File #1
lua Code:
  1. -- Some packages are protected
  2. local protectedPkg = {};
  3. protectedPkg["scripts"] = true;
  4. protectedPkg["default"] = true;
  5. protectedPkg["desktops"] = true;
  6.  
  7. function RDXDB.IsProtectedPkg(name)
  8.     return protectedPkg[name];
  9. end

File #2
lua Code:
  1. local truePkg = {};
  2. truePkg["true_core"] = true;
  3. truePkg["true_unitframes"] = true;
  4. truePkg["true_multiframes"] = true;
  5.  
  6. hooksecurefunc("RDXDB.IsProtectedPkg", function()
  7.     return truePkg[name]; end
  8. );
__________________


kúdan: im playing pantheon
JRCapablanca: no youre not
** Pantheon has been Banned. **

Last edited by unlimit : 02-04-12 at 12:42 AM.
  Reply With Quote
02-04-12, 02:53 AM   #6
Saiket
A Chromatic Dragonspawn
 
Saiket's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 154
Originally Posted by unlimit View Post
File #2
lua Code:
  1. local truePkg = {};
  2. truePkg["true_core"] = true;
  3. truePkg["true_unitframes"] = true;
  4. truePkg["true_multiframes"] = true;
  5.  
  6. hooksecurefunc("RDXDB.IsProtectedPkg", function()
  7.     return truePkg[name]; end
  8. );
hooksecurefunc takes either a variable name implicitly located in _G, or a table and a key. You would need to use it like this:
lua Code:
  1. hooksecurefunc(RDXDB, "IsProtectedPkg", function()

Even if you did fix that though, it still wouldn't work since hooks from hooksecurefunc discard your return values and preserve the original ones. You would have to manually (insecurely) hook the function like this:
lua Code:
  1. local RDXDBIsProtectedPkg = RDXDB.IsProtectedPkg;
  2. function RDXDB.IsProtectedPkg(name, ...)
  3.   return RDXDBIsProtectedPkg(name, ...) or mylist[name]
  4. end
An insecure hook is fine in this case since RDX's function is insecure to begin with. I wasn't sure if you wanted to add to or entirely replace returns from the original function, so my example just adds to them.
  Reply With Quote
02-04-12, 03:29 AM   #7
unlimit
Lookin' Good
 
unlimit's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 484
Thanks Saiket, and Seerah for helping me through this!

If I may ask, what does the ... signify as a function operator? (I think that's what it's called)
__________________


kúdan: im playing pantheon
JRCapablanca: no youre not
** Pantheon has been Banned. **
  Reply With Quote
02-04-12, 03:41 AM   #8
Saiket
A Chromatic Dragonspawn
 
Saiket's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 154
Originally Posted by unlimit View Post
Thanks Saiket, and Seerah for helping me through this!

If I may ask, what does the ... signify as a function operator? (I think that's what it's called)
`...` is called a vararg expression. It contains all extra arguments to the function without needing to give them each names. I used it to pass all input to the original function as a safety precaution, since we can't be sure that future versions of RDXDB.IsProtectedPkg won't use more arguments.
  Reply With Quote
02-04-12, 03:45 AM   #9
unlimit
Lookin' Good
 
unlimit's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 484
Ah! I see, cool stuff! Thanks for the quick reply!
__________________


kúdan: im playing pantheon
JRCapablanca: no youre not
** Pantheon has been Banned. **
  Reply With Quote
02-04-12, 05:30 AM   #10
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,322
Originally Posted by Saiket View Post
lua Code:
  1. local RDXDBIsProtectedPkg = RDXDB.IsProtectedPkg;
  2. function RDXDB.IsProtectedPkg(name, ...)
  3.   return RDXDBIsProtectedPkg(name, ...) or mylist[name]
  4. end
You may need to be careful, if RDXDBIsProtectedPkg() returns an actual value instead of nil that would represent false, it'll propagate to searching mylist anyway. The best way to do this is check it against nil before deciding which to return. The following will only fall back to mylist when RDXDBIsProtectedPkg() returns nil while preserving data instead of an accidental boolean conversion in some cases.
lua Code:
  1. local RDXDBIsProtectedPkg = RDXDB.IsProtectedPkg;
  2. function RDXDB.IsProtectedPkg(name,...)
  3.     local val=RDXDBIsProtectedPkg(name,...);
  4.     if val~=nil then
  5.         return val;
  6.     else
  7.         return mylist[name];
  8.     end
  9. end
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Adding new things to global functions?

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