-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathVersionCheck.lua
More file actions
341 lines (280 loc) · 9.73 KB
/
VersionCheck.lua
File metadata and controls
341 lines (280 loc) · 9.73 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
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
--[[
Necrosis - Version Check Module
Checks for addon updates from GitHub releases
Copyright (C) - GPL v2
--]]
local VersionCheck = CreateFrame("Frame")
local L = LibStub("AceLocale-3.0"):GetLocale(NECROSIS_ID, true)
-- Configuration - Get version from Necrosis.Data (initialized in Initialize.lua)
local CONFIG = {
ADDON_NAME = "Necrosis",
CURRENT_VERSION = (Necrosis and Necrosis.Data and Necrosis.Data.Version) or "8.4.1",
GITHUB_REPO = "CKNOEP/Necrosis",
GITHUB_API_URL = "https://api.github.com/repos/CKNOEP/Necrosis/releases/latest",
CURSEFORGE_URL = "https://www.curseforge.com/wow/addons/necrosis",
GITHUB_URL = "https://github.com/CKNOEP/Necrosis",
CHECK_INTERVAL = 86400, -- 24 heures en secondes
}
-- State
VersionCheck.LastCheckTime = 0
VersionCheck.RemoteVersion = nil
VersionCheck.UpdateAvailable = false
VersionCheck.Enabled = true
----------------------------------------------
-- Version Comparison
----------------------------------------------
function VersionCheck:ParseVersion(versionString)
if not versionString then return nil end
-- Extract version numbers from string like "v8.3.0" or "8.3.0"
local major, minor, patch = versionString:match("v?(%d+)%.(%d+)%.(%d+)")
if not major then
return nil
end
return {
major = tonumber(major),
minor = tonumber(minor),
patch = tonumber(patch),
original = versionString,
}
end
function VersionCheck:CompareVersions(current, remote)
if not current or not remote then
return false, "Invalid version format"
end
local cur = self:ParseVersion(current)
local rem = self:ParseVersion(remote)
if not cur or not rem then
return false, "Could not parse versions"
end
-- Compare versions: remote > current ?
if rem.major > cur.major then
return true, rem.original
elseif rem.major == cur.major then
if rem.minor > cur.minor then
return true, rem.original
elseif rem.minor == cur.minor then
if rem.patch > cur.patch then
return true, rem.original
end
end
end
return false, nil
end
----------------------------------------------
-- HTTP Request (Fallback methods)
----------------------------------------------
function VersionCheck:FetchLatestVersion()
-- Try LibHTTP first (if available)
if LibStub then
local HttpLib = LibStub:GetLibrary("LibHTTP", true)
if HttpLib then
return self:FetchViaLibHTTP()
end
end
-- Fallback: No HTTP capability, will use local check
return nil
end
function VersionCheck:FetchViaLibHTTP()
-- This is a framework for LibHTTP if available
-- In practice, WoW Lua HTTP is limited, so we use fallback methods
return nil
end
function VersionCheck:ParseGitHubResponse(jsonString)
if not jsonString then
return nil
end
-- Simple JSON parsing for version tag
-- Looking for: "tag_name": "v8.3.0"
local version = jsonString:match('"tag_name"%s*:%s*"([^"]+)"')
if version then
return version
end
return nil
end
----------------------------------------------
-- Version Check Logic
----------------------------------------------
function VersionCheck:CheckForUpdate()
if not self.Enabled then
return
end
local currentTime = GetTime()
-- Check interval: only check every 24h (or on manual request)
if currentTime - self.LastCheckTime < CONFIG.CHECK_INTERVAL and self.RemoteVersion then
return self.UpdateAvailable
end
self.LastCheckTime = currentTime
-- Try to fetch remote version
local remoteVersion = self:FetchLatestVersion()
-- Fallback: If no HTTP available, notify user to check manually
if not remoteVersion then
self:NotifyManualCheck()
return false
end
self.RemoteVersion = remoteVersion
-- Compare versions
local updateAvailable, newVersion = self:CompareVersions(CONFIG.CURRENT_VERSION, remoteVersion)
self.UpdateAvailable = updateAvailable
if updateAvailable then
self:NotifyUpdate(newVersion)
end
return updateAvailable
end
----------------------------------------------
-- User Notifications
----------------------------------------------
function VersionCheck:NotifyUpdate(newVersion)
local message = format(
"|cff00ff00[%s]|r " .. (L["VERSION_UPDATE_AVAILABLE"] or "Update available"),
CONFIG.ADDON_NAME
)
print(message)
print(format("|cffff9900%s:|r %s → %s",
L["CURRENT_VERSION"] or "Current Version",
CONFIG.CURRENT_VERSION,
newVersion
))
print(format("|cff1e90ff%s:|r %s",
L["DOWNLOAD"] or "Download",
CONFIG.CURSEFORGE_URL
))
print(format("|cff1e90ff%s:|r %s",
L["GITHUB"] or "GitHub",
CONFIG.GITHUB_URL .. "/releases"
))
print(format("|cffaabb00%s|r /necrosis version",
L["TYPE"] or "Type"
))
-- Create popup notification (optional)
self:CreateUpdateNotificationFrame(newVersion)
end
function VersionCheck:NotifyManualCheck()
-- No HTTP available - suggest manual check
local message = format(
"|cff00ff00[%s]|r " .. (L["VERSION_CHECK_MANUAL"] or "Check for updates manually"),
CONFIG.ADDON_NAME
)
print(message)
print(format("|cff1e90ff%s|r: %s",
L["CURSEFORGE"] or "CurseForge",
CONFIG.CURSEFORGE_URL
))
print(format("|cff1e90ff%s|r: %s",
L["GITHUB"] or "GitHub",
CONFIG.GITHUB_URL
))
end
function VersionCheck:NotifyCurrentVersion()
print(format(
"|cff00ff00[%s]|r " .. (L["CURRENT_VERSION"] or "Version") .. ": |cffffffff%s|r",
CONFIG.ADDON_NAME,
CONFIG.CURRENT_VERSION
))
if self.RemoteVersion then
local updateAvailable = self:CompareVersions(CONFIG.CURRENT_VERSION, self.RemoteVersion)
if updateAvailable then
print(format("|cffff0000%s|r: %s",
L["UPDATE_AVAILABLE"] or "Update Available",
self.RemoteVersion
))
else
print(format("|cff00ff00%s|r",
L["UP_TO_DATE"] or "You are up to date!"
))
end
end
end
----------------------------------------------
-- Update Notification Frame
----------------------------------------------
function VersionCheck:CreateUpdateNotificationFrame(newVersion)
-- Create a simple notification frame (optional, can be disabled)
if _G["NecrosisUpdateNotification"] then
_G["NecrosisUpdateNotification"]:Hide()
end
local frame = CreateFrame("Frame", "NecrosisUpdateNotification", UIParent)
frame:SetSize(400, 120)
frame:SetPoint("TOPLEFT", UIParent, "TOPLEFT", 20, -80)
frame:SetFrameStrata("DIALOG")
-- Backdrop
frame:SetBackdrop({
bgFile = "Interface/Tooltips/UI-Tooltip-Background",
edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
tile = true,
tileSize = 16,
edgeSize = 16,
insets = { left = 4, right = 4, top = 4, bottom = 4 }
})
frame:SetBackdropColor(0.1, 0.1, 0.15, 0.9)
frame:SetBackdropBorderColor(0.31, 0.31, 0.31)
-- Title
local title = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
title:SetPoint("TOPLEFT", frame, "TOPLEFT", 12, -12)
title:SetText("|cff00ff00" .. CONFIG.ADDON_NAME .. "|r " .. (L["UPDATE_AVAILABLE"] or "Update Available"))
-- Version info
local versionText = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
versionText:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -8)
versionText:SetText(format("|cffffaa00%s:|r %s → %s",
L["VERSION"] or "Version",
CONFIG.CURRENT_VERSION,
newVersion
))
-- Action button
local btn = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
btn:SetSize(80, 24)
btn:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -12, 12)
btn:SetText(L["UPDATE"] or "Update")
btn:SetScript("OnClick", function()
if IsAddOnLoaded("Curse") or IsAddOnLoaded("Overwolf") then
-- Open CurseForge client addon search
SlashCmdList["ADDONUPDATE"](CONFIG.ADDON_NAME)
else
-- Open browser
print("|cff1e90ff" .. CONFIG.CURSEFORGE_URL .. "|r")
end
frame:Hide()
end)
-- Close button
local closeBtn = CreateFrame("Button", nil, frame, "UIPanelCloseButton")
closeBtn:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -4, -4)
closeBtn:SetScript("OnClick", function() frame:Hide() end)
-- Auto-hide after 10 seconds
C_Timer.After(10, function()
if frame and frame:IsVisible() then
frame:Hide()
end
end)
frame:Show()
end
----------------------------------------------
-- Initialization
----------------------------------------------
function VersionCheck:Init()
self.Enabled = NecrosisConfig.VersionCheck and NecrosisConfig.VersionCheck.Enabled ~= false
if not self.Enabled then
return
end
-- Check for updates on addon load (with slight delay)
C_Timer.After(5, function()
self:CheckForUpdate()
end)
-- Register slash command for manual check
SLASH_NECROSISVERSION1 = "/necrosis version"
SlashCmdList["NECROSISVERSION"] = function()
VersionCheck:NotifyCurrentVersion()
VersionCheck:CheckForUpdate()
end
end
----------------------------------------------
-- Class Gate - Initialize for Warlocks
----------------------------------------------
-- Initialize version check and slash command for Warlocks
do
local _, playerClass = UnitClass("player")
if playerClass == "WARLOCK" then
-- Initialize with slight delay to ensure NecrosisConfig is loaded
C_Timer.After(1, function()
VersionCheck:Init()
end)
end
end