Advertisement
Guest User

Command Pocket

a guest
May 19th, 2015
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.27 KB | None | 0 0
  1. if pocket then
  2.     os.pullEventRaw = function(sEvent)
  3.         local file = fs.open(".async","r")
  4.         local hadLines = false
  5.         for line in file.readLine do
  6.             os.queueEvent("task_complete",unpack(textutils.unserialize(line)))
  7.             hadLines = true
  8.         end
  9.         if hadLines then fs.open(".async","w").close() end
  10.         return coroutine.yield(sEvent)
  11.     end
  12.     commands = {
  13.         list = function()
  14.             local file = fs.open(".code","r")
  15.             local cmd = textutils.unserialize(file.readLine())
  16.             table.insert(cmd.exec,"return commands.list()")      
  17.             file.close()
  18.             file = fs.open(".code","w")
  19.             file.writeLine(textutils.serialize(cmd):gsub("\n",""))
  20.             file.close()
  21.             while true do
  22.                 file = fs.open(".exec","r")
  23.                 local output = file.readLine()
  24.                 if output then
  25.                     file.close()
  26.                     fs.open(".exec","w").close()
  27.                     return textutils.unserialize(output)[1]
  28.                 end
  29.                 file.close()
  30.                 sleep(0.0)
  31.             end
  32.         end,
  33.         exec = function(sCommand)
  34.             assert(type(sCommand)=="string" or not sCommand,"string expected got "..type(sCommand))
  35.             local file = fs.open(".code","r")
  36.             local cmd = textutils.unserialize(file.readLine())
  37.             table.insert(cmd.exec,"os.setComputerLabel("..((os.computerLabel()=="Command Pocket Computer" or os.computerLabel()==nil) and "" or ("[["..os.computerLabel().."]]"))..") return commands.exec([["..sCommand.."]])")      
  38.             file.close()
  39.             file = fs.open(".code","w")
  40.             file.writeLine(textutils.serialize(cmd):gsub("\n",""))
  41.             file.close()
  42.             while true do
  43.                 file = fs.open(".exec","r")
  44.                 local output = file.readLine()
  45.                 if output then
  46.                     file.close()
  47.                     fs.open(".exec","w").close()
  48.                     return unpack(textutils.unserialize(output))
  49.                 end
  50.                 file.close()
  51.                 sleep(0.05)
  52.             end
  53.         end,
  54.         execAsync = function(sCommand)
  55.             assert(type(sCommand)=="string" or not sCommand,"string expected got "..type(sCommand))
  56.             local file = fs.open(".code","r")
  57.             local cmd = textutils.unserialize(file.readLine())
  58.             table.insert(cmd.async,"os.setComputerLabel("..((os.computerLabel()=="Command Pocket Computer" or os.computerLabel()==nil) and "" or ("[["..os.computerLabel().."]]"))..") return commands.execAsync([["..sCommand.."]])")      
  59.             file.close()
  60.             file = fs.open(".code","w")
  61.             file.writeLine(textutils.serialize(cmd):gsub("\n",""))
  62.             file.close()
  63.         end,
  64.         getBlockPosition = function()
  65.             local file = fs.open(".code","r")
  66.             local cmd = textutils.unserialize(file.readLine())
  67.             table.insert(cmd.exec,"return commands.getBlockPosition()")      
  68.             file.close()
  69.             file = fs.open(".code","w")
  70.             file.writeLine(textutils.serialize(cmd):gsub("\n",""))
  71.             file.close()
  72.             while true do
  73.                 file = fs.open(".exec","r")
  74.                 local output = file.readLine()
  75.                 if output then
  76.                     file.close()
  77.                     fs.open(".exec","w").close()
  78.                     return unpack(textutils.unserialize(output))
  79.                 end
  80.                 file.close()
  81.                 sleep(0.05)
  82.             end
  83.         end,
  84.         getBlockInfo = function(x,y,z)
  85.             local file = fs.open(".code","r")
  86.             local cmd = textutils.unserialize(file.readLine())
  87.             table.insert(cmd.exec,"return commands.getBlockInfo("..x..","..y..","..z..")")      
  88.             file.close()
  89.             file = fs.open(".code","w")
  90.             file.writeLine(textutils.serialize(cmd):gsub("\n",""))
  91.             file.close()
  92.             while true do
  93.                 file = fs.open(".exec","r")
  94.                 local output = file.readLine()
  95.                 if output then
  96.                     file.close()
  97.                     fs.open(".exec","w").close()
  98.                     return unpack(textutils.unserialize(output))
  99.                 end
  100.                 file.close()
  101.                 sleep(0.05)
  102.             end
  103.         end,
  104.         async = {},
  105.     }
  106.     local tbl = commands.list()
  107.     for _,k in ipairs(tbl) do
  108.         commands[k] = function(sCommand)
  109.             assert(type(sCommand)=="string" or not sCommand,"string expected got "..type(sCommand))
  110.             local file = fs.open(".code","r")
  111.             local cmd = textutils.unserialize(file.readLine())
  112.             table.insert(cmd.exec,"os.setComputerLabel("..((os.computerLabel()=="Command Pocket Computer" or os.computerLabel()==nil) and "" or ("[["..os.computerLabel().."]]"))..") return commands."..k.."([["..sCommand.."]])")      
  113.             file.close()
  114.             file = fs.open(".code","w")
  115.             file.writeLine(textutils.serialize(cmd):gsub("\n",""))
  116.             file.close()
  117.             while true do
  118.                 file = fs.open(".exec","r")
  119.                 local output = file.readLine()
  120.                 if output then
  121.                     file.close()
  122.                     fs.open(".exec","w").close()
  123.                     return unpack(textutils.unserialize(output))
  124.                 end
  125.                 file.close()
  126.                 sleep(0.05)
  127.             end
  128.         end
  129.        
  130.         commands.async[k] = function(sCommand)
  131.             assert(type(sCommand)=="string" or not sCommand,"string expected got "..type(sCommand))
  132.             local file = fs.open(".code","r")
  133.             local cmd = textutils.unserialize(file.readLine())
  134.             table.insert(cmd.async,"os.setComputerLabel("..((os.computerLabel()=="Command Pocket Computer" or os.computerLabel()==nil) and "" or ("[["..os.computerLabel().."]]"))..") return commands.async."..k.."([["..sCommand.."]])")      
  135.             file.close()
  136.             file = fs.open(".code","w")
  137.             file.writeLine(textutils.serialize(cmd):gsub("\n",""))
  138.             file.close()
  139.         end
  140.     end
  141. else
  142.   if not fs.exists(".code") then
  143.     local file = fs.open(".code","w")
  144.     file.writeLine(textutils.serialize({exec={},async={}}):gsub("\n",""))
  145.     file.close()
  146.     fs.open(".async","w").close()
  147.     fs.open(".exec","w").close()
  148.   end
  149.   foo = function()
  150.     while true do
  151.       term.clear()
  152.       term.setCursorPos(1,1)
  153.       term.write("name: ")
  154.       input = read()
  155.       commands.async.give(input.." ComputerCraft:pocketComputer 1 1 {display:{Name:Command Pocket Computer},computerID:"..os.computerID().."}")
  156.       sleep(0.0)
  157.     end
  158.   end
  159.   bar = function()
  160.     while true do
  161.         local file = fs.open(".code","r")
  162.         local cmd = textutils.unserialize(file.readLine())
  163.         if cmd then
  164.             for i=1,#cmd.exec do
  165.                 output = {loadstring(cmd.exec[i])()}
  166.                 local execFile = fs.open(".exec","a")
  167.                 execFile.writeLine(textutils.serialize(output):gsub("\n",""))
  168.                 execFile.close()    
  169.             end
  170.             for i=1,#cmd.async do
  171.                 output = {loadstring(cmd.async[i])()}
  172.                 local asyncFile = fs.open(".async","a")
  173.                 asyncFile.writeLine(textutils.serialize(output):gsub("\n",""))
  174.                 asyncFile.close()
  175.             end
  176.         end
  177.         file.close()
  178.         file = fs.open(".code","w")
  179.         file.write(textutils.serialize({exec={},async={}}):gsub("\n",""))
  180.         file.close()
  181.         sleep(0.1)
  182.     end
  183.   end
  184.   while true do
  185.     parallel.waitForAny(foo,bar)
  186.   end
  187. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement