Repository navigation
Expand file tree
/
Copy pathProfiles.lua
More file actions
350 lines (323 loc) · 12.9 KB
/
Copy pathProfiles.lua
File metadata and controls
350 lines (323 loc) · 12.9 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
342
343
344
345
346
347
348
349
350
-- Named pet configurations and portable import/export for PETAL.
--
-- Saved sets deliberately contain only the selected species and their five
-- situation assignments. UI preferences, keybindings, timers, and summon
-- history remain global and are never replaced by loading a set.
local APS = _G.PETAL
if not APS then return end
local FORMAT = "PETAL1"
-- PETAL1 is a portable file format. Its field order must never depend on the
-- shape or ordering of a mutable runtime table.
local FORMAT_TAG_IDS = { "city", "dungeon", "raid", "openworld", "neighborhood" }
local MAX_CODE_LENGTH = 100000
local MAX_SPECIES = 5000
local MAX_NAME_LENGTH = 32
local function cleanName(value)
if APS.IsSecret and APS:IsSecret(value) then return nil end
if type(value) ~= "string" then return nil end
value = value:gsub("^%s+", ""):gsub("%s+$", ""):gsub("[%c]", " ")
value = value:sub(1, MAX_NAME_LENGTH)
return value ~= "" and value or nil
end
local function sortedSpecies(source)
local seen, result = {}, {}
if type(source) == "table" then
for key, value in pairs(source) do
local candidate = (#source > 0) and value or (value and key or nil)
local speciesID = tonumber(candidate)
if speciesID and speciesID > 0 then
speciesID = math.floor(speciesID)
if not seen[speciesID] and #result < MAX_SPECIES then
seen[speciesID] = true
result[#result + 1] = speciesID
end
end
end
end
table.sort(result)
return result
end
local function listMap(list)
local result = {}
for _, value in ipairs(sortedSpecies(list)) do result[value] = true end
return result
end
local function copySnapshot(snapshot)
local result = { version = 1, selected = {}, tags = {} }
if type(snapshot) ~= "table" then return result end
result.selected = sortedSpecies(snapshot.selected)
for _, tag in ipairs(APS.CONDITION_TAGS or {}) do
result.tags[tag.id] = sortedSpecies(type(snapshot.tags) == "table" and snapshot.tags[tag.id] or nil)
end
return result
end
local function snapshotSignature(snapshot)
snapshot = copySnapshot(snapshot)
local parts = { table.concat(snapshot.selected, ",") }
for _, tag in ipairs(APS.CONDITION_TAGS or {}) do
parts[#parts + 1] = tag.id .. "=" .. table.concat(snapshot.tags[tag.id] or {}, ",")
end
return table.concat(parts, "|")
end
local function percentEncode(value)
return (tostring(value or ""):gsub("([^%w%-%._~])", function(char)
return string.format("%%%02X", string.byte(char))
end))
end
local function percentDecode(value)
if type(value) ~= "string" then return nil end
local cursor = 1
while true do
local position = value:find("%", cursor, true)
if not position then break end
if not value:sub(position + 1, position + 2):match("^%x%x$") then return nil end
cursor = position + 3
end
value = value:gsub("%%(%x%x)", function(hex)
local byte = tonumber(hex, 16)
return string.char(byte)
end)
return value
end
local function checksum(text)
local a, b = 1, 0
for index = 1, #text do
a = (a + string.byte(text, index)) % 65521
b = (b + a) % 65521
end
return string.format("%08X", b * 65536 + a)
end
local function normalizeImportCode(text)
-- WoW EditBoxes escape a literal pipe as two pipes when text is copied and
-- pasted between controls. Only collapse them when the format prefix proves
-- that the complete delimiter set was escaped; this preserves genuine
-- empty fields (which arrive as four pipes in an escaped code).
text = text:gsub("^\239\187\191", "") -- UTF-8 BOM
text = text:gsub("\226\128\139", "") -- zero-width space
text = text:gsub("%s", "")
if text:sub(1, #FORMAT + 2) == FORMAT .. "||" then
text = text:gsub("||", "|")
end
return text
end
local function splitExact(text, separator)
local result, start = {}, 1
while true do
local position = string.find(text, separator, start, true)
if not position then
result[#result + 1] = string.sub(text, start)
return result
end
result[#result + 1] = string.sub(text, start, position - 1)
start = position + #separator
end
end
local function parseSpeciesList(text)
if text == "" then return {} end
local result, seen = {}, {}
for _, token in ipairs(splitExact(text, ",")) do
if token == "" or not token:match("^%d+$") then return nil end
local speciesID = tonumber(token)
if not speciesID or speciesID <= 0 or speciesID > 2147483647 then return nil end
speciesID = math.floor(speciesID)
if not seen[speciesID] then
if #result >= MAX_SPECIES then return nil end
seen[speciesID] = true
result[#result + 1] = speciesID
end
end
table.sort(result)
return result
end
local function ownedSpeciesMap()
local result = {}
local journal = _G.C_PetJournal
if not journal then return result end
local function add(petID, speciesID)
if petID == nil or speciesID == nil then return end
if APS.IsSecret and (APS:IsSecret(petID) or APS:IsSecret(speciesID)) then return end
speciesID = tonumber(speciesID)
if speciesID and speciesID > 0 and result[math.floor(speciesID)] == nil then
result[math.floor(speciesID)] = petID
end
end
if type(journal.GetOwnedPetIDs) == "function" and type(journal.GetPetInfoTableByPetID) == "function" then
local ok, petIDs = pcall(journal.GetOwnedPetIDs)
if ok and type(petIDs) == "table" then
for index = 1, math.min(#petIDs, 50000) do
local petID = petIDs[index]
local infoOK, info = pcall(journal.GetPetInfoTableByPetID, petID)
if infoOK and type(info) == "table" then add(petID, info.speciesID) end
end
end
end
if next(result) == nil and type(journal.GetNumPets) == "function" and type(journal.GetPetInfoByIndex) == "function" then
local ok, count = pcall(journal.GetNumPets)
count = ok and tonumber(count) or 0
for index = 1, math.min(count or 0, 50000) do
local infoOK, petID, speciesID, owned = pcall(journal.GetPetInfoByIndex, index)
if infoOK and owned == true then add(petID, speciesID) end
end
end
return result
end
local function ensureStore(self)
local db = self:GetDB()
if type(db.savedPetSets) ~= "table" then db.savedPetSets = {} end
if type(db.activePetSetName) ~= "string" or db.savedPetSets[db.activePetSetName] == nil then
db.activePetSetName = nil
end
return db
end
function APS:CapturePetSet()
local snapshot = { version = 1, selected = {}, tags = {} }
local selectedBySpecies = {}
for _, tag in ipairs(self.CONDITION_TAGS or {}) do snapshot.tags[tag.id] = {} end
local roster = self:GetSelectedRoster()
for petID, enabled in pairs(roster or {}) do
if enabled then
local speciesID = self:GetPetSpeciesID(petID)
if speciesID then
speciesID = math.floor(speciesID)
selectedBySpecies[speciesID] = true
local petTags = self:GetPetTags(petID)
for _, tag in ipairs(self.CONDITION_TAGS or {}) do
if petTags[tag.id] == true then snapshot.tags[tag.id][speciesID] = true end
end
end
end
end
snapshot.selected = sortedSpecies(selectedBySpecies)
for _, tag in ipairs(self.CONDITION_TAGS or {}) do
snapshot.tags[tag.id] = sortedSpecies(snapshot.tags[tag.id])
end
return snapshot
end
function APS:GetPetSetStore()
local db = ensureStore(self)
return db.savedPetSets, db.activePetSetName
end
function APS:GetPetSetNames()
local sets, active = self:GetPetSetStore()
local names = {}
for name, snapshot in pairs(sets) do
if cleanName(name) and type(snapshot) == "table" then names[#names + 1] = name end
end
table.sort(names, function(left, right)
if left == active then return true end
if right == active then return false end
return string.lower(left) < string.lower(right)
end)
return names
end
function APS:HasUnsavedPetSetChanges()
local sets, active = self:GetPetSetStore()
if not active or type(sets[active]) ~= "table" then return false end
return snapshotSignature(self:CapturePetSet()) ~= snapshotSignature(sets[active])
end
function APS:SavePetSet(name)
name = cleanName(name)
if not name then return false, "name required" end
local db = ensureStore(self)
if db.savedPetSets[name] ~= nil and db.activePetSetName ~= name then
return false, "exists"
end
db.savedPetSets[name] = self:CapturePetSet()
db.activePetSetName = name
return true, name
end
function APS:SaveActivePetSet()
local db = ensureStore(self)
if not db.activePetSetName then return false, "no active set" end
return self:SavePetSet(db.activePetSetName)
end
function APS:LoadPetSet(name)
name = cleanName(name)
local db = ensureStore(self)
local source = name and db.savedPetSets[name]
if type(source) ~= "table" then return false, "set not found" end
local snapshot = copySnapshot(source)
local owned = ownedSpeciesMap()
local selected, missing = {}, 0
for _, speciesID in ipairs(snapshot.selected) do
local petID = owned[speciesID]
if petID then selected[petID] = true else missing = missing + 1 end
end
self:SetSelectedPets(selected)
for _, tag in ipairs(self.CONDITION_TAGS or {}) do
local tagged = {}
for _, speciesID in ipairs(snapshot.tags[tag.id] or {}) do
local petID = owned[speciesID]
if petID and selected[petID] then tagged[petID] = true end
end
self:SetRulePets(tag.ruleID, tagged)
end
db.activePetSetName = name
if self.RequestEvaluation then self:RequestEvaluation(0.1, "pet set loaded") end
return true, name, missing
end
function APS:DeletePetSet(name)
name = cleanName(name)
local db = ensureStore(self)
if not name or db.savedPetSets[name] == nil then return false, "set not found" end
if db.activePetSetName == name then return false, "active set" end
db.savedPetSets[name] = nil
return true
end
function APS:ExportPetSet(name)
name = cleanName(name)
local db = ensureStore(self)
local snapshot = name and db.savedPetSets[name]
if type(snapshot) ~= "table" then return nil, "set not found" end
snapshot = copySnapshot(snapshot)
local fields = { FORMAT, percentEncode(name), table.concat(snapshot.selected, ",") }
for _, tagID in ipairs(FORMAT_TAG_IDS) do
fields[#fields + 1] = table.concat(snapshot.tags[tagID] or {}, ",")
end
local payload = table.concat(fields, "|")
return payload .. "|" .. checksum(payload)
end
local function uniqueImportedName(sets, wanted)
if sets[wanted] == nil then return wanted end
local base = wanted:sub(1, MAX_NAME_LENGTH - 13)
for index = 2, 99 do
local candidate = (base .. " (Imported " .. index .. ")"):sub(1, MAX_NAME_LENGTH)
if sets[candidate] == nil then return candidate end
end
return nil
end
function APS:ImportPetSet(text)
if self.IsSecret and self:IsSecret(text) then return nil, "invalid code" end
if type(text) ~= "string" then return nil, "invalid code" end
text = normalizeImportCode(text)
if #text == 0 or #text > MAX_CODE_LENGTH then return nil, "invalid code" end
local fields = splitExact(text, "|")
local expectedFields = 4 + #FORMAT_TAG_IDS
if #fields ~= expectedFields or fields[1] ~= FORMAT then return nil, "wrong format" end
local suppliedChecksum = fields[#fields]
table.remove(fields)
local payload = table.concat(fields, "|")
if suppliedChecksum ~= checksum(payload) then return nil, "checksum failed" end
local name = cleanName(percentDecode(fields[2]) or "") or "Imported set"
local selected = parseSpeciesList(fields[3])
if not selected then return nil, "invalid pets" end
local snapshot = { version = 1, selected = selected, tags = {} }
for index, tagID in ipairs(FORMAT_TAG_IDS) do
local values = parseSpeciesList(fields[index + 3])
if not values then return nil, "invalid situations" end
local selectedMap = listMap(selected)
local filtered = {}
for _, speciesID in ipairs(values) do
if selectedMap[speciesID] then filtered[#filtered + 1] = speciesID end
end
snapshot.tags[tagID] = filtered
end
local db = ensureStore(self)
name = uniqueImportedName(db.savedPetSets, name)
if not name then return nil, "too many copies" end
-- Import is deliberately non-destructive: create a saved set and leave
-- the current working configuration untouched until the player clicks Use.
db.savedPetSets[name] = copySnapshot(snapshot)
return name
end
APS.PET_SET_FORMAT = FORMAT