Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- _args = { ... }
- m = peripheral.find("monitor")
- if m then
- m.setTextScale(0.5)
- end
- term.clear()
- if #_args < 1 then
- error("Usage: " .. shell.getRunningProgram() .. " <file location> <rate>")
- return
- else
- file_location = _args[1]
- end
- ws, err = http.websocket(file_location)
- if not ws then
- print(err)
- return
- end
- fdata = ""
- jd = os.epoch()
- pack = {}
- bytedata = {}
- sleep(1)
- print("Starting")
- while true do
- ws.send("hi")
- data = ws.receive()
- if data == "done" then
- break
- end
- --print("Received chunk " .. #data .. " bytes big")
- for i=1, #data do
- table.insert(bytedata, data:sub(i,i))
- if #bytedata >= 65536 then
- table.insert(pack, bytedata)
- bytedata = {}
- end
- end
- if os.epoch() - jd > 72000 then
- print("Downloaded " .. (#(pack[1])*#pack)/1024000 .. "MB")
- jd = os.epoch()
- end
- end
- print("Hello there")
- if #bytedata > 0 then
- table.insert(pack, bytedata)
- end
- print("Loaded " .. #pack .. " packs with " .. #pack[1] .. "kb each")
- ws.close()
- local seek = -1
- --pack = {{111,112},{113,114}}
- function fileread()
- seek = seek + 1
- if pack[1] then
- local index = (seek%#pack[1])+1
- if index == #pack[1] then
- v = pack[1][index]
- table.remove(pack, 1)
- return string.byte(v)
- else
- return string.byte(pack[1][index])
- end
- else
- return nil
- end
- end
- function converthex(...)
- local _hex_string = ""
- for i,k in pairs(arg) do
- if i ~= "n" then
- local _hex_val = string.format('%x', k)
- _hex_val = string.rep("0", 2 - #_hex_val ) .. _hex_val
- _hex_string = _hex_string .. _hex_val
- end
- end
- return _hex_string
- end
- function reverse(...)
- reversed = {}
- for i,k in pairs(arg) do
- if i ~= "n" then
- table.insert(reversed, 1, k)
- end
- end
- return table.unpack(reversed)
- end
- function toMinutes(_nSeconds)
- local _s = tostring(_nSeconds % 60)
- local _m = tostring(math.ceil(_nSeconds - _s) / 60)
- return ("0"):rep(2-#_m) .. _m .. ":" .. ("0"):rep(2-#_s) .. _s
- end
- -- construct blitmap
- -- read image size
- loc = 0
- local width = tonumber(converthex(reverse(fileread(), fileread())), 16)
- loc = loc + 2
- local height = tonumber(converthex(reverse(fileread(), fileread())), 16)
- loc = loc + 2
- local frames = {}
- local framecount = 0
- local jstart = os.epoch()
- --print("Read header to bytes" .. tostring(loc))
- while true do
- local frameinfo = {}
- local blitmap = {}
- -- read palette data
- local palette = {}
- for i=1, 16, 1 do
- _k = 2^(i-1)
- palette[_k] = {}
- palette[_k]['r'] = fileread()
- loc = loc + 1
- if palette[_k]['r'] == nil then
- print("Breaking")
- break
- end
- palette[_k]['r'] = palette[_k]['r']/255
- palette[_k]['g'] = fileread()/255
- palette[_k]['b'] = fileread()/255
- loc = loc + 2
- end
- --print("Read palette to bytes " .. tostring(loc))
- if palette[_k]['r'] == nil then
- break
- end
- -- read image data
- local byte = fileread()
- loc = loc + 1
- local cstart = os.epoch()
- local px, py, pc = 0, 0, 0
- -- px: pixel x coord
- -- py: pixel y coord
- -- pc: pixel count
- for i=1, ((width*height)/2)-1, 1 do
- breakall = false
- if byte then
- local hex_value = converthex(byte)
- for i=1, 2 do
- px = math.fmod(pc, width)
- py = math.floor(( pc / width ) % height)
- pc = pc + 1
- if px == width - 1 and py == height - 1 then
- breakall = true
- elseif blitmap[py] == nil then
- blitmap[py] = ""
- end
- blitmap[py] = blitmap[py] .. hex_value:sub(i,i)
- end
- byte = fileread()
- loc = loc + 1
- if os.epoch() - cstart >= 72000 then
- print(math.ceil((pc/(width*height))*100) .. "% Remaining: " .. toMinutes(math.ceil(((os.epoch()-cstart)/7200)*(1/(pc/(width*height))))) .. " - " .. tostring(loc))
- cstart = os.epoch()
- sleep(0)
- end
- else
- breakall = true
- end
- end
- frameinfo['data'] = blitmap
- frameinfo['palette'] = palette
- frames[framecount] = frameinfo
- framecount = framecount + 1
- print("Parsed frame " .. tostring(framecount) .. ". Current location: " .. tostring(loc))
- if framecount % 10 == 1 then
- sleep(0)
- end
- end
- print("Load time: " .. toMinutes(math.ceil((os.epoch() - jstart)/72000)) .. ". " .. math.ceil(width*height/((os.epoch() - jstart)/72000)) .. "pps")
- print(width .. "x" .. height)
- io.read()
- function renderLine(bytemap, x, y, w, h)
- for i=0, h do
- if bytemap[y+i-1] == nil then
- bytemap[y+i-1] = ("0"):rep(width)
- end
- ldata = bytemap[y+i-1]:sub(x, x+w)
- ldata = ldata .. ("0"):rep(w-#ldata-1)
- term.setCursorPos(1, i+1)
- term.blit((" "):rep(#ldata), (" "):rep(#ldata), ldata)
- end
- end
- sw, sh = term.getSize()
- frame_count = 0
- while true do
- frame_number = frame_count % #frames
- for i,k in pairs(frames[frame_number]['palette']) do
- term.setPaletteColour(i, k['r'], k['g'], k['b'])
- end
- renderLine(frames[frame_number]['data'], 1, 1, sw, sh)
- frame_count = frame_count + 1
- sleep(tonumber(_args[2]))
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement