-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWA_SafeGuard.lua
More file actions
328 lines (286 loc) · 10.3 KB
/
Copy pathWA_SafeGuard.lua
File metadata and controls
328 lines (286 loc) · 10.3 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
WA_SafeGuardDB = WA_SafeGuardDB or {}
local P = "|cff00ccff[SafeGuard]|r "
----------------------------------------------------------------
-- Serializer: per-cxhunk processing to avoid large memory allocations
----------------------------------------------------------------
local B, N
local function w(v, seen, depth)
if depth > 80 then N = N + 1; B[N] = "nil"; return end
local t = type(v)
if t == "table" then
if seen[v] then N = N + 1; B[N] = "nil"; return end
seen[v] = true
N = N + 1; B[N] = "{"
for k, val in pairs(v) do
local kt = type(k)
local vt = type(val)
if (kt == "string" or kt == "number" or kt == "boolean")
and vt ~= "function" and vt ~= "userdata" and vt ~= "thread" then
N = N + 1; B[N] = "["
w(k, seen, depth + 1)
N = N + 1; B[N] = "]="
w(val, seen, depth + 1)
N = N + 1; B[N] = ","
end
end
N = N + 1; B[N] = "}"
seen[v] = nil
elseif t == "string" then
N = N + 1; B[N] = string.format("%q", v)
elseif t == "number" then
if v ~= v then N = N + 1; B[N] = "0"
elseif v == 1/0 then N = N + 1; B[N] = "1e308"
elseif v == -1/0 then N = N + 1; B[N] = "-1e308"
else N = N + 1; B[N] = tostring(v) end
elseif t == "boolean" then
N = N + 1; B[N] = v and "true" or "false"
else
N = N + 1; B[N] = "nil"
end
end
local function Serialize(tbl)
B = {}; N = 0
w(tbl, {}, 0)
local parts = {}
local pn = 0
local CHUNK = 4000
for i = 1, N, CHUNK do
local j = i + CHUNK - 1
if j > N then j = N end
pn = pn + 1
parts[pn] = table.concat(B, "", i, j)
end
B = nil
return table.concat(parts)
end
local function Deserialize(s)
if not s or #s < 2 then return nil end
local fn = loadstring("return " .. s)
if not fn then return nil end
setfenv(fn, {})
local ok, r = pcall(fn)
return ok and type(r) == "table" and r or nil
end
----------------------------------------------------------------
-- Helpers
----------------------------------------------------------------
local function HasBackup()
return type(WA_SafeGuardDB) == "table"
and type(WA_SafeGuardDB.displays) == "table"
and next(WA_SafeGuardDB.displays) ~= nil
end
local function IsBroken()
if type(WeakAurasSaved) ~= "table" then return true end
if type(WeakAurasSaved.displays) ~= "table" then return true end
if not next(WeakAurasSaved.displays) and HasBackup() then return true end
return false
end
local function NumAuras(t)
t = t or WeakAurasSaved
if type(t) ~= "table" or type(t.displays) ~= "table" then return 0 end
local c = 0
for _ in pairs(t.displays) do c = c + 1 end
return c
end
----------------------------------------------------------------
-- Backup
----------------------------------------------------------------
local function DoBackup(silent)
if IsBroken() then
if not silent then print(P .. "|cffff0000WA data broken, cannot backup|r") end
return false
end
local saved = WeakAurasSaved
local db = {
displays = {},
meta = {},
n = 0,
t = time(),
}
local count = 0
local errors = 0
for name, aura in pairs(saved.displays) do
local ok, s = pcall(Serialize, aura)
if ok and s and #s > 2 then
db.displays[name] = s
count = count + 1
else
errors = errors + 1
end
end
local skipKeys = {
displays = true, registered = true, tempIconCache = true,
iconCache = true, loaded = true, newFeaturesSeen = true,
}
for k, v in pairs(saved) do
if not skipKeys[k] then
local ok, s = pcall(Serialize, {v})
if ok and s then
db.meta[k] = s
end
end
end
db.n = count
WA_SafeGuardDB = db
if not silent then
local totalKB = 0
for _, s in pairs(db.displays) do totalKB = totalKB + #s end
for _, s in pairs(db.meta) do totalKB = totalKB + #s end
totalKB = math.floor(totalKB / 1024)
local msg = format("|cff00ff00Backup OK|r %d auras ~%dKB", count, totalKB)
if errors > 0 then
msg = msg .. format(" |cffffff00(%d skipped)|r", errors)
end
print(P .. msg)
end
return true
end
----------------------------------------------------------------
-- Restore
----------------------------------------------------------------
local function DoRestore()
if not HasBackup() then
print(P .. "|cffff0000No backup exists|r")
return false
end
local db = WA_SafeGuardDB
local restored = { displays = {} }
if type(db.meta) == "table" then
for k, s in pairs(db.meta) do
local wrapper = Deserialize(s)
if wrapper then
restored[k] = wrapper[1]
end
end
end
local count = 0
local errors = 0
local failedAuras = {}
local totalInBackup = 0
-- Count total auras in backup first
for _ in pairs(db.displays) do totalInBackup = totalInBackup + 1 end
for name, s in pairs(db.displays) do
local aura = Deserialize(s)
if aura then
restored.displays[name] = aura
count = count + 1
else
errors = errors + 1
failedAuras[errors] = name
end
end
if count == 0 then
print(P .. "|cffff0000All auras failed to deserialize|r")
return false
end
WeakAurasSaved = restored
print(P .. format("Backup contains: |cff00ccff%d|r auras", totalInBackup))
print(P .. format("|cff00ff00RESTORED %d auras!|r", count))
if errors > 0 then
print(P .. format("|cffff0000FAILED: %d auras could not be deserialized|r", errors))
local showCount = math.min(errors, 5)
for i = 1, showCount do
print(P .. format(" - %s", failedAuras[i]))
end
if errors > 5 then
print(P .. format(" ... and %d more", errors - 5))
end
end
return true
end
----------------------------------------------------------------
-- Status
----------------------------------------------------------------
local function ShowStatus()
print(P .. "=== Status ===")
if IsBroken() then
print(P .. "WeakAuras: |cffff0000CORRUPTED / EMPTY|r")
else
print(P .. format("WeakAuras: |cff00ff00OK|r (%d auras)", NumAuras()))
end
if HasBackup() then
local totalKB = 0
for _, s in pairs(WA_SafeGuardDB.displays) do totalKB = totalKB + #s end
totalKB = math.floor(totalKB / 1024)
local ago = math.floor((time() - (WA_SafeGuardDB.t or 0)) / 60)
print(P .. format("Backup: |cff00ff00OK|r %d auras ~%dKB %d min ago",
WA_SafeGuardDB.n or 0, totalKB, ago))
else
print(P .. "Backup: |cffff0000NONE|r (in-memory)")
print(P .. "|cffffff00Tip:|r Type |cff00ff00/wabak|r for .bak file recovery instructions.")
end
end
----------------------------------------------------------------
-- .bak File Recovery Guide
----------------------------------------------------------------
local function ShowBakRecoveryGuide()
print(P .. "=== .bak File Recovery ===")
print(P .. "When WoW force-disconnects, WA_SafeGuard.lua can be truncated.")
print(P .. "Your backup may still exist as |cff00ff00WA_SafeGuard.lua.bak|r on disk.")
print(P .. " ")
print(P .. "|cffffff00Steps:|r")
print(P .. " 1. |cffff0000Exit WoW completely|r (close ALL clients)")
print(P .. " 2. Go to: |cffaaaaaaWTF\\Account\\<ACCOUNT>\\SavedVariables\\|r")
print(P .. " 3. Check file sizes:")
print(P .. " - |cffff0000WA_SafeGuard.lua|r (truncated = ~1KB = bad)")
print(P .. " - |cff00ff00WA_SafeGuard.lua.bak|r (large = good backup)")
print(P .. " 4. Delete the truncated |cffff0000WA_SafeGuard.lua|r")
print(P .. " 5. Rename |cff00ff00WA_SafeGuard.lua.bak|r to |cff00ff00WA_SafeGuard.lua|r")
print(P .. " 6. Start WoW, log in, type |cff00ff00/warestore|r")
print(P .. " 7. Type |cff00ff00/reload|r to apply restored auras")
print(P .. " ")
print(P .. "|cffaaaaaaIf no .bak exists, you must reimport your auras manually.|r")
end
----------------------------------------------------------------
-- Boot & Auto-Restore
----------------------------------------------------------------
local frame = CreateFrame("Frame")
local booted = false
local initDelay = CreateFrame("Frame")
initDelay.elapsed = 0
initDelay:Hide()
initDelay:SetScript("OnUpdate", function(self, dt)
self.elapsed = self.elapsed + dt
if self.elapsed < 3 then return end
self:Hide()
self.elapsed = 0
if IsBroken() then
print(P .. "|cffff4400== WeakAuras data is CORRUPTED! ==")
if HasBackup() then
DoRestore()
print(P .. "|cffffff00Auras restored to SavedVariables. Type /reload to apply.|r")
else
print(P .. "|cffff0000No valid backup found in memory.|r")
print(P .. "Your backup may exist as |cff00ff00WA_SafeGuard.lua.bak|r on disk.")
print(P .. "Type |cff00ff00/wabak|r for recovery instructions.")
end
else
DoBackup(true) -- Silent backup on healthy login
end
end)
frame:RegisterEvent("PLAYER_LOGIN")
frame:RegisterEvent("PLAYER_LEAVING_WORLD")
frame:SetScript("OnEvent", function(self, event)
if event == "PLAYER_LOGIN" and not booted then
booted = true
initDelay:Show()
elseif event == "PLAYER_LEAVING_WORLD" then
-- Backup BEFORE logout saves WeakAuras data
if not IsBroken() then
pcall(DoBackup, true)
end
end
end)
----------------------------------------------------------------
-- Slash commands
----------------------------------------------------------------
SLASH_WASG1 = "/wasave"
SLASH_WASG2 = "/wabackup"
SlashCmdList.WASG = function() DoBackup() end
SLASH_WASGR1 = "/warestore"
SlashCmdList.WASGR = function() DoRestore() end
SLASH_WASGS1 = "/wastatus"
SlashCmdList.WASGS = function() ShowStatus() end
SLASH_WASGB1 = "/wabak"
SlashCmdList.WASGB = function() ShowBakRecoveryGuide() end
print(P .. "Loaded. Commands: /wastatus /wasave /warestore /wabak")