-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDumbXmlParser.lua
228 lines (208 loc) · 5.35 KB
/
DumbXmlParser.lua
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
-- first strip out UTF8 BOM from files with powershell
local lfs = require "lfs"
local FRAMEXML = "../#FrameXML/Generate-Globals/wow-ui-source/"
-- too lazy to parse FrameXML_TBC.toc or whatever the new file loading structure is
local flavors = {
mainline = {FRAMEXML.."Interface"},
vanilla = {
FRAMEXML.."Interface",
FRAMEXML.."Interface_Vanilla",
},
tbc = {
FRAMEXML.."Interface",
FRAMEXML.."Interface_TBC",
},
wrath = {
FRAMEXML.."Interface",
FRAMEXML.."Interface_WRATH",
},
cata = {
FRAMEXML.."Interface",
},
}
local OUT_PATH = "../BlizzardInterfaceResources/Resources"
local flavorFilters = {
cata = {
["Mainline"] = true,
["Vanilla"] = true,
["TBC"] = true,
["Wrath"] = true,
}
}
local skipDir = {
["."] = true,
[".."] = true,
["GlueXML"] = true,
}
local dataTypes = {
mixin = {
label = "Mixins",
data = {},
tostring = function(name)
return string.format('\t"%s",', name)
end,
},
template = {
label = "Templates",
data = {},
tostring = function(tbl)
local s = string.format('\t["%s"] = {type = "%s"', tbl.name, tbl.object)
if tbl.mixin then
s = s..string.format(', mixin = "%s"', tbl.mixin)
end
if tbl.inherits then
s = s..string.format(', inherits = "%s"', tbl.inherits)
end
if tbl.intrinsic then
s = s..string.format(', intrinsic = true', tbl.inherits)
end
s = s.."},"
return s
end,
},
}
local mixinFunc = {
CreateFromMixins = "CreateFromMixins%((.-)%)",
CreateAndInitFromMixin = "CreateAndInitFromMixin%((.-)[,%)]", -- 9 mixins
}
local filterMixinArgs = {
[""] = true, -- no args
["..."] = true, -- definition
["baseModule or DEFAULT_OBJECTIVE_TRACKER_MODULE"] = true,
["dataProvider"] = true,
["mixin"] = true,
["nodeMixin"] = true,
["self"] = true,
["subSystemMixin"] = true,
["pinFrameLevel"] = true,
}
local filterTemplates = {
-- AddOns\Blizzard_GuildControlUI\Blizzard_GuildControlUI.xml
GuildBankTabPermissionsTabTemplate = true, -- commented out
}
local function SortTable(tbl)
local t = {}
for k in pairs(tbl) do
table.insert(t, k)
end
table.sort(t, function(a, b)
return a:lower() < b:lower()
end)
return t
end
local function IsFolderFlavor(folder, flavor)
if flavorFilters[flavor] then
return not flavorFilters[flavor][folder]
end
return true
end
local m = {}
function m:IterateFiles(folder, flavor)
for fileName in lfs.dir(folder) do
local path = folder.."/"..fileName
local attr = lfs.attributes(path)
if attr.mode == "directory" then
if not skipDir[fileName] and IsFolderFlavor(fileName, flavor) then
-- print(path)
self:IterateFiles(path, flavor)
end
else
if fileName:find("%.xml") then
self:ParseXml(path, fileName)
elseif fileName:find("%.lua") then
self:ParseLua(path)
end
end
end
end
function m:ParseXml(path, fileName)
-- could use xml2lua for less dumb parsing
local file = io.open(path, "r")
local lookbackLines, lineIdx = {}, 0
for line in file:lines() do
lineIdx = lineIdx + 1
local mixin = self:FindAttribute(line, "mixin")
-- <Frame name="ProfessionsQualityContainerTemplate" mixin="" virtual="true">
if mixin and #mixin > 0 then
self:HandleCommaString(dataTypes.mixin.data, mixin)
end
local virtual = self:FindAttribute(line, "virtual")
local intrinsic = self:FindAttribute(line, "intrinsic")
if virtual == "true" or intrinsic then -- attribute value for IMECandidatesFrame is "false"
local objectType = line:match('<(.-) ')
local name = line:match(' name%s?="(.-)"')
if not name and fileName == "SecureHandlerTemplates.xml" then
-- not everything is on 1 line e.g. SecureHandlerDragTemplate
line = lookbackLines[lineIdx-2]..lookbackLines[lineIdx-1]..line
objectType, name = line:match('<(.-) name%s?="(.-)"')
end
if name and not filterTemplates[name] and not name:find("%$") then
dataTypes.template.data[name] = {
name = name,
object = objectType,
inherits = self:FindAttribute(line, "inherits"),
mixin = mixin,
intrinsic = intrinsic,
}
end
end
lookbackLines[lineIdx] = line
end
file:close()
end
function m:ParseLua(path)
local file = io.open(path, "r")
for line in file:lines() do
for _, pattern in pairs(mixinFunc) do
local attrValue = line:match(pattern)
if attrValue and not filterMixinArgs[attrValue] then
self:HandleCommaString(dataTypes.mixin.data, attrValue)
end
end
end
file:close()
end
function m:FindAttribute(line, name)
local pattern = name..'%s?="(.-)"'
return line:match(pattern)
end
local bl = {
GetMaxPinLevel = true,
PIN_LEVEL_RANGE = true,
settings = true,
self = true,
pinFrameLevel = true,
}
function m:HandleCommaString(tbl, str)
if str:find(",") then
for part in str:gmatch("[^%s,%(%)]+") do
if part:find("%w") and not bl[part] then
tbl[part] = part
end
end
else
tbl[str] = str
end
end
function m:WriteDataFile(info)
local fullPath = OUT_PATH.."/"..info.label..".lua"
print("writing", fullPath)
local file = io.open(fullPath, "w")
file:write(string.format("local %s = {\n", info.label))
for _, key in pairs(SortTable(info.data)) do
local value = info.data[key]
local text = info.tostring(value)
file:write(text.."\n")
end
file:write(string.format("}\n\nreturn %s\n", info.label))
file:close()
end
function m:main(flavor)
for _, folder in pairs(flavors[flavor]) do
m:IterateFiles(folder, flavor)
end
for _, info in pairs(dataTypes) do
m:WriteDataFile(info)
end
end
return m