=======================================Defining the container table
local
PMLib = {}
function
PMLib.FindLen(a_str)
return
string.byte
(a_str,1) * 256 +
string.byte
(a_str,2), a_str:sub(3)
end
function
PMLib:new(a_Channel)
local
obj = {}
setmetatable
(obj, PMLib)
self.__index = self
obj.Channel = a_Channel
obj.Msg =
""
return
obj
end
function
PMLib:writeUTF(a_str)
assert
(type(a_str) ==
"string"
,
"Not a string!"
)
local
len = a_str:len()
self.Msg = self.Msg ..
string.char
((len-127)/256,len % 256) .. a_str
return
self
end
function
PMLib:writeByte(n)
assert
(
not
(n > 127),n ..
" is too large"
)
assert
(
not
(n < -128),n ..
" is too small"
)
n = (n < 0)
and
(256 + n)
or
n
self.Msg = self.Msg ..
string.char
(n%256)
return
self
end
function
PMLib:writeShort(n)
assert
(
not
(n > 32767),n ..
" is too large"
)
assert
(
not
(n < -32768),n ..
" is too small"
)
n = (n < 0)
and
(65536 + n)
or
n
self.Msg = self.Msg ..
string.char
((
math.modf
(n/256))%256,n%256)
return
self
end
function
PMLib:writeInt(n)
assert
(
not
(n > 2147483647),n ..
" is too large"
)
assert
(
not
(n < -2147483648),n ..
" is too small"
)
n = (n < 0)
and
(4294967296 + n)
or
n
self.Msg = self.Msg ..
string.char
((
math.modf
(n/16777216))%256, (
math.modf
(n/65536))%256, (
math.modf
(n/256))%256, n%256)
return
self
end
function
PMLib:writeLong(n)
assert
(
not
(n > 9223372036854775807),n ..
" is too large"
)
assert
(
not
(n < -9223372036854775808),n ..
" is too small"
)
n = (n < 0)
and
(1 + n)
or
n
self.Msg = self.Msg ..
string.char
((
math.modf
(n/72057594037927936))%256,(
math.modf
(n/281474976710656))%256,(
math.modf
(n/1099511627776))%256,(
math.modf
(n/4294967296))%256,(
math.modf
(n/16777216))%256,(
math.modf
(n/65536))%256,(
math.modf
(n/256))%256,n%256)
return
self
end
function
PMLib:writeBool(a_state)
assert
(type(a_state) ==
"boolean"
,
"Input is not a boolean!"
)
if
a_state
then
self.Msg = self.Msg ..
string.char
(0,1,1)
else
self.Msg = self.Msg ..
string.char
(0,1,0)
end
return
self
end
function
PMLib:GetOut()
return
self.Channel, self.Msg
end
function
PMLib:startparse(a_mess)
local
obj = {}
setmetatable
(obj, PMLib)
self.__index = self
obj.Msg = a_mess
obj.arr = {}
return
obj
end
function
PMLib:readUTF()
local
len =
tonumber
(
string.byte
(self.Msg,1)) * 256 +
tonumber
(
string.byte
(self.Msg,2))
self.arr[#self.arr + 1] = self.Msg:sub(3,len+2)
self.Msg = self.Msg:sub(len+3)
return
self
end
function
PMLib:readByte()
local
n =
tonumber
(self.Msg:byte(1))
n = (n > 127)
and
(n - 256)
or
n
self.arr[#self.arr + 1] = n
self.Msg = self.Msg:sub(4)
return
self
end
function
PMLib:readShort()
local
n =
tonumber
(self.Msg:byte(1))*256+
tonumber
(self.Msg:byte(2))
n = (n > 32767)
and
(n - 65536)
or
n
self.arr[#self.arr + 1] = n
self.Msg = self.Msg:sub(5)
return
self
end
function
PMLib:readInt()
local
n =
tonumber
(self.Msg:byte(1))*16777216 +
tonumber
(self.Msg:byte(2))*65536 +
tonumber
(self.Msg:byte(3))*256 +
tonumber
(self.Msg:byte(4))
n = (n > 2147483647)
and
(n - 4294967296)
or
n
self.arr[#self.arr + 1] = n
self.Msg = self.Msg:sub(7)
return
self
end
function
PMLib:readLong()
local
n =
tonumber
(self.Msg:byte(1))*72057594037927936 +
tonumber
(self.Msg:byte(2))*281474976710656 +
tonumber
(self.Msg:byte(3))*1099511627776 +
tonumber
(self.Msg:byte(4))*4294967296 +
tonumber
(self.Msg:byte(5))*16777216 +
tonumber
(self.Msg:byte(6))*65536 +
tonumber
(self.Msg:byte(9))*256 +
tonumber
(self.Msg:byte(10))
n = (n > 9223372036854775807)
and
(n - 18446744073709551616)
or
n, self.Msg:sub(11)
self.arr[#self.arr + 1] = n
self.Msg = self.Msg:sub(11)
return
self
end
function
PMLib:readBool()
assert
(self.Msg:byte() == 0
and
self.Msg:byte(2) == 1,
"Not a boolean value!"
)
assert
(self.Msg:byte(3) == 0
or
self.Msg:byte(3) == 1,
"Not a boolean value!"
)
self.arr[#self.arr + 1] = self.Msg:byte(3) == 0
and
false
or
true
self.Msg = self.Msg:sub(4)
return
self
end
function
PMLib:GetIn()
return
self.arr
end
--[[
function
PMLib:writeFloat(a_num)
end
function
PMLib:readFloat()
local
bytestring = self.Msg:sub(1,4)
self.Msg = self.Msg:sub(5)
local
num = ZLib.readFloat
self.arr[#self.arr + 1] = num
return
self
end
function
PMLib:writeDouble(a_num)
self.Msg = self.Msg .. ZLib.double2ByteString(a_num)
end
function
PMLib:readDouble()
local
bytestring = self.Msg:sub(1,8)
self.Msg = self.Msg:sub(9)
self.arr[#self.arr + 1] = ZLib.byteString2Double(bytestring)
return
self
end
--[[ Unused because ****
this
...
function
PMLib:readUTF16()
end
function
PMLib:writeUTF16(a_char)
end
return
PMLib