Advertisement
Wyvern67

FakeOS.lua

Apr 24th, 2014
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.52 KB | None | 0 0
  1. -- #### Vars
  2.  
  3. userStartupPath = "userStartup"
  4. osName = "fakeOS"
  5. hiddenPrograms = {osName, "edit"}
  6. fake = {}
  7. real = {}
  8.  
  9. -- #### "Local" functions
  10.  
  11. function getAnEmptyPath()
  12.     while true do
  13.         local dir = tostring(math.random(1, 65535))
  14.         if real.exists(dir) then
  15.             return dir
  16.         end
  17.     end
  18. end
  19.  
  20. function shouldBeHidden(path)
  21.     for i=1, #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. 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. 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.list = fs.list
  62. function fakeList(path)
  63.     list = real.list(path)
  64.     for j=1, #hiddenPrograms do
  65.         for i=1,#list do
  66.             if string.lower(list[i]) == string.lower(hiddenPrograms[j]) then
  67.                 table.remove(list, i)
  68.                 break
  69.             end
  70.         end
  71.     end
  72.    
  73.     for i=1,#list do
  74.         if list[i] == userStartupPath then
  75.             list[i] = "startup"
  76.         end
  77.     end
  78.     return list
  79. end
  80. fs.list = fakeList
  81.  
  82. real.exists = fs.exists
  83. function fakeExists(path)
  84.     if shouldBeHidden(path) then
  85.         path = getAnEmptyPath()
  86.     end
  87.     if string.lower(path) == "startup" then
  88.         args[i] = userStartupPath
  89.     end
  90.    
  91.     return real.exists(path)
  92. end
  93. fs.exists = fakeExists
  94.  
  95. real.isReadOnly = fs.isReadOnly
  96. function fakeIsReadOnly(path)
  97.     if shouldBeHidden(path) then
  98.         path = getAnEmptyPath()
  99.     end
  100.     if string.lower(path) == "startup" then
  101.         args[i] = userStartupPath
  102.     end
  103.    
  104.     return real.isReadOnly(path)
  105. end
  106. fs.isReadOnly = fakeIsReadOnly
  107.  
  108. real.getSize = fs.getSize
  109. function fakeGetSize(path)
  110.     if shouldBeHidden(path) then
  111.         path = getAnEmptyPath()
  112.     end
  113.     if string.lower(path) == "startup" then
  114.         args[i] = userStartupPath
  115.     end
  116.    
  117.     return real.getSize(path)
  118. end
  119. fs.getSize = fakeGetSize
  120.  
  121. real.move = fs.move
  122. function fakeMove(fromPath, toPath)
  123.     if shouldBeHidden(fromPath) then
  124.         fromPath = getAnEmptyPath()
  125.     end
  126.     if string.lower(fromPath) == "startup" then
  127.         args[i] = userStartupPath
  128.     end
  129.    
  130.     if shouldBeHidden(toPath) then
  131.         toPath = getAnEmptyPath()
  132.     end
  133.     if string.lower(toPath) == "startup" then
  134.         args[i] = userStartupPath
  135.     end
  136.    
  137.     return real.move(fromPath, toPath)
  138. end
  139. fs.move = fakeMove
  140.  
  141. real.copy = fs.copy
  142. function fakeCopy(fromPath, toPath)
  143.     if shouldBeHidden(fromPath) then
  144.         fromPath = getAnEmptyPath()
  145.     end
  146.     if string.lower(fromPath) == "startup" then
  147.         args[i] = userStartupPath
  148.     end
  149.    
  150.     if shouldBeHidden(toPath) then
  151.         toPath = getAnEmptyPath()
  152.     end
  153.     if string.lower(toPath) == "startup" then
  154.         args[i] = userStartupPath
  155.     end
  156.    
  157.     return real.copy(fromPath, toPath)
  158. end
  159. fs.copy = fakeCopy
  160.  
  161. real.delete = fs.delete
  162. function fakeDelete(path)
  163.     if shouldBeHidden(path) then
  164.         path = getAnEmptyPath()
  165.     end
  166.     if string.lower(path) == "startup" then
  167.         args[i] = userStartupPath
  168.     end
  169.    
  170.     return real.delete(path)
  171. end
  172.  
  173. real.open = fs.open
  174. function fakeOpen(path, mode)
  175.     if shouldBeHidden(path) then
  176.         path = getAnEmptyPath()
  177.     end
  178.     if string.lower(path) == "startup" then
  179.         args[i] = userStartupPath
  180.     end
  181.    
  182.     return real.open(path, mode)
  183. end
  184.  
  185. -- #### Actual FakeOS
  186.  
  187. shell.run("copy", "roms/programs/edit", "edit")
  188.  
  189.  
  190. term.clear()
  191. term.setCursorPos(1,1)
  192. term.writeYellow(os.version())
  193. term.setCursorPos(1,2)
  194.  
  195. while true do
  196.     term.writeYellow(shell.dir().."> ")
  197.     input = io.read()
  198.     if input == "$$areyouthere" then
  199.         print("Yes.")
  200.     end
  201.    
  202.     if type(input) == "string" then
  203.         args = splitIntoArgs(input)
  204.         command = table.remove(args, 1) -- command = args[1] and then it pops off args[1] from the table
  205.        
  206.         for j=1, #hiddenPrograms do
  207.             for i=1, #args do
  208.                 if string.lower(args[i]) == string.lower(hiddenPrograms[j]) then
  209.                     command = nil
  210.                 end
  211.             end
  212.         end
  213.        
  214.         for i=1, #args do
  215.             if string.lower(args[i]) == "startup" then
  216.                 args[i] = userStartupPath
  217.             end
  218.         end
  219.        
  220.         if type(command) == "string" then
  221.             shell.run(command, args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10])
  222.         end
  223.     end
  224. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement