diff --git a/.gitignore b/.gitignore index a9e9c991..cf547437 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,7 @@ hs_err_pid* # 忽略生成的协议文件 **/jsProtocol/ +**/gdProtocol/ **/CsProtocol/ **/LuaProtocol/ **/zapp-user/protocol/ diff --git a/net/src/main/java/com/zfoo/net/config/model/NetConfig.java b/net/src/main/java/com/zfoo/net/config/model/NetConfig.java index b268ee9a..327a29cf 100644 --- a/net/src/main/java/com/zfoo/net/config/model/NetConfig.java +++ b/net/src/main/java/com/zfoo/net/config/model/NetConfig.java @@ -36,6 +36,7 @@ public class NetConfig { private boolean generateJsProtocol; private boolean generateCsProtocol; private boolean generateLuaProtocol; + private boolean generateGdProtocol; private RegistryConfig registry; private MonitorConfig monitor; @@ -136,6 +137,14 @@ public class NetConfig { this.consumer = consumer; } + public boolean isGenerateGdProtocol() { + return generateGdProtocol; + } + + public void setGenerateGdProtocol(boolean generateGdProtocol) { + this.generateGdProtocol = generateGdProtocol; + } + @Override public boolean equals(Object o) { if (this == o) { @@ -145,8 +154,7 @@ public class NetConfig { return false; } NetConfig netConfig = (NetConfig) o; - return generateJsProtocol == netConfig.generateJsProtocol && - Objects.equals(id, netConfig.id) && + return Objects.equals(id, netConfig.id) && Objects.equals(protocolLocation, netConfig.protocolLocation) && Objects.equals(registry, netConfig.registry) && Objects.equals(monitor, netConfig.monitor) && @@ -156,6 +164,6 @@ public class NetConfig { @Override public int hashCode() { - return Objects.hash(id, protocolLocation, generateJsProtocol, registry, monitor, provider, consumer); + return Objects.hash(id, protocolLocation, registry, monitor, provider, consumer); } } diff --git a/net/src/main/java/com/zfoo/net/packet/service/PacketService.java b/net/src/main/java/com/zfoo/net/packet/service/PacketService.java index 3476b8eb..6467c90e 100644 --- a/net/src/main/java/com/zfoo/net/packet/service/PacketService.java +++ b/net/src/main/java/com/zfoo/net/packet/service/PacketService.java @@ -66,7 +66,7 @@ public class PacketService implements IPacketService { public static final String NET_COMMON_MODULE = "common"; - private Predicate netGenerateProtocolFilter = registration + private final Predicate netGenerateProtocolFilter = registration -> ProtocolManager.moduleByModuleId(registration.module()).getName().matches(NET_COMMON_MODULE) || registration.protocolConstructor().getDeclaringClass().getSimpleName().endsWith(NET_REQUEST_SUFFIX) || registration.protocolConstructor().getDeclaringClass().getSimpleName().endsWith(NET_RESPONSE_SUFFIX) @@ -87,6 +87,7 @@ public class PacketService implements IPacketService { var generateJsProtocol = NetContext.getConfigManager().getLocalConfig().isGenerateJsProtocol(); var generateCsharpProtocol = NetContext.getConfigManager().getLocalConfig().isGenerateCsProtocol(); var generateLuaProtocol = NetContext.getConfigManager().getLocalConfig().isGenerateLuaProtocol(); + var generateGdProtocol = NetContext.getConfigManager().getLocalConfig().isGenerateGdProtocol(); var generateOperation = new GenerateOperation(); generateOperation.setFoldProtocol(foldProtocol); generateOperation.setProtocolParam(protocolParam); @@ -99,6 +100,9 @@ public class PacketService implements IPacketService { if (generateLuaProtocol) { generateOperation.getGenerateLanguages().add(CodeLanguage.Lua); } + if (generateGdProtocol) { + generateOperation.getGenerateLanguages().add(CodeLanguage.GdScript); + } // 设置生成协议的过滤器 GenerateProtocolFile.generateProtocolFilter = netGenerateProtocolFilter; diff --git a/net/src/main/java/com/zfoo/net/schema/NetDefinitionParser.java b/net/src/main/java/com/zfoo/net/schema/NetDefinitionParser.java index 115e01e4..93ecaf83 100644 --- a/net/src/main/java/com/zfoo/net/schema/NetDefinitionParser.java +++ b/net/src/main/java/com/zfoo/net/schema/NetDefinitionParser.java @@ -92,6 +92,7 @@ public class NetDefinitionParser implements BeanDefinitionParser { resolvePlaceholder("generate-js-protocol", "generateJsProtocol", builder, element, parserContext); resolvePlaceholder("generate-cs-protocol", "generateCsProtocol", builder, element, parserContext); resolvePlaceholder("generate-lua-protocol", "generateLuaProtocol", builder, element, parserContext); + resolvePlaceholder("generate-gd-protocol", "generateGdProtocol", builder, element, parserContext); resolvePlaceholder("fold-protocol", "foldProtocol", builder, element, parserContext); resolvePlaceholder("protocol-param", "protocolParam", builder, element, parserContext); diff --git a/net/src/main/resources/net-1.0.xsd b/net/src/main/resources/net-1.0.xsd index ba4c9912..deebf7b7 100644 --- a/net/src/main/resources/net-1.0.xsd +++ b/net/src/main/resources/net-1.0.xsd @@ -69,6 +69,7 @@ + diff --git a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java index 24c17f07..4c76da7b 100644 --- a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java +++ b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java @@ -18,6 +18,7 @@ import com.zfoo.protocol.registration.ProtocolAnalysis; import com.zfoo.protocol.registration.ProtocolRegistration; import com.zfoo.protocol.serializer.CodeLanguage; import com.zfoo.protocol.serializer.cs.GenerateCsUtils; +import com.zfoo.protocol.serializer.gd.GenerateGdUtils; import com.zfoo.protocol.serializer.js.GenerateJsUtils; import com.zfoo.protocol.serializer.lua.GenerateLuaUtils; @@ -116,6 +117,13 @@ public abstract class GenerateProtocolFile { allSortedGenerateProtocols.forEach(it -> GenerateLuaUtils.createLuaProtocolFile((ProtocolRegistration) it)); } + // 生成GdScript协议 + if (generateLanguages.contains(CodeLanguage.GdScript)) { + GenerateGdUtils.init(); + GenerateGdUtils.createProtocolManager(allSortedGenerateProtocols); + allSortedGenerateProtocols.forEach(it -> GenerateGdUtils.createGdProtocolFile((ProtocolRegistration) it)); + } + // 预留参数,以后可能会用,比如给Lua修改一个后缀名称 var protocolParam = generateOperation.getProtocolParam(); } 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 0d7e5496..53bf357b 100644 --- a/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java +++ b/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java @@ -23,6 +23,7 @@ import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.generate.GenerateProtocolPath; import com.zfoo.protocol.registration.field.*; import com.zfoo.protocol.serializer.cs.GenerateCsUtils; +import com.zfoo.protocol.serializer.gd.GenerateGdUtils; import com.zfoo.protocol.serializer.js.GenerateJsUtils; import com.zfoo.protocol.serializer.lua.GenerateLuaUtils; import com.zfoo.protocol.serializer.reflect.*; @@ -58,7 +59,7 @@ public class ProtocolAnalysis { private static Map, ISerializer> baseSerializerMap = new HashMap<>(); // 临时变量,启动完成就会销毁,协议Id对应的Class类 - private static Map> protocolClassMap = new HashMap<>(MAX_PROTOCOL_NUM); + private static final Map> protocolClassMap = new HashMap<>(MAX_PROTOCOL_NUM); // 临时变量,启动完成就会销毁,协议下包含的子协议,只包含一层子协议 private static Map> subProtocolIdMap = new HashMap<>(MAX_PROTOCOL_NUM); @@ -224,6 +225,7 @@ public class ProtocolAnalysis { GenerateCsUtils.clear(); GenerateJsUtils.clear(); GenerateLuaUtils.clear(); + GenerateGdUtils.clear(); EnhanceUtils.clear(); } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java index 6b8f42fc..6ed4a074 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java @@ -27,6 +27,8 @@ public enum CodeLanguage { Lua, - CSharp; + CSharp, + + GdScript } 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 52ee952f..27d7e385 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,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeBooleanArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteBooleanArray({});", objectStr)).append(LS); break; @@ -67,6 +70,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeBooleanArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteBooleanArray({});", objectStr)).append(LS); break; @@ -82,6 +88,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteByteArray({});", objectStr)).append(LS); break; @@ -97,6 +106,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteByteArray({});", objectStr)).append(LS); break; @@ -112,6 +124,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteShortArray({});", objectStr)).append(LS); break; @@ -127,6 +142,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteShortArray({});", objectStr)).append(LS); break; @@ -142,6 +160,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteIntArray({});", objectStr)).append(LS); break; @@ -157,6 +178,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteIntArray({});", objectStr)).append(LS); break; @@ -172,6 +196,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteLongArray({});", objectStr)).append(LS); break; @@ -187,6 +214,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteLongArray({});", objectStr)).append(LS); break; @@ -202,6 +232,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteFloatArray({});", objectStr)).append(LS); break; @@ -217,6 +250,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteFloatArray({});", objectStr)).append(LS); break; @@ -232,6 +268,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteDoubleArray({});", objectStr)).append(LS); break; @@ -247,6 +286,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteDoubleArray({});", objectStr)).append(LS); break; @@ -262,6 +304,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeStringArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeStringArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteStringArray({});", objectStr)).append(LS); break; @@ -277,6 +322,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeCharArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeCharArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteCharArray({});", objectStr)).append(LS); break; @@ -292,6 +340,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeCharArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeCharArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteCharArray({});", objectStr)).append(LS); break; @@ -309,6 +360,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writePacketArray({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, objectProtocolField.getProtocolId())).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WritePacketArray<{}>({}, {});", getArrayClassSimpleName(arrayField), objectStr, objectProtocolField.getProtocolId())).append(LS); break; @@ -340,6 +394,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readBooleanArray();", array)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", array)).append(LS); + break; default: flag = false; } @@ -352,6 +409,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readBooleanArray();", array)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", array)).append(LS); + break; default: flag = false; } @@ -364,6 +424,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readByteArray();", array)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readByteArray()", array)).append(LS); + break; default: flag = false; } @@ -376,6 +439,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readByteArray();", array)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readByteArray()", array)).append(LS); + break; default: flag = false; } @@ -388,6 +454,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readShortArray();", array)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readShortArray()", array)).append(LS); + break; default: flag = false; } @@ -400,6 +469,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readShortArray();", array)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readShortArray()", array)).append(LS); + break; default: flag = false; } @@ -412,6 +484,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readIntArray();", array)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readIntArray()", array)).append(LS); + break; default: flag = false; } @@ -424,6 +499,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readIntArray();", array)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readIntArray()", array)).append(LS); + break; default: flag = false; } @@ -436,6 +514,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readLongArray();", array)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readLongArray()", array)).append(LS); + break; default: flag = false; } @@ -448,6 +529,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readLongArray();", array)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readLongArray()", array)).append(LS); + break; default: flag = false; } @@ -460,6 +544,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readFloatArray();", array)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readFloatArray()", array)).append(LS); + break; default: flag = false; } @@ -472,6 +559,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readFloatArray();", array)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readFloatArray()", array)).append(LS); + break; default: flag = false; } @@ -484,6 +574,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readDoubleArray();", array)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", array)).append(LS); + break; default: flag = false; } @@ -496,6 +589,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readDoubleArray();", array)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", array)).append(LS); + break; default: flag = false; } @@ -508,6 +604,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readStringArray();", array)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readStringArray()", array)).append(LS); + break; default: flag = false; } @@ -520,6 +619,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readCharArray();", array)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readCharArray()", array)).append(LS); + break; default: flag = false; } @@ -532,6 +634,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readCharArray();", array)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readCharArray()", array)).append(LS); + break; default: flag = false; } @@ -544,6 +649,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readPacketArray({});", array, objectProtocolField.getProtocolId())).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", array, objectProtocolField.getProtocolId())).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadPacketArray<{}>({});", array, getArrayClassSimpleName(arrayField), objectProtocolField.getProtocolId())).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 0c938881..d06fbf8e 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java @@ -50,6 +50,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeBooleanArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteBooleanList({});", objectStr)).append(LS); break; @@ -65,6 +68,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteByteList({});", objectStr)).append(LS); break; @@ -80,6 +86,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteShortList({});", objectStr)).append(LS); break; @@ -95,6 +104,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteIntList({});", objectStr)).append(LS); break; @@ -110,6 +122,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteLongList({});", objectStr)).append(LS); break; @@ -126,6 +141,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteFloatList({});", objectStr)).append(LS); break; @@ -142,6 +160,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteDoubleList({});", objectStr)).append(LS); break; @@ -158,6 +179,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeStringArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeStringArray({})", objectStr)).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WriteStringList({});", objectStr)).append(LS); break; @@ -176,6 +200,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writePacketArray({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, objectProtocolField.getProtocolId())).append(LS); + break; case CSharp: builder.append(StringUtils.format("buffer.WritePacketList<{}>({}, {});", getListClassSimpleName(listField), objectStr, objectProtocolField.getProtocolId())).append(LS); break; @@ -205,6 +232,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readBooleanArray();", list)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", list)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadBooleanList();", list)).append(LS); break; @@ -220,6 +250,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readByteArray();", list)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readByteArray()", list)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadByteList();", list)).append(LS); break; @@ -235,6 +268,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readShortArray();", list)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readShortArray()", list)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadShortList();", list)).append(LS); break; @@ -250,6 +286,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readIntArray();", list)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readIntArray()", list)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntList();", list)).append(LS); break; @@ -265,6 +304,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readLongArray();", list)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readLongArray()", list)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongList();", list)).append(LS); break; @@ -280,6 +322,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readFloatArray();", list)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readFloatArray()", list)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadFloatList();", list)).append(LS); break; @@ -295,6 +340,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readDoubleArray();", list)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", list)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadDoubleList();", list)).append(LS); break; @@ -310,6 +358,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readStringArray();", list)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readStringArray()", list)).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringList();", list)).append(LS); break; @@ -327,6 +378,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readPacketArray({});", list, objectProtocolField.getProtocolId())).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", list, objectProtocolField.getProtocolId())).append(LS); + break; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadPacketList<{}>({});", list, getListClassSimpleName(listField), objectProtocolField.getProtocolId())).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 eb1ccdf8..dee91696 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java @@ -59,6 +59,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeIntIntMap({});", objectStr)).append(LS); return true; + case GdScript: + builder.append(StringUtils.format("buffer.writeIntIntMap({})", objectStr)).append(LS); + return true; } } else if (valueSerializer == LongSerializer.INSTANCE) { switch (language) { @@ -68,6 +71,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeIntLongMap({});", objectStr)).append(LS); return true; + case GdScript: + builder.append(StringUtils.format("buffer.writeIntLongMap({})", objectStr)).append(LS); + return true; } } else if (valueSerializer == StringSerializer.INSTANCE) { switch (language) { @@ -77,6 +83,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeIntStringMap({});", objectStr)).append(LS); return true; + case GdScript: + builder.append(StringUtils.format("buffer.writeIntStringMap({})", objectStr)).append(LS); + return true; } } else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) { switch (language) { @@ -86,6 +95,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeIntPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; + case GdScript: + builder.append(StringUtils.format("buffer.writeIntPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + return true; } } } else if (keySerializer == LongSerializer.INSTANCE) { @@ -97,6 +109,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeLongIntMap({});", objectStr)).append(LS); return true; + case GdScript: + builder.append(StringUtils.format("buffer.writeLongIntMap({})", objectStr)).append(LS); + return true; } } else if (valueSerializer == LongSerializer.INSTANCE) { switch (language) { @@ -106,6 +121,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeLongLongMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; + case GdScript: + builder.append(StringUtils.format("buffer.writeLongLongMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + return true; } } else if (valueSerializer == StringSerializer.INSTANCE) { switch (language) { @@ -115,6 +133,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeLongStringMap({});", objectStr)).append(LS); return true; + case GdScript: + builder.append(StringUtils.format("buffer.writeLongStringMap({})", objectStr)).append(LS); + return true; } } else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) { switch (language) { @@ -124,6 +145,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeLongPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; + case GdScript: + builder.append(StringUtils.format("buffer.writeLongPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + return true; } } } else if (keySerializer == StringSerializer.INSTANCE) { @@ -135,6 +159,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeStringIntMap({});", objectStr)).append(LS); return true; + case GdScript: + builder.append(StringUtils.format("buffer.writeStringIntMap({})", objectStr)).append(LS); + return true; } } else if (valueSerializer == LongSerializer.INSTANCE) { switch (language) { @@ -144,6 +171,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeStringLongMap({});", objectStr)).append(LS); return true; + case GdScript: + builder.append(StringUtils.format("buffer.writeStringLongMap({})", objectStr)).append(LS); + return true; } } else if (valueSerializer == StringSerializer.INSTANCE) { switch (language) { @@ -153,6 +183,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeStringStringMap({});", objectStr)).append(LS); return true; + case GdScript: + builder.append(StringUtils.format("buffer.writeStringStringMap({})", objectStr)).append(LS); + return true; } } else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) { switch (language) { @@ -162,6 +195,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeStringPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; + case GdScript: + builder.append(StringUtils.format("buffer.writeStringPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + return true; } } } @@ -189,6 +225,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readIntIntMap();", map)).append(LS); return map; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readIntIntMap()", map)).append(LS); + return map; } } else if (valueSerializer == LongSerializer.INSTANCE) { switch (language) { @@ -198,6 +237,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readIntLongMap();", map)).append(LS); return map; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readIntLongMap()", map)).append(LS); + return map; } } else if (valueSerializer == StringSerializer.INSTANCE) { switch (language) { @@ -207,6 +249,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readIntStringMap();", map)).append(LS); return map; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readIntStringMap()", map)).append(LS); + return map; } } else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) { switch (language) { @@ -216,6 +261,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readIntPacketMap({});", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return map; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readIntPacketMap({})", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + return map; } } } else if (keySerializer == LongSerializer.INSTANCE) { @@ -227,6 +275,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readLongIntMap();", map)).append(LS); return map; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readLongIntMap()", map)).append(LS); + return map; } } else if (valueSerializer == LongSerializer.INSTANCE) { switch (language) { @@ -236,6 +287,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readLongLongMap();", map)).append(LS); return map; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readLongLongMap()", map)).append(LS); + return map; } } else if (valueSerializer == StringSerializer.INSTANCE) { switch (language) { @@ -245,6 +299,9 @@ 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); + return map; } } else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) { switch (language) { @@ -254,6 +311,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readLongPacketMap({});", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return map; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readLongPacketMap({})", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + return map; } } } else if (keySerializer == StringSerializer.INSTANCE) { @@ -265,6 +325,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readStringIntMap();", map)).append(LS); return map; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readStringIntMap()", map)).append(LS); + return map; } } else if (valueSerializer == LongSerializer.INSTANCE) { switch (language) { @@ -274,6 +337,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readStringLongMap();", map)).append(LS); return map; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readStringLongMap()", map)).append(LS); + return map; } } else if (valueSerializer == StringSerializer.INSTANCE) { switch (language) { @@ -283,6 +349,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readStringStringMap();", map)).append(LS); return map; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readStringStringMap()", map)).append(LS); + return map; } } else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) { switch (language) { @@ -292,6 +361,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readStringPacketMap({});", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return map; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readStringPacketMap({})", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).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 5a7a5084..a90b4d19 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java @@ -50,6 +50,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeBooleanArray({})", objectStr)).append(LS); + break; default: flag = false; } @@ -62,6 +65,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS); + break; default: flag = false; } @@ -74,6 +80,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS); + break; default: flag = false; } @@ -86,6 +95,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS); + break; default: flag = false; } @@ -98,6 +110,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS); + break; default: flag = false; } @@ -110,6 +125,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS); + break; default: flag = false; } @@ -122,6 +140,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS); + break; default: flag = false; } @@ -134,6 +155,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeStringArray({});", objectStr)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writeStringArray({})", objectStr)).append(LS); + break; default: flag = false; } @@ -149,6 +173,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writePacketArray({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS); break; + case GdScript: + builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, objectProtocolField.getProtocolId())).append(LS); + break; default: flag = false; } @@ -175,6 +202,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readBooleanArray();", set)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", set)).append(LS); + break; default: flag = false; } @@ -187,6 +217,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readByteArray();", set)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readByteArray()", set)).append(LS); + break; default: flag = false; } @@ -199,6 +232,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readShortArray();", set)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readShortArray()", set)).append(LS); + break; default: flag = false; } @@ -211,6 +247,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readIntArray();", set)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readIntArray()", set)).append(LS); + break; default: flag = false; } @@ -223,6 +262,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readLongArray();", set)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readLongArray()", set)).append(LS); + break; default: flag = false; } @@ -235,6 +277,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readFloatArray();", set)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readFloatArray()", set)).append(LS); + break; default: flag = false; } @@ -247,6 +292,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readDoubleArray();", set)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", set)).append(LS); + break; default: flag = false; } @@ -259,6 +307,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readStringArray();", set)).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readStringArray()", set)).append(LS); + break; default: flag = false; } @@ -273,6 +324,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readPacketArray({});", set, objectProtocolField.getProtocolId())).append(LS); break; + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", set, objectProtocolField.getProtocolId())).append(LS); + break; default: flag = false; } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdArraySerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdArraySerializer.java new file mode 100644 index 00000000..81927bc4 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdArraySerializer.java @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.gd; + +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; + +import static com.zfoo.protocol.util.FileUtils.LS; + + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class GdArraySerializer implements IGdSerializer { + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + if (CutDownArraySerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.GdScript)) { + return; + } + + ArrayField arrayField = (ArrayField) fieldRegistration; + + builder.append(StringUtils.format("if ({} == null):", objectStr)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("buffer.writeInt(0)").append(LS); + GenerateProtocolFile.addTab(builder, deep); + + builder.append("else:").append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("buffer.writeInt({}.size())", objectStr)).append(LS); + + String element = "element" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("for {} in {}:", element, objectStr)).append(LS); + GenerateGdUtils.gdSerializer(arrayField.getArrayElementRegistration().serializer()) + .writeObject(builder, element, deep + 2, field, arrayField.getArrayElementRegistration()); + } + + @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.GdScript); + if (cutDown != null) { + return cutDown; + } + + ArrayField arrayField = (ArrayField) fieldRegistration; + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + builder.append(StringUtils.format("var {} = []", result)).append(LS); + + String i = "index" + GenerateProtocolFile.index.getAndIncrement(); + String size = "size" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.readInt()", size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("if ({} > 0):", size)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("for {} in range({}):", i, size)).append(LS); + String readObject = GenerateGdUtils.gdSerializer(arrayField.getArrayElementRegistration().serializer()) + .readObject(builder, deep + 2, field, arrayField.getArrayElementRegistration()); + GenerateProtocolFile.addTab(builder, deep + 2); + builder.append(StringUtils.format("{}.append({})", result, readObject)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdBooleanSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdBooleanSerializer.java new file mode 100644 index 00000000..ffe74a53 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdBooleanSerializer.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.gd; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class GdBooleanSerializer implements IGdSerializer { + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.writeBool({})", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.readBool() ", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdByteSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdByteSerializer.java new file mode 100644 index 00000000..36e198cc --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdByteSerializer.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.gd; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class GdByteSerializer implements IGdSerializer { + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.writeByte({})", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.readByte()", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdCharSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdCharSerializer.java new file mode 100644 index 00000000..ce2565c2 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdCharSerializer.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.gd; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class GdCharSerializer implements IGdSerializer { + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.writeChar({})", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.readChar()", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdDoubleSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdDoubleSerializer.java new file mode 100644 index 00000000..8a33f4dd --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdDoubleSerializer.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.gd; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class GdDoubleSerializer implements IGdSerializer { + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.writeDouble({})", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.readDouble()", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdFloatSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdFloatSerializer.java new file mode 100644 index 00000000..d9af429e --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdFloatSerializer.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.gd; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class GdFloatSerializer implements IGdSerializer { + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.writeFloat({})", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.readFloat()", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdIntSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdIntSerializer.java new file mode 100644 index 00000000..324189ef --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdIntSerializer.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.gd; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class GdIntSerializer implements IGdSerializer { + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.writeInt({})", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.readInt()", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdListSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdListSerializer.java new file mode 100644 index 00000000..247045c3 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdListSerializer.java @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.gd; + +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; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class GdListSerializer implements IGdSerializer { + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + if (CutDownListSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.GdScript)) { + return; + } + + ListField listField = (ListField) fieldRegistration; + + builder.append(StringUtils.format("if ({} == null):", objectStr)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("buffer.writeInt(0)").append(LS); + GenerateProtocolFile.addTab(builder, deep); + + builder.append("else:").append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("buffer.writeInt({}.size())", objectStr)).append(LS); + + String element = "element" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("for {} in {}:", element, objectStr)).append(LS); + GenerateGdUtils.gdSerializer(listField.getListElementRegistration().serializer()) + .writeObject(builder, element, deep + 2, field, listField.getListElementRegistration()); + } + + @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.GdScript); + if (cutDown != null) { + return cutDown; + } + + ListField listField = (ListField) fieldRegistration; + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + builder.append(StringUtils.format("var {} = []", result)).append(LS); + + String i = "index" + GenerateProtocolFile.index.getAndIncrement(); + String size = "size" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.readInt()", size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("if ({} > 0):", size)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("for {} in range({}):", i, size)).append(LS); + String readObject = GenerateGdUtils.gdSerializer(listField.getListElementRegistration().serializer()) + .readObject(builder, deep + 2, field, listField.getListElementRegistration()); + GenerateProtocolFile.addTab(builder, deep + 2); + builder.append(StringUtils.format("{}.append({})", result, readObject)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdLongSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdLongSerializer.java new file mode 100644 index 00000000..b8aff23b --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdLongSerializer.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.gd; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class GdLongSerializer implements IGdSerializer { + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.writeLong({})", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.readLong()", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdMapSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdMapSerializer.java new file mode 100644 index 00000000..ba82f0b9 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdMapSerializer.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.gd; + +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; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class GdMapSerializer implements IGdSerializer { + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + if (CutDownMapSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.GdScript)) { + return; + } + + MapField mapField = (MapField) fieldRegistration; + builder.append(StringUtils.format("if ({} == null):", objectStr)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("buffer.writeInt(0)").append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append("else:").append(LS); + + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("buffer.writeInt({}.size())", objectStr)).append(LS); + + String key = "key" + GenerateProtocolFile.index.getAndIncrement(); + String value = "value" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("for {} in {}:", key, objectStr)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 2); + builder.append(StringUtils.format("var {} = {}[{}]", value, objectStr, key)).append(LS); + GenerateGdUtils.gdSerializer(mapField.getMapKeyRegistration().serializer()) + .writeObject(builder, key, deep + 2, field, mapField.getMapKeyRegistration()); + GenerateGdUtils.gdSerializer(mapField.getMapValueRegistration().serializer()) + .writeObject(builder, value, deep + 2, field, mapField.getMapValueRegistration()); + } + + @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.GdScript); + if (cutDown != null) { + return cutDown; + } + + MapField mapField = (MapField) fieldRegistration; + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + builder.append(StringUtils.format("var {} = {}", result)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + String size = "size" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("var {} = buffer.readInt()", size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("if ({} > 0):", size)).append(LS); + + String i = "index" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("for {} in range({}):", i, size)).append(LS); + + String keyObject = GenerateGdUtils.gdSerializer(mapField.getMapKeyRegistration().serializer()) + .readObject(builder, deep + 2, field, mapField.getMapKeyRegistration()); + + + String valueObject = GenerateGdUtils.gdSerializer(mapField.getMapValueRegistration().serializer()) + .readObject(builder, deep + 2, field, mapField.getMapValueRegistration()); + GenerateProtocolFile.addTab(builder, deep + 2); + + builder.append(StringUtils.format("{}[{}] = {}", result, keyObject, valueObject)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdObjectProtocolSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdObjectProtocolSerializer.java new file mode 100644 index 00000000..a6e8e610 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdObjectProtocolSerializer.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.gd; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.registration.field.ObjectProtocolField; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class GdObjectProtocolSerializer implements IGdSerializer { + + @Override + 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("buffer.writePacket({}, {})", objectStr, objectProtocolField.getProtocolId())).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration; + var result = "result" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.readPacket({})", result, objectProtocolField.getProtocolId())).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdSetSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdSetSerializer.java new file mode 100644 index 00000000..a286e171 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdSetSerializer.java @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.gd; + +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; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class GdSetSerializer implements IGdSerializer { + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + if (CutDownSetSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.GdScript)) { + return; + } + + SetField setField = (SetField) fieldRegistration; + + builder.append(StringUtils.format("if ({} == null):", objectStr)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("buffer.writeInt(0)").append(LS); + GenerateProtocolFile.addTab(builder, deep); + + builder.append("else:").append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("buffer.writeInt({}.size())", objectStr)).append(LS); + + String element = "element" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("for {} in {}:", element, objectStr)).append(LS); + GenerateGdUtils.gdSerializer(setField.getSetElementRegistration().serializer()) + .writeObject(builder, element, deep + 2, field, setField.getSetElementRegistration()); + } + + @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.GdScript); + if (cutDown != null) { + return cutDown; + } + + SetField setField = (SetField) fieldRegistration; + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + builder.append(StringUtils.format("var {} = []", result)).append(LS); + + String i = "index" + GenerateProtocolFile.index.getAndIncrement(); + String size = "size" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.readInt()", size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("if ({} > 0):", size)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("for {} in range({}):", i, size)).append(LS); + String readObject = GenerateGdUtils.gdSerializer(setField.getSetElementRegistration().serializer()) + .readObject(builder, deep + 2, field, setField.getSetElementRegistration()); + GenerateProtocolFile.addTab(builder, deep + 2); + builder.append(StringUtils.format("{}.append({})", result, readObject)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdShortSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdShortSerializer.java new file mode 100644 index 00000000..a410f409 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdShortSerializer.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.gd; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class GdShortSerializer implements IGdSerializer { + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.writeShort({})", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.readShort()", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdStringSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdStringSerializer.java new file mode 100644 index 00000000..864068b7 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GdStringSerializer.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.gd; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class GdStringSerializer implements IGdSerializer { + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.writeString({})", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.readString()", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GenerateGdUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GenerateGdUtils.java new file mode 100644 index 00000000..cddade87 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/GenerateGdUtils.java @@ -0,0 +1,233 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.gd; + +import com.zfoo.protocol.generate.GenerateProtocolDocument; +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.generate.GenerateProtocolPath; +import com.zfoo.protocol.registration.IProtocolRegistration; +import com.zfoo.protocol.registration.ProtocolRegistration; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.serializer.reflect.*; +import com.zfoo.protocol.util.ClassUtils; +import com.zfoo.protocol.util.FileUtils; +import com.zfoo.protocol.util.IOUtils; +import com.zfoo.protocol.util.StringUtils; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.*; + +import static com.zfoo.protocol.util.FileUtils.LS; +import static com.zfoo.protocol.util.StringUtils.TAB; + +/** + * @author jaysunxiao + * @version 3.0 + */ +public abstract class GenerateGdUtils { + + private static final String PROTOCOL_OUTPUT_ROOT_PATH = "gdProtocol/"; + + private static Map gdSerializerMap; + + public static IGdSerializer gdSerializer(ISerializer serializer) { + return gdSerializerMap.get(serializer); + } + + public static void init() { + FileUtils.deleteFile(new File(PROTOCOL_OUTPUT_ROOT_PATH)); + FileUtils.createDirectory(PROTOCOL_OUTPUT_ROOT_PATH); + + gdSerializerMap = new HashMap<>(); + gdSerializerMap.put(BooleanSerializer.INSTANCE, new GdBooleanSerializer()); + gdSerializerMap.put(ByteSerializer.INSTANCE, new GdByteSerializer()); + gdSerializerMap.put(ShortSerializer.INSTANCE, new GdShortSerializer()); + gdSerializerMap.put(IntSerializer.INSTANCE, new GdIntSerializer()); + gdSerializerMap.put(LongSerializer.INSTANCE, new GdLongSerializer()); + gdSerializerMap.put(FloatSerializer.INSTANCE, new GdFloatSerializer()); + gdSerializerMap.put(DoubleSerializer.INSTANCE, new GdDoubleSerializer()); + gdSerializerMap.put(CharSerializer.INSTANCE, new GdCharSerializer()); + gdSerializerMap.put(StringSerializer.INSTANCE, new GdStringSerializer()); + gdSerializerMap.put(ArraySerializer.INSTANCE, new GdArraySerializer()); + gdSerializerMap.put(ListSerializer.INSTANCE, new GdListSerializer()); + gdSerializerMap.put(SetSerializer.INSTANCE, new GdSetSerializer()); + gdSerializerMap.put(MapSerializer.INSTANCE, new GdMapSerializer()); + gdSerializerMap.put(ObjectProtocolSerializer.INSTANCE, new GdObjectProtocolSerializer()); + } + + public static void clear() { + gdSerializerMap = null; + } + + public static void createProtocolManager(List protocolList) throws IOException { + var list = List.of("gd/buffer/ByteBuffer.gd"); + + for (var fileName : list) { + var fileInputStream = ClassUtils.getFileFromClassPath(fileName); + var createFile = new File(StringUtils.format("{}{}", PROTOCOL_OUTPUT_ROOT_PATH, StringUtils.substringAfterFirst(fileName, "gd/"))); + FileUtils.writeInputStreamToFile(createFile, fileInputStream); + } + + + // 生成ProtocolManager.gd文件 + var gdBuilder = new StringBuilder(); + + protocolList.stream() + .filter(it -> Objects.nonNull(it)) + .forEach(it -> { + var name = it.protocolConstructor().getDeclaringClass().getSimpleName(); + var path = GenerateProtocolPath.getProtocolPath(it.protocolId()); + gdBuilder.append(StringUtils.format("const {} = preload(\"res://gdProtocol/{}/{}.gd\")", name, path, name)).append(LS); + }); + + + gdBuilder.append(LS); + + var protocolManagerStr = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("gd/ProtocolManager.gd"))); + gdBuilder.append(protocolManagerStr); + + gdBuilder.append("static func initProtocol():").append(LS); + protocolList.stream() + .filter(it -> Objects.nonNull(it)) + .forEach(it -> gdBuilder.append(TAB).append(StringUtils.format("protocols[{}] = {}", it.protocolId(), it.protocolConstructor().getDeclaringClass().getSimpleName())).append(LS)); + + FileUtils.writeStringToFile(new File(StringUtils.format("{}{}", PROTOCOL_OUTPUT_ROOT_PATH, "ProtocolManager.gd")), gdBuilder.toString()); + } + + public static void createGdProtocolFile(ProtocolRegistration registration) { + // 初始化index + GenerateProtocolFile.index.set(0); + + var protocolId = registration.protocolId(); + var registrationConstructor = registration.getConstructor(); + + var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName(); + + var gdBuilder = new StringBuilder(); + + // 协议的属性生成 + gdBuilder.append(protocolClass(registration)); + + // protocolId + gdBuilder.append(protocolIdField(registration)); + + // writeObject method + gdBuilder.append(writeObject(registration)); + + // readObject method + gdBuilder.append(readObject(registration)); + + var protocolOutputPath = StringUtils.format("{}{}/{}.gd" + , PROTOCOL_OUTPUT_ROOT_PATH + , GenerateProtocolPath.getProtocolPath(protocolId) + , protocolClazzName); + FileUtils.writeStringToFile(new File(protocolOutputPath), gdBuilder.toString()); + } + + private static String protocolClass(ProtocolRegistration registration) { + var protocolId = registration.getId(); + var fields = registration.getFields(); + var protocolClazzName = registration.getConstructor().getDeclaringClass().getSimpleName(); + + var protocolDocument = GenerateProtocolDocument.getProtocolDocument(protocolId); + var docTitle = protocolDocument.getKey(); + var docFieldMap = protocolDocument.getValue(); + + var gdBuilder = new StringBuilder(); + + if (!StringUtils.isBlank(docTitle)) { + gdBuilder.append(gdDocument(docTitle)).append(LS); + } + + for (var field : fields) { + var propertyName = field.getName(); + + // 生成注释 + var doc = docFieldMap.get(propertyName); + if (!StringUtils.isBlank(doc)) { + Arrays.stream(doc.split(LS)).forEach(it -> gdBuilder.append(gdDocument(it)).append(LS)); + } + + gdBuilder.append(StringUtils.format("var {}", propertyName)) + .append(" # ").append(field.getGenericType().getTypeName()) // 生成类型的注释 + .append(LS); + } + + gdBuilder.append(LS); + return gdBuilder.toString(); + } + + private static String protocolIdField(ProtocolRegistration registration) { + var protocolId = registration.getId(); + var gdBuilder = new StringBuilder(); + gdBuilder.append(StringUtils.format("const PROTOCOL_ID = {}", protocolId)).append(LS).append(LS); + return gdBuilder.toString(); + } + + + private static String writeObject(ProtocolRegistration registration) { + var fields = registration.getFields(); + var fieldRegistrations = registration.getFieldRegistrations(); + + var gdBuilder = new StringBuilder(); + gdBuilder.append(StringUtils.format("static func write(buffer, packet):")).append(LS); + + gdBuilder.append(TAB).append("if (buffer.writePacketFlag(packet)):").append(LS); + gdBuilder.append(TAB + TAB).append("return").append(LS); + + for (int i = 0; i < fields.length; i++) { + Field field = fields[i]; + IFieldRegistration fieldRegistration = fieldRegistrations[i]; + + gdSerializer(fieldRegistration.serializer()).writeObject(gdBuilder, "packet." + field.getName(), 1, field, fieldRegistration); + } + + gdBuilder.append(LS).append(LS); + return gdBuilder.toString(); + } + + + private static String readObject(ProtocolRegistration registration) { + var fields = registration.getFields(); + var fieldRegistrations = registration.getFieldRegistrations(); + var protocolClazzName = registration.getConstructor().getDeclaringClass().getSimpleName(); + + var jsBuilder = new StringBuilder(); + jsBuilder.append(StringUtils.format("static func read(buffer):")).append(LS); + jsBuilder.append(TAB).append("if (!buffer.readBool()):").append(LS); + jsBuilder.append(TAB + TAB).append("return null").append(LS); + + + jsBuilder.append(TAB).append(StringUtils.format("var packet = buffer.newInstance({})", registration.protocolId())).append(LS); + + + for (var i = 0; i < fields.length; i++) { + var field = fields[i]; + var fieldRegistration = fieldRegistrations[i]; + + var readObject = gdSerializer(fieldRegistration.serializer()).readObject(jsBuilder, 1, field, fieldRegistration); + jsBuilder.append(TAB).append(StringUtils.format("packet.{} = {}", field.getName(), readObject)).append(LS); + } + + jsBuilder.append(TAB).append("return packet").append(LS); + return jsBuilder.toString(); + } + + private static String gdDocument(String doc) { + return doc.replaceAll("//", "#"); + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/gd/IGdSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/IGdSerializer.java new file mode 100644 index 00000000..abea42e5 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/gd/IGdSerializer.java @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.gd; + +import com.zfoo.protocol.registration.field.IFieldRegistration; + +import java.lang.reflect.Field; + +/** + * @author jaysunxiao + * @version 3.0 + */ +public interface IGdSerializer { + + void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration); + + String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration); + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/js/GenerateJsUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/js/GenerateJsUtils.java index 82d21a29..900d1bdc 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/js/GenerateJsUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/js/GenerateJsUtils.java @@ -13,12 +13,10 @@ package com.zfoo.protocol.serializer.js; -import com.zfoo.protocol.collection.CollectionUtils; import com.zfoo.protocol.generate.GenerateProtocolDocument; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.generate.GenerateProtocolPath; import com.zfoo.protocol.registration.IProtocolRegistration; -import com.zfoo.protocol.registration.ProtocolAnalysis; import com.zfoo.protocol.registration.ProtocolRegistration; import com.zfoo.protocol.registration.field.IFieldRegistration; import com.zfoo.protocol.serializer.reflect.*; @@ -182,8 +180,7 @@ public abstract class GenerateJsUtils { jsBuilder.append(TAB) .append(StringUtils.format("this.{} = {};", propertyName, propertyName)) - // 生成类型的注释 - .append(" // ").append(field.getGenericType().getTypeName()) + .append(" // ").append(field.getGenericType().getTypeName())// 生成类型的注释 .append(LS); } diff --git a/protocol/src/main/resources/gd/ProtocolManager.gd b/protocol/src/main/resources/gd/ProtocolManager.gd new file mode 100644 index 00000000..1f55472f --- /dev/null +++ b/protocol/src/main/resources/gd/ProtocolManager.gd @@ -0,0 +1,21 @@ +const protocols = {} + +static func getProtocol(protocolId: int): + return protocols[protocolId] + +static func newInstance(protocolId: int): + var protocol = protocols[protocolId] + return protocol.new() + +static func write(buffer, packet): + var protocolId: int = packet.PROTOCOL_ID + buffer.writeShort(protocolId) + var protocol = protocols[protocolId] + protocol.write(buffer, packet) + +static func read(buffer): + var protocolId = buffer.readShort(); + var protocol = protocols[protocolId] + var packet = protocol.read(buffer); + return packet; + diff --git a/protocol/src/main/resources/gd/buffer/ByteBuffer.gd b/protocol/src/main/resources/gd/buffer/ByteBuffer.gd new file mode 100644 index 00000000..8186a2af --- /dev/null +++ b/protocol/src/main/resources/gd/buffer/ByteBuffer.gd @@ -0,0 +1,661 @@ +const ProtocolManager = preload("res://gdProtocol/ProtocolManager.gd") + +const EMPTY: String = "" + +var buffer = StreamPeerBuffer.new() + +var writeOffset: int = 0 setget setWriteOffset, getWriteOffset +var readOffset: int = 0 setget setReadOffset, getReadOffset + +func _init(): + buffer.big_endian = true + +# -------------------------------------------------get/set------------------------------------------------- +func setWriteOffset(writeIndex: int) -> void: + if (writeIndex > buffer.get_size()): + var template = "writeIndex[{}] out of bounds exception: readerIndex: {}, writerIndex: {} (expected: 0 <= readerIndex <= writerIndex <= capacity: {})" + printerr(template.format([writeIndex, readOffset, writeOffset, buffer.size()], "{}")) + return + writeOffset = writeIndex + +func getWriteOffset() -> int: + return writeOffset + +func setReadOffset(readIndex: int) -> void: + if (readIndex > writeOffset): + var template = "readIndex[{}] out of bounds exception: readerIndex: {}, writerIndex: {} (expected: 0 <= readerIndex <= writerIndex <= capacity: {})" + printerr(template.format([readIndex, readOffset, writeOffset, buffer.size()], "{}")) + return + readOffset = readIndex + +func getReadOffset() -> int: + return readOffset + +# -------------------------------------------------write/read------------------------------------------------- +func writePoolByteArray(value: PoolByteArray): + var length = value.size() + buffer.put_partial_data(value) + writeOffset += length + +func writeBool(value: bool) -> void: + var byte = 0 + if (value): + byte = 1 + buffer.seek(writeOffset) + buffer.put_8(byte) + writeOffset += 1 + +func readBool() -> bool: + buffer.seek(readOffset) + var byte = buffer.get_8() + readOffset += 1 + return byte == 1 + +func writeByte(value: int) -> void: + buffer.seek(writeOffset) + buffer.put_8(value) + writeOffset += 1 + +func readByte() -> int: + buffer.seek(readOffset) + var value = buffer.get_8() + readOffset += 1 + return value + +func writeShort(value: int) -> void: + buffer.seek(writeOffset) + buffer.put_16(value) + writeOffset += 2 + +func readShort() -> int: + buffer.seek(readOffset) + var value = buffer.get_16() + readOffset += 2 + return value + +func writeInt(value) -> void: + writeLong(value) + +func readInt() -> int: + return readLong() + +func writeLong(longValue: int) -> void: + var value = (longValue << 1) ^ (longValue >> 63) + + if (value >> 7 == 0): + writeByte(value) + return + + if (value >> 14 == 0): + writeByte(value | 0x80) + writeByte(value >> 7) + return + + if (value >> 21 == 0): + writeByte(value | 0x80) + writeByte((value >> 7) | 0x80) + writeByte(value >> 14) + return + + if (value >> 28 == 0): + writeByte(value | 0x80) + writeByte((value >> 7) | 0x80) + writeByte((value >> 14) | 0x80) + writeByte(value >> 21) + return + + if (value >> 35 == 0): + writeByte(value | 0x80) + writeByte((value >> 7) | 0x80) + writeByte((value >> 14) | 0x80) + writeByte((value >> 21) | 0x80) + writeByte(value >> 28) + return + + if (value >> 42 == 0): + writeByte(value | 0x80) + writeByte((value >> 7) | 0x80) + writeByte((value >> 14) | 0x80) + writeByte((value >> 21) | 0x80) + writeByte((value >> 28) | 0x80) + writeByte(value >> 35) + return + + if (value >> 49 == 0): + writeByte(value | 0x80) + writeByte((value >> 7) | 0x80) + writeByte((value >> 14) | 0x80) + writeByte((value >> 21) | 0x80) + writeByte((value >> 28) | 0x80) + writeByte((value >> 35) | 0x80) + writeByte(value >> 42) + return + + if (value >> 56 == 0): + writeByte(value | 0x80) + writeByte((value >> 7) | 0x80) + writeByte((value >> 14) | 0x80) + writeByte((value >> 21) | 0x80) + writeByte((value >> 28) | 0x80) + writeByte((value >> 35) | 0x80) + writeByte((value >> 42) | 0x80) + writeByte(value >> 49) + return + + writeByte(value | 0x80) + writeByte((value >> 7) | 0x80) + writeByte((value >> 14) | 0x80) + writeByte((value >> 21) | 0x80) + writeByte((value >> 28) | 0x80) + writeByte((value >> 35) | 0x80) + writeByte((value >> 42) | 0x80) + writeByte((value >> 49) | 0x80) + writeByte(value >> 56) + +func readLong() -> int: + var byte: int = readByte() + var value: int = byte + if (byte < 0): + byte = readByte() + value = value & 0x00000000_0000007F | byte << 7 + if (byte < 0): + byte = readByte() + value = value & 0x00000000_00003FFF | byte << 14 + if (byte < 0): + byte = readByte() + value = value & 0x00000000_001FFFFF | byte << 21 + if (byte < 0): + byte = readByte() + value = value & 0x00000000_0FFFFFFF | byte << 28 + if (byte < 0): + byte = readByte() + value = value & 0x00000007_FFFFFFFF | byte << 35 + if (byte < 0): + byte = readByte() + value = value & 0x000003FF_FFFFFFFF | byte << 42 + if (byte < 0): + byte = readByte() + value = value & 0x0001FFFF_FFFFFFFF | byte << 49 + if (byte < 0): + byte = readByte() + value = value & 0x00FFFFFF_FFFFFFFF | byte << 56 + + var mask = value >> 1 + if (mask < 0): + mask = mask & 0x7FFFFFFF_FFFFFFFF + return mask ^ -(value & 1) + + +func writeFloat(value: float) -> void: + buffer.seek(writeOffset) + buffer.put_float(value) + writeOffset += 4 + +func readFloat() -> float: + buffer.seek(readOffset) + var value = buffer.get_float() + readOffset += 4 + return value + +func writeDouble(value: float) -> void: + buffer.seek(writeOffset) + buffer.put_double(value) + writeOffset += 8 + +func readDouble() -> float: + buffer.seek(readOffset) + var value = buffer.get_double() + readOffset += 8 + return value + + +func writeString(value: String) -> void: + if (value == null || value.length() ==0): + writeInt(0) + return + + buffer.seek(writeOffset) + + var strBytes = value.to_utf8() + var length = strBytes.size() + writeInt(length) + buffer.put_partial_data(strBytes) + writeOffset += length + +func readString() -> String: + var length = readInt() + if (length <= 0): + return EMPTY + + buffer.seek(readOffset) + var value = buffer.get_utf8_string(length) + var strBytes = value.to_utf8() + readOffset += length + return value + +func writeChar(value) -> void: + if (value == null || value.length() == 0): + writeString(EMPTY) + return + writeString(value[0]) + +func readChar() -> String: + return readString() + +func writePacketFlag(packet) -> bool: + var flag = (packet == null) + writeBool(!flag) + return flag + +func writePacket(packet, protocolId): + var protocolRegistration = ProtocolManager.getProtocol(protocolId) + protocolRegistration.write(self, packet) + +func readPacket(protocolId): + var protocolRegistration = ProtocolManager.getProtocol(protocolId) + return protocolRegistration.read(self) + +func newInstance(protocolId: int): + return ProtocolManager.newInstance(protocolId) + +func writeBooleanArray(value): + if (value == null): + writeInt(0) + else: + writeInt(value.size()); + for element in value: + writeBool(element) + +func readBooleanArray(): + var array = [] + var size = readInt() + if (size > 0): + for index in range(size): + array.append(readBool()) + return array + +func writeByteArray(value): + if (value == null): + writeInt(0) + else: + writeInt(value.size()); + for element in value: + writeByte(element) + +func readByteArray(): + var array = [] + var size = readInt() + if (size > 0): + for index in range(size): + array.append(readByte()) + return array + +func writeShortArray(value): + if (value == null): + writeInt(0) + else: + writeInt(value.size()); + for element in value: + writeShort(element) + +func readShortArray(): + var array = [] + var size = readInt() + if (size > 0): + for index in range(size): + array.append(readShort()) + return array + +func writeIntArray(value): + if (value == null): + writeInt(0) + else: + writeInt(value.size()); + for element in value: + writeInt(element) + +func readIntArray(): + var array = [] + var size = readInt() + if (size > 0): + for index in range(size): + array.append(readInt()) + return array + +func writeLongArray(value): + if (value == null): + writeInt(0) + else: + writeInt(value.size()); + for element in value: + writeLong(element) + +func readLongArray(): + var array = [] + var size = readInt() + if (size > 0): + for index in range(size): + array.append(readLong()) + return array + +func writeFloatArray(value): + if (value == null): + writeInt(0) + else: + writeInt(value.size()); + for element in value: + writeFloat(element) + +func readFloatArray(): + var array = [] + var size = readInt() + if (size > 0): + for index in range(size): + array.append(readFloat()) + return array + +func writeDoubleArray(value): + if (value == null): + writeInt(0) + else: + writeInt(value.size()); + for element in value: + writeDouble(element) + +func readDoubleArray(): + var array = [] + var size = readInt() + if (size > 0): + for index in range(size): + array.append(readDouble()) + return array + +func writeCharArray(value): + if (value == null): + writeInt(0) + else: + writeInt(value.size()); + for element in value: + writeChar(element) + +func readCharArray(): + var array = [] + var size = readInt() + if (size > 0): + for index in range(size): + array.append(readChar()) + return array + +func writeStringArray(value): + if (value == null): + writeInt(0) + else: + writeInt(value.size()); + for element in value: + writeString(element) + +func readStringArray(): + var array = [] + var size = readInt() + if (size > 0): + for index in range(size): + array.append(readString()) + return array + + +func writePacketArray(value, protocolId): + if (value == null): + writeInt(0) + else: + var protocolRegistration = ProtocolManager.getProtocol(protocolId) + writeInt(value.size()); + for element in value: + protocolRegistration.write(self, element) + +func readPacketArray(protocolId): + var array = [] + var size = readInt() + if (size > 0): + var protocolRegistration = ProtocolManager.getProtocol(protocolId) + for index in range(size): + array.append(protocolRegistration.read(self)) + return array + +func writeIntIntMap(value): + if (value == null): + writeInt(0) + else: + writeInt(value.size()) + for key in value: + writeInt(key) + writeInt(value[key]) + +func readIntIntMap(): + var map = {} + var size = readInt() + if (size > 0): + for index in range(size): + var key = readInt() + var value = readInt() + map[key] = value + return map + +func writeIntLongMap(value): + if (value == null): + writeInt(0) + else: + writeInt(value.size()) + for key in value: + writeInt(key) + writeLong(value[key]) + +func readIntLongMap(): + var map = {} + var size = readInt() + if (size > 0): + for index in range(size): + var key = readInt() + var value = readLong() + map[key] = value + return map + +func writeIntStringMap(value): + if (value == null): + writeInt(0) + else: + writeInt(value.size()) + for key in value: + writeInt(key) + writeString(value[key]) + +func readIntStringMap(): + var map = {} + var size = readInt() + if (size > 0): + for index in range(size): + var key = readInt() + var value = readString() + map[key] = value + return map + + +func writeIntPacketMap(value, protocolId): + if (value == null): + writeInt(0) + else: + var protocolRegistration = ProtocolManager.getProtocol(protocolId) + writeInt(value.size()) + for key in value: + writeInt(key) + protocolRegistration.write(self, value[key]) + +func readIntPacketMap(protocolId): + var map = {} + var size = readInt() + if (size > 0): + var protocolRegistration = ProtocolManager.getProtocol(protocolId) + for index in range(size): + var key = readInt() + var value = protocolRegistration.read(self) + map[key] = value + return map + + +func writeLongIntMap(value): + if (value == null): + writeInt(0) + else: + writeInt(value.size()) + for key in value: + writeLong(key) + writeInt(value[key]) + +func readLongIntMap(): + var map = {} + var size = readInt() + if (size > 0): + for index in range(size): + var key = readLong() + var value = readInt() + map[key] = value + return map + +func writeLongLongMap(value): + if (value == null): + writeInt(0) + else: + writeInt(value.size()) + for key in value: + writeLong(key) + writeLong(value[key]) + +func readLongLongMap(): + var map = {} + var size = readInt() + if (size > 0): + for index in range(size): + var key = readLong() + var value = readLong() + map[key] = value + return map + +func writeLongStringMap(value): + if (value == null): + writeInt(0) + else: + writeInt(value.size()) + for key in value: + writeLong(key) + writeString(value[key]) + +func readLongStringMap(): + var map = {} + var size = readInt() + if (size > 0): + for index in range(size): + var key = readLong() + var value = readString() + map[key] = value + return map + + +func writeLongPacketMap(value, protocolId): + if (value == null): + writeInt(0) + else: + var protocolRegistration = ProtocolManager.getProtocol(protocolId) + writeInt(value.size()) + for key in value: + writeLong(key) + protocolRegistration.write(self, value[key]) + +func readLongPacketMap(protocolId): + var map = {} + var size = readInt() + if (size > 0): + var protocolRegistration = ProtocolManager.getProtocol(protocolId) + for index in range(size): + var key = readLong() + var value = protocolRegistration.read(self) + map[key] = value + return map + + +func writeStringIntMap(value): + if (value == null): + writeInt(0) + else: + writeInt(value.size()) + for key in value: + writeString(key) + writeInt(value[key]) + +func readStringIntMap(): + var map = {} + var size = readInt() + if (size > 0): + for index in range(size): + var key = readString() + var value = readInt() + map[key] = value + return map + +func writeStringLongMap(value): + if (value == null): + writeInt(0) + else: + writeInt(value.size()) + for key in value: + writeString(key) + writeLong(value[key]) + +func readStringLongMap(): + var map = {} + var size = readInt() + if (size > 0): + for index in range(size): + var key = readString() + var value = readLong() + map[key] = value + return map + +func writeStringStringMap(value): + if (value == null): + writeInt(0) + else: + writeInt(value.size()) + for key in value: + writeString(key) + writeString(value[key]) + +func readStringStringMap(): + var map = {} + var size = readInt() + if (size > 0): + for index in range(size): + var key = readString() + var value = readString() + map[key] = value + return map + + +func writeStringPacketMap(value, protocolId): + if (value == null): + writeInt(0) + else: + var protocolRegistration = ProtocolManager.getProtocol(protocolId) + writeInt(value.size()) + for key in value: + writeString(key) + protocolRegistration.write(self, value[key]) + +func readStringPacketMap(protocolId): + var map = {} + var size = readInt() + if (size > 0): + var protocolRegistration = ProtocolManager.getProtocol(protocolId) + for index in range(size): + var key = readString() + var value = protocolRegistration.read(self) + map[key] = value + return map