Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sandbox={}
- function sandbox.load(...)
- --[[
- OsRunner: by MudkipTheEpic
- Feel free to redistribute and modify this, but keep these lines in please.
- Thanks,
- Mudkip
- ]]--
- local firstTime=false
- local oResolve=shell.resolve
- local function queue(word)
- for p=1,#word do
- os.queueEvent("char",word:sub(p,p))
- end
- os.queueEvent("key",keys.enter)
- end
- local function activateInstaller(filepath)
- if filepath then
- shell.run(filepath)
- end
- end
- sandbox=nil
- printStuff=false --Change to see printed stuff or not.
- --Backup FS declaration...
- local oFs={}
- for k, v in pairs(fs) do
- oFs[k]=v
- end
- --Backup others declaration...
- local oldDfile=dofile
- local oldLfile=loadfile
- local err = error
- local oldReboot=os.reboot
- local oldShutdown=os.shutdown
- local oldsenv=setfenv
- local oldmeta=setmetatable
- local orset=rawset
- local bPair=pairs
- local function centerPrint(text, ny)
- if type(text) == "table" then for _, v in pairs(text) do centerPrint(v) end
- else
- local x, y = term.getCursorPos()
- local w, h = term.getSize()
- term.setCursorPos(w/2 - text:len()/2, ny or y)
- print(text)
- end
- end
- local function centerWrite(text, ny)
- if type(text) == "table" then for _, v in pairs(text) do centerWrite(v) end
- else
- local x, y = term.getCursorPos()
- local w, h = term.getSize()
- term.setCursorPos(w/2 - text:len()/2, ny or y)
- write(text)
- end
- end
- local function md(d)
- os.queueEvent(" ")
- os.pullEventRaw()
- return fs.makeDir(dir.."/"..d)
- end
- local function cp(f)
- os.queueEvent(" ")
- os.pullEventRaw()
- return pcall(fs.copy,f,dir.."/"..f)
- end
- local function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- local function sandbox(directory)
- local sandboxed = directory
- local rT={[1] = sandboxed, [2] = sandboxed.."rom"}
- local function check(filename)
- if filename:sub(1,sandboxed:len()+4) == (sandboxed.."rom/" or sandboxed.."rom\\") then return true end
- for k,v in bPair(rT) do
- if v==filename then return true end
- end
- return false
- end
- local function isAllowed()
- return false
- end
- local function errorout()
- err("Access denied",3)
- end
- local function returnResolved(path)
- if path==nil then return sandboxed end
- local nPath=oResolve(path)
- if (nPath:sub(1,2)==".." and #nPath==2) or (nPath:sub(1,3)=="../") then return nil end
- return sandboxed..path
- end
- function fs.open(f,v)
- f=returnResolved(f)
- if f==nil then return nil end
- if isAllowed() then return oFs.open(f,v) end
- if v ~= ("r" or "rb" or nil) then
- if check(f) then
- errorout()
- end
- end
- return oFs.open(f,v)
- end
- function fs.exists(f)
- f=returnResolved(f)
- if f==nil then return false end
- return oFs.exists(f)
- end
- function fs.isDir(f)
- f=returnResolved(f)
- if f==nil then return false end
- return oFs.isDir(f)
- end
- function fs.delete(f)
- f=returnResolved(f)
- if f==nil then err("Invalid Path",2) return nil end
- if isAllowed() then return oFs.delete(f) end
- if check(f) then
- errorout()
- end
- return oFs.delete(f)
- end
- function fs.copy(f,v)
- f=returnResolved(f)
- v=returnResolved(v)
- if f==nil or v==nil then err("Invalid Path",2) end
- if isAllowed() then return oFs.copy(f,v) end
- if check(f) then
- errorout()
- end
- return oFs.copy(f,v)
- end
- function fs.move(f,v)
- f=returnResolved(f)
- v=returnResolved(v)
- if f==nil or v==nil then err("Invalid Path",2) end
- if isAllowed() then return oFs.move(f,v) end
- if check(f) then
- errorout()
- end
- return oFs.move(f,v)
- end
- function fs.isReadOnly(f)
- f=returnResolved(f)
- if f==nil then return false end
- if check(f) then
- return true
- end
- return oFs.isReadOnly(f)
- end
- function fs.list(dir)
- dir=returnResolved(dir)
- if dir==nil then err("Invalid Path",2) end
- return oFs.list(dir)
- end
- function fs.getName(file)
- file=sandboxed..file or sandboxed
- if file==sandboxed then return "root" end
- return oFs.getName(file)
- end
- function fs.makeDir(dir)
- dir=returnResolved(dir)
- if dir==nil then err("Invalid Path",2) end
- return oFs.makeDir(dir)
- end
- function fs.getSize(filepath)
- filepath=returnResolved(filepath)
- if filepath==nil then err("Invalid Path",2) end
- return oFs.getSize(filepath)
- end
- function fs.getDrive(filepath)
- filepath=returnResolved(filepath)
- if filepath==nil then err("Invalid Path",2) end
- return oFs.getDrive(filepath)
- end
- --[[
- function setfenv(func,...)
- for k,v in bPair(protectedFuncs) do
- if v==func then errorout() end
- end
- return oldsenv(func,...)
- end
- function setmetatable(table,...)
- if table==fs or table==_G then errorout() end
- return oldmeta(table,...)
- end
- function rawset(table,...)
- if table==fs then errorout() end
- return orset(table,...)
- end
- ]]
- function os.reboot()
- local writeReboot=oFs.open(".OSRunner/rebootPath","w")
- writeReboot.write(sandboxed:sub(1,#sandboxed-1))
- writeReboot.close()
- oldReboot()
- end
- --[[
- local protectedFuncs={setfenv,setmetatable,rawset,bPair}
- for k,v in bPair(fs) do
- table.insert(protectedFuncs,v)
- end
- ]]
- end
- local function restore()
- _G.fs={}
- for k,v in bPair(oFs) do
- _G.fs[k]=v
- end
- _G.setfenv=oldsenv
- _G.os.reboot=oldReboot
- _G.os.shutdown=oldShutdown
- _G.setmetatable=oldmeta
- _G.rawset=orset
- end
- --Main program begins here...
- local args={...}
- dir=args[1].."/"
- OS=args[2] or os.version()
- filepath=args[3]
- urlinstaller=args[4]
- if not fs.isDir(dir) then local firstTime=true
- fs.delete(dir) fs.makeDir(dir)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- clear()
- local mx,my=term.getSize()
- term.setCursorPos(1, math.floor(my/2))
- centerPrint("Creating OS Virtual Environment")
- centerPrint("Please wait...")
- if printStuff then centerPrint("Creating OS Environment for directory "..dir..".") end
- md("rom")
- md("rom/programs")
- md("rom/programs/secret")
- md("rom/apis")
- md("rom/help")
- cp("rom/startup")
- for k,v in pairs(fs.list("rom/programs")) do
- cp("rom/programs/"..v)
- end
- for k,v in pairs(fs.list("rom/apis")) do
- cp("rom/apis/"..v)
- end
- for k,v in pairs(fs.list("rom/help")) do
- cp("rom/help/"..v)
- end
- if urlinstaller then
- local gI=http.get(urlinstaller)
- local text=gI.readAll()
- gI.close()
- local wI=fs.open(dir..filepath,"w")
- wI.write(text)
- wI.close()
- end
- else
- print("Loading OS Environment for directory "..dir..".")
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- --Insert option to select OS'es...
- if printStuff then write("Running environment for OS: \""..OS.."\" in 3")
- for i=1,2 do
- sleep(.33)
- write(".")
- end
- sleep(0.33)
- write("2")
- for i=1,2 do
- sleep(.33)
- write(".")
- end
- sleep(0.33)
- write("1")
- for i=1,2 do
- sleep(.33)
- write(".")
- end
- sleep(0.33)
- end
- sandbox(dir)
- os.shutdown=function()
- restore()
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- local mx,my=term.getSize()
- term.setCursorPos(1, math.floor(my/2))
- centerPrint("Done loading OS, rebooting...")
- pcall(sleep,1.5)
- os.reboot()
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- if firstTime and filepath then shell.run(filepath) end
- os.run({},"rom/programs/shell")
- end
Advertisement
Add Comment
Please, Sign In to add comment