--:Minify:-- -- CCT driver peripheral helper local kernel=... kernel.cct={} kernel.cct.peripheral={} local peripheral=kernel.cct.peripheral local apis = kernel.apis local native = apis.peripheral local sides = {"top", "bottom", "left", "right", "front", "back"} function peripheral.getNames() local results = {} for n = 1, #sides do local side = sides[n] if native.isPresent(side) then table.insert(results, side) if native.hasType(side, "peripheral_hub") then local remote = native.call(side, "getNamesRemote") for _, name in ipairs(remote) do table.insert(results, name) end end end end return results end function peripheral.isPresent(name) if native.isPresent(name) then return true end for n = 1, #sides do local side = sides[n] if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then return true end end return false end function peripheral.getType(peripheral) if type(peripheral) == "string" then if native.isPresent(peripheral) then return native.getType(peripheral) end for n = 1, #sides do local side = sides[n] if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", peripheral) then return native.call(side, "getTypeRemote", peripheral) end end return nil else local mt = getmetatable(peripheral) if not mt or mt.__name ~= "peripheral" or type(mt.types) ~= "table" then error("bad argument #1 (table is not a peripheral)", 2) end return table.unpack(mt.types) end end function peripheral.hasType(peripheral, peripheral_type) if type(peripheral) == "string" then if native.isPresent(peripheral) then return native.hasType(peripheral, peripheral_type) end for n = 1, #sides do local side = sides[n] if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", peripheral) then return native.call(side, "hasTypeRemote", peripheral, peripheral_type) end end return nil else local mt = getmetatable(peripheral) if not mt or mt.__name ~= "peripheral" or type(mt.types) ~= "table" then error("bad argument #1 (table is not a peripheral)", 2) end return mt.types[peripheral_type] ~= nil end end function peripheral.getMethods(name) if native.isPresent(name) then return native.getMethods(name) end for n = 1, #sides do local side = sides[n] if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then return native.call(side, "getMethodsRemote", name) end end return nil end function peripheral.getName(peripheral) local mt = getmetatable(peripheral) if not mt or mt.__name ~= "peripheral" or type(mt.name) ~= "string" then error("bad argument #1 (table is not a peripheral)", 2) end return mt.name end function peripheral.call(name, method, ...) if native.isPresent(name) then return native.call(name, method, ...) end for n = 1, #sides do local side = sides[n] if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then return native.call(side, "callRemote", name, method, ...) end end return nil end function peripheral.wrap(name) local methods = peripheral.getMethods(name) if not methods then return nil end local types = { peripheral.getType(name) } for i = 1, #types do types[types[i]] = true end local result = setmetatable({}, { __name = "peripheral", name = name, type = types[1], types = types, }) for _, method in ipairs(methods) do result[method] = function(...) return peripheral.call(name, method, ...) end end return result end function peripheral.find(ty, filter) local results = {} for _, name in ipairs(peripheral.getNames()) do if peripheral.hasType(name, ty) then local wrapped = peripheral.wrap(name) if filter == nil or filter(name, wrapped) then table.insert(results, wrapped) end end end return table.unpack(results) end