12.07.2015 Views

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

464 Part III ■ Advanced Addon Techniqueswithout becoming tainted; <strong>and</strong> because that code can remain secure, it cancompile the attribute contents into an executable function that is also secure.When it compiles this function, it can also restrict its access to specific functions<strong>and</strong> variables so that the secure function can’t use in<strong>for</strong>mation that the Blizzarddesigners don’t want influencing sensitive decisions.Writing a SnippetA snippet is the body <strong>of</strong> a function, without the function (args, ...) <strong>and</strong>final end tags. These elements are provided by the secure code that compilesthe snippet into a usable function. Because many snippets emulate frame scripth<strong>and</strong>lers (OnClick, OnShow, <strong>and</strong> so on), the argument list is generally the sameas it would be in the equivalent XML script , , <strong>and</strong> so <strong>for</strong>th.Snippets <strong>of</strong>ten contain code comprised <strong>of</strong> quotes, line breaks, <strong>and</strong> othercharacters that normally need to be escaped inside Lua string literals. Because<strong>of</strong> this, there is an emerging convention to enclose snippet literals in [[longbrackets,]] which ignore all special characters (except the end <strong>of</strong> the bracket).This has the advantage <strong>of</strong> allowing you to <strong>for</strong>mat your code in a more naturalfashion, as well as <strong>of</strong>ten making it easier <strong>for</strong> you to distinguish the secure <strong>and</strong>insecure parts <strong>of</strong> your addon code.You can demo a very simple snippet with the following line:/run SecureH<strong>and</strong>lerExecute(PlayerFrame, [[self:Hide()]])By itself, this isn’t terribly exciting. Insecure code can’t use SecureH<strong>and</strong>lerExecute during combat, <strong>and</strong> that code could always hide the player framedirectly. SecureH<strong>and</strong>lerExecute is mostly used to per<strong>for</strong>m setup on more complexsecure h<strong>and</strong>ler frames. The real power <strong>of</strong> snippets <strong>and</strong> secure executioncomes from the ability to attach snippets to frames that will run them securelyin response to user input or selected state changes. Be<strong>for</strong>e we move on to that,though, put the player frame back by running the following code (snippetscan be stored in variables as well as in string literals):local showAction = [[self:Show()]]; iSecureH<strong>and</strong>lerExecute(PlayerFrame, showAction)Secure H<strong>and</strong>ler FramesTo get <strong>WoW</strong> to compile <strong>and</strong> run your snippets, you have to store <strong>and</strong> attachthem correctly. The most common mechanism <strong>for</strong> this is a set <strong>of</strong> protectedtemplates called the secure h<strong>and</strong>lers.Much the same way as SecureActionButtonTemplate provides a securefunction as its OnClick h<strong>and</strong>ler, these templates each provide one or twosecure functions as various script h<strong>and</strong>lers. Each <strong>of</strong> these functions looks <strong>for</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!