Repository navigation
Expand file tree
/
Copy pathProfiles.lua
More file actions
333 lines (302 loc) · 11.9 KB
/
Copy pathProfiles.lua
File metadata and controls
333 lines (302 loc) · 11.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
-- Perfiles: guardar, cambiar, copiar y compartir la configuración por código.
--
-- ModiToolsDB = {
-- global = { window, minimap, language }, -- común a todos los perfiles
-- profiles = { [nombre] = { yards = {...}, focus = {...}, ... } },
-- chars = { ["Personaje-Reino"] = nombre }, -- perfil activo de cada personaje
-- }
-- ns.db apunta siempre al perfil activo del personaje.
local _, ns = ...
local L = ns.L
local DEFAULT_PROFILE = "Default"
local CODE_PREFIX = "MT1:"
local MAX_NAME = 24
local function charKey()
return (UnitName("player") or "?") .. "-" .. (GetRealmName() or "?")
end
-- Las vistas previas (desbloqueado) nunca se conservan: ni al reloguear ni al cambiar de perfil.
local function ClearPreviews(profile)
for key in pairs(ns.defaults) do
if type(profile[key]) == "table" then profile[key].unlocked = false end
end
end
local function CleanName(name)
name = tostring(name or ""):gsub("[%c]", ""):gsub("%s+", " "):match("^%s*(.-)%s*$")
return name:sub(1, MAX_NAME)
end
local function UniqueName(name)
local profiles = ns.root.profiles
if not profiles[name] then return name end
local i = 2
while profiles[name:sub(1, MAX_NAME - 4) .. " (" .. i .. ")"] do i = i + 1 end
return name:sub(1, MAX_NAME - 4) .. " (" .. i .. ")"
end
---------------------------------------------------------------------------
-- Inicio y cambio de perfil
---------------------------------------------------------------------------
function ns.InitProfiles()
ns.freshInstall = ModiToolsDB == nil or next(ModiToolsDB) == nil -- primera vez que se usa el addon
ModiToolsDB = ModiToolsDB or {}
local root = ModiToolsDB
-- migración desde la versión sin perfiles (todo estaba en la raíz)
if not root.profiles then
local legacy = {}
for key in pairs(ns.defaults) do
legacy[key] = root[key]
root[key] = nil
end
root.global = root.global or {}
for _, k in ipairs({ "window", "minimap", "language" }) do
root.global[k] = root[k]
root[k] = nil
end
root.profiles = { [DEFAULT_PROFILE] = legacy }
end
root.global = root.global or {}
root.chars = root.chars or {}
ns.root = root
ns.global = root.global
ns.MergeDefaults(ns.global, ns.globalDefaults)
local name = root.chars[charKey()] or DEFAULT_PROFILE
if not root.profiles[name] then name = DEFAULT_PROFILE end
root.profiles[name] = root.profiles[name] or {}
root.chars[charKey()] = name
ns.profileName = name
ns.db = root.profiles[name]
ns.MergeDefaults(ns.db, ns.defaults)
ClearPreviews(ns.db)
end
function ns.ListProfiles()
local names = {}
for name in pairs(ns.root.profiles) do names[#names + 1] = name end
table.sort(names, function(a, b) return a:lower() < b:lower() end)
return names
end
function ns.SetProfile(name)
local root = ns.root
if not root.profiles[name] then return false, L["Profile not found."] end
root.chars[charKey()] = name
ns.profileName = name
ns.db = root.profiles[name]
ns.MergeDefaults(ns.db, ns.defaults)
ClearPreviews(ns.db)
for _, key in ipairs(ns.order) do ns.modules[key].Apply() end
if ns.RefreshOptions then ns.RefreshOptions() end
return true
end
-- Crea un perfil como copia del actual (o de `source`) y lo activa.
function ns.CreateProfile(name, source)
name = CleanName(name)
if name == "" then return false, L["Type a name first."] end
if ns.root.profiles[name] then return false, L["A profile with that name already exists."] end
local from = ns.root.profiles[source or ns.profileName]
ns.root.profiles[name] = ns.DeepCopy(from or {})
ns.SetProfile(name)
return true, name
end
function ns.RenameProfile(newName)
newName = CleanName(newName)
if newName == "" then return false, L["Type a name first."] end
if ns.root.profiles[newName] then return false, L["A profile with that name already exists."] end
local old = ns.profileName
ns.root.profiles[newName] = ns.root.profiles[old]
ns.root.profiles[old] = nil
for char, p in pairs(ns.root.chars) do
if p == old then ns.root.chars[char] = newName end
end
ns.profileName = newName
if ns.RefreshOptions then ns.RefreshOptions() end
return true, newName
end
function ns.DeleteProfile(name)
local profiles = ns.root.profiles
if not profiles[name] then return false, L["Profile not found."] end
local count = 0
for _ in pairs(profiles) do count = count + 1 end
if count <= 1 then return false, L["You cannot delete the only profile."] end
profiles[name] = nil
if ns.profileName == name then
local fallback = profiles[DEFAULT_PROFILE] and DEFAULT_PROFILE or ns.ListProfiles()[1]
ns.SetProfile(fallback)
elseif ns.RefreshOptions then
ns.RefreshOptions()
end
return true
end
function ns.ResetProfile()
ns.root.profiles[ns.profileName] = {}
ns.SetProfile(ns.profileName)
end
---------------------------------------------------------------------------
-- Serialización (formato propio, sin loadstring: el código importado nunca se ejecuta)
---------------------------------------------------------------------------
local function Serialize(value, out)
local t = type(value)
if t == "number" then
out[#out + 1] = "n" .. string.format("%.10g", value) .. ";"
elseif t == "boolean" then
out[#out + 1] = value and "T" or "F"
elseif t == "string" then
out[#out + 1] = "s" .. #value .. ":" .. value
elseif t == "table" then
out[#out + 1] = "{"
for k, v in pairs(value) do
Serialize(k, out)
Serialize(v, out)
end
out[#out + 1] = "}"
end
end
local function Deserialize(str)
local pos = 1
local function parse(depth)
if depth > 8 then error("depth") end
local c = str:sub(pos, pos)
pos = pos + 1
if c == "n" then
local e = str:find(";", pos, true)
local num = e and tonumber(str:sub(pos, e - 1))
if not num then error("number") end
pos = e + 1
return num
elseif c == "T" then
return true
elseif c == "F" then
return false
elseif c == "s" then
local e = str:find(":", pos, true)
local len = e and tonumber(str:sub(pos, e - 1))
if not len or len < 0 or len > 5000 then error("length") end
local s = str:sub(e + 1, e + len)
if #s ~= len then error("truncated") end
pos = e + len + 1
return s
elseif c == "{" then
local t = {}
while str:sub(pos, pos) ~= "}" do
if pos > #str then error("eof") end
local k = parse(depth + 1)
local v = parse(depth + 1)
if type(k) ~= "string" and type(k) ~= "number" then error("key") end
t[k] = v
end
pos = pos + 1
return t
end
error("token")
end
return parse(0)
end
local B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
local B64_INDEX = {}
for i = 1, #B64 do B64_INDEX[B64:sub(i, i)] = i - 1 end
local function Base64Encode(data)
local out = {}
for i = 1, #data, 3 do
local a, b, c = data:byte(i, i + 2)
local n = a * 65536 + (b or 0) * 256 + (c or 0)
local c1 = math.floor(n / 262144) % 64
local c2 = math.floor(n / 4096) % 64
local c3 = math.floor(n / 64) % 64
local c4 = n % 64
out[#out + 1] = B64:sub(c1 + 1, c1 + 1) .. B64:sub(c2 + 1, c2 + 1)
.. (b and B64:sub(c3 + 1, c3 + 1) or "=") .. (c and B64:sub(c4 + 1, c4 + 1) or "=")
end
return table.concat(out)
end
local function Base64Decode(text)
text = text:gsub("[^%w%+/=]", "")
local out = {}
for i = 1, #text, 4 do
local chunk = text:sub(i, i + 3)
if #chunk < 4 then error("length") end
local a, b = B64_INDEX[chunk:sub(1, 1)], B64_INDEX[chunk:sub(2, 2)]
local c, d = B64_INDEX[chunk:sub(3, 3)], B64_INDEX[chunk:sub(4, 4)]
if not a or not b then error("char") end
local n = a * 262144 + b * 4096 + (c or 0) * 64 + (d or 0)
out[#out + 1] = string.char(math.floor(n / 65536) % 256)
if c then out[#out + 1] = string.char(math.floor(n / 256) % 256) end
if d then out[#out + 1] = string.char(n % 256) end
end
return table.concat(out)
end
---------------------------------------------------------------------------
-- Exportar / importar
---------------------------------------------------------------------------
function ns.ExportProfile(name)
local data = ns.root.profiles[name or ns.profileName]
if not data then return nil, L["Profile not found."] end
local copy = ns.DeepCopy(data)
ClearPreviews(copy)
local out = {}
Serialize({ name = name or ns.profileName, data = copy }, out)
return CODE_PREFIX .. Base64Encode(table.concat(out))
end
-- Reconstruye un valor a partir de los valores por defecto: solo se aceptan campos conocidos
-- y del tipo correcto, así un código ajeno no puede colar datos inesperados.
local function Sanitize(value, default)
if type(default) == "table" then
local out = {}
for k, d in pairs(default) do
local v
if type(value) == "table" then v = value[k] end
out[k] = Sanitize(v, d)
end
-- tablas sin campos fijos (prepot.extra, timeline.spells: listas de spellIDs)
if next(default) == nil and type(value) == "table" then
for k, v in pairs(value) do
local isKey = type(k) == "number" or type(k) == "string"
if isKey and type(v) == "boolean" then
out[k] = v
elseif type(k) == "number" and type(v) == "number" and v ~= 0 and math.abs(v) < 10000000 then
out[k] = v -- listas de IDs (negativo = objeto)
elseif type(k) == "number" and type(v) == "table" then
-- ajustes por hechizo (p. ej. timeline.spellSounds): solo textos y números simples
local inner = {}
for kk, vv in pairs(v) do
if type(kk) == "string" and #kk <= 20 then
if type(vv) == "string" then
inner[kk] = vv:sub(1, 40)
elseif type(vv) == "number" and vv == vv and math.abs(vv) <= 10000 then
inner[kk] = vv
end
end
end
out[k] = inner
end
end
end
return out
end
if type(value) == type(default) then
if type(value) == "string" then return value:sub(1, 200) end
if type(value) == "number" and (value ~= value or math.abs(value) > 10000) then
return default
end
return value
end
return ns.DeepCopy(default)
end
-- Devuelve el nombre del perfil creado, o nil y el motivo del error.
function ns.ImportProfile(code, newName)
code = tostring(code or ""):gsub("%s+", "")
if code == "" then return nil, L["Paste a code first."] end
if code:sub(1, #CODE_PREFIX) ~= CODE_PREFIX then return nil, L["Invalid profile code."] end
local ok, payload = pcall(function()
return Deserialize(Base64Decode(code:sub(#CODE_PREFIX + 1)))
end)
if not ok or type(payload) ~= "table" or type(payload.data) ~= "table" then
return nil, L["Invalid profile code."]
end
local name = CleanName(newName)
if name == "" then name = CleanName(payload.name) end
if name == "" then name = "Imported" end
name = UniqueName(name)
local clean = {}
for key, defaults in pairs(ns.defaults) do
clean[key] = Sanitize(payload.data[key], defaults)
end
ClearPreviews(clean)
ns.root.profiles[name] = clean
return name
end