diff --git a/Src/Hyperion-firmware-cct/boot/cct/initdisks b/Src/Hyperion-firmware-cct/boot/cct/initdisks index 3669757..09997dd 100644 --- a/Src/Hyperion-firmware-cct/boot/cct/initdisks +++ b/Src/Hyperion-firmware-cct/boot/cct/initdisks @@ -6,31 +6,147 @@ local native = apis.peripheral local peripheral = {} local sides = {"top", "bottom", "left", "right", "front", "back"} -function peripheral.getType(name) - if native.isPresent(name) then return native.getType(name) end +function peripheral.getNames() + local results = {} 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, "getTypeRemote", name) + 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.getNames() - local names = {} +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.isPresent(side) then table.insert(names, side) end - if native.hasType(side, "peripheral_hub") then - local hubSides = native.call(side, "getConnectedSides") - for _, hubSide in ipairs(hubSides) do - table.insert(names, hubSide) + 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 names + return table.unpack(results) end local disks = {} @@ -130,14 +246,9 @@ local function refresh() if not peripheral.getType(id) then disks[id] = nil end end - for _, name in ipairs(peripheral.getNames()) do - if peripheral.getType(name) == "disk" then - if not disks[name] then - local mount = disk.getMountPath(name) - if mount then - disks[name] = createDisk(name, mount, false, disk) - end - end + for _, disk in ipairs({peripheral.find("drive")}) do + if disk.isDiskPresent() then + disks[tostring(disk.getDiskID())]=createDisk("cctdisk"..tostring(disk.getDiskID()), disk.getMountPath(), false, fs) end end end