233 lines
No EOL
5.9 KiB
Lua
233 lines
No EOL
5.9 KiB
Lua
local util = require("util")
|
|
|
|
-- 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()
|
|
return string.pack(table.concat(self.typeList, ""), table.unpack(self.dataValues))
|
|
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()
|
|
local value = string.unpack(">b", string.sub(self.bufferData, self.offset, self.offset + 1))
|
|
self.offset = self.offset + 1
|
|
return value
|
|
end
|
|
|
|
function bufferStuff.Reader:readUByte()
|
|
local value = string.unpack(">B", string.sub(self.bufferData, self.offset, self.offset + 1))
|
|
self.offset = self.offset + 1
|
|
return value
|
|
end
|
|
|
|
function bufferStuff.Reader:readShortString()
|
|
local length = string.unpack(">B", string.sub(self.bufferData, self.offset, self.offset + 1))
|
|
self.offset = self.offset + 1
|
|
local text = ""
|
|
|
|
for i = 1, length do
|
|
text = text .. string.char(string.unpack(">B", string.sub(self.bufferData, self.offset, self.offset + 1)))
|
|
self.offset = self.offset + 1
|
|
end
|
|
|
|
return text
|
|
end
|
|
|
|
function bufferStuff.Reader:readShort()
|
|
local value = string.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2))
|
|
self.offset = self.offset + 2
|
|
return value
|
|
end
|
|
|
|
function bufferStuff.Reader:readUShort()
|
|
local value = string.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2))
|
|
self.offset = self.offset + 2
|
|
return value
|
|
end
|
|
|
|
function bufferStuff.Reader:readInt()
|
|
local value = string.unpack(">i", string.sub(self.bufferData, self.offset, self.offset + 4))
|
|
self.offset = self.offset + 4
|
|
return value
|
|
end
|
|
|
|
function bufferStuff.Reader:readUInt()
|
|
local value = string.unpack(">I", string.sub(self.bufferData, self.offset, self.offset + 4))
|
|
self.offset = self.offset + 4
|
|
return value
|
|
end
|
|
|
|
function bufferStuff.Reader:readLong()
|
|
local value = string.unpack(">l", string.sub(self.bufferData, self.offset, self.offset + 8))
|
|
self.offset = self.offset + 8
|
|
return value
|
|
end
|
|
|
|
function bufferStuff.Reader:readULong()
|
|
local value = string.unpack(">L", string.sub(self.bufferData, self.offset, self.offset + 8))
|
|
self.offset = self.offset + 8
|
|
return value
|
|
end
|
|
|
|
function bufferStuff.Reader:readFloat()
|
|
local value = string.unpack(">f", string.sub(self.bufferData, self.offset, self.offset + 4))
|
|
self.offset = self.offset + 4
|
|
return value
|
|
end
|
|
|
|
function bufferStuff.Reader:readDouble()
|
|
local value = string.unpack(">d", string.sub(self.bufferData, self.offset, self.offset + 8))
|
|
self.offset = self.offset + 8
|
|
return value
|
|
end
|
|
|
|
function bufferStuff.Reader:readString()
|
|
local length = string.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2))
|
|
self.offset = self.offset + 2
|
|
local text = ""
|
|
|
|
for i = 1, length do
|
|
text = text .. string.char(string.unpack(">B", string.sub(self.bufferData, self.offset, self.offset + 1)))
|
|
self.offset = self.offset + 1
|
|
end
|
|
|
|
return text
|
|
end
|
|
|
|
function bufferStuff.Reader:readString16()
|
|
local length = string.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2))
|
|
self.offset = self.offset + 2
|
|
local text = ""
|
|
|
|
for i = 1, length do
|
|
text = text .. string.char(string.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2)))
|
|
self.offset = self.offset + 2
|
|
end
|
|
|
|
return text
|
|
end
|
|
|
|
return bufferStuff |