forked from IgorTimofeev/GUI
-
Notifications
You must be signed in to change notification settings - Fork 3
/
advancedLua.lua
586 lines (475 loc) · 17.8 KB
/
advancedLua.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
local filesystem = require("filesystem")
local unicode = require("unicode")
local bit32 = require("bit32")
----------------------------------------------------------------------------------------------------
function _G.getCurrentScript()
local info
for runLevel = 0, math.huge do
info = debug.getinfo(runLevel)
if info then
if info.what == "main" then
return info.source:sub(2, -1)
end
else
error("Failed to get debug info for runlevel " .. runLevel)
end
end
end
----------------------------------------------------------------------------------------------------
function bit32.merge(number2, number1)
local cutter = math.ceil(math.log(number1 + 1, 256)) * 8
while number2 > 0 do
number1, number2, cutter = bit32.bor(bit32.lshift(bit32.band(number2, 0xFF), cutter), number1), bit32.rshift(number2, 8), cutter + 8
end
return number1
end
function bit32.numberToByteArray(number)
local byteArray = {}
repeat
table.insert(byteArray, 1, bit32.band(number, 0xFF))
number = bit32.rshift(number, 8)
until number <= 0
return byteArray
end
function bit32.numberToFixedSizeByteArray(number, size)
local byteArray, counter = {}, 0
repeat
table.insert(byteArray, 1, bit32.band(number, 0xFF))
number = bit32.rshift(number, 8)
counter = counter + 1
until number <= 0
for i = 1, size - counter do
table.insert(byteArray, 1, 0x0)
end
return byteArray
end
function bit32.byteArrayToNumber(byteArray)
local result = byteArray[1]
for i = 2, #byteArray do
result = bit32.bor(bit32.lshift(result, 8), byteArray[i])
end
return result
end
function bit32.bitArrayToByte(bitArray)
local result = 0
for i = 1, #bitArray do
result = bit32.bor(bitArray[i], bit32.lshift(result, 1))
end
return result
end
----------------------------------------------------------------------------------------------------
function math.round(num)
if num >= 0 then
return math.floor(num + 0.5)
else
return math.ceil(num - 0.5)
end
end
function math.roundToDecimalPlaces(num, decimalPlaces)
local mult = 10 ^ (decimalPlaces or 0)
return math.round(num * mult) / mult
end
function math.getDigitCount(num)
return num == 0 and 1 or math.ceil(math.log(num + 1, 10))
end
function math.shorten(number, digitCount)
local shortcuts = {
"K",
"M",
"B",
"T"
}
local index = math.floor(math.log(number, 1000))
if number < 1000 then
return number
elseif index > #shortcuts then
index = #shortcuts
end
return math.roundToDecimalPlaces(number / 1000 ^ index, digitCount) .. shortcuts[index]
end
----------------------------------------------------------------------------------------------------
-- function filesystem.path(path)
-- return path:match("^(.+%/).") or ""
-- end
-- function filesystem.name(path)
-- return path:match("%/?([^%/]+)%/?$")
-- end
function filesystem.extension(path, lower)
local extension = path:match("[^%/]+(%.[^%/]+)%/?$")
return (lower and extension) and (unicode.lower(extension)) or extension
end
function filesystem.hideExtension(path)
return path:match("(.+)%..+") or path
end
function filesystem.isFileHidden(path)
if path:match("^%..+$") then
return true
end
return false
end
function filesystem.sortedList(path, sortingMethod, showHiddenFiles, filenameMatcher, filenameMatcherCaseSensitive)
if not filesystem.exists(path) then
error("Failed to get file list: directory \"" .. tostring(path) .. "\" doesn't exists")
end
if not filesystem.isDirectory(path) then
error("Failed to get file list: path \"" .. tostring(path) .. "\" is not a directory")
end
local fileList, sortedFileList = {}, {}
for file in filesystem.list(path) do
if not filenameMatcher or string.unicodeFind(filenameMatcherCaseSensitive and file or unicode.lower(file), filenameMatcherCaseSensitive and filenameMatcher or unicode.lower(filenameMatcher)) then
table.insert(fileList, file)
end
end
if #fileList > 0 then
if sortingMethod == "type" then
local extension
for i = 1, #fileList do
extension = filesystem.extension(fileList[i]) or "Script"
if filesystem.isDirectory(path .. fileList[i]) and extension ~= ".app" then
extension = ".01_Folder"
end
fileList[i] = { fileList[i], extension }
end
table.sort(fileList, function(a, b) return unicode.lower(a[2]) < unicode.lower(b[2]) end)
local currentExtensionList, currentExtension = {}, fileList[1][2]
for i = 1, #fileList do
if currentExtension == fileList[i][2] then
table.insert(currentExtensionList, fileList[i][1])
else
table.sort(currentExtensionList, function(a, b) return unicode.lower(a) < unicode.lower(b) end)
for j = 1, #currentExtensionList do
table.insert(sortedFileList, currentExtensionList[j])
end
currentExtensionList, currentExtension = { fileList[i][1] }, fileList[i][2]
end
end
table.sort(currentExtensionList, function(a, b) return unicode.lower(a) < unicode.lower(b) end)
for j = 1, #currentExtensionList do
table.insert(sortedFileList, currentExtensionList[j])
end
elseif sortingMethod == "name" then
sortedFileList = fileList
table.sort(sortedFileList, function(a, b) return unicode.lower(a) < unicode.lower(b) end)
elseif sortingMethod == "date" then
for i = 1, #fileList do
fileList[i] = { fileList[i], filesystem.lastModified(path .. fileList[i]) }
end
table.sort(fileList, function(a, b) return unicode.lower(a[2]) > unicode.lower(b[2]) end)
for i = 1, #fileList do
table.insert(sortedFileList, fileList[i][1])
end
else
error("Unknown sorting method: " .. tostring(sortingMethod))
end
local i = 1
while i <= #sortedFileList do
if not showHiddenFiles and filesystem.isFileHidden(sortedFileList[i]) then
table.remove(sortedFileList, i)
else
i = i + 1
end
end
end
return sortedFileList
end
function filesystem.directorySize(path)
local size = 0
for file in filesystem.list(path) do
if filesystem.isDirectory(path .. file) then
size = size + filesystem.directorySize(path .. file)
else
size = size + filesystem.size(path .. file)
end
end
return size
end
function filesystem.readUnicodeChar(file)
local byteArray = { string.byte(file:read(1)) }
local nullBitPosition = 0
for i = 1, 7 do
if bit32.band(bit32.rshift(byteArray[1], 8 - i), 0x1) == 0x0 then
nullBitPosition = i
break
end
end
for i = 1, nullBitPosition - 2 do
table.insert(byteArray, string.byte(file:read(1)))
end
return string.char(table.unpack(byteArray))
end
----------------------------------------------------------------------------------------------------
function table.serialize(array, prettyLook, indentationWidth, indentUsingTabs, recursionStackLimit)
checkArg(1, array, "table")
recursionStackLimit = recursionStackLimit or math.huge
local indentationSymbolAdder = string.rep(indentUsingTabs and " " or " ", indentationWidth or 2)
local equalsSymbol = prettyLook and " = " or "="
local function serializeRecursively(array, currentIndentationSymbol, currentRecusrionStack)
local result, nextIndentationSymbol, keyType, valueType, stringValue = { "{" }, currentIndentationSymbol .. indentationSymbolAdder
if prettyLook then
table.insert(result, "\n")
end
for key, value in pairs(array) do
keyType, valueType, stringValue = type(key), type(value), tostring(value)
if prettyLook then
table.insert(result, nextIndentationSymbol)
end
if keyType == "number" then
table.insert(result, "[")
table.insert(result, key)
table.insert(result, "]")
table.insert(result, equalsSymbol)
elseif keyType == "string" then
-- Короч, если типа начинается с буковки, а также если это алфавитно-нумерическая поеботня
if prettyLook and key:match("^%a") and key:match("^[%w%_]+$") then
table.insert(result, key)
else
table.insert(result, "[\"")
table.insert(result, key)
table.insert(result, "\"]")
end
table.insert(result, equalsSymbol)
end
if valueType == "number" or valueType == "boolean" or valueType == "nil" then
table.insert(result, stringValue)
elseif valueType == "string" or valueType == "function" then
table.insert(result, "\"")
table.insert(result, stringValue)
table.insert(result, "\"")
elseif valueType == "table" then
if currentRecusrionStack < recursionStackLimit then
table.insert(
result,
table.concat(
serializeRecursively(
value,
nextIndentationSymbol,
currentRecusrionStack + 1
)
)
)
else
table.insert(result, "\"…\"")
end
end
table.insert(result, ",")
if prettyLook then
table.insert(result, "\n")
end
end
-- Удаляем запятую
if prettyLook then
if #result > 2 then
table.remove(result, #result - 1)
end
table.insert(result, currentIndentationSymbol)
else
if #result > 1 then
table.remove(result, #result)
end
end
table.insert(result, "}")
return result
end
return table.concat(serializeRecursively(array, "", 1))
end
function table.unserialize(serializedString)
checkArg(1, serializedString, "string")
local result, reason = load("return " .. serializedString)
if result then
result, reason = pcall(result)
if result then
return reason
else
return nil, reason
end
else
return nil, reason
end
end
table.toString = table.serialize
table.fromString = table.unserialize
function table.toFile(path, array, prettyLook, indentationWidth, indentUsingTabs, recursionStackLimit, appendToFile)
checkArg(1, path, "string")
checkArg(2, array, "table")
filesystem.makeDirectory(filesystem.path(path) or "")
local file, reason = io.open(path, appendToFile and "a" or "w")
if file then
file:write(table.serialize(array, prettyLook, indentationWidth, indentUsingTabs, recursionStackLimit))
file:close()
else
error("Failed to open file for writing: " .. tostring(reason))
end
end
function table.fromFile(path)
checkArg(1, path, "string")
local file, reason = io.open(path, "r")
if file then
local data, reason = table.unserialize(file:read("*a"))
file:close()
if data then
return data
else
error("Failed to unserialize file \"" .. path .. "\": " .. tostring(reason))
end
else
error("Failed to open file \"" .. path .. "\" for reading: " .. tostring(reason))
end
end
function table.copy(tableToCopy)
local function copyTableRecursively(source, destination)
for key, value in pairs(source) do
if type(value) == "table" then
destination[key] = {}
doTableCopy(source[key], destination[key])
else
destination[key] = value
end
end
end
local result = {}
copyTableRecursively(tableToCopy, result)
return result
end
function table.size(t)
local size = 0
for key in pairs(t) do size = size + 1 end
return size
end
function table.contains(t, object)
for _, value in pairs(t) do
if value == object then
return true
end
end
return false
end
function table.indexOf(t, object)
for i = 1, #t do
if t[i] == object then
return i
end
end
end
function table.sortAlphabetically(t)
table.sort(t, function(a, b) return a < b end)
end
----------------------------------------------------------------------------------------------------
function string.brailleChar(a, b, c, d, e, f, g, h)
return unicode.char(10240 + 128 * h + 64 * g + 32 * f + 16 * d + 8 * b + 4 * e + 2 * c + a)
end
function string.canonicalPath(str)
return string.gsub("/" .. str, "%/+", "/")
end
function string.optimize(str, indentationWidth)
str = string.gsub(str, "\r\n", "\n")
str = string.gsub(str, " ", string.rep(" ", indentationWidth or 2))
return str
end
function string.optimizeForURLRequests(code)
if code then
code = string.gsub(code, "([^%w ])", function(c)
return string.format("%%%02X", string.byte(c))
end)
code = string.gsub(code, " ", "+")
end
return code
end
function string.unicodeFind(str, pattern, init, plain)
if init then
if init < 0 then
init = -#unicode.sub(str, init)
elseif init > 0 then
init = #unicode.sub(str, 1, init - 1) + 1
end
end
a, b = string.find(str, pattern, init, plain)
if a then
local ap, bp = str:sub(1, a - 1), str:sub(a, b)
a = unicode.len(ap) + 1
b = a + unicode.len(bp) - 1
return a, b
else
return a
end
end
function string.limit(s, limit, mode, noDots)
local length = unicode.len(s)
if length <= limit then return s end
if mode == "left" then
if noDots then
return unicode.sub(s, length - limit + 1, -1)
else
return "…" .. unicode.sub(s, length - limit + 2, -1)
end
elseif mode == "center" then
local integer, fractional = math.modf(limit / 2)
if fractional == 0 then
return unicode.sub(s, 1, integer) .. "…" .. unicode.sub(s, -integer + 1, -1)
else
return unicode.sub(s, 1, integer) .. "…" .. unicode.sub(s, -integer, -1)
end
else
if noDots then
return unicode.sub(s, 1, limit)
else
return unicode.sub(s, 1, limit - 1) .. "…"
end
end
end
function string.wrap(data, limit)
if type(data) == "string" then data = { data } end
local wrappedLines, result, preResult, position = {}
-- Дублируем таблицу строк, шоб не перекосоебить ченить переносами
for i = 1, #data do
wrappedLines[i] = data[i]
end
-- Отсечение возврата каретки-ебуретки
local i = 1
while i <= #wrappedLines do
local position = string.unicodeFind(wrappedLines[i], "\n")
if position then
table.insert(wrappedLines, i + 1, unicode.sub(wrappedLines[i], position + 1, -1))
wrappedLines[i] = unicode.sub(wrappedLines[i], 1, position - 1)
end
i = i + 1
end
-- Сам перенос
local i = 1
while i <= #wrappedLines do
result = ""
for word in wrappedLines[i]:gmatch("[^%s]+") do
preResult = result .. word
if unicode.len(preResult) > limit then
if unicode.len(word) > limit then
table.insert(wrappedLines, i + 1, unicode.sub(wrappedLines[i], limit + 1, -1))
result = unicode.sub(wrappedLines[i], 1, limit)
else
table.insert(wrappedLines, i + 1, unicode.sub(wrappedLines[i], unicode.len(result) + 1, -1))
end
break
else
result = preResult .. " "
end
end
wrappedLines[i] = result:gsub("%s+$", ""):gsub("^%s+", "")
i = i + 1
end
return wrappedLines
end
----------------------------------------------------------------------------------------------------
local keyboard = require("keyboard")
function keyboard.isCommandDown()
return keyboard.pressedCodes[219]
end
----------------------------------------------------------------------------------------------------
local event = require("event")
local computer = require("computer")
-- Sleeps "time" of seconds via "busy-wait" concept
function event.sleep(time)
checkArg(1, time, "number", "nil")
local deadline = computer.uptime() + (time or 0)
repeat
event.pull(deadline - computer.uptime())
until computer.uptime() >= deadline
end
return { loaded = true }