support open os command line programs
This commit is contained in:
@@ -2,6 +2,8 @@ local fs = _G.fs
|
||||
|
||||
local linkfs = { }
|
||||
|
||||
-- TODO: implement broken links
|
||||
|
||||
local methods = { 'exists', 'getFreeSpace', 'getSize',
|
||||
'isDir', 'isReadOnly', 'list', 'listEx', 'makeDir', 'open', 'getDrive' }
|
||||
|
||||
|
||||
@@ -17,53 +17,59 @@ end
|
||||
|
||||
function Peripheral.addDevice(deviceList, side)
|
||||
local name = side
|
||||
local ptype = Peripheral.getType(side)
|
||||
pcall(function()
|
||||
local ptype = Peripheral.getType(side)
|
||||
local dev = Peripheral.wrap(side)
|
||||
|
||||
if not ptype then
|
||||
return
|
||||
end
|
||||
|
||||
if ptype == 'modem' then
|
||||
if not Peripheral.call(name, 'isWireless') then
|
||||
-- ptype = 'wireless_modem'
|
||||
-- else
|
||||
ptype = 'wired_modem'
|
||||
if not ptype or not dev then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
local sides = {
|
||||
front = true,
|
||||
back = true,
|
||||
top = true,
|
||||
bottom = true,
|
||||
left = true,
|
||||
right = true
|
||||
}
|
||||
|
||||
if sides[name] then
|
||||
local i = 1
|
||||
local uniqueName = ptype
|
||||
while deviceList[uniqueName] do
|
||||
uniqueName = ptype .. '_' .. i
|
||||
i = i + 1
|
||||
if ptype == 'modem' then
|
||||
if not Peripheral.call(name, 'isWireless') then
|
||||
-- ptype = 'wireless_modem'
|
||||
-- else
|
||||
ptype = 'wired_modem'
|
||||
if dev.getMetadata then
|
||||
-- avoid open computer relays being registered
|
||||
-- as 'wired_modem'
|
||||
ptype = dev.getMetadata().name or 'wired_modem'
|
||||
end
|
||||
end
|
||||
end
|
||||
name = uniqueName
|
||||
end
|
||||
|
||||
-- this can randomly fail
|
||||
if not deviceList[name] then
|
||||
pcall(function()
|
||||
deviceList[name] = Peripheral.wrap(side)
|
||||
end)
|
||||
local sides = {
|
||||
front = true,
|
||||
back = true,
|
||||
top = true,
|
||||
bottom = true,
|
||||
left = true,
|
||||
right = true
|
||||
}
|
||||
|
||||
if deviceList[name] then
|
||||
Util.merge(deviceList[name], {
|
||||
name = name,
|
||||
type = ptype,
|
||||
side = side,
|
||||
})
|
||||
if sides[name] then
|
||||
local i = 1
|
||||
local uniqueName = ptype
|
||||
while deviceList[uniqueName] do
|
||||
uniqueName = ptype .. '_' .. i
|
||||
i = i + 1
|
||||
end
|
||||
name = uniqueName
|
||||
end
|
||||
end
|
||||
|
||||
-- this can randomly fail
|
||||
if not deviceList[name] then
|
||||
deviceList[name] = dev
|
||||
|
||||
if deviceList[name] then
|
||||
Util.merge(deviceList[name], {
|
||||
name = name,
|
||||
type = ptype,
|
||||
side = side,
|
||||
})
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
return deviceList[name]
|
||||
end
|
||||
@@ -117,4 +123,4 @@ function Peripheral.get(args)
|
||||
end
|
||||
end
|
||||
|
||||
return Peripheral
|
||||
return Peripheral
|
||||
@@ -659,6 +659,37 @@ function Util.wordWrap(str, limit)
|
||||
return lines
|
||||
end
|
||||
|
||||
-- https://github.com/MightyPirates/OpenComputers
|
||||
function Util.parse(...)
|
||||
local params = table.pack(...)
|
||||
local args = {}
|
||||
local options = {}
|
||||
local doneWithOptions = false
|
||||
for i = 1, params.n do
|
||||
local param = params[i]
|
||||
if not doneWithOptions and type(param) == "string" then
|
||||
if param == "--" then
|
||||
doneWithOptions = true -- stop processing options at `--`
|
||||
elseif param:sub(1, 2) == "--" then
|
||||
local key, value = param:match("%-%-(.-)=(.*)")
|
||||
if not key then
|
||||
key, value = param:sub(3), true
|
||||
end
|
||||
options[key] = value
|
||||
elseif param:sub(1, 1) == "-" and param ~= "-" then
|
||||
for j = 2, string.len(param) do
|
||||
options[string.sub(param, j, j)] = true
|
||||
end
|
||||
else
|
||||
table.insert(args, param)
|
||||
end
|
||||
else
|
||||
table.insert(args, param)
|
||||
end
|
||||
end
|
||||
return args, options
|
||||
end
|
||||
|
||||
function Util.args(arg)
|
||||
local options, args = { }, { }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user