-
Notifications
You must be signed in to change notification settings - Fork 1
/
LuaUtils.lua
575 lines (502 loc) · 13.1 KB
/
LuaUtils.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
--[[ Made By Folfy Blue
Wiki: https://github.com/FolfyBlue/Utils/wiki (If the Wiki is not updated use the README.MD)
Source: https://github.com/FolfyBlue/Utils
]]
local chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
string.encode = {}
string.decode = {}
console = {}
--took that from http://lua-users.org/wiki/FunctionalLibrary !
operator = {
pow = math.pow;
add = function(n, m) return n + m end;
sub = function(n, m) return n - m end;
mul = function(n, m) return n * m end;
div = function(n, m) return n / m end;
gt = function(n, m) return n > m end;
lt = function(n, m) return n < m end;
eq = function(n, m) return n == m end;
le = function(n, m) return n <= m end;
ge = function(n, m) return n >= m end;
ne = function(n, m) return n ~= m end;
}
--
local morse = {
["a"] = ".-",
["b"] = "-...",
["c"] = "-.-.",
["d"] = "-..",
["e"] = ".",
["f"] = "..-.",
["g"] = "--.",
["h"] = "....",
["i"] = "..",
["j"] = ".---",
["k"] = "-.-",
["l"] = ".-..",
["m"] = "--",
["n"] = "-.",
["o"] = "---",
["p"] = ".--.",
["q"] = "--.-",
["r"] = ".-.",
["s"] = "...",
["t"] = "-",
["u"] = "..-",
["v"] = "...-",
["w"] = ".--",
["x"] = "-..-",
["y"] = "-.--",
["z"] = "--..",
["0"] = "-----",
["1"] = ".----",
["2"] = "..---",
["3"] = "...--",
["4"] = "....-",
["5"] = ".....",
["6"] = "-....",
["7"] = "--...",
["8"] = "---..",
["9"] = "----.",
['"'] = "--..--",
["?"] = "..--..",
["'"] = ".----.",
["!"] = "-.-.--",
["/"] = "-..-.",
["("] = "-.--.",
[")"] = "-.--.-",
["&"] = ".-...",
[":"] = "---...",
[";"] = "-.-.-.",
["="] = "-...-",
["+"] = ".-.-.",
["_"] = "..--.-",
['"'] = ".-..-.",
["&"] = "...-..-",
["@"] = ".--.-."
}
--------------------------------------------------------------------------
-- [-- OTHERS --]
--------------------------------------------------------------------------
function mix(unknown)
local n = false
if type(unknown) == "number" then
unknown = tostring(unknown)
n = true
end -- set the int as a string
local out = {}
for i = 1, #unknown do
table.insert(out,unknown:sub(i, i)) -- making each char as a string
end
for i = 1, #unknown do
local a = math.random(1, #unknown)
local b = math.random(1, #unknown) -- makin random numbers
local atemp = out[a]
out[a] = out[b] -- mixing tables
out[b] = atemp -- mixing tables
end
local out = table.concat(out) -- making tables ints
if n then
out = tonumber(out) -- if it was a int make the int gr8 again
end
return out
end
function isType(obj,typecheck)
return type(obj) == typecheck
end
function sleep(s)
local t = os.clock() + s
repeat until os.clock() >= t -- hand made, but works very well!
end
----------------------------------------------------------------------
-- [-- OS --]
----------------------------------------------------------------------
function os.splitpath(s)
return s:match(s, "(.-)([^\\]-([^%.]+))$") -- returns C:\Path\, directory.ext, ext
end
function os.getOS() -- the good ol' trick
if package.config:sub(1, 1) == '\\' then
return 'windows'
elseif package.config:sub(1, 1) == '/' then
return 'unix'
else
return 'unknown'
end
end
function os.getArch()
return (#tostring({})-7)*4 -- Another good ol' trick
end
function os.clear()
if not os.execute("clear") and not os.execute("cls") then -- if not linux/windows
for i = 1,25 do -- print many \n's
print("\n\n")
end
end
end
function os.outputexec(...)
return io.popen(table.concat({...},' ')):read("*a") -- get the output of a system cmd
end
function os.find(file,path)
local os = os.getOS:lower()
local cmd
if os == "windows" then
cmd = "dir"
elseif os == "linux" or os == "mac" or os == "macos"
then
cmd = "ls"
else
error("Invalid OS!")
end
if not file then
error("Can't find \"nil\"","git gud"
,"What did you expect me to say? like searching NOT" ..
"HING, NIL, NULL, EMPTY, etc.. :<")
end
local f = io.popen(cmd .. " " .. path)
if string.find(f:read("*a"), file) then -- if the file / folder is find with the cmd above
return true
else
return false
end
end
---------------------------------------------------------------------------
-- [-- STRINGS --]
---------------------------------------------------------------------------
function string.random(count,min,max)
if not count then
count = 1
end
if not max then
max = 255
end
if not min then
min = 1
end
if max > 255 or min < 1 or max < 1 or min > 255 then
error("Synthax error: Select a number between 1-255.")
elseif max < min then
local tmin = min
max = min
max = tmin
end
local rds = ""
for i = 1,count do
rds = rds .. string.char(math.random(min,max)) -- string.char for count time and with the limit of min and max
end
return rds
end
function string.decode.hex(str)
return (str:gsub('..', function (cc)
return string.char(tonumber(cc, 16))
end))
end
function string.encode.hex(str)
return (str:gsub('.', function (c)
return string.format('%02X', string.byte(c))
end))
end
function string.split(str,split)
if not str then
error("Can't split with nil","Hey have you tried splitting " ..
"air? Spoiler: it dont work") --What did you expected me to say? :p
end
local array = {}
for w in (str .. split):gmatch("([^" .. split .. "]*)" .. split) do -- matchin' stuff
table.insert(array, w) -- insert the matchin' stuff in a table
end
return array
end
function string.startswith(str, ptrn)
return string.find(str, ptrn, 1) == 1
end
function string.endswith(str, ptrn)
local s = #str + 1 - #ptrn
return string.find(str, ptrn, s) == s
end
------B64 from http://lua-users.org/wiki/BaseSixtyFour--------------
--I just edited the function names.
-- Lua 5.1+ base64 v3.0 (c) 2009 by Alex Kloss <[email protected]>
-- licensed under the terms of the LGPL2
function string.encode.b64(data)
return ((data:gsub('.', function(x)
local r,b='',x:byte()
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+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
return chars:sub(c+1,c+1)
end)..({ '', '==', '=' })[#data%3+1])
end
function string.decode.b64(data)
data = string.gsub(data, '[^'..chars..'=]', '')
return (data:gsub('.', function(x)
if (x == '=') then return '' end
local r,f='',(char:find(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+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
return string.char(c)
end))
end
--------------------------------------------------------------------
string.mix = mix
function string.totable(str)
return string.split(str,",")
end
----------------------------------------------------------------------
-- [-- IO --]
----------------------------------------------------------------------
function io.readfile(path)
local file = open(path, "rb") --open the file
if not file then return nil end
local content = file:read "*a" -- read the file and store the content
file:close() -- close the file
return content -- return the content
end
function io.store(file, data, nl)
if not file or not data then
return false
end
if not nl then
nl = ""
else
nl = "\n"
end
-------------------------------------------------------------------------
-- [-- TABLE --]
-------------------------------------------------------------------------
function table.list(t) -- no need to explain
local tstr = ""
local c = 0
for k,v in pairs(t) do
c = c+1
tstr = tstr .. k .. " = " .. v .. ",\n"
end
return tstr:sub(0,#tstr-2), c
end
function table.find(t, value)
local c = 0
for k, v in pairs(t) do
c = c+1
if value == v then -- omygud it match
return v, c
end
end
return nil, -1
end
function table.head(t)
return t[1]
end
function table.tail(t)
local nt = {}
local _,ts = table.list(t)
if ts < 1 then
return {}
end
i = 2
while (i <= ts) do
table.insert(nt, (i - 1), t[i]) -- make a new table with the head remov'd
i = i + 1
end
return nt
end
function table.reverse(t)
local nt = {}
for i = 1,#t do
nt[i] = t[#t+1-i]
end
return nt
end
function table.to2D(arg)
local arg = tostring(arg)
local t = {}
local line = {}
for i = 1, #arg do
local c = arg:sub(i,i) -- take one char
if c == "\n" then -- make a new table if the char is a new space
line = {}
table.insert(t, line)
else -- else insert the char to the current table
table.insert(line, c)
end
end
return t
end
function table.toString(t)
local a = table.concat(t,", ") -- do i even need to comment that?
return "{" .. a .. "}"
end
function table.merge(t1, t2)
if not t1 then -- this piece of code is horrible, but it works
return false
end
if not t2 then
return t1
end
local t = {}
for _,a,b,c in pairs(t1) do
if a and b and c then
table.insert(t, a)
table.insert(t, b)
table.insert(t, c)
elseif a and b then
table.insert(t, a)
table.insert(t, b)
else
table.insert(t, a)
end
end
for _, a, b, c in pairs(t2) do
if a and b and c then
table.insert(t, a)
table.insert(t, b)
table.insert(t, c)
elseif a and b then
table.insert(t, a)
table.insert(t, b)
else
table.insert(t, a)
end
end
return t
end
---------------------------------------------------------------------------
-- [-- CONSOLE --]
---------------------------------------------------------------------------
function error(type,msg,other)
if other ~= nil then --Check if other an argument
other = "Other informations: " .. other .. "\n" -- if so, add it
else
other = ""
end
if msg ~= nil then -- check if there's an error message
msg = "\nDetailed message: " .. msg .. "\n" -- if so, add it
else
msg = ""
end
if not type then --check if the type is a give argument
type = "Unknown" --if not, make it unknown
end
error(os.date("\n[%x|%X]") .. " An error occured !\nType : " .. type .. msg
.. other) -- error it !
end
function console.slowPrint(str)
local str = tostring(str)
if not type(str) == 'string' then
return nil
end
local n = 0
for i = 1,#str do
n = n + 1
sleep(0.05) -- uses the sleep function to wait before io.write-ting
io.write(str:sub(n, n))
end
print() -- new space
end
function console.slowWrite(str) -- same as above without the newspace
local str = tostring(str)
if not type(str) == 'string' then
return nil
end
local n = 0
for i = 1,#str do
n = n + 1
sleep(0.05)
io.write(str:sub(n, n))
end
end
local file = io.open(file, "a+") -- oppens the file in append mode +
file:write(data, nl) -- write stuff
file:close() -- close stuff
return true
end
function console.log(type,...)
if not type then
type = "INFO" -- probably what ppls do when they set it to false
end
print(os.date("[%x|%X] [" .. type:upper() .. "]"),...) -- prints it to the console/terminal
end
function console.update(slow,...)
local args = {...}
os.clear()
local vprint
if slow then
vprint = console.slowPrint
else
vprint = print
end
for i = 1,#args do
vprint(args[i])
end
end
-----------------------------------------------------------------------
-- [-- MATH --]
-----------------------------------------------------------------------
function math.calc(arg)
if not arg then return end
arg = arg:lower():gsub("function",""):gsub("end",""):gsub("[\"\'[]",""):gsub("rep","") --make sure peoples don't try to break it
arg = "return (" .. arg .. ")"
local sandbox = setmetatable({}, {__index = math}) -- make a sandbox
local fn, syntaxError = load(arg, 'Calc', 't', sandbox) -- execute the "code"
if not fn then return syntaxError end
local success, result = pcall(fn)
if not success then return result end
return result
end
math.mix = mix
--[[return {
---------------console
console,
console.log,
console.slowPrint,
console.slowWrite,
error,
console.update,
---------------io
io.readfile,
io.store,
---------------string
string.random,
string.split,
string.mix,
string.startswith,
string.endswith,
string.encode,
string.decode,
string.encode.hex,
string.decode.hex,
string.encode.b64,
string.decode.b64,
string.totable,
---------------os
os.find,
os.clear,
os.getOS,
os.getArchos,
os.outputexec,
os.splitpath,
---------------table
table.merge,
table.to2D,
table.toString,
table.list,
table.head,
table.tail,
table.find,
table.reverse,
---------------math
math.mix,
---------------Others
sleep,
operator,
mix,
morse,
chars,
dostring,
isType
}]]