-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibGuildStorage-1.2.lua
458 lines (403 loc) · 13.1 KB
/
LibGuildStorage-1.2.lua
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
-- This library handles storing information in officer notes. It
-- streamlines and optimizes access to these notes. It should be noted
-- that the library does not have correct information until
-- PLAYER_ENTERING_WORLD is fired (for Ace authors this is after OnInitialize
-- is called). The API is as follows:
--
-- GetNote(name): Returns the officer note of member 'name'
--
-- SetNote(name, note): Sets the officer note of member 'name' to
-- 'note'
--
-- GetClass(name): Returns the class of member 'name'
--
-- GetGuildInfo(): Returns the guild info text
--
-- IsCurrentState(): Return true if the state of the library is current.
--
-- Snapshot(table) -- DEPRECATED: Write out snapshot in the table
-- provided. table.guild_info will contain the epgp clause in guild
-- info and table.notes a table of {name, class, note}.
--
-- The library also fires the following messages, which you can
-- register for through RegisterCallback and unregister through
-- UnregisterCallback. You can also unregister all messages through
-- UnregisterAllCallbacks.
--
-- GuildInfoChanged(info): Fired when guild info has changed since its
-- previous state. The info is the new guild info.
--
-- GuildNoteChanged(name, note): Fired when a guild note changes. The
-- name is the name of the member of which the note changed and the
-- note is the new note.
--
-- StateChanged(): Fired when the state of the guild storage cache has
-- changed.
--
-- SetOutsidersEnabled(isOutsidersEnabled): Allows developers to enable/
-- disable the outsiders patch, which allows raidleaders to store EPGP
-- data of non-guildies in a lvl 1 character which is in guild.
local MAJOR_VERSION = "LibGuildStorage-1.2"
local MINOR_VERSION = tonumber(("$Revision$"):match("%d+")) or 0
local ADDON_MESSAGE_PREFIX = "GuildStorage10"
RegisterAddonMessagePrefix(ADDON_MESSAGE_PREFIX)
local lib, oldMinor = LibStub:NewLibrary(MAJOR_VERSION, MINOR_VERSION)
if not lib then return end
local Debug = LibStub("LibDebug-1.0")
local GUILDFRAMEVISIBLE = false
local OUTSIDERSENABLED = false
local CallbackHandler = LibStub("CallbackHandler-1.0")
if not lib.callbacks then
lib.callbacks = CallbackHandler:New(lib)
end
local callbacks = lib.callbacks
local AceHook = LibStub("AceHook-3.0")
AceHook:Embed(lib)
lib:UnhookAll()
if lib.frame then
lib.frame:UnregisterAllEvents()
lib.frame:SetScript("OnEvent", nil)
lib.frame:SetScript("OnUpdate", nil)
else
lib.frame = CreateFrame("Frame", MAJOR_VERSION .. "_Frame")
end
local frame = lib.frame
frame:Show()
frame:SetScript("OnEvent",
function(self, event, ...)
lib[event](lib, ...)
end)
local SendAddonMessage = _G.SendAddonMessage
if ChatThrottleLib then
SendAddonMessage = function(...)
ChatThrottleLib:SendAddonMessage(
"ALERT", ADDON_MESSAGE_PREFIX, ...)
end
end
local SetState
-- state of the cache: UNINITIALIZED, STALE,
-- STALE_WAITING_FOR_ROSTER_UPDATE, CURRENT, FLUSHING, REMOTE_FLUSHING
--
-- A complete graph of state changes is found in LibGuildStorage-1.0.dot
local state = "STALE_WAITING_FOR_ROSTER_UPDATE"
local initialized
local index
-- name -> {note=, seen=, class=}
local cache = {}
-- pending notes to write out
local pending_note = {}
local guild_info = ""
function lib:GetNote(name)
local e = cache[name]
if e then return e.note end
end
function lib:SetNote(name, note)
local e = cache[name]
if e then
if pending_note[name] then
DEFAULT_CHAT_FRAME:AddMessage(
string.format("Ignoring attempt to set note before flushing pending "..
"note for %s! "..
"current=[%s] pending=[%s] new[%s]. "..
"Please report this bug along with the actions that "..
"lead to this on http://epgp.googlecode.com",
tostring(name),
tostring(e.note),
tostring(pending_note[name]),
tostring(note)))
else
pending_note[name] = note
SetState("FLUSHING")
end
return e.note
end
end
function lib:GetClass(name)
local e = cache[name]
if e then return e.class end
end
function lib:GetRank(name)
local e = cache[name]
if e then return e.rank end
end
function lib:GetGuildInfo()
return guild_info
end
function lib:IsCurrentState()
return state == "CURRENT"
end
-- This is kept for historical reasons. See:
-- http://code.google.com/p/epgp/issues/detail?id=350.
function lib:Snapshot(t)
assert(type(t) == "table")
t.guild_info = guild_info:match("%-EPGP%-\n(.*)\n\%-EPGP%-")
t.roster_info = {}
for name,info in pairs(cache) do
table.insert(t.roster_info, {name, info.class, info.note})
end
end
-- This function allows users to enable or disable the outsiders patch
function lib:SetOutsidersEnabled(isOutsidersEnabled)
-- Dont do anything if the boolean is the same
if (OUTSIDERSENABLED == isOutsidersEnabled) then
return
end
OUTSIDERSENABLED = isOutsidersEnabled
Debug("outsider changed, now is ", OUTSIDERSENABLED)
-- Force reloading of guildnotes
index = nil
SetState("STALE")
end
--
-- Event handlers
--
frame:RegisterEvent("PLAYER_GUILD_UPDATE")
frame:RegisterEvent("GUILD_ROSTER_UPDATE")
frame:RegisterEvent("CHAT_MSG_ADDON")
frame:RegisterEvent("PLAYER_ENTERING_WORLD")
function lib:CHAT_MSG_ADDON(prefix, msg, type, sender)
Debug("CHAT_MSG_ADDON: %s, %s, %s, %s", prefix, msg, type, sender)
if prefix ~= MAJOR_VERSION or sender == UnitName("player") then return end
if msg == "CHANGES_PENDING" then
SetState("REMOTE_FLUSHING")
elseif msg == "CHANGES_FLUSHED" then
SetState("STALE_WAITING_FOR_ROSTER_UPDATE")
end
end
function lib:PLAYER_GUILD_UPDATE()
if IsInGuild() then
frame:Show()
else
frame:Hide()
end
SetState("STALE_WAITING_FOR_ROSTER_UPDATE")
end
function lib:PLAYER_ENTERING_WORLD()
lib:PLAYER_GUILD_UPDATE()
end
function lib:GUILD_ROSTER_UPDATE(loc)
Debug("GUILD_ROSTER_UPDATE(%s)", tostring(loc))
if loc then
SetState("FLUSHING") -- SetState("STALE_WAITING_FOR_ROSTER_UPDATE")
else
if state ~= "UNINITIALIZED" then
SetState("STALE")
index = nil
end
end
end
--
-- Locally defined functions
--
local valid_transitions = {
UNINITIALIZED = {
CURRENT = true,
},
STALE = {
CURRENT = true,
REMOTE_FLUSHING = true,
STALE_WAITING_FOR_ROSTER_UPDATE = true,
},
STALE_WAITING_FOR_ROSTER_UPDATE = {
STALE = true,
FLUSHING = true,
},
CURRENT = {
FLUSHING = true,
REMOTE_FLUSHING = true,
STALE = true,
},
FLUSHING = {
STALE_WAITING_FOR_ROSTER_UPDATE = true,
},
REMOTE_FLUSHING = {
STALE_WAITING_FOR_ROSTER_UPDATE = true,
},
}
function SetState(new_state)
if state == new_state then return end
if not valid_transitions[state][new_state] then
Debug("Ignoring state change %s -> %s", state, new_state)
return
else
Debug("StateChanged: %s -> %s", state, new_state)
state = new_state
if new_state == FLUSHING then
SendAddonMessage("CHANGES_PENDING", "GUILD")
end
callbacks:Fire("StateChanged")
end
end
local function ForceShowOffline()
-- We need to always show offline members in the roster otherwise this
-- lib won't work.
if GUILDFRAMEVISIBLE then
return true
end
SetGuildRosterShowOffline(true)
return false
end
local function Frame_OnUpdate(self, elapsed)
local startTime = debugprofilestop()
if ForceShowOffline() then
return
end
if state == "CURRENT" then
return
end
if state == "STALE_WAITING_FOR_ROSTER_UPDATE" then
GuildRoster()
return
end
local num_guild_members = GetNumGuildMembers()
-- Sometimes GetNumGuildMembers returns 0. In this case return now,
-- so that we call it again and get a proper value.
if num_guild_members == 0 then return end
if not index or index >= num_guild_members then
index = 1
end
-- Check guild info for changes.
if index == 1 then
local new_guild_info = GetGuildInfoText() or ""
if new_guild_info ~= guild_info then
guild_info = new_guild_info
callbacks:Fire("GuildInfoChanged", guild_info)
end
end
-- Read up to 100 members at a time.
local last_index = math.min(index + 100, num_guild_members)
if not initialized then last_index = num_guild_members end
Debug("Processing from %d to %d members", index, last_index)
for i = index, last_index do
local name, rank, _, _, _, _, pubNote, note, _, _, class = GetGuildRosterInfo(i)
-- strip off the -server portion of roster info
local name = Ambiguate(name, "none")
-- Start of outsiders patch
if OUTSIDERSENABLED then
local extName = strmatch(pubNote, 'ext:%s-(%S+)%s-')
local holder
if extName then
-- the name is now the note and the external name is the new name.
local entry = cache[extName]
if not entry then
entry = {}
cache[extName] = entry
end
local ep_test = EPGP:DecodeNote(note)
if not ep_test then --current character does not contain epgp info in its note, map to the character who contains
holder = note
else
holder = name
end
Debug("Entry " .. holder .. " is " .. extName)
-- Mark this note as seen
entry.seen = true
if entry.note ~= holder then
entry.note = holder
local _, unitClass = UnitClass(extName)
entry.rank = "Outsider("..name..")"
-- instead of using '' when there's no "unitClass", using the "class" of the placeholderalt
-- (don't know if this is needed with resetting "seen"-flag. This was my first good try to avoid
-- a bug : \epgp\ui.lua line 1203: attempt to index local 'c' (a nil value) -- local c = RAID_CLASS_COLORS[EPGP:GetClass(row.name)])
entry.class = unitClass or class
if initialized then
callbacks:Fire("GuildNoteChanged", extName, holder)
end
if entry.pending_note then
callbacks:Fire("InconsistentNote", extName, holder, entry.note, entry.pending_note)
end
end
if entry.pending_note then
GuildRosterSetOfficerNote(i, entry.pending_note)
entry.pending_note = nil
end
end
end -- if OUTSIDERSENABLED
if name then
local entry = cache[name]
local pending = pending_note[name]
if not entry then
entry = {}
cache[name] = entry
end
entry.rank = rank
entry.class = class
-- Mark this note as seen
entry.seen = true
if entry.note ~= note then
entry.note = note
-- We want to delay all GuildNoteChanged calls until we have a
-- complete view of the guild, otherwise alts might not be
-- rejected (we read alts note before we even know about the
-- main).
if initialized then
callbacks:Fire("GuildNoteChanged", name, note)
end
if pending then
callbacks:Fire("InconsistentNote", name, note, entry.note, pending)
end
end
if pending then
GuildRosterSetOfficerNote(i, pending)
pending_note[name] = nil
end
end
end
index = last_index
if index >= num_guild_members then
-- We are done, we need to clear the seen marks and delete the
-- unmarked entries. We also fire events for removed members now.
for name, t in pairs(cache) do
if t.seen then
t.seen = nil
else
cache[name] = nil
callbacks:Fire("GuildNoteDeleted", name)
end
end
if not initialized then
-- Now make all GuildNoteChanged calls because we have a full
-- state.
for name, t in pairs(cache) do
callbacks:Fire("GuildNoteChanged", name, t.note)
end
initialized = true
callbacks:Fire("StateChanged")
end
if state == "STALE" then
SetState("CURRENT")
elseif state == "FLUSHING" then
if not next(pending_note) then
SetState("STALE_WAITING_FOR_ROSTER_UPDATE")
SendAddonMessage("CHANGES_FLUSHED", "GUILD")
end
end
end
Debug(tostring(debugprofilestop() - startTime).."ms for LibGuildStorage:OnUpdate")
end
-- Disable updates when the guild roster is open.
-- This is a temporary hack until we get a better location for data storage
lib:RawHook("GuildFrame_LoadUI", function(...)
SetGuildRosterShowOffline(EPGP.db.profile.blizzard_show_offline)
lib.hooks.GuildFrame_LoadUI(...)
lib:RawHookScript(GuildRosterFrame, "OnShow", function(frame, ...)
GUILDFRAMEVISIBLE = true
if GuildRosterShowOfflineButton then
GuildRosterShowOfflineButton:SetChecked(EPGP.db.profile.blizzard_show_offline)
GuildRosterShowOfflineButton:Enable()
end
SetGuildRosterShowOffline(EPGP.db.profile.blizzard_show_offline)
lib.hooks[frame].OnShow(frame, ...)
end)
lib:RawHookScript(GuildRosterFrame, "OnHide", function(frame, ...)
GUILDFRAMEVISIBLE = false
EPGP.db.profile.blizzard_show_offline = GetGuildRosterShowOffline()
lib.hooks[frame].OnHide(frame, ...)
SetGuildRosterShowOffline(true)
end)
lib:Unhook("GuildFrame_LoadUI")
SetGuildRosterShowOffline(true)
end, true)
ForceShowOffline()
frame:SetScript("OnUpdate", Frame_OnUpdate)
GuildRoster()