-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
284 lines (229 loc) · 6.87 KB
/
main.lua
File metadata and controls
284 lines (229 loc) · 6.87 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
-- local Log = LibImplex_Logger()
-- ----------------------------------------------------------------------------
local lib = {}
lib.name = 'LibImplex'
lib.displayName = 'LibImplex'
local EVENT_NAMESPACE = 'LIBIMPLEX_EVEN_NAMESPACE'
local EVENT_BEFORE_UPDATE = 1
local EVENT_AFTER_UPDATE = 2
local EVENT_NAMES = {
[EVENT_BEFORE_UPDATE] = 'EVENT_BEFORE_UPDATE',
[EVENT_AFTER_UPDATE] = 'EVENT_AFTER_UPDATE',
}
lib.callbacks = {
[EVENT_BEFORE_UPDATE] = {},
[EVENT_AFTER_UPDATE] = {},
}
-- ----------------------------------------------------------------------------
local function calc_stats(tbl, length)
local total = 0
local max = 0
length = length or #tbl
for i = 1, length do
if tbl[i] > max then
max = tbl[i]
end
total = total + tbl[i]
tbl[i] = nil
end
return total / length, max
end
function lib:OnPlayerActivated(initial)
local updateVectors = LibImplex.UpdateVectors
local getUpdateableObjects = LibImplex.GetUpdateableObjects
local function updateMarkers()
self:FireCallbacks(EVENT_BEFORE_UPDATE)
updateVectors()
for object, _ in pairs(getUpdateableObjects()) do
object:Update()
end
self:FireCallbacks(EVENT_AFTER_UPDATE)
end
if self.sv.debugEnabled then
local updateArray = {}
local counter = 0
local function timeIt(func)
local function inner()
local repetitions = self.sv.repetitions
local start = GetGameTimeMilliseconds()
for _ = 1, repetitions do
func()
end
local finish = GetGameTimeMilliseconds()
counter = counter + 1
updateArray[counter] = (finish - start) / repetitions
end
return inner
end
updateMarkers = timeIt(updateMarkers)
EVENT_MANAGER:RegisterForUpdate(EVENT_NAMESPACE..'DebugWindow', 1000 * 1, function()
local repetitions = self.sv.repetitions
local avg, max = calc_stats(updateArray, counter)
local avgUnits, avgPrecision = 'ms', 3
local totalTime = avg * repetitions
local maxFps = 750 / avg
local stats = LibImplex.Context.GetContextStats()
local stats_table = {}
for context, counts in pairs(stats) do
stats_table[#stats_table+1] = ('%s: %d / %d'):format(context, counts[1], counts[2])
end
if avg < 0.1 then
avg = avg * 1000
avgUnits = 'us'
avgPrecision = 1
end
LibImplex_DebugWindowText2:SetText(
(
[[|c00EE00%dx|r repetition(s)
Objects per context (active/total):
%s
Avg: |c00EE00%.]] .. avgPrecision .. [[f %s|r (max: %.3f ms)
Total: %.3f ms
Max FPS: ~%d (low: ~%d)
Current FPS: ~%d (low: ~%d)]]
):format(
repetitions,
table.concat(stats_table, '\n'),
avg,
avgUnits,
max,
totalTime,maxFps,
750 / max,
750 / totalTime,
750 / (max * repetitions)
)
)
counter = 0
end)
end
EVENT_MANAGER:RegisterForUpdate(EVENT_NAMESPACE, 0, updateMarkers)
end
function lib:RegisterForEvent(namespace, event, callback)
if self.callbacks[event][namespace] then
error(('Event %s for %s already registered'):format(EVENT_NAMES[event], namespace))
end
self.callbacks[event][namespace] = callback
end
function lib:UnregisterForEvent(namespace, event)
self.callbacks[event][namespace] = nil
end
function lib:FireCallbacks(event)
for _, callback in pairs(self.callbacks[event]) do
callback() -- TODO: pcall
end
end
-- ----------------------------------------------------------------------------
function lib:AddSettings()
local LAM = LibAddonMenu2
if not LAM then return end
local panelName = self.name .. 'SettingsPanelControl'
local panelData = {
type = 'panel',
name = self.displayName,
author = '@imPDA',
}
local panel = LAM:RegisterAddonPanel(panelName, panelData)
local optionsData = {
{
type = 'checkbox',
name = 'Show on HUD scene only',
tooltip = 'All objects/markers will be displayed on HUD UI scene only and hidden on all other scenes (inventory, map, menu, etc.)',
getFunc = function() return self.sv.hudOnly end,
setFunc = function(value) self.sv.hudOnly = value end,
requiresReload = true,
}
}
LAM:RegisterOptionControls(panelName, optionsData)
end
function lib:AddDebugSettings()
local LAM = LibAddonMenu2
if not LAM then return end
local panelName = self.name .. 'DebugSettingsPanelControl'
local panelData = {
type = 'panel',
name = '[DEV] '..self.displayName,
author = '@imPDA',
}
local panel = LAM:RegisterAddonPanel(panelName, panelData)
local optionsData = {
{
type = 'checkbox',
name = 'Enable',
getFunc = function() return self.sv.debugEnabled end,
setFunc = function(value) self.sv.debugEnabled = value end,
requiresReload = true,
},
{
type = 'slider',
name = 'Repetitions',
getFunc = function() return self.sv.repetitions end,
setFunc = function(value)
self.sv.repetitions = value
end,
disabled = function() return not self.sv.debugEnabled end,
min = 1,
max = 300,
},
{
type = 'checkbox',
name = 'Show 3D Origin',
getFunc = function() return self.sv.showOrigin end,
setFunc = function(value)
self.sv.showOrigin = value
if value then
LibImplex_ShowOrigin()
else
LibImplex_HideOrigin()
end
end,
disabled = function() return not self.sv.debugEnabled end,
}
}
LAM:RegisterOptionControls(panelName, optionsData)
end
function lib:OnLoad()
local sv = ZO_SavedVars:NewAccountWide('LibImplexSavedVariables', 1, nil, {
debugEnabled = false,
debugMinimized = true,
debugAnchorOffsets = {128, 20},
repetitions = 1,
devMode = false,
hudOnly = true,
})
self.sv = sv
if sv.devMode then
SLASH_COMMANDS['/r'] = SLASH_COMMANDS['/reloadui']
self:AddDebugSettings()
if sv.debugEnabled then
LibImplex_ShowDebugWindow(self)
if sv.showOrigin then
LibImplex_ShowOrigin()
end
end
end
self:AddSettings()
-- TODO: do not update if scene is different (small impact )
if sv.hudOnly then
local canvasFargment = ZO_HUDFadeSceneFragment:New(IMP_LibImplex_Canvas)
HUD_SCENE:AddFragment(canvasFargment) -- TODO: HUD_UI_SCENE?
-- TODO: detect setting change on the fly
if GetSetting(SETTING_TYPE_GRAPHICS, GRAPHICS_SETTING_SUB_SAMPLING) ~= '2' then
local secondCanvasFargment = ZO_HUDFadeSceneFragment:New(IMP_LibImplex_SecondCanvas)
HUD_SCENE:AddFragment(secondCanvasFargment) -- TODO: HUD_UI_SCENE?
end
end
LibImplex.RegisterReticleOverEvents()
EVENT_MANAGER:RegisterForEvent(EVENT_NAMESPACE, EVENT_PLAYER_ACTIVATED, function(_, initial) self:OnPlayerActivated(initial) end)
end
EVENT_MANAGER:RegisterForEvent(EVENT_NAMESPACE, EVENT_ADD_ON_LOADED, function(_, addonName)
if addonName ~= lib.name then return end
EVENT_MANAGER:UnregisterForEvent(lib.name, EVENT_ADD_ON_LOADED)
lib:OnLoad()
end)
LibImplex = LibImplex or {}
LibImplex.EVENT_MANAGER = {
RegisterForEvent = function(...) lib:RegisterForEvent(...) end,
UnregisterForEvent = function(...) lib:UnregisterForEvent(...) end,
EVENT_BEFORE_UPDATE = EVENT_BEFORE_UPDATE,
EVENT_AFTER_UPDATE = EVENT_AFTER_UPDATE,
}