-
Notifications
You must be signed in to change notification settings - Fork 1
/
WagoAnalytics.lua
executable file
·356 lines (315 loc) · 10.1 KB
/
WagoAnalytics.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
--[[
Follow the instructions located at https://github.com/wagoio/WagoAnalyticsShim to use this.
Example Usage:
Options = {
breadcrumbCount = 10, -- Default: 20. Number of breadcrumbs to push with an error
reportErrors = true, -- Default: true. Should we report errors?
}
local WagoAnalytics = LibStub("WagoAnalytics"):Register("<Your Wago addon ID>") -- 2nd argument is an optional list of options
-- Add breadcrumb data with arg1 message
WagoAnalytics:Breadcrumb("Some useful debug information here.")
-- Increments the counter arg1 by arg2 amount
WagoAnalytics:Counter("SomeCounter", 50)
-- Set a boolean arg1 value to arg2 or true
WagoAnalytics:Switch("SomeSwitch")
-- Throw a custom error message arg1. This includes the previous breadcrumbs automatically.
WagoAnalytics:Error("Variable was expected to be defined, but wasn't")
--]]
local _, addon = ...
WagoAnalytics = {}
local WagoAnalytics = WagoAnalytics
local SV, playerClass, playerRegion, playerMinLevel, playerMaxLevel, playerRace, playerFaction, playerLocale, playerName, playerRealm
local registeredAddons, playerSpecs, playerAddons, variableCount = {}, {}, {}, {}
local isRetail = WOW_PROJECT_ID == (WOW_PROJECT_MAINLINE or 1)
do
local tostring, pairs, ipairs, debugstack, debuglocals, tIndexOf, tinsert, match =
tostring, pairs, ipairs, debugstack, debuglocals, tIndexOf, table.insert, string.match
local GetLocale, UnitFactionGroup, GetCurrentRegion, UnitAffectingCombat, InCombatLockdown, CreateFrame, UnitClass, UnitLevel, UnitRace, GetSpecialization, GetSpecializationInfo =
GetLocale, UnitFactionGroup, GetCurrentRegion, UnitAffectingCombat, InCombatLockdown, CreateFrame, UnitClass, UnitLevel, UnitRace, GetSpecialization, GetSpecializationInfo
local GetAddOnMetadata = C_AddOns and C_AddOns.GetAddOnMetadata or GetAddOnMetadata
local GetAddOnInfo = C_AddOns and C_AddOns.GetAddOnInfo or GetAddOnInfo
local GetNumAddOns = C_AddOns and C_AddOns.GetNumAddOns or GetNumAddOns
-- isSimple 3 state: True is simple, False is pcall, Nil is not simple
local function handleError(errorMessage, isSimple, errorObj)
errorMessage = tostring(errorMessage)
local ok, wagoID = pcall(GetAddOnMetadata, match(errorMessage, "AddOns[\\/]([^\\/]+)[\\/]") or "Unknown", "X-Wago-ID")
if not ok or not wagoID or not registeredAddons[wagoID] then
return
end
local addonObj = registeredAddons[wagoID]
for _, err in ipairs(addonObj.errors) do
if err.message and err.message == errorMessage then
return
end
end
if isSimple then
addonObj:Error({
message = errorMessage
})
else
addonObj:Error({
message = errorMessage,
stack = errorObj and errorObj.stack or debugstack(3),
locals = errorObj and errorObj.locals or (InCombatLockdown() or UnitAffectingCombat("player")) and "Skipped (In Encounter)" or debuglocals(isSimple == nil and 3 or 5)
})
end
end
do
local oldErrorHandler = _G.geterrorhandler()
_G.seterrorhandler(function(err)
local ok, innerError = pcall(handleError, err, false)
oldErrorHandler(err)
if not ok then
oldErrorHandler(innerError)
end
end)
end
local frame = CreateFrame("Frame")
frame:RegisterEvent("PLAYER_LOGIN")
frame:RegisterEvent("PLAYER_LEVEL_UP")
frame:RegisterEvent("ADDON_LOADED")
frame:RegisterEvent("ADDON_ACTION_BLOCKED")
frame:RegisterEvent("ADDON_ACTION_FORBIDDEN")
frame:RegisterEvent("LUA_WARNING")
if isRetail then
frame:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED")
end
frame:SetScript("OnEvent", function(_, event, arg1, arg2)
-- Handles when the addon loads
if event == "PLAYER_LOGIN" then
if not WagoAnalyticsSV then
WagoAnalyticsSV = {}
end
local _, _, _playerClass = UnitClass("player")
playerClass = _playerClass
if isRetail then
local currentSpec = GetSpecialization()
if currentSpec then
local playerSpec = GetSpecializationInfo(currentSpec)
tinsert(playerSpecs, playerSpec)
end
end
local _, _, _playerRace = UnitRace("player")
playerRace = _playerRace
playerFaction = UnitFactionGroup("player") or "Neutral"
playerMinLevel = UnitLevel("player")
playerMaxLevel = playerMinLevel
playerLocale = GetLocale()
playerRegion = GetCurrentRegion()
playerName = UnitName("player")
playerRealm = GetRealmName()
for i = 1, GetNumAddOns() do
local name, _, _, enabled = GetAddOnInfo(i)
if enabled then
playerAddons[name] = GetAddOnMetadata(i, "Version") or "Unknown"
end
end
-- Hooks into BugSack
local BugGrabber = _G["BugGrabber"]
if BugGrabber and BugGrabber.RegisterCallback then
BugGrabber.RegisterCallback(addon, "BugGrabber_BugGrabbed", function(_, error)
handleError(error.message, error.stack and true or nil, error)
end)
end
frame:RegisterEvent("ADDONS_UNLOADING")
-- Handles when the player changes their specialization
elseif event == "PLAYER_SPECIALIZATION_CHANGED" then
local currentSpec = GetSpecialization()
if currentSpec then
local playerSpec = GetSpecializationInfo(currentSpec)
if not tIndexOf(playerSpecs, playerSpec) then
tinsert(playerSpecs, playerSpec)
end
end
-- Handles when the player levels up
elseif event == "PLAYER_LEVEL_UP" then
playerMaxLevel = arg1
-- Handles when an addon is loaded
elseif event == "ADDON_LOADED" then
playerAddons[arg1] = GetAddOnMetadata(arg1, "Version") or "Unknown"
-- Handles when an addon fires a bad action (protected or forbidden)
elseif event == "ADDON_ACTION_BLOCKED" or event == "ADDON_ACTION_FORBIDDEN" then
handleError(("[%s] AddOn '%s' tried to call the protected function '%s'."):format(event, arg1 or "<name>", arg2 or "<func>"))
-- Handles when an addon fires bad Lua code
elseif event == "LUA_WARNING" then
handleError(arg2, true)
-- Handles when the player closes the game or logs out
elseif event == "ADDONS_UNLOADING" then
for _, addonObj in pairs(registeredAddons) do
addonObj:Save()
end
end
end)
end
-- Start Utility functions
local CollectBufferElements
do
local tinsert = table.insert
function CollectBufferElements(buffer)
local elements = {}
for i = buffer:GetNumElements(), 1, -1 do
tinsert(elements, buffer:GetEntryAtIndex(i))
end
return elements
end
end
-- End utility functions
local wagoPrototype = {}
function wagoPrototype:IncrementCounter(name, increment)
return self:SetCounter(name, (self.counters[name] or 0) + (increment or 1))
end
function wagoPrototype:DecrementCounter(name, decrement)
return self:SetCounter(name, (self.counters[name] or 0) - (decrement or 1))
end
function wagoPrototype:SetCounter(name, value)
if type(name) ~= "string" then
return false
end
if #name > 128 then
name = name:sub(0, 128)
end
if not self.counters[name] then
local elemLen = variableCount[self.addon].counters
if elemLen > 512 then
return false
end
variableCount[self.addon].counters = elemLen + 1
end
self.counters[name] = value
end
function wagoPrototype:Switch(name, value)
value = value == nil and true or value
if type(name) ~= "string" or type(value) ~= "boolean" then
return false
end
if #name > 128 then
name = name:sub(0, 128)
end
local elemLen = variableCount[self.addon].switches
if elemLen > 512 then
return false
end
variableCount[self.addon].switches = elemLen + 1
self.switches[name] = value
end
do
local tinsert = table.insert
function wagoPrototype:Error(error)
if type(error) == "string" then
return self:Error({ message = error })
end
if type(error) ~= "table" then
return false
end
if #self.errors > 512 then
return false
end
-- Errors must contain at least a message
if not error.message then
return false
end
if #error.message > 1024 then
error.message = error.message:sub(0, 1021) .. "..."
end
tinsert(self.errors, {
error = error,
breadcrumb = CollectBufferElements(self.breadcrumbs)
})
end
end
do
local type = type
function wagoPrototype:Breadcrumb(data)
if type(data) ~= "string" then
return false
end
if #data > 255 then
data = data:sub(0, 252) .. "..."
end
self.breadcrumbs:PushFront(data)
end
end
do
local gsub, format, random, time, pairs, next = string.gsub, string.format, math.random, time, pairs, next
local function StripExcessAnalyticsEntries()
local count, lastTime, lastK = 0, math.huge
for k, v in pairs(WagoAnalyticsSV) do
count = count + 1
if v.time < lastTime then
lastK = k
lastTime = v.time
end
if count > 2 then
WagoAnalyticsSV[lastK] = nil
break
end
end
end
function wagoPrototype:Save()
if not SV then
SV = {
data = {},
time = time(),
addons = playerAddons,
playerData = {
name = playerName,
realm = playerRealm,
locale = playerLocale,
class = playerClass,
region = playerRegion,
specs = playerSpecs,
levelMin = playerMinLevel,
levelMax = playerMaxLevel,
race = playerRace,
faction = playerFaction
}
}
end
-- Prevent saving addon data if there's no analytics
if variableCount[self.addon].counters > 0 or variableCount[self.addon].switches > 0 or #self.errors > 0 then
SV.data[self.addon] = {
counters = self.counters,
switches = self.switches,
errors = self.errors
}
end
if not next(SV.data) then
return
end
local uuid = gsub("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "x", function()
return format("%x", random(0, 0xf))
end)
WagoAnalyticsSV[uuid] = SV
StripExcessAnalyticsEntries()
end
end
do
local CreateCircularBuffer, mmin, setmetatable = CreateCircularBuffer, math.min, setmetatable
function WagoAnalytics:Register(addonName, options)
if registeredAddons[addonName] then
return registeredAddons[addonName]
end
if not options then
options = {}
end
if options.reportErrors == nil then
options.reportErrors = true
end
local obj = setmetatable({
addon = addonName,
options = options,
counters = {},
switches = {},
errors = {},
breadcrumbs = CreateCircularBuffer(mmin(options.breadcrumbCount or 20, 50))
}, {
__index = wagoPrototype
})
registeredAddons[addonName] = obj
variableCount[addonName] = {
counters = 0,
switches = 0
}
return obj
end
end