Advertisement
McMrARM

client

Sep 5th, 2015
119
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. end
  28. local printerID = 1
  29. print("Connecting to printer...")
  30. rednet.send(printerID, "printerConnect")
  31. local startClock = os.clock()
  32. success = false
  33. while os.clock() - startClock < 0.2 do
  34.     local id, msg = rednet.receive(0.2)
  35.     if id == printerID and msg == "printerConnectSuccess" then
  36.         success = true
  37.         break
  38.     end
  39. end
  40. if success then
  41.     function peripheral.isPresent(side)
  42.         if side == "left" then
  43.             return true
  44.         else
  45.             return oldPeripheral.isPresent(side)
  46.         end
  47.     end
  48.     function peripheral.getType(side)
  49.         if side == "left" then
  50.             return "printer"
  51.         else
  52.             return oldPeripheral.getType(side)
  53.         end
  54.     end
  55.     function peripheral.getMethods(side)
  56.         if side == "left" then
  57.             return {"write","setCursorPos","getCursorPos","getPageSize","newPage","endPage","getInkLevel","setPageTitle","getPaperLevel"}
  58.         else
  59.             return oldPeripheral.getMethods(side)
  60.         end
  61.     end
  62.     function peripheral.call(side, methodName, p1,p2,p3,p4,p5)
  63.         if side == "left" then
  64.             local tableData = {"callFunction", methodName,p1,p2,p3,p4,p5}
  65.             rednet.send(printerID, textutils.serialize(tableData))
  66.             local time = os.clock()
  67.             local successAction = false
  68.             while os.clock() - time < 1 do
  69.                 local rID, msg = rednet.receive(1)
  70.                 if rID == printerID then
  71.                     local toBeProcessed = textutils.unserialize(msg)
  72.                     if type(toBeProcessed) == "table" then
  73.                         return unpack(toBeProcessed)
  74.                     else
  75.                         return nil
  76.                     end
  77.                 end
  78.             end
  79.             print("Warning: Printer connection lost!")
  80.             return nil
  81.         else
  82.             return oldPeripheral.call(side, methodName, p1,p2,p3,p4,p5)
  83.         end
  84.     end
  85.     print("Printer connected!")
  86. else
  87.     print("Could not connect to printer!")
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement