📄 stubby.lua
字号:
--[[ Stubby$Id: Stubby.lua 803 2006-04-06 21:48:45Z aradan $Version: 3.5.0.0908 (Platypus)Stubby is an addon that allows you to register boot code foryour addon.This bootcode will be run whenever your addon does not demandload on startup so that you can setup your own conditions forloading.A quick example of this is:------------------------------------------- Stubby.RegisterBootCode("myAddOn", "CommandHandler", [[ local function cmdHandler(msg) LoadAddOn("myAddOn") MyAddOn_Command(msg) end SLASH_MYADDON1 = "/myaddon" SlashCmdList['MYADDON'] = cmdHandler ]]);-------------------------------------------So, what did this just do? It registered some boot code(called "CommandHandler") with Stubby that Stubby will (in the case you are not demand loaded) execute on yourbehalf.In the above example, your boot code sets up a command handlerwhich causes your addon to load and process the command.Another example:-------------------------------------------Stubby.CreateAddOnLoadBootCode("myAddOn", "Blizzard_AuctionUI")-------------------------------------------Ok, what was that? Well you just setup some boot codefor your addon that will register an addon hook whenStubby loads and your addon doesn't. This addon hookwill cause your addon to load when the AuctionUI does.The primary functions that you will be interested in are: CreateAddOnLoadBootCode(ownerAddOn, triggerAddOn) CreateEventLoadBootCode(ownerAddOn, triggerEvent) CreateFunctionLoadBootCode(ownerAddOn, triggerFunction)And the manual, but vastly more powerful: RegisterBootCode(ownerAddOn, bootName, bootCode)Stubby can also save variables for you if you wish to retainstateful information in your boot code. (maybe you haverecieved notification from your user that they wish alwaysto have your addon load for the current toon?)These are the variable functions: SetConfig(ownerAddOn, variable, value, isGlobal)GetConfig(ownerAddOn, variable) ClearConfig(ownerAddOn, variable)The SetConfig function sets the configuration variable"variable" for ownerAddOn to value. The variable isper-toon unless isGlobal is set.The GetConfig function gets "variable" for ownerAddOnit will return per-toon values before global ones.The ClearConfig function clears the toon specific andglobal "variable" for ownerAddOn.The following functions are also available for you to useif you need to use some manual boot code and want tohook into some function, addon or event within your bootcode: Stubby.RegisterFunctionHook(triggerFunction, position, hookFunction, ...) Stubby.RegisterAddOnHook(triggerAddOn, ownerAddOn, hookFunction, ...) Stubby.RegisterEventHook(triggerEvent, ownerAddOn, hookFunction, ...)RegisterFunctionHook allows you to hook into a function.* The triggerFunction is a string that names the function you want to hook into. eg: "GameTooltip.SetOwner"* The position is a negative or positive number that defines the actual calling order of the addon. The smaller or more negative the number, the earlier in the call sequence your hookFunction will be called, the larger the number, the later your hook will be called. The actual original (hooked) function is called at position 0, so if your addon is hooked at a negative position, you will not have access to any return values.* You pass (by reference) your function that you wish called as hookFunction. This function will be called with the following parameters: hookFunction(hookParams, returnValue, hook1, hook2 .. hookN) - hookParams is a table containing the additional parameters passed to the RegisterFunctionHook function (the "..." params) - returnValue is an array of the returned values of the function or nil if none. - hook1..hookN are the original parameters of the hooked function in the original order.RegisterAddOnHook is very much like the register function hookcall except that there is no positioning (you may get notified inany order with respect to any other addons which may be hooked)* The triggerAddOn specifies the name of the addon of which you want to be notified of it's loading.* The ownerAddOn is your addon's name (used for removing hooks)* The hookFunction is a function that gets called when the triggerAddOn loads or if it is already loaded straight away. This function will be called with the following parameters hookFunction(hookParams) - hookParams is a table containing the additional parameters passed to the RegisterAddOnHook function (the "..." params)RegisterEventHook allows you to hook an event in much the sameway as the above functions.* The triggerEvent is an event which causes your hookFunction to be executed.* The ownerAddOn is your addon's name (used for removing hooks)* The hookFunction is a function that gets called whenever the triggerEvent fires (until canceled with UnregisterEventHook) This function will be called with the following parameters: hookFunction(hookParams, event, hook1, hook2 .. hookN)- hookParams is a table containing the additional parameterspassed to the RegisterEventHook function (the "..." params)- event is the event string that has just been fired- hook1..hookN are the original parameters of the eventfunction in the original order.Other functions which may be of interest are: UnregisterFunctionHook(triggerFunction, hookFunc) UnregisterAddOnHook(triggerAddOn, ownerAddOn) UnregisterEventHook(triggerEvent, ownerAddOn) UnregisterBootCode(ownerAddOn, bootName) There is also a single exposed 'constant' allowing you to dosome basic version checking for compatibility:Stubby.VERSION (introduced in revision 507)This constant is Stubby's revision number, a simple positiveinteger that will increase by an arbitrary amount with eachnew version of Stubby.Current $Revision: 803 $Example:-------------------------------------------if (Stubby.VERSION and Stubby.VERSION >= 507) then -- Register boot codeelse Stubby.Print("You need to update your version of Stubby!")end------------------------------------------- License: This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program(see GLP.txt); if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.--]]local cleanListlocal config = { hooks = { functions={}, origFuncs={} }, calls = { functions={}, callList={} }, loads = {}, events = {},}StubbyConfig = {}-- Function prototypeslocal chatPrint -- chatPrint(...)local checkAddOns -- checkAddOns()local clearConfig -- clearConfig(ownerAddOn, variable)local createAddOnLoadBootCode -- createAddOnLoadBootCode(ownerAddOn, triggerAddOn)local createEventLoadBootCode -- createEventLoadBootCode(ownerAddOn, triggerEvent)local createFunctionLoadBootCode -- createFunctionLoadBootCode(ownerAddOn, triggerFunction)local eventWatcher -- eventWatcher(event)local events -- events(event, param)local getConfig -- getConfig(ownerAddOn, variable)local getOrigFunc -- getOrigFunc(triggerFunction)local hookCall -- hookCall(funcName, a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20)local hookInto -- hookInto(triggerFunction)local inspectAddOn -- inspectAddOn(addonName, title, info)local loadWatcher -- loadWatcher(loadedAddOn)local onLoaded -- onLoaded()local onWorldStart -- onWorldStart()local rebuildNotifications -- rebuildNotifications(notifyItems)local registerAddOnHook -- registerAddOnHook(triggerAddOn, ownerAddOn, hookFunction, ...)local registerBootCode -- registerBootCode(ownerAddOn, bootName, bootCode)local registerEventHook -- registerEventHook(triggerEvent, ownerAddOn, hookFunction, ...)local registerFunctionHook -- registerFunctionHook(triggerFunction, position, hookFunc, ...)local runBootCodes -- runBootCodes()local searchForNewAddOns -- searchForNewAddOns()local cleanUpAddOnData -- cleanUpAddOnData()local cleanUpAddOnConfigs -- cleanUpAddOnConfigs()local setConfig -- setConfig(ownerAddOn, variable, value, isGlobal)local shouldInspectAddOn -- shouldInspectAddOn(addonName)local unregisterAddOnHook -- unregisterAddOnHook(triggerAddOn, ownerAddOn)local unregisterBootCode -- unregisterBootCode(ownerAddOn, bootName)local unregisterEventHook -- unregisterEventHook(triggerEvent, ownerAddOn)local unregisterFunctionHook -- unregisterFunctionHook(triggerFunction, hookFunc)-- Function definitions-- This function takes all the items and their requested orders-- and assigns an actual ordering to them.function rebuildNotifications(notifyItems) local notifyFuncs = {} for hookType, hData in notifyItems do notifyFuncs[hookType] = {} -- Sort all hooks for this type in ascending numerical order. local sortedPositions = {} for requestedPos,_ in hData do table.insert(sortedPositions, requestedPos) end table.sort(sortedPositions) -- Process the sorted request list and insert in correct -- order into the call list. for _,requestedPos in sortedPositions do local func = hData[requestedPos] table.insert(notifyFuncs[hookType], func) end end return notifyFuncsend-- This function's purpose is to execute all the attached-- functions in order and the original call at just before-- position 0.function hookCall(funcName, a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20) local r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20 local orig = Stubby.GetOrigFunc(funcName) if (not orig) then return end local res; local retVal = nil local returns = false local callees if config.calls and config.calls.callList and config.calls.callList[funcName] then callees = config.calls.callList[funcName] end if (callees) then for _,func in pairs(callees) do if (orig and func.p >= 0) then r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20 = orig(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20) if r1 or r2 or r3 or r4 or r5 or r6 or r7 or r8 or r9 or r10 or r11 or r12 or r13 or r14 or r15 or r16 or r17 or r18 or r19 or r20 then retVal = { r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20 } returns = true end orig = nil end local res, addit = func.f(func.a, retVal, a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20); if (type(res) == 'string') then if (res == 'abort') then return end if (res == 'killorig') then orig = nil end if (res == 'setreturn') then retVal = addit returns = true end if (res == 'setparams') then a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20 = unpack(addit) end end end end if (orig) then r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20 = orig(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20) if r1 or r2 or r3 or r4 or r5 or r6 or r7 or r8 or r9 or r10 or r11 or r12 or r13 or r14 or r15 or r16 or r17 or r18 or r19 or r20 then returns = true end end if (returns) then return r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20 endend-- This function automatically hooks Stubby in place of the-- original function, dynamically.Stubby_OldFunction = nilStubby_NewFunction = nilfunction hookInto(triggerFunction) if (config.hooks.origFuncs[triggerFunction]) then return end RunScript("Stubby_OldFunction = "..triggerFunction) RunScript("Stubby_NewFunction = function(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20) return Stubby.HookCall('"..triggerFunction.."', a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20) end") RunScript(triggerFunction.." = Stubby_NewFunction") config.hooks.functions[triggerFunction] = Stubby_NewFunction; config.hooks.origFuncs[triggerFunction] = Stubby_OldFunction; Stubby_NewFunction = nil Stubby_OldFunction = nilendfunction getOrigFunc(triggerFunction) if (config.hooks) and (config.hooks.origFuncs) then return config.hooks.origFuncs[triggerFunction] endend-- This function causes a given function to be hooked by stubby and-- configures the hook function to be called at the given position.-- The original function gets executed a position 0. Use a negative-- number to get called before the original function, and positive-- number to get called after the original function. Default position-- is 200. If someone else is already using your number, you will get-- automatically moved up for after or down for before. Please also-- leave space for other people who may need to position their hooks-- in between your hook and the original.function registerFunctionHook(triggerFunction, position, hookFunc, ...) local insertPos = tonumber(position) or 200 local funcObj = { f=hookFunc, a=arg, p=position } if (table.getn(arg) == 0) then funcObj.a = nil; end if (not config.calls) then config.calls = {} end if (not config.calls.functions) then config.calls.functions = {} end if (config.calls.functions[triggerFunction]) then while (config.calls.functions[triggerFunction][insertPos]) do if (position >= 0) then insertPos = insertPos + 1 else insertPos = insertPos - 1 end end config.calls.functions[triggerFunction][insertPos] = funcObj else config.calls.functions[triggerFunction] = {} config.calls.functions[triggerFunction][insertPos] = funcObj end config.calls.callList = rebuildNotifications(config.calls.functions); hookInto(triggerFunction)endfunction unregisterFunctionHook(triggerFunction, hookFunc) if not (config.calls and config.calls.functions and config.calls.functions[triggerFunction]) then return end for pos, funcObj in config.calls.functions[triggerFunction] do if (funcObj and funcObj.f == hookFunc) then config.calls.functions[triggerFunction][pos] = nil end endend-- This function registers a given function to be called when a given-- addon is loaded, or immediatly if it is already loaded (this can be-- used to setup a hooking function to execute when an addon is loaded-- but not before)function registerAddOnHook(triggerAddOn, ownerAddOn, hookFunction, ...) if (IsAddOnLoaded(triggerAddOn)) then hookFunction(unpack(arg))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -