Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Base64 api by LBPHacker
- local lookup_V2C = {}
- for ixChar = 65, 90 do lookup_V2C[ixChar - 65] = string.char(ixChar) end
- for ixChar = 97, 122 do lookup_V2C[ixChar - 71] = string.char(ixChar) end
- for ixChar = 48, 57 do lookup_V2C[ixChar + 4] = string.char(ixChar) end
- lookup_V2C[62] = "+"
- lookup_V2C[63] = "/"
- local lookup_C2V = {}
- for key, value in pairs(lookup_V2C) do lookup_C2V[value] = key end
- function encode(data)
- local result = ""
- for ix = 1, #data, 3 do
- local all24 = data:sub(ix, ix):byte() * 65536 + (data:sub(ix + 1, ix + 1):byte() or 0) * 256 + (data:sub(ix + 2, ix + 2):byte() or 0)
- result = result .. lookup_V2C[bit.band(all24, 16515072) / 262144] .. lookup_V2C[bit.band(all24, 258048) / 4096] .. lookup_V2C[bit.band(all24, 4032) / 64] .. lookup_V2C[bit.band(all24, 63)]
- end
- local padding = (3 - data:len() % 3) % 3
- return result:sub(1, result:len() - padding) .. string.rep("=", padding)
- end
- function decode(data)
- local result = ""
- for ix = 1, #data, 4 do
- local all24 = lookup_C2V[data:sub(ix, ix)] * 262144 + (lookup_C2V[data:sub(ix + 1, ix + 1)] or 0) * 4096 + (lookup_C2V[data:sub(ix + 2, ix + 2)] or 0) * 64 + (lookup_C2V[data:sub(ix + 3, ix + 3)] or 0)
- result = result .. string.char(bit.band(all24, 16711680) / 65536) .. string.char(bit.band(all24, 65280) / 256) .. string.char(bit.band(all24, 255))
- end
- return result:sub(1, result:len() - #data:match("^.-(=*)$"))
- end
- -- My code below:
- local b = peripheral.find("bitnet_tower") or peripheral.find("bitnet_antenna")
- local function ebn()
- if not b then
- error("No bitnet found!", 0)
- end
- end
- function sendMessage(message, useBase)
- useBase = useBase or false
- if type(useBase) ~= "boolean" then
- useBase = false
- end
- ebn()
- if type(message) == "table" and useBase == true then
- message = "t.bntp::"..encode(textutils.serialize(message))
- elseif type(message) ~= "table" and useBase == true then
- message = "bntp::"..encode(message)
- end
- b.transmit(message)
- end
- function receive(timeout)
- ebn()
- local filter = nil
- if type(timeout) ~= "number" then
- filter = "bitnet_message"
- else
- local tid = os.startTimer(timeout)
- end
- while true do
- local e,s,m,d = os.pullEvent(filter)
- if e == "bitnet_message" then
- if m:sub(1,8) == "t.bntp::" then
- m = textutils.unserialize(decode(m:sub(9)))
- elseif m:sub(1, 6) == "bntp::" then
- m = decode(m:sub(7))
- end
- return m,d
- elseif e == "timer" and s == tid then
- return nil
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment