Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- API to send/receive files via rednet;
- -- version 1.1 (updated)
- --GLOBAL VARIABLES--
- version="1.1"
- restrictedIds={}
- --GLOBAL VARIABLES--
- -------------------------------------------------------UTILS START-------------------------------------------------------
- local function contains(table, element)
- if type(table)~="table" then error("Expected table,element,got "..type(table)..","..type(element),0)
- else
- for _, value in pairs(table) do
- if value == element then
- return true
- end
- end
- return false
- end
- end
- local function getKey(table,element)
- if type(table)~="table" then error("Expected table,element,got "..type(table)..","..type(element),0)
- else
- for key, value in pairs(table) do
- if value == element then
- return key
- end
- end
- return
- end
- end
- function openSide()
- for _,side in ipairs({"top", "bottom", "front", "left", "right", "back"}) do
- if peripheral.isPresent(side)
- then
- if peripheral.getType(side)=="modem"
- then
- modemside=side
- rednet.open(modemside)
- end
- end
- end
- end
- function getVersion()
- return version
- end
- function blacklist(id)
- if type(id) ~="number" then
- error("Expected number,got "..type(id),0)
- else
- if not contains(restrictedIds,id) then
- table.insert(restrictedIds,id)
- else return
- end
- end
- end
- function removeBlacklist(id)
- if type(id) ~="number" then
- error("Expected number,got "..type(id),0)
- else
- table.remove(restrictedIds,getKey(restrictedIds,id))
- end
- end
- -------------------------------------------------------REDWORKS----------------------------------------------------------
- local function timedReceive(path,timeout,ovr)
- senderID,message,protocol=rednet.receive("FileTransfer",timeout)
- if message ~=nil then
- if not ovr and fs.exists(path) then
- error("File already exists.",0)
- elseif ovr or not fs.exists(path) then
- local l=fs.open(path,"w")
- l.write(message)
- l.close()
- return senderID
- end
- else
- return
- end
- end
- local function INTreceiveFile(path,ovr)
- event,senderID,message,protocol=os.pullEvent("rednet_message")
- if not ovr and fs.exists(path) then
- error("File already exists.",0)
- else
- local l=fs.open(path,"w")
- print(message)
- l.write(message)
- l.close()
- return senderID
- end
- end
- local oldPull=os.pullEventRaw
- function os.pullEventRaw( sFilter )
- while true do
- local event = { oldPull() }
- if coFoo~=nil then
- if coroutine.status( coFoo ) == "suspended" then
- if event[1]=="rednet_message" and not contains(restrictedIds,event[2]) and event[4]=="FileTransfer" then
- coroutine.resume( coFoo, unpack( event ) )
- end
- end
- elseif TimedcoFoo~=nil then
- if coroutine.status(TimedcoFoo) =="suspended" then
- if event[1]=="rednet_message" and not contains(restrictedIds,event[2]) and event[4]=="FileTransfer" then
- coroutine.resume( TimedcoFoo, unpack( event ) )
- end
- end
- end
- if sFilter == event[ 1 ] or not sFilter and not contains(restrictedIds,event[2]) and event[4]=="FileTransfer" then
- return unpack( event )
- end
- if sFilter == event[ 1 ] or not sFilter then
- return unpack( event )
- end
- end
- end
- function sendFile(path,receiverID)
- if fs.exists(path) and type(receiverID)=="number"
- then
- g=fs.open(path,"r")
- file=g.readAll()
- g.close()
- rednet.send(receiverID , file,"FileTransfer" )
- else error("Expected valid path,number got invalid path or invalid number",0)
- end
- end
- function broadcastFile(path)
- if fs.exists(path)
- then
- g=fs.open(path,"r")
- file=g.readAll()
- g.close()
- rednet.broadcast(file,"FileTransfer" )
- else error("Expected valid path",0)
- end
- end
- function ASyncReceiveFile(path,timeout,override)
- if type(timeout)=="boolean" and override==nil then coFoo=coroutine.create(INTreceiveFile) coroutine.resume(coFoo,path,override)
- elseif type(timeout)~="boolean" and type(timeout)~="number" then error("Expected valid path,number,boolean,got path, "..type(timeout)..","..type(override),0)
- elseif override~=nil and type(override)~="boolean" then error("Expected valid path,number,boolean,got path, "..type(timeout)..","..type(override),0)
- else TimedcoFoo=coroutine.create(timedReceive) coroutine.resume(TimedcoFoo,path,timeout,override)
- end
- end
- function receiveFile(path,timeout,override)
- if type(timeout)=="boolean" and override==nil then INTreceiveFile(path,timeout)
- elseif type(timeout)~="boolean" and type(timeout)~="number" then error("Expected valid path,number,boolean,got paaaaath, "..type(timeout)..","..type(override),0)
- elseif override~=nil and type(override)~="boolean" then error("Expected valid path,number,boolean,got path, "..type(timeout)..","..type(override),0)
- else timedReceive(path,timeout,override)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment