Thread Tools Display Modes
07-22-12, 07:39 AM   #1
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Determine which addon calls a function

Hello.

I've got a simple question but I'm not sure if there is a solution.

I'm making an addon which is to serve as a framework. It has a an API function in its namespace with parameters which any addon will be able to call. Like this:

Code:
myAddon = {}

function myAddon:myFunction(arg1, arg2)
	-- do things
end
What I want to know is, is it possible to find out which addon calls the function, and get information about that addon, e.g. using GetAddOnMetadata()? All I need to know is the name, so that I can use this name in the function.

This could be done manually of course, and anyone using the function would simple have to pass the name as a parameter. But that can contain typos or simply be wrong. I want to protect the API function from screwing up because of bad usage in other addons.
  Reply With Quote
07-22-12, 10:14 AM   #2
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
I found this, but nothing that works in WoW atm, maybe there are some equivalent API?
http://stackoverflow.com/questions/1...lling-function
  Reply With Quote
07-22-12, 11:11 AM   #3
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Good idea - I found a debugstack() function by sniffing around in Blizzard_DebugTools. I'll test it and see if I can do what I tried with it.
  Reply With Quote
07-22-12, 11:14 AM   #4
Foxlit
A Warpwood Thunder Caller
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 91
Originally Posted by Haleth View Post
What I want to know is, is it possible to find out which addon calls the function, and get information about that addon, e.g. using GetAddOnMetadata()?
No, there is no reliable way to do that. Furthermore, having your library behave differently depending on which addon calls it is a horrible idea.
__________________
... and you do get used to it, after a while.
  Reply With Quote
07-22-12, 11:16 AM   #5
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
I simply want to display the name of the addon calling it - the function won't behave differently.
  Reply With Quote
07-22-12, 11:28 AM   #6
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Here we go:

Code:
local _, _, name = strsplit("\\", debugstack(2))
Placing this inside a function returns the name of the folder in which said function was called. Crude, but seems to work.
  Reply With Quote
07-22-12, 12:12 PM   #7
Foxlit
A Warpwood Thunder Caller
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 91
As long as your function is only called directly from another addon's code -- loadstring() and any library-provided pcall/xpcall-equivalents will result in incorrect values.
__________________
... and you do get used to it, after a while.
  Reply With Quote
07-22-12, 02:27 PM   #8
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by Haleth View Post
Code:
myAddon = {}

function myAddon:myFunction(arg1, arg2)
	-- do things
end
Blizzard already creates a table for you.

Code:
-- Put this at the top of your file
local folderName, myAddon = ...
You'll get a string which is the name of the folder your AddOn lives in (and I use as the display name for the AddOn itself) and a table which is shared between all of the files in the AddOn itself.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
07-22-12, 02:41 PM   #9
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Foxlit; Good point. Perhaps I should make sure that the previous return of the split is "AddOns".

Torhal; I used a bad example, my bad. I don't really create a table - I make one frame, assign a name to it, and use that name as the table. Then people will be able to easily access both the visual part (onscreen frame) as well as the internal part.
  Reply With Quote
07-23-12, 02:46 AM   #10
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,322
If a line is too long, debugstack() will truncate the beginning of it and add an ellipsis. There is no way to prevent it from doing so.

I've been working on a similar function for a new version of Spydon that'll provide a stack trace on every addon message sent out and for every prefix registration. I originally planned to have it return the name of the addon that called the respective function, but in dealing with framework/library code, I had to fallback to a full stack trace.
__________________
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)

Last edited by SDPhantom : 07-23-12 at 03:09 AM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Determine which addon calls a function

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