Exported getSideOf into module utils

This commit is contained in:
Hadinos Sanosam 2021-01-09 03:55:07 +01:00
parent 0ed81baa6e
commit e25e97b7cc
3 changed files with 26 additions and 20 deletions

View File

@ -1,22 +1,18 @@
local sides = peripheral.getNames()
for i = 1, #sides do
if peripheral.getType(sides[i]) == "modem" then
modemSide = sides[i]
break
end
end
dofile("utils")
modemSide = utils.getSideOf("modem") --get side the modem is on
rednet.open(modemSide)
--get message, no timeout
id, msg, dist = rednet.receive()
print("Received data:")
term.write("Filename: ")
name = tostring(read())
print(msg)
print("Received data:")
term.write("Output filename: ")
name = tostring(read())
--decompress string
content = textutils.unserialize(msg)
--write to file
local file = fs.open(name, "w")
file.write(content)
file.close()
rednet.close(modemSide)

View File

@ -1,10 +1,5 @@
local sides = peripheral.getNames()
for i = 1, #sides do
if peripheral.getType(sides[i]) == "modem" then
modemSide = sides[i]
break
end
end
dofile("utils")
modemSide = utils.getSideOf("modem")
rednet.open(modemSide)
@ -30,4 +25,4 @@ else
print("Aborted.")
end
rednet.close(modemSide)
rednet.close(modemSide)

15
utils Normal file
View File

@ -0,0 +1,15 @@
--lua module "utils" for random utilities
if utils then return end --avoid loading twice
utils = {}
function utils.getSideOf(name) --get which side, if any, a peripheral p is connected to
local sides = peripheral.getNames()
for i = 1, #sides do
if peripheral.getType(sides[i]) == name then
--found it!
return sides[i]
end
end
end
return utils