Advertisement
Guest User

mount

a guest
Jan 4th, 2013
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.04 KB | None | 0 0
  1. --code by majsky
  2.  
  3. local args = {...}
  4. local version = 1
  5.  
  6. if #args ~= 1 and #args ~= 2 then
  7.     print "Usage:\n mount <side> [server ID]"
  8.     return
  9. end
  10.  
  11. if http then
  12.     local data = nil
  13.     parallel.waitForAll(
  14.         function()
  15.             textutils.slowPrint("Contacting update server...")
  16.         end,
  17.         function()
  18.             data = http.get("https://dl.dropbox.com/u/49227540/cc/wireless_peripheral_client/ver.txt")
  19.         end
  20.     )
  21.     if data then
  22.         local version = data.readLine()
  23.         if version then
  24.             if ver < tonumber(version) then
  25.                 print("New version on wireless peripherals found\nRun mount update to download newest version")
  26.             end
  27.         end
  28.     end
  29. end
  30.  
  31. if #args == 1 and http then
  32.     if string.lower(args[1]) == "update" then
  33.         local prog = nil
  34.         parallel.waitForAll(
  35.             function()
  36.                 textutils.slowPrint("Downloading update...")
  37.             end,
  38.             function()
  39.                 prog = http.get("https://dl.dropbox.com/u/49227540/cc/wireless_peripheral_client/prog.lua")
  40.             end
  41.         )
  42.         if prog then
  43.             prog = prog.readAll()
  44.             local file = io.open(shell.getRunningProgram(), "w")
  45.             file:write(prog)
  46.             file:close()
  47.             print "Program was sucessfully updated\nRestart the program, please"
  48.             return
  49.         end
  50.     end
  51. end
  52.  
  53. os.sleep(1.5)
  54. term.clear()
  55.  
  56. local modem = false
  57.  
  58. for k, v in pairs(rs.getSides()) do
  59.     if peripheral.getType(v) == "modem" then
  60.         modem = true
  61.         rednet.open(v)
  62.         break
  63.     end
  64. end
  65.  
  66. if not modem then
  67.     print 'Modem not found'
  68.     return
  69. end
  70.  
  71. local periType = nil
  72. local methods = {}
  73. local server = nil
  74.  
  75. for i=1, 3 do
  76.     if #args == 2 then
  77.         rednet.send(tonumber(args[2], "#PERI_CLIENT"))
  78.     else
  79.         rednet.broadcast("#PERI_CLIENT")
  80.     end
  81.     local id, msg = rednet.receive(1)
  82.     if msg == "#PERI_SERVER" then
  83.         server = id
  84.         for i = 1, 3 do
  85.             id, msg = rednet.receive(1)
  86.             if id == server then
  87.                 data = textutils.unserialize(msg)
  88.                 periType = data[1]
  89.                 methods = data[2]
  90.                 break
  91.             end
  92.         end
  93.         break
  94.     end
  95. end
  96.  
  97. if not server then
  98.     print "Peripheral server not found!"
  99.     return
  100. end
  101.  
  102. print("Connected to server "..server..", which is running "..periType)
  103.  
  104. local oldPeri = {}
  105. for k, v in pairs(peripheral) do
  106.     oldPeri[k] = v
  107. end
  108.  
  109. function peripheral.isPresent(side)
  110.     if side == args[1] then
  111.         return true
  112.     end
  113.     return oldPeri.isPresent(side)
  114. end
  115.  
  116. function peripheral.getType(side)
  117.     if side == args[1] then
  118.         return periType
  119.     end
  120.     return oldPeri.getType(side)
  121. end
  122.  
  123. function peripheral.getMethods(side)
  124.     if side == args[1] then
  125.         return methods
  126.     end
  127.     return oldPeri.getMethods(side)
  128. end
  129.  
  130. function peripheral.call(side, func, par1, par2, par3, par4, par5)
  131.     if side == args[1] then
  132.         local arg = {"call", func, par1, par2, par3, par4, par5}
  133.         rednet.send(server, textutils.serialize(arg))
  134.         for i = 1, 3 do
  135.             local id, msg = rednet.receive(1)
  136.             if id == server then
  137.                 local response = textutils.unserialize(msg)
  138.                 if type(response) == "table" then
  139.                     return unpack(response)
  140.                 end
  141.                 return nil
  142.             end
  143.         end
  144.         print 'Connection lost!'
  145.         return nil
  146.     end
  147.     return oldPeri.call(side, func, par1, par2, par3, par4, par5)
  148. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement