Files
aceVM/AceVM/components/disks.lua
2025-09-30 12:20:47 -04:00

156 lines
3.8 KiB
Lua

local fss = {}
local object = {}
local uObject = {}
local function deepcopy(orig, copies)
copies = copies or {}
if type(orig) ~= 'table' then
return orig
elseif copies[orig] then
return copies[orig]
end
local copy = {}
copies[orig] = copy
for k, v in next, orig, nil do
local copied_key = deepcopy(k, copies)
local copied_val = deepcopy(v, copies)
copy[copied_key] = copied_val
end
return copy
end
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
function aceVM.initDisks(tble)
for i,v in ipairs(tble) do
if fs.exists("/AceVM/disks/"..tostring(v).."h") then
local obj = deepcopy(object)
obj.__UDATA_id=v
obj.id="disk_"..tostring(v)
obj.type="hdd"
aceVM.newComponent("disk", obj)
fss[tostring(v)]="/AceVM/disks/"..tostring(v).."h/"
elseif fs.exists("/AceVM/disks/"..tostring(v).."f") then
local obj = deepcopy(object)
obj.__UDATA_id=v
obj.id="disk_"..tostring(v)
obj.type="fdd"
aceVM.newComponent("disk", obj)
fss[tostring(v)]="/AceVM/disks/"..tostring(v).."f/"
elseif fs.exists("/AceVM/disks/"..tostring(v).."u") then
local obj = deepcopy(uObject)
obj.__UDATA_id=v
obj.id="disk_"..tostring(v)
obj.type="udd"
aceVM.newComponent("disk", obj)
fss[tostring(v)]="/AceVM/disks/"..tostring(v).."u"
else
fs.makeDir("/AceVM/disks/"..tostring(v).."f/")
local obj = deepcopy(object)
obj.__UDATA_id=v
obj.id="disk_"..tostring(v)
obj.type="fdd"
aceVM.newComponent("disk", obj)
fss[tostring(v)]="/AceVM/disks/"..tostring(v).."f/"
end
end
end