-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSFUtils_VersionChecker.lua
More file actions
242 lines (214 loc) · 6.96 KB
/
SFUtils_VersionChecker.lua
File metadata and controls
242 lines (214 loc) · 6.96 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
-- LibSFUtils is already defined in prior loaded file
LibSFUtils = LibSFUtils or {}
local sfutil = LibSFUtils
--[[
An implementation of a logger which does nothing.
--]]
local nilLibDebug = {
Error = function(self,...) end,
Warn = function(self,...) end,
Info = function(self,...) end,
Debug = function(self,...) end,
}
setmetatable(nilLibDebug, { __call = function(self, name)
self.addonName = name
return self
end
})
--[[
An implementation of a logger which uses the lua print function
to output the messages.
--]]
local printLibDebug = {
Error = function(self,...) print("ERROR: "..string.format(...)) end,
Warn = function(self,...) print("WARN: "..string.format(...)) end,
Info = function(self,...) print("INFO: "..string.format(...)) end,
Debug = function(self,...) print("DEBUG: "..string.format(...)) end,
}
setmetatable(printLibDebug, getmetatable(nilLibDebug))
-- -----------------------------------------------------------------------
-- Object for checking minimum version of libraries is met.
-- Use of this requires a logger such as the LibDebugLogger addon,
-- or you can write your own that implements <logger>:Error(), <logger>:Warn(),
-- and <logger>:Info().
--
local VC = {}
setmetatable(VC, { __call = function(self, name, plogger) return VC:New(name) end
})
function VC:New(addonName, logger)
local o = {}
setmetatable(o, self)
self.__index = self
local mt = getmetatable(o)
mt.__index = self
o.addonName = addonName
o.enabled = true
o.logger = logger
if not logger then
o.logger = nilLibDebug
end
return o
end
--[[
Add a logger to the VersionChecker instance and enable logging.
(If a logger is not specified, then default to the printLibDebug logger.)
Note: The LibDebugLogger is a drop-in replacement for the other loggers we define here.
We simply require an object that implements Error, Warn, Info, and Debug object methods
for us to use as a logger.
--]]
function VC:Enable(plogger)
self.enabled = true
if not plogger then
if self.logger == nilLibDebug then
self.logger = printLibDebug
end
else
self.logger = plogger
end
end
--[[
Disable logging in the VersionChecker instance
--]]
function VC:Disable()
self.enabled = false
end
--[[
*local function
Load in information about all loaded addons to a table indexed by addon Name.
The table also has a "count" entry containing the number of all of the addons in the table.
The table is Global to the game; so once it is loaded it is available for any addon to use.
--]]
LibSFUtils.addonlist = { count=0 }
local function loadAddonList()
local addonlist = LibSFUtils.addonlist
--if addonlist.count > 0 then return end
local AddOnManager = GetAddOnManager()
local currAddons = AddOnManager:GetNumAddOns()
if addonlist.count >= currAddons then return end
for i = 1, currAddons do
local name, title, author, description, enabled, state,
isOutOfDate, isLibrary = AddOnManager:GetAddOnInfo(i)
if addonlist[name] == nil then
addonlist[name] = {
index=i,
enabled=enabled,
state=state,
isOutOfDate=isOutOfDate,
isLibrary=isLibrary
}
addonlist.count=addonlist.count+1
local version = AddOnManager.GetAddOnVersion
and AddOnManager:GetAddOnVersion(i) or 0
addonlist[name].version = version
end
end
end
--[[
Force load in information about all loaded addons to a table indexed by addon Name.
Previous information that might have been in the addon table will be discarded.
The table also has a "count" entry containing the number of all of the addons in the table.
The table is Global to the game; so once it is loaded it is available for any addon to use.
Note: This is a LibSFUtils function - not a VersionChecker one.
--]]
function LibSFUtils.ForceUpdateAddons()
local addonlist = { count=0 }
local AddOnManager = GetAddOnManager()
local currAddons = AddOnManager:GetNumAddOns()
for i = 1, currAddons do
local name, title, author, description, enabled, state,
isOutOfDate, isLibrary = AddOnManager:GetAddOnInfo(i)
addonlist[name] = {
index=i,
enabled=enabled,
state=state,
isOutOfDate=isOutOfDate,
isLibrary=isLibrary
}
addonlist.count=addonlist.count+1
local version = AddOnManager.GetAddOnVersion
and AddOnManager:GetAddOnVersion(i) or 0
addonlist[name].version = version
end
LibSFUtils.addonlist = addonlist
end
--[[
Get all the information about an addon from the name.
Returns the following fields: name, title, author, description,
enabled, state, isOutOfDate, isLibrary
or
nil if addon is not found.
This does another api call since not all the information is cached.
--]]
function LibSFUtils.GetAddonInfo(libname)
if LibSFUtils.addonlist.count == 0 then
loadAddonList()
end
local AddOnManager = GetAddOnManager()
if LibSFUtils.addonlist[libname] then
local indx = LibSFUtils.addonlist[libname].index
if indx then
return LibSFUtils.addonlist[libname]
--return AddOnManager:GetAddOnInfo(indx)
else
return
end
end
return
end
--[[
Get the index of an addon from the name.
--]]
function LibSFUtils.GetAddonIndex(libname)
if LibSFUtils.addonlist.count == 0 then
loadAddonList()
end
if LibSFUtils.addonlist[libname] then
return LibSFUtils.addonlist[libname].index
end
return -1
end
--[[
Get the version of an addon from the name.
--]]
function LibSFUtils.GetAddonVersion(name)
if LibSFUtils.addonlist.count == 0 then
loadAddonList()
end
if LibSFUtils.addonlist[name] then
return LibSFUtils.addonlist[name].version
end
return -1
end
--[[
Check the internal addon table for the addon named and compare the
loaded version to the expected one.
This requires a logger being specified for the VersionChecker instance.
It also depends on LibSFUtils functions.
Note: This is a VersionChecker function.
--]]
function VC:CheckVersion(libname, expectedVersion)
if not self.enabled then return end
if not libname then return end
if not expectedVersion then expectedVersion = 9999999 end
local logger = self.logger
local version = LibSFUtils.GetAddonVersion(libname)
if version < 0 then
logger:Warn("Missing required Library \"%s\" ", libname)
return
end
if version == 0 then
logger:Warn("Library \"%s\" does not provide version information", libname)
return
end
if version < expectedVersion then
logger:Error("Outdated version of \"%s\" detected (%d) - expected version %d - possibly embedded in another older addon.", libname, version or -1, expectedVersion)
end
end
--[[
Write a message to the logger that the named addon does not provide version information.
--]]
function VC:NoVersion(libname)
if not self.enabled then return end
self.logger:Info("Library \"%s\" does not provide version information", libname)
end
LibSFUtils.VersionChecker = VC