WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Adding new things to global functions? (https://www.wowinterface.com/forums/showthread.php?t=42529)

unlimit 02-03-12 10:17 PM

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?

Seerah 02-03-12 10:27 PM

Sure. Since it's a global function, you can either completely replace it or hook it.

unlimit 02-03-12 11:15 PM

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


Seerah 02-04-12 12:05 AM

Get rid of the parentheses. ;) They're not a part of your function name.

unlimit 02-04-12 12:10 AM

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. );

Saiket 02-04-12 02:53 AM

Quote:

Originally Posted by unlimit (Post 252068)
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.

unlimit 02-04-12 03:29 AM

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)

Saiket 02-04-12 03:41 AM

Quote:

Originally Posted by unlimit (Post 252073)
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.

unlimit 02-04-12 03:45 AM

Ah! I see, cool stuff! :) Thanks for the quick reply! :D

SDPhantom 02-04-12 05:30 AM

Quote:

Originally Posted by Saiket (Post 252072)
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


All times are GMT -6. The time now is 12:38 PM.

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