From 20d73fe7e6a9167fb87ec11944fe2b26810e1153 Mon Sep 17 00:00:00 2001 From: godotg Date: Sun, 27 Aug 2023 10:08:35 +0800 Subject: [PATCH] feat[python]: support python --- .../generate/GenerateProtocolPath.java | 2 +- .../serializer/CutDownArraySerializer.java | 72 +++ .../serializer/CutDownListSerializer.java | 36 ++ .../serializer/CutDownMapSerializer.java | 48 ++ .../serializer/CutDownSetSerializer.java | 54 ++ .../serializer/python/PyArraySerializer.java | 2 +- .../serializer/python/PyListSerializer.java | 2 +- .../serializer/python/PyMapSerializer.java | 2 +- .../serializer/python/PySetSerializer.java | 2 +- .../resources/gdscript/buffer/ByteBuffer.gd | 20 +- .../src/main/resources/python/ByteBuffer.py | 604 +++++++++++++++++- 11 files changed, 826 insertions(+), 18 deletions(-) diff --git a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolPath.java b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolPath.java index 7020d113..b9d83a12 100644 --- a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolPath.java +++ b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolPath.java @@ -72,7 +72,7 @@ public abstract class GenerateProtocolPath { if (StringUtils.isBlank(getProtocolPath(protocolId))) { path = StringUtils.PERIOD; } else { - path = StringUtils.format("{}.{}", path, name); + path = StringUtils.substringBeforeLast(path, StringUtils.SLASH); path = path.replaceAll(StringUtils.SLASH , StringUtils.PERIOD); } break; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java index 2d041d78..0dd5d9f9 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java @@ -52,6 +52,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeBooleanArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeBooleanArray({})", objectStr)).append(LS); break; case Lua: @@ -78,6 +79,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeBooleanBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeBooleanArray({})", objectStr)).append(LS); break; case Lua: @@ -104,6 +106,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeByteArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS); break; case Lua: @@ -130,6 +133,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeByteBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS); break; case Lua: @@ -156,6 +160,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeShortArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS); break; case Lua: @@ -182,6 +187,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeShortBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS); break; case Lua: @@ -208,6 +214,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeIntArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS); break; case Lua: @@ -234,6 +241,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeIntBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS); break; case Lua: @@ -260,6 +268,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeLongArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS); break; case Lua: @@ -286,6 +295,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeLongBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS); break; case Lua: @@ -312,6 +322,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeFloatArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS); break; case Lua: @@ -338,6 +349,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeFloatBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS); break; case Lua: @@ -364,6 +376,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeDoubleArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS); break; case Lua: @@ -390,6 +403,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeDoubleBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS); break; case Lua: @@ -416,6 +430,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeStringArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeStringArray({})", objectStr)).append(LS); break; case Lua: @@ -442,6 +457,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeCharArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeCharArray({})", objectStr)).append(LS); break; case Lua: @@ -468,6 +484,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeCharBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeCharArray({})", objectStr)).append(LS); break; case Lua: @@ -496,6 +513,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writePacketArray($1, {}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId))); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, protocolId)).append(LS); break; case Lua: @@ -541,6 +559,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", array)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readBooleanArray()", array)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readBooleanArray()", array)).append(LS); break; @@ -569,6 +590,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", array)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readBooleanArray()", array)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readBooleanArray()", array)).append(LS); break; @@ -597,6 +621,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readByteArray()", array)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readByteArray()", array)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readByteArray()", array)).append(LS); break; @@ -625,6 +652,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readByteArray()", array)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readByteArray()", array)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readByteArray()", array)).append(LS); break; @@ -653,6 +683,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readShortArray()", array)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readShortArray()", array)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readShortArray()", array)).append(LS); break; @@ -681,6 +714,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readShortArray()", array)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readShortArray()", array)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readShortArray()", array)).append(LS); break; @@ -709,6 +745,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntArray()", array)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readIntArray()", array)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readIntArray()", array)).append(LS); break; @@ -737,6 +776,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntArray()", array)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readIntArray()", array)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readIntArray()", array)).append(LS); break; @@ -765,6 +807,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongArray()", array)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readLongArray()", array)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readLongArray()", array)).append(LS); break; @@ -793,6 +838,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongArray()", array)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readLongArray()", array)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readLongArray()", array)).append(LS); break; @@ -821,6 +869,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readFloatArray()", array)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readFloatArray()", array)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readFloatArray()", array)).append(LS); break; @@ -849,6 +900,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readFloatArray()", array)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readFloatArray()", array)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readFloatArray()", array)).append(LS); break; @@ -877,6 +931,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", array)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readDoubleArray()", array)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readDoubleArray()", array)).append(LS); break; @@ -905,6 +962,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", array)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readDoubleArray()", array)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readDoubleArray()", array)).append(LS); break; @@ -933,6 +993,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringArray()", array)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readStringArray()", array)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readStringArray()", array)).append(LS); break; @@ -961,6 +1024,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readCharArray()", array)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readCharArray()", array)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readCharArray()", array)).append(LS); break; @@ -989,6 +1055,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readCharArray()", array)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readCharArray()", array)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readCharArray()", array)).append(LS); break; @@ -1017,6 +1086,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", array, protocolId)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readPacketArray({})", array, protocolId)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readPacketArray({})", array, protocolId)).append(LS); break; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java index 19bdfed9..df29046b 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java @@ -51,6 +51,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeBooleanList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeBooleanArray({})", objectStr)).append(LS); break; case Lua: @@ -77,6 +78,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeByteList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS); break; case Lua: @@ -103,6 +105,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeShortList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS); break; case Lua: @@ -129,6 +132,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeIntList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS); break; case Lua: @@ -155,6 +159,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeLongList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS); break; case Lua: @@ -182,6 +187,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeFloatList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS); break; case Lua: @@ -209,6 +215,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeDoubleList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS); break; case Lua: @@ -236,6 +243,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeStringList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeStringArray({})", objectStr)).append(LS); break; case Lua: @@ -265,6 +273,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writePacketList($1, (List){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId))); break; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, protocolId)).append(LS); break; case Lua: @@ -307,6 +316,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", list)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readBooleanArray()", list)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readBooleanArray()", list)).append(LS); break; @@ -335,6 +347,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readByteArray()", list)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readByteArray()", list)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readByteArray()", list)).append(LS); break; @@ -363,6 +378,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readShortArray()", list)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readShortArray()", list)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readShortArray()", list)).append(LS); break; @@ -391,6 +409,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntArray()", list)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readIntArray()", list)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readIntArray()", list)).append(LS); break; @@ -419,6 +440,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongArray()", list)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readLongArray()", list)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readLongArray()", list)).append(LS); break; @@ -447,6 +471,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readFloatArray()", list)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readFloatArray()", list)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readFloatArray()", list)).append(LS); break; @@ -475,6 +502,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", list)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readDoubleArray()", list)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readDoubleArray()", list)).append(LS); break; @@ -503,6 +533,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringArray()", list)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readStringArray()", list)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readStringArray()", list)).append(LS); break; @@ -533,6 +566,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", list, protocolId)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readPacketArray({})", list, protocolId)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readPacketArray({})", list, protocolId)).append(LS); break; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java index 1507a033..fe40250a 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java @@ -58,6 +58,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeIntIntMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr)); return true; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeIntIntMap({})", objectStr)).append(LS); return true; case Lua: @@ -81,6 +82,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeIntLongMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr)); return true; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeIntLongMap({})", objectStr)).append(LS); return true; case Lua: @@ -104,6 +106,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeIntStringMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr)); return true; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeIntStringMap({})", objectStr)).append(LS); return true; case Lua: @@ -127,6 +130,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeIntPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId()))); return true; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeIntPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; case Lua: @@ -152,6 +156,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeLongIntMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr)); return true; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeLongIntMap({})", objectStr)).append(LS); return true; case Lua: @@ -175,6 +180,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeLongLongMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr)); return true; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeLongLongMap({})", objectStr)).append(LS); return true; case Lua: @@ -198,6 +204,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeLongStringMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr)); return true; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeLongStringMap({})", objectStr)).append(LS); return true; case Lua: @@ -221,6 +228,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeLongPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId()))); return true; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeLongPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; case Lua: @@ -246,6 +254,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeStringIntMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr)); return true; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeStringIntMap({})", objectStr)).append(LS); return true; case Lua: @@ -269,6 +278,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeStringLongMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr)); return true; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeStringLongMap({})", objectStr)).append(LS); return true; case Lua: @@ -292,6 +302,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeStringStringMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr)); return true; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeStringStringMap({})", objectStr)).append(LS); return true; case Lua: @@ -315,6 +326,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("{}.writeStringPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId()))); return true; case GdScript: + case Python: builder.append(StringUtils.format("buffer.writeStringPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; case Lua: @@ -358,6 +370,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntIntMap()", map)).append(LS); return map; + case Python: + builder.append(StringUtils.format("{} = buffer.readIntIntMap()", map)).append(LS); + return map; case Lua: builder.append(StringUtils.format("local {} = buffer:readIntIntMap()", map)).append(LS); return map; @@ -383,6 +398,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntLongMap()", map)).append(LS); return map; + case Python: + builder.append(StringUtils.format("{} = buffer.readIntLongMap()", map)).append(LS); + return map; case Lua: builder.append(StringUtils.format("local {} = buffer:readIntLongMap()", map)).append(LS); return map; @@ -408,6 +426,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntStringMap()", map)).append(LS); return map; + case Python: + builder.append(StringUtils.format("{} = buffer.readIntStringMap()", map)).append(LS); + return map; case Lua: builder.append(StringUtils.format("local {} = buffer:readIntStringMap()", map)).append(LS); return map; @@ -434,6 +455,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntPacketMap({})", map, protocolId)).append(LS); return map; + case Python: + builder.append(StringUtils.format("{} = buffer.readIntPacketMap({})", map, protocolId)).append(LS); + return map; case Lua: builder.append(StringUtils.format("local {} = buffer:readIntPacketMap({})", map, protocolId)).append(LS); return map; @@ -460,6 +484,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongIntMap()", map)).append(LS); return map; + case Python: + builder.append(StringUtils.format("{} = buffer.readLongIntMap()", map)).append(LS); + return map; case Lua: builder.append(StringUtils.format("local {} = buffer:readLongIntMap()", map)).append(LS); return map; @@ -485,6 +512,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongLongMap()", map)).append(LS); return map; + case Python: + builder.append(StringUtils.format("{} = buffer.readLongLongMap()", map)).append(LS); + return map; case Lua: builder.append(StringUtils.format("local {} = buffer:readLongLongMap()", map)).append(LS); return map; @@ -510,6 +540,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongStringMap()", map)).append(LS); return map; + case Python: + builder.append(StringUtils.format("{} = buffer.readLongStringMap()", map)).append(LS); + return map; case Lua: builder.append(StringUtils.format("local {} = buffer:readLongStringMap()", map)).append(LS); return map; @@ -536,6 +569,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongPacketMap({})", map, protocolId)).append(LS); return map; + case Python: + builder.append(StringUtils.format("{} = buffer.readLongPacketMap({})", map, protocolId)).append(LS); + return map; case Lua: builder.append(StringUtils.format("local {} = buffer:readLongPacketMap({})", map, protocolId)).append(LS); return map; @@ -563,6 +599,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringIntMap()", map)).append(LS); return map; + case Python: + builder.append(StringUtils.format("{} = buffer.readStringIntMap()", map)).append(LS); + return map; case Lua: builder.append(StringUtils.format("local {} = buffer:readStringIntMap()", map)).append(LS); return map; @@ -588,6 +627,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringLongMap()", map)).append(LS); return map; + case Python: + builder.append(StringUtils.format("{} = buffer.readStringLongMap()", map)).append(LS); + return map; case Lua: builder.append(StringUtils.format("local {} = buffer:readStringLongMap()", map)).append(LS); return map; @@ -613,6 +655,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringStringMap()", map)).append(LS); return map; + case Python: + builder.append(StringUtils.format("{} = buffer.readStringStringMap()", map)).append(LS); + return map; case Lua: builder.append(StringUtils.format("local {} = buffer:readStringStringMap()", map)).append(LS); return map; @@ -639,6 +684,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringPacketMap({})", map, protocolId)).append(LS); return map; + case Python: + builder.append(StringUtils.format("{} = buffer.readStringPacketMap({})", map, protocolId)).append(LS); + return map; case Lua: builder.append(StringUtils.format("local {} = buffer:readStringPacketMap({})", map, protocolId)).append(LS); return map; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java index c0b7fb87..6fa73301 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java @@ -54,6 +54,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeBooleanArray({})", objectStr)).append(LS); break; + case Python: + builder.append(StringUtils.format("buffer.writeBooleanSet({})", objectStr)).append(LS); + break; case Lua: builder.append(StringUtils.format("buffer:writeBooleanArray({})", objectStr)).append(LS); break; @@ -80,6 +83,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS); break; + case Python: + builder.append(StringUtils.format("buffer.writeByteSet({})", objectStr)).append(LS); + break; case Lua: builder.append(StringUtils.format("buffer:writeByteArray({})", objectStr)).append(LS); break; @@ -106,6 +112,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS); break; + case Python: + builder.append(StringUtils.format("buffer.writeShortSet({})", objectStr)).append(LS); + break; case Lua: builder.append(StringUtils.format("buffer:writeShortArray({})", objectStr)).append(LS); break; @@ -132,6 +141,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS); break; + case Python: + builder.append(StringUtils.format("buffer.writeIntSet({})", objectStr)).append(LS); + break; case Lua: builder.append(StringUtils.format("buffer:writeIntArray({})", objectStr)).append(LS); break; @@ -158,6 +170,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS); break; + case Python: + builder.append(StringUtils.format("buffer.writeLongSet({})", objectStr)).append(LS); + break; case Lua: builder.append(StringUtils.format("buffer:writeLongArray({})", objectStr)).append(LS); break; @@ -184,6 +199,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS); break; + case Python: + builder.append(StringUtils.format("buffer.writeFloatSet({})", objectStr)).append(LS); + break; case Lua: builder.append(StringUtils.format("buffer:writeFloatArray({})", objectStr)).append(LS); break; @@ -210,6 +228,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS); break; + case Python: + builder.append(StringUtils.format("buffer.writeDoubleSet({})", objectStr)).append(LS); + break; case Lua: builder.append(StringUtils.format("buffer:writeDoubleArray({})", objectStr)).append(LS); break; @@ -236,6 +257,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeStringArray({})", objectStr)).append(LS); break; + case Python: + builder.append(StringUtils.format("buffer.writeStringSet({})", objectStr)).append(LS); + break; case Lua: builder.append(StringUtils.format("buffer:writeStringArray({})", objectStr)).append(LS); break; @@ -265,6 +289,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, protocolId)).append(LS); break; + case Python: + builder.append(StringUtils.format("buffer.writePacketSet({}, {})", objectStr, protocolId)).append(LS); + break; case Lua: builder.append(StringUtils.format("buffer:writePacketArray({}, {})", objectStr, protocolId)).append(LS); break; @@ -305,6 +332,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", set)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readBooleanSet()", set)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readBooleanArray()", set)).append(LS); break; @@ -333,6 +363,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readByteArray()", set)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readByteSet()", set)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readByteArray()", set)).append(LS); break; @@ -361,6 +394,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readShortArray()", set)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readShortSet()", set)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readShortArray()", set)).append(LS); break; @@ -389,6 +425,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntArray()", set)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readIntSet()", set)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readIntArray()", set)).append(LS); break; @@ -417,6 +456,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongArray()", set)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readLongSet()", set)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readLongArray()", set)).append(LS); break; @@ -445,6 +487,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readFloatArray()", set)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readFloatSet()", set)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readFloatArray()", set)).append(LS); break; @@ -473,6 +518,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", set)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readDoubleSet()", set)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readDoubleArray()", set)).append(LS); break; @@ -501,6 +549,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringArray()", set)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readStringSet()", set)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readStringArray()", set)).append(LS); break; @@ -531,6 +582,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", set, protocolId)).append(LS); break; + case Python: + builder.append(StringUtils.format("{} = buffer.readPacketSet({})", set, protocolId)).append(LS); + break; case Lua: builder.append(StringUtils.format("local {} = buffer:readPacketArray({})", set, protocolId)).append(LS); break; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/python/PyArraySerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/python/PyArraySerializer.java index 74b60c5a..8f46ddb5 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/python/PyArraySerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/python/PyArraySerializer.java @@ -51,7 +51,7 @@ public class PyArraySerializer implements IPySerializer { builder.append("else:").append(LS); GeneratePyUtils.addTab(builder, deep + 1); - builder.append(StringUtils.format("buffer.writeInt({}.size())", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeInt(len({}))", objectStr)).append(LS); String element = "element" + GenerateProtocolFile.index.getAndIncrement(); GeneratePyUtils.addTab(builder, deep + 1); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/python/PyListSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/python/PyListSerializer.java index f9a6b716..199977ed 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/python/PyListSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/python/PyListSerializer.java @@ -51,7 +51,7 @@ public class PyListSerializer implements IPySerializer { builder.append("else:").append(LS); GeneratePyUtils.addTab(builder, deep + 1); - builder.append(StringUtils.format("buffer.writeInt({}.size())", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeInt(len({}))", objectStr)).append(LS); String element = "element" + GenerateProtocolFile.index.getAndIncrement(); GeneratePyUtils.addTab(builder, deep + 1); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/python/PyMapSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/python/PyMapSerializer.java index a92ce89c..4520973b 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/python/PyMapSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/python/PyMapSerializer.java @@ -51,7 +51,7 @@ public class PyMapSerializer implements IPySerializer { builder.append("else:").append(LS); GeneratePyUtils.addTab(builder, deep + 1); - builder.append(StringUtils.format("buffer.writeInt({}.size())", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeInt(len({}))", objectStr)).append(LS); String key = "key" + GenerateProtocolFile.index.getAndIncrement(); String value = "value" + GenerateProtocolFile.index.getAndIncrement(); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/python/PySetSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/python/PySetSerializer.java index ae0a8cac..60752c6f 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/python/PySetSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/python/PySetSerializer.java @@ -51,7 +51,7 @@ public class PySetSerializer implements IPySerializer { builder.append("else:").append(LS); GeneratePyUtils.addTab(builder, deep + 1); - builder.append(StringUtils.format("buffer.writeInt({}.size())", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeInt(len({}))", objectStr)).append(LS); String element = "element" + GenerateProtocolFile.index.getAndIncrement(); GeneratePyUtils.addTab(builder, deep + 1); diff --git a/protocol/src/main/resources/gdscript/buffer/ByteBuffer.gd b/protocol/src/main/resources/gdscript/buffer/ByteBuffer.gd index c57b15cf..f0240a12 100644 --- a/protocol/src/main/resources/gdscript/buffer/ByteBuffer.gd +++ b/protocol/src/main/resources/gdscript/buffer/ByteBuffer.gd @@ -277,7 +277,7 @@ func writeBooleanArray(array): if (array == null): writeInt(0) else: - writeInt(array.size()); + writeInt(array.size()) for element in array: writeBool(element) @@ -293,7 +293,7 @@ func writeByteArray(array): if (array == null): writeInt(0) else: - writeInt(array.size()); + writeInt(array.size()) for element in array: writeByte(element) @@ -309,7 +309,7 @@ func writeShortArray(array): if (array == null): writeInt(0) else: - writeInt(array.size()); + writeInt(array.size()) for element in array: writeShort(element) @@ -325,7 +325,7 @@ func writeIntArray(array): if (array == null): writeInt(0) else: - writeInt(array.size()); + writeInt(array.size()) for element in array: writeInt(element) @@ -341,7 +341,7 @@ func writeLongArray(array): if (array == null): writeInt(0) else: - writeInt(array.size()); + writeInt(array.size()) for element in array: writeLong(element) @@ -357,7 +357,7 @@ func writeFloatArray(array): if (array == null): writeInt(0) else: - writeInt(array.size()); + writeInt(array.size()) for element in array: writeFloat(element) @@ -373,7 +373,7 @@ func writeDoubleArray(array): if (array == null): writeInt(0) else: - writeInt(array.size()); + writeInt(array.size()) for element in array: writeDouble(element) @@ -389,7 +389,7 @@ func writeCharArray(array): if (array == null): writeInt(0) else: - writeInt(array.size()); + writeInt(array.size()) for element in array: writeChar(element) @@ -405,7 +405,7 @@ func writeStringArray(array): if (array == null): writeInt(0) else: - writeInt(array.size()); + writeInt(array.size()) for element in array: writeString(element) @@ -423,7 +423,7 @@ func writePacketArray(array, protocolId): writeInt(0) else: var protocolRegistration = ProtocolManager.getProtocol(protocolId) - writeInt(array.size()); + writeInt(array.size()) for element in array: protocolRegistration.write(self, element) diff --git a/protocol/src/main/resources/python/ByteBuffer.py b/protocol/src/main/resources/python/ByteBuffer.py index 4b90befa..45775e57 100644 --- a/protocol/src/main/resources/python/ByteBuffer.py +++ b/protocol/src/main/resources/python/ByteBuffer.py @@ -1,4 +1,5 @@ import struct +from . import ProtocolManager maxSize = 655537 maxInt = 2147483647 @@ -11,7 +12,7 @@ empty_str = "" class ByteBuffer(): writeOffset = 0 readOffset = 0 - buffer = bytearray(8) + buffer = bytearray(256) def setWriteOffset(self, writeOffset): if writeOffset > len(self.buffer): @@ -42,6 +43,15 @@ class ByteBuffer(): # auto grow capacity self.buffer.extend(bytearray(len(self.buffer))) + def clear(self): + self.writeOffset = 0 + self.readOffset = 0 + self.buffer = bytearray(256) + pass + + def toBytes(self): + return self.buffer[0:self.writeOffset] + def writeBool(self, value): if value: self.writeByte(1) @@ -81,7 +91,7 @@ class ByteBuffer(): return value def writeShort(self, value): - self.ensureCapacity(1) + self.ensureCapacity(2) struct.pack_into('>h', self.buffer, self.writeOffset, value) self.writeOffset += 2 @@ -306,4 +316,592 @@ class ByteBuffer(): self.writeBool(False) else: self.writeBool(True) - pass \ No newline at end of file + pass + + def writePacket(self, packet, protocolId): + protocolRegistration = ProtocolManager.getProtocol(protocolId) + protocolRegistration.write(self, packet) + + def readPacket(self, protocolId): + protocolRegistration = ProtocolManager.getProtocol(protocolId) + return protocolRegistration.read(self) + + def writeBooleanArray(self, array): + if array is None: + self.writeInt(0) + else: + self.writeInt(len(array)) + for element in array: + self.writeBool(element) + pass + + def readBooleanArray(self): + array = [] + size = self.readInt() + if size > 0: + for index in range(size): + array.append(self.readBool()) + return array + + def writeByteArray(self, array): + if array is None: + self.writeInt(0) + else: + self.writeInt(len(array)) + for element in array: + self.writeByte(element) + pass + + def readByteArray(self): + array = [] + size = self.readInt() + if size > 0: + for index in range(size): + array.append(self.readByte()) + return array + + def writeShortArray(self, array): + if array is None: + self.writeInt(0) + else: + self.writeInt(len(array)) + for element in array: + self.writeShort(element) + pass + + def readShortArray(self): + array = [] + size = self.readInt() + if size > 0: + for index in range(size): + array.append(self.readShort()) + return array + + def writeIntArray(self, array): + if array is None: + self.writeInt(0) + else: + self.writeInt(len(array)) + for element in array: + self.writeInt(element) + pass + + def readIntArray(self): + array = [] + size = self.readInt() + if size > 0: + for index in range(size): + array.append(self.readInt()) + return array + + def writeLongArray(self, array): + if array is None: + self.writeInt(0) + else: + self.writeInt(len(array)) + for element in array: + self.writeLong(element) + pass + + def readLongArray(self): + array = [] + size = self.readInt() + if size > 0: + for index in range(size): + array.append(self.readLong()) + return array + + def writeFloatArray(self, array): + if array is None: + self.writeInt(0) + else: + self.writeInt(len(array)) + for element in array: + self.writeFloat(element) + pass + + def readFloatArray(self): + array = [] + size = self.readInt() + if size > 0: + for index in range(size): + array.append(self.readFloat()) + return array + + def writeDoubleArray(self, array): + if array is None: + self.writeInt(0) + else: + self.writeInt(len(array)) + for element in array: + self.writeDouble(element) + pass + + def readDoubleArray(self): + array = [] + size = self.readInt() + if size > 0: + for index in range(size): + array.append(self.readDouble()) + return array + + def writeCharArray(self, array): + if array is None: + self.writeInt(0) + else: + self.writeInt(len(array)) + for element in array: + self.writeChar(element) + pass + + def readCharArray(self): + array = [] + size = self.readInt() + if size > 0: + for index in range(size): + array.append(self.readChar()) + return array + + def writeStringArray(self, array): + if array is None: + self.writeInt(0) + else: + self.writeInt(len(array)) + for element in array: + self.writeString(element) + pass + + def readStringArray(self): + array = [] + size = self.readInt() + if size > 0: + for index in range(size): + array.append(self.readString()) + return array + + def writePacketArray(self, array, protocolId): + if array is None: + self.writeInt(0) + else: + self.writeInt(len(array)) + for element in array: + self.writePacket(element, protocolId) + pass + + def readPacketArray(self, protocolId): + array = [] + size = self.readInt() + if size > 0: + for index in range(size): + array.append(self.readPacket(protocolId)) + return array + + def writeIntIntMap(self, map): + if map is None: + self.writeInt(0) + else: + self.writeInt(len(map)) + for key, value in map.items(): + self.writeInt(key) + self.writeInt(value) + pass + + def readIntIntMap(self): + map = {} + size = self.readInt() + if size > 0: + for index in range(size): + key = self.readInt() + value = self.readInt() + map[key] = value + return map + + def writeIntLongMap(self, map): + if map is None: + self.writeInt(0) + else: + self.writeInt(len(map)) + for key, value in map.items(): + self.writeInt(key) + self.writeLong(value) + pass + + def readIntLongMap(self): + map = {} + size = self.readInt() + if size > 0: + for index in range(size): + key = self.readInt() + value = self.readLong() + map[key] = value + return map + + def writeIntStringMap(self, map): + if map is None: + self.writeInt(0) + else: + self.writeInt(len(map)) + for key, value in map.items(): + self.writeInt(key) + self.writeString(value) + pass + + def readIntStringMap(self): + map = {} + size = self.readInt() + if size > 0: + for index in range(size): + key = self.readInt() + value = self.readString() + map[key] = value + return map + + def writeIntPacketMap(self, map, protocolId): + if map is None: + self.writeInt(0) + else: + self.writeInt(len(map)) + for key, value in map.items(): + self.writeInt(key) + self.writePacket(value, protocolId) + pass + + def readIntPacketMap(self, protocolId): + map = {} + size = self.readInt() + if size > 0: + for index in range(size): + key = self.readInt() + value = self.readPacket(protocolId) + map[key] = value + return map + + def writeLongIntMap(self, map): + if map is None: + self.writeInt(0) + else: + self.writeInt(len(map)) + for key, value in map.items(): + self.writeLong(key) + self.writeInt(value) + pass + + def readLongIntMap(self): + map = {} + size = self.readInt() + if size > 0: + for index in range(size): + key = self.readLong() + value = self.readInt() + map[key] = value + return map + + def writeLongLongMap(self, map): + if map is None: + self.writeInt(0) + else: + self.writeInt(len(map)) + for key, value in map.items(): + self.writeLong(key) + self.writeLong(value) + pass + + def readLongLongMap(self): + map = {} + size = self.readInt() + if size > 0: + for index in range(size): + key = self.readLong() + value = self.readLong() + map[key] = value + return map + + def writeLongStringMap(self, map): + if map is None: + self.writeInt(0) + else: + self.writeInt(len(map)) + for key, value in map.items(): + self.writeLong(key) + self.writeString(value) + pass + + def readLongStringMap(self): + map = {} + size = self.readInt() + if size > 0: + for index in range(size): + key = self.readLong() + value = self.readString() + map[key] = value + return map + + def writeLongPacketMap(self, map, protocolId): + if map is None: + self.writeInt(0) + else: + self.writeInt(len(map)) + for key, value in map.items(): + self.writeLong(key) + self.writePacket(value, protocolId) + pass + + def readLongPacketMap(self, protocolId): + map = {} + size = self.readInt() + if size > 0: + for index in range(size): + key = self.readLong() + value = self.readPacket(protocolId) + map[key] = value + return map + + def writeStringIntMap(self, map): + if map is None: + self.writeInt(0) + else: + self.writeInt(len(map)) + for key, value in map.items(): + self.writeString(key) + self.writeInt(value) + pass + + def readStringIntMap(self): + map = {} + size = self.readInt() + if size > 0: + for index in range(size): + key = self.readString() + value = self.readInt() + map[key] = value + return map + + def writeStringLongMap(self, map): + if map is None: + self.writeInt(0) + else: + self.writeInt(len(map)) + for key, value in map.items(): + self.writeString(key) + self.writeLong(value) + pass + + def readStringLongMap(self): + map = {} + size = self.readInt() + if size > 0: + for index in range(size): + key = self.readString() + value = self.readLong() + map[key] = value + return map + + def writeStringStringMap(self, map): + if map is None: + self.writeInt(0) + else: + self.writeInt(len(map)) + for key, value in map.items(): + self.writeString(key) + self.writeString(value) + pass + + def readStringStringMap(self): + map = {} + size = self.readInt() + if size > 0: + for index in range(size): + key = self.readString() + value = self.readString() + map[key] = value + return map + + def writeStringPacketMap(self, map, protocolId): + if map is None: + self.writeInt(0) + else: + self.writeInt(len(map)) + for key, value in map.items(): + self.writeString(key) + self.writePacket(value, protocolId) + pass + + def readStringPacketMap(self, protocolId): + map = {} + size = self.readInt() + if size > 0: + for index in range(size): + key = self.readString() + value = self.readPacket(protocolId) + map[key] = value + return map + + def writeBooleanSet(self, value): + if value is None: + self.writeInt(0) + else: + self.writeInt(len(value)) + for element in value: + self.writeBool(element) + pass + + def readBooleanSet(self): + value = set() + size = self.readInt() + if size > 0: + for index in range(size): + value.add(self.readBool()) + return value + + def writeByteSet(self, value): + if value is None: + self.writeInt(0) + else: + self.writeInt(len(value)) + for element in value: + self.writeByte(element) + pass + + def readByteSet(self): + value = set() + size = self.readInt() + if size > 0: + for index in range(size): + value.add(self.readByte()) + return value + + def writeShortSet(self, value): + if value is None: + self.writeInt(0) + else: + self.writeInt(len(value)) + for element in value: + self.writeShort(element) + pass + + def readShortSet(self): + value = set() + size = self.readInt() + if size > 0: + for index in range(size): + value.add(self.readShort()) + return value + + def writeIntSet(self, value): + if value is None: + self.writeInt(0) + else: + self.writeInt(len(value)) + for element in value: + self.writeInt(element) + pass + + def readIntSet(self): + value = set() + size = self.readInt() + if size > 0: + for index in range(size): + value.add(self.readInt()) + return value + + def writeLongSet(self, value): + if value is None: + self.writeInt(0) + else: + self.writeInt(len(value)) + for element in value: + self.writeLong(element) + pass + + def readLongSet(self): + value = set() + size = self.readInt() + if size > 0: + for index in range(size): + value.add(self.readLong()) + return value + + def writeFloatSet(self, value): + if value is None: + self.writeInt(0) + else: + self.writeInt(len(value)) + for element in value: + self.writeFloat(element) + pass + + def readFloatSet(self): + value = set() + size = self.readInt() + if size > 0: + for index in range(size): + value.add(self.readFloat()) + return value + + def writeDoubleSet(self, value): + if value is None: + self.writeInt(0) + else: + self.writeInt(len(value)) + for element in value: + self.writeDouble(element) + pass + + def readDoubleSet(self): + value = set() + size = self.readInt() + if size > 0: + for index in range(size): + value.add(self.readDouble()) + return value + + def writeCharSet(self, value): + if value is None: + self.writeInt(0) + else: + self.writeInt(len(value)) + for element in value: + self.writeChar(element) + pass + + def readCharValue(self): + value = set() + size = self.readInt() + if size > 0: + for index in range(size): + value.add(self.readChar()) + return value + + def writeStringSet(self, value): + if value is None: + self.writeInt(0) + else: + self.writeInt(len(value)) + for element in value: + self.writeString(element) + pass + + def readStringSet(self): + value = set() + size = self.readInt() + if size > 0: + for index in range(size): + value.add(self.readString()) + return value + + def writePacketSet(self, value, protocolId): + if value is None: + self.writeInt(0) + else: + self.writeInt(len(value)) + for element in value: + self.writePacket(element, protocolId) + pass + + def readPacketSet(self, protocolId): + value = set() + size = self.readInt() + if size > 0: + for index in range(size): + value.add(self.readPacket(protocolId)) + return value \ No newline at end of file