Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ser = require "ser"
- if not ser then
- -- Download random library squid recommended
- local function a(b,c)local d=fs.open(b,"w")d.write(c)d.close()end;local function e(b)local d=fs.open(b,"r")local c=d.readAll()d.close()return c end;local function f(g)local h=http.get(g)local c=h.readAll()h.close()return c end;local function i(g,d)a(d,f(g))end;i("https://pastebin.com/raw/KXHSsHkt","ser")
- end
- local function move_to_start(tape)
- tape.seek(-tape.getSize())
- end
- local function get_byte(num, byte)
- return bit.band(bit.brshift(num, byte * 8), 0xFF)
- end
- local function from_bytes(b)
- local n = 0
- for ix, byte in pairs(b) do
- n = bit.bor(n, bit.blshift(byte, (ix - 1) * 8))
- end
- return n
- end
- local function write_string(tape, str, ignore_out_of_space)
- if tape.getSize() < (#str + 4) and not ignore_out_of_space then
- return false, "Not enough space"
- end
- move_to_start(tape)
- tape.write(get_byte(#str, 0))
- tape.write(get_byte(#str, 1))
- tape.write(get_byte(#str, 2))
- tape.write(get_byte(#str, 3))
- return tape.write(str)
- end
- local function read_string(tape)
- move_to_start(tape)
- local data_length = from_bytes {tape.read(), tape.read(), tape.read(), tape.read()}
- local data_remaining = data_length
- if data_length > (tape.getSize() - 4) then
- return false, "Invalid data length."
- end
- local data = ""
- repeat
- local block = tape.read(256)
- block = block:sub(1, data_remaining)
- data_remaining = data_remaining - #block
- data = data .. block -- for some reason, only 256 chars at once can be read?
- sleep()
- until data_remaining == 0
- return data
- end
- local function write(tape, dat, ignore_out_of_space)
- return write_string(tape, ser.serialize(dat), ignore_out_of_space)
- end
- local function read(tape)
- return ser.deserialize(read_string(tape))
- end
- return { read_string = read_string, read = read, write_string = write_string, write = write }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement