· 4 years ago · Mar 18, 2021, 10:38 AM
1-- API to send/receive files via rednet;
2-- version 1.1 (updated)
3
4
5--GLOBAL VARIABLES--
6
7version="1.1"
8
9restrictedIds={}
10
11--GLOBAL VARIABLES--
12
13
14
15
16
17-------------------------------------------------------UTILS START-------------------------------------------------------
18local function contains(table, element)
19 if type(table)~="table" then error("Expected table,element,got "..type(table)..","..type(element),0)
20else
21 for _, value in pairs(table) do
22 if value == element then
23 return true
24 end
25 end
26 return false
27end
28end
29
30local function getKey(table,element)
31if type(table)~="table" then error("Expected table,element,got "..type(table)..","..type(element),0)
32else
33for key, value in pairs(table) do
34 if value == element then
35 return key
36 end
37 end
38 return
39 end
40end
41
42
43function openSide()
44 for _,side in ipairs({"top", "bottom", "front", "left", "right", "back"}) do
45 if peripheral.isPresent(side)
46 then
47 if peripheral.getType(side)=="modem"
48 then
49 modemside=side
50 rednet.open(modemside)
51 end
52 end
53 end
54end
55
56
57
58function getVersion()
59return version
60end
61
62function blacklist(id)
63if type(id) ~="number" then
64error("Expected number,got "..type(id),0)
65else
66if not contains(restrictedIds,id) then
67table.insert(restrictedIds,id)
68else return
69end
70end
71end
72
73function removeBlacklist(id)
74if type(id) ~="number" then
75error("Expected number,got "..type(id),0)
76else
77table.remove(restrictedIds,getKey(restrictedIds,id))
78end
79end
80
81
82
83
84
85
86
87
88-------------------------------------------------------REDWORKS----------------------------------------------------------
89
90local function timedReceive(path,timeout,ovr)
91
92 senderID,message,protocol=rednet.receive("FileTransfer",timeout)
93
94
95
96 if message ~=nil then
97
98 if not ovr and fs.exists(path) then
99 error("File already exists.",0)
100 elseif ovr or not fs.exists(path) then
101
102 local l=fs.open(path,"w")
103 l.write(message)
104 l.close()
105 return senderID
106 end
107 else
108
109 return
110 end
111
112end
113
114local function INTreceiveFile(path,ovr)
115 event,senderID,message,protocol=os.pullEvent("rednet_message")
116
117 if not ovr and fs.exists(path) then
118 error("File already exists.",0)
119 else
120 local l=fs.open(path,"w")
121 print(message)
122 l.write(message)
123 l.close()
124 return senderID
125 end
126end
127
128
129local oldPull=os.pullEventRaw
130
131
132function os.pullEventRaw( sFilter )
133 while true do
134 local event = { oldPull() }
135 if coFoo~=nil then
136 if coroutine.status( coFoo ) == "suspended" then
137 if event[1]=="rednet_message" and not contains(restrictedIds,event[2]) and event[4]=="FileTransfer" then
138 coroutine.resume( coFoo, unpack( event ) )
139 end
140 end
141 elseif TimedcoFoo~=nil then
142 if coroutine.status(TimedcoFoo) =="suspended" then
143 if event[1]=="rednet_message" and not contains(restrictedIds,event[2]) and event[4]=="FileTransfer" then
144 coroutine.resume( TimedcoFoo, unpack( event ) )
145 end
146 end
147 end
148 if sFilter == event[ 1 ] or not sFilter and not contains(restrictedIds,event[2]) and event[4]=="FileTransfer" then
149 return unpack( event )
150
151 end
152 if sFilter == event[ 1 ] or not sFilter then
153 return unpack( event )
154 end
155 end
156end
157
158
159
160function sendFile(path,receiverID)
161
162 if fs.exists(path) and type(receiverID)=="number"
163 then
164 g=fs.open(path,"r")
165 file=g.readAll()
166 g.close()
167 rednet.send(receiverID , file,"FileTransfer" )
168
169 else error("Expected valid path,number got invalid path or invalid number",0)
170
171 end
172
173end
174
175function broadcastFile(path)
176if fs.exists(path)
177 then
178 g=fs.open(path,"r")
179 file=g.readAll()
180 g.close()
181 rednet.broadcast(file,"FileTransfer" )
182
183 else error("Expected valid path",0)
184
185 end
186end
187
188function ASyncReceiveFile(path,timeout,override)
189if type(timeout)=="boolean" and override==nil then coFoo=coroutine.create(INTreceiveFile) coroutine.resume(coFoo,path,override)
190
191elseif type(timeout)~="boolean" and type(timeout)~="number" then error("Expected valid path,number,boolean,got path, "..type(timeout)..","..type(override),0)
192elseif override~=nil and type(override)~="boolean" then error("Expected valid path,number,boolean,got path, "..type(timeout)..","..type(override),0)
193else TimedcoFoo=coroutine.create(timedReceive) coroutine.resume(TimedcoFoo,path,timeout,override)
194end
195end
196
197function receiveFile(path,timeout,override)
198if type(timeout)=="boolean" and override==nil then INTreceiveFile(path,timeout)
199
200elseif type(timeout)~="boolean" and type(timeout)~="number" then error("Expected valid path,number,boolean,got paaaaath, "..type(timeout)..","..type(override),0)
201elseif override~=nil and type(override)~="boolean" then error("Expected valid path,number,boolean,got path, "..type(timeout)..","..type(override),0)
202else timedReceive(path,timeout,override)
203end
204end
205
206function sendDirectory( directory, receiverID )
207 local files = fs.list( directory )
208 local splitOnDir = str.split( directory, "/" )
209 local lastFolder = splitOnDir[(#splitOnDir)]
210 for _, file in pairs(files) do
211 if not fs.isDir(file) then
212 rednet.send( receiverID, lastFolder .. "/" .. file, "receiveFile")
213 filenet.sendFile( directory .. "/" .. file, receiverID )
214 end
215 end
216end