Advertisement
Guest User

HTTP_ACCI_Viewer

a guest
Mar 9th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.60 KB | None | 0 0
  1. _args = { ... }
  2.  
  3. m = peripheral.find("monitor")
  4. if m then
  5.     m.setTextScale(0.5)
  6. end
  7. term.clear()
  8.  
  9.  
  10. if #_args < 1 then
  11.     error("Usage: " .. shell.getRunningProgram() .. " <file location> <rate>")
  12.     return
  13. else
  14.     file_location = _args[1]
  15. end
  16. ws, err = http.websocket(file_location)
  17. if not ws then
  18.     print(err)
  19.     return
  20. end
  21. fdata = ""
  22. jd = os.epoch()
  23. pack = {}
  24. bytedata = {}
  25. sleep(1)
  26. print("Starting")
  27. while true do
  28.     ws.send("hi")
  29.     data = ws.receive()
  30.     if data == "done" then
  31.         break
  32.     end
  33.     --print("Received chunk " .. #data .. " bytes big")
  34.     for i=1, #data do
  35.         table.insert(bytedata, data:sub(i,i))
  36.         if #bytedata >= 65536 then
  37.             table.insert(pack, bytedata)
  38.             bytedata = {}
  39.         end
  40.     end
  41.     if os.epoch() - jd > 72000 then
  42.         print("Downloaded " .. (#(pack[1])*#pack)/1024000 .. "MB")
  43.         jd = os.epoch()
  44.     end
  45. end
  46. print("Hello there")
  47. if #bytedata > 0 then
  48.     table.insert(pack, bytedata)
  49. end
  50. print("Loaded " .. #pack .. " packs with " .. #pack[1] .. "kb each")
  51. ws.close()
  52. local seek = -1
  53.  
  54. --pack = {{111,112},{113,114}}
  55.  
  56. function fileread()
  57.     seek = seek + 1
  58.     if pack[1] then
  59.         local index = (seek%#pack[1])+1
  60.         if index == #pack[1] then
  61.             v = pack[1][index]
  62.             table.remove(pack, 1)
  63.             return string.byte(v)
  64.         else
  65.             return string.byte(pack[1][index])
  66.         end
  67.     else
  68.         return nil
  69.     end
  70. end
  71. function converthex(...)
  72.     local _hex_string = ""
  73.     for i,k in pairs(arg) do
  74.         if i ~= "n" then
  75.             local _hex_val = string.format('%x', k)
  76.             _hex_val = string.rep("0", 2 - #_hex_val ) .. _hex_val
  77.             _hex_string = _hex_string .. _hex_val
  78.         end
  79.     end
  80.     return _hex_string
  81. end
  82.  
  83. function reverse(...)
  84.     reversed = {}
  85.     for i,k in pairs(arg) do
  86.         if i ~= "n" then
  87.             table.insert(reversed, 1, k)
  88.         end
  89.     end
  90.     return table.unpack(reversed)
  91. end
  92.  
  93. function toMinutes(_nSeconds)
  94.     local _s = tostring(_nSeconds % 60)
  95.     local _m = tostring(math.ceil(_nSeconds - _s) / 60)
  96.     return ("0"):rep(2-#_m) .. _m .. ":" .. ("0"):rep(2-#_s) .. _s
  97. end
  98.  
  99. -- construct blitmap
  100.  
  101.  
  102. -- read image size
  103. loc = 0
  104. local width = tonumber(converthex(reverse(fileread(), fileread())), 16)
  105. loc = loc + 2
  106. local height = tonumber(converthex(reverse(fileread(), fileread())), 16)
  107. loc = loc + 2
  108. local frames = {}
  109. local framecount = 0
  110. local jstart = os.epoch()
  111.  
  112.  
  113. --print("Read header to bytes" .. tostring(loc))
  114. while true do
  115.     local frameinfo = {}
  116.     local blitmap = {}
  117.  
  118.     -- read palette data
  119.     local palette = {}
  120.     for i=1, 16, 1 do
  121.         _k = 2^(i-1)
  122.         palette[_k] = {}
  123.         palette[_k]['r'] = fileread()
  124.         loc = loc + 1
  125.         if palette[_k]['r'] == nil then
  126.             print("Breaking")
  127.             break
  128.         end
  129.         palette[_k]['r'] = palette[_k]['r']/255
  130.         palette[_k]['g'] = fileread()/255
  131.         palette[_k]['b'] = fileread()/255
  132.         loc = loc + 2
  133.     end
  134.     --print("Read palette to bytes " .. tostring(loc))
  135.     if palette[_k]['r'] == nil then
  136.         break
  137.     end
  138.  
  139.     -- read image data
  140.     local byte = fileread()
  141.     loc = loc + 1
  142.     local cstart = os.epoch()
  143.     local px, py, pc = 0, 0, 0
  144.  
  145.     -- px: pixel x coord
  146.     -- py: pixel y coord
  147.     -- pc: pixel count
  148.     for i=1, ((width*height)/2)-1, 1 do
  149.         breakall = false
  150.         if byte then
  151.             local hex_value = converthex(byte)
  152.             for i=1, 2 do
  153.  
  154.                 px = math.fmod(pc, width)
  155.                 py = math.floor(( pc / width ) % height)
  156.                 pc = pc + 1
  157.  
  158.                 if px == width - 1 and py == height - 1 then
  159.                     breakall = true
  160.                 elseif blitmap[py] == nil then
  161.                     blitmap[py] = ""
  162.                 end
  163.  
  164.                 blitmap[py] = blitmap[py] .. hex_value:sub(i,i)
  165.             end
  166.             byte = fileread()
  167.             loc = loc + 1
  168.  
  169.             if os.epoch() - cstart >= 72000 then
  170.                 print(math.ceil((pc/(width*height))*100) .. "% Remaining: " .. toMinutes(math.ceil(((os.epoch()-cstart)/7200)*(1/(pc/(width*height))))) .. " - " .. tostring(loc))
  171.                 cstart = os.epoch()
  172.                 sleep(0)
  173.             end
  174.         else
  175.             breakall = true
  176.         end
  177.     end
  178.     frameinfo['data'] = blitmap
  179.     frameinfo['palette'] = palette
  180.     frames[framecount] = frameinfo
  181.  
  182.     framecount = framecount + 1
  183.  
  184.     print("Parsed frame " .. tostring(framecount) .. ". Current location: " .. tostring(loc))
  185.     if framecount % 10 == 1 then
  186.         sleep(0)
  187.     end
  188. end
  189.  
  190. print("Load time: " .. toMinutes(math.ceil((os.epoch() - jstart)/72000)) .. ". " .. math.ceil(width*height/((os.epoch() - jstart)/72000)) .. "pps")
  191. print(width .. "x" .. height)
  192. io.read()
  193.  
  194. function renderLine(bytemap, x, y, w, h)
  195.     for i=0, h do
  196.         if bytemap[y+i-1] == nil then
  197.             bytemap[y+i-1] = ("0"):rep(width)
  198.         end
  199.         ldata = bytemap[y+i-1]:sub(x, x+w)
  200.         ldata = ldata .. ("0"):rep(w-#ldata-1)
  201.         term.setCursorPos(1, i+1)
  202.         term.blit((" "):rep(#ldata), (" "):rep(#ldata), ldata)
  203.     end
  204. end
  205.  
  206.  
  207. sw, sh = term.getSize()
  208. frame_count = 0
  209. while true do
  210.     frame_number = frame_count % #frames
  211.  
  212.     for i,k in pairs(frames[frame_number]['palette']) do
  213.         term.setPaletteColour(i, k['r'], k['g'], k['b'])
  214.     end
  215.        
  216.     renderLine(frames[frame_number]['data'], 1, 1, sw, sh)
  217.     frame_count = frame_count + 1
  218.     sleep(tonumber(_args[2]))
  219. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement