/
main.lua
384 lines (350 loc) · 10.3 KB
/
main.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
--[[
0.01
[re]Started a new thing!
]]
local dataLocation = false -- Location of data, defaults to "/data/"
local modulesLocation = false -- Location of modules, defaults to "/data/modules/"
local customFileName = false -- Location of 'customization' file, defaults to "/data/custom.modu"
local dynaName = false -- Location of DynaStore, defaults to "/data/dyna.modu"
local manipulatorOverride = false -- [Not currently working!]If you require a specific manipulator, change this value to the name of the manipulator on the network
local version = 001 -- Version number
local chests = {} -- All chest names stored in a table.
local allitems = {} -- All items stored in a table (fast cache :tm:).
local chestCache = {} -- NOT REQUIRED, DELET DELET DELET?
local modules = {} -- Modules, stored as a table.
local manipFuncs = {} -- Manipulator functions returned
local dynaStore = false -- DynaStore data
local manip = false -- manipulator confusion DELET?
local custom = false -- customization DELET?
local running = false -- DELET?
local functionsNeeded = {
manipulator = {
"tell",
"getInventory",
"getEnder",
"getName",
"capture",
"clearCaptures",
}
}
local function dCopy(a,b)
--DeepCopy
assert(type(a) == "table","copy: first input is not a table.")
for k,v in pairs(a) do
if type(v) == "table" then
b[k] = {}--If table, recursive copy
dCopy(a[k],b[k])
else
b[k] = v
end
end
end
local function tell(stuff)
local lines = {}
if stuff:len() > 100 then
local o = 1
for i = 1,stuff:len(),100 do
lines[o] = stuff:sub(i,i+99)
o = o + 1
end
else
lines[1] = stuff
end
for i = 1,#lines do
local a,b = pcall(manipFuncs.tell,lines[i])
if not a then
printError("Your chat-recorder is either missing or not bound properly.")
term.setTextColor(colors.yellow)
print(b)
term.setTextColor(colors.white)
end
end
end
local function doErr(func,cont)
local err = "ERR:" .. tostring(func) .. ": " .. tostring(cont)
tell(err)
error(err)
end
local function overWriteMain(variable,equals)
--Checks for instances of "local" as first word
--on a line. If so, checks variable
--if == variable, update it to be equal to
-- "equals"
--Assumes only major variables (variables at 'root' level)
local declaration = "local " .. variable
local find = "^" .. declaration
local h = fs.open(shell.getRunningProgram(),"r")
--If open...
if h then
--load lines of program into data
local data = {}
local line = h.readLine()
local o = 0
repeat
o = o + 1
data[o] = line
line = h.readLine()
until not line
h.close()
--search through the data for the variable.
o = -1 --if o == -1 after searching, failed.
for i = 1,#data do
if data[i]:match("^local ") then
local a = data[i]:gmatch("%w+")
a()
local var = a()
a = nil
--if the variable we found is named "variable" then
if var == variable then
-- we found our variable
o = i
break
end
end
end
if o == -1 then
doErr("overWriteMain","Failed to find variable " .. tostring(variable))
return false
else
declaration = declaration .. " = "
if type(equals) == "string" then
declaration = declaration .. "\"" .. equals .. "\""
elseif type(equals) == "table" then
declaration = declaration .. textutils.serialize(equals)
elseif type(equals) == "number" or type(equals) == "boolean" then
declaration = declaration .. tostring(equals)
else
doErr("overWriteMain","Unsupported type (" .. type(equals) .. ")")
return false
end
data[o] = declaration
local h = fs.open(shell.getRunningProgram(),"w")
if h then
for i = 1,#data do
h.writeLine(data[i])
end
h.close()
return true
else
doErr("overWriteMain","No handle (2)")
return false
end
end
else
doErr("overWriteMain","No handle (1).")
return false
end
end
--[[
local dataLocation = false
local modulesLocation = false
local customFileName = false
local dynaName = false
local chestSaveName = false
local dynaStore = false
local manip = false
local custom = false
local running = false
]]
--Check if the setup is valid (Function will overwrite variables for faster startup times.)
local function checkSetup()
-----------------------------this boio checks variables and updates them.
local function check()
local f = true
if dataLocation then
print("Data Location is specified, checking.")
if type(dataLocation) == "string" then
assert(dataLocation:match("^/.+/$"),"Cannot run; variable dataLocation: expected absolute path to folder. (Must start and end in '/').")
print("dataLocation is valid.")
else
doErr("checkSetup","Cannot run; variable dataLocation is of incorrect type.")
end
else
f = false
print("dataLocation is false.")
overWriteMain("dataLocation","/data/")
print("updated")
end
if modulesLocation then
if type(modulesLocation) == "string" then
assert(modulesLocation:match("^/.+/$"),"Cannot run; variable modulesLocation: expected absolute path to folder. (Must start and end in '/').")
print("modulesLocation is valid.")
else
doErr("checkSetup","Cannot run; variable modulesLocation is of incorrect type.")
end
else
f = false
print("modulesLocation is false.")
overWriteMain("modulesLocation","/data/modules/")
print("updated")
end
if customFileName then
if type(customFileName) == "string" then
assert(customFileName:match("^/.+%.modu$"),"Cannot run; variable customFileName: expected absolute path to file, ending with file-extension '.modu'.")
print("customFileName is valid.")
else
doErr("checkSetup","Cannot run; variable customFileName is of incorrect type.")
end
else
f = false
print("customFileName is false.")
overWriteMain("customFileName","/data/custom.modu")
print("updated")
end
if dynaName then
if type(dynaName) == "string" then
assert(dynaName:match("^.+%.modu$"),"Cannot run; variable dynaName:")
print("dynaName is valid.")
else
doErr("checkSetup","Cannot run; variable dynaName is of incorrect type.")
end
else
f = false
print("dynaName is false.")
overWriteMain("dynaName","/data/dyna.modu")
print("updated")
end
return f
end
--Start this stuff...
local vars = {"dataLocation","modulesLocation","customFileName","dynaName"}
if not dataLocation or not modulesLocation or not customFileName or not dynaName then
print("Setup is invalid (First run?)")
check()
print("Setup should be valid. Rebooting.")
os.sleep(3)
os.reboot()
else
if not check() then
print("Setup was invalid, rebooting now.")
end
print("Setup is valid.")
end
end
local function findFunctionsInPeripherals(ttab)
local tab = dCopy(ttab,tab)
--[[
{
peripheralType = {
"peripheralFunc1",
"peripheralFunc2",
overridePeripheral = "peripheralFunc5",
},
peripheralType2 = {
"peripheralFunc3",
"peripheralFunc4",
}
}
]]
local perip = {}
local function getPeriphs(tp)
local all = peripheral.getNames()
local perips = {}
for i = 1,#all do
if all[i]:find(tp) then
perips[#perips+1] = peripheral.wrap(all[i])
end
end
return perips
end
for k,v in pairs(tab) do
--[[
each k: peripheralType
each v: table of peripheralFuncs
]]
assert(type(v) == "table","ERR:findFunctionsInPeripherals: Expected table, index " .. tostring(k))
local pers = getPeriphs(k)
if pers then
for k2,v2 in pairs(v) do
--[[
each k2: number index or string override
each v2: peripheralFunctionName
]]
local grabbed = false
if type(k2) == "number" then
--No override
for i = 1,#pers do
--[[
each i: peripheral of type k
]]
for k3,v3 in pairs(pers[i]) do
--[[
each k3: peripheralFunctionName of peripheral of type k
each v3: peripheralFunction of peripheral of type k
]]
if k3 == v2 then
perip[k3] = v3
tab[k][k2] = nil
grabbed = true
break
end
end
if grabbed then break end
end
elseif type(k2) == "string" then
--Override
if peripheral.isPresent(k2) then
local ps = peripheral.wrap(k2)
for k3,v3 in pairs(ps) do
--[[
each k3: peripheralFunctionName of peripheral of type k
each v3: peripheralFunction of peripheral of type k
]]
if k3 == v2 then
perip[k3] = v3
tab[k][k2] = nil
break
end
end
end
else
doErr("findFunctionsInPeripherals","Unexpected error ID 1")
end
end
end
end
for k,v in pairs(tab) do
local notThere = true
for k2,v2 in pairs(v) do
notThere = false
break
end
if notThere then
tab[k] = nil
end
end
return perip,tab
end
local function saveDyna()
local h = fs.open(dataLocation..dynaName,"w")
if h then
h.write(textutils.serialize(dynaStore))
h.close()
end
end
local function loadDyna()
local h = fs.open(dataLocation..dynaName,"r")
if h then
dynaStore = textutils.unserialize(h.readAll())
h.close()
else
dynaStore = false
end
end
local function prepareDyna()
loadDyna()
if not dynaStore then loadDyna() end
if not dynaStore then dynaStore = {} end
local tmods = fs.list(modulesLocation)
for i = 1,#tmods do
local h = fs.open(modulesLocation..tmods[i],"r")
h.readLine()
local no = h.readLine()
h.close()
custom.modules[tmods[i]] = textutils.unserialize(no)
if not dynaStore[tmods[i]] then
dynaStore[tmods[i]] = {}
end
end
end
return 1