Repository navigation
Expand file tree
/
Copy pathguildbank.lua
More file actions
378 lines (316 loc) · 9.88 KB
/
Copy pathguildbank.lua
File metadata and controls
378 lines (316 loc) · 9.88 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
--[[ Stash / guild bank --------------------------------------------------------
Vanilla 1.12 has no guild bank; it arrived with patch 2.3. Whether this client
backported one is a runtime question, so everything here is feature detected
against the standard API and stays dormant when it is missing.
/stash probe reports exactly which pieces this client has
When the API is present the flow mirrors the player bank: capture the tabs
while the guild bank window is open, then replay them through Blizzard's own
GuildBankFrame by diverting the same functions it reads.
-----------------------------------------------------------------------------]]
Stash.modules.guildbank = true
local pairs, ipairs = pairs, ipairs
local getn = table.getn
local SLOTS_PER_TAB = 98 -- MAX_GUILDBANK_SLOTS_PER_TAB in 2.3
local QUERYDELAY = 0.6 -- seconds between tab queries, the server rate limits
local queryqueue, capturequeue, nextquery
-- The functions a working guild bank needs, in probe order.
local required = {
"GetNumGuildBankTabs",
"GetGuildBankTabInfo",
"GetGuildBankItemInfo",
"GetGuildBankItemLink",
"QueryGuildBankTab",
}
local optional = {
"GetCurrentGuildBankTab",
"GetGuildBankMoney",
"GuildBankFrame",
"SetCurrentGuildBankTab",
}
-- --------------------------------------------------------------------------
-- detection
-- --------------------------------------------------------------------------
function Stash:HasGuildBank()
for _, name in ipairs(required) do
if not getglobal(name) then
return nil
end
end
return true
end
function Stash:ProbeGuildBank()
Stash:Print("guild bank support on this client:")
for _, name in ipairs(required) do
local present = getglobal(name) and "|cff33ff33yes|r" or "|cffff5555no|r"
Stash:Print(" " .. name .. ": " .. present)
end
for _, name in ipairs(optional) do
local present = getglobal(name) and "|cff33ff33yes|r" or "|cff888888no|r"
Stash:Print(" " .. name .. " (optional): " .. present)
end
local ui = "|cffff5555no|r"
if IsAddOnLoaded and GetAddOnInfo and GetAddOnInfo("Blizzard_GuildBankUI") then
ui = "|cff33ff33present|r"
end
Stash:Print(" Blizzard_GuildBankUI: " .. ui)
if Stash:HasGuildBank() then
Stash:Print("|cff33ff33guild bank API found - caching is active.|r")
else
Stash:Print("|cffff5555no guild bank on this client; that half stays off.|r")
end
end
-- --------------------------------------------------------------------------
-- storage
-- --------------------------------------------------------------------------
local function GuildKey()
local guild = GetGuildInfo("player")
return guild
end
function Stash:GetGuild(name)
local realm = Stash:GetRealm()
if not realm then
return nil
end
realm.guilds = realm.guilds or {}
return realm.guilds[name or Stash.viewingguild or GuildKey()]
end
local function EnsureGuild()
local realm = Stash:GetRealm()
local guild = GuildKey()
if not realm or not guild then
return nil
end
realm.guilds = realm.guilds or {}
realm.guilds[guild] = realm.guilds[guild] or { tabs = {} }
return realm.guilds[guild]
end
-- --------------------------------------------------------------------------
-- capture
-- --------------------------------------------------------------------------
function Stash:CaptureGuildTab(tab)
local store = EnsureGuild()
if not store then
return
end
local name, icon = GetGuildBankTabInfo(tab)
local record = { name = name, icon = icon, items = {} }
for slot = 1, (MAX_GUILDBANK_SLOTS_PER_TAB or SLOTS_PER_TAB) do
local texture, count = GetGuildBankItemInfo(tab, slot)
if texture then
record.items[slot] = {
texture = texture,
count = count or 1,
link = GetGuildBankItemLink(tab, slot),
}
end
end
store.tabs[tab] = record
store.updated = time()
if GetGuildBankMoney then
store.money = GetGuildBankMoney()
end
Stash:Debug("captured guild bank tab " .. tab)
end
-- Tabs have to be asked for one at a time and the answer arrives later, so the
-- queue queries a tab, then reads it back on the following tick.
local function StartCapture()
local tabs = GetNumGuildBankTabs()
if not tabs or tabs < 1 then
return
end
queryqueue, capturequeue, nextquery = {}, {}, nil
for tab = 1, tabs do
queryqueue[getn(queryqueue) + 1] = tab
end
end
-- --------------------------------------------------------------------------
-- api diversion
-- --------------------------------------------------------------------------
local function Serving()
return Stash.gboffline and true or nil
end
if Stash:HasGuildBank() then
local origNumTabs = GetNumGuildBankTabs
function GetNumGuildBankTabs()
if Serving() then
local store = Stash:GetGuild()
if store then
local highest = 0
for tab in pairs(store.tabs) do
if tab > highest then
highest = tab
end
end
return highest
end
end
return origNumTabs()
end
local origTabInfo = GetGuildBankTabInfo
function GetGuildBankTabInfo(tab)
if Serving() then
local store = Stash:GetGuild()
local record = store and store.tabs[tab]
if record then
-- name, icon, isViewable, canDeposit, numWithdrawals, remaining
return record.name, record.icon, 1, nil, 0, 0
end
return nil
end
return origTabInfo(tab)
end
local origItemInfo = GetGuildBankItemInfo
function GetGuildBankItemInfo(tab, slot)
if Serving() then
local store = Stash:GetGuild()
local record = store and store.tabs[tab]
local item = record and record.items[slot]
if not item then
return nil
end
return item.texture, item.count, nil
end
return origItemInfo(tab, slot)
end
local origItemLink = GetGuildBankItemLink
function GetGuildBankItemLink(tab, slot)
if Serving() then
local store = Stash:GetGuild()
local record = store and store.tabs[tab]
local item = record and record.items[slot]
return item and item.link
end
return origItemLink(tab, slot)
end
local origQuery = QueryGuildBankTab
function QueryGuildBankTab(tab)
if Serving() then
return
end
return origQuery(tab)
end
if GetGuildBankMoney then
local origMoney = GetGuildBankMoney
function GetGuildBankMoney()
if Serving() then
local store = Stash:GetGuild()
return store and store.money or 0
end
return origMoney()
end
end
end
-- --------------------------------------------------------------------------
-- viewer
-- --------------------------------------------------------------------------
function Stash:ShowGuildBank()
if not Stash:HasGuildBank() then
Stash:Error("this client has no guild bank API - run |cffffcc00/stash probe|r for details.")
return
end
if Stash.atguildbank then
Stash:Print("you are standing at the guild bank - that is the real thing.")
return
end
local guild = GuildKey()
if not guild then
Stash:Error("you are not in a guild.")
return
end
local store = Stash:GetGuild(guild)
if not store or not next(store.tabs) then
Stash:Error("nothing cached for " .. guild .. " yet - open the guild bank once.")
return
end
if not IsAddOnLoaded("Blizzard_GuildBankUI") then
LoadAddOn("Blizzard_GuildBankUI")
end
if not GuildBankFrame then
Stash:Error("the default guild bank frame is missing on this client.")
return
end
if Stash.gboffline and GuildBankFrame:IsShown() then
HideUIPanel(GuildBankFrame)
return
end
Stash.viewingguild = guild
Stash.gboffline = true
if ShowUIPanel then
ShowUIPanel(GuildBankFrame)
else
GuildBankFrame:Show()
end
if GuildBankFrame_Update then
GuildBankFrame_Update()
end
Stash:Print(guild .. "'s guild bank, cached copy.")
end
function Stash:EndGuildOffline()
if not Stash.gboffline then
return
end
Stash.gboffline = nil
Stash.viewingguild = nil
if GuildBankFrame and GuildBankFrame:IsShown() and GuildBankFrame_Update then
GuildBankFrame_Update()
end
end
-- --------------------------------------------------------------------------
-- events
-- --------------------------------------------------------------------------
if Stash:HasGuildBank() then
local gb = CreateFrame("Frame")
-- Registering an event this client does not know about would error, so each
-- one is attempted on its own.
for _, name in ipairs({ "GUILDBANKFRAME_OPENED", "GUILDBANKFRAME_CLOSED", "GUILDBANKBAGSLOTS_CHANGED" }) do
pcall(function()
gb:RegisterEvent(name)
end)
end
gb:SetScript("OnEvent", function()
if event == "GUILDBANKFRAME_OPENED" then
Stash:EndGuildOffline()
Stash.atguildbank = true
StartCapture()
elseif event == "GUILDBANKFRAME_CLOSED" then
Stash.atguildbank = nil
queryqueue, capturequeue = nil, nil
elseif event == "GUILDBANKBAGSLOTS_CHANGED" then
if Stash.atguildbank and GetCurrentGuildBankTab then
local tab = GetCurrentGuildBankTab()
if tab and tab > 0 then
Stash:CaptureGuildTab(tab)
end
end
end
end)
gb:SetScript("OnUpdate", function()
if not Stash.atguildbank or not queryqueue then
return
end
local now = GetTime()
if nextquery and now < nextquery then
return
end
nextquery = now + QUERYDELAY
-- read back whatever was asked for last tick
if capturequeue and getn(capturequeue) > 0 then
local tab = table.remove(capturequeue, 1)
Stash:CaptureGuildTab(tab)
end
if getn(queryqueue) > 0 then
local tab = table.remove(queryqueue, 1)
QueryGuildBankTab(tab)
capturequeue[getn(capturequeue) + 1] = tab
end
end)
if GuildBankFrame then
local origOnHide = GuildBankFrame:GetScript("OnHide")
GuildBankFrame:SetScript("OnHide", function()
if origOnHide then
origOnHide()
end
Stash:EndGuildOffline()
end)
end
end