Files
HyperionOS/src/disks/1h/sys/modules/cc.disks.kd

100 lines
2.5 KiB
Plaintext

local kernelArgs={...}
local apis=kernelArgs[1]
local drivers=kernelArgs[2]
local log=kernelArgs[3]
local driver={}
local disks={}
local driverutil=kernelArgs[4]
driver.type = "disk"
driver.name = "CC:Disks"
driver.version = "1.0.0"
driver.apiVersion = 1
driver.description = "Driver for CC:Tweaked disks"
driver.arch = "cc"
driver.api = {}
local function abs(path)
if path:sub(1,1)~="/" then path="/"..path end
return path
end
local function newDisk(root)
root="/"..root
return {
readAllText=function(path)
local file=apis.fs.open(root..abs(path),"r")
local text=file.readAll()
file.close()
return text
end,
writeAllText=function(path, content)
local file=apis.fs.open(root..abs(path),"w")
file.write(content)
file.close()
end,
list=function(path)
return apis.fs.list(root..abs(path))
end,
delete=function(path)
return apis.fs.delete(root..abs(path))
end,
makeDirectory=function(path)
return apis.fs.makeDir(root..abs(path))
end,
makeFile=function(path)
local file=apis.fs.open(root..abs(path),"w")
file.close()
end,
directoryExists=function(path)
return apis.fs.exists(root..abs(path)) and apis.fs.isDir(root..abs(path))
end,
fileExists=function(path)
return apis.fs.exists(root..abs(path)) and not apis.fs.isDir(root..abs(path))
end
}
end
local function refreshDisks(peripheral)
local tmp2={}
tmp2["disk"]=newDisk("disk")
for i,v in ipairs(peripheral.api.getNames()) do
if peripheral.api.isType(v, "drive") then
local p=peripheral.api.wrap(v)
if p.isDiskPresent() then
if p.getMountPath()~="disk" then
tmp2["disk_"..p.getID()] = newDisk(p.getMountPath())
end
end
end
end
disks=tmp2
return true
end
function driver.init()
local peripheral=driverutil.getFirst("component")
refreshDisks(peripheral)
end
function driver.api.list()
local tmp={}
for i,v in pairs(disks) do
tmp[#tmp+1]={id=i, obj=v}
end
local i=0
return function()
i=i+1
if tmp[i]==nil then return end
return tmp[i].id, tmp[i].obj
end
end
function driver.main()
while true do
refreshDisks()
sleep(5)
end
end
drivers[#drivers+1] = driver