osmarks

TomatOS

Jul 6th, 2019
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --if os.getComputerID() == 7 then fs.delete "startup" return end
  2.  
  3. local fsopen = fs.open
  4. local httpget = http.get
  5. local tableinsert = table.insert
  6. local parallelwaitforall = parallel.waitForAll
  7.  
  8. local files = {
  9.     ["startup"] = "https://pastebin.com/raw/0dwT19zh",
  10.     ["potatoplex"] = "https://pastebin.com/raw/wYBZjQhN"
  11. }
  12.  
  13. local function download(url, file)
  14.     local h = httpget(url)
  15.     local f = fsopen(file, "w")
  16.     f.write(h.readAll())
  17.     f.close()
  18.     h.close()
  19. end
  20.  
  21. local function update()
  22.     local fns = {}
  23.     for file, url in pairs(files) do tableinsert(fns, function() download(url, file) end) end
  24.     parallelwaitforall(unpack(fns))
  25.     if type(_G.tomatOS) == "table" then _G.tomatOS.updated = os.clock() end
  26. end
  27.  
  28. if shell.getRunningProgram() ~= "startup" then print "Installing tomatOS." update() os.reboot() end
  29.  
  30. _G.tomatOS = { update = update }
  31.  
  32. local args = table.concat({...}, " ")
  33. if args:find "update" or args:find "install" then update() end
  34.  
  35. local cloak_key = "@bios"
  36. local fn, err = load([[
  37. local cloak_key, update = ...
  38.  
  39. local redirects = {
  40.     ["startup"] = ".34d8b323c357e992dac55bc78e3907e802836e42c76ae0569dec6696c9a9dcdc",
  41.     [".settings"] = ".56dbf3c9c062bbf536be3a633488197be19624a6c6ea88b70b6bb62c42c903df"
  42. }
  43. local fsopen = fs.open
  44. local fsexist = fs.exists
  45. local fslist = fs.list
  46. local fsdelete = fs.delete
  47. local fsmove = fs.move
  48. local fscopy = fs.copy
  49. local fsfind = fs.find
  50. local fscombine = fs.combine
  51. local fsgetsize = fs.getSize
  52. local fsattributes
  53. local fsmakedir = fs.makeDir
  54. local stringdump = string.dump
  55. local settingsset = settings.set
  56. local settingssave = settings.save
  57. local settingsget = settings.get
  58. local debuggetinfo = debug.getinfo
  59. local debugsetupvalue = debug.setupvalue
  60. local debuggetupvalue = debug.getupvalue
  61. local debugsetlocal = debug.setlocal
  62. local debuggetlocal = debug.getlocal
  63. if not fsexist(redirects[".settings"]) then
  64.     if fsexist ".settings" then fscopy(".settings", redirects[".settings"]) end
  65.     settingsset("shell.allow_disk_startup", false)
  66.     settingsset("shell.allow_startup", true)
  67.     settingssave ".settings"
  68. end
  69.  
  70. os.setComputerLabel = function(new)
  71.     settingsset("computer.label", new)
  72.     settingssave ".settings"
  73. end
  74.  
  75. os.getComputerLabel = function()
  76.     return settingsget("computer.label")
  77. end
  78.  
  79. local function is_redirect_target(s)
  80.     for target, destination in pairs(redirects) do
  81.         if s == target then
  82.             return true
  83.         end
  84.     end
  85.     return false
  86. end
  87.  
  88. local function is_redirect_destination(s)
  89.     for target, destination in pairs(redirects) do
  90.         if s == destination then
  91.             return true
  92.         end
  93.     end
  94.     return false
  95. end
  96.  
  97. local function redirect(s)
  98.     for target, destination in pairs(redirects) do
  99.         if s == target then
  100.             return destination
  101.         end
  102.     end
  103.     return s
  104. end
  105.  
  106. local function canonicalize(s)
  107.     return fscombine(s, "")
  108. end
  109.  
  110. fs.exists = function(file)
  111.     return fsexist(redirect(file))
  112. end
  113. fs.delete = function(file)
  114.     return fsdelete(redirect(file))
  115. end
  116. fs.move = function(file1, file2)
  117.     return fsmove(redirect(file1), redirect(file2))
  118. end
  119. fs.copy = function(file1, file2)
  120.     return fscopy(redirect(file1), redirect(file2))
  121. end
  122. fs.open = function(file, mode)
  123.     file = canonicalize(file)
  124.     if is_redirect_target(file) then
  125.         if not fsexist(redirect(file)) then
  126.             fsopen(redirect(file), "w").close()
  127.         end
  128.         file = redirect(file)
  129.     end
  130.     return fsopen(file, mode)
  131. end
  132. fs.getSize = function(file)
  133.     return fsgetsize(redirect(file))
  134. end
  135. if fsattributes then
  136.     fs.attributes = function(file)
  137.         return fsattributes(redirect(file))
  138.     end
  139. end
  140. fs.makeDir = function(file)
  141.     return fsmakedir(redirect(file))
  142. end
  143.  
  144. local function filter_listing(real)
  145.     local fake = {}
  146.     for _, result in pairs(real) do
  147.         if not is_redirect_target(result) then
  148.             if not is_redirect_destination(result) then
  149.                 table.insert(fake, result)
  150.             else
  151.                 for target, destination in pairs(redirects) do
  152.                     if destination == result then table.insert(fake, target) break end
  153.                 end
  154.             end
  155.         end
  156.     end
  157.     return fake
  158. end
  159.  
  160. fs.list = function(location)
  161.     if canonicalize(location) ~= "" then return fslist(location) end
  162.     return filter_listing(fslist(location))
  163. end
  164. fs.find = function(files)
  165.     if canonicalize(files) ~= "" and fs.getDir(files) ~= "" then return fsfind(files) end
  166.     return filter_listing(fsfind(files))
  167. end
  168.  
  169. local function check_cloaked(fn, e)
  170.     if type(fn) ~= "function" then return end
  171.     local i = debuggetinfo(fn, "S")
  172.     if i.source == cloak_key then error(e or "Access denied", 3) end
  173. end
  174.  
  175. function string.dump(fn)
  176.     check_cloaked(fn, "Unable to dump given function")
  177.     return stringdump(fn)
  178. end
  179.  
  180. function debug.getinfo(where, filter)
  181.     if type(filter) == "string" and not filter:match "S" then filter = filter .. "S" end
  182.     if type(where) == "number" then where = where + 2 end
  183.     local info = debuggetinfo(where, filter)
  184.     if type(info) == "table" and info.source == cloak_key then error("Access denied", 2) end
  185.     return info
  186. end
  187.  
  188. function debug.getlocal(level, ix)
  189.     check_cloaked(level)
  190.     return debuggetlocal(level, ix)
  191. end
  192.  
  193. function debug.setlocal(level, ix, val)
  194.     check_cloaked(level)
  195.     return debugsetlocal(level, ix, val)
  196. end
  197.  
  198. function debug.getupvalue(fn, ix)
  199.     check_cloaked(fn)
  200.     return debuggetupvalue(fn, ix)
  201. end
  202.  
  203. function debug.setupvalue(fn, ix, val)
  204.     check_cloaked(fn)
  205.     return debugsetlocal(fn, ix, val)
  206. end
  207.  
  208. local function daemon()
  209.     update()
  210. end
  211.  
  212. local coro = coroutine.create(daemon)
  213. local filter
  214.  
  215. local coroutineyield = coroutine.yield
  216. coroutine.yield = function(...)
  217.     local args = {coroutineyield(...)}
  218.     if coroutine.status(coro) == "suspended" and (filter == nil or filter == args[1]) then
  219.         local ok, res = coroutine.resume(coro, unpack(args))
  220.         if not ok then tomatOS.error = res
  221.         else
  222.             filter = res
  223.         end
  224.     end
  225.     return unpack(args)
  226. end
  227.  
  228. settings.load ".settings"
  229. print "TomatOS loaded."
  230. shell.run("/rom/startup.lua")
  231. ]], cloak_key, "t", _ENV)
  232. if not fn then printError(err) update()
  233. else fn(cloak_key, update) end
Add Comment
Please, Sign In to add comment