WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   SecureHandler template object fails visibility check (https://www.wowinterface.com/forums/showthread.php?t=56024)

Eungavi 02-07-18 05:12 AM

SecureHandler template object fails visibility check
 
So, I'm currently trying to make a button that cancels aura on right mouse button and here's what I've got atm

Lua Code:
  1. local button = CreateFrame("Button", nil, UIParent, "SecureHandlerClickTemplate");
  2. button:RegisterEvent("PLAYER_ENTERING_WORLD");
  3. button:RegisterUnitEvent("UNIT_AURA", "player");
  4. button:RegisterForClicks("RightButtonUp");
  5. button:SetSize(50, 50);
  6. button:SetPoint("TOP", 0, -150);
  7. button:SetScript("OnEvent", function(self, event, ...)
  8.     if event == "PLAYER_ENTERING_WORLD" or event == "UNIT_AURA" then
  9.         if event == "PLAYER_ENTERING_WORLD" then
  10.             self:UnregisterEvent("PLAYER_ENTERING_WORLD");
  11.         end
  12.  
  13.         local spellName = GetSpellInfo(self.spellID);
  14.  
  15.         local _, _, _, count, _, duration, expires = UnitBuff("player", spellName);
  16.  
  17.         local cd = self.cd;
  18.         if duration and duration > 0 then
  19.             cd:SetCooldown(expires - duration, duration);
  20.             cd:Show();
  21.  
  22.             self.icon:SetDesaturated(false);
  23.  
  24.             self.duration = duration;
  25.         else
  26.             cd:Hide();
  27.  
  28.             self.icon:SetDesaturated(true);
  29.  
  30.             self.duration = nil;
  31.         end
  32.  
  33.         self.count:SetText((count and count > 1) and count or "");
  34.     end
  35. end);
  36. button:SetAttribute("_onclick", [=[
  37.     if button == "RightButton" and not down then
  38.         local name = self:GetAttribute("spellName");
  39.  
  40.         local cd = self:GetFrameRef("cd");
  41.        
  42.         if cd:IsShown() then
  43.             CancelUnitBuff("player", name);
  44.         end
  45.     end
  46. ]=]);
  47.  
  48. button.spellID = 241846;
  49. button:SetAttribute("spellName", GetSpellInfo(button.spellID));
  50.  
  51. local cd = CreateFrame("Cooldown", nil, button, "CooldownFrameTemplate");
  52. cd:SetAllPoints(button);
  53. cd:SetReverse(true);
  54. cd:SetDrawEdge(true);
  55. cd:SetHideCountdownNumbers(false);
  56.  
  57. button:SetFrameRef("cd", cd);

It currently fails at L#42 where I check cd's visibility.

Would that be because cd itself is not a SecureHandler template object?

kurapica.igas 02-07-18 07:45 AM

You may check the RestrictedEnvironment.lua#L18-L94, the CancelUnitBuff can't be used in the secure snippets. Also none secure frames like the cooldown can't be used in the secure snippets.

You should use SecureActionButtonTemplate instead of the SecureHandlerClickTemplate

Lua Code:
  1. local button = CreateFrame("Button", nil, UIParent, "SecureActionButtonTemplate")
  2. button:SetAttribute("*type2", "cancelaura")
  3. button:SetAttribute("spell", "the spell name")

See SecureTemplates.lua#L443-L459 for details.

MunkDev 02-07-18 08:04 AM

To clarify, you can get frame references to unprotected frames in a secure scope, but you can't see them in a secure scope when you're in combat. :GetFrameRef will not return your frame if it's not protected.

https://github.com/Gethe/wow-ui-sour...es.lua#L76-L97

Eungavi 02-07-18 03:24 PM

Many thanks to Kurapica and MunkDev.

Using SecureActionButtonTemplate did the trick :)

It seems like you would need a unit attribute set as well to use cancelaura, btw ;)

Eungavi 02-07-18 10:29 PM

Just being curious with

Lua Code:
  1. button:SetAttribute("*type2", "cancelaura")

Can you hook the script when it actually calls cancelaura function(?) either by hooksecurefunc() or :HookScript()?

kurapica.igas 02-08-18 05:44 AM

Quote:

Originally Posted by Engavi (Post 326763)
Just being curious with

Lua Code:
  1. button:SetAttribute("*type2", "cancelaura")

Can you hook the script when it actually calls cancelaura function(?) either by hooksecurefunc() or :HookScript()?

You can hooksecurefunc the CancelUnitBuff or use button:HookScript("OnClick", ...)

Eungavi 02-08-18 06:32 PM

Quote:

Originally Posted by kurapica.igas (Post 326764)
You can hooksecurefunc the CancelUnitBuff or use button:HookScript("OnClick", ...)

So, I just need to hook CancelUnitBuff method?

That's easier than I thought :D


All times are GMT -6. The time now is 01:57 AM.

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