Thread Tools Display Modes
10-23-15, 06:43 AM   #1
LanceDH
A Cyclonian
 
LanceDH's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 41
Need help with SpellBook taint

I'm currently working on an addon that interracts with the SpellBook but I seem to taint it which causes errors when trying to cast a spell from the book (because CastSpell() is protected).

The idea is that I have a button showing a spell, and when clicked it opens the SpellBook to the page where the spell is located.
As far as I've seen there's no function for this so I basically go through all the tabs, pages and slots to find the correct spell.
The issue is that this, and even just toggling the SpellBook, will taint it.
So I'm trying to figure out if there's a way I can fix it.
Full code can be found on my GitHub

Once the button is pressed the only relevant code is the following:
(line 122- 141)
It get the name of the spell, then calls the function to scout through the spellbook for that name.
If it's found it plays a highlight animation, else it just opens on the default page.
And then it opens the SpellBook if it's not open yet.
Lua Code:
  1. elseif (self.unlockType == UNLOCKTYPE_SPELL) then
  2.     local spellName = self.SpellName:GetText();
  3.     local success, slot = OpenSpellBookAtSpell(spellName);
  4.     if success then
  5.         ILW_SpellBookHighlight:ClearAllPoints();
  6.         ILW_SpellBookHighlight:SetPoint("TOPLEFT", slot, "TOPRIGHT");
  7.         ILW_SpellBookHighlight:Show();
  8.         ILW_SpellBookHighlight.increasing = true;
  9.         ILW_SpellBookHighlight:SetAlpha(0);
  10.     else
  11.         print("|cFFFFD100ILWhat:|r |cFFFF5555" .. spellName .." is not in the spellbook. It might be hidden.|r");
  12.         SpellBookSkillLineTab_OnClick(_G["SpellBookSkillLineTab2"]);
  13.         while (SpellBookPrevPageButton:IsEnabled()) do
  14.             SpellBookPrevPageButton_OnClick();
  15.         end
  16.     end
  17.        
  18.     if (not SpellBookFrame:IsShown()) then
  19.         ToggleSpellBook(BOOKTYPE_SPELL);
  20.     end

The function to scout works like this:
(line 47 - 105)
It simply starts at the first tab, checks all the slots until it reaches an empty one or the last one.
Goes through the different pages on that tab until it's done and goes to the second tab.
if it finds the spell it stays on that page and returns success and the slot (to archor the highlight to)
Lua Code:
  1. local function OpenSpellBookAtSpell(searchName)
  2.     local tabNr = 1;
  3.     local maxTabs = 2;
  4.     local buttonNr = 1;
  5.     local buttonPerPage = 12;
  6.     local spellName = "";
  7.     local firsTimeInTab = false;
  8.    
  9.     SpellBookSkillLineTab_OnClick(_G["SpellBookSkillLineTab"..tabNr]);
  10.     while (tabNr <= maxTabs) do
  11.    
  12.         -- go to the first page
  13.         if (not firsTimeInTab) then
  14.             while (SpellBookPrevPageButton:IsEnabled()) do
  15.                 SpellBookPrevPageButton_OnClick();
  16.             end
  17.             firsTimeInTab = true;
  18.         end
  19.    
  20.         -- if target slot has a spell in it
  21.         if _G["SpellButton"..buttonNr.."SpellName"] ~= nil and _G["SpellButton"..buttonNr.."SpellName"]:IsShown() then
  22.             -- if the current tab still has unchecked spells
  23.             if (buttonNr <= buttonPerPage) then
  24.                
  25.                 spellName = _G["SpellButton"..buttonNr.."SpellName"]:GetText();
  26.                
  27.                 if (spellName == searchName) then
  28.                     -- Found the spell, end the world
  29.                     return true, _G["SpellButton"..buttonNr];
  30.                 end
  31.                
  32.                 buttonNr = buttonNr + 2;
  33.  
  34.                 -- reached limit on uneven, go even
  35.                 -- Needed because slotNr goes L->R U->D while spells go U->D L->R
  36.                 if (buttonNr > 12 and buttonNr %2 == 1) then
  37.                     buttonNr = 2;
  38.                 end
  39.             end
  40.         else -- else check for next page
  41.            
  42.             -- has next page, flip page
  43.             if (SpellBookNextPageButton:IsEnabled() ) then
  44.                
  45.                 SpellBookNextPageButton_OnClick();
  46.                 buttonNr = 1;
  47.             else -- else next tab
  48.                
  49.                 buttonNr = 1;
  50.                 tabNr = tabNr + 1;
  51.                 SpellBookSkillLineTab_OnClick(_G["SpellBookSkillLineTab"..tabNr]);
  52.                 firsTimeInTab = false;
  53.             end        
  54.         end
  55.     end
  56.    
  57.     return false;
  58.    
  59. end

The template for the buttons are in the XML file.
Template lines 15 to 136
Actual buttons at 242 - 300

So I'm trying to figure out if there is a way to prevent the taint, or if there's just no way to do it correctly.

Last edited by LanceDH : 10-23-15 at 07:05 AM.
  Reply With Quote
10-23-15, 10:27 AM   #2
lightspark
A Rage Talon Dragon Guard
 
lightspark's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2012
Posts: 341
Not at home yet, can't run live tests, but here's my wild guess after I checked your code on GH... You have quite many Show/Hide calls, but you dun always check if character is in combat or not, you can't toggle frame visibility whilst in combat... You have to use InCombatLockdown() function to check whether it's safe to make those calls or not.

And I strongly recommend using taint log, it helps quite much...
__________________

Last edited by lightspark : 10-23-15 at 11:01 AM. Reason: checked code on github
  Reply With Quote
10-23-15, 12:35 PM   #3
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
I think your only chance of getting this to work correctly without breaking the spellbook is to create a secure button which executes a macro to /click the spellbook forward/back buttons to the proper page, because calling any of the spellbook functions directly is going to taint everything it touches.

That being said, why not just make it so people can drag the spell off the button you're displaying, rather than open the spellbook? It seems like an extra step, unless I'm not understanding what your addon is meant to do.
  Reply With Quote
10-23-15, 12:45 PM   #4
LanceDH
A Cyclonian
 
LanceDH's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 41
Originally Posted by lightspark View Post
Not at home yet, can't run live tests, but here's my wild guess after I checked your code on GH... You have quite many Show/Hide calls, but you dun always check if character is in combat or not, you can't toggle frame visibility whilst in combat... You have to use InCombatLockdown() function to check whether it's safe to make those calls or not.

And I strongly recommend using taint log, it helps quite much...
It's not the best way to do things, but I have the 2main frames not work during combat and hide once you enter combat.
The other ones are (in theory) impossible to trigger without either of those 2 frames being shown.
I used UnitAffectingCombat("player") for it though. Achieves the same thing but might look into using the function you gave instead.
As for taint log, totally forgot that was a thing.

Originally Posted by semlar View Post
I think your only chance of getting this to work correctly without breaking the spellbook is to create a secure button which executes a macro to /click the spellbook forward/back buttons to the proper page, because calling any of the spellbook functions directly is going to taint everything it touches.

That being said, why not just make it so people can drag the spell off the button you're displaying, rather than open the spellbook? It seems like an extra step, unless I'm not understanding what your addon is meant to do.
Yea as I was waiting for a reply I pretty much implemented the dragging as well.
I was already going to add that but the reason I wanted to have the SpellBook is because I also do things like open the dungeon journal, talent frame, glyph frame, .. which... I'm probably also tainting beyon belief though I don't think they call protected functions like the SpellBook does.
Another annoyance is that IsPassiveSpell("name") doesn't actually work correctly as it returns false for Tiger Strikes whish is a passive, probably because it triggers another spell with the same name that isn't a passive.
Not the biggest issue but just annoying and might require more work.

I'll check the taint log and see what comes up. worst case I'll have to live without it.


Edit:
Seems WoW just instantly taints any global variable in addons.
But as far as I understand my functions need to be global to call them in my XML code?
Or is there some other way to do this.

Last edited by LanceDH : 10-23-15 at 01:09 PM.
  Reply With Quote
10-23-15, 01:54 PM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
Originally Posted by LanceDH View Post
But as far as I understand my functions need to be global to call them in my XML code?
Correct. Though it's one of many reasons why it's suggested to not use XML. You can do almost everything directly from Lua.
__________________
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
10-23-15, 02:11 PM   #6
LanceDH
A Cyclonian
 
LanceDH's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 41
Originally Posted by SDPhantom View Post
Correct. Though it's one of many reasons why it's suggested to not use XML. You can do almost everything directly from Lua.
Well fk.
Here I've been programming addons in nothing but LUA thinking I'm not doing it the way I should.
And now I, for once, code one with XML..
  Reply With Quote
10-23-15, 02:41 PM   #7
lightspark
A Rage Talon Dragon Guard
 
lightspark's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2012
Posts: 341
Originally Posted by LanceDH View Post
It's not the best way to do things, but I have the 2main frames not work during combat and hide once you enter combat.
The other ones are (in theory) impossible to trigger without either of those 2 frames being shown.
I used UnitAffectingCombat("player") for it though. Achieves the same thing but might look into using the function you gave instead.
As for taint log, totally forgot that was a thing.
However, I managed to get one when I clicked ILW_SpellBookTab (button in spellbook) TWICE, not once, but twice, on first click I saw a warning from your addon in my chat, however, on second one I've got a taint (line #491, inside ToggleUnlockedPage function).

Code:
'ILW_UnlockContainer:Hide()'.
!BugGrabber\BugGrabber.lua:573: in function <!BugGrabber\BugGrabber.lua:573>
[C]:: in function 'Hide'
ILearnedWhat\ILearnedWhat-6.2b1.lua:491: in function <ILearnedWhat\ILearnedWhat.lua:486>
ILearnedWhat\ILearnedWhat-6.2b1.lua:504: in function <ILearnedWhat\ILearnedWhat.lua:503>

Originally Posted by LanceDH View Post
Seems WoW just instantly taints any global variable in addons.
But as far as I understand my functions need to be global to call them in my XML code?
Or is there some other way to do this.
Well, write it in pure Lua, safest approach possible.

One more thing. Their API is almost always safe, there are some exceptions though, but NEVER (almost never tbh, there are few exceptions too) use functions blizz implemented in Lua themselves, it almost always causes taints. Your taint you were talking about in OP was caused by usage of blizz' SpellBookSkillLineTab_* functions.

P.S. BTW, sorry, completely missed the point in my first comment, was a bit tired. Went full retard, realized it just now after a short nap,
__________________

Last edited by lightspark : 10-23-15 at 02:49 PM.
  Reply With Quote
10-23-15, 03:11 PM   #8
LanceDH
A Cyclonian
 
LanceDH's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 41
Originally Posted by lightspark View Post
However, I managed to get one when I clicked ILW_SpellBookTab (button in spellbook) TWICE, not once, but twice, on first click I saw a warning from your addon in my chat, however, on second one I've got a taint (line #491, inside ToggleUnlockedPage function).
Had no clue :Hide() also caused errors in combat so can't say I 'protected' any of those.
I guess PLAYER_REGEN_DISABLED, where I hide everything when combat starts, happens before errors start happening.
Will have to fix that now that I know.

Originally Posted by lightspark View Post
Well, write it in pure Lua, safest approach possible.

One more thing. Their API is almost always safe, there are some exceptions though, but NEVER (almost never tbh, there are few exceptions too) use functions blizz implemented in Lua themselves, it almost always causes taints. Your taint you were talking about in OP was caused by usage of blizz' SpellBookSkillLineTab_* functions.

P.S. BTW, sorry, completely missed the point in my first comment, was a bit tired. Went full retard, realized it just now after a short nap,
Wouldn't it be worth it to create frames in XML and use SetScript with local functions in LUA?
Or does the XML itself cause issues?
Having done it both ways now I must say I find XML to make the base for frames a lot easier to use and keep track of things, and doesn't clutter my LUA files as much.
  Reply With Quote
10-23-15, 03:46 PM   #9
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
Just wanted to clear something up, since people seem a little confused as to what taint actually is.

Anything an addon does is considered "tainted". Its purpose is to prevent an addon from tricking a blizzard function into executing code that it wouldn't normally be able to do directly, or otherwise influence the result of a protected function.

So, when we call a function like SpellBookPrevPageButton_OnClick, which calls SpellBookFrame_Update, which calls SpellBook_UpdatePlayerTab, which calls SpellBookFrame_UpdateSpells, which calls SpellButton_UpdateButton, which sets "self.offSpecID", which is then read by SpellButton_OnClick when you click the spell button, it directly influences whether or not "CastSpell" is called by the interface.

Since we've "tainted" the execution path by calling a function which ultimately affected a protected function, the interface prevents the protected function from being called.

When you create a global variable, it's tainted because it wasn't created by blizzard's "trusted" code, it was created by your addon, which is untrustworthy. If something in the interface were to read that variable it would taint its execution path.

This doesn't mean that global variables are bad, just that you need to take care not to name them something that would ever be referenced by anything else.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Need help with SpellBook taint

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