Repository navigation
Expand file tree
/
Copy pathshim.lua
More file actions
247 lines (215 loc) · 7.32 KB
/
Copy pathshim.lua
File metadata and controls
247 lines (215 loc) · 7.32 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
--[[ Stash / shim --------------------------------------------------------------
The bridge between the cache and Blizzard's frames.
Every function below wraps the real API and only diverts bank containers, and
only while the offline view is up. Everything else falls straight through, so
the wrappers are harmless when they are not in use - which also means they can
be installed once at load and never removed. An error while the view is open
can therefore never leave the game answering from stale data.
Because the diversion happens at the API rather than in a window of our own,
anything that asks the usual questions about bank containers sees the cache:
Blizzard's BankFrame, and equally any bag addon reading the same functions.
-----------------------------------------------------------------------------]]
Stash.modules.shim = true
local pairs = pairs
-- invSlot -> generic bank slot index, e.g. inventory 40 is bank slot 2
local bankslot_by_inv = {}
-- invSlot -> bank bag container id, e.g. inventory 68 is container 5
local bankbag_by_inv = {}
local function BuildInventoryMaps()
if BankButtonIDToInvSlotID then
for slot = 1, (NUM_BANKGENERIC_SLOTS or 24) do
local inventory = BankButtonIDToInvSlotID(slot)
if inventory then
bankslot_by_inv[inventory] = slot
end
end
end
if ContainerIDToInventoryID then
for bag = Stash.FIRST_BANK_BAG, Stash.LAST_BANK_BAG do
local inventory = ContainerIDToInventoryID(bag)
if inventory then
bankbag_by_inv[inventory] = bag
end
end
end
end
BuildInventoryMaps()
-- tooltip.lua needs the same lookups
Stash.invmaps = { bankslot = bankslot_by_inv, bankbag = bankbag_by_inv }
-- --------------------------------------------------------------------------
-- predicates
-- --------------------------------------------------------------------------
-- true when a container query should be answered from the cache
function Stash:Serving(bag)
return Stash.offline and bag and Stash.bankbags[bag] or nil
end
local function ServingUnit(unit)
return Stash.offline and (unit == "player" or unit == nil)
end
-- --------------------------------------------------------------------------
-- container API
-- --------------------------------------------------------------------------
local origContainerNumSlots = GetContainerNumSlots
function GetContainerNumSlots(bag)
if Stash:Serving(bag) then
local container = Stash:GetContainer(bag)
return container and container.size or 0
end
return origContainerNumSlots(bag)
end
local origContainerItemInfo = GetContainerItemInfo
function GetContainerItemInfo(bag, slot)
if Stash:Serving(bag) then
local container = Stash:GetContainer(bag)
local item = container and container.items[slot]
if not item then
return nil
end
-- texture, count, locked, quality, readable
return item.texture, item.count, nil, item.quality, nil
end
return origContainerItemInfo(bag, slot)
end
local origContainerItemLink = GetContainerItemLink
function GetContainerItemLink(bag, slot)
if Stash:Serving(bag) then
local container = Stash:GetContainer(bag)
local item = container and container.items[slot]
return item and item.link
end
return origContainerItemLink(bag, slot)
end
if GetContainerItemCooldown then
local origContainerItemCooldown = GetContainerItemCooldown
function GetContainerItemCooldown(bag, slot)
if Stash:Serving(bag) then
return 0, 0, 0
end
return origContainerItemCooldown(bag, slot)
end
end
-- --------------------------------------------------------------------------
-- inventory API
--
-- The 24 bank slots are inventory slots as well as container -1 slots, and
-- Blizzard's own bank buttons read them the inventory way, so both paths have
-- to answer from the same cache.
-- --------------------------------------------------------------------------
-- Resolves an inventory slot to the cached record behind it. Returns the item
-- for a bank slot, or the container for one of the six bag slots.
local function ResolveInventory(slot)
local bankslot = bankslot_by_inv[slot]
if bankslot then
local container = Stash:GetContainer(Stash.BANK_CONTAINER)
return container and container.items[bankslot], nil
end
local bag = bankbag_by_inv[slot]
if bag then
return nil, Stash:GetContainer(bag)
end
return nil, nil
end
local origInventoryItemTexture = GetInventoryItemTexture
function GetInventoryItemTexture(unit, slot)
if ServingUnit(unit) then
local item, container = ResolveInventory(slot)
if item then
return item.texture
end
if container then
return container.texture
end
if bankslot_by_inv[slot] or bankbag_by_inv[slot] then
return nil
end
end
return origInventoryItemTexture(unit, slot)
end
local origInventoryItemLink = GetInventoryItemLink
function GetInventoryItemLink(unit, slot)
if ServingUnit(unit) then
local item, container = ResolveInventory(slot)
if item then
return item.link
end
if container then
return container.link
end
if bankslot_by_inv[slot] or bankbag_by_inv[slot] then
return nil
end
end
return origInventoryItemLink(unit, slot)
end
local origInventoryItemCount = GetInventoryItemCount
function GetInventoryItemCount(unit, slot)
if ServingUnit(unit) then
local item, container = ResolveInventory(slot)
if item then
return item.count or 1
end
if container then
return container.invcount or 1
end
if bankslot_by_inv[slot] or bankbag_by_inv[slot] then
return 0
end
end
return origInventoryItemCount(unit, slot)
end
if GetInventoryItemCooldown then
local origInventoryItemCooldown = GetInventoryItemCooldown
function GetInventoryItemCooldown(unit, slot)
if ServingUnit(unit) and (bankslot_by_inv[slot] or bankbag_by_inv[slot]) then
return 0, 0, 0
end
return origInventoryItemCooldown(unit, slot)
end
end
if IsInventoryItemLocked then
local origIsInventoryItemLocked = IsInventoryItemLocked
function IsInventoryItemLocked(slot)
if Stash.offline and (bankslot_by_inv[slot] or bankbag_by_inv[slot]) then
return nil
end
return origIsInventoryItemLocked(slot)
end
end
-- --------------------------------------------------------------------------
-- bank specific API
-- --------------------------------------------------------------------------
if GetNumBankSlots then
local origNumBankSlots = GetNumBankSlots
function GetNumBankSlots()
if Stash.offline then
local char = Stash:GetChar(Stash.viewing)
if char and char.bankslots then
return char.bankslots, char.bankfull
end
end
return origNumBankSlots()
end
end
-- Hiding BankFrame runs CloseBankFrame, which talks to the server about a bank
-- the player is not standing at. Swallow it while the offline view is up.
if CloseBankFrame then
local origCloseBankFrame = CloseBankFrame
function CloseBankFrame()
if Stash.offline then
return
end
return origCloseBankFrame()
end
end
-- Buying bank slots offline would be nonsense, and BankFrame offers the button
-- whenever GetNumBankSlots reports room left.
if PurchaseSlot then
local origPurchaseSlot = PurchaseSlot
function PurchaseSlot()
if Stash.offline then
Stash:Error("you have to be at a bank to buy slots.")
return
end
return origPurchaseSlot()
end
end