public
Description: WoW Addon - My fork of Ace's AddonLoader... cause I'm not willing to install an SVN client :)
Homepage: http://www.wowace.com/wiki/AddonLoader
Clone URL: git://github.com/tekkub/addonloader.git
tekkub (author)
Sat Jul 19 19:21:10 -0700 2008
commit  97dc0fe94727f6efbc513f4b8389d4715134022f
tree    49bb559d722faa78be6b02dbf203405028290912
parent  43fb50edcbcdf3deca9a8b1422e690abccf3eee9
addonloader / Conditions.lua
100644 276 lines (270 sloc) 8.921 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
local function returnTrue() return true end
 
-- There should be a cleaner way to handle this
local hookenv = setmetatable({}, {__index = getfenv(0)})
function hookenv.LoadAddOn(name)
  return AddonLoader:LoadAddOn(name)
end
 
local delayFrame -- will be filled if needed with a frame for loading addons delayed.
local BZ -- will be a reference to babble-zone-3.0 if needed
 
AddonLoader.conditions = {
  ["X-LoadOn-Mailbox"] = {
    events = {"MAIL_SHOW"},
    handler = returnTrue
  },
  ["X-LoadOn-AuctionHouse"] = {
    events = {"AUCTIONHOUSE_SHOW"},
    handler = returnTrue
  },
  ["X-LoadOn-Bank"] = {
    events = {"BANKFRAME_OPENED"},
    handler = returnTrue
  },
  ["X-LoadOn-Arena"] = {
    events = {"ZONE_CHANGED_NEW_AREA", "PLAYER_ENTERING_WORLD"},
    handler = function() return select(2, IsInInstance()) == "arena" end,
  },
  ["X-LoadOn-Battleground"] = {
    events = {"ZONE_CHANGED_NEW_AREA", "PLAYER_ENTERING_WORLD"},
    handler = function() return select(2, IsInInstance()) == "pvp" end,
  },
  ["X-LoadOn-Instance"] = {
    events = {"ZONE_CHANGED_NEW_AREA", "PLAYER_ENTERING_WORLD"},
    handler = function()
      local instanceType = select(2, IsInInstance())
      return instanceType == "party" or instanceType == "raid"
    end,
  },
  ["X-LoadOn-Combat"] = {
    events = {"PLAYER_REGEN_DISABLED", "PLAYER_ENTERING_WORLD"},
    handler = function( event, name, arg )
      if event == "PLAYER_REGEN_DISABLED" then return true end
      if event == "PLAYER_ENTERING_WORLD" then return InCombatLockdown() end
    end,
  },
  ["X-LoadOn-Crafting"] = {
    events = {"TRADE_SKILL_SHOW", "CRAFT_SHOW"},
    handler = returnTrue,
  },
  ["X-LoadOn-Group"] = {
    events = {"PARTY_MEMBERS_CHANGED","RAID_ROSTER_UPDATE", "PLAYER_ENTERING_WORLD"},
    handler = function() return GetNumRaidMembers() > 0 or GetNumPartyMembers() > 0 end,
  },
  ["X-LoadOn-Merchant"] = {
    events = {"MERCHANT_SHOW"},
    handler = returnTrue,
  },
  ["X-LoadOn-PvPFlagged"] = {
    events = {"UNIT_FACTION", "PLAYER_ENTERING_WORLD"},
    handler = function() return UnitIsPVP("player") end,
  },
  ["X-LoadOn-Raid"] = {
    events = {"RAID_ROSTER_UPDATE", "PLAYER_ENTERING_WORLD"},
    handler = function() return GetNumRaidMembers() > 0 end,
  },
  ["X-LoadOn-Class"] = {
    events = {"PLAYER_LOGIN"},
    handler = function(event, name, arg) return tostring(arg):upper() == select(2,UnitClass("player")) end,
  },
  ["X-LoadOn-Guild"] = {
    events = {"PLAYER_LOGIN"},
    handler = function() return IsInGuild() end,
  },
  ["X-LoadOn-Always"] = {
    events = {"PLAYER_LOGIN"},
    handler = function( event, name, arg )
      if (arg or ""):lower() ~= "delayed" then return true end
      -- delayed loading, one addon per second
      if not delayFrame then
        delayFrame = CreateFrame("Frame")
        delayFrame.addons = {}
        delayFrame.elapsed = 0
        delayFrame:SetScript("OnUpdate", function(self, elapsed)
          self.elapsed = self.elapsed + elapsed
          if self.elapsed >= 0.25 then
            self.elapsed = 0
 
            if next(self.addons) then
              for addon in pairs(self.addons) do
                AddonLoader:LoadAddOn(addon)
                self.addons[addon] = nil -- nuke from the list
                break
              end
            else
              self:Hide()
            end
          end
        end)
      end
      delayFrame.addons[name] = true
      delayFrame:Show()
    end,
  },
  ["X-LoadOn-Resting"] = {
    events = {"PLAYER_UPDATE_RESTING", "PLAYER_ENTERING_WORLD"},
    handler = function() return IsResting() end,
  },
  ["X-LoadOn-NotResting"] = {
    events = {"PLAYER_UPDATE_RESTING", "PLAYER_ENTERING_WORLD"},
    handler = function() return not IsResting() end,
  },
  ["X-LoadOn-Level"] = {
    events = {"PLAYER_LEVEL_UP", "PLAYER_ENTERING_WORLD"},
    handler = function(event, name, arg)
      local level = UnitLevel("player")
      for chunk in arg:gmatch('([%d%p^,]+)') do
        if tonumber(chunk) then -- '68'
          if level == tonumber(chunk) then return true end
        elseif chunk:match('%+') then -- '40+'
          if level >= tonumber(chunk:match('%d+')) then return true end
        elseif chunk:match('%-$') then -- '30-'
          if level <= tonumber(chunk:match('%d+')) then return true end
        elseif chunk:match('%d+%-%d+') then -- '20-47'
          local low, high = tonumber(chunk:match('(%d+)%-(%d+)'))
          if level >= low and level <= high then return true end
        end
      end
    end,
  },
  ["X-LoadOn-Events"] = {
    events = {"PLAYER_LOGIN"},
    handler = function(event, name, arg)
      for metaevent in arg:gmatch("[^ ,]+") do
        local meta
        local lookfor = "X-LoadOn-"..metaevent
        local conditiontext = AddonLoader.conditiontexts[name]
        for line in conditiontext:gmatch("[^\n]+") do
          local condname, text = string.match(line, "^([^:]*): (.*)$")
          if condname and text and condname == lookfor then
            meta = text
          end
        end
        if meta then
          local status, func, err = pcall(loadstring, meta)
          if func then
            setfenv(func, hookenv)
            if not AddonLoader.events[metaevent] then
              AddonLoader.events[metaevent] = {}
              AddonLoader.frame:RegisterEvent(metaevent)
            end
            if not AddonLoader.events[metaevent][name] then
              AddonLoader.events[metaevent][name] = {}
            end
            AddonLoader.events[metaevent][name][name..metaevent] = { -- name..metaevent to fake a unique condition name
              handler = func,
              arg = "",
            }
          else
            geterrorhandler()('## X-LoadOn-'..event..' ('..name..'): '..err)
          end
        end
      end
      -- We specifically DO NOT return true here, this handler just sets up the other conditions. And will remain dorment for the remainder
    end,
  },
  ["X-LoadOn-Hooks"] = {
    events = {"PLAYER_LOGIN"},
    handler = function(event, name, arg)
      for hook in arg:gmatch("[^ ,]+") do
        local meta
        local lookfor = "X-LoadOn-"..hook
        local conditiontext = AddonLoader.conditiontexts[name]
        for line in conditiontext:gmatch("[^\n]+") do
          local condname, text = string.match(line, "^([^:]*): (.*)$")
          if condname and text and condname == lookfor then
            meta = text
          end
        end
        if meta then
          local status, func, err = pcall(loadstring, meta)
          if func then
            hooksecurefunc( hook, func )
          else
            geterrorhandler()('## X-LoadOn-'..hook..' ('..name..'): '..err)
          end
        end
      end
      -- We specifically DO NOT return true here, this handler just sets up the other conditions. And will remain dorment for the remainder
    end,
  },
  ["X-LoadOn-Slash"] = {
    events = {"PLAYER_LOGIN"},
    handler = function(event, name, arg)
      local name_upper = name:upper():gsub('[^%w]','')
      local i = 0
      local slashes = {}
      for slash in arg:gmatch('([^, ]+)') do
        i = i + 1
        if slash:sub(1,1) ~= '/' then
          slash = '/'..slash
        end
        _G['SLASH_'..name_upper..i] = slash
        slashes[#slashes+1] = slash:upper()
      end
      SlashCmdList[name_upper] = function(text)
        local new = _G['SLASH_'..name_upper..'1']
        SlashCmdList[name_upper] = nil
        for _, v in ipairs( slashes ) do
          hash_SlashCmdList[v] = nil
        end
        AddonLoader:LoadAddOn(name)
        ChatFrame_OpenChat()
        ChatFrameEditBox:SetText(new..' '..text)
        ChatEdit_SendText(ChatFrameEditBox,1)
      end
      -- We specifically DO NOT return true here, this handler just sets up the other conditions. And will remain dorment for the remainder
    end,
  },
  ["X-LoadOn-LDB-Launcher"] = {
    events = {"PLAYER_LOGIN"},
    handler = function(event, name, arg)
      local texture, brokername = string.split(" ", arg)
      brokername = brokername or name
 
      local OnClick, dataobj
      OnClick = function(...)
        AddonLoader:LoadAddOn(name)
        if OnClick ~= dataobj.OnClick then dataobj.OnClick(...) end
      end
      dataobj = LibStub:GetLibrary("LibDataBroker-1.1"):NewDataObject(brokername, {type = "launcher", tocname = name, icon = texture, OnClick = OnClick})
      -- We specifically DO NOT return true here, this handler just sets up the other conditions. And will remain dorment for the remainder
    end,
  },
  ["X-LoadOn-Zone"] = {
    events = {"ZONE_CHANGED_NEW_AREA", "PLAYER_ENTERING_WORLD"},
    handler = function(event, name, arg)
      if not BZ then
        BZ = LibStub and LibStub("Babble-Zone-3.0", true) -- silent check for BZ
      end
      for zone in arg:gmatch('(%w[^,]+%w)') do
        if (BZ and GetRealZoneText() == BZ[zone]) or GetRealZoneText() == zone then
          return true
        end
      end
    end,
  },
  ["X-LoadOn-Execute"] = {
    events = {"PLAYER_LOGIN"},
    handler = function(event, name, arg)
      for i = 2, 5 do
        local lookfor = "X-LoadOn-Execute"..i
        local md
        local conditiontext = AdddonLoader.conditiontexts[name]
        for line in conditiontext:gmatch("[^\n]+") do
          local condname, text = string.match(line, "^([^:]*): (.*)$")
          if condname and text and condname == lookfor then
            md = text
          end
        end
        if md then
          arg = arg..' '..md
        else
          break
        end
      end
      local status, func, err = pcall(loadstring, arg)
      if func then
        func()
      else
        geterrorhandler()('## X-LoadOn-Execute '..name..': '..err)
      end
    end,
  },
}