-
Notifications
You must be signed in to change notification settings - Fork 2
/
essentials.lua
719 lines (604 loc) · 14.7 KB
/
essentials.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
711
712
713
714
715
716
717
718
719
local essentials = {
_VERSION = "essentials.lua v1.1.0",
_DESCRIPTION = "Extends the standard Lua API with helpful functions.",
_URL = "https://github.com/luastoned/lua-essentials",
_LICENSE = [[Copyright (c) 2017 @LuaStoned]]
}
-- http://www.emoji-cheat-sheet.com/
--------------------------------------------------
-- Local Helpers
--------------------------------------------------
local function patternEscape(str)
return string.gsub(str, "[%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%0")
--return string.gsub(str, "[%-%.%+%[%]%(%)%$%^%%%?%*]", "%%%1")
--return string.gsub(str, "[%-%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%1")
end
local patternReplacements = {
["("] = "%(",
[")"] = "%)",
["."] = "%.",
["%"] = "%%",
["+"] = "%+",
["-"] = "%-",
["*"] = "%*",
["?"] = "%?",
["["] = "%[",
["]"] = "%]",
["^"] = "%^",
["$"] = "%$",
["\0"] = "%z",
}
local function patternSafe(str)
return string.gsub(str, ".", patternReplacements)
end
local function expandTab(str, num)
return string.gsub(str, "([^\t]*)\t", function(s)
return s .. string.rep(" ", num - #s % num)
end)
end
local function expandTabs(str, num)
num = num or 8
if not string.find(str, "\n") then
return expandTab(str, num)
end
local res = {}
for k, line in pairs(string.split(str, "\n")) do
res[k] = expandTab(line, num)
end
return table.concat(res, "\n")
end
local function getMetaMethod(func, method)
local ok, m = pcall(function(x)
return getmetatable(x)[method]
end, func)
if (type(m) ~= "function") then
m = nil
end
return m
end
local function isCallable(func)
if (type(func) == "function") then
return func
end
return getMetaMethod(func, "__call")
end
--------------------------------------------------
-- Global Defines and Functions
--------------------------------------------------
include = dofile
function printf(fmt, ...)
io.write(string.format(fmt, ...))
end
function runString(str)
local runStr = rawget(_G, "loadstring") or load
local ok, err = pcall(runStr(str))
if (not ok) then
print(err)
end
return ok
end
function eval(str)
local runStr = rawget(_G, "loadstring") or load
return runStr("return " .. str)()
end
function profile(func, ...)
local t = os.clock()
func(...)
return os.clock() - t
end
function clear()
os.execute("cls")
end
function get()
return io.stdin:read()
end
function lerp(delta, from, to)
if (delta > 1) then return to end
if (delta < 0) then return from end
return from + (to - from) * delta
end
--------------------------------------------------
-- Meta Additions aka Magic
--------------------------------------------------
-- http://www.lua.org/manual/5.1/manual.html#2.8
local stringMeta = getmetatable("")
stringMeta.__index = function(self, key)
if (string[key]) then
return string[key]
elseif (tonumber(key)) then
return string.sub(self, key, key)
else
error("attempt to index a string value with bad key ('" .. tostring(key) .. "' is not part of the string library)", 2)
end
end
stringMeta.__mul = function(self, num)
return string.rep(self, num)
end
stringMeta.__mod = function(self, arg)
if (type(arg) == "string") then
return string.format(self, arg)
else
return string.format(self, unpack(arg))
end
end
--------------------------------------------------
-- Debug
--------------------------------------------------
-- http://leafo.net/guides/dynamic-scoping-in-lua.html
function debug.dynamic(name)
local level = 2
-- iterate over
while true do
local i = 1
-- iterate over each local by index
while true do
local found_name, found_val = debug.getlocal(level, i)
if not found_name then break end
if found_name == name then
return found_val
end
i = i + 1
end
level = level + 1
end
end
-- http://www.facepunch.com/showthread.php?884409-debug.getparams-Get-the-names-of-a-function-s-arguments
function debug.getparams(func)
local co = coroutine.create(func)
local params = {}
debug.sethook(co, function(event, line)
local i, k, v = 1, debug.getlocal(co, 2, 1)
while k do
if k ~= "(*temporary)" then
table.insert(params, k)
end
i = i + 1
k, v = debug.getlocal(co, 2, i)
end
coroutine.yield()
end, "c")
local res = coroutine.resume(co)
return params
end
function debug.getparamnames(func)
local func_paramInfoDict = debug.getinfo(func, 'u')
local func_paramCount = func_paramInfoDict.nparams
local func_isVarArg = func_paramInfoDict.isvararg
local paramNameList = {}
for i = 1, func_paramCount do
local param_name, param_value = debug.getlocal(func, i)
paramNameList[i] = param_name
end
return paramNameList, func_isVarArg
end
-- Argument Check
local prevFunc, prevLine, curArg = "", 0, 0
local function requireArg(arg, expected)
local info = debug.getinfo(2)
if (prevLine >= info.currentline or prevFunc ~= info.name) then
curArg = 0
end
curArg = curArg + 1
prevFunc = info.name
prevLine = info.currentline
assert(type(arg) == expected, string.format("bad argument #%d to '%s' (%s expected, got %s)", curArg, info.name, expected, type(arg)))
end
--------------------------------------------------
-- File
--------------------------------------------------
-- If it doesn't already exist create a global file table
file = file or {}
file.delete = os.remove
file.rename = os.rename
file.fileSeperator = string.sub(package.config, 1, 1)
file.fileSeperator = string.match(package.config, "^(%S+)\n")
function file.catFile(...)
return table.concat({...}, file.fileSeperator)
end
function file.read(str, mode)
local fh = io.open(str, mode or "r")
if (fh == nil) then
return ""
end
str = fh:read("*a")
fh:close()
return str
end
function file.write(str, src, mode)
local fh = io.open(str, mode or "w")
if (fh == nil) then
return false
end
fh:write(src or "")
fh:close()
return true
end
function file.append(str, src, mode)
local fh = io.open(str, mode or "a+")
if (fh == nil) then
return false
end
fh:write(src or "")
fh:close()
return true
end
--------------------------------------------------
-- Math
--------------------------------------------------
local hex2bin = {
["0"] = "0000",
["1"] = "0001",
["2"] = "0010",
["3"] = "0011",
["4"] = "0100",
["5"] = "0101",
["6"] = "0110",
["7"] = "0111",
["8"] = "1000",
["9"] = "1001",
["a"] = "1010",
["b"] = "1011",
["c"] = "1100",
["d"] = "1101",
["e"] = "1110",
["f"] = "1111",
}
local bin2hex = {
["0000"] = "0",
["0001"] = "1",
["0010"] = "2",
["0011"] = "3",
["0100"] = "4",
["0101"] = "5",
["0110"] = "6",
["0111"] = "7",
["1000"] = "8",
["1001"] = "9",
["1010"] = "A",
["1011"] = "B",
["1100"] = "C",
["1101"] = "D",
["1110"] = "E",
["1111"] = "F",
}
function math.hex2bin(s)
-- s -> hexadecimal string
local ret = ""
local i = 0
for i in string.gfind(s, ".") do
i = string.lower(i)
ret = ret .. hex2bin[i]
end
return ret
end
function math.bin2hex(s)
-- s -> binary string
local l = 0
local h = ""
local b = ""
local rem
l = string.len(s)
rem = l % 4
l = l - 1
h = ""
-- need to prepend zeros to eliminate mod 4
if (rem > 0) then
s = string.rep("0", 4 - rem) .. s
end
for i = 1, l, 4 do
b = string.sub(s, i, i + 3)
h = h..bin2hex[b]
end
return h
end
function math.bin2dec(s)
-- return tonumber(bin,2)
-- s -> binary string
local num = 0
local ex = string.len(s) - 1
local l = 0
l = ex + 1
for i = 1, l do
b = string.sub(s, i, i)
if b == "1" then
num = num + 2 ^ ex
end
ex = ex - 1
end
return string.format("%u", num)
end
function math.dec2bin(s, num)
-- s -> Base10 string
-- num -> string length to extend to
--[[
local str = string.format("%o",int)
local a = {
["0"]="000",["1"]="001", ["2"]="010",["3"]="011",
["4"]="100",["5"]="101", ["6"]="110",["7"]="111"
}
local bin = string.gsub( str, "(.)", function ( d ) return a[ d ] end )
return bin
]]
local n = num or 0
s = string.format("%x", s)
s = math.hex2bin(s)
while string.len(s) < n do
s = "0" .. s
end
return s
end
function math.hex2dec(s)
-- s -> hexadecimal string
local s = math.hex2bin(s)
return math.bin2dec(s)
end
function math.dec2hex(s)
-- s -> Base10 string
s = string.format("%x", s)
return s
end
function math.clamp(num, low, high)
if (num < low) then return low end
if (num > high) then return high end
return num
--return math.min(high, math.max(num, low))
end
function math.round(num, idp)
local e = 10^(idp or 0)
return math.floor(num * e + 0.5) / e
end
--------------------------------------------------
-- String
--------------------------------------------------
-- character table string
local base64table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
function string.decodeBase64(str)
str = string.gsub(str, "[^" .. base64table .. "=]", "")
return (string.gsub(str, ".", function(x)
if (x == "=") then return "" end
local r, f = "", (string.find(base64table, x) - 1)
for i = 6, 1, -1 do
r = r .. (f % 2 ^ i - f % 2 ^ (i - 1) > 0 and "1" or "0")
end
return r
end):gsub("%d%d%d?%d?%d?%d?%d?%d?", function(x)
if (#x ~= 8) then return "" end
local c = 0
for i=1, 8 do
c =c + (string.sub(x, i, i) == "1" and 2 ^ (8 - i) or 0)
end
return string.char(c)
end))
end
function string.encodeBase64(str)
return ((string.gsub(str, ".", function(x)
local r, b = "", string.byte(x)
for i = 8, 1, -1 do
r = r .. (b % 2 ^ i - b % 2 ^ (i - 1) > 0 and "1" or "0")
end
return r
end) .. "0000"):gsub("%d%d%d?%d?%d?%d?", function(x)
if (#x < 6) then return "" end
local c = 0
for i = 1, 6 do
c = c + (string.sub(x, i, i) == "1" and 2 ^ (6 - i) or 0)
end
return string.sub(base64table, c + 1, c + 1)
end) .. ({"", "==", "="})[#str % 3 + 1])
end
function string.getChar(str, pos)
return string.sub(str, pos, pos)
end
function string.setChar(str, pos, char)
local pre = string.sub(str, 0, pos - 1)
local post = string.sub(str, pos + 1)
return pre .. char .. post
end
function string.trim(str, char)
char = char and patternSafe(char) or "%s"
return string.match(str, "^" .. char .. "*(.-)" .. char .. "*$") or str
end
function string.left(str, num)
return string.sub(str, 1, num)
end
function string.right(str, num)
return string.sub(str, -num)
end
function string.replace(str, find, replace)
return string.gsub(str, patternSafe(find), string.gsub(replace, "%%", "%%%1"))
end
function string.split(str, separator, isPattern)
if (not separator or separator == "") then return string.toTable(str) end
local tbl = {}
local index, lastPosition = 1, 1
-- Escape all magic characters in separator
if not isPattern then separator = string.gsub(separator, "[%-%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%1") end
-- Find the parts
for startPosition, endPosition in string.gmatch(str, "()" .. separator .. "()") do
tbl[index] = string.sub(str, lastPosition, startPosition - 1)
index = index + 1
-- Keep track of the position
lastPosition = endPosition
end
-- Add last part by using the position we stored
tbl[index] = string.sub(str, lastPosition)
return tbl
end
function string.toTable(str, num)
num = num or 1
local tbl = {}
for i = 1, string.len(str), num do
table.insert(tbl, string.sub(str, i, i + num - 1))
end
return tbl
end
function string.fromHex(str, fmt)
return string.gsub(str, fmt or "..", function(num)
return string.char(tonumber(num, 16))
end)
end
function string.toHex(str, fmt)
return string.gsub(".", function(char)
return string.format(fmt or "%02X", string.byte(char))
end)
end
--------------------------------------------------
-- Table
--------------------------------------------------
function table.copy(tbl, lookupTbl)
local tblCopy = {}
setmetatable(tblCopy, getmetatable(tbl))
for k, v in pairs(tbl) do
if (type(v) ~= "table") then
tblCopy[k] = v
else
lookupTbl = lookupTbl or {}
lookupTbl[tbl] = tblCopy
if lookupTbl[v] then
tblCopy[k] = lookupTbl[v]
else
tblCopy[k] = table.copy(v, lookupTbl)
end
end
end
return tblCopy
end
function table.count(tbl)
local i = 0
for k in pairs(tbl) do i = i + 1 end
return i
end
function table.forEach(tbl, func)
for k, v in pairs(tbl) do
func(k, v)
end
end
function table.getFirstKey(tbl)
local k, v = next(tbl)
return k
end
function table.getFirstValue(tbl)
local k, v = next(tbl)
return v
end
function table.getLastKey(tbl)
local k, v = next(tbl, table.count(tbl) - 1)
return k
end
function table.getLastValue(tbl)
local k, v = next(tbl, table.count(tbl) - 1)
return v
end
function table.findNext(tbl, val)
local bFound = false
for k, v in pairs(tbl) do
if (bFound) then return v end
if (val == v) then bFound = true end
end
return table.getFirstValue(tbl)
end
function table.findPrev(tbl, val)
local last = table.getLastValue(tbl)
for k, v in pairs(tbl) do
if (val == v) then return last end
last = v
end
return last
end
function table.hasKey(tbl, key)
for k, v in pairs(tbl) do
if (k == key) then
return true
end
end
return false
end
function table.hasValue(tbl, val)
for k, v in pairs(tbl) do
if (v == val) then
return true
end
end
return false
end
function table.invert(tbl)
local inv = {}
for k, v in pars(tbl) do
inv[v] = k
end
return tbl
end
function table.isSequential(tbl)
local i = 1
for k, v in pairs(tbl) do
if not tonumber(i) or key ~= i then
return false
end
i = i + 1
end
return true
end
function table.merge(dest, source)
for k, v in pairs(source) do
if (type(v) == "table" and type(dest[k]) == "table") then
-- don't overwrite one table with another;
-- instead merge them recurisvely
table.merge(dest[k], v)
else
dest[k] = v
end
end
return dest
end
function table.toString(tbl, n, nice)
nice = nice or true
local nl, tab = "", ""
if nice then
nl, tab = "\n", "\t"
end
local function makeTable(tbl, nice, indent, done)
local str = ""
local done = done or {}
local indent = indent or 0
local isSequential = table.isSequential(tbl)
local idt = ""
if (nice) then
idt = string.rep("\t", indent)
end
for k, v in pairs(tbl) do
str = str .. idt .. tab
if (not isSequential) then
if type(k) == "number" or type(k) == "boolean" then
k = "[" .. tostring(k) .. "]" .. tab .. "="
else
k = tostring(k) .. tab .. "="
end
else
k = ""
end
if (type(v) == "table" and not done[v]) then
done [v] = true
str = str .. k .. tab .. "{" .. nl .. makeTable(v, nice, indent + 1, done)
str = str .. idt .. tab .. "}," .. nl
else
if (type(v) == "string") then
v = '"' .. tostring(v) .. '"'
else
v = tostring(v)
end
str = str .. k .. tab .. v .. "," .. nl
end
end
return str
end
local str = ""
if n then
str = n .. tab .. "=" .. tab
end
str = str .. "{" .. nl .. makeTable(tbl, nice) .. "}"
return str
end
function table.print(tbl)
print(table.toString(tbl))
end
return essentials