turtle5204

BNTP api

May 30th, 2015
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.48 KB | None | 0 0
  1. --Base64 api by LBPHacker
  2. local lookup_V2C = {}
  3. for ixChar = 65, 90 do lookup_V2C[ixChar - 65] = string.char(ixChar) end
  4. for ixChar = 97, 122 do lookup_V2C[ixChar - 71] = string.char(ixChar) end
  5. for ixChar = 48, 57 do lookup_V2C[ixChar + 4] = string.char(ixChar) end
  6. lookup_V2C[62] = "+"
  7. lookup_V2C[63] = "/"
  8. local lookup_C2V = {}
  9. for key, value in pairs(lookup_V2C) do lookup_C2V[value] = key end
  10.  
  11. function encode(data)
  12.     local result = ""
  13.     for ix = 1, #data, 3 do
  14.         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)
  15.         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)]
  16.     end
  17.     local padding = (3 - data:len() % 3) % 3
  18.     return result:sub(1, result:len() - padding) .. string.rep("=", padding)
  19. end
  20.  
  21. function decode(data)
  22.     local result = ""
  23.     for ix = 1, #data, 4 do
  24.         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)
  25.         result = result .. string.char(bit.band(all24, 16711680) / 65536) .. string.char(bit.band(all24, 65280) / 256) .. string.char(bit.band(all24, 255))
  26.     end
  27.     return result:sub(1, result:len() - #data:match("^.-(=*)$"))
  28. end
  29.  
  30.  
  31.  
  32.  
  33. -- My code below:
  34.  
  35. local b = peripheral.find("bitnet_tower") or peripheral.find("bitnet_antenna")
  36.  
  37. local function ebn()
  38.  if not b then
  39.   error("No bitnet found!", 0)
  40.  end
  41. end
  42.  
  43. function sendMessage(message, useBase)
  44.  useBase = useBase or false
  45.  if type(useBase) ~= "boolean" then
  46.   useBase = false
  47.  end
  48.  
  49.  ebn()
  50.  if type(message) == "table" and useBase == true then
  51.   message = "t.bntp::"..encode(textutils.serialize(message))
  52.  elseif type(message) ~= "table" and useBase == true then
  53.   message = "bntp::"..encode(message)
  54.  end
  55.  
  56.  b.transmit(message)
  57. end
  58.  
  59.  
  60. function receive(timeout)
  61.     ebn()
  62.     local filter = nil
  63.     if type(timeout) ~= "number" then
  64.         filter = "bitnet_message"
  65.     else
  66.         local tid = os.startTimer(timeout)
  67.     end
  68.  
  69.     while true do
  70.         local e,s,m,d = os.pullEvent(filter)
  71.  
  72.         if e == "bitnet_message" then
  73.             if m:sub(1,8) == "t.bntp::" then
  74.                 m = textutils.unserialize(decode(m:sub(9)))
  75.             elseif m:sub(1, 6) == "bntp::" then
  76.                 m = decode(m:sub(7))
  77.             end
  78.             return m,d
  79.         elseif e == "timer" and s == tid then
  80.             return nil
  81.         end
  82.     end
  83.  
  84. end
Advertisement
Add Comment
Please, Sign In to add comment