Advertisement
theCPU

printall.lua

Mar 28th, 2018
942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.53 KB | None | 0 0
  1. local component = require("component")
  2. local shell     = require("shell")
  3. local gpu = component.gpu
  4. x , y = gpu.getResolution()
  5. local function setColor(c)
  6.   if c and gpu.getForeground() ~= c then
  7.     gpu.setForeground(c)
  8.   end
  9. end
  10.  
  11. local function cwrite(c, ...)
  12.   local oldCol = gpu.getForeground()
  13.   setColor(c)
  14.   io.write(...)
  15.   setColor(oldCol)
  16. end
  17.  
  18. local args = shell.parse(...)
  19. if #args < 1 then
  20.   io.write("Используйте: printAll FILE [Количество] [Номер определенного блока] \n")
  21.   os.exit(0)
  22. end
  23.  
  24. count = 1
  25. if #args > 1 then
  26.   count = assert(tonumber(args[2]), tostring(args[2]) .. " is not a valid count")
  27. end
  28. block = nil
  29. if #args > 2 then
  30.   block = assert(tonumber(args[3]), tostring(args[3]) .. "Not valid block_number")
  31. end
  32.  
  33. local file, reason = io.open(args[1], "r")
  34. if not file then
  35.   io.stderr:write("Ошибка при открытии файла: " .. reason .. "\n")
  36.   os.exit(1)
  37. end
  38.  
  39. local rawdata = file:read("*all")
  40. file:close()
  41. local data, reason = load("return " .. rawdata)
  42. if not data then
  43.   io.stderr:write("Ошибка загрузки модели: " .. reason .. "\n")
  44.   os.exit(2)
  45. end
  46.  
  47. local data1 = {}
  48.   for _, i in pairs({data()}) do
  49.     if (#i.shapes > 0) then
  50.       table.insert(data1, i)
  51.     end
  52.   end
  53.  
  54. cwrite( nil,"  Блоков найдено: ")
  55. cwrite( 0x00ffff, #data1, "\n")
  56.  
  57. local printers = {}
  58. for addr in component.list("printer3d") do
  59.   table.insert(printers, component.proxy(addr))
  60. end
  61.  
  62. cwrite( nil,"Принтеров найдено: ")
  63. cwrite( 0x00ffff, #printers, "\n")
  64.  
  65. local pIndex = 0
  66. local printer
  67.  
  68. -- Loop all models in file
  69. local blockNumber = 1
  70.  
  71. if block ~= nil then
  72.    blockNumber = block
  73. else
  74.    blockNumber = 1
  75.  
  76. end
  77.  
  78. while blockNumber <= #data1 do
  79.   if block ~= nil then
  80.     if blockNumber == block+1 then
  81.      break
  82.     end
  83.   end
  84.   -- Looking for idle printer
  85.   pIndex = (pIndex+1)>#printers and 1 or pIndex+1
  86.   printer = printers[pIndex]
  87.   if printer.status() ~= "idle" then goto continue end
  88.   if block ~= nil then
  89.   if blockNumber == block then
  90.   m = data1[block]
  91.   end
  92.   else
  93.     m = data1[blockNumber]
  94.   end
  95.   printer.reset()
  96.  
  97.   if m.label then
  98.     printer.setLabel(m.label)
  99.   end
  100.  
  101.   if m.tooltip then
  102.     printer.setTooltip(m.tooltip)
  103.   else
  104.     printer.setTooltip("["..blockNumber.."]")
  105.   end
  106.  
  107.   if m.lightLevel and printer.setLightLevel then -- as of OC 1.5.7
  108.     printer.setLightLevel(m.lightLevel)
  109.   end
  110.  
  111.   if m.emitRedstone then
  112.     printer.setRedstoneEmitter(m.emitRedstone)
  113.   end
  114.  
  115.   if m.buttonMode then
  116.     printer.setButtonMode(m.buttonMode)
  117.   end
  118.  
  119.   for _, shape in ipairs(m.shapes) do
  120.     local result, reason = printer.addShape(shape[1], shape[2], shape[3], shape[4], shape[5], shape[6], shape.texture, shape.state, shape.tint)
  121.     if not result then
  122.       io.write("Failed adding shape: " .. tostring(reason) .. "\n")
  123.     end
  124.   end
  125.   cwrite(nil,"#"); cwrite(0x00ffff,blockNumber); cwrite(nil,"/") cwrite(0x00ffff,#data1); cwrite(nil,": ")
  126.   cwrite(0xffffff, printer.getLabel() or "[no label]", " ")
  127.   cwrite(0x555555, printer.getTooltip() or "[no tooltip]", " ")
  128.   cwrite(nil,"Shape = "); cwrite(0xffff00, printer.getShapeCount()*1)
  129.   local result, reason = printer.commit(count)
  130.   if result then
  131.     cwrite(0x00ff00," >> Печать..\n");
  132.     blockNumber = blockNumber + 1
  133.     os.sleep(0.5)
  134.   else
  135.     io.stderr:write("\nFailed committing job: " .. tostring(reason) .. "\n")
  136.   end
  137.   ::continue::
  138. end
  139.  
  140. cwrite(nil,"Завершение печати")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement