-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.lua
More file actions
134 lines (114 loc) · 3.9 KB
/
Copy pathcache.lua
File metadata and controls
134 lines (114 loc) · 3.9 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
--[[ Stash / cache -------------------------------------------------------------
Captures the bank while the player is standing at one. Bank containers only
report their contents to the client while the bank window is open, so every
write is gated on that; outside the bank the stored copy is left alone.
-----------------------------------------------------------------------------]]
Stash.modules.cache = true
local pairs = pairs
local getn = table.getn
-- --------------------------------------------------------------------------
-- capture
-- --------------------------------------------------------------------------
-- Stores one container. Bank bags also record the bag item itself so the
-- offline view can draw the six bag buttons down the side of the frame.
function Stash:CaptureContainer(bag)
local char = Stash:GetChar()
if not char then
return
end
local size = GetContainerNumSlots(bag)
if not size or size < 1 then
char.containers[bag] = nil
return
end
local container = { size = size, items = {} }
if bag ~= Stash.BANK_CONTAINER then
local inventory = ContainerIDToInventoryID(bag)
if inventory then
container.link = GetInventoryItemLink("player", inventory)
container.texture = GetInventoryItemTexture("player", inventory)
container.invcount = GetInventoryItemCount("player", inventory)
end
end
for slot = 1, size do
local texture, count, _, quality = GetContainerItemInfo(bag, slot)
if texture then
container.items[slot] = {
texture = texture,
count = count or 1,
quality = quality,
link = GetContainerItemLink(bag, slot),
}
end
end
char.containers[bag] = container
end
function Stash:CaptureBank()
local char = Stash:GetChar()
if not char then
return
end
Stash:CaptureContainer(Stash.BANK_CONTAINER)
for bag = Stash.FIRST_BANK_BAG, Stash.LAST_BANK_BAG do
Stash:CaptureContainer(bag)
end
-- how many of the six bag slots have been bought, so the offline frame can
-- grey out the ones that are still locked
if GetNumBankSlots then
char.bankslots, char.bankfull = GetNumBankSlots()
end
char.updated = time()
Stash:Debug("captured bank")
end
function Stash:CaptureMoney()
local char = Stash:GetChar()
if char then
char.money = GetMoney()
end
end
function Stash:Debug(msg)
if Stash.debug then
Stash:Print("|cff888888" .. msg .. "|r")
end
end
-- --------------------------------------------------------------------------
-- events
-- --------------------------------------------------------------------------
local cache = CreateFrame("Frame")
cache:RegisterEvent("BANKFRAME_OPENED")
cache:RegisterEvent("BANKFRAME_CLOSED")
cache:RegisterEvent("PLAYERBANKSLOTS_CHANGED")
cache:RegisterEvent("PLAYERBANKBAGSLOTS_CHANGED")
cache:RegisterEvent("BAG_UPDATE")
cache:RegisterEvent("PLAYER_MONEY")
cache:SetScript("OnEvent", function()
if event == "BANKFRAME_OPENED" then
-- A real bank takes over from the offline view; otherwise the API wrappers
-- would keep answering from the cache in front of the live window.
Stash:EndOffline()
Stash.atbank = true
Stash:CaptureBank()
elseif event == "BANKFRAME_CLOSED" then
-- Capture once more on the way out: this is the state the player will see
-- next time they open the offline view.
if Stash.atbank then
Stash:CaptureBank()
end
Stash.atbank = nil
elseif event == "PLAYERBANKSLOTS_CHANGED" then
if Stash.atbank then
Stash:CaptureContainer(Stash.BANK_CONTAINER)
end
elseif event == "PLAYERBANKBAGSLOTS_CHANGED" then
if Stash.atbank then
Stash:CaptureBank()
end
elseif event == "BAG_UPDATE" then
-- Only bank bags matter here, and only while their contents are readable.
if Stash.atbank and arg1 and Stash.bankbags[arg1] then
Stash:CaptureContainer(arg1)
end
elseif event == "PLAYER_MONEY" then
Stash:CaptureMoney()
end
end)