Skip to content

Commit

Permalink
feat[lua]: compatible field of inside protocol class in lua
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Oct 18, 2023
1 parent 468a1bd commit e5b0cdd
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
*/
public abstract class GenerateLuaUtils {

private static String protocolOutputRootPath = "LuaProtocol/";
// custom configuration
public static String protocolOutputRootPath = "zfoolua";
public static String protocolOutputPath = StringUtils.EMPTY;

private static Map<ISerializer, ILuaSerializer> luaSerializerMap;

Expand All @@ -51,10 +53,13 @@ public static ILuaSerializer luaSerializer(ISerializer serializer) {
}

public static void init(GenerateOperation generateOperation) {
protocolOutputRootPath = FileUtils.joinPath(generateOperation.getProtocolPath(), protocolOutputRootPath);

FileUtils.deleteFile(new File(protocolOutputRootPath));
FileUtils.createDirectory(protocolOutputRootPath);
// if not specify output path, then use current default path
if (StringUtils.isEmpty(generateOperation.getProtocolPath())) {
protocolOutputPath = FileUtils.joinPath(generateOperation.getProtocolPath(), protocolOutputRootPath);
} else {
protocolOutputPath = generateOperation.getProtocolPath();
}
FileUtils.deleteFile(new File(protocolOutputPath));

luaSerializerMap = new HashMap<>();
luaSerializerMap.put(BooleanSerializer.INSTANCE, new LuaBooleanSerializer());
Expand All @@ -75,6 +80,7 @@ public static void init(GenerateOperation generateOperation) {
public static void clear() {
luaSerializerMap = null;
protocolOutputRootPath = null;
protocolOutputPath = null;
}

public static void createProtocolManager(List<IProtocolRegistration> protocolList) throws IOException {
Expand Down
97 changes: 76 additions & 21 deletions protocol/src/main/resources/lua/Buffer/ByteBuffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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(currentWriteIndex - length)
self:writeInt(length)
self:writeRawByteStr(bytes)
end
end

function ByteBuffer:compatibleRead(beforeReadIndex, length)
return length ~= -1 and self:getReadOffset() < length + beforeReadIndex
end

-------------------------------------get和set-------------------------------------
function ByteBuffer:getWriteOffset()
Expand Down Expand Up @@ -202,6 +223,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 = longValue >> 63
local value = longValue << 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)
Expand Down Expand Up @@ -469,7 +524,7 @@ function ByteBuffer:writeBooleanArray(array)
if array == null then
self:writeInt(0)
else
self:writeInt(#array);
self:writeInt(#array)
for index, element in pairs(array) do
self:writeBoolean(element)
end
Expand All @@ -492,7 +547,7 @@ function ByteBuffer:writeByteArray(array)
if array == null then
self:writeInt(0)
else
self:writeInt(#array);
self:writeInt(#array)
for index, element in pairs(array) do
self:writeByte(element)
end
Expand All @@ -515,7 +570,7 @@ function ByteBuffer:writeShortArray(array)
if array == null then
self:writeInt(0)
else
self:writeInt(#array);
self:writeInt(#array)
for index, element in pairs(array) do
self:writeShort(element)
end
Expand All @@ -538,7 +593,7 @@ function ByteBuffer:writeIntArray(array)
if array == null then
self:writeInt(0)
else
self:writeInt(#array);
self:writeInt(#array)
for index, element in pairs(array) do
self:writeInt(element)
end
Expand All @@ -561,7 +616,7 @@ function ByteBuffer:writeLongArray(array)
if array == null then
self:writeInt(0)
else
self:writeInt(#array);
self:writeInt(#array)
for index, element in pairs(array) do
self:writeLong(element)
end
Expand All @@ -584,7 +639,7 @@ function ByteBuffer:writeFloatArray(array)
if array == null then
self:writeInt(0)
else
self:writeInt(#array);
self:writeInt(#array)
for index, element in pairs(array) do
self:writeFloat(element)
end
Expand All @@ -607,7 +662,7 @@ function ByteBuffer:writeDoubleArray(array)
if array == null then
self:writeInt(0)
else
self:writeInt(#array);
self:writeInt(#array)
for index, element in pairs(array) do
self:writeDouble(element)
end
Expand All @@ -630,7 +685,7 @@ function ByteBuffer:writeStringArray(array)
if array == null then
self:writeInt(0)
else
self:writeInt(#array);
self:writeInt(#array)
for index, element in pairs(array) do
self:writeString(element)
end
Expand All @@ -654,7 +709,7 @@ function ByteBuffer:writePacketArray(array, protocolId)
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
Expand All @@ -678,7 +733,7 @@ function ByteBuffer:writeIntIntMap(map)
if map == null 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)
Expand All @@ -704,7 +759,7 @@ function ByteBuffer:writeIntLongMap(map)
if map == null 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)
Expand All @@ -730,7 +785,7 @@ function ByteBuffer:writeIntStringMap(map)
if map == null 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)
Expand All @@ -757,7 +812,7 @@ function ByteBuffer:writeIntPacketMap(map, protocolId)
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)
Expand All @@ -784,7 +839,7 @@ function ByteBuffer:writeLongIntMap(map)
if map == null 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)
Expand All @@ -810,7 +865,7 @@ function ByteBuffer:writeLongLongMap(map)
if map == null 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)
Expand All @@ -836,7 +891,7 @@ function ByteBuffer:writeLongStringMap(map)
if map == null 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)
Expand All @@ -863,7 +918,7 @@ function ByteBuffer:writeLongPacketMap(map, protocolId)
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)
Expand All @@ -890,7 +945,7 @@ function ByteBuffer:writeStringIntMap(map)
if map == null 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)
Expand All @@ -916,7 +971,7 @@ function ByteBuffer:writeStringLongMap(map)
if map == null 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)
Expand All @@ -942,7 +997,7 @@ function ByteBuffer:writeStringStringMap(map)
if map == null 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)
Expand All @@ -969,7 +1024,7 @@ function ByteBuffer:writeStringPacketMap(map, protocolId)
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)
Expand Down

0 comments on commit e5b0cdd

Please sign in to comment.