Advertisement
osmarks

PotatOS Version v2

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