-
Notifications
You must be signed in to change notification settings - Fork 33
/
babelfish.lua
500 lines (461 loc) · 13.5 KB
/
babelfish.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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
--[[
This little script get all Locales from the given files.
Used mostly for AtlasLoot
]]--
-- ################################
-- Settings
-- ################################
-- Output Path / Folder must exist
local OUTPUT_PATH = ""
-- Identifier for Locales
local IDENTIFIER = "AL"
-- Parse XML-Files for embeded lua scripts?
local PARSE_XML = true
-- Create a log file
local LOG = false
-- Set log lvl 0: info,warning,error - 1:warning,error - 2:error
local LOG_LVL = 1
-- Base namespace, globales that are found in base are removed from other namespaces
local BASE_NAMESPACE = "Global"
-- Move translations that are used in multiple namespaces into base namespace?
local REBASE_LOCALES = true
-- FileList, supports toc, xml and lua
local FILE_LIST_ID = {
-- optional: { "path", <bool>ParseXML } -- disable/enable xml parsing for single files
{
name = "Global",
"AtlasLootClassic/AtlasLootClassic.toc",
"SpecialLoc.lua",
},
{
name = "Collections",
"AtlasLootClassic_Collections/AtlasLootClassic_Collections.toc",
},
{
name = "Crafting",
"AtlasLootClassic_Crafting/AtlasLootClassic_Crafting.toc",
},
{
name = "DungeonsAndRaids",
"AtlasLootClassic_DungeonsAndRaids/data.lua",
},
{
name = "DungeonsAndRaidsTBC",
base = "DungeonsAndRaids",
"AtlasLootClassic_DungeonsAndRaids/data-tbc.lua",
},
{
name = "DungeonsAndRaidsWrath",
base = "DungeonsAndRaidsTBC",
"AtlasLootClassic_DungeonsAndRaids/data-wrath.lua",
},
{
name = "PvP",
"AtlasLootClassic_PvP/AtlasLootClassic_PvP.toc",
},
{
name = "Options",
"AtlasLootClassic_Options/AtlasLootClassic_Options.toc",
},
{
name = "Factions",
"AtlasLootClassic_Factions/AtlasLootClassic_Factions.toc",
},
}
-- Ignore Files / Pathes
local IGNORE_LIST = {
["AtlasLootClassic/Locales/"] = true,
["AtlasLootClassic/Libs"] = true,
["AtlasLootClassic_Options/Libs"] = true,
}
-- ################################
-- Script part, no changes here
-- ################################
local gmatch, sub, format, lower, gsub, match, byte = string.gmatch, string.sub, string.format, string.lower, string.gsub, string.match, string.byte
local unpack = table.unpack or unpack
local io_lines = io.lines
local FILE_LIST = {}
for k,v in ipairs(FILE_LIST_ID) do
FILE_LIST[v.name] = v
end
assert(not BASE_NAMESPACE or (type(BASE_NAMESPACE) == "string"), "BASE_NAMESPACE must be a string")
assert(not BASE_NAMESPACE or (FILE_LIST[BASE_NAMESPACE]), "BASE_NAMESPACE not found in FILE_LIST")
assert(not BASE_NAMESPACE or (#FILE_LIST[BASE_NAMESPACE] > 0), "BASE_NAMESPACE must contain at least one entry in FILE_LIST")
local FILE_IDENTIFIERS = {
["lua"] = true,
["xml"] = true,
["toc"] = true,
}
local FILE_CONTAINS_LOCALE = {
["lua"] = true,
}
local FILE_IDENTIFIERS_MAX_SIZE = 0
for k,v in pairs(FILE_IDENTIFIERS) do
if FILE_IDENTIFIERS_MAX_SIZE < #k then FILE_IDENTIFIERS_MAX_SIZE = #k end
end
local IDENTIFIER_SIZE = #IDENTIFIER
local LogTable = {}
local DuplicateProtection = {}
local LogTypes = {
[0] = "Info",
[1] = "Warning",
[2] = "Error",
[99] = "Result",
}
local function AddLog(type, msg)
if not LOG or type < LOG_LVL then return end
LogTable[#LogTable+1] = "["..os.date('%Y-%m-%d %H:%M:%S').."] "..LogTypes[type]..": "..msg
end
local function FileExists(fileName)
local f = io.open(fileName, "rb")
if f then f:close() end
return f ~= nil
end
local function AddIntoITable(d, s, d2)
if not s or not d then return end
DuplicateProtection[d] = DuplicateProtection[d] or {}
if type(s) == "table" then
for k,v in ipairs(s) do
if not DuplicateProtection[d][v] and (not d2 or not DuplicateProtection[d2][v]) then
d[#d+1] = v
DuplicateProtection[d][v] = #d
else
DuplicateProtection[d][v] = 0
end
end
else
if not DuplicateProtection[d][s] and (not d2 or not DuplicateProtection[d2][s]) then
d[#d+1] = s
DuplicateProtection[d][s] = #d
else
DuplicateProtection[d][s] = 0
end
end
end
-- path, file, fileType
local SplitCache = {}
local function SplitPathAndFileName(fileName)
assert(fileName, "fileName not found: "..(fileName or "nil"))
if SplitCache[fileName] then
return unpack(SplitCache[fileName])
end
local path, file, fileType = ""
local tmp, lastC = "", ""
local count = 0
for c in gmatch(fileName, ".") do
count = count + 1
if count >= (#fileName-FILE_IDENTIFIERS_MAX_SIZE-1) and c == "." then
if not file then
file, tmp, c = tmp, "", ""
end
elseif c == "\\" or c == "/" then
if lastC ~= "/" and lastC ~= "" then
c = "/"
else
c = ""
end
elseif lastC == "/" or lastC == "" then
path, tmp = path..tmp, ""
end
tmp = tmp..c
lastC = c
end
if FILE_IDENTIFIERS[lower(tmp)] then
fileType = tmp
else
for i = 1, #tmp do
local s = sub(path, i, #tmp)
if FILE_IDENTIFIERS[lower(s)] then
fileType = s
break
end
end
end
if not fileType then
if file then
file = file..tmp
else
path = path..tmp
end
end
SplitCache[fileName] = { path, file, fileType, true }
if path and file and fileType then SplitCache[fileName][4] = format("%s%s.%s", path, file, fileType)
elseif path and file then SplitCache[fileName][4] = format("%s%s", path, file)
elseif path then SplitCache[fileName][4] = path end
return unpack(SplitCache[fileName])
end
local function IsOnIgnoreList(fileName)
if not fileName then return end
local path, file, fileType, fileNameNew = SplitPathAndFileName(fileName)
if IGNORE_LIST[fileNameNew] then return true end
for k,v in pairs(IGNORE_LIST) do
local p, f, fT, fN = SplitPathAndFileName(k)
if match(fileNameNew, fN) then
return true
end
end
end
local function ParseLuaFile(fileName, base)
if not FileExists(fileName) then return end
local _
_, _, _, fileName = SplitPathAndFileName(fileName)
if IsOnIgnoreList(fileName) then return end
local t = {}
local isComment, commentCount = false, 0
local oneLineCom, multiComment, multiComReg = false, false, nil
local tmp, loc, start, lastC = ""
for line in io_lines(fileName) do
for c in gmatch(line, ".") do
if oneLineCom or multiComment then
if oneLineCom and not tmp and lastC == "-" and c=="[" then
oneLineCom = c
elseif type(oneLineCom) == "string" and not tmp and (lastC=="=" or lastC=="[") and (c=="=" or c=="[") then
oneLineCom = oneLineCom..c
elseif multiComment and not tmp and lastC == "]" and ( c == "]" or c == "=" ) then
tmp = "]"..c
elseif multiComment and tmp and (c=="=" or c=="]") then
tmp = tmp..c
else
--tmp = nil
end
if type(oneLineCom) == "string" and #oneLineCom > 1 and not tmp and lastC=="[" and (c~="=" or c~="[") then
multiComReg = gsub(oneLineCom, "%[", "]")
multiComment, oneLineCom = true, false
elseif multiComment and tmp and multiComReg == tmp then
multiComment, multiComReg, tmp = false, nil, ""
end
end
if not oneLineCom and not multiComment then
if tmp == IDENTIFIER and c == "[" then
start = true
elseif tmp == IDENTIFIER and c ~= "[" then
loc, start, tmp = nil, false, ""
elseif start and c ~= "\"" and not loc then --start
loc, start, tmp = nil, false, ""
elseif start and c == "\"" and not loc then --start
loc = ""
elseif loc and start and c == "\"" and lastC ~= "\\" then
AddIntoITable(t, loc, base)
loc, start, tmp = nil, false, ""
elseif loc and start then
loc = loc..c
elseif c == "-" and lastC == "-" then
oneLineCom, tmp = true, nil
end
if oneLineCom then
elseif #tmp == IDENTIFIER_SIZE then
tmp = sub(tmp..c, 2, IDENTIFIER_SIZE+1)
else
tmp = tmp..c
end
end
lastC = c
end
if oneLineCom then
oneLineCom, tmp = false, ""
end
end
if #t < 1 then AddLog(0, "No match inside "..fileName) end
return t
end
local function ParseXMLFile(fileName, overrideParseXML)
if not FileExists(fileName) then return end
local oriPath, _
oriPath, _, _, fileName = SplitPathAndFileName(fileName)
local t = {}
local isComment, commentCount, waiteForFile, start = false, 0, false, false
local tmp, lastC = ""
for line in io_lines(fileName) do
for c in gmatch(line, ".") do
if tmp == "!--" then
isComment, commentCount, tmp = true, commentCount+1, ""
end
if isComment then
if c == "-" and lastC ~= "-" then
tmp = "-"
elseif c == "-" and lastC == "-" and #tmp<2 then
tmp = tmp..c
elseif c == ">" and tmp == "--" then
commentCount = commentCount - 1
isComment = commentCount > 0 and true or false
elseif c ~= "-" and lastC ~= "-" then
tmp = ""
end
else
local lTmp = lower(tmp)
if start then
if c == "\"" and lastC ~= "\\" then
start = false
tmp = oriPath..tmp
local path, file, fileType, tmp = SplitPathAndFileName(tmp)
if path and file and fileType and FILE_CONTAINS_LOCALE[fileType] then
AddIntoITable(t, tmp)
elseif fileType and lower(fileType) == "xml" and ( overrideParseXML or PARSE_XML ) then
AddIntoITable(t, ParseXMLFile(tmp))
end
end
elseif lastC == "<" then
tmp = ""
elseif c == ">" then
tmp, waiteForFile = "", false
elseif lower(lTmp) == "script" or lower(lTmp) == "include" then
waiteForFile = true
elseif lower(lTmp) == "scripts" then
waiteForFile = false
elseif waiteForFile and ( lTmp == "file" or lTmp == "file=") and c == " " then
c = ""
elseif waiteForFile and lTmp == "file=" and c == "\"" then
start, tmp, c = true, "", ""
elseif lower(c) == "f" and waiteForFile then
tmp = ""
end
if c ~= "<" then tmp=tmp..c end
end
lastC = c
end
end
if #t < 1 then AddLog(1, "No file or file with record loaded inside "..fileName) end
return t
end
local function ParseTocFile(fileName, overrideParseXML)
assert(FileExists(fileName), "Toc File not found: "..(fileName or "nil"))
local oriPath, _
oriPath, _, _, fileName = SplitPathAndFileName(fileName)
local t = {}
for line in io_lines(fileName) do
local s = ""
for c in gmatch(line, ".") do
if c == "#" then break end
s = s..c
end
if #s > 0 then
s = oriPath..s
local path, file, fileType, s = SplitPathAndFileName(s)
if path and file and fileType and FILE_CONTAINS_LOCALE[fileType] then
AddIntoITable(t, s)
elseif fileType and lower(fileType) == "xml" and ( overrideParseXML or PARSE_XML ) then
AddIntoITable(t, ParseXMLFile(s))
end
end
end
if #t < 1 then AddLog(1, "No file or file with record loaded inside "..fileName) end
return t
end
local function ParseNamespace(namespace)
if not namespace or not FILE_LIST[namespace] then return end
local t = {}
for _, pathG in ipairs(FILE_LIST[namespace]) do
local overrideParseXML
if type(pathG) == "table" then
overrideParseXML = pathG[2]
pathG = pathG[1]
end
local path, file, fileType, fullPath = SplitPathAndFileName(pathG)
if not IsOnIgnoreList(fullPath) then
if path and file and fileType and FILE_CONTAINS_LOCALE[fileType] then
AddIntoITable(t, fullPath)
elseif fileType and lower(fileType) == "toc" then
AddIntoITable(t, ParseTocFile(fullPath, overrideParseXML))
elseif fileType and lower(fileType) == "xml" and ( overrideParseXML or PARSE_XML ) then
AddIntoITable(t, ParseXMLFile(fullPath))
end
end
end
return t
end
local function Finalize_Rebase(t)
if not REBASE_LOCALES or not BASE_NAMESPACE then return end
local usage = {}
for namespace, nsT in pairs(t) do
if namespace ~= BASE_NAMESPACE then
for _, loc in ipairs(nsT) do
if not usage[loc] then usage[loc] = {} end
usage[loc][#usage[loc]+1] = { namespace }
end
end
end
for loc,locT in pairs(usage) do
if #locT > 1 then
AddIntoITable(t[BASE_NAMESPACE], loc)
end
end
end
local function Finalize_Build(t1, t2)
if not t1 then return end
if t2 and t1 ~= t2 then
local tR = {}
for k,v in ipairs(t1) do
if DuplicateProtection[t2][v] then
AddLog(1, IDENTIFIER.."[\""..v.."\"] found dublicate.")
tR[#tR+1] = k
end
end
for i = #tR, 1, -1 do table.remove(t1, tR[i]) end
end
table.sort(t1)
end
local function BuildFileList()
local t = {}
if BASE_NAMESPACE then
t[BASE_NAMESPACE] = ParseNamespace(BASE_NAMESPACE)
end
for nsID, nt in pairs(FILE_LIST_ID) do
namespace = nt.name
if namespace ~= BASE_NAMESPACE then
t[namespace] = ParseNamespace(namespace)
end
end
return t
end
local function BuildLocaleList(fileList)
if not fileList then return end
local t = {}
for nsID, fliNs in pairs(FILE_LIST_ID) do
--for ns,nst in pairs(fileList) do
local ns = fliNs.name
local nst = fileList[ns]
t[ns] = {}
for k,v in ipairs(nst) do
if fliNs.base and type(fliNs.base) == "string" and t[fliNs.base] then
AddIntoITable(t[ns], ParseLuaFile(v), t[fliNs.base])
else
AddIntoITable(t[ns], ParseLuaFile(v), nil)
end
end
end
return t
end
local function getJsonFormattedString(s)
return s
end
local function Finalize(t)
Finalize_Rebase(t)
local base = BASE_NAMESPACE and t[BASE_NAMESPACE] or nil
local c = 0
for namespace, nsT in pairs(t) do
Finalize_Build(nsT, base)
if #nsT > 0 then
local nsFfile = io.open(OUTPUT_PATH..namespace..".lua", "w+")
for _,loc in ipairs(nsT) do
nsFfile:write(format(IDENTIFIER.."[\"%s\"] = true\n", getJsonFormattedString(loc)))
c = c + 1
end
AddLog(99,"Created namespace:"..namespace.." <"..#nsT..">")
nsFfile:close()
end
end
return c
end
-- ################################
-- Build
-- ################################
local FileList = BuildFileList()
local Locales = BuildLocaleList(FileList)
local LocCount = Finalize(Locales)
if LOG then
local logFile = io.open(OUTPUT_PATH.."log.txt" , "w+")
io.output(logFile)
io.write(table.concat(LogTable, "\n"))
io.close(logFile)
end
print("--- FINISHED <"..LocCount.." locales> ---")