-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportExport.lua
More file actions
399 lines (331 loc) · 12.9 KB
/
ImportExport.lua
File metadata and controls
399 lines (331 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
-- ImportExport.lua
-- Scale import/export functionality for Valuate
-- ========================================
-- Constants
-- ========================================
-- Current scale tag format version
local SCALE_TAG_VERSION = 1
-- Import result status codes
Valuate.ImportResult = {
SUCCESS = 1,
ALREADY_EXISTS = 2,
TAG_ERROR = 3,
VERSION_ERROR = 4,
}
-- ========================================
-- Export Functions
-- ========================================
-- Generates an export string (scale tag) for a scale
-- scaleName: Internal scale name (key in scales table)
-- Returns: Scale tag string, or nil if scale doesn't exist
function Valuate:GetScaleTag(scaleName)
if not scaleName or scaleName == "" then
return nil
end
local scales = Valuate:GetScales()
local scale = scales[scaleName]
if not scale then
return nil
end
-- Start building the tag: {Valuate:v1:ScaleName{...}}
local displayName = scale.DisplayName or scaleName
local tag = string.format("{Valuate:v%d:%s{", SCALE_TAG_VERSION, displayName)
local parts = {}
-- Add Color (required, default to white if missing)
local color = scale.Color or "FFFFFF"
table.insert(parts, string.format("Color=%s", color))
-- Add Visible flag (0 or 1)
local visible = (scale.Visible ~= false) and 1 or 0
table.insert(parts, string.format("Visible=%d", visible))
-- Add Icon path if present
if scale.Icon and scale.Icon ~= "" then
table.insert(parts, string.format("Icon=%s", scale.Icon))
end
-- Add stat weights (skip zero values)
if scale.Values then
-- Sort stat names for consistent output
local statNames = {}
for statName, _ in pairs(scale.Values) do
table.insert(statNames, statName)
end
table.sort(statNames)
for _, statName in ipairs(statNames) do
local value = scale.Values[statName]
if value and value ~= 0 then
table.insert(parts, string.format("%s=%s", statName, tostring(value)))
end
end
end
-- Add Unusable stats (banned stats)
if scale.Unusable then
local unusableNames = {}
for statName, _ in pairs(scale.Unusable) do
table.insert(unusableNames, statName)
end
table.sort(unusableNames)
for _, statName in ipairs(unusableNames) do
if scale.Unusable[statName] then
table.insert(parts, string.format("Unusable.%s=1", statName))
end
end
end
-- Concatenate all parts with commas
tag = tag .. table.concat(parts, ",")
-- Close the tag
tag = tag .. "}}"
return tag
end
-- Exports all scales as a series of scale tags
-- Returns: String containing all scale tags separated by spaces
function Valuate:ExportAllScales()
local tags = {}
-- Get all scale names and sort them
local scaleNames = {}
local scales = Valuate:GetScales()
for scaleName, _ in pairs(scales) do
table.insert(scaleNames, scaleName)
end
table.sort(scaleNames)
-- Generate tag for each scale
for _, scaleName in ipairs(scaleNames) do
local tag = self:GetScaleTag(scaleName)
if tag then
table.insert(tags, tag)
end
end
-- Join with double space for readability
return table.concat(tags, " ")
end
-- ========================================
-- Import Functions
-- ========================================
-- Parses a scale tag and extracts the scale data
-- scaleTag: The import string
-- Returns: scaleName, scaleData, errorMessage, versionMessage
function Valuate:ParseScaleTag(scaleTag)
if not scaleTag or type(scaleTag) ~= "string" then
return nil, nil, "Invalid input: scale tag must be a non-empty string"
end
-- Trim whitespace
scaleTag = strtrim(scaleTag)
if scaleTag == "" then
return nil, nil, "Invalid input: scale tag must be a non-empty string"
end
-- Parse the outer structure: {Valuate:v1:ScaleName{props}}
local version, scaleName, propsString = string.match(scaleTag, "^{Valuate:v(%d+):([^{]+){(.+)}}$")
if not version or not scaleName or not propsString then
return nil, nil, "Invalid format: scale tag must be in format {Valuate:v1:Name{props}}"
end
version = tonumber(version)
if not version then
return nil, nil, "Invalid version number in scale tag"
end
-- Trim scale name
scaleName = strtrim(scaleName)
if scaleName == "" then
return nil, nil, "Scale name cannot be empty"
end
-- Validate scale name doesn't contain characters that could break the UI
-- Disallow: { } | (used in scale tag format) and control characters
if string.match(scaleName, "[{}|]") then
return nil, nil, "Scale name cannot contain '{', '}', or '|' characters"
end
-- Check version compatibility
if version > SCALE_TAG_VERSION then
-- Future version - we might not be able to parse it correctly
return nil, nil, "This scale tag is from a newer version of Valuate (v" .. version .. "). Please update the addon.", version
end
-- Parse the properties string (key=value pairs separated by commas)
local scaleData = {
DisplayName = scaleName,
Values = {},
Unusable = {},
}
-- Split by commas, but need to handle icon paths that might contain commas
-- We'll use a simple state machine approach
local currentPos = 1
while currentPos <= #propsString do
-- Find the next key=value pair
local keyStart, keyEnd, key, value
-- Match pattern: Key=Value (where Value can contain backslashes for paths)
-- Look for the equals sign
local equalsPos = string.find(propsString, "=", currentPos, true)
if not equalsPos then
break
end
-- Extract key (everything before =)
key = string.sub(propsString, currentPos, equalsPos - 1)
key = strtrim(key)
-- Extract value (everything until next comma or end)
-- Special handling: if key is "Icon", value might have backslashes and go until next stat name pattern
local valueStart = equalsPos + 1
local valueEnd
if key == "Icon" then
-- Icon path - look for the next comma followed by a key=value pattern
-- Pattern: ",KeyName=" where KeyName doesn't contain backslashes (stat names don't have them)
-- This correctly handles icon paths with capital letters like "Interface\Icons\INV_Sword_04"
local nextKeyStart = string.find(propsString, ",([^\\,=]+)=", valueStart)
if nextKeyStart then
valueEnd = nextKeyStart - 1 -- Don't include the comma
else
valueEnd = #propsString -- Go to end
end
else
-- Regular value - find next comma
valueEnd = string.find(propsString, ",", valueStart, true)
if valueEnd then
valueEnd = valueEnd - 1
else
valueEnd = #propsString
end
end
value = string.sub(propsString, valueStart, valueEnd)
value = strtrim(value)
-- Process the key=value pair
if key and value and key ~= "" and value ~= "" then
if key == "Color" then
scaleData.Color = value
elseif key == "Visible" then
scaleData.Visible = (tonumber(value) == 1)
elseif key == "Icon" then
scaleData.Icon = value
else
-- Check if this is an Unusable stat (e.g., "Unusable.Intellect")
local statName = string.match(key, "^Unusable%.(.+)$")
if statName and statName ~= "" then
scaleData.Unusable[statName] = true
else
-- Regular stat weight
local numValue = tonumber(value)
if numValue then
scaleData.Values[key] = numValue
end
end
end
end
-- Move to next key=value pair
-- Skip comma (valueEnd points to the last char of value, so +2 skips comma and space)
currentPos = valueEnd + 2
if currentPos > #propsString then
break
end
end
-- Validate that we got at least some data
if not next(scaleData.Values) then
-- No stat values found
return nil, nil, "No valid stat values found in scale tag"
end
-- Clean up empty Unusable table
if not next(scaleData.Unusable) then
scaleData.Unusable = nil
end
return scaleName, scaleData, nil, nil
end
-- Imports a scale from a scale tag
-- scaleTag: The import string
-- overwrite: If true, overwrite existing scale with same name; if false, fail if exists
-- Returns: status, scaleName, errorMessage
-- status: One of Valuate.ImportResult.*
-- scaleName: The name of the imported scale
-- errorMessage: Detailed error message if import failed
function Valuate:ImportScale(scaleTag, overwrite)
local scaleName, scaleData, errorMessage, versionMessage = self:ParseScaleTag(scaleTag)
if not scaleName then
if versionMessage then
return Valuate.ImportResult.VERSION_ERROR, nil, errorMessage
else
return Valuate.ImportResult.TAG_ERROR, nil, errorMessage
end
end
-- Check if scale already exists
local scales = Valuate:GetScales()
local alreadyExists = (scales[scaleName] ~= nil)
if alreadyExists and not overwrite then
return Valuate.ImportResult.ALREADY_EXISTS, scaleName, nil
end
-- Import the scale
scales[scaleName] = scaleData
-- If the UI is loaded, refresh it
if Valuate.RefreshScaleList then
Valuate:RefreshScaleList()
end
if Valuate.RefreshStatEditor then
Valuate:RefreshStatEditor()
end
-- Reset all tooltips to show the new/updated scale immediately
if Valuate.ResetTooltips then
Valuate:ResetTooltips()
end
return Valuate.ImportResult.SUCCESS, scaleName, nil
end
-- Parses multiple scale tags from a single string
-- text: String containing one or more scale tags
-- Returns: array of {scaleName, scaleData}, array of {error, tag}
function Valuate:ParseMultipleScaleTags(text)
if not text or type(text) ~= "string" then
return {}, {}
end
local parsedScales = {}
local errors = {}
-- Extract all scale tags using pattern matching
-- Pattern: {Valuate:...}
for scaleTag in string.gmatch(text, "{Valuate:[^}]+}}") do
local scaleName, scaleData, errorMessage, versionMessage = self:ParseScaleTag(scaleTag)
if scaleName and scaleData then
table.insert(parsedScales, {
name = scaleName,
data = scaleData,
tag = scaleTag
})
else
table.insert(errors, {
error = errorMessage or "Unknown error",
tag = scaleTag
})
end
end
return parsedScales, errors
end
-- Imports multiple scales from a string containing multiple scale tags
-- text: String containing one or more scale tags
-- overwrite: If true, overwrite existing scales; if false, return list of conflicts
-- Returns: successCount, failCount, existingScales (array of scale names that exist)
function Valuate:ImportMultipleScales(text, overwrite)
local parsedScales, errors = self:ParseMultipleScaleTags(text)
local successCount = 0
local failCount = #errors
local existingScales = {}
-- First pass: check for existing scales if not overwriting
if not overwrite then
local scales = Valuate:GetScales()
for _, parsed in ipairs(parsedScales) do
if scales[parsed.name] then
table.insert(existingScales, parsed.name)
end
end
-- If there are existing scales, return without importing
if #existingScales > 0 then
return 0, 0, existingScales
end
end
-- Second pass: import all scales
local scales = Valuate:GetScales()
for _, parsed in ipairs(parsedScales) do
scales[parsed.name] = parsed.data
successCount = successCount + 1
end
-- Refresh UI once at the end
if successCount > 0 then
if Valuate.RefreshScaleList then
Valuate:RefreshScaleList()
end
if Valuate.RefreshStatEditor then
Valuate:RefreshStatEditor()
end
-- Reset all tooltips to show the new/updated scales immediately
if Valuate.ResetTooltips then
Valuate:ResetTooltips()
end
end
return successCount, failCount, existingScales
end