is there any way to do this?
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Craftception
Started by Creeper9207, 26 June 2015 - 11:08 PMPosted 27 June 2015 - 01:08 AM
i basically want to make a virtualbox-based system in cc, basically if LyqydOS tried to run in it, and tried to save a file as "/LyqydOS/data" then it would save as "/emu/LyqydOS/data" if you get what im saying
is there any way to do this?
is there any way to do this?
Posted 27 June 2015 - 01:18 AM
If understand what you're asking correctly, you could modify the fs api to append your base directory to any path trying to be read/written to. However, you'll need to make sure that callers can still access the 'rom' files.
Maybe something like this?
This is, of course, untested.
Maybe something like this?
local baseDir = "/emu/"
local oldFs = {}
for name, func in pairs(_G.fs) do
oldFS[name] = func
end
for name, func in pairs(oldFs) do
_G.fs[name] = function(path, ...)
path = shell.resolve(path)
if oldFS.isReadOnly(path) then
return oldFs[name](path, ...)
end
return oldFs[name](baseDir .. path, ...)
end
end
This is, of course, untested.
Posted 27 June 2015 - 05:30 AM
How would I modify the fs api?
Posted 27 June 2015 - 07:07 AM
That is not needed:
you can use
Just usefor name, func in pairs(_G.fs) do oldFS[name] = func end
local oldFS = _G.fs
The fs api table is not read onlyHow would I modify the fs api?
you can use
fs.open = function()
--somestrangecode xD
end
Edited on 27 June 2015 - 05:07 AM
Posted 27 June 2015 - 07:12 AM
Oh btw Grim, your code would override fs.combine() so this code:
/emu/emu/testdir/fileintestdir
path1 = "testdir"
path2 = "fileintestdir"
path = fs.combine(path1,path2)
fs.open(path,"r")
--continuing with code
would open/emu/emu/testdir/fileintestdir
Posted 27 June 2015 - 03:44 PM
That is not needed:Just usefor name, func in pairs(_G.fs) do oldFS[name] = func end
local oldFS = _G.fs
That won't work, because that simply assigns _fs as a pointer to the _G.fs table, which we later change the values of. Sure, you can make the _G.fs table an entirely new table, but then the version of fs io uses won't be overridden.
Posted 27 June 2015 - 03:54 PM
I need something that would essentially run out-of-the-box
Posted 27 June 2015 - 03:58 PM
local baseDir = "/emu/"
local oldFs = {}
for name, func in pairs(_G.fs) do
oldFS[name] = func
end
for name, func in pairs(oldFs) do
_G.fs[name] = function(path, ...)
path = shell.resolve(path)
if oldFS.isReadOnly(path) then
return oldFs[name](path, ...)
end
return oldFs[name](baseDir .. path, ...)
end
end
_G.fs.combine = oldFs.combine
Try this. It's a slightly modified version of Grim's code, it should work. Unless, of course, he got something wrong when he wrote it.
Posted 27 June 2015 - 06:49 PM
That is not needed:Just usefor name, func in pairs(_G.fs) do oldFS[name] = func end
local oldFS = _G.fs
That code is needed, I think, because you must make a copy of the original fs api functions. Simply doing
local oldFS = _G.fs
means that the oldFS table simply points to the _G.fs table. So, by overwriting _G.fs, you overwrite oldFS as well, leaving the original and critical fs api functions nowhere to be found.Oh btw Grim, your code would override fs.combine() so this code:would openpath1 = "testdir" path2 = "fileintestdir" path = fs.combine(path1,path2) fs.open(path,"r") --continuing with code
/emu/emu/testdir/fileintestdir
You're absolutely right about this one. I don't use the entirety of the fs api much, so my knowledge of its contents is lacking.
Yami's code should work to correct that.