-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.lua
More file actions
113 lines (101 loc) · 3.77 KB
/
Copy pathDatabase.lua
File metadata and controls
113 lines (101 loc) · 3.77 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
local _, MacroStudio = ...
local Database = {
CURRENT_SCHEMA_VERSION = 4,
}
MacroStudio.Database = Database
local function nextAvailableId(records, prefix)
local highest = 0
for id in pairs(records) do
if type(id) == "string" then
local number = tonumber(id:match("^" .. prefix .. "%-(%d+)$"))
if number and number > highest then
highest = number
end
end
end
return highest + 1
end
local function normalizeCharacterLibrary(database)
if type(database.characterLibrary) ~= "table" then
database.characterLibrary = {}
end
local library = database.characterLibrary
if type(library.characters) ~= "table" then
library.characters = {}
end
if type(library.order) ~= "table" then
library.order = {}
end
local normalizedOrder = {}
local seen = {}
for _, characterId in ipairs(library.order) do
local record = type(characterId) == "string" and library.characters[characterId] or nil
if type(record) == "table" and not seen[characterId] then
record.id = characterId
record.macros = type(record.macros) == "table" and record.macros or {}
normalizedOrder[#normalizedOrder + 1] = characterId
seen[characterId] = true
end
end
for characterId, record in pairs(library.characters) do
if type(characterId) == "string" and type(record) == "table" and not seen[characterId] then
record.id = characterId
record.macros = type(record.macros) == "table" and record.macros or {}
normalizedOrder[#normalizedOrder + 1] = characterId
seen[characterId] = true
end
end
library.order = normalizedOrder
end
function Database:Initialize()
if type(MacroStudioDB) ~= "table" then
MacroStudioDB = {}
end
local previousSchema = tonumber(MacroStudioDB.schemaVersion) or 1
if type(MacroStudioDB.settings) ~= "table" then
MacroStudioDB.settings = {}
end
if type(MacroStudioDB.settings.useMacroStudioSlashCommands) ~= "boolean" then
MacroStudioDB.settings.useMacroStudioSlashCommands = true
end
if type(MacroStudioDB.settings.showMinimapButton) ~= "boolean" then
MacroStudioDB.settings.showMinimapButton = true
end
local minimapAngle = tonumber(MacroStudioDB.settings.minimapAngle)
if not minimapAngle then
minimapAngle = 225
end
MacroStudioDB.settings.minimapAngle = minimapAngle % 360
if type(MacroStudioDB.metadata) ~= "table" then
MacroStudioDB.metadata = {}
end
if type(MacroStudioDB.metadata.records) ~= "table" then
MacroStudioDB.metadata.records = {}
end
if type(MacroStudioDB.metadata.nextId) ~= "number" then
MacroStudioDB.metadata.nextId = nextAvailableId(MacroStudioDB.metadata.records, "metadata")
end
if type(MacroStudioDB.categories) ~= "table" then
MacroStudioDB.categories = {}
end
if type(MacroStudioDB.categories.byId) ~= "table" then
MacroStudioDB.categories.byId = {}
end
if type(MacroStudioDB.categories.order) ~= "table" then
MacroStudioDB.categories.order = {}
end
if type(MacroStudioDB.categories.nextId) ~= "number" then
MacroStudioDB.categories.nextId = nextAvailableId(MacroStudioDB.categories.byId, "category")
end
if type(MacroStudioDB.history) ~= "table" then
MacroStudioDB.history = {}
end
normalizeCharacterLibrary(MacroStudioDB)
-- Never downgrade a database created by a newer addon version.
if previousSchema <= self.CURRENT_SCHEMA_VERSION then
MacroStudioDB.schemaVersion = self.CURRENT_SCHEMA_VERSION
else
MacroStudioDB.schemaVersion = previousSchema
end
MacroStudio.db = MacroStudioDB
end