240 lines
No EOL
6 KiB
Lua
240 lines
No EOL
6 KiB
Lua
local util = require("util")
|
|
local sp = require("string_pack")
|
|
|
|
-- 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 sp.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 = sp.unpack(">b", string.sub(self.bufferData, self.offset, self.offset + 1))
|
|
self.offset = self.offset + 1
|
|
return value
|
|
end
|
|
|
|
function bufferStuff.Reader:readBool()
|
|
local value = sp.unpack(">b", string.sub(self.bufferData, self.offset, self.offset + 1))
|
|
self.offset = self.offset + 1
|
|
return value >= 1
|
|
end
|
|
|
|
function bufferStuff.Reader:readUByte()
|
|
local value = sp.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 = sp.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(sp.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 = sp.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 = sp.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 = sp.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 = sp.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 = sp.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 = sp.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 = sp.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 = sp.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 = sp.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(sp.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 = sp.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(sp.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2)))
|
|
self.offset = self.offset + 2
|
|
end
|
|
|
|
return text
|
|
end
|
|
|
|
return bufferStuff |