This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Creeper9207's profile picture

Craftception

Started by Creeper9207, 26 June 2015 - 11:08 PM
Creeper9207 #1
Posted 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?
Grim Reaper #2
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?

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.
Creeper9207 #3
Posted 27 June 2015 - 05:30 AM
How would I modify the fs api?
Luca_S #4
Posted 27 June 2015 - 07:07 AM
That is not needed:

for name, func in pairs(_G.fs) do
	oldFS[name] = func
end
Just use

local oldFS = _G.fs
How would I modify the fs api?
The fs api table is not read only
you can use

fs.open = function()
  --somestrangecode xD
end
Edited on 27 June 2015 - 05:07 AM
Luca_S #5
Posted 27 June 2015 - 07:12 AM
Oh btw Grim, your code would override fs.combine() so this code:

path1 = "testdir"
path2 = "fileintestdir"
path = fs.combine(path1,path2)
fs.open(path,"r")
--continuing with code
would open
/emu/emu/testdir/fileintestdir
KingofGamesYami #6
Posted 27 June 2015 - 03:44 PM
That is not needed:

for name, func in pairs(_G.fs) do
	oldFS[name] = func
end
Just use

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.
Creeper9207 #7
Posted 27 June 2015 - 03:54 PM
I need something that would essentially run out-of-the-box
KingofGamesYami #8
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.
Grim Reaper #9
Posted 27 June 2015 - 06:49 PM
That is not needed:

for name, func in pairs(_G.fs) do
	oldFS[name] = func
end
Just use

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:

path1 = "testdir"
path2 = "fileintestdir"
path = fs.combine(path1,path2)
fs.open(path,"r")
--continuing with code
would open
/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.