cc-mc-server/mc/bufferstuff.lua

240 lines
6 KiB
Lua
Raw Permalink Normal View History

2024-07-12 16:58:47 +01:00
local util = require("util")
2024-07-14 14:23:30 +01:00
local sp = require("string_pack")
2024-07-12 16:58:47 +01:00
-- quick and dirty BIG ENDIAN ONLY port of bufferstuff to lua
local bufferStuff = {
Writer = {},
Reader = {}
}
-- WRITER
local defaultsTableWriter = {
typeList = {">"},
dataValues = {}
}
function bufferStuff.Writer:new()
local newTable = util.deepCopyTable(defaultsTableWriter) or {}
-- Set this table as this """class"""'s metatable
setmetatable(newTable, self)
self.__index = self
return newTable
end
function bufferStuff.Writer:toBuffer()
2024-07-14 14:23:30 +01:00
return sp.pack(table.concat(self.typeList, ""), table.unpack(self.dataValues))
2024-07-12 16:58:47 +01:00
end
function bufferStuff.Writer:writeByte(value)
table.insert(self.typeList, "b")
table.insert(self.dataValues, value)
end
function bufferStuff.Writer:writeUByte(value)
table.insert(self.typeList, "B")
table.insert(self.dataValues, value)
end
function bufferStuff.Writer:writeBool(value)
table.insert(self.typeList, "B")
table.insert(self.dataValues, value and 1 or 0)
end
function bufferStuff.Writer:writeShort(value)
table.insert(self.typeList, "h")
table.insert(self.dataValues, value)
end
function bufferStuff.Writer:writeUShort(value)
table.insert(self.typeList, "H")
table.insert(self.dataValues, value)
end
function bufferStuff.Writer:writeInt(value)
table.insert(self.typeList, "i")
table.insert(self.dataValues, value)
end
function bufferStuff.Writer:writeUInt(value)
table.insert(self.typeList, "I")
table.insert(self.dataValues, value)
end
function bufferStuff.Writer:writeLong(value)
table.insert(self.typeList, "l")
table.insert(self.dataValues, value)
end
function bufferStuff.Writer:writeULong(value)
table.insert(self.typeList, "L")
table.insert(self.dataValues, value)
end
function bufferStuff.Writer:writeFloat(value)
table.insert(self.typeList, "f")
table.insert(self.dataValues, value)
end
function bufferStuff.Writer:writeDouble(value)
table.insert(self.typeList, "d")
table.insert(self.dataValues, value)
end
function bufferStuff.Writer:writeShortString(value)
table.insert(self.typeList, "B")
table.insert(self.dataValues, #value)
for char in string.gmatch(value, ".") do
table.insert(self.typeList, "B")
table.insert(self.dataValues, string.byte(char))
end
end
function bufferStuff.Writer:writeString(value)
table.insert(self.typeList, "H")
table.insert(self.dataValues, #value)
for char in string.gmatch(value, ".") do
table.insert(self.typeList, "B")
table.insert(self.dataValues, string.byte(char))
end
end
function bufferStuff.Writer:writeString16(value)
table.insert(self.typeList, "H")
table.insert(self.dataValues, #value)
for char in string.gmatch(value, ".") do
table.insert(self.typeList, "H")
table.insert(self.dataValues, string.byte(char))
end
end
-- READER
local defaultsTableReader = {
bufferData = "",
offset = 1
}
function bufferStuff.Reader:new(data)
local newTable = util.deepCopyTable(defaultsTableReader) or {}
newTable.bufferData = data
-- Set this table as this """class"""'s metatable
setmetatable(newTable, self)
self.__index = self
return newTable
end
function bufferStuff.Reader:readByte()
2024-07-14 14:23:30 +01:00
local value = sp.unpack(">b", string.sub(self.bufferData, self.offset, self.offset + 1))
2024-07-12 16:58:47 +01:00
self.offset = self.offset + 1
return value
end
2024-07-12 19:49:56 +01:00
function bufferStuff.Reader:readBool()
2024-07-14 14:23:30 +01:00
local value = sp.unpack(">b", string.sub(self.bufferData, self.offset, self.offset + 1))
2024-07-12 19:49:56 +01:00
self.offset = self.offset + 1
return value >= 1
end
2024-07-12 16:58:47 +01:00
function bufferStuff.Reader:readUByte()
2024-07-14 14:23:30 +01:00
local value = sp.unpack(">B", string.sub(self.bufferData, self.offset, self.offset + 1))
2024-07-12 16:58:47 +01:00
self.offset = self.offset + 1
return value
end
function bufferStuff.Reader:readShortString()
2024-07-14 14:23:30 +01:00
local length = sp.unpack(">B", string.sub(self.bufferData, self.offset, self.offset + 1))
2024-07-12 16:58:47 +01:00
self.offset = self.offset + 1
local text = ""
for i = 1, length do
2024-07-14 14:23:30 +01:00
text = text .. string.char(sp.unpack(">B", string.sub(self.bufferData, self.offset, self.offset + 1)))
2024-07-12 16:58:47 +01:00
self.offset = self.offset + 1
end
return text
end
function bufferStuff.Reader:readShort()
2024-07-14 14:23:30 +01:00
local value = sp.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2))
2024-07-12 16:58:47 +01:00
self.offset = self.offset + 2
return value
end
function bufferStuff.Reader:readUShort()
2024-07-14 14:23:30 +01:00
local value = sp.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2))
2024-07-12 16:58:47 +01:00
self.offset = self.offset + 2
return value
end
function bufferStuff.Reader:readInt()
2024-07-14 14:23:30 +01:00
local value = sp.unpack(">i", string.sub(self.bufferData, self.offset, self.offset + 4))
2024-07-12 16:58:47 +01:00
self.offset = self.offset + 4
return value
end
function bufferStuff.Reader:readUInt()
2024-07-14 14:23:30 +01:00
local value = sp.unpack(">I", string.sub(self.bufferData, self.offset, self.offset + 4))
2024-07-12 16:58:47 +01:00
self.offset = self.offset + 4
return value
end
function bufferStuff.Reader:readLong()
2024-07-14 14:23:30 +01:00
local value = sp.unpack(">l", string.sub(self.bufferData, self.offset, self.offset + 8))
2024-07-12 16:58:47 +01:00
self.offset = self.offset + 8
return value
end
function bufferStuff.Reader:readULong()
2024-07-14 14:23:30 +01:00
local value = sp.unpack(">L", string.sub(self.bufferData, self.offset, self.offset + 8))
2024-07-12 16:58:47 +01:00
self.offset = self.offset + 8
return value
end
function bufferStuff.Reader:readFloat()
2024-07-14 14:23:30 +01:00
local value = sp.unpack(">f", string.sub(self.bufferData, self.offset, self.offset + 4))
2024-07-12 16:58:47 +01:00
self.offset = self.offset + 4
return value
end
function bufferStuff.Reader:readDouble()
2024-07-14 14:23:30 +01:00
local value = sp.unpack(">d", string.sub(self.bufferData, self.offset, self.offset + 8))
2024-07-12 16:58:47 +01:00
self.offset = self.offset + 8
return value
end
function bufferStuff.Reader:readString()
2024-07-14 14:23:30 +01:00
local length = sp.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2))
2024-07-12 16:58:47 +01:00
self.offset = self.offset + 2
local text = ""
for i = 1, length do
2024-07-14 14:23:30 +01:00
text = text .. string.char(sp.unpack(">B", string.sub(self.bufferData, self.offset, self.offset + 1)))
2024-07-12 16:58:47 +01:00
self.offset = self.offset + 1
end
return text
end
function bufferStuff.Reader:readString16()
2024-07-14 14:23:30 +01:00
local length = sp.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2))
2024-07-12 16:58:47 +01:00
self.offset = self.offset + 2
local text = ""
for i = 1, length do
2024-07-14 14:23:30 +01:00
text = text .. string.char(sp.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2)))
2024-07-12 16:58:47 +01:00
self.offset = self.offset + 2
end
return text
end
return bufferStuff