2012-08-30 07:59:10 +02:00
|
|
|
function OutputMessage:addData(data)
|
|
|
|
if type(data) == 'boolean' then
|
|
|
|
self:addU8(NetworkMessageTypes.Boolean)
|
|
|
|
self:addU8(booleantonumber(data))
|
|
|
|
elseif type(data) == 'number' then
|
2013-01-16 17:20:17 +01:00
|
|
|
if math.isu8(data) then
|
|
|
|
self:addU8(NetworkMessageTypes.U8)
|
|
|
|
self:addU8(data)
|
|
|
|
elseif math.isu16(data) then
|
|
|
|
self:addU8(NetworkMessageTypes.U16)
|
|
|
|
self:addU16(data)
|
|
|
|
elseif math.isu32(data) then
|
|
|
|
self:addU8(NetworkMessageTypes.U32)
|
|
|
|
self:addU32(data)
|
|
|
|
elseif math.isu64(data) then
|
|
|
|
self:addU8(NetworkMessageTypes.U64)
|
|
|
|
self:addU64(data)
|
|
|
|
else -- negative or non integer numbers
|
|
|
|
self:addU8(NetworkMessageTypes.NumberString)
|
|
|
|
self:addString(tostring(data))
|
|
|
|
end
|
2012-08-30 07:59:10 +02:00
|
|
|
elseif type(data) == 'string' then
|
|
|
|
self:addU8(NetworkMessageTypes.String)
|
|
|
|
self:addString(data)
|
|
|
|
elseif type(data) == 'table' then
|
|
|
|
self:addU8(NetworkMessageTypes.Table)
|
|
|
|
self:addTable(data)
|
|
|
|
else
|
|
|
|
perror('Invalid data type ' .. type(data))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function OutputMessage:addTable(data)
|
|
|
|
local size = 0
|
|
|
|
|
2013-01-16 17:20:17 +01:00
|
|
|
-- reserve for size (should be addData, find a way to use it further)
|
2012-08-30 07:59:10 +02:00
|
|
|
local sizePos = self:getWritePos()
|
2013-01-16 17:20:17 +01:00
|
|
|
self:addU16(size)
|
2012-08-30 07:59:10 +02:00
|
|
|
local sizeSize = self:getWritePos() - sizePos
|
|
|
|
|
|
|
|
-- add values
|
|
|
|
for key,value in pairs(data) do
|
|
|
|
self:addData(key)
|
|
|
|
self:addData(value)
|
|
|
|
size = size + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
-- write size
|
|
|
|
local currentPos = self:getWritePos()
|
|
|
|
self:setWritePos(sizePos)
|
2013-01-16 17:20:17 +01:00
|
|
|
self:addU16(size)
|
2012-08-30 07:59:10 +02:00
|
|
|
|
|
|
|
-- fix msg size and go back to end
|
|
|
|
self:setMessageSize(self:getMessageSize() - sizeSize)
|
|
|
|
self:setWritePos(currentPos)
|
2013-01-16 17:20:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function OutputMessage:addColor(color)
|
|
|
|
self:addU8(color.r)
|
|
|
|
self:addU8(color.g)
|
|
|
|
self:addU8(color.b)
|
|
|
|
self:addU8(color.a)
|
|
|
|
end
|
2013-01-30 22:08:43 +01:00
|
|
|
|
|
|
|
function OutputMessage:addPosition(position)
|
|
|
|
self:addU16(position.x)
|
|
|
|
self:addU16(position.y)
|
|
|
|
self:addU8(position.z)
|
|
|
|
end
|