Files
aceVM/AceVM/components/disks.lua
2025-09-29 14:13:43 -04:00

139 lines
3.5 KiB
Lua

local fss = {}
local object = {}
local uObject = {}
local function getFs(disk)
return fss[tostring(disk.__UDATA_id)]
end
local function normalize(path)
if path:sub(1,1)=="/" then
path=path:sub(2)
end
return path
end
function object:open(path)
path=normalize(path)
local f = getFs(self)
return {
read=function()
local file = fs.open(f..path, "r")
local text = file.readAll()
file.close()
return text
end,
write=function(text)
local file = fs.open(f..path, "w")
file.write(text)
file.close()
end,
append=function(text)
local file = fs.open(f..path, "a")
file.write(text)
file.close()
end
}
end
function object:list(path)
path=normalize(path)
local f = getFs(self)
return fs.list(f..path)
end
function object:fileExists(path)
path=normalize(path)
local f = getFs(self)
return fs.exists(f..path) and not fs.isDir(f..path)
end
function object:directoryExists(path)
path=normalize(path)
local f = getFs(self)
return fs.exists(f..path) and fs.isDir(f..path)
end
function object:makeDirectory(path)
path=normalize(path)
local f = getFs(self)
fs.makeDir(f..path)
end
function object:delete(path)
path=normalize(path)
local f = getFs(self)
fs.delete(f..path)
end
function object:getSize(path)
path=normalize(path)
local f = getFs(self)
return fs.getSize(f..path)
end
function uObject:readBytes(start, length)
local f = getFs(self)
local file=fs.open(f, "r")
local data=file.readAll()
file.close()
return data:sub(start+1, start+length)
end
function uObject:writeBytes(start, dat)
local f = getFs(self)
local file=fs.open(f, "r")
local data=file.readAll()
file.close()
file = fs.open(f, "w")
file.write(data:sub(1, start)..dat..data:sub(start+1+#dat, #data))
file.close()
end
function uObject:getSize()
local f = getFs(self)
local file=fs.open(f, "r")
local data=file.readAll()
file.close()
return #data
end
local function findDisks(tble)
for i,v in ipairs(tble) do
if fs.exists("/disks/"..tostring(v).."h") then
local obj = deepcopy(object)
obj.__UDATA_id=v
obj.id="disk_"..tostring(v)
obj.type="hdd"
newComponent("disk", obj)
fss[tostring(v)]="/disks/"..tostring(v).."h/"
elseif fs.exists("/disks/"..tostring(v).."f") then
local obj = deepcopy(object)
obj.__UDATA_id=v
obj.id="disk_"..tostring(v)
obj.type="fdd"
newComponent("disk", obj)
fss[tostring(v)]="/disks/"..tostring(v).."f/"
elseif fs.exists("/disks/"..tostring(v).."u") then
local obj = deepcopy(uObject)
obj.__UDATA_id=v
obj.id="disk_"..tostring(v)
obj.type="udd"
newComponent("disk", obj)
fss[tostring(v)]="/disks/"..tostring(v).."u"
else
fs.makeDir("/disks/"..tostring(v).."f/")
local obj = deepcopy(object)
obj.__UDATA_id=v
obj.id="disk_"..tostring(v)
obj.type="fdd"
newComponent("disk", obj)
fss[tostring(v)]="/disks/"..tostring(v).."f/"
end
end
end
local file = fs.open("/computers/0/computer.ltable", "r")
local config = load("return "..file.readAll())()
file.close()
findDisks(config.disks)