From 2c7b818d933ebdd2276ad4bc4b099e554a4d2773 Mon Sep 17 00:00:00 2001 From: jaysunxiao Date: Fri, 22 Oct 2021 20:35:25 +0800 Subject: [PATCH] =?UTF-8?q?perf[protocol]:=20=E4=BC=98=E5=8C=96Lua?= =?UTF-8?q?=E5=8D=8F=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../registration/ProtocolAnalysis.java | 2 +- .../registration/field/ArrayField.java | 14 +- .../serializer/CutDownArraySerializer.java | 8 +- .../serializer/CutDownListSerializer.java | 101 +++++- .../serializer/CutDownMapSerializer.java | 73 +++- .../serializer/CutDownSetSerializer.java | 102 +++++- .../serializer/cs/CsArraySerializer.java | 4 +- .../cs/CsObjectProtocolSerializer.java | 12 +- .../enhance/EnhanceArraySerializer.java | 4 +- .../serializer/lua/LuaArraySerializer.java | 17 +- .../serializer/lua/LuaListSerializer.java | 17 +- .../serializer/lua/LuaMapSerializer.java | 17 +- .../serializer/lua/LuaSetSerializer.java | 17 +- .../serializer/reflect/ArraySerializer.java | 4 +- .../main/resources/cs/Buffer/ByteBuffer.cs | 12 + .../main/resources/gd/buffer/ByteBuffer.gd | 158 ++++----- .../main/resources/js/buffer/ByteBuffer.js | 202 +++++------ .../main/resources/lua/Buffer/ByteBuffer.lua | 318 ++++++++++++++++++ 18 files changed, 823 insertions(+), 259 deletions(-) diff --git a/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java b/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java index 53bf357b..a66f9815 100644 --- a/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java +++ b/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java @@ -385,7 +385,7 @@ public class ProtocolAnalysis { Class arrayClazz = fieldTypeClazz.getComponentType(); IFieldRegistration registration = typeToRegistration(clazz, arrayClazz); - return ArrayField.valueOf(field, registration); + return ArrayField.valueOf(registration, field.getType().getComponentType()); } else if (Set.class.isAssignableFrom(fieldTypeClazz)) { if (!fieldTypeClazz.equals(Set.class)) { throw new RunException("[class:{}]类型声明不正确,必须是Set接口类型", clazz.getCanonicalName()); diff --git a/protocol/src/main/java/com/zfoo/protocol/registration/field/ArrayField.java b/protocol/src/main/java/com/zfoo/protocol/registration/field/ArrayField.java index 5be378b7..26702c18 100644 --- a/protocol/src/main/java/com/zfoo/protocol/registration/field/ArrayField.java +++ b/protocol/src/main/java/com/zfoo/protocol/registration/field/ArrayField.java @@ -16,8 +16,6 @@ package com.zfoo.protocol.registration.field; import com.zfoo.protocol.serializer.reflect.ArraySerializer; import com.zfoo.protocol.serializer.reflect.ISerializer; -import java.lang.reflect.Field; - /** * @author jaysunxiao * @version 3.0 @@ -25,21 +23,19 @@ import java.lang.reflect.Field; public class ArrayField implements IFieldRegistration { private IFieldRegistration arrayElementRegistration; - private Field field; + private Class type; - public static ArrayField valueOf(Field field, IFieldRegistration arrayElementRegistration) { + public static ArrayField valueOf(IFieldRegistration arrayElementRegistration, Class type) { ArrayField arrayField = new ArrayField(); - arrayField.field = field; arrayField.arrayElementRegistration = arrayElementRegistration; + arrayField.type = type; return arrayField; } - - public Field getField() { - return field; + public Class getType() { + return type; } - @Override public ISerializer serializer() { return ArraySerializer.INSTANCE; 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 ee66f031..ea752e8b 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java @@ -15,6 +15,7 @@ package com.zfoo.protocol.serializer; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.EnhanceUtils; import com.zfoo.protocol.registration.field.ArrayField; +import com.zfoo.protocol.registration.field.BaseField; import com.zfoo.protocol.registration.field.IFieldRegistration; import com.zfoo.protocol.registration.field.ObjectProtocolField; import com.zfoo.protocol.serializer.enhance.EnhanceObjectProtocolSerializer; @@ -832,8 +833,11 @@ public class CutDownArraySerializer implements ICutDownSerializer { } public String getArrayClassName(ArrayField arrayField) { - // 去掉包装类型的前缀java.lang - return arrayField.getField().getType().getComponentType().getCanonicalName().replaceFirst("java.lang.", StringUtils.EMPTY); + if (arrayField.getArrayElementRegistration() instanceof BaseField) { + return arrayField.getType().getSimpleName(); + } else { + return arrayField.getType().getCanonicalName(); + } } } 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 407c845b..14da3dca 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java @@ -14,6 +14,7 @@ package com.zfoo.protocol.serializer; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.EnhanceUtils; +import com.zfoo.protocol.registration.field.BaseField; import com.zfoo.protocol.registration.field.IFieldRegistration; import com.zfoo.protocol.registration.field.ListField; import com.zfoo.protocol.registration.field.ObjectProtocolField; @@ -21,6 +22,7 @@ import com.zfoo.protocol.serializer.enhance.EnhanceObjectProtocolSerializer; import com.zfoo.protocol.util.StringUtils; import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; import static com.zfoo.protocol.util.FileUtils.LS; @@ -41,8 +43,9 @@ public class CutDownListSerializer implements ICutDownSerializer { var listField = (ListField) fieldRegistration; var flag = true; - switch (listField.getType().getTypeName()) { - case "java.util.List": + var listName = getListClassName(listField); + switch (listName) { + case "Boolean": switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeBooleanList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); @@ -53,6 +56,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeBooleanArray({})", objectStr)).append(LS); break; + case Lua: + builder.append(StringUtils.format("buffer:writeBooleanArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteBooleanList({});", objectStr)).append(LS); break; @@ -60,7 +66,7 @@ public class CutDownListSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.List": + case "Byte": switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeByteList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); @@ -71,6 +77,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS); break; + case Lua: + builder.append(StringUtils.format("buffer:writeByteArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteByteList({});", objectStr)).append(LS); break; @@ -78,7 +87,7 @@ public class CutDownListSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.List": + case "Short": switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeShortList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); @@ -89,6 +98,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS); break; + case Lua: + builder.append(StringUtils.format("buffer:writeShortArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteShortList({});", objectStr)).append(LS); break; @@ -96,7 +108,7 @@ public class CutDownListSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.List": + case "Integer": switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeIntList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); @@ -107,6 +119,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS); break; + case Lua: + builder.append(StringUtils.format("buffer:writeIntArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteIntList({});", objectStr)).append(LS); break; @@ -114,7 +129,7 @@ public class CutDownListSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.List": { + case "Long": { switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeLongList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); @@ -125,6 +140,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS); break; + case Lua: + builder.append(StringUtils.format("buffer:writeLongArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteLongList({});", objectStr)).append(LS); break; @@ -133,7 +151,7 @@ public class CutDownListSerializer implements ICutDownSerializer { } break; } - case "java.util.List": { + case "Float": { switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeFloatList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); @@ -144,6 +162,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS); break; + case Lua: + builder.append(StringUtils.format("buffer:writeFloatArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteFloatList({});", objectStr)).append(LS); break; @@ -152,7 +173,7 @@ public class CutDownListSerializer implements ICutDownSerializer { } break; } - case "java.util.List": { + case "Double": { switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeDoubleList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); @@ -163,6 +184,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS); break; + case Lua: + builder.append(StringUtils.format("buffer:writeDoubleArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteDoubleList({});", objectStr)).append(LS); break; @@ -171,7 +195,7 @@ public class CutDownListSerializer implements ICutDownSerializer { } break; } - case "java.util.List": + case "String": switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeStringList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); @@ -182,6 +206,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeStringArray({})", objectStr)).append(LS); break; + case Lua: + builder.append(StringUtils.format("buffer:writeStringArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteStringList({});", objectStr)).append(LS); break; @@ -203,6 +230,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, objectProtocolField.getProtocolId())).append(LS); break; + case Lua: + builder.append(StringUtils.format("buffer:writePacketArray({}, {})", objectStr, objectProtocolField.getProtocolId())).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WritePacketList({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS); break; @@ -223,8 +253,9 @@ public class CutDownListSerializer implements ICutDownSerializer { var list = "list" + GenerateProtocolFile.index.getAndIncrement(); var flag = true; - switch (listField.getType().getTypeName()) { - case "java.util.List": + var listName = getListClassName(listField); + switch (listName) { + case "Boolean": switch (language) { case Enhance: builder.append(StringUtils.format("List {} = {}.readBooleanList($1);", list, EnhanceUtils.byteBufUtils)); @@ -235,6 +266,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", list)).append(LS); break; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readBooleanArray()", list)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadBooleanList();", list)).append(LS); break; @@ -242,7 +276,7 @@ public class CutDownListSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.List": + case "Byte": switch (language) { case Enhance: builder.append(StringUtils.format("List {} = {}.readByteList($1);", list, EnhanceUtils.byteBufUtils)); @@ -253,6 +287,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readByteArray()", list)).append(LS); break; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readByteArray()", list)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadByteList();", list)).append(LS); break; @@ -260,7 +297,7 @@ public class CutDownListSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.List": + case "Short": switch (language) { case Enhance: builder.append(StringUtils.format("List {} = {}.readShortList($1);", list, EnhanceUtils.byteBufUtils)); @@ -271,6 +308,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readShortArray()", list)).append(LS); break; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readShortArray()", list)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadShortList();", list)).append(LS); break; @@ -278,7 +318,7 @@ public class CutDownListSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.List": + case "Integer": switch (language) { case Enhance: builder.append(StringUtils.format("List {} = {}.readIntList($1);", list, EnhanceUtils.byteBufUtils)); @@ -289,6 +329,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntArray()", list)).append(LS); break; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readIntArray()", list)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntList();", list)).append(LS); break; @@ -296,7 +339,7 @@ public class CutDownListSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.List": + case "Long": switch (language) { case Enhance: builder.append(StringUtils.format("List {} = {}.readLongList($1);", list, EnhanceUtils.byteBufUtils)); @@ -307,6 +350,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongArray()", list)).append(LS); break; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readLongArray()", list)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongList();", list)).append(LS); break; @@ -314,7 +360,7 @@ public class CutDownListSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.List": + case "Float": switch (language) { case Enhance: builder.append(StringUtils.format("List {} = {}.readFloatList($1);", list, EnhanceUtils.byteBufUtils)); @@ -325,6 +371,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readFloatArray()", list)).append(LS); break; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readFloatArray()", list)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadFloatList();", list)).append(LS); break; @@ -332,7 +381,7 @@ public class CutDownListSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.List": + case "Double": switch (language) { case Enhance: builder.append(StringUtils.format("List {} = {}.readDoubleList($1);", list, EnhanceUtils.byteBufUtils)); @@ -343,6 +392,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", list)).append(LS); break; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readDoubleArray()", list)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadDoubleList();", list)).append(LS); break; @@ -350,7 +402,7 @@ public class CutDownListSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.List": + case "String": switch (language) { case Enhance: builder.append(StringUtils.format("List {} = {}.readStringList($1);", list, EnhanceUtils.byteBufUtils)); @@ -361,6 +413,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringArray()", list)).append(LS); break; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readStringArray()", list)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringList();", list)).append(LS); break; @@ -381,6 +436,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", list, objectProtocolField.getProtocolId())).append(LS); break; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readPacketArray({})", list, objectProtocolField.getProtocolId())).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadPacketList<{}>({});", list, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId()), objectProtocolField.getProtocolId())).append(LS); break; @@ -401,4 +459,11 @@ public class CutDownListSerializer implements ICutDownSerializer { } } + public String getListClassName(ListField listField) { + if (listField.getListElementRegistration() instanceof BaseField) { + return ((Class) ((ParameterizedType) listField.getType()).getActualTypeArguments()[0]).getSimpleName(); + } else { + return listField.getType().getTypeName(); + } + } } 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 f1f130be..0a7fbdd0 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java @@ -63,6 +63,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeIntIntMap({})", objectStr)).append(LS); return true; + case Lua: + builder.append(StringUtils.format("buffer:writeIntIntMap({})", objectStr)).append(LS); + return true; case CSharp: builder.append(StringUtils.format("buffer.WriteIntIntMap({});", objectStr)).append(LS); return true; @@ -78,6 +81,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeIntLongMap({})", objectStr)).append(LS); return true; + case Lua: + builder.append(StringUtils.format("buffer:writeIntLongMap({})", objectStr)).append(LS); + return true; case CSharp: builder.append(StringUtils.format("buffer.WriteIntLongMap({});", objectStr)).append(LS); return true; @@ -93,6 +99,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeIntStringMap({})", objectStr)).append(LS); return true; + case Lua: + builder.append(StringUtils.format("buffer:writeIntStringMap({})", objectStr)).append(LS); + return true; case CSharp: builder.append(StringUtils.format("buffer.WriteIntStringMap({});", objectStr)).append(LS); return true; @@ -108,6 +117,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeIntPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; + case Lua: + builder.append(StringUtils.format("buffer:writeIntPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + return true; case CSharp: builder.append(StringUtils.format("buffer.WriteIntPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; @@ -125,6 +137,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeLongIntMap({})", objectStr)).append(LS); return true; + case Lua: + builder.append(StringUtils.format("buffer:writeLongIntMap({})", objectStr)).append(LS); + return true; case CSharp: builder.append(StringUtils.format("buffer.WriteLongIntMap({});", objectStr)).append(LS); return true; @@ -140,6 +155,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeLongLongMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; + case Lua: + builder.append(StringUtils.format("buffer:writeLongLongMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + return true; case CSharp: builder.append(StringUtils.format("buffer.WriteLongLongMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; @@ -155,6 +173,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeLongStringMap({})", objectStr)).append(LS); return true; + case Lua: + builder.append(StringUtils.format("buffer:writeLongStringMap({})", objectStr)).append(LS); + return true; case CSharp: builder.append(StringUtils.format("buffer.WriteLongStringMap({});", objectStr)).append(LS); return true; @@ -170,6 +191,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeLongPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; + case Lua: + builder.append(StringUtils.format("buffer:writeLongPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + return true; case CSharp: builder.append(StringUtils.format("buffer.WriteLongPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; @@ -187,6 +211,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeStringIntMap({})", objectStr)).append(LS); return true; + case Lua: + builder.append(StringUtils.format("buffer:writeStringIntMap({})", objectStr)).append(LS); + return true; case CSharp: builder.append(StringUtils.format("buffer.WriteStringIntMap({});", objectStr)).append(LS); return true; @@ -202,6 +229,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeStringLongMap({})", objectStr)).append(LS); return true; + case Lua: + builder.append(StringUtils.format("buffer:writeStringLongMap({})", objectStr)).append(LS); + return true; case CSharp: builder.append(StringUtils.format("buffer.WriteStringLongMap({});", objectStr)).append(LS); return true; @@ -217,6 +247,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeStringStringMap({})", objectStr)).append(LS); return true; + case Lua: + builder.append(StringUtils.format("buffer:writeStringStringMap({})", objectStr)).append(LS); + return true; case CSharp: builder.append(StringUtils.format("buffer.WriteStringStringMap({});", objectStr)).append(LS); return true; @@ -232,6 +265,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeStringPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; + case Lua: + builder.append(StringUtils.format("buffer:writeStringPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + return true; case CSharp: builder.append(StringUtils.format("buffer.WriteStringPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; @@ -265,6 +301,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntIntMap()", map)).append(LS); return map; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readIntIntMap()", map)).append(LS); + return map; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntIntMap();", map)).append(LS); return map; @@ -280,6 +319,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntLongMap()", map)).append(LS); return map; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readIntLongMap()", map)).append(LS); + return map; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntLongMap();", map)).append(LS); return map; @@ -295,6 +337,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntStringMap()", map)).append(LS); return map; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readIntStringMap()", map)).append(LS); + return map; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntStringMap();", map)).append(LS); return map; @@ -311,6 +356,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntPacketMap({})", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return map; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readIntPacketMap({})", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + return map; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntPacketMap<{}>({});", map, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); return map; @@ -328,6 +376,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongIntMap()", map)).append(LS); return map; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readLongIntMap()", map)).append(LS); + return map; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongIntMap();", map)).append(LS); return map; @@ -343,6 +394,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongLongMap()", map)).append(LS); return map; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readLongLongMap()", map)).append(LS); + return map; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongLongMap();", map)).append(LS); return map; @@ -355,8 +409,8 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readLongStringMap();", map)).append(LS); return map; - case GdScript: - builder.append(StringUtils.format("var {} = buffer.readLongStringMap()", map)).append(LS); + case Lua: + builder.append(StringUtils.format("local {} = buffer:readLongStringMap()", map)).append(LS); return map; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongStringMap();", map)).append(LS); @@ -374,6 +428,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongPacketMap({})", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return map; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readLongPacketMap({})", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + return map; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongPacketMap<{}>({});", map, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); return map; @@ -391,6 +448,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringIntMap()", map)).append(LS); return map; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readStringIntMap()", map)).append(LS); + return map; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringIntMap();", map)).append(LS); return map; @@ -406,6 +466,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringLongMap()", map)).append(LS); return map; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readStringLongMap()", map)).append(LS); + return map; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringLongMap();", map)).append(LS); return map; @@ -421,6 +484,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringStringMap()", map)).append(LS); return map; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readStringStringMap()", map)).append(LS); + return map; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringStringMap();", map)).append(LS); return map; @@ -437,6 +503,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringPacketMap({})", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return map; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readStringPacketMap({})", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + return map; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringPacketMap<{}>({});", map, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), 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 41f7f175..8c17d839 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java @@ -14,6 +14,7 @@ package com.zfoo.protocol.serializer; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.EnhanceUtils; +import com.zfoo.protocol.registration.field.BaseField; import com.zfoo.protocol.registration.field.IFieldRegistration; import com.zfoo.protocol.registration.field.ObjectProtocolField; import com.zfoo.protocol.registration.field.SetField; @@ -21,6 +22,7 @@ import com.zfoo.protocol.serializer.enhance.EnhanceObjectProtocolSerializer; import com.zfoo.protocol.util.StringUtils; import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; import static com.zfoo.protocol.util.FileUtils.LS; @@ -40,10 +42,11 @@ public class CutDownSetSerializer implements ICutDownSerializer { public boolean writeObject(StringBuilder builder, String objectStr, Field field, IFieldRegistration fieldRegistration, CodeLanguage language) { var setField = (SetField) fieldRegistration; var flag = true; + var setName = getSetClassName(setField); // 直接在字节码里调用方法是为了减小生成字节码的体积,下面的代码去掉也不会有任何影响 - switch (setField.getType().getTypeName()) { - case "java.util.Set": + switch (setName) { + case "Boolean": switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeBooleanSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr)); @@ -54,6 +57,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeBooleanArray({})", objectStr)).append(LS); break; + case Lua: + builder.append(StringUtils.format("buffer:writeBooleanArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteBooleanSet({});", objectStr)).append(LS); break; @@ -61,7 +67,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.Set": + case "Byte": switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeByteSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr)); @@ -72,6 +78,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS); break; + case Lua: + builder.append(StringUtils.format("buffer:writeByteArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteByteSet({});", objectStr)).append(LS); break; @@ -79,7 +88,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.Set": + case "Short": switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeShortSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr)); @@ -90,6 +99,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS); break; + case Lua: + builder.append(StringUtils.format("buffer:writeShortArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteShortSet({});", objectStr)).append(LS); break; @@ -97,7 +109,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.Set": + case "Integer": switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeIntSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr)); @@ -108,6 +120,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS); break; + case Lua: + builder.append(StringUtils.format("buffer:writeIntArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteIntSet({});", objectStr)).append(LS); break; @@ -115,7 +130,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.Set": + case "Long": switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeLongSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr)); @@ -126,6 +141,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS); break; + case Lua: + builder.append(StringUtils.format("buffer:writeLongArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteLongSet({});", objectStr)).append(LS); break; @@ -133,7 +151,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.Set": + case "Float": switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeFloatSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr)); @@ -144,6 +162,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS); break; + case Lua: + builder.append(StringUtils.format("buffer:writeFloatArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteFloatSet({});", objectStr)).append(LS); break; @@ -151,7 +172,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.Set": + case "Double": switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeDoubleSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr)); @@ -162,6 +183,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS); break; + case Lua: + builder.append(StringUtils.format("buffer:writeDoubleArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteDoubleSet({});", objectStr)).append(LS); break; @@ -169,7 +193,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.Set": + case "String": switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeStringSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr)); @@ -180,6 +204,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeStringArray({})", objectStr)).append(LS); break; + case Lua: + builder.append(StringUtils.format("buffer:writeStringArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteStringSet({});", objectStr)).append(LS); break; @@ -201,6 +228,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, objectProtocolField.getProtocolId())).append(LS); break; + case Lua: + builder.append(StringUtils.format("buffer:writePacketArray({}, {})", objectStr, objectProtocolField.getProtocolId())).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WritePacketSet({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS); break; @@ -220,9 +250,10 @@ public class CutDownSetSerializer implements ICutDownSerializer { var setField = (SetField) fieldRegistration; var set = "set" + GenerateProtocolFile.index.getAndIncrement(); var flag = true; + var setName = getSetClassName(setField); - switch (setField.getType().getTypeName()) { - case "java.util.Set": + switch (setName) { + case "Boolean": switch (language) { case Enhance: builder.append(StringUtils.format("Set {} = {}.readBooleanSet($1);", set, EnhanceUtils.byteBufUtils)); @@ -233,6 +264,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", set)).append(LS); break; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readBooleanArray()", set)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadBooleanSet();", set)).append(LS); break; @@ -240,7 +274,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.Set": + case "Byte": switch (language) { case Enhance: builder.append(StringUtils.format("Set {} = {}.readByteSet($1);", set, EnhanceUtils.byteBufUtils)); @@ -251,6 +285,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readByteArray()", set)).append(LS); break; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readByteArray()", set)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadByteSet();", set)).append(LS); break; @@ -258,7 +295,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.Set": + case "Short": switch (language) { case Enhance: builder.append(StringUtils.format("Set {} = {}.readShortSet($1);", set, EnhanceUtils.byteBufUtils)); @@ -269,6 +306,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readShortArray()", set)).append(LS); break; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readShortArray()", set)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadShortSet();", set)).append(LS); break; @@ -276,7 +316,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.Set": + case "Integer": switch (language) { case Enhance: builder.append(StringUtils.format("Set {} = {}.readIntSet($1);", set, EnhanceUtils.byteBufUtils)); @@ -287,6 +327,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntArray()", set)).append(LS); break; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readIntArray()", set)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntSet();", set)).append(LS); break; @@ -294,7 +337,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.Set": + case "Long": switch (language) { case Enhance: builder.append(StringUtils.format("Set {} = {}.readLongSet($1);", set, EnhanceUtils.byteBufUtils)); @@ -305,6 +348,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongArray()", set)).append(LS); break; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readLongArray()", set)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongSet();", set)).append(LS); break; @@ -312,7 +358,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.Set": + case "Float": switch (language) { case Enhance: builder.append(StringUtils.format("Set {} = {}.readFloatSet($1);", set, EnhanceUtils.byteBufUtils)); @@ -323,6 +369,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readFloatArray()", set)).append(LS); break; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readFloatArray()", set)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadFloatSet();", set)).append(LS); break; @@ -330,7 +379,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.Set": + case "Double": switch (language) { case Enhance: builder.append(StringUtils.format("Set {} = {}.readDoubleSet($1);", set, EnhanceUtils.byteBufUtils)); @@ -341,6 +390,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", set)).append(LS); break; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readDoubleArray()", set)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadDoubleSet();", set)).append(LS); break; @@ -348,7 +400,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { flag = false; } break; - case "java.util.Set": + case "String": switch (language) { case Enhance: builder.append(StringUtils.format("Set {} = {}.readStringSet($1);", set, EnhanceUtils.byteBufUtils)); @@ -359,6 +411,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringArray()", set)).append(LS); break; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readStringArray()", set)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringSet();", set)).append(LS); break; @@ -379,6 +434,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", set, objectProtocolField.getProtocolId())).append(LS); break; + case Lua: + builder.append(StringUtils.format("local {} = buffer:readPacketArray({})", set, objectProtocolField.getProtocolId())).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadPacketSet<{}>({});", set, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId()), objectProtocolField.getProtocolId())).append(LS); break; @@ -399,4 +457,12 @@ public class CutDownSetSerializer implements ICutDownSerializer { } } + public String getSetClassName(SetField setField) { + if (setField.getSetElementRegistration() instanceof BaseField) { + return ((Class) ((ParameterizedType) setField.getType()).getActualTypeArguments()[0]).getSimpleName(); + } else { + return setField.getType().getTypeName(); + } + } + } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cs/CsArraySerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cs/CsArraySerializer.java index 5a87d06d..6f240884 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/cs/CsArraySerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cs/CsArraySerializer.java @@ -65,7 +65,7 @@ public class CsArraySerializer implements ICsSerializer { GenerateProtocolFile.addTab(builder, deep + 2); String element = "element" + GenerateProtocolFile.index.getAndIncrement(); - builder.append(StringUtils.format("{} {} = {}[{}];", GenerateCsUtils.toCsClassName(arrayField.getField().getType().getComponentType().getSimpleName()), element, objectStr, i)).append(LS); + builder.append(StringUtils.format("{} {} = {}[{}];", GenerateCsUtils.toCsClassName(arrayField.getType().getSimpleName()), element, objectStr, i)).append(LS); GenerateCsUtils.csSerializer(arrayField.getArrayElementRegistration().serializer()) .writeObject(builder, element, deep + 2, field, arrayField.getArrayElementRegistration()); @@ -88,7 +88,7 @@ public class CsArraySerializer implements ICsSerializer { var arrayField = (ArrayField) fieldRegistration; var result = "result" + GenerateProtocolFile.index.getAndIncrement(); - var typeName = GenerateCsUtils.toCsClassName(arrayField.getField().getType().getComponentType().getSimpleName()); + var typeName = GenerateCsUtils.toCsClassName(arrayField.getType().getSimpleName()); var i = "index" + GenerateProtocolFile.index.getAndIncrement(); var size = "size" + GenerateProtocolFile.index.getAndIncrement(); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cs/CsObjectProtocolSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cs/CsObjectProtocolSerializer.java index fd92a16d..9dab1fd3 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/cs/CsObjectProtocolSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cs/CsObjectProtocolSerializer.java @@ -13,10 +13,10 @@ package com.zfoo.protocol.serializer.cs; -import com.zfoo.protocol.ProtocolManager; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.IFieldRegistration; import com.zfoo.protocol.registration.field.ObjectProtocolField; +import com.zfoo.protocol.serializer.enhance.EnhanceObjectProtocolSerializer; import com.zfoo.protocol.util.StringUtils; import java.lang.reflect.Field; @@ -34,7 +34,7 @@ public class CsObjectProtocolSerializer implements ICsSerializer { public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration; GenerateProtocolFile.addTab(builder, deep); - builder.append(StringUtils.format("ProtocolManager.GetProtocol({}).Write(buffer, {});", objectProtocolField.getProtocolId(), objectStr)) + builder.append(StringUtils.format("buffer.Write({}, {});", objectStr, objectProtocolField.getProtocolId())) .append(LS); } @@ -43,14 +43,12 @@ public class CsObjectProtocolSerializer implements ICsSerializer { ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration; String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + var protocolSimpleName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId()); + GenerateProtocolFile.addTab(builder, deep); - builder.append(StringUtils.format("{} {} = ({}) ProtocolManager.GetProtocol({}).Read(buffer);", getProtocolSimpleName(objectProtocolField), result, getProtocolSimpleName(objectProtocolField), objectProtocolField.getProtocolId())) + builder.append(StringUtils.format("{} {} = buffer.Read<{}>({});", protocolSimpleName, result, protocolSimpleName, objectProtocolField.getProtocolId())) .append(LS); return result; } - private String getProtocolSimpleName(ObjectProtocolField objectProtocolField) { - return ProtocolManager.getProtocol(objectProtocolField.getProtocolId()).protocolConstructor().getDeclaringClass().getSimpleName(); - } - } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/enhance/EnhanceArraySerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/enhance/EnhanceArraySerializer.java index a5ba9862..a8e864b0 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/enhance/EnhanceArraySerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/enhance/EnhanceArraySerializer.java @@ -51,7 +51,7 @@ public class EnhanceArraySerializer implements IEnhanceSerializer { builder.append(StringUtils.format("{} {} = {}[{}];", arrayName, element, array, i)); EnhanceUtils.enhanceSerializer(arrayField.getArrayElementRegistration().serializer()) - .writeObject(builder, element, arrayField.getField(), arrayField.getArrayElementRegistration()); + .writeObject(builder, element, field, arrayField.getArrayElementRegistration()); builder.append("}"); } @@ -75,7 +75,7 @@ public class EnhanceArraySerializer implements IEnhanceSerializer { var i = "i" + GenerateProtocolFile.index.getAndIncrement(); builder.append(StringUtils.format("for(int {}=0; {} < {}; {}++){", i, i, length, i)); var readObject = EnhanceUtils.enhanceSerializer(arrayField.getArrayElementRegistration().serializer()) - .readObject(builder, arrayField.getField(), arrayField.getArrayElementRegistration()); + .readObject(builder, field, arrayField.getArrayElementRegistration()); builder.append(StringUtils.format("{}[{}] = {};}", array, i, readObject)); return array; } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaArraySerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaArraySerializer.java index 37e527db..551e93fe 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaArraySerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaArraySerializer.java @@ -16,6 +16,8 @@ package com.zfoo.protocol.serializer.lua; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.ArrayField; import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.CutDownArraySerializer; import com.zfoo.protocol.util.StringUtils; import java.lang.reflect.Field; @@ -31,9 +33,12 @@ public class LuaArraySerializer implements ILuaSerializer { @Override public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { - ArrayField arrayField = (ArrayField) fieldRegistration; - GenerateProtocolFile.addTab(builder, deep); + if (CutDownArraySerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Lua)) { + return; + } + + ArrayField arrayField = (ArrayField) fieldRegistration; builder.append(StringUtils.format("if {} == null then", objectStr)).append(LS); GenerateProtocolFile.addTab(builder, deep + 1); builder.append("byteBuffer:writeInt(0)").append(LS); @@ -57,10 +62,14 @@ public class LuaArraySerializer implements ILuaSerializer { @Override public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + var cutDown = CutDownArraySerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Lua); + if (cutDown != null) { + return cutDown; + } + var arrayField = (ArrayField) fieldRegistration; var result = "result" + GenerateProtocolFile.index.getAndIncrement(); - - GenerateProtocolFile.addTab(builder, deep); builder.append(StringUtils.format("local {} = {}", result)).append(LS); var i = "index" + GenerateProtocolFile.index.getAndIncrement(); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaListSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaListSerializer.java index b124cb1c..de2a3e16 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaListSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaListSerializer.java @@ -16,6 +16,8 @@ package com.zfoo.protocol.serializer.lua; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.IFieldRegistration; import com.zfoo.protocol.registration.field.ListField; +import com.zfoo.protocol.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.CutDownListSerializer; import com.zfoo.protocol.util.StringUtils; import java.lang.reflect.Field; @@ -30,9 +32,12 @@ public class LuaListSerializer implements ILuaSerializer { @Override public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { - ListField listField = (ListField) fieldRegistration; - GenerateProtocolFile.addTab(builder, deep); + if (CutDownListSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Lua)) { + return; + } + + ListField listField = (ListField) fieldRegistration; builder.append(StringUtils.format("if {} == null then", objectStr)).append(LS); GenerateProtocolFile.addTab(builder, deep + 1); builder.append("byteBuffer:writeInt(0)").append(LS); @@ -56,10 +61,14 @@ public class LuaListSerializer implements ILuaSerializer { @Override public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + var cutDown = CutDownListSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Lua); + if (cutDown != null) { + return cutDown; + } + ListField listField = (ListField) fieldRegistration; String result = "result" + GenerateProtocolFile.index.getAndIncrement(); - - GenerateProtocolFile.addTab(builder, deep); builder.append(StringUtils.format("local {} = {}", result)).append(LS); GenerateProtocolFile.addTab(builder, deep); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaMapSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaMapSerializer.java index 3ac68874..c45b758e 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaMapSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaMapSerializer.java @@ -16,6 +16,8 @@ package com.zfoo.protocol.serializer.lua; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.IFieldRegistration; import com.zfoo.protocol.registration.field.MapField; +import com.zfoo.protocol.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.CutDownMapSerializer; import com.zfoo.protocol.util.StringUtils; import java.lang.reflect.Field; @@ -30,9 +32,12 @@ public class LuaMapSerializer implements ILuaSerializer { @Override public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { - MapField mapField = (MapField) fieldRegistration; - GenerateProtocolFile.addTab(builder, deep); + if (CutDownMapSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Lua)) { + return; + } + + MapField mapField = (MapField) fieldRegistration; builder.append(StringUtils.format("if {} == null then", objectStr)).append(LS); GenerateProtocolFile.addTab(builder, deep + 1); builder.append("byteBuffer:writeInt(0)").append(LS); @@ -60,10 +65,14 @@ public class LuaMapSerializer implements ILuaSerializer { @Override public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + var cutDown = CutDownMapSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Lua); + if (cutDown != null) { + return cutDown; + } + MapField mapField = (MapField) fieldRegistration; String result = "result" + GenerateProtocolFile.index.getAndIncrement(); - - GenerateProtocolFile.addTab(builder, deep); builder.append(StringUtils.format("local {} = {}", result)).append(LS); GenerateProtocolFile.addTab(builder, deep); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaSetSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaSetSerializer.java index 1abfc664..70390f67 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaSetSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaSetSerializer.java @@ -16,6 +16,8 @@ package com.zfoo.protocol.serializer.lua; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.IFieldRegistration; import com.zfoo.protocol.registration.field.SetField; +import com.zfoo.protocol.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.CutDownSetSerializer; import com.zfoo.protocol.util.StringUtils; import java.lang.reflect.Field; @@ -30,9 +32,12 @@ public class LuaSetSerializer implements ILuaSerializer { @Override public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { - SetField setField = (SetField) fieldRegistration; - GenerateProtocolFile.addTab(builder, deep); + if (CutDownSetSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Lua)) { + return; + } + + SetField setField = (SetField) fieldRegistration; builder.append(StringUtils.format("if {} == null then", objectStr)).append(LS); GenerateProtocolFile.addTab(builder, deep + 1); builder.append("byteBuffer:writeInt(0)").append(LS); @@ -57,10 +62,14 @@ public class LuaSetSerializer implements ILuaSerializer { @Override public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + var cutDown = CutDownSetSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Lua); + if (cutDown != null) { + return cutDown; + } + SetField setField = (SetField) fieldRegistration; String result = "result" + GenerateProtocolFile.index.getAndIncrement(); - - GenerateProtocolFile.addTab(builder, deep); builder.append(StringUtils.format("local {} = {}", result)).append(LS); GenerateProtocolFile.addTab(builder, deep); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ArraySerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ArraySerializer.java index 3f48fa0e..f411e05d 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ArraySerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ArraySerializer.java @@ -56,10 +56,10 @@ public class ArraySerializer implements ISerializer { var length = ByteBufUtils.readInt(buffer); ArrayField arrayField = (ArrayField) fieldRegistration; if (length <= 0) { - return Array.newInstance(arrayField.getField().getType().getComponentType(), 0); + return Array.newInstance(arrayField.getType(), 0); } - Object array = Array.newInstance(arrayField.getField().getType().getComponentType(), length); + Object array = Array.newInstance(arrayField.getType(), length); for (var i = 0; i < length; i++) { Object value = arrayField.getArrayElementRegistration().serializer().readObject(buffer, arrayField.getArrayElementRegistration()); diff --git a/protocol/src/main/resources/cs/Buffer/ByteBuffer.cs b/protocol/src/main/resources/cs/Buffer/ByteBuffer.cs index 71609a34..5f451716 100644 --- a/protocol/src/main/resources/cs/Buffer/ByteBuffer.cs +++ b/protocol/src/main/resources/cs/Buffer/ByteBuffer.cs @@ -566,6 +566,18 @@ namespace CsProtocol.Buffer return flag; } + public void WritePacket(T packet, short protocolId) + { + IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId); + protocolRegistration.Write(this, (IPacket) packet); + } + + public T ReadPacket(short protocolId) + { + IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId); + return protocolRegistration.Read(this);; + } + public void WriteBooleanArray(bool[] array) { if ((array == null) || (array.Length == 0)) diff --git a/protocol/src/main/resources/gd/buffer/ByteBuffer.gd b/protocol/src/main/resources/gd/buffer/ByteBuffer.gd index 8186a2af..f66aa4bc 100644 --- a/protocol/src/main/resources/gd/buffer/ByteBuffer.gd +++ b/protocol/src/main/resources/gd/buffer/ByteBuffer.gd @@ -258,12 +258,12 @@ func readPacket(protocolId): func newInstance(protocolId: int): return ProtocolManager.newInstance(protocolId) -func writeBooleanArray(value): +func writeBooleanArray(array): if (value == null): writeInt(0) else: - writeInt(value.size()); - for element in value: + writeInt(array.size()); + for element in array: writeBool(element) func readBooleanArray(): @@ -274,12 +274,12 @@ func readBooleanArray(): array.append(readBool()) return array -func writeByteArray(value): +func writeByteArray(array): if (value == null): writeInt(0) else: - writeInt(value.size()); - for element in value: + writeInt(array.size()); + for element in array: writeByte(element) func readByteArray(): @@ -290,12 +290,12 @@ func readByteArray(): array.append(readByte()) return array -func writeShortArray(value): +func writeShortArray(array): if (value == null): writeInt(0) else: - writeInt(value.size()); - for element in value: + writeInt(array.size()); + for element in array: writeShort(element) func readShortArray(): @@ -306,12 +306,12 @@ func readShortArray(): array.append(readShort()) return array -func writeIntArray(value): +func writeIntArray(array): if (value == null): writeInt(0) else: - writeInt(value.size()); - for element in value: + writeInt(array.size()); + for element in array: writeInt(element) func readIntArray(): @@ -322,12 +322,12 @@ func readIntArray(): array.append(readInt()) return array -func writeLongArray(value): +func writeLongArray(array): if (value == null): writeInt(0) else: - writeInt(value.size()); - for element in value: + writeInt(array.size()); + for element in array: writeLong(element) func readLongArray(): @@ -338,12 +338,12 @@ func readLongArray(): array.append(readLong()) return array -func writeFloatArray(value): +func writeFloatArray(array): if (value == null): writeInt(0) else: - writeInt(value.size()); - for element in value: + writeInt(array.size()); + for element in array: writeFloat(element) func readFloatArray(): @@ -354,12 +354,12 @@ func readFloatArray(): array.append(readFloat()) return array -func writeDoubleArray(value): +func writeDoubleArray(array): if (value == null): writeInt(0) else: - writeInt(value.size()); - for element in value: + writeInt(array.size()); + for element in array: writeDouble(element) func readDoubleArray(): @@ -370,12 +370,12 @@ func readDoubleArray(): array.append(readDouble()) return array -func writeCharArray(value): +func writeCharArray(array): if (value == null): writeInt(0) else: - writeInt(value.size()); - for element in value: + writeInt(array.size()); + for element in array: writeChar(element) func readCharArray(): @@ -386,12 +386,12 @@ func readCharArray(): array.append(readChar()) return array -func writeStringArray(value): +func writeStringArray(array): if (value == null): writeInt(0) else: - writeInt(value.size()); - for element in value: + writeInt(array.size()); + for element in array: writeString(element) func readStringArray(): @@ -403,13 +403,13 @@ func readStringArray(): return array -func writePacketArray(value, protocolId): +func writePacketArray(array, protocolId): if (value == null): writeInt(0) else: var protocolRegistration = ProtocolManager.getProtocol(protocolId) - writeInt(value.size()); - for element in value: + writeInt(array.size()); + for element in array: protocolRegistration.write(self, element) func readPacketArray(protocolId): @@ -421,14 +421,14 @@ func readPacketArray(protocolId): array.append(protocolRegistration.read(self)) return array -func writeIntIntMap(value): +func writeIntIntMap(map): if (value == null): writeInt(0) else: - writeInt(value.size()) - for key in value: + writeInt(map.size()) + for key in map: writeInt(key) - writeInt(value[key]) + writeInt(map[key]) func readIntIntMap(): var map = {} @@ -440,14 +440,14 @@ func readIntIntMap(): map[key] = value return map -func writeIntLongMap(value): +func writeIntLongMap(map): if (value == null): writeInt(0) else: - writeInt(value.size()) - for key in value: - writeInt(key) - writeLong(value[key]) + writeInt(map.size()) + for key in map: + writeInt(map) + writeLong(map[key]) func readIntLongMap(): var map = {} @@ -459,14 +459,14 @@ func readIntLongMap(): map[key] = value return map -func writeIntStringMap(value): +func writeIntStringMap(map): if (value == null): writeInt(0) else: - writeInt(value.size()) - for key in value: + writeInt(map.size()) + for key in map: writeInt(key) - writeString(value[key]) + writeString(map[key]) func readIntStringMap(): var map = {} @@ -479,15 +479,15 @@ func readIntStringMap(): return map -func writeIntPacketMap(value, protocolId): +func writeIntPacketMap(map, protocolId): if (value == null): writeInt(0) else: var protocolRegistration = ProtocolManager.getProtocol(protocolId) - writeInt(value.size()) - for key in value: + writeInt(map.size()) + for key in map: writeInt(key) - protocolRegistration.write(self, value[key]) + protocolRegistration.write(self, map[key]) func readIntPacketMap(protocolId): var map = {} @@ -501,14 +501,14 @@ func readIntPacketMap(protocolId): return map -func writeLongIntMap(value): +func writeLongIntMap(map): if (value == null): writeInt(0) else: - writeInt(value.size()) - for key in value: + writeInt(map.size()) + for key in map: writeLong(key) - writeInt(value[key]) + writeInt(map[key]) func readLongIntMap(): var map = {} @@ -520,14 +520,14 @@ func readLongIntMap(): map[key] = value return map -func writeLongLongMap(value): +func writeLongLongMap(map): if (value == null): writeInt(0) else: - writeInt(value.size()) - for key in value: + writeInt(map.size()) + for key in map: writeLong(key) - writeLong(value[key]) + writeLong(map[key]) func readLongLongMap(): var map = {} @@ -539,14 +539,14 @@ func readLongLongMap(): map[key] = value return map -func writeLongStringMap(value): +func writeLongStringMap(map): if (value == null): writeInt(0) else: - writeInt(value.size()) - for key in value: + writeInt(map.size()) + for key in map: writeLong(key) - writeString(value[key]) + writeString(map[key]) func readLongStringMap(): var map = {} @@ -559,15 +559,15 @@ func readLongStringMap(): return map -func writeLongPacketMap(value, protocolId): +func writeLongPacketMap(map, protocolId): if (value == null): writeInt(0) else: var protocolRegistration = ProtocolManager.getProtocol(protocolId) - writeInt(value.size()) - for key in value: + writeInt(map.size()) + for key in map: writeLong(key) - protocolRegistration.write(self, value[key]) + protocolRegistration.write(self, map[key]) func readLongPacketMap(protocolId): var map = {} @@ -581,14 +581,14 @@ func readLongPacketMap(protocolId): return map -func writeStringIntMap(value): +func writeStringIntMap(map): if (value == null): writeInt(0) else: - writeInt(value.size()) - for key in value: + writeInt(map.size()) + for key in map: writeString(key) - writeInt(value[key]) + writeInt(map[key]) func readStringIntMap(): var map = {} @@ -600,14 +600,14 @@ func readStringIntMap(): map[key] = value return map -func writeStringLongMap(value): +func writeStringLongMap(map): if (value == null): writeInt(0) else: - writeInt(value.size()) - for key in value: + writeInt(map.size()) + for key in map: writeString(key) - writeLong(value[key]) + writeLong(map[key]) func readStringLongMap(): var map = {} @@ -619,14 +619,14 @@ func readStringLongMap(): map[key] = value return map -func writeStringStringMap(value): +func writeStringStringMap(map): if (value == null): writeInt(0) else: - writeInt(value.size()) - for key in value: + writeInt(map.size()) + for key in map: writeString(key) - writeString(value[key]) + writeString(map[key]) func readStringStringMap(): var map = {} @@ -639,15 +639,15 @@ func readStringStringMap(): return map -func writeStringPacketMap(value, protocolId): +func writeStringPacketMap(map, protocolId): if (value == null): writeInt(0) else: var protocolRegistration = ProtocolManager.getProtocol(protocolId) - writeInt(value.size()) - for key in value: + writeInt(map.size()) + for key in map: writeString(key) - protocolRegistration.write(self, value[key]) + protocolRegistration.write(self, map[key]) func readStringPacketMap(protocolId): var map = {} diff --git a/protocol/src/main/resources/js/buffer/ByteBuffer.js b/protocol/src/main/resources/js/buffer/ByteBuffer.js index 70cdebfb..0dc22431 100644 --- a/protocol/src/main/resources/js/buffer/ByteBuffer.js +++ b/protocol/src/main/resources/js/buffer/ByteBuffer.js @@ -1,4 +1,4 @@ -import { readInt64, writeInt64 } from './longbits.js'; +import {readInt64, writeInt64} from './longbits.js'; import ProtocolManager from '../ProtocolManager.js'; const empty_str = ''; @@ -344,28 +344,28 @@ const ByteBuffer = function() { return flag; }; - this.writePacket = function(value, protocolId) { + this.writePacket = function (packet, protocolId) { const protocolRegistration = ProtocolManager.getProtocol(protocolId); - protocolRegistration.write(this, value); + protocolRegistration.write(this, packet); }; - this.readPacket = function(protocolId) { + this.readPacket = function (protocolId) { const protocolRegistration = ProtocolManager.getProtocol(protocolId); return protocolRegistration.read(this); }; - this.writeBooleanArray = function(value) { - if (value === null) { + this.writeBooleanArray = function (array) { + if (array === null) { this.writeInt(0); } else { - this.writeInt(value.length); - value.forEach(element => { + this.writeInt(array.length); + array.forEach(element => { this.writeBoolean(element); }); } }; - this.readBooleanArray = function() { + this.readBooleanArray = function () { const array = []; const length = this.readInt(); if (length > 0) { @@ -376,18 +376,18 @@ const ByteBuffer = function() { return array; }; - this.writeByteArray = function(value) { - if (value === null) { + this.writeByteArray = function (array) { + if (array === null) { this.writeInt(0); } else { - this.writeInt(value.length); - value.forEach(element => { + this.writeInt(array.length); + array.forEach(element => { this.writeByte(element); }); } }; - this.readByteArray = function() { + this.readByteArray = function () { const array = []; const length = this.readInt(); if (length > 0) { @@ -398,18 +398,18 @@ const ByteBuffer = function() { return array; }; - this.writeShortArray = function(value) { - if (value === null) { + this.writeShortArray = function (array) { + if (array === null) { this.writeInt(0); } else { - this.writeInt(value.length); - value.forEach(element => { + this.writeInt(array.length); + array.forEach(element => { this.writeShort(element); }); } }; - this.readShortArray = function() { + this.readShortArray = function () { const array = []; const length = this.readInt(); if (length > 0) { @@ -420,18 +420,18 @@ const ByteBuffer = function() { return array; }; - this.writeIntArray = function(value) { - if (value === null) { + this.writeIntArray = function (array) { + if (array === null) { this.writeInt(0); } else { - this.writeInt(value.length); - value.forEach(element => { + this.writeInt(array.length); + array.forEach(element => { this.writeInt(element); }); } }; - this.readIntArray = function() { + this.readIntArray = function () { const array = []; const length = this.readInt(); if (length > 0) { @@ -442,18 +442,18 @@ const ByteBuffer = function() { return array; }; - this.writeLongArray = function(value) { - if (value === null) { + this.writeLongArray = function (array) { + if (array === null) { this.writeInt(0); } else { - this.writeInt(value.length); - value.forEach(element => { + this.writeInt(array.length); + array.forEach(element => { this.writeLong(element); }); } }; - this.readLongArray = function() { + this.readLongArray = function () { const array = []; const length = this.readInt(); if (length > 0) { @@ -464,18 +464,18 @@ const ByteBuffer = function() { return array; }; - this.writeFloatArray = function(value) { - if (value === null) { + this.writeFloatArray = function (array) { + if (array === null) { this.writeInt(0); } else { - this.writeInt(value.length); - value.forEach(element => { + this.writeInt(array.length); + array.forEach(element => { this.writeFloat(element); }); } }; - this.readFloatArray = function() { + this.readFloatArray = function () { const array = []; const length = this.readInt(); if (length > 0) { @@ -486,18 +486,18 @@ const ByteBuffer = function() { return array; }; - this.writeDoubleArray = function(value) { - if (value === null) { + this.writeDoubleArray = function (array) { + if (array === null) { this.writeInt(0); } else { - this.writeInt(value.length); - value.forEach(element => { + this.writeInt(array.length); + array.forEach(element => { this.writeDouble(element); }); } }; - this.readDoubleArray = function() { + this.readDoubleArray = function () { const array = []; const length = this.readInt(); if (length > 0) { @@ -508,18 +508,18 @@ const ByteBuffer = function() { return array; }; - this.writeStringArray = function(value) { - if (value === null) { + this.writeStringArray = function (array) { + if (array === null) { this.writeInt(0); } else { - this.writeInt(value.length); - value.forEach(element => { + this.writeInt(array.length); + array.forEach(element => { this.writeString(element); }); } }; - this.readStringArray = function() { + this.readStringArray = function () { const array = []; const length = this.readInt(); if (length > 0) { @@ -530,18 +530,18 @@ const ByteBuffer = function() { return array; }; - this.writeCharArray = function(value) { - if (value === null) { + this.writeCharArray = function (array) { + if (array === null) { this.writeInt(0); } else { - this.writeInt(value.length); - value.forEach(element => { + this.writeInt(array.length); + array.forEach(element => { this.writeChar(element); }); } }; - this.readCharArray = function() { + this.readCharArray = function () { const array = []; const length = this.readInt(); if (length > 0) { @@ -552,13 +552,13 @@ const ByteBuffer = function() { return array; }; - this.writePacketArray = function(value, protocolId) { - if (value === null) { + this.writePacketArray = function (array, protocolId) { + if (array === null) { this.writeInt(0); } else { const protocolRegistration = ProtocolManager.getProtocol(protocolId); - this.writeInt(value.length); - value.forEach(element => { + this.writeInt(array.length); + array.forEach(element => { protocolRegistration.write(this, element); }); } @@ -576,12 +576,12 @@ const ByteBuffer = function() { return array; }; - this.writeIntIntMap = function(value) { - if (value === null) { + this.writeIntIntMap = function (map) { + if (map === null) { this.writeInt(0); } else { - this.writeInt(value.size); - value.forEach((value, key) => { + this.writeInt(map.size); + map.forEach((value, key) => { this.writeInt(key); this.writeInt(value); }); @@ -601,12 +601,12 @@ const ByteBuffer = function() { return map; }; - this.writeIntLongMap = function(value) { - if (value === null) { + this.writeIntLongMap = function (map) { + if (map === null) { this.writeInt(0); } else { - this.writeInt(value.size); - value.forEach((value, key) => { + this.writeInt(map.size); + map.forEach((value, key) => { this.writeInt(key); this.writeLong(value); }); @@ -626,12 +626,12 @@ const ByteBuffer = function() { return map; }; - this.writeIntStringMap = function(value) { - if (value === null) { + this.writeIntStringMap = function (map) { + if (map === null) { this.writeInt(0); } else { - this.writeInt(value.size); - value.forEach((value, key) => { + this.writeInt(map.size); + map.forEach((value, key) => { this.writeInt(key); this.writeString(value); }); @@ -651,13 +651,13 @@ const ByteBuffer = function() { return map; }; - this.writeIntPacketMap = function(value, protocolId) { - if (value === null) { + this.writeIntPacketMap = function (map, protocolId) { + if (map === null) { this.writeInt(0); } else { const protocolRegistration = ProtocolManager.getProtocol(protocolId); - this.writeInt(value.size); - value.forEach((value, key) => { + this.writeInt(map.size); + map.forEach((value, key) => { this.writeInt(key); protocolRegistration.write(this, value); }); @@ -678,12 +678,12 @@ const ByteBuffer = function() { return map; }; - this.writeLongIntMap = function(value) { - if (value === null) { + this.writeLongIntMap = function (map) { + if (map === null) { this.writeInt(0); } else { - this.writeInt(value.size); - value.forEach((value, key) => { + this.writeInt(map.size); + map.forEach((value, key) => { this.writeLong(key); this.writeInt(value); }); @@ -703,12 +703,12 @@ const ByteBuffer = function() { return map; }; - this.writeLongLongMap = function(value) { - if (value === null) { + this.writeLongLongMap = function (map) { + if (map === null) { this.writeInt(0); } else { - this.writeInt(value.size); - value.forEach((value, key) => { + this.writeInt(map.size); + map.forEach((value, key) => { this.writeLong(key); this.writeLong(value); }); @@ -728,12 +728,12 @@ const ByteBuffer = function() { return map; }; - this.writeLongStringMap = function(value) { - if (value === null) { + this.writeLongStringMap = function (map) { + if (map === null) { this.writeInt(0); } else { - this.writeInt(value.size); - value.forEach((value, key) => { + this.writeInt(map.size); + map.forEach((value, key) => { this.writeLong(key); this.writeString(value); }); @@ -753,13 +753,13 @@ const ByteBuffer = function() { return map; }; - this.writeLongPacketMap = function(value, protocolId) { - if (value === null) { + this.writeLongPacketMap = function (map, protocolId) { + if (map === null) { this.writeInt(0); } else { const protocolRegistration = ProtocolManager.getProtocol(protocolId); - this.writeInt(value.size); - value.forEach((value, key) => { + this.writeInt(map.size); + map.forEach((value, key) => { this.writeLong(key); protocolRegistration.write(this, value); }); @@ -780,12 +780,12 @@ const ByteBuffer = function() { return map; }; - this.writeStringIntMap = function(value) { - if (value === null) { + this.writeStringIntMap = function (map) { + if (map === null) { this.writeInt(0); } else { - this.writeInt(value.size); - value.forEach((value, key) => { + this.writeInt(map.size); + map.forEach((value, key) => { this.writeString(key); this.writeInt(value); }); @@ -805,12 +805,12 @@ const ByteBuffer = function() { return map; }; - this.writeStringLongMap = function(value) { - if (value === null) { + this.writeStringLongMap = function (map) { + if (map === null) { this.writeInt(0); } else { - this.writeInt(value.size); - value.forEach((value, key) => { + this.writeInt(map.size); + map.forEach((value, key) => { this.writeString(key); this.writeLong(value); }); @@ -830,12 +830,12 @@ const ByteBuffer = function() { return map; }; - this.writeStringStringMap = function(value) { - if (value === null) { + this.writeStringStringMap = function (map) { + if (map === null) { this.writeInt(0); } else { - this.writeInt(value.size); - value.forEach((value, key) => { + this.writeInt(map.size); + map.forEach((value, key) => { this.writeString(key); this.writeString(value); }); @@ -855,13 +855,13 @@ const ByteBuffer = function() { return map; }; - this.writeStringPacketMap = function(value, protocolId) { - if (value === null) { + this.writeStringPacketMap = function (map, protocolId) { + if (map === null) { this.writeInt(0); } else { const protocolRegistration = ProtocolManager.getProtocol(protocolId); - this.writeInt(value.size); - value.forEach((value, key) => { + this.writeInt(map.size); + map.forEach((value, key) => { this.writeString(key); protocolRegistration.write(this, value); }); diff --git a/protocol/src/main/resources/lua/Buffer/ByteBuffer.lua b/protocol/src/main/resources/lua/Buffer/ByteBuffer.lua index abb2f513..35499206 100644 --- a/protocol/src/main/resources/lua/Buffer/ByteBuffer.lua +++ b/protocol/src/main/resources/lua/Buffer/ByteBuffer.lua @@ -708,4 +708,322 @@ function ByteBuffer:readPacketArray(protocolId) return array end +function ByteBuffer:writeIntIntMap(map) + if map == null then + self:writeInt(0) + else + self:writeInt(#map); + for key, value in pairs(map) do + self:writeInt(key) + self:writeInt(value) + end + end + return self +end + +function ByteBuffer:readIntIntMap() + local map = {} + local size = self:readInt() + if size > 0 then + for index = 1, size do + local key = self:readInt() + local value = self:readInt() + map[key] = value + end + end + return map +end + +function ByteBuffer:writeIntLongMap(map) + if map == null then + self:writeInt(0) + else + self:writeInt(#map); + for key, value in pairs(map) do + self:writeInt(key) + self:writeLong(value) + end + end + return self +end + +function ByteBuffer:readIntLongMap() + local map = {} + local size = self:readInt() + if size > 0 then + for index = 1, size do + local key = self:readInt() + local value = self:readLong() + map[key] = value + end + end + return map +end + +function ByteBuffer:writeIntStringMap(map) + if map == null then + self:writeInt(0) + else + self:writeInt(#map); + for key, value in pairs(map) do + self:writeInt(key) + self:writeString(value) + end + end + return self +end + +function ByteBuffer:readIntStringMap() + local map = {} + local size = self:readInt() + if size > 0 then + for index = 1, size do + local key = self:readInt() + local value = self:readString() + map[key] = value + end + end + return map +end + +function ByteBuffer:writeIntPacketMap(map, protocolId) + if map == null then + self:writeInt(0) + else + local protocolRegistration = ProtocolManager.getProtocol(protocolId) + self:writeInt(#map); + for key, value in pairs(map) do + self:writeInt(key) + protocolRegistration:write(self, value) + end + end + return self +end + +function ByteBuffer:readIntPacketMap(protocolId) + local map = {} + local size = self:readInt() + if size > 0 then + local protocolRegistration = ProtocolManager.getProtocol(protocolId) + for index = 1, size do + local key = self:readInt() + local value = protocolRegistration:read(self) + map[key] = value + end + end + return map +end + +function ByteBuffer:writeLongIntMap(map) + if map == null then + self:writeInt(0) + else + self:writeInt(#map); + for key, value in pairs(map) do + self:writeLong(key) + self:writeInt(value) + end + end + return self +end + +function ByteBuffer:readLongIntMap() + local map = {} + local size = self:readInt() + if size > 0 then + for index = 1, size do + local key = self:readLong() + local value = self:readInt() + map[key] = value + end + end + return map +end + +function ByteBuffer:writeLongLongMap(map) + if map == null then + self:writeInt(0) + else + self:writeInt(#map); + for key, value in pairs(map) do + self:writeLong(key) + self:writeLong(value) + end + end + return self +end + +function ByteBuffer:readLongLongMap() + local map = {} + local size = self:readInt() + if size > 0 then + for index = 1, size do + local key = self:readLong() + local value = self:readLong() + map[key] = value + end + end + return map +end + +function ByteBuffer:writeLongStringMap(map) + if map == null then + self:writeInt(0) + else + self:writeInt(#map); + for key, value in pairs(map) do + self:writeLong(key) + self:writeString(value) + end + end + return self +end + +function ByteBuffer:readLongStringMap() + local map = {} + local size = self:readInt() + if size > 0 then + for index = 1, size do + local key = self:readLong() + local value = self:readString() + map[key] = value + end + end + return map +end + +function ByteBuffer:writeLongPacketMap(map, protocolId) + if map == null then + self:writeInt(0) + else + local protocolRegistration = ProtocolManager.getProtocol(protocolId) + self:writeInt(#map); + for key, value in pairs(map) do + self:writeLong(key) + protocolRegistration:write(self, value) + end + end + return self +end + +function ByteBuffer:readLongPacketMap(protocolId) + local map = {} + local size = self:readInt() + if size > 0 then + local protocolRegistration = ProtocolManager.getProtocol(protocolId) + for index = 1, size do + local key = self:readLong() + local value = protocolRegistration:read(self) + map[key] = value + end + end + return map +end + +function ByteBuffer:writeStringIntMap(map) + if map == null then + self:writeInt(0) + else + self:writeInt(#map); + for key, value in pairs(map) do + self:writeString(key) + self:writeInt(value) + end + end + return self +end + +function ByteBuffer:readStringIntMap() + local map = {} + local size = self:readInt() + if size > 0 then + for index = 1, size do + local key = self:readString() + local value = self:readInt() + map[key] = value + end + end + return map +end + +function ByteBuffer:writeStringLongMap(map) + if map == null then + self:writeInt(0) + else + self:writeInt(#map); + for key, value in pairs(map) do + self:writeString(key) + self:writeLong(value) + end + end + return self +end + +function ByteBuffer:readStringLongMap() + local map = {} + local size = self:readInt() + if size > 0 then + for index = 1, size do + local key = self:readString() + local value = self:readLong() + map[key] = value + end + end + return map +end + +function ByteBuffer:writeStringStringMap(map) + if map == null then + self:writeInt(0) + else + self:writeInt(#map); + for key, value in pairs(map) do + self:writeString(key) + self:writeString(value) + end + end + return self +end + +function ByteBuffer:readStringStringMap() + local map = {} + local size = self:readInt() + if size > 0 then + for index = 1, size do + local key = self:readString() + local value = self:readString() + map[key] = value + end + end + return map +end + +function ByteBuffer:writeStringPacketMap(map, protocolId) + if map == null then + self:writeInt(0) + else + local protocolRegistration = ProtocolManager.getProtocol(protocolId) + self:writeInt(#map); + for key, value in pairs(map) do + self:writeString(key) + protocolRegistration:write(self, value) + end + end + return self +end + +function ByteBuffer:readStringPacketMap(protocolId) + local map = {} + local size = self:readInt() + if size > 0 then + local protocolRegistration = ProtocolManager.getProtocol(protocolId) + for index = 1, size do + local key = self:readString() + local value = protocolRegistration:read(self) + map[key] = value + end + end + return map +end + return ByteBuffer \ No newline at end of file