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

This commit is contained in:
godotg
2023-10-19 13:14:11 +08:00
parent a20f744eb4
commit 4ebc3841e2
13 changed files with 557 additions and 446 deletions
@@ -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.Text;
using NUnit.Framework;
using Spring.Util;
using UnityEngine;
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]
public void ComplexObjectTest()
{
// 获取复杂对象的字节流
var complexObjectBytes = File.ReadAllBytes("D:\\zfoo\\protocol\\src\\test\\resources\\ComplexObject.bytes");
luaEnv.DoString(luaDebugBuilder.ToString());
var luaEnv = new LuaEnv();
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.AddLoader(CustomLoader);
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");
LuaFunction byteBufferTestFunction = luaEnv.Global.Get<LuaFunction>("byteBufferTest");
byteBufferTestFunction.Call();
LuaFunction complexObjectTestFuction = luaEnv.Global.Get<LuaFunction>("complexObjectTest");
complexObjectTestFuction.Call(complexObjectBytes);
}
public static byte[] CustomLoader(ref string filepath)
{
filepath = filepath.Replace(".", "/") + ".lua";
return File.ReadAllBytes(TEST_PATH + filepath);
}
// 获取复杂对象的字节流
var complexObjectBytes = File.ReadAllBytes("D:\\Project\\zfoo\\protocol\\src\\test\\resources\\complexObject.bytes");
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");
// 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");
LuaFunction normalObjectTestFuction = luaEnv.Global.Get<LuaFunction>("normalObjectTest");
normalObjectTestFuction.Call(normalObjectBytes);
}
}
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 ProtocolManager = require("LuaProtocol.ProtocolManager")
local ByteBuffer = require("zfoolua.ByteBuffer")
local ProtocolManager = require("zfoolua.ProtocolManager")
-------------------------------------ProtocolManager的测试-------------------------------------
function complexObjectTest(bytes)
print("complex size ", #bytes)
ProtocolManager.initProtocol()
local byteBuffer = ByteBuffer:new()
byteBuffer:writeBuffer(bytes)
local packet = ProtocolManager.read(byteBuffer)
-- complexObjec是老的协议,所以序列化回来myCompatible是nil,所以要重新赋值
packet.myCompatible = 0
local newByteBuffer = ByteBuffer:new()
ProtocolManager.write(newByteBuffer, packet)
assert(#byteBuffer.buffer <= #newByteBuffer.buffer)
@@ -26,6 +25,27 @@ function complexObjectTest(bytes)
return packet
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的测试-------------------------------------
@@ -129,8 +149,6 @@ function byteBufferTest()
byteBuffer:writeString(s)
assert(byteBuffer:readString() == s)
byteBuffer:writeChar(s)
assert(byteBuffer:readChar() == "")
byteBuffer:setWriteOffset(0)
byteBuffer:setReadOffset(0)
@@ -5,7 +5,7 @@
--右移操作>>是无符号右移
--local Long = require("Long")
local ProtocolManager = require("LuaProtocol.ProtocolManager")
local ProtocolManager = require("zfoolua.ProtocolManager")
local maxInt = 2147483647
local minInt = -2147483648
@@ -87,6 +87,27 @@ local function utf8sub(str, startChar, numChars)
return str:sub(startIndex, currentIndex - 1)
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-------------------------------------
function ByteBuffer:getWriteOffset()
@@ -94,10 +115,8 @@ function ByteBuffer:getWriteOffset()
end
function ByteBuffer:setWriteOffset(writeOffset)
if writeOffset > #self.buffer then
error("index out of bounds exception: readerIndex: " + self.readOffset
+ ", writerIndex: " + self.writeOffset
+ "(expected: 0 <= readerIndex <= writerIndex <= capacity:" + #self.buffer)
if writeOffset > #self.buffer + 1 then
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))
end
self.writeOffset = writeOffset
return self
@@ -109,9 +128,7 @@ end
function ByteBuffer:setReadOffset(readOffset)
if readOffset > self.writeOffset then
error("index out of bounds exception: readerIndex: " + self.readOffset
+ ", writerIndex: " + this.writeOffset
+ "(expected: 0 <= readerIndex <= writerIndex <= capacity:" + #self.buffer)
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))
end
self.readOffset = readOffset
return self
@@ -202,6 +219,40 @@ function ByteBuffer:readInt()
return self:readLong()
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
function ByteBuffer:writeRawInt(intValue)
local str = string.pack(">i", intValue)
@@ -408,21 +459,6 @@ function ByteBuffer:readString()
return self:readBuffer(length)
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
function ByteBuffer:writeBuffer(str)
for i = 1, #str do
@@ -463,12 +499,6 @@ function ByteBuffer:getBytes(startIndex, endIndex)
return table.concat(self.buffer, "", startIndex, endIndex)
end
function ByteBuffer:writePacketFlag(value)
local flag = (value == null)
self:writeBoolean(not flag)
return flag
end
function ByteBuffer:writePacket(value, protocolId)
local protocolRegistration = ProtocolManager.getProtocol(protocolId)
protocolRegistration:write(self, value)
@@ -481,10 +511,10 @@ function ByteBuffer:readPacket(protocolId)
end
function ByteBuffer:writeBooleanArray(array)
if array == null then
if array == nil then
self:writeInt(0)
else
self:writeInt(#array);
self:writeInt(#array)
for index, element in pairs(array) do
self:writeBoolean(element)
end
@@ -504,10 +534,10 @@ function ByteBuffer:readBooleanArray()
end
function ByteBuffer:writeByteArray(array)
if array == null then
if array == nil then
self:writeInt(0)
else
self:writeInt(#array);
self:writeInt(#array)
for index, element in pairs(array) do
self:writeByte(element)
end
@@ -527,10 +557,10 @@ function ByteBuffer:readByteArray()
end
function ByteBuffer:writeShortArray(array)
if array == null then
if array == nil then
self:writeInt(0)
else
self:writeInt(#array);
self:writeInt(#array)
for index, element in pairs(array) do
self:writeShort(element)
end
@@ -550,10 +580,10 @@ function ByteBuffer:readShortArray()
end
function ByteBuffer:writeIntArray(array)
if array == null then
if array == nil then
self:writeInt(0)
else
self:writeInt(#array);
self:writeInt(#array)
for index, element in pairs(array) do
self:writeInt(element)
end
@@ -573,10 +603,10 @@ function ByteBuffer:readIntArray()
end
function ByteBuffer:writeLongArray(array)
if array == null then
if array == nil then
self:writeInt(0)
else
self:writeInt(#array);
self:writeInt(#array)
for index, element in pairs(array) do
self:writeLong(element)
end
@@ -596,10 +626,10 @@ function ByteBuffer:readLongArray()
end
function ByteBuffer:writeFloatArray(array)
if array == null then
if array == nil then
self:writeInt(0)
else
self:writeInt(#array);
self:writeInt(#array)
for index, element in pairs(array) do
self:writeFloat(element)
end
@@ -619,10 +649,10 @@ function ByteBuffer:readFloatArray()
end
function ByteBuffer:writeDoubleArray(array)
if array == null then
if array == nil then
self:writeInt(0)
else
self:writeInt(#array);
self:writeInt(#array)
for index, element in pairs(array) do
self:writeDouble(element)
end
@@ -641,34 +671,11 @@ function ByteBuffer:readDoubleArray()
return array
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)
if array == null then
if array == nil then
self:writeInt(0)
else
self:writeInt(#array);
self:writeInt(#array)
for index, element in pairs(array) do
self:writeString(element)
end
@@ -688,11 +695,11 @@ function ByteBuffer:readStringArray()
end
function ByteBuffer:writePacketArray(array, protocolId)
if array == null then
if array == nil then
self:writeInt(0)
else
local protocolRegistration = ProtocolManager.getProtocol(protocolId)
self:writeInt(#array);
self:writeInt(#array)
for index, element in pairs(array) do
protocolRegistration:write(self, element)
end
@@ -713,10 +720,10 @@ function ByteBuffer:readPacketArray(protocolId)
end
function ByteBuffer:writeIntIntMap(map)
if map == null then
if map == nil then
self:writeInt(0)
else
self:writeInt(table.mapSize(map));
self:writeInt(table.mapSize(map))
for key, value in pairs(map) do
self:writeInt(key)
self:writeInt(value)
@@ -739,10 +746,10 @@ function ByteBuffer:readIntIntMap()
end
function ByteBuffer:writeIntLongMap(map)
if map == null then
if map == nil then
self:writeInt(0)
else
self:writeInt(table.mapSize(map));
self:writeInt(table.mapSize(map))
for key, value in pairs(map) do
self:writeInt(key)
self:writeLong(value)
@@ -765,10 +772,10 @@ function ByteBuffer:readIntLongMap()
end
function ByteBuffer:writeIntStringMap(map)
if map == null then
if map == nil then
self:writeInt(0)
else
self:writeInt(table.mapSize(map));
self:writeInt(table.mapSize(map))
for key, value in pairs(map) do
self:writeInt(key)
self:writeString(value)
@@ -791,11 +798,11 @@ function ByteBuffer:readIntStringMap()
end
function ByteBuffer:writeIntPacketMap(map, protocolId)
if map == null then
if map == nil then
self:writeInt(0)
else
local protocolRegistration = ProtocolManager.getProtocol(protocolId)
self:writeInt(table.mapSize(map));
self:writeInt(table.mapSize(map))
for key, value in pairs(map) do
self:writeInt(key)
protocolRegistration:write(self, value)
@@ -819,10 +826,10 @@ function ByteBuffer:readIntPacketMap(protocolId)
end
function ByteBuffer:writeLongIntMap(map)
if map == null then
if map == nil then
self:writeInt(0)
else
self:writeInt(table.mapSize(map));
self:writeInt(table.mapSize(map))
for key, value in pairs(map) do
self:writeLong(key)
self:writeInt(value)
@@ -845,10 +852,10 @@ function ByteBuffer:readLongIntMap()
end
function ByteBuffer:writeLongLongMap(map)
if map == null then
if map == nil then
self:writeInt(0)
else
self:writeInt(table.mapSize(map));
self:writeInt(table.mapSize(map))
for key, value in pairs(map) do
self:writeLong(key)
self:writeLong(value)
@@ -871,10 +878,10 @@ function ByteBuffer:readLongLongMap()
end
function ByteBuffer:writeLongStringMap(map)
if map == null then
if map == nil then
self:writeInt(0)
else
self:writeInt(table.mapSize(map));
self:writeInt(table.mapSize(map))
for key, value in pairs(map) do
self:writeLong(key)
self:writeString(value)
@@ -897,11 +904,11 @@ function ByteBuffer:readLongStringMap()
end
function ByteBuffer:writeLongPacketMap(map, protocolId)
if map == null then
if map == nil then
self:writeInt(0)
else
local protocolRegistration = ProtocolManager.getProtocol(protocolId)
self:writeInt(table.mapSize(map));
self:writeInt(table.mapSize(map))
for key, value in pairs(map) do
self:writeLong(key)
protocolRegistration:write(self, value)
@@ -925,10 +932,10 @@ function ByteBuffer:readLongPacketMap(protocolId)
end
function ByteBuffer:writeStringIntMap(map)
if map == null then
if map == nil then
self:writeInt(0)
else
self:writeInt(table.mapSize(map));
self:writeInt(table.mapSize(map))
for key, value in pairs(map) do
self:writeString(key)
self:writeInt(value)
@@ -951,10 +958,10 @@ function ByteBuffer:readStringIntMap()
end
function ByteBuffer:writeStringLongMap(map)
if map == null then
if map == nil then
self:writeInt(0)
else
self:writeInt(table.mapSize(map));
self:writeInt(table.mapSize(map))
for key, value in pairs(map) do
self:writeString(key)
self:writeLong(value)
@@ -977,10 +984,10 @@ function ByteBuffer:readStringLongMap()
end
function ByteBuffer:writeStringStringMap(map)
if map == null then
if map == nil then
self:writeInt(0)
else
self:writeInt(table.mapSize(map));
self:writeInt(table.mapSize(map))
for key, value in pairs(map) do
self:writeString(key)
self:writeString(value)
@@ -1003,11 +1010,11 @@ function ByteBuffer:readStringStringMap()
end
function ByteBuffer:writeStringPacketMap(map, protocolId)
if map == null then
if map == nil then
self:writeInt(0)
else
local protocolRegistration = ProtocolManager.getProtocol(protocolId)
self:writeInt(table.mapSize(map));
self:writeInt(table.mapSize(map))
for key, value in pairs(map) do
self:writeString(key)
protocolRegistration:write(self, value)
@@ -33,11 +33,13 @@ function ProtocolManager.read(buffer)
end
function initProtocol()
local ComplexObject = require("LuaProtocol.Packet.ComplexObject")
local NormalObject = require("LuaProtocol.Packet.NormalObject")
local ObjectA = require("LuaProtocol.Packet.ObjectA")
local ObjectB = require("LuaProtocol.Packet.ObjectB")
local SimpleObject = require("LuaProtocol.Packet.SimpleObject")
local EmptyObject = require("zfoolua.packet.EmptyObject")
local ComplexObject = require("zfoolua.packet.ComplexObject")
local NormalObject = require("zfoolua.packet.NormalObject")
local ObjectA = require("zfoolua.packet.ObjectA")
local ObjectB = require("zfoolua.packet.ObjectB")
local SimpleObject = require("zfoolua.packet.SimpleObject")
protocols[0] = EmptyObject
protocols[100] = ComplexObject
protocols[101] = NormalObject
protocols[102] = ObjectA
@@ -1,70 +1,61 @@
-- 复杂的对象
-- 包括了各种复杂的结构,数组,List,Set,Map
--
-- @author godotg
-- 复杂的对象,包括了各种复杂的结构,数组,List,Set,Map
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 = {
-- byte类型,最简单的整形
a = a, -- byte
-- byte的包装类型
-- 优先使用基础类型,包装类型会有装箱拆箱
aa = aa, -- java.lang.Byte
a = 0, -- byte
-- byte的包装类型,优先使用基础类型,包装类型会有装箱拆箱
aa = 0, -- byte
-- 数组类型
aaa = aaa, -- byte[]
aaaa = aaaa, -- java.lang.Byte[]
b = b, -- short
bb = bb, -- java.lang.Short
bbb = bbb, -- short[]
bbbb = bbbb, -- java.lang.Short[]
c = c, -- int
cc = cc, -- java.lang.Integer
ccc = ccc, -- int[]
cccc = cccc, -- java.lang.Integer[]
d = d, -- long
dd = dd, -- java.lang.Long
ddd = ddd, -- long[]
dddd = dddd, -- java.lang.Long[]
e = e, -- float
ee = ee, -- java.lang.Float
eee = eee, -- float[]
eeee = eeee, -- java.lang.Float[]
f = f, -- double
ff = ff, -- java.lang.Double
fff = fff, -- double[]
ffff = ffff, -- java.lang.Double[]
g = g, -- boolean
gg = gg, -- java.lang.Boolean
ggg = ggg, -- boolean[]
gggg = gggg, -- java.lang.Boolean[]
h = h, -- char
hh = hh, -- java.lang.Character
hhh = hhh, -- char[]
hhhh = hhhh, -- java.lang.Character[]
jj = jj, -- java.lang.String
jjj = jjj, -- java.lang.String[]
kk = kk, -- com.zfoo.protocol.packet.ObjectA
kkk = kkk, -- com.zfoo.protocol.packet.ObjectA[]
l = l, -- java.util.List<java.lang.Integer>
ll = ll, -- java.util.List<java.util.List<java.util.List<java.lang.Integer>>>
lll = lll, -- java.util.List<java.util.List<com.zfoo.protocol.packet.ObjectA>>
llll = llll, -- java.util.List<java.lang.String>
lllll = lllll, -- java.util.List<java.util.Map<java.lang.Integer, java.lang.String>>
m = m, -- java.util.Map<java.lang.Integer, java.lang.String>
mm = mm, -- java.util.Map<java.lang.Integer, com.zfoo.protocol.packet.ObjectA>
mmm = mmm, -- java.util.Map<com.zfoo.protocol.packet.ObjectA, java.util.List<java.lang.Integer>>
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>>>>
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>>>
s = s, -- java.util.Set<java.lang.Integer>
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>>
aaa = {}, -- byte[]
aaaa = {}, -- byte[]
b = 0, -- short
bb = 0, -- short
bbb = {}, -- short[]
bbbb = {}, -- short[]
c = 0, -- int
cc = 0, -- int
ccc = {}, -- int[]
cccc = {}, -- int[]
d = 0, -- long
dd = 0, -- long
ddd = {}, -- long[]
dddd = {}, -- long[]
e = 0, -- float
ee = 0, -- float
eee = {}, -- float[]
eeee = {}, -- float[]
f = 0, -- double
ff = 0, -- double
fff = {}, -- double[]
ffff = {}, -- double[]
g = false, -- bool
gg = false, -- bool
ggg = {}, -- bool[]
gggg = {}, -- bool[]
jj = "", -- string
jjj = {}, -- string[]
kk = nil, -- ObjectA
kkk = {}, -- ObjectA[]
l = {}, -- List<int>
ll = {}, -- List<List<List<int>>>
lll = {}, -- List<List<ObjectA>>
llll = {}, -- List<string>
lllll = {}, -- List<Dictionary<int, string>>
m = {}, -- Dictionary<int, string>
mm = {}, -- Dictionary<int, ObjectA>
mmm = {}, -- Dictionary<ObjectA, List<int>>
mmmm = {}, -- Dictionary<List<List<ObjectA>>, List<List<List<int>>>>
mmmmm = {}, -- Dictionary<List<Dictionary<int, string>>, HashSet<Dictionary<int, string>>>
s = {}, -- HashSet<int>
ss = {}, -- HashSet<HashSet<List<int>>>
sss = {}, -- HashSet<HashSet<ObjectA>>
ssss = {}, -- HashSet<string>
sssss = {}, -- HashSet<Dictionary<int, string>>
-- 如果要修改协议并且兼容老协议,需要加上Compatible注解,按照增加的顺序添加order
myCompatible = myCompatible, -- int
myObject = myObject -- com.zfoo.protocol.packet.ObjectA
myCompatible = 0, -- int
myObject = nil -- ObjectA
}
setmetatable(obj, self)
self.__index = self
@@ -76,9 +67,12 @@ function ComplexObject:protocolId()
end
function ComplexObject:write(buffer, packet)
if buffer:writePacketFlag(packet) then
if packet == nil then
buffer:writeInt(0)
return
end
local beforeWriteIndex = buffer:getWriteOffset()
buffer:writeInt(36962)
buffer:writeByte(packet.a)
buffer:writeByte(packet.aa)
buffer:writeByteArray(packet.aaa)
@@ -107,10 +101,6 @@ function ComplexObject:write(buffer, packet)
buffer:writeBoolean(packet.gg)
buffer:writeBooleanArray(packet.ggg)
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:writeStringArray(packet.jjj)
buffer:writePacket(packet.kk, 102)
@@ -247,12 +237,15 @@ function ComplexObject:write(buffer, packet)
end
buffer:writeInt(packet.myCompatible)
buffer:writePacket(packet.myObject, 102)
buffer:adjustPadding(36962, beforeWriteIndex)
end
function ComplexObject:read(buffer)
if not(buffer:readBoolean()) then
local length = buffer:readInt()
if length == 0 then
return nil
end
local beforeReadIndex = buffer:getReadOffset()
local packet = ComplexObject:new()
local result32 = buffer:readByte()
packet.a = result32
@@ -310,109 +303,109 @@ function ComplexObject:read(buffer)
packet.ggg = array58
local array59 = buffer:readBooleanArray()
packet.gggg = array59
local result60 = buffer:readChar()
packet.h = result60
local result61 = buffer:readChar()
packet.hh = result61
local array62 = buffer:readCharArray()
packet.hhh = array62
local array63 = buffer:readCharArray()
packet.hhhh = array63
local result64 = buffer:readString()
packet.jj = result64
local array65 = buffer:readStringArray()
packet.jjj = array65
local result66 = buffer:readPacket(102)
packet.kk = result66
local array67 = buffer:readPacketArray(102)
packet.kkk = array67
local list68 = buffer:readIntArray()
packet.l = list68
local result69 = {}
local size70 = buffer:readInt()
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)
local result60 = buffer:readString()
packet.jj = result60
local array61 = buffer:readStringArray()
packet.jjj = array61
local result62 = buffer:readPacket(102)
packet.kk = result62
local array63 = buffer:readPacketArray(102)
packet.kkk = array63
local list64 = buffer:readIntArray()
packet.l = list64
local result65 = {}
local size66 = buffer:readInt()
if size66 > 0 then
for index67 = 1, size66 do
local result68 = {}
local size69 = buffer:readInt()
if size69 > 0 then
for index70 = 1, size69 do
local list71 = buffer:readIntArray()
table.insert(result68, list71)
end
end
table.insert(result69, result72)
table.insert(result65, result68)
end
end
packet.ll = result69
local result76 = {}
local size77 = buffer:readInt()
if size77 > 0 then
for index78 = 1, size77 do
local list79 = buffer:readPacketArray(102)
table.insert(result76, list79)
packet.ll = result65
local result72 = {}
local size73 = buffer:readInt()
if size73 > 0 then
for index74 = 1, size73 do
local list75 = buffer:readPacketArray(102)
table.insert(result72, list75)
end
end
packet.lll = result76
local list80 = buffer:readStringArray()
packet.llll = list80
local result81 = {}
local size82 = buffer:readInt()
if size82 > 0 then
for index83 = 1, size82 do
local map84 = buffer:readIntStringMap()
table.insert(result81, map84)
packet.lll = result72
local list76 = buffer:readStringArray()
packet.llll = list76
local result77 = {}
local size78 = buffer:readInt()
if size78 > 0 then
for index79 = 1, size78 do
local map80 = buffer:readIntStringMap()
table.insert(result77, map80)
end
end
packet.lllll = result81
local map85 = buffer:readIntStringMap()
packet.m = map85
local map86 = buffer:readIntPacketMap(102)
packet.mm = map86
local result87 = {}
local size88 = buffer:readInt()
if size88 > 0 then
for index89 = 1, size88 do
local result90 = buffer:readPacket(102)
local list91 = buffer:readIntArray()
result87[result90] = list91
packet.lllll = result77
local map81 = buffer:readIntStringMap()
packet.m = map81
local map82 = buffer:readIntPacketMap(102)
packet.mm = map82
local result83 = {}
local size84 = buffer:readInt()
if size84 > 0 then
for index85 = 1, size84 do
local result86 = buffer:readPacket(102)
local list87 = buffer:readIntArray()
result83[result86] = list87
end
end
packet.mmm = result87
local result92 = {}
local size93 = buffer:readInt()
if size93 > 0 then
for index94 = 1, size93 do
packet.mmm = result83
local result88 = {}
local size89 = buffer:readInt()
if size89 > 0 then
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 size96 = buffer:readInt()
if size96 > 0 then
for index97 = 1, size96 do
local list98 = buffer:readPacketArray(102)
table.insert(result95, list98)
end
end
local result99 = {}
local size100 = buffer:readInt()
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)
local result98 = {}
local size99 = buffer:readInt()
if size99 > 0 then
for index100 = 1, size99 do
local list101 = buffer:readIntArray()
table.insert(result98, list101)
end
end
table.insert(result99, result102)
table.insert(result95, result98)
end
end
result92[result95] = result99
result88[result91] = result95
end
end
packet.mmmm = result92
local result106 = {}
local size107 = buffer:readInt()
if size107 > 0 then
for index108 = 1, size107 do
packet.mmmm = result88
local result102 = {}
local size103 = buffer:readInt()
if size103 > 0 then
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 size110 = buffer:readInt()
if size110 > 0 then
@@ -421,66 +414,59 @@ function ComplexObject:read(buffer)
table.insert(result109, map112)
end
end
local result113 = {}
local size114 = buffer:readInt()
if size114 > 0 then
for index115 = 1, size114 do
local map116 = buffer:readIntStringMap()
table.insert(result113, map116)
result102[result105] = result109
end
end
packet.mmmmm = result102
local set113 = buffer:readIntArray()
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
result106[result109] = result113
table.insert(result114, result117)
end
end
packet.mmmmm = result106
local set117 = buffer:readIntArray()
packet.s = set117
local result118 = {}
local size119 = buffer:readInt()
if size119 > 0 then
for index120 = 1, size119 do
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)
packet.ss = result114
local result121 = {}
local size122 = buffer:readInt()
if size122 > 0 then
for index123 = 1, size122 do
local set124 = buffer:readPacketArray(102)
table.insert(result121, set124)
end
end
packet.ss = result118
local result125 = {}
local size126 = buffer:readInt()
if size126 > 0 then
for index127 = 1, size126 do
local set128 = buffer:readPacketArray(102)
table.insert(result125, set128)
packet.sss = result121
local set125 = buffer:readStringArray()
packet.ssss = set125
local result126 = {}
local size127 = buffer:readInt()
if size127 > 0 then
for index128 = 1, size127 do
local map129 = buffer:readIntStringMap()
table.insert(result126, map129)
end
end
packet.sss = result125
local set129 = buffer:readStringArray()
packet.ssss = set129
local result130 = {}
local size131 = buffer:readInt()
if size131 > 0 then
for index132 = 1, size131 do
local map133 = buffer:readIntStringMap()
table.insert(result130, map133)
end
packet.sssss = result126
if buffer:compatibleRead(beforeReadIndex, length) then
local result130 = buffer:readInt()
packet.myCompatible = result130
end
packet.sssss = result130
if not(buffer:isReadable()) then
return packet
if buffer:compatibleRead(beforeReadIndex, length) then
local result131 = buffer:readPacket(102)
packet.myObject = result131
end
local result134 = buffer:readInt()
packet.myCompatible = result134
if not(buffer:isReadable()) then
return packet
if length > 0 then
buffer:setReadOffset(beforeReadIndex + length)
end
local result135 = buffer:readPacket(102)
packet.myObject = result135
return packet
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 = {}
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 = {
a = a, -- byte
aaa = aaa, -- byte[]
b = b, -- short
c = c, -- int
d = d, -- long
e = e, -- float
f = f, -- double
g = g, -- boolean
jj = jj, -- java.lang.String
kk = kk, -- com.zfoo.protocol.packet.ObjectA
l = l, -- java.util.List<java.lang.Integer>
ll = ll, -- java.util.List<java.lang.Long>
lll = lll, -- java.util.List<com.zfoo.protocol.packet.ObjectA>
llll = llll, -- java.util.List<java.lang.String>
m = m, -- java.util.Map<java.lang.Integer, java.lang.String>
mm = mm, -- java.util.Map<java.lang.Integer, com.zfoo.protocol.packet.ObjectA>
s = s, -- java.util.Set<java.lang.Integer>
ssss = ssss -- java.util.Set<java.lang.String>
a = 0, -- byte
aaa = {}, -- byte[]
b = 0, -- short
c = 0, -- int
d = 0, -- long
e = 0, -- float
f = 0, -- double
g = false, -- bool
jj = "", -- string
kk = nil, -- ObjectA
l = {}, -- List<int>
ll = {}, -- List<long>
lll = {}, -- List<ObjectA>
llll = {}, -- List<string>
m = {}, -- Dictionary<int, string>
mm = {}, -- Dictionary<int, ObjectA>
s = {}, -- HashSet<int>
ssss = {}, -- HashSet<string>
outCompatibleValue = 0 -- int
}
setmetatable(obj, self)
self.__index = self
@@ -33,9 +33,12 @@ function NormalObject:protocolId()
end
function NormalObject:write(buffer, packet)
if buffer:writePacketFlag(packet) then
if packet == nil then
buffer:writeInt(0)
return
end
local beforeWriteIndex = buffer:getWriteOffset()
buffer:writeInt(854)
buffer:writeByte(packet.a)
buffer:writeByteArray(packet.aaa)
buffer:writeShort(packet.b)
@@ -54,12 +57,16 @@ function NormalObject:write(buffer, packet)
buffer:writeIntPacketMap(packet.mm, 102)
buffer:writeIntArray(packet.s)
buffer:writeStringArray(packet.ssss)
buffer:writeInt(packet.outCompatibleValue)
buffer:adjustPadding(854, beforeWriteIndex)
end
function NormalObject:read(buffer)
if not(buffer:readBoolean()) then
local length = buffer:readInt()
if length == 0 then
return nil
end
local beforeReadIndex = buffer:getReadOffset()
local packet = NormalObject:new()
local result0 = buffer:readByte()
packet.a = result0
@@ -97,6 +104,13 @@ function NormalObject:read(buffer)
packet.s = set16
local set17 = buffer:readStringArray()
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
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 = {}
function SimpleObject:new(c, g)
function SimpleObject:new()
local obj = {
c = c, -- int
g = g -- boolean
c = 0, -- int
g = false -- bool
}
setmetatable(obj, self)
self.__index = self
@@ -17,22 +16,29 @@ function SimpleObject:protocolId()
end
function SimpleObject:write(buffer, packet)
if buffer:writePacketFlag(packet) then
if packet == nil then
buffer:writeInt(0)
return
end
buffer:writeInt(-1)
buffer:writeInt(packet.c)
buffer:writeBoolean(packet.g)
end
function SimpleObject:read(buffer)
if not(buffer:readBoolean()) then
local length = buffer:readInt()
if length == 0 then
return nil
end
local beforeReadIndex = buffer:getReadOffset()
local packet = SimpleObject:new()
local result0 = buffer:readInt()
packet.c = result0
local result1 = buffer:readBoolean()
packet.g = result1
if length > 0 then
buffer:setReadOffset(beforeReadIndex + length)
end
return packet
end