McMrARM

Untitled

Sep 5th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local tArgs = {...}
  2.  
  3. print("Beta version of printer client made by 1lann")
  4.  
  5. local modemPresent = false
  6.  
  7. for k,v in pairs(rs.getSides()) do
  8.     if peripheral.getType(v) == "modem" then
  9.         modemPresent = true
  10.         rednet.open(v)
  11.         break
  12.     end
  13. end
  14.  
  15. if not modemPresent then print("No modem found") error() end
  16.  
  17. local function copyTable(tableid)
  18.     local newTable = {}
  19.     for k,v in pairs(tableid) do
  20.         newTable[k] = v
  21.     end
  22.     return newTable
  23. end
  24.  
  25. local oldPeripheral = copyTable(peripheral)
  26.  
  27. if #tArgs ~= 2 then
  28.     print("Usage: pconnect <id> <side>")
  29.     error()
  30. end
  31. local printerID = tonumber(tArgs[1])
  32. print("Connecting to printer...")
  33. rednet.send(printerID, "printerConnect")
  34. local startClock = os.clock()
  35. success = false
  36. while os.clock() - startClock < 0.2 do
  37.     local id, msg = rednet.receive(0.2)
  38.     if id == printerID and msg == "printerConnectSuccess" then
  39.         success = true
  40.         break
  41.     end
  42. end
  43. if success then
  44.     function peripheral.isPresent(side)
  45.         if side == tArgs[2] then
  46.             return true
  47.         else
  48.             return oldPeripheral.isPresent(side)
  49.         end
  50.     end
  51.     function peripheral.getType(side)
  52.         if side == tArgs[2] then
  53.             return "printer"
  54.         else
  55.             return oldPeripheral.getType(side)
  56.         end
  57.     end
  58.     function peripheral.getMethods(side)
  59.         if side == tArgs[2] then
  60.             return {"write","setCursorPos","getCursorPos","getPageSize","newPage","endPage","getInkLevel","setPageTitle","getPaperLevel"}
  61.         else
  62.             return oldPeripheral.getMethods(side)
  63.         end
  64.     end
  65.     function peripheral.call(side, methodName, p1,p2,p3,p4,p5)
  66.         if side == tArgs[2] then
  67.             local tableData = {"callFunction", methodName,p1,p2,p3,p4,p5}
  68.             rednet.send(printerID, textutils.serialize(tableData))
  69.             local time = os.clock()
  70.             local successAction = false
  71.             while os.clock() - time < 1 do
  72.                 local rID, msg = rednet.receive(1)
  73.                 if rID == printerID then
  74.                     local toBeProcessed = textutils.unserialize(msg)
  75.                     if type(toBeProcessed) == "table" then
  76.                         return unpack(toBeProcessed)
  77.                     else
  78.                         return nil
  79.                     end
  80.                 end
  81.             end
  82.             print("Warning: Printer connection lost!")
  83.             return nil
  84.         else
  85.             return oldPeripheral.call(side, methodName, p1,p2,p3,p4,p5)
  86.         end
  87.     end
  88.     print("Printer connected!")
  89. else
  90.     print("Could not connect to printer!")
  91. end
Add Comment
Please, Sign In to add comment