Skip to content

Instantly share code, notes, and snippets.

@CoderPuppy
Last active July 28, 2016 01:16
Show Gist options
  • Save CoderPuppy/892a3aa87072bab1861d08682abfc927 to your computer and use it in GitHub Desktop.
Save CoderPuppy/892a3aa87072bab1861d08682abfc927 to your computer and use it in GitHub Desktop.
Some VFS code for hbomb79
local VFS_ENV = setmetatable({
TEST_VAR = "TEST_VAR_VALUE";
}, {__index = _ENV or getfenv()})
VFS_ENV._G = VFS_ENV
VFS_ENV._ENV = VFS_ENV
function VFS_ENV.load(src, name, mode, env)
return load(src, name or '(load)', mode, env or VFS_ENV)
end
function VFS_ENV.loadstring(src, env)
return VFS_ENV.load(src, src, 't', env or VFS_ENV)
end
function VFS_ENV.loadfile(file, env)
local _ENV = VFS_ENV
local h = fs.open(file, 'r')
if h then
local fn, e = load(h.readAll(), fs.getName(file), 't', env or VFS_ENV)
h.close()
return fn, e
end
return nil, 'File not found'
end
if setfenv then
setfenv(VFS_ENV.loadfile, VFS_ENV)
end
function VFS_ENV.dofile(file)
local _ENV = VFS_ENV
local fn, e = loadfile(file, VFS_ENV)
if fn then
return fn()
else
error(e, 2)
end
end
if setfenv then
setfenv(VFS_ENV.dofile, VFS_ENV)
end
VFS_ENV.os = setmetatable({}, { __index = _G.os })
function VFS_ENV.os.run(env, path, ...)
local _ENV = VFS_ENV
local args = {...}
setmetatable(env, { __index = VFS_ENV })
local fn, e = loadfile(path, env)
if fn then
local ok, e = pcall(function()
fn(table.unpack(args))
end)
if not ok then
printError(e)
return false
end
return true
else
printError(e)
return false
end
end
if setfenv then
setfenv(VFS_ENV.os.run, VFS_ENV)
end
VFS_ENV.shell = setmetatable({}, { __index = shell })
function VFS_ENV.shell.run(...)
local _ENV = VFS_ENV
local words = {}
do
local quoted = false
for m in (table.concat({...}) .. '"'):gmatch('(.-)"') do
if quoted then
table.insert(words, m)
else
for m in m:gmatch('[^ \t]+') do
table.insert(words, m)
end
end
quoted = not quoted
end
end
if words[1] then
local path = shell.resolve(words[1])
if path then
return os.run({}, path, ...)
end
else
return false
end
end
if setfenv then
setfenv(VFS_ENV.shell.run, VFS_ENV)
end
VFS_ENV.fs = setmetatable({}, { __index = function(t, k)
local old = fs[k]
local v = old
if type(old) == 'function' then
v = function(...)
print(k, '(', ..., ')')
return old(...)
end
end
t[k] = v
return v
end })
VFS_ENV.dofile "main.lua"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment