-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_modem_network.lua
211 lines (169 loc) · 5.25 KB
/
single_modem_network.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
--- Implements peripheral.find and etc, but for a single modem instead of all
--- attached modems.
---@class SingleModemNetwork
local smn = {}
---@type Modem? The modem to use.
local modem
--- Set the modem to use.
---@param modem_side computerSide The side of the modem to use.
function smn.set_modem(modem_side)
local tmp = peripheral.wrap(modem_side) --[[@as Modem?]]
if not tmp then
error(("No modem found on side '%s'."):format(modem_side), 2)
end
if tmp.isWireless() then
error("Modem cannot be wireless.", 2)
end
modem = tmp
end
--- Get the name of the modem in use.
---@return string? name The name of the modem.
function smn.get_modem()
return modem and peripheral.getName(modem)
end
--- Find a peripheral on the network.
---@param type string The type of peripheral to find.
---@param filter_func nil|fun(name: string, wrapped: table?): boolean? A function to filter the peripherals.
---@return wrappedPeripheral? ... The found peripherals.
function smn.find(type, filter_func)
if not modem then
error("No modem set.", 2)
end
filter_func = filter_func or function() return true end
local names = modem.getNamesRemote()
local found = {}
for _, name in ipairs(names) do
if modem.hasTypeRemote(name, type) and filter_func(name, smn.wrap(name)) then
table.insert(found, peripheral.wrap(name))
end
end
return table.unpack(found)
end
--- Wrap a peripheral on the network.
---@param name string The name of the peripheral to wrap.
---@return wrappedPeripheral? peripheral The wrapped peripheral.
function smn.wrap(name)
if not modem then
error("No modem set.", 2)
end
if not modem.isPresentRemote(name) then
return nil -- so peripheral.wrap doesn't wrap something from another network.
end
return peripheral.wrap(name)
end
--- Call a method on a peripheral on the network.
---@param name string The name of the peripheral to call.
---@param method string The method to call.
---@param ... any The arguments to pass to the method.
---@return any? ... The return values of the method.
function smn.call(name, method, ...)
if not modem then
error("No modem set.", 2)
end
return modem.callRemote(name, method, ...)
end
--- Get the methods of a peripheral on the network.
---@param name string The name of the peripheral to get the methods of.
---@return string[] methods The methods of the peripheral.
function smn.getMethods(name)
if not modem then
error("No modem set.", 2)
end
return modem.getMethodsRemote(name)
end
--- Get the names of the peripherals on the network.
---@return string[] names The names of the peripherals.
function smn.getNames()
if not modem then
error("No modem set.", 2)
end
return modem.getNamesRemote()
end
--- Check if a peripheral is present on the network.
---@param name string The name of the peripheral to check for.
---@return boolean present Whether the peripheral is present.
function smn.isPresent(name)
if not modem then
error("No modem set.", 2)
end
return modem.isPresentRemote(name)
end
--- Get the type of a peripheral on the network.
---@param name string The name of the peripheral to get the type of.
---@return string? ... The types of the peripheral.
function smn.getType(name)
if not modem then
error("No modem set.", 2)
end
return modem.getTypeRemote(name)
end
--- Check if a peripheral has a type on the network.
---@param name string The name of the peripheral to check the type of.
---@param type string The type to check for.
---@return boolean hasType Whether the peripheral has the type.
function smn.hasType(name, type)
if not modem then
error("No modem set.", 2)
end
return modem.hasTypeRemote(name, type)
end
--- Open a channel on the modem.
---@param channel number The channel to open.
function smn.open(channel)
if not modem then
error("No modem set.", 2)
end
modem.open(channel)
end
--- Close a channel on the modem.
---@param channel number The channel to close.
function smn.close(channel)
if not modem then
error("No modem set.", 2)
end
modem.close(channel)
end
--- Close all channels on the modem.
function smn.closeAll()
if not modem then
error("No modem set.", 2)
end
modem.closeAll()
end
--- Check if a channel is open on the modem.
---@param channel number The channel to check.
---@return boolean isOpen Whether the channel is open.
function smn.isOpen(channel)
if not modem then
error("No modem set.", 2)
end
return modem.isOpen(channel)
end
--- Check if the modem is wireless. NOTE: This module can only be used on wired
--- modems, so this should always return false.
---@return boolean isWireless Whether the modem is wireless.
function smn.isWireless()
if not modem then
error("No modem set.", 2)
end
return modem.isWireless()
end
--- Transmit a message on the modem.
---@param channel number The channel to transmit on.
---@param replyChannel number The channel to reply on.
---@param message any The message to transmit
function smn.transmit(channel, replyChannel, message)
if not modem then
error("No modem set.", 2)
end
modem.transmit(channel, replyChannel, message)
end
--- Get the local name.
---@return string name The local name.
function smn.getNameLocal()
if not modem then
error("No modem set.", 2)
end
return modem.getNameLocal()
end
return smn