test[lua]: compatible field of inside protocol class in lua

This commit is contained in:
godotg committed 2023-10-19 13:14:11 +08:00
1 parent a20f744eb4
commit 4ebc3841e2
13 files changed
+557 -446

No files matched your search

@@ -1,43 +0,0 @@
-- @author godotg
local ObjectA = {}
function ObjectA:new(a, m, objectB)
local obj = {
a = a, -- int
m = m, -- java.util.Map<java.lang.Integer, java.lang.String>
objectB = objectB -- com.zfoo.protocol.packet.ObjectB
}
setmetatable(obj, self)
self.__index = self
return obj
end
function ObjectA:protocolId()
return 102
end
function ObjectA:write(buffer, packet)
if buffer:writePacketFlag(packet) then
return
end
buffer:writeInt(packet.a)
buffer:writeIntStringMap(packet.m)
buffer:writePacket(packet.objectB, 103)
end
function ObjectA:read(buffer)
if not(buffer:readBoolean()) then
return nil
end
local packet = ObjectA:new()
local result0 = buffer:readInt()
packet.a = result0
local map1 = buffer:readIntStringMap()
packet.m = map1
local result2 = buffer:readPacket(103)
packet.objectB = result2
return packet
end
return ObjectA
@@ -1,35 +0,0 @@
-- @author godotg
local ObjectB = {}
function ObjectB:new(flag)
local obj = {
flag = flag -- boolean
}
setmetatable(obj, self)
self.__index = self
return obj
end
function ObjectB:protocolId()
return 103
end
function ObjectB:write(buffer, packet)
if buffer:writePacketFlag(packet) then
return
end
buffer:writeBoolean(packet.flag)
end
function ObjectB:read(buffer)
if not(buffer:readBoolean()) then
return nil
end
local packet = ObjectB:new()
local result0 = buffer:readBoolean()
packet.flag = result0
return packet
end
return ObjectB
+47 -36
View File
@@ -1,48 +1,59 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using NUnit.Framework; using UnityEngine;
using Spring.Util;
using XLua; using XLua;
using zfoocs;
namespace Test.Editor.LuaTest public class Main : MonoBehaviour
{ {
public class LuaProtocolTest
public static readonly string TEST_PATH = "Assets/";
private void Start()
{ {
public static readonly string TEST_PATH = "Assets/Test/Editor/LuaTest/"; var luaEnv = new LuaEnv();
luaEnv.DoString("CS.UnityEngine.Debug.Log('hello world')");
var luaDebugBuilder = new StringBuilder();
// Rider的断点调试
// luaDebugBuilder.Append("package.cpath = package.cpath .. ';C:/Users/Administrator/AppData/Roaming/JetBrains/Rider2022.3/plugins/EmmyLua/debugger/emmy/windows/x64/?.dll'").Append(System.Environment.NewLine);
// luaDebugBuilder.Append("local dbg = require('emmy_core')").Append(System.Environment.NewLine);
// luaDebugBuilder.Append("dbg.tcpConnect('localhost', 9966)").Append(System.Environment.NewLine);
[Test] luaEnv.DoString(luaDebugBuilder.ToString());
public void ComplexObjectTest()
{
// 获取复杂对象的字节流
var complexObjectBytes = File.ReadAllBytes("D:\\zfoo\\protocol\\src\\test\\resources\\ComplexObject.bytes");
var luaEnv = new LuaEnv(); luaEnv.AddLoader(CustomLoader);
var luaDebugBuilder = new StringBuilder();
// Rider的断点调试
// luaDebugBuilder.Append("package.cpath = package.cpath .. ';C:/Users/godotg/AppData/Roaming/JetBrains/Rider2021.1/plugins/EmmyLua/classes/debugger/emmy/windows/x64/?.dll'").Append(FileUtils.LS);
// luaDebugBuilder.Append("local dbg = require('emmy_core')").Append(FileUtils.LS);
// luaDebugBuilder.Append("dbg.tcpListen('localhost', 9966)").Append(FileUtils.LS);
// luaDebugBuilder.Append("dbg.waitIDE()").Append(FileUtils.LS);
luaEnv.DoString(luaDebugBuilder.ToString()); var luaProtocolTestStr = File.ReadAllText( "Assets/main.lua");
luaEnv.DoString(luaProtocolTestStr, "main");
luaEnv.AddLoader(CustomLoader); LuaFunction byteBufferTestFunction = luaEnv.Global.Get<LuaFunction>("byteBufferTest");
byteBufferTestFunction.Call();
var luaProtocolTestStr = File.ReadAllText(TEST_PATH + "main.lua");
luaEnv.DoString(luaProtocolTestStr, "main"); // 获取复杂对象的字节流
var complexObjectBytes = File.ReadAllBytes("D:\\Project\\zfoo\\protocol\\src\\test\\resources\\complexObject.bytes");
LuaFunction byteBufferTestFunction = luaEnv.Global.Get<LuaFunction>("byteBufferTest"); LuaFunction complexObjectTestFuction = luaEnv.Global.Get<LuaFunction>("complexObjectTest");
byteBufferTestFunction.Call(); complexObjectTestFuction.Call(complexObjectBytes);
LuaFunction complexObjectTestFuction = luaEnv.Global.Get<LuaFunction>("complexObjectTest"); // 获取普通对象的字节流
complexObjectTestFuction.Call(complexObjectBytes); // var normalObjectBytes = File.ReadAllBytes("D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-no-compatible.bytes");
} // var normalObjectBytes = File.ReadAllBytes("D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-compatible.bytes");
// var normalObjectBytes = File.ReadAllBytes("D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-inner-compatible.bytes");
public static byte[] CustomLoader(ref string filepath) // var normalObjectBytes = File.ReadAllBytes("D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-compatible.bytes");
{ var normalObjectBytes = File.ReadAllBytes("D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-inner-compatible.bytes");
filepath = filepath.Replace(".", "/") + ".lua"; LuaFunction normalObjectTestFuction = luaEnv.Global.Get<LuaFunction>("normalObjectTest");
normalObjectTestFuction.Call(normalObjectBytes);
return File.ReadAllBytes(TEST_PATH + filepath);
}
} }
}
public static byte[] CustomLoader(ref string filepath)
{
filepath = filepath.Replace(".", "/") + ".lua";
return File.ReadAllBytes(TEST_PATH + filepath);
}
}
+24 -6
View File
@@ -1,17 +1,16 @@
local ByteBuffer = require("LuaProtocol.Buffer.ByteBuffer") local ByteBuffer = require("zfoolua.ByteBuffer")
local ProtocolManager = require("LuaProtocol.ProtocolManager") local ProtocolManager = require("zfoolua.ProtocolManager")
-------------------------------------ProtocolManager的测试------------------------------------- -------------------------------------ProtocolManager的测试-------------------------------------
function complexObjectTest(bytes) function complexObjectTest(bytes)
print("complex size ", #bytes)
ProtocolManager.initProtocol() ProtocolManager.initProtocol()
local byteBuffer = ByteBuffer:new() local byteBuffer = ByteBuffer:new()
byteBuffer:writeBuffer(bytes) byteBuffer:writeBuffer(bytes)
local packet = ProtocolManager.read(byteBuffer) local packet = ProtocolManager.read(byteBuffer)
-- complexObjec是老的协议,所以序列化回来myCompatible是nil,所以要重新赋值
packet.myCompatible = 0
local newByteBuffer = ByteBuffer:new() local newByteBuffer = ByteBuffer:new()
ProtocolManager.write(newByteBuffer, packet) ProtocolManager.write(newByteBuffer, packet)
assert(#byteBuffer.buffer <= #newByteBuffer.buffer) assert(#byteBuffer.buffer <= #newByteBuffer.buffer)
@@ -26,6 +25,27 @@ function complexObjectTest(bytes)
return packet return packet
end end
function normalObjectTest(bytes)
ProtocolManager.initProtocol()
local byteBuffer = ByteBuffer:new()
byteBuffer:writeBuffer(bytes)
local packet = ProtocolManager.read(byteBuffer)
local newByteBuffer = ByteBuffer:new()
ProtocolManager.write(newByteBuffer, packet)
-- set和map是无序的,所以有的时候输入和输出的字节流有可能不一致,但是长度一定是一致的
--for i = 1, #byteBuffer.buffer do
-- print(i)
-- assert(byteBuffer.buffer[i] == newByteBuffer.buffer[i], i)
--end
local newPacket = ProtocolManager.read(newByteBuffer)
print("normal source size ", #bytes)
print("normal target size ", newByteBuffer:getLen())
return packet
end
-------------------------------------ByteBuffer的测试------------------------------------- -------------------------------------ByteBuffer的测试-------------------------------------
@@ -129,8 +149,6 @@ function byteBufferTest()
byteBuffer:writeString(s) byteBuffer:writeString(s)
assert(byteBuffer:readString() == s) assert(byteBuffer:readString() == s)
byteBuffer:writeChar(s)
assert(byteBuffer:readChar() == "你")
byteBuffer:setWriteOffset(0) byteBuffer:setWriteOffset(0)
byteBuffer:setReadOffset(0) byteBuffer:setReadOffset(0)
@@ -5,7 +5,7 @@
--右移操作>>是无符号右移 --右移操作>>是无符号右移
--local Long = require("Long") --local Long = require("Long")
local ProtocolManager = require("LuaProtocol.ProtocolManager") local ProtocolManager = require("zfoolua.ProtocolManager")
local maxInt = 2147483647 local maxInt = 2147483647
local minInt = -2147483648 local minInt = -2147483648
@@ -87,6 +87,27 @@ local function utf8sub(str, startChar, numChars)
return str:sub(startIndex, currentIndex - 1) return str:sub(startIndex, currentIndex - 1)
end end
function ByteBuffer:adjustPadding(predictionLength, beforeWriteIndex)
local currentWriteIndex = self.writeOffset
local predictionCount = self:writeIntCount(predictionLength)
local length = currentWriteIndex - beforeWriteIndex - predictionCount
local lengthCount = self:writeIntCount(length)
local padding = lengthCount - predictionCount
if padding == 0 then
self:setWriteOffset(beforeWriteIndex)
self:writeInt(length)
self:setWriteOffset(currentWriteIndex)
else
local bytes = self:getBytes(currentWriteIndex - length, currentWriteIndex - 1)
self:setWriteOffset(beforeWriteIndex)
self:writeInt(length)
self:writeBuffer(bytes)
end
end
function ByteBuffer:compatibleRead(beforeReadIndex, length)
return length ~= -1 and self:getReadOffset() < length + beforeReadIndex
end
-------------------------------------get和set------------------------------------- -------------------------------------get和set-------------------------------------
function ByteBuffer:getWriteOffset() function ByteBuffer:getWriteOffset()
@@ -94,10 +115,8 @@ function ByteBuffer:getWriteOffset()
end end
function ByteBuffer:setWriteOffset(writeOffset) function ByteBuffer:setWriteOffset(writeOffset)
if writeOffset > #self.buffer then if writeOffset > #self.buffer + 1 then
error("index out of bounds exception: readerIndex: " + self.readOffset error(string.format("writeOffset: %s index out of bounds exception: readerIndex: %s, writerIndex: %s, (expected: 0 <= readerIndex <= writerIndex <= capacity: %s)", writeOffset, self.readOffset, self.writeOffset, #self.buffer))
+ ", writerIndex: " + self.writeOffset
+ "(expected: 0 <= readerIndex <= writerIndex <= capacity:" + #self.buffer)
end end
self.writeOffset = writeOffset self.writeOffset = writeOffset
return self return self
@@ -109,9 +128,7 @@ end
function ByteBuffer:setReadOffset(readOffset) function ByteBuffer:setReadOffset(readOffset)
if readOffset > self.writeOffset then if readOffset > self.writeOffset then
error("index out of bounds exception: readerIndex: " + self.readOffset error(string.format("readOffset: %s index out of bounds exception: readerIndex: %s, writerIndex: %s, (expected: 0 <= readerIndex <= writerIndex <= capacity: %s)", readOffset, self.readOffset, self.writeOffset, #self.buffer))
+ ", writerIndex: " + this.writeOffset
+ "(expected: 0 <= readerIndex <= writerIndex <= capacity:" + #self.buffer)
end end
self.readOffset = readOffset self.readOffset = readOffset
return self return self
@@ -202,6 +219,40 @@ function ByteBuffer:readInt()
return self:readLong() return self:readLong()
end end
function ByteBuffer:writeIntCount(intValue)
if (math.type(intValue) ~= "integer") then
error("intValue must be integer")
end
if ((minInt > intValue) or (intValue > maxInt)) then
error("intValue must range between minInt:-2147483648 and maxInt:2147483647")
end
--lua中的右移为无符号右移,要特殊处理
local mask = intValue >> 63
local value = intValue << 1
if (mask == 1) then
value = value ~ 0xFFFFFFFFFFFFFFFF
end
if (value >> 7) == 0 then
return 1
end
if (value >> 14) == 0 then
return 2
end
if (value >> 21) == 0 then
return 3
end
if (value >> 28) == 0 then
return 4
end
return 5
end
-- int -- int
function ByteBuffer:writeRawInt(intValue) function ByteBuffer:writeRawInt(intValue)
local str = string.pack(">i", intValue) local str = string.pack(">i", intValue)
@@ -408,21 +459,6 @@ function ByteBuffer:readString()
return self:readBuffer(length) return self:readBuffer(length)
end end
--char
function ByteBuffer:writeChar(charValue)
if charValue == nil or #charValue == 0 then
self:writeString("")
return
end
local str = utf8sub(charValue, 1, 1)
self:writeString(str)
return self
end
function ByteBuffer:readChar()
return self:readString()
end
--- Write a encoded char array into buf --- Write a encoded char array into buf
function ByteBuffer:writeBuffer(str) function ByteBuffer:writeBuffer(str)
for i = 1, #str do for i = 1, #str do
@@ -463,12 +499,6 @@ function ByteBuffer:getBytes(startIndex, endIndex)
return table.concat(self.buffer, "", startIndex, endIndex) return table.concat(self.buffer, "", startIndex, endIndex)
end end
function ByteBuffer:writePacketFlag(value)
local flag = (value == null)
self:writeBoolean(not flag)
return flag
end
function ByteBuffer:writePacket(value, protocolId) function ByteBuffer:writePacket(value, protocolId)
local protocolRegistration = ProtocolManager.getProtocol(protocolId) local protocolRegistration = ProtocolManager.getProtocol(protocolId)
protocolRegistration:write(self, value) protocolRegistration:write(self, value)
@@ -481,10 +511,10 @@ function ByteBuffer:readPacket(protocolId)
end end
function ByteBuffer:writeBooleanArray(array) function ByteBuffer:writeBooleanArray(array)
if array == null then if array == nil then
self:writeInt(0) self:writeInt(0)
else else
self:writeInt(#array); self:writeInt(#array)
for index, element in pairs(array) do for index, element in pairs(array) do
self:writeBoolean(element) self:writeBoolean(element)
end end
@@ -504,10 +534,10 @@ function ByteBuffer:readBooleanArray()
end end
function ByteBuffer:writeByteArray(array) function ByteBuffer:writeByteArray(array)
if array == null then if array == nil then
self:writeInt(0) self:writeInt(0)
else else
self:writeInt(#array); self:writeInt(#array)
for index, element in pairs(array) do for index, element in pairs(array) do
self:writeByte(element) self:writeByte(element)
end end
@@ -527,10 +557,10 @@ function ByteBuffer:readByteArray()
end end
function ByteBuffer:writeShortArray(array) function ByteBuffer:writeShortArray(array)
if array == null then if array == nil then
self:writeInt(0) self:writeInt(0)
else else
self:writeInt(#array); self:writeInt(#array)
for index, element in pairs(array) do for index, element in pairs(array) do
self:writeShort(element) self:writeShort(element)
end end
@@ -550,10 +580,10 @@ function ByteBuffer:readShortArray()
end end
function ByteBuffer:writeIntArray(array) function ByteBuffer:writeIntArray(array)
if array == null then if array == nil then
self:writeInt(0) self:writeInt(0)
else else
self:writeInt(#array); self:writeInt(#array)
for index, element in pairs(array) do for index, element in pairs(array) do
self:writeInt(element) self:writeInt(element)
end end
@@ -573,10 +603,10 @@ function ByteBuffer:readIntArray()
end end
function ByteBuffer:writeLongArray(array) function ByteBuffer:writeLongArray(array)
if array == null then if array == nil then
self:writeInt(0) self:writeInt(0)
else else
self:writeInt(#array); self:writeInt(#array)
for index, element in pairs(array) do for index, element in pairs(array) do
self:writeLong(element) self:writeLong(element)
end end
@@ -596,10 +626,10 @@ function ByteBuffer:readLongArray()
end end
function ByteBuffer:writeFloatArray(array) function ByteBuffer:writeFloatArray(array)
if array == null then if array == nil then
self:writeInt(0) self:writeInt(0)
else else
self:writeInt(#array); self:writeInt(#array)
for index, element in pairs(array) do for index, element in pairs(array) do
self:writeFloat(element) self:writeFloat(element)
end end
@@ -619,10 +649,10 @@ function ByteBuffer:readFloatArray()
end end
function ByteBuffer:writeDoubleArray(array) function ByteBuffer:writeDoubleArray(array)
if array == null then if array == nil then
self:writeInt(0) self:writeInt(0)
else else
self:writeInt(#array); self:writeInt(#array)
for index, element in pairs(array) do for index, element in pairs(array) do
self:writeDouble(element) self:writeDouble(element)
end end
@@ -641,34 +671,11 @@ function ByteBuffer:readDoubleArray()
return array return array
end end
function ByteBuffer:writeCharArray(array)
if array == null then
self:writeInt(0)
else
self:writeInt(#array);
for index, element in pairs(array) do
self:writeChar(element)
end
end
return self
end
function ByteBuffer:readCharArray()
local array = {}
local size = self:readInt()
if size > 0 then
for index = 1, size do
table.insert(array, self:readChar())
end
end
return array
end
function ByteBuffer:writeStringArray(array) function ByteBuffer:writeStringArray(array)
if array == null then if array == nil then
self:writeInt(0) self:writeInt(0)
else else
self:writeInt(#array); self:writeInt(#array)
for index, element in pairs(array) do for index, element in pairs(array) do
self:writeString(element) self:writeString(element)
end end
@@ -688,11 +695,11 @@ function ByteBuffer:readStringArray()
end end
function ByteBuffer:writePacketArray(array, protocolId) function ByteBuffer:writePacketArray(array, protocolId)
if array == null then if array == nil then
self:writeInt(0) self:writeInt(0)
else else
local protocolRegistration = ProtocolManager.getProtocol(protocolId) local protocolRegistration = ProtocolManager.getProtocol(protocolId)
self:writeInt(#array); self:writeInt(#array)
for index, element in pairs(array) do for index, element in pairs(array) do
protocolRegistration:write(self, element) protocolRegistration:write(self, element)
end end
@@ -713,10 +720,10 @@ function ByteBuffer:readPacketArray(protocolId)
end end
function ByteBuffer:writeIntIntMap(map) function ByteBuffer:writeIntIntMap(map)
if map == null then if map == nil then
self:writeInt(0) self:writeInt(0)
else else
self:writeInt(table.mapSize(map)); self:writeInt(table.mapSize(map))
for key, value in pairs(map) do for key, value in pairs(map) do
self:writeInt(key) self:writeInt(key)
self:writeInt(value) self:writeInt(value)
@@ -739,10 +746,10 @@ function ByteBuffer:readIntIntMap()
end end
function ByteBuffer:writeIntLongMap(map) function ByteBuffer:writeIntLongMap(map)
if map == null then if map == nil then
self:writeInt(0) self:writeInt(0)
else else
self:writeInt(table.mapSize(map)); self:writeInt(table.mapSize(map))
for key, value in pairs(map) do for key, value in pairs(map) do
self:writeInt(key) self:writeInt(key)
self:writeLong(value) self:writeLong(value)
@@ -765,10 +772,10 @@ function ByteBuffer:readIntLongMap()
end end
function ByteBuffer:writeIntStringMap(map) function ByteBuffer:writeIntStringMap(map)
if map == null then if map == nil then
self:writeInt(0) self:writeInt(0)
else else
self:writeInt(table.mapSize(map)); self:writeInt(table.mapSize(map))
for key, value in pairs(map) do for key, value in pairs(map) do
self:writeInt(key) self:writeInt(key)
self:writeString(value) self:writeString(value)
@@ -791,11 +798,11 @@ function ByteBuffer:readIntStringMap()
end end
function ByteBuffer:writeIntPacketMap(map, protocolId) function ByteBuffer:writeIntPacketMap(map, protocolId)
if map == null then if map == nil then
self:writeInt(0) self:writeInt(0)
else else
local protocolRegistration = ProtocolManager.getProtocol(protocolId) local protocolRegistration = ProtocolManager.getProtocol(protocolId)
self:writeInt(table.mapSize(map)); self:writeInt(table.mapSize(map))
for key, value in pairs(map) do for key, value in pairs(map) do
self:writeInt(key) self:writeInt(key)
protocolRegistration:write(self, value) protocolRegistration:write(self, value)
@@ -819,10 +826,10 @@ function ByteBuffer:readIntPacketMap(protocolId)
end end
function ByteBuffer:writeLongIntMap(map) function ByteBuffer:writeLongIntMap(map)
if map == null then if map == nil then
self:writeInt(0) self:writeInt(0)
else else
self:writeInt(table.mapSize(map)); self:writeInt(table.mapSize(map))
for key, value in pairs(map) do for key, value in pairs(map) do
self:writeLong(key) self:writeLong(key)
self:writeInt(value) self:writeInt(value)
@@ -845,10 +852,10 @@ function ByteBuffer:readLongIntMap()
end end
function ByteBuffer:writeLongLongMap(map) function ByteBuffer:writeLongLongMap(map)
if map == null then if map == nil then
self:writeInt(0) self:writeInt(0)
else else
self:writeInt(table.mapSize(map)); self:writeInt(table.mapSize(map))
for key, value in pairs(map) do for key, value in pairs(map) do
self:writeLong(key) self:writeLong(key)
self:writeLong(value) self:writeLong(value)
@@ -871,10 +878,10 @@ function ByteBuffer:readLongLongMap()
end end
function ByteBuffer:writeLongStringMap(map) function ByteBuffer:writeLongStringMap(map)
if map == null then if map == nil then
self:writeInt(0) self:writeInt(0)
else else
self:writeInt(table.mapSize(map)); self:writeInt(table.mapSize(map))
for key, value in pairs(map) do for key, value in pairs(map) do
self:writeLong(key) self:writeLong(key)
self:writeString(value) self:writeString(value)
@@ -897,11 +904,11 @@ function ByteBuffer:readLongStringMap()
end end
function ByteBuffer:writeLongPacketMap(map, protocolId) function ByteBuffer:writeLongPacketMap(map, protocolId)
if map == null then if map == nil then
self:writeInt(0) self:writeInt(0)
else else
local protocolRegistration = ProtocolManager.getProtocol(protocolId) local protocolRegistration = ProtocolManager.getProtocol(protocolId)
self:writeInt(table.mapSize(map)); self:writeInt(table.mapSize(map))
for key, value in pairs(map) do for key, value in pairs(map) do
self:writeLong(key) self:writeLong(key)
protocolRegistration:write(self, value) protocolRegistration:write(self, value)
@@ -925,10 +932,10 @@ function ByteBuffer:readLongPacketMap(protocolId)
end end
function ByteBuffer:writeStringIntMap(map) function ByteBuffer:writeStringIntMap(map)
if map == null then if map == nil then
self:writeInt(0) self:writeInt(0)
else else
self:writeInt(table.mapSize(map)); self:writeInt(table.mapSize(map))
for key, value in pairs(map) do for key, value in pairs(map) do
self:writeString(key) self:writeString(key)
self:writeInt(value) self:writeInt(value)
@@ -951,10 +958,10 @@ function ByteBuffer:readStringIntMap()
end end
function ByteBuffer:writeStringLongMap(map) function ByteBuffer:writeStringLongMap(map)
if map == null then if map == nil then
self:writeInt(0) self:writeInt(0)
else else
self:writeInt(table.mapSize(map)); self:writeInt(table.mapSize(map))
for key, value in pairs(map) do for key, value in pairs(map) do
self:writeString(key) self:writeString(key)
self:writeLong(value) self:writeLong(value)
@@ -977,10 +984,10 @@ function ByteBuffer:readStringLongMap()
end end
function ByteBuffer:writeStringStringMap(map) function ByteBuffer:writeStringStringMap(map)
if map == null then if map == nil then
self:writeInt(0) self:writeInt(0)
else else
self:writeInt(table.mapSize(map)); self:writeInt(table.mapSize(map))
for key, value in pairs(map) do for key, value in pairs(map) do
self:writeString(key) self:writeString(key)
self:writeString(value) self:writeString(value)
@@ -1003,11 +1010,11 @@ function ByteBuffer:readStringStringMap()
end end
function ByteBuffer:writeStringPacketMap(map, protocolId) function ByteBuffer:writeStringPacketMap(map, protocolId)
if map == null then if map == nil then
self:writeInt(0) self:writeInt(0)
else else
local protocolRegistration = ProtocolManager.getProtocol(protocolId) local protocolRegistration = ProtocolManager.getProtocol(protocolId)
self:writeInt(table.mapSize(map)); self:writeInt(table.mapSize(map))
for key, value in pairs(map) do for key, value in pairs(map) do
self:writeString(key) self:writeString(key)
protocolRegistration:write(self, value) protocolRegistration:write(self, value)
@@ -33,11 +33,13 @@ function ProtocolManager.read(buffer)
end end
function initProtocol() function initProtocol()
local ComplexObject = require("LuaProtocol.Packet.ComplexObject") local EmptyObject = require("zfoolua.packet.EmptyObject")
local NormalObject = require("LuaProtocol.Packet.NormalObject") local ComplexObject = require("zfoolua.packet.ComplexObject")
local ObjectA = require("LuaProtocol.Packet.ObjectA") local NormalObject = require("zfoolua.packet.NormalObject")
local ObjectB = require("LuaProtocol.Packet.ObjectB") local ObjectA = require("zfoolua.packet.ObjectA")
local SimpleObject = require("LuaProtocol.Packet.SimpleObject") local ObjectB = require("zfoolua.packet.ObjectB")
local SimpleObject = require("zfoolua.packet.SimpleObject")
protocols[0] = EmptyObject
protocols[100] = ComplexObject protocols[100] = ComplexObject
protocols[101] = NormalObject protocols[101] = NormalObject
protocols[102] = ObjectA protocols[102] = ObjectA
@@ -1,70 +1,61 @@
-- 复杂的对象 -- 复杂的对象,包括了各种复杂的结构,数组,List,Set,Map
-- 包括了各种复杂的结构,数组,List,Set,Map
--
-- @author godotg
local ComplexObject = {} local ComplexObject = {}
function ComplexObject:new(a, aa, aaa, aaaa, b, bb, bbb, bbbb, c, cc, ccc, cccc, d, dd, ddd, dddd, e, ee, eee, eeee, f, ff, fff, ffff, g, gg, ggg, gggg, h, hh, hhh, hhhh, jj, jjj, kk, kkk, l, ll, lll, llll, lllll, m, mm, mmm, mmmm, mmmmm, s, ss, sss, ssss, sssss, myCompatible, myObject) function ComplexObject:new()
local obj = { local obj = {
-- byte类型,最简单的整形 -- byte类型,最简单的整形
a = a, -- byte a = 0, -- byte
-- byte的包装类型 -- byte的包装类型,优先使用基础类型,包装类型会有装箱拆箱
-- 优先使用基础类型,包装类型会有装箱拆箱 aa = 0, -- byte
aa = aa, -- java.lang.Byte
-- 数组类型 -- 数组类型
aaa = aaa, -- byte[] aaa = {}, -- byte[]
aaaa = aaaa, -- java.lang.Byte[] aaaa = {}, -- byte[]
b = b, -- short b = 0, -- short
bb = bb, -- java.lang.Short bb = 0, -- short
bbb = bbb, -- short[] bbb = {}, -- short[]
bbbb = bbbb, -- java.lang.Short[] bbbb = {}, -- short[]
c = c, -- int c = 0, -- int
cc = cc, -- java.lang.Integer cc = 0, -- int
ccc = ccc, -- int[] ccc = {}, -- int[]
cccc = cccc, -- java.lang.Integer[] cccc = {}, -- int[]
d = d, -- long d = 0, -- long
dd = dd, -- java.lang.Long dd = 0, -- long
ddd = ddd, -- long[] ddd = {}, -- long[]
dddd = dddd, -- java.lang.Long[] dddd = {}, -- long[]
e = e, -- float e = 0, -- float
ee = ee, -- java.lang.Float ee = 0, -- float
eee = eee, -- float[] eee = {}, -- float[]
eeee = eeee, -- java.lang.Float[] eeee = {}, -- float[]
f = f, -- double f = 0, -- double
ff = ff, -- java.lang.Double ff = 0, -- double
fff = fff, -- double[] fff = {}, -- double[]
ffff = ffff, -- java.lang.Double[] ffff = {}, -- double[]
g = g, -- boolean g = false, -- bool
gg = gg, -- java.lang.Boolean gg = false, -- bool
ggg = ggg, -- boolean[] ggg = {}, -- bool[]
gggg = gggg, -- java.lang.Boolean[] gggg = {}, -- bool[]
h = h, -- char jj = "", -- string
hh = hh, -- java.lang.Character jjj = {}, -- string[]
hhh = hhh, -- char[] kk = nil, -- ObjectA
hhhh = hhhh, -- java.lang.Character[] kkk = {}, -- ObjectA[]
jj = jj, -- java.lang.String l = {}, -- List<int>
jjj = jjj, -- java.lang.String[] ll = {}, -- List<List<List<int>>>
kk = kk, -- com.zfoo.protocol.packet.ObjectA lll = {}, -- List<List<ObjectA>>
kkk = kkk, -- com.zfoo.protocol.packet.ObjectA[] llll = {}, -- List<string>
l = l, -- java.util.List<java.lang.Integer> lllll = {}, -- List<Dictionary<int, string>>
ll = ll, -- java.util.List<java.util.List<java.util.List<java.lang.Integer>>> m = {}, -- Dictionary<int, string>
lll = lll, -- java.util.List<java.util.List<com.zfoo.protocol.packet.ObjectA>> mm = {}, -- Dictionary<int, ObjectA>
llll = llll, -- java.util.List<java.lang.String> mmm = {}, -- Dictionary<ObjectA, List<int>>
lllll = lllll, -- java.util.List<java.util.Map<java.lang.Integer, java.lang.String>> mmmm = {}, -- Dictionary<List<List<ObjectA>>, List<List<List<int>>>>
m = m, -- java.util.Map<java.lang.Integer, java.lang.String> mmmmm = {}, -- Dictionary<List<Dictionary<int, string>>, HashSet<Dictionary<int, string>>>
mm = mm, -- java.util.Map<java.lang.Integer, com.zfoo.protocol.packet.ObjectA> s = {}, -- HashSet<int>
mmm = mmm, -- java.util.Map<com.zfoo.protocol.packet.ObjectA, java.util.List<java.lang.Integer>> ss = {}, -- HashSet<HashSet<List<int>>>
mmmm = mmmm, -- java.util.Map<java.util.List<java.util.List<com.zfoo.protocol.packet.ObjectA>>, java.util.List<java.util.List<java.util.List<java.lang.Integer>>>> sss = {}, -- HashSet<HashSet<ObjectA>>
mmmmm = mmmmm, -- java.util.Map<java.util.List<java.util.Map<java.lang.Integer, java.lang.String>>, java.util.Set<java.util.Map<java.lang.Integer, java.lang.String>>> ssss = {}, -- HashSet<string>
s = s, -- java.util.Set<java.lang.Integer> sssss = {}, -- HashSet<Dictionary<int, string>>
ss = ss, -- java.util.Set<java.util.Set<java.util.List<java.lang.Integer>>>
sss = sss, -- java.util.Set<java.util.Set<com.zfoo.protocol.packet.ObjectA>>
ssss = ssss, -- java.util.Set<java.lang.String>
sssss = sssss, -- java.util.Set<java.util.Map<java.lang.Integer, java.lang.String>>
-- 如果要修改协议并且兼容老协议,需要加上Compatible注解,按照增加的顺序添加order -- 如果要修改协议并且兼容老协议,需要加上Compatible注解,按照增加的顺序添加order
myCompatible = myCompatible, -- int myCompatible = 0, -- int
myObject = myObject -- com.zfoo.protocol.packet.ObjectA myObject = nil -- ObjectA
} }
setmetatable(obj, self) setmetatable(obj, self)
self.__index = self self.__index = self
@@ -76,9 +67,12 @@ function ComplexObject:protocolId()
end end
function ComplexObject:write(buffer, packet) function ComplexObject:write(buffer, packet)
if buffer:writePacketFlag(packet) then if packet == nil then
buffer:writeInt(0)
return return
end end
local beforeWriteIndex = buffer:getWriteOffset()
buffer:writeInt(36962)
buffer:writeByte(packet.a) buffer:writeByte(packet.a)
buffer:writeByte(packet.aa) buffer:writeByte(packet.aa)
buffer:writeByteArray(packet.aaa) buffer:writeByteArray(packet.aaa)
@@ -107,10 +101,6 @@ function ComplexObject:write(buffer, packet)
buffer:writeBoolean(packet.gg) buffer:writeBoolean(packet.gg)
buffer:writeBooleanArray(packet.ggg) buffer:writeBooleanArray(packet.ggg)
buffer:writeBooleanArray(packet.gggg) buffer:writeBooleanArray(packet.gggg)
buffer:writeChar(packet.h)
buffer:writeChar(packet.hh)
buffer:writeCharArray(packet.hhh)
buffer:writeCharArray(packet.hhhh)
buffer:writeString(packet.jj) buffer:writeString(packet.jj)
buffer:writeStringArray(packet.jjj) buffer:writeStringArray(packet.jjj)
buffer:writePacket(packet.kk, 102) buffer:writePacket(packet.kk, 102)
@@ -247,12 +237,15 @@ function ComplexObject:write(buffer, packet)
end end
buffer:writeInt(packet.myCompatible) buffer:writeInt(packet.myCompatible)
buffer:writePacket(packet.myObject, 102) buffer:writePacket(packet.myObject, 102)
buffer:adjustPadding(36962, beforeWriteIndex)
end end
function ComplexObject:read(buffer) function ComplexObject:read(buffer)
if not(buffer:readBoolean()) then local length = buffer:readInt()
if length == 0 then
return nil return nil
end end
local beforeReadIndex = buffer:getReadOffset()
local packet = ComplexObject:new() local packet = ComplexObject:new()
local result32 = buffer:readByte() local result32 = buffer:readByte()
packet.a = result32 packet.a = result32
@@ -310,109 +303,109 @@ function ComplexObject:read(buffer)
packet.ggg = array58 packet.ggg = array58
local array59 = buffer:readBooleanArray() local array59 = buffer:readBooleanArray()
packet.gggg = array59 packet.gggg = array59
local result60 = buffer:readChar() local result60 = buffer:readString()
packet.h = result60 packet.jj = result60
local result61 = buffer:readChar() local array61 = buffer:readStringArray()
packet.hh = result61 packet.jjj = array61
local array62 = buffer:readCharArray() local result62 = buffer:readPacket(102)
packet.hhh = array62 packet.kk = result62
local array63 = buffer:readCharArray() local array63 = buffer:readPacketArray(102)
packet.hhhh = array63 packet.kkk = array63
local result64 = buffer:readString() local list64 = buffer:readIntArray()
packet.jj = result64 packet.l = list64
local array65 = buffer:readStringArray() local result65 = {}
packet.jjj = array65 local size66 = buffer:readInt()
local result66 = buffer:readPacket(102) if size66 > 0 then
packet.kk = result66 for index67 = 1, size66 do
local array67 = buffer:readPacketArray(102) local result68 = {}
packet.kkk = array67 local size69 = buffer:readInt()
local list68 = buffer:readIntArray() if size69 > 0 then
packet.l = list68 for index70 = 1, size69 do
local result69 = {} local list71 = buffer:readIntArray()
local size70 = buffer:readInt() table.insert(result68, list71)
if size70 > 0 then
for index71 = 1, size70 do
local result72 = {}
local size73 = buffer:readInt()
if size73 > 0 then
for index74 = 1, size73 do
local list75 = buffer:readIntArray()
table.insert(result72, list75)
end end
end end
table.insert(result69, result72) table.insert(result65, result68)
end end
end end
packet.ll = result69 packet.ll = result65
local result76 = {} local result72 = {}
local size77 = buffer:readInt() local size73 = buffer:readInt()
if size77 > 0 then if size73 > 0 then
for index78 = 1, size77 do for index74 = 1, size73 do
local list79 = buffer:readPacketArray(102) local list75 = buffer:readPacketArray(102)
table.insert(result76, list79) table.insert(result72, list75)
end end
end end
packet.lll = result76 packet.lll = result72
local list80 = buffer:readStringArray() local list76 = buffer:readStringArray()
packet.llll = list80 packet.llll = list76
local result81 = {} local result77 = {}
local size82 = buffer:readInt() local size78 = buffer:readInt()
if size82 > 0 then if size78 > 0 then
for index83 = 1, size82 do for index79 = 1, size78 do
local map84 = buffer:readIntStringMap() local map80 = buffer:readIntStringMap()
table.insert(result81, map84) table.insert(result77, map80)
end end
end end
packet.lllll = result81 packet.lllll = result77
local map85 = buffer:readIntStringMap() local map81 = buffer:readIntStringMap()
packet.m = map85 packet.m = map81
local map86 = buffer:readIntPacketMap(102) local map82 = buffer:readIntPacketMap(102)
packet.mm = map86 packet.mm = map82
local result87 = {} local result83 = {}
local size88 = buffer:readInt() local size84 = buffer:readInt()
if size88 > 0 then if size84 > 0 then
for index89 = 1, size88 do for index85 = 1, size84 do
local result90 = buffer:readPacket(102) local result86 = buffer:readPacket(102)
local list91 = buffer:readIntArray() local list87 = buffer:readIntArray()
result87[result90] = list91 result83[result86] = list87
end end
end end
packet.mmm = result87 packet.mmm = result83
local result92 = {} local result88 = {}
local size93 = buffer:readInt() local size89 = buffer:readInt()
if size93 > 0 then if size89 > 0 then
for index94 = 1, size93 do for index90 = 1, size89 do
local result91 = {}
local size92 = buffer:readInt()
if size92 > 0 then
for index93 = 1, size92 do
local list94 = buffer:readPacketArray(102)
table.insert(result91, list94)
end
end
local result95 = {} local result95 = {}
local size96 = buffer:readInt() local size96 = buffer:readInt()
if size96 > 0 then if size96 > 0 then
for index97 = 1, size96 do for index97 = 1, size96 do
local list98 = buffer:readPacketArray(102) local result98 = {}
table.insert(result95, list98) local size99 = buffer:readInt()
end if size99 > 0 then
end for index100 = 1, size99 do
local result99 = {} local list101 = buffer:readIntArray()
local size100 = buffer:readInt() table.insert(result98, list101)
if size100 > 0 then
for index101 = 1, size100 do
local result102 = {}
local size103 = buffer:readInt()
if size103 > 0 then
for index104 = 1, size103 do
local list105 = buffer:readIntArray()
table.insert(result102, list105)
end end
end end
table.insert(result99, result102) table.insert(result95, result98)
end end
end end
result92[result95] = result99 result88[result91] = result95
end end
end end
packet.mmmm = result92 packet.mmmm = result88
local result106 = {} local result102 = {}
local size107 = buffer:readInt() local size103 = buffer:readInt()
if size107 > 0 then if size103 > 0 then
for index108 = 1, size107 do for index104 = 1, size103 do
local result105 = {}
local size106 = buffer:readInt()
if size106 > 0 then
for index107 = 1, size106 do
local map108 = buffer:readIntStringMap()
table.insert(result105, map108)
end
end
local result109 = {} local result109 = {}
local size110 = buffer:readInt() local size110 = buffer:readInt()
if size110 > 0 then if size110 > 0 then
@@ -421,66 +414,59 @@ function ComplexObject:read(buffer)
table.insert(result109, map112) table.insert(result109, map112)
end end
end end
local result113 = {} result102[result105] = result109
local size114 = buffer:readInt() end
if size114 > 0 then end
for index115 = 1, size114 do packet.mmmmm = result102
local map116 = buffer:readIntStringMap() local set113 = buffer:readIntArray()
table.insert(result113, map116) packet.s = set113
local result114 = {}
local size115 = buffer:readInt()
if size115 > 0 then
for index116 = 1, size115 do
local result117 = {}
local size118 = buffer:readInt()
if size118 > 0 then
for index119 = 1, size118 do
local list120 = buffer:readIntArray()
table.insert(result117, list120)
end end
end end
result106[result109] = result113 table.insert(result114, result117)
end end
end end
packet.mmmmm = result106 packet.ss = result114
local set117 = buffer:readIntArray() local result121 = {}
packet.s = set117 local size122 = buffer:readInt()
local result118 = {} if size122 > 0 then
local size119 = buffer:readInt() for index123 = 1, size122 do
if size119 > 0 then local set124 = buffer:readPacketArray(102)
for index120 = 1, size119 do table.insert(result121, set124)
local result121 = {}
local size122 = buffer:readInt()
if size122 > 0 then
for index123 = 1, size122 do
local list124 = buffer:readIntArray()
table.insert(result121, list124)
end
end
table.insert(result118, result121)
end end
end end
packet.ss = result118 packet.sss = result121
local result125 = {} local set125 = buffer:readStringArray()
local size126 = buffer:readInt() packet.ssss = set125
if size126 > 0 then local result126 = {}
for index127 = 1, size126 do local size127 = buffer:readInt()
local set128 = buffer:readPacketArray(102) if size127 > 0 then
table.insert(result125, set128) for index128 = 1, size127 do
local map129 = buffer:readIntStringMap()
table.insert(result126, map129)
end end
end end
packet.sss = result125 packet.sssss = result126
local set129 = buffer:readStringArray() if buffer:compatibleRead(beforeReadIndex, length) then
packet.ssss = set129 local result130 = buffer:readInt()
local result130 = {} packet.myCompatible = result130
local size131 = buffer:readInt()
if size131 > 0 then
for index132 = 1, size131 do
local map133 = buffer:readIntStringMap()
table.insert(result130, map133)
end
end end
packet.sssss = result130 if buffer:compatibleRead(beforeReadIndex, length) then
if not(buffer:isReadable()) then local result131 = buffer:readPacket(102)
return packet packet.myObject = result131
end end
local result134 = buffer:readInt() if length > 0 then
packet.myCompatible = result134 buffer:setReadOffset(beforeReadIndex + length)
if not(buffer:isReadable()) then
return packet
end end
local result135 = buffer:readPacket(102)
packet.myObject = result135
return packet return packet
end end
@@ -0,0 +1,39 @@
local EmptyObject = {}
function EmptyObject:new()
local obj = {
}
setmetatable(obj, self)
self.__index = self
return obj
end
function EmptyObject:protocolId()
return 0
end
function EmptyObject:write(buffer, packet)
if packet == nil then
buffer:writeInt(0)
return
end
buffer:writeInt(-1)
end
function EmptyObject:read(buffer)
local length = buffer:readInt()
if length == 0 then
return nil
end
local beforeReadIndex = buffer:getReadOffset()
local packet = EmptyObject:new()
if length > 0 then
buffer:setReadOffset(beforeReadIndex + length)
end
return packet
end
return EmptyObject
@@ -1,27 +1,27 @@
-- @author godotg
local NormalObject = {} local NormalObject = {}
function NormalObject:new(a, aaa, b, c, d, e, f, g, jj, kk, l, ll, lll, llll, m, mm, s, ssss) function NormalObject:new()
local obj = { local obj = {
a = a, -- byte a = 0, -- byte
aaa = aaa, -- byte[] aaa = {}, -- byte[]
b = b, -- short b = 0, -- short
c = c, -- int c = 0, -- int
d = d, -- long d = 0, -- long
e = e, -- float e = 0, -- float
f = f, -- double f = 0, -- double
g = g, -- boolean g = false, -- bool
jj = jj, -- java.lang.String jj = "", -- string
kk = kk, -- com.zfoo.protocol.packet.ObjectA kk = nil, -- ObjectA
l = l, -- java.util.List<java.lang.Integer> l = {}, -- List<int>
ll = ll, -- java.util.List<java.lang.Long> ll = {}, -- List<long>
lll = lll, -- java.util.List<com.zfoo.protocol.packet.ObjectA> lll = {}, -- List<ObjectA>
llll = llll, -- java.util.List<java.lang.String> llll = {}, -- List<string>
m = m, -- java.util.Map<java.lang.Integer, java.lang.String> m = {}, -- Dictionary<int, string>
mm = mm, -- java.util.Map<java.lang.Integer, com.zfoo.protocol.packet.ObjectA> mm = {}, -- Dictionary<int, ObjectA>
s = s, -- java.util.Set<java.lang.Integer> s = {}, -- HashSet<int>
ssss = ssss -- java.util.Set<java.lang.String> ssss = {}, -- HashSet<string>
outCompatibleValue = 0 -- int
} }
setmetatable(obj, self) setmetatable(obj, self)
self.__index = self self.__index = self
@@ -33,9 +33,12 @@ function NormalObject:protocolId()
end end
function NormalObject:write(buffer, packet) function NormalObject:write(buffer, packet)
if buffer:writePacketFlag(packet) then if packet == nil then
buffer:writeInt(0)
return return
end end
local beforeWriteIndex = buffer:getWriteOffset()
buffer:writeInt(854)
buffer:writeByte(packet.a) buffer:writeByte(packet.a)
buffer:writeByteArray(packet.aaa) buffer:writeByteArray(packet.aaa)
buffer:writeShort(packet.b) buffer:writeShort(packet.b)
@@ -54,12 +57,16 @@ function NormalObject:write(buffer, packet)
buffer:writeIntPacketMap(packet.mm, 102) buffer:writeIntPacketMap(packet.mm, 102)
buffer:writeIntArray(packet.s) buffer:writeIntArray(packet.s)
buffer:writeStringArray(packet.ssss) buffer:writeStringArray(packet.ssss)
buffer:writeInt(packet.outCompatibleValue)
buffer:adjustPadding(854, beforeWriteIndex)
end end
function NormalObject:read(buffer) function NormalObject:read(buffer)
if not(buffer:readBoolean()) then local length = buffer:readInt()
if length == 0 then
return nil return nil
end end
local beforeReadIndex = buffer:getReadOffset()
local packet = NormalObject:new() local packet = NormalObject:new()
local result0 = buffer:readByte() local result0 = buffer:readByte()
packet.a = result0 packet.a = result0
@@ -97,6 +104,13 @@ function NormalObject:read(buffer)
packet.s = set16 packet.s = set16
local set17 = buffer:readStringArray() local set17 = buffer:readStringArray()
packet.ssss = set17 packet.ssss = set17
if buffer:compatibleRead(beforeReadIndex, length) then
local result18 = buffer:readInt()
packet.outCompatibleValue = result18
end
if length > 0 then
buffer:setReadOffset(beforeReadIndex + length)
end
return packet return packet
end end
@@ -0,0 +1,57 @@
local ObjectA = {}
function ObjectA:new()
local obj = {
a = 0, -- int
m = {}, -- Dictionary<int, string>
objectB = nil, -- ObjectB
innerCompatibleValue = 0 -- int
}
setmetatable(obj, self)
self.__index = self
return obj
end
function ObjectA:protocolId()
return 102
end
function ObjectA:write(buffer, packet)
if packet == nil then
buffer:writeInt(0)
return
end
local beforeWriteIndex = buffer:getWriteOffset()
buffer:writeInt(201)
buffer:writeInt(packet.a)
buffer:writeIntStringMap(packet.m)
buffer:writePacket(packet.objectB, 103)
buffer:writeInt(packet.innerCompatibleValue)
buffer:adjustPadding(201, beforeWriteIndex)
end
function ObjectA:read(buffer)
local length = buffer:readInt()
if length == 0 then
return nil
end
local beforeReadIndex = buffer:getReadOffset()
local packet = ObjectA:new()
local result0 = buffer:readInt()
packet.a = result0
local map1 = buffer:readIntStringMap()
packet.m = map1
local result2 = buffer:readPacket(103)
packet.objectB = result2
if buffer:compatibleRead(beforeReadIndex, length) then
local result3 = buffer:readInt()
packet.innerCompatibleValue = result3
end
if length > 0 then
buffer:setReadOffset(beforeReadIndex + length)
end
return packet
end
return ObjectA
@@ -0,0 +1,49 @@
local ObjectB = {}
function ObjectB:new()
local obj = {
flag = false, -- bool
innerCompatibleValue = 0 -- int
}
setmetatable(obj, self)
self.__index = self
return obj
end
function ObjectB:protocolId()
return 103
end
function ObjectB:write(buffer, packet)
if packet == nil then
buffer:writeInt(0)
return
end
local beforeWriteIndex = buffer:getWriteOffset()
buffer:writeInt(4)
buffer:writeBoolean(packet.flag)
buffer:writeInt(packet.innerCompatibleValue)
buffer:adjustPadding(4, beforeWriteIndex)
end
function ObjectB:read(buffer)
local length = buffer:readInt()
if length == 0 then
return nil
end
local beforeReadIndex = buffer:getReadOffset()
local packet = ObjectB:new()
local result0 = buffer:readBoolean()
packet.flag = result0
if buffer:compatibleRead(beforeReadIndex, length) then
local result1 = buffer:readInt()
packet.innerCompatibleValue = result1
end
if length > 0 then
buffer:setReadOffset(beforeReadIndex + length)
end
return packet
end
return ObjectB
@@ -1,11 +1,10 @@
-- @author godotg
local SimpleObject = {} local SimpleObject = {}
function SimpleObject:new(c, g) function SimpleObject:new()
local obj = { local obj = {
c = c, -- int c = 0, -- int
g = g -- boolean g = false -- bool
} }
setmetatable(obj, self) setmetatable(obj, self)
self.__index = self self.__index = self
@@ -17,22 +16,29 @@ function SimpleObject:protocolId()
end end
function SimpleObject:write(buffer, packet) function SimpleObject:write(buffer, packet)
if buffer:writePacketFlag(packet) then if packet == nil then
buffer:writeInt(0)
return return
end end
buffer:writeInt(-1)
buffer:writeInt(packet.c) buffer:writeInt(packet.c)
buffer:writeBoolean(packet.g) buffer:writeBoolean(packet.g)
end end
function SimpleObject:read(buffer) function SimpleObject:read(buffer)
if not(buffer:readBoolean()) then local length = buffer:readInt()
if length == 0 then
return nil return nil
end end
local beforeReadIndex = buffer:getReadOffset()
local packet = SimpleObject:new() local packet = SimpleObject:new()
local result0 = buffer:readInt() local result0 = buffer:readInt()
packet.c = result0 packet.c = result0
local result1 = buffer:readBoolean() local result1 = buffer:readBoolean()
packet.g = result1 packet.g = result1
if length > 0 then
buffer:setReadOffset(beforeReadIndex + length)
end
return packet return packet
end end