-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
362 lines (320 loc) · 7.32 KB
/
utils.lua
File metadata and controls
362 lines (320 loc) · 7.32 KB
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
--= Utils =--
versions = {
TCCAIC = "4",
Server = "4",
Client = {
Pirate = "3",
Pocket = "4",
},
}
table.isEmpty = function(t)
if t == nil then return false end
for i,v in pairs(t) do
if type(v) == "table" then
if table.isEmpty(v) == false then
return false
end
elseif v then
return false
end
end
for i=0,#t do
if type(t[i]) == "table" then
if table.isEmpty(t[i]) == false then
return false
end
elseif t[i] then
return false
end
end
return true
end
table.protect = function(t)
if not type(t) == "table" then
return
end
return setmetatable({}, {
__index = t,
__newindex = function(tbl, key, value)
error("cannot modify: table is protected")
end
})
end
table.copy = function(t)
if type(t) ~= "table" then return t end
--local meta = getmetatable(t)
local target = {}
for k, v in pairs(t) do
if type(v) == "table" then
target[k] = table.copy(v)
else
target[k] = v
end
end
--setmetatable(target, meta)
return target
end
table.isEqual = function(t1,t2,ignore_mt)
local ty1 = type(t1)
local ty2 = type(t2)
if ty1 ~= ty2 then return false end
-- non-table types can be directly compared
if ty1 ~= 'table' and ty2 ~= 'table' then return t1 == t2 end
-- as well as tables which have the metamethod __eq
local mt = getmetatable(t1)
if not ignore_mt and mt and mt.__eq then return t1 == t2 end
for k1,v1 in pairs(t1) do
local v2 = t2[k1]
if v2 == nil or not table.isEqual(v1,v2) then return false end
end
for k2,v2 in pairs(t2) do
local v1 = t1[k2]
if v1 == nil or not table.isEqual(v1,v2) then return false end
end
return true
end
table.tts = function(t,splitChar)
if type(t) ~= "table" then
return
end
local str = "nil"
local f = true
if splitChar == nil then
splitChar = " "
end
for i,v in pairs(t) do
if type(v) ~= "string" then
v = tostring(v):gsub(" ","") -- function: xxxx -> function:xxxx
end
if not f then
str = str..splitChar..v
else
str = v
f = false
end
end
return str
end
table.cut = function(t,startPos,endPos)
local res = {}
if endPos == nil then
endPos = table.getn(t)
end
if startPos == nil then
if endPos == table.getn(t) then
return t
else
startPos = 1
end
end
for k=startPos,endPos do
table.insert(res,t[k])
end
table.tts(res)
return res
end
table.getn = function(t)
if t == nil then return 0 end
local n = 0
for _,v in pairs(t) do
n = n + 1
end
return n
end
table.print = function(t,indent)
if indent == nil then indent = 0 end
if indent > 0 then io.write(string.rep(" ",indent)) end
io.write("{\n")
for i,k in pairs(t) do
io.write(string.rep(" ",indent+1))
io.write("["..i.."]=")
if type(k) == "table" then
table.print(k,indent+1)
else
io.write(tostring(k))
end
io.write(",\n")
end
if indent > 0 then io.write(string.rep(" ",indent)) end
io.write("}")
if indent == 0 then io.write("\n") end
end
table.search = function(t,e)
for i,v in pairs(t) do
if v == e then
return true,i
end
end
for i=1,#t do
if t[i] == e then
return true,i
end
end
return false
end
string.stt = function(str,sep)
if sep == nil then
sep = " "
end
local res = {}
local reg = string.format("([^%s]+)",sep)
for i in string.gmatch(tostring(str),reg) do
table.insert(res,i)
end
return res
end
string.clean = function(str)
return string.gsub(str,"%s+","")
end
string.starts = function(str,substr)
return string.sub(str,1,string.len(substr)) == substr
end
string.ends = function(str,substr)
return string.sub(str,string.len(str)-string.len(substr)+1) == substr
end
fs.isFile = function(path)
return (fs.exists(path) and not fs.isDir(path))
end
colors.success = colors.green
colors.error = colors.red
colors.info = colors.blue
colors.action = colors.yellow
colors.value = colors.yellow
colors.name = colors.cyan
colors.map = colors.orange
colors.cache = colors.orange
colors.path = colors.purple
colors.msg = colors.lightBlue
colors.chat = colors.gray
local colorNumberMap = table.protect({
['0'] = colors.white,
['1'] = colors.orange,
['2'] = colors.magenta,
['3'] = colors.lightBlue,
['4'] = colors.yellow,
['5'] = colors.lime,
['6'] = colors.pink,
['7'] = colors.gray,
['8'] = colors.lightGray,
['9'] = colors.cyan,
['a'] = colors.purple,
['b'] = colors.blue,
['c'] = colors.brown,
['d'] = colors.green,
['e'] = colors.red,
['f'] = colors.black
})
function colors.asColor(str)
return colorNumberMap[string.lower(str)]
end
paintutils.loadAnim = function(path)
local frames = {}
local frame = {}
if not fs.exists(path) then
error("file path invalid")
end
local f = fs.open(path,"r")
local l
local tl = {}
while true do
l = f.readLine()
if l == "~" or l == nil then
-- add frame to frames
table.insert(frames,frame)
frame = {}
if l == nil then -- eof
break
end
else
local pixelColor
for j=1,string.len(l) do
-- add char to line
pixelColor = colors.asColor(string.sub(l,j,j))
if pixelColor == nil then
pixelColor = term.getBackgroundColor()
end
table.insert(tl,pixelColor)
end
-- add line to frame
table.insert(frame,tl)
tl = {}
end
end
f.close()
return frames
end
paintutils.drawAnim = function(frames,x,y,delay,loop,terminal)
if x == nil then
x = 1
end
if y == nil then
y = 1
end
if delay == nil then
delay = .25
end
if loop == nil then
loop = false
end
if terminal == nil then
terminal = term.current()
end
while true do
for i,frame in pairs(frames) do -- iterate through frames
paintutils.drawImage(frame,x,y,terminal) -- draw frame
sleep(delay)
end
if not loop then
break
end
end
end
folder = {}
folder.game = "game/"
folder.apis = folder.game.."apis/"
folder.root = "AppData/"
folder.map = folder.root.."maps/"
folder.bot = folder.root.."bots/"
folder = table.protect(folder)
term.w = function()
local w,_ = term.getSize()
return w
end
term.h = function()
local _,h = term.getSize()
return h
end
local nwrite = io.write
io.write = function(par,fgc,bgc,slow)
local tfgc = term.getTextColor()
local tbgc = term.getBackgroundColor()
if fgc ~= nil then term.setTextColor(fgc) end
if bgc ~= nil then term.setBackgroundColor(bgc) end
if par == nil then
par = ""
else
par = tostring(par)
end
if not slow then nwrite(tostring(par))
else textutils.slowWrite(tostring(par))
end
if fgc ~= nil then term.setTextColor(tfgc) end
if bgc ~= nil then term.setBackgroundColor(tbgc) end
end
os.p = function()
io.write("Press SPACE to continue.",colors.action)
local e,c
repeat
e,c = os.pullEventRaw()
until (e == "char" and c == " ") or e == "monitor_touch"
local x,y = term.getCursorPos()
term.clearLine()
term.setCursorPos(1,y)
end
os.c = function()
term.current().clear()
term.current().setCursorPos(1,1)
end
os.lua = function()
shell.run("lua")
end