-
Notifications
You must be signed in to change notification settings - Fork 1
/
shared.lua
710 lines (511 loc) · 17.1 KB
/
shared.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
local string = string;
local math = math;
local table = table;
local strsub = string.sub;
local strbyte = string.byte;
local strchar = string.char;
local strlen = string.len;
local strfind = string.find;
local fread = fileRead;
local ipairs = ipairs;
local pairs = pairs;
local collectgarbage = collectgarbage;
function isflag(a, b)
return (a % (2 ^ (b+1)) - a % (2 ^ b)) == 2 ^ b;
end
function table.delete(tab, obj)
local m,n;
for m,n in ipairs(tab) do
if (n == obj) then
table.remove(tab, m);
return m;
end
end
error("failed to delete table item", 2);
return false;
end
function traceback()
local level = debug.getinfo(1);
local n = 1;
while (level) do
outputDebugString(level.short_src .. ": " .. level.currentline .. ", " .. level.linedefined);
n = n + 1;
level = debug.getinfo(n);
end
error("traceback!", 2);
end
function fileGetHash(path)
if not (fileExists(path)) then return ""; end;
local file = fileOpen(path);
if not (file) then return ""; end;
local content = fileRead(file, fileGetSize(file));
fileClose(file);
return md5(content);
end
bigendian = {
int_to_bytes = function(n)
n = (n < 0) and (4294967296 + n) or n;
return (math.modf(n/0x1000000))%0xFF, (math.modf(n/0x10000))%0xFF, (math.modf(n/0x100))%0xFF, n%0xFF;
end,
fileReadInt = function(file)
local a, b, c, d = strbyte(fread(file, 4), 1, 4);
if not (d) then return false; end;
return a * 0x1000000 + b * 0x10000 + c * 0x100 + d;
end,
fileReadShort = function(file)
local high, low = strbyte(fread(file, 2), 1, 2);
if not (low) then return false; end;
return high * 256 + low;
end,
fileReadByte = function(file)
local byte = fread(file, 1);
if not (byte) then return false; end;
return strbyte(byte);
end,
fileWriteInt = function(file, n)
return fileWrite(file, strchar((math.modf(n/0x1000000))%0xFF, (math.modf(n/0x10000))%0xFF, (math.modf(n/0x100))%0xFF, n%0xFF));
end,
fileWriteShort = function(file, n)
return fileWrite(file, strchar((math.modf(n/0x100))%0xFF, n%0xFF));
end,
fileWriteByte = function(file, n)
return fileWrite(file, strchar(n%0xFF));
end
}
littleendian = {
int_to_bytes = function(file)
n = (n < 0) and (4294967296 + n) or n;
return n%0xFF, (math.modf(n/0x100))%0xFF, (math.modf(n/0x10000))%0xFF, (math.modf(n/0x1000000))%0xFF;
end,
fileReadInt = function(file)
local a, b, c, d = strbyte(fread(file, 4), 1, 4);
if not (d) then return false; end;
return d * 0x1000000 + c * 0x10000 + b * 0x100 + a;
end,
fileReadShort = function(file)
local high, low = strbyte(fread(file, 2), 1, 2);
if not (low) then return false; end;
return low * 256 + high;
end,
fileReadByte = function(file)
local byte = fread(file, 1);
if not (byte) then return false; end;
return strbyte(byte);
end,
fileWriteInt = function(file, n)
return fileWrite(file, strchar(n%0x100, (math.modf(n/0x100))%0x100, (math.modf(n/0x10000))%0x100, (math.modf(n/0x1000000))%0x100));
end,
fileWriteShort = function(file, n)
return fileWrite(file, strchar(n%0x100, (math.modf(n/0x100))%0x100));
end,
fileWriteByte = function(file, n)
return fileWrite(file, strchar(n%0x100));
end
}
function fileSeek(file, seek)
return fileSetPos(file, fileGetPos(file) + seek);
end
function getFileExtension(src)
local lastOffset;
local begin, last = strfind(src, "[.]");
if not (begin) then return false; end;
while (begin) do
lastOffset = begin+1;
begin, last = strfind(src, "[.]", lastOffset);
end
return strsub(src, lastOffset, strlen(src));
end
function fileParsePath(path)
local arches = {};
local n = 0;
local lastBreak = 1;
local name;
local len = strlen(path);
local j,k;
local output = "";
while (true) do
n = strfind(path, "[\\/]", lastBreak);
if not (n) then break; end;
local archName = strsub(path, lastBreak, n - 1);
if (strlen(archName) == 0) then return false; end;
if (strfind(archName, "[<>:\"/|?*]")) then return false; end;
if (archName == "..") then
if (#arches == 0) then return false; end;
table.remove(arches);
elseif not (archName == ".") then
table.insert(arches, archName);
end
lastBreak = n + 1;
end
for j,k in ipairs(arches) do
output = output .. k .. "/";
end
if (lastBreak <= len) then
local filename = strsub(path, lastBreak, len);
if (strfind(filename, "[<>:\"/|?*]")) then return false; end;
return output .. filename, true;
end
return output, false;
end
-- New string.match routine
function globmatch(str, matchWith)
local m,n;
local lastBreak;
local len = strlen(str);
lastBreak = strfind(matchWith, "*", 1);
if not (lastBreak) then
return str == matchWith;
end
lastBreak = lastBreak + 1;
if not (strsub(str, 1, lastBreak - 2) == strsub(matchWith, 1, lastBreak - 2)) then return false; end;
m = lastBreak - 1;
while (true) do
n = strfind(matchWith, "*", lastBreak);
if not (n) then break; end;
local find = strsub(matchWith, lastBreak, n - 1);
m = strfind(str, find, m, true);
if not (m) then return false; end;
m = m + strlen(find);
lastBreak = n + 1;
end
local find = strsub(matchWith, lastBreak, strlen(matchWith));
if (strlen(find) == 0) then return true; end;
m = strfind(str, find, m, true);
if (m) then
m = m + 1;
while (true) do
n = strfind(str, find, m, true);
if not (n) then break; end;
m = n + 1;
end
m = m - 1 + strlen(find);
else
return false;
end
return m == len + 1;
end
function isName(char)
if ((char > 96) and (char < 123)) or ((char > 64) and (char < 91))
or (char == 95) -- _
or (char == 45) -- -
or (char == 46) -- .
or (char == 58) then -- :
return true;
end
return false;
end
function getNextSimpleItem(script, offset)
local m,n;
local len=strlen(script);
local char;
local parseOffset;
parseOffset = strfind(script, "[%a%d_.:]", offset);
if not (parseOffset) then
return false, nil, len, offset;
end
m = strfind(script, "[^%a%d_.:]", parseOffset+1);
if (m == nil) then
m = len+1;
end
return m, strsub(script, parseOffset, m-1), parseOffset;
end
-- returns new offset and item
function getNextItem(script, offset)
local m,n;
local len=strlen(script);
local char;
local parseOffset;
m = string.find(script, "[^%s%c]", offset);
if (m == nil) then
return false, nil, len, offset;
end
char = strbyte(script, m);
if (char == 45) and (strbyte(script, m+1) == 45) then -- --
parseOffset = m;
if (strbyte(script, m+2) == 91) and (strbyte(script, m+3) == 91) then -- --[[
m, n = strfind(script, "]]", m+4);
if (m == nil) then
return false, "--[[", len, parseOffset;
end
return m+2, strsub(script, parseOffset, n), parseOffset;
end
m = strfind(script, "\n", m+2);
if (m == nil) then
m = len + 1;
end
return m+1, strsub(script, parseOffset, m-1), parseOffset;
elseif (char == 91) and (strbyte(script, m+1) == 91) then -- [[
parseOffset = m;
m = strfind(script, "]]", m+2);
if (m == nil) then
return false, "[[", len, parseOffset;
end
return m+2, strsub(script, parseOffset, m+1), parseOffset;
elseif (char == 34) then -- "
parseOffset = m;
repeat
m = strfind(script, '[\n\\"]', m+1);
if (m == nil) then
return false, '"', len, parseOffset;
end
char = strbyte(script, m);
if (char == 92) then
m = m + 1;
elseif (char == 10) then
return false, '"', m-1, parseOffset;
end
until (char == 34);
return m+1, strsub(script, parseOffset, m), parseOffset;
elseif (char == 39) then -- '
parseOffset = m;
repeat
m = strfind(script, "[\n\\']", m+1);
if (m == nil) then
return false, '"', len, parseOffset;
end
char = strbyte(script, m);
if (char == 92) then
m = m + 1;
elseif (char == 10) then
return false, '"', m-1, parseOffset;
end
until (char == 39);
return m+1, strsub(script, parseOffset, m), parseOffset;
end
parseOffset = m;
m = strfind(script, "[^%a%d%._:]", m);
if (m == nil) then
m = len + 1;
elseif (m == parseOffset) then
return m+1, strchar(char), m;
end
return m, strsub(script, parseOffset, m-1), parseOffset;
end
local function xmlScanNode(xml, offset)
local offset, token, nodeEnd = getNextItem(xml, offset);
local nodeStart;
local inNode=false;
while (offset) do
token = strbyte(token);
if (inNode) then
if (token == 33) or (token == 63) then
local begin, offsetEnd = strfind(xml, ">", offset);
if not (begin) then
return false;
end
offset = begin + 1;
inNode = false;
elseif (token == 62) then
return strsub(xml, nodeStart, nodeEnd), offset, nodeStart;
elseif (token == 60) then
return false;
end
else
if (token == 60) then
if (inNode) then
return false;
end
inNode = true;
nodeStart = nodeEnd;
elseif (token == 62) then
return false;
end
end
offset, token, nodeEnd = getNextItem(xml, offset);
end
return false;
end
function parseStringInternal(input)
local n=1;
local len=strlen(input);
local output = "";
while (n <= len) do
local char = strsub(input, n, n);
if (char == "\\") then
n = n + 1;
char = strsub(input, n, n);
if (char == "n") then
output = output .. "\n";
elseif (char == "a") then
output = output .. "\a";
end
else
output = output .. char;
end
n = n + 1;
end
return output;
end
function convertStringInternal(input)
local n=1;
local len=strlen(input);
local output = "";
while (n <= len) do
local char = strsub(input, n, n);
if (char == "\n") then
output = output .. "\\n";
elseif (char == "\a") then
output = output .. "\\a";
else
output = output .. char;
end
n = n + 1;
end
return output;
end
function xmlParse(xml)
return xmlParseNode(xml, 1);
end
function xmlParseNode(xml, xmlOffset)
local token, offset = xmlScanNode(xml, xmlOffset);
local setOffset, setToken;
local key, value;
if not (token) then return false; end;
-- Parse the node, it is the node we want
local setting = strsub(token, 2, strlen(token) - 1);
setOffset, setToken = getNextItem(setting, 1);
local node = xmlCreateNodeEx(setToken);
if not (node) then return false; end;
setOffset, key = getNextItem(setting, setOffset);
while (setOffset) do
if (key == "/") then
-- The node is parsed correctly
return node, offset;
elseif not (isName(strbyte(key))) then
return false; -- Some wrong char was input
end
setOffset, setToken = getNextItem(setting, setOffset);
-- There can be only '='
if not (setToken == "=") then
outputDebugString("error "..setToken .. " " .. tostring(strbyte(setToken)));
return false;
end
setOffset, value = getNextItem(setting, setOffset);
-- Has to be string, yea
if not (strbyte(value) == 34) then return false; end;
-- But this string has to be parsed, then we can set the attribute
node.attr[key] = parseStringInternal(strsub(value, 2, strlen(value)-1));
setOffset, key = getNextItem(setting, setOffset);
end
token, offset, key = xmlScanNode(xml, offset);
while (token) do
-- See whether this token closes us
setting = strsub(token, 2, offset - 2);
setOffset, setToken = getNextItem(setting, 1);
if (setToken == "/") then
setOffset, setToken = getNextItem(setting, setOffset);
if not (setToken == node.name) then return false; end;
-- Finished children correctly
return node, offset;
elseif not (setToken == "!") then
local child;
child, offset = xmlParseNode(xml, key);
if not (child) then return false; end;
table.insert(node.children, child);
end
token, offset, key = xmlScanNode(xml, offset);
end
-- We need a closing node
return false;
end
function xmlCreateNodeEx(name)
local node = {};
if not (name) or (strlen(name) == 0) then return false; end;
if (strfind(name, "[^%a%d_-]")) then return false; end;
node.name = name;
node.children = {};
node.attr = {};
function node.writeTree(tabcount)
local buff = string.rep(" ", tabcount * 4) .. "<" .. node.name;
local m,n;
if not (tabcount) then
tabcount = 0;
end
for m,n in pairs(node.attr) do
buff = buff .. " " .. m .. "=\"" .. convertStringInternal(n) .. "\"";
end
if (#node.children == 0) then
buff = buff .. " />\n";
return buff;
end
buff = buff .. ">\n";
for j,k in ipairs(node.children) do
buff = buff .. k.writeTree(tabcount + 1);
end
buff = buff .. string.rep(" ", tabcount * 4) .. "</" .. node.name .. ">\n";
return buff;
end
return node;
end
function xmlCreateChildEx(parent, name)
local node = xmlCreateNodeEx(name);
node.parent = parent;
table.insert(parent.children, node);
return node;
end
function xmlDestroyNodeEx(node)
if (node.parent) then
table.delete(node.parent.children, node);
end
return true;
end
-- Pull xml node into table
function xmlGetNode(pXml, parent)
local pBuff;
local m,n;
local attribs;
if not (pXml) then return false; end;
if (parent) then
pBuff = xmlCreateChildEx(parent, xmlNodeGetName(pXml));
else
pBuff = xmlCreateNodeEx(xmlNodeGetName(pXml));
end
attribs=xmlNodeGetAttributes(pXml);
for m,n in pairs(attribs) do
pBuff.attr[m]=n;
end
local children=xmlNodeGetChildren(pXml);
for m,n in ipairs(children) do
xmlGetNode(n, pBuff);
end
return pBuff;
end
-- Save the node from table
function xmlSetNode(pXml, content)
local m,n;
local children = xmlNodeGetChildren(pXml);
local attributes = xmlNodeGetAttributes(pXml);
-- Destroy previous children
for m,n in ipairs(children) do
xmlDestroyNode(n);
end
-- Unset previous attributes
for m,n in pairs(attributes) do
xmlNodeSetAttribute(pXml, m, nil);
end
xmlNodeSetName(pXml, content.name);
for m,n in pairs(content.attr) do
xmlNodeSetAttribute(pXml, m, n);
end
for m,n in ipairs(content.children) do
xmlSetNode(xmlCreateChild(pXml, n.name), n);
end
return true;
end
function xmlFindSubNodeEx(node, name)
local m,n;
for m,n in ipairs(node.children) do
if (n.name == name) then
return n;
end
end
return false;
end
function findCreateNode(node, name)
local found = xmlFindSubNodeEx(node, name);
if not (found) then
found = xmlCreateNodeEx(name);
table.insert(node.children, found);
end
return found;
end