This commit is contained in:
meiwei
2023-08-28 15:45:56 +08:00
65 changed files with 18766 additions and 150 deletions
+1
View File
@@ -37,6 +37,7 @@ test.md
# 忽略生成的协议文件
**/jsProtocol/
**/gdProtocol/
**/pyProtocol/
**/CsProtocol/
**/LuaProtocol/
**/cppProtocol/
+2 -2
View File
@@ -5,7 +5,7 @@ English | [简体中文](./README_CN.md)
Why is zfoo protocol ?
-----------
- The protocol currently supports C++ C# Go Java Javascript TypeScript GDScript Lua
- The protocol currently supports C++ C# Go Java Javascript TypeScript Lua GDScript Python
- Universal distributed server protocol implementation, there are implementations in other languages, which can easily
implement RPC, servers, and microservices on other platforms
- The fastest custom format framework for Java binary serialization, with the fewest serialized bytes, more secure and
@@ -63,7 +63,7 @@ Perfect work development process, complete online solution
**JDK 11 or 17****OpenJDK** or **Oracle JDK** maven install to local repository
```
If you do not have JDK 11+ installed, the quick installation method is to download directly
If you do not have JDK installed, the quick installation method is to download directly
from Project Structure, Platform Settings, SDKs in the upper right corner of Idea
```
+2 -2
View File
@@ -5,7 +5,7 @@
Why is zfoo protocol ?
-----------
- 协议原生支持 C++ C# Java Go Javascript TypeScript GDScript Lua
- 协议原生支持 C++ C# Java Go Javascript TypeScript Lua GDScript Python
- 通用的分布式服务器协议实现,有其它语言的实现,可以轻易实现其它平台的的RPC,微服务
- 二进制序列化速度最快的自定义格式框架,并且为序列化字节最少的框架,更加安全,可前后兼容
- 单线程的Benchmark测试中,序列化和反序列化速度比Protobuf快50%,比Kryo快100%
@@ -58,7 +58,7 @@ Why is zfoo protocol ?
**JDK 11 或者 JDK 17**,可以在 **OpenJDK****Oracle JDK** 无缝切换,下载完本项目,maven install到本地仓库即可使用
```
如果你没有安装JDK 11+,快速的安装方法是在Idea的右上角Project StructurePlatform SettingsSDKs中直接下载
如果你没有安装JDK,快速的安装方法是在Idea的右上角Project StructurePlatform SettingsSDKs中直接下载
```
#### 2. [protocol](protocol/README.md) 目前性能最好的Java序列化和反序列化库
-7
View File
@@ -35,7 +35,6 @@
<!-- network framework -->
<netty.version>4.1.84.Final</netty.version>
<jprotobuf.version>2.4.17</jprotobuf.version>
<kryo.version>5.3.0</kryo.version>
<!-- zookeeper -->
<curator.version>5.3.0</curator.version>
@@ -51,12 +50,6 @@
<poi.version>5.2.3</poi.version>
<csv.version>1.9.0</csv.version>
<!-- Toolkit(工具包) -->
<jctools.version>4.0.1</jctools.version>
<!-- elastic search -->
<elastic.search.version>8.4.3</elastic.search.version>
<java.version>11</java.version>
<file.encoding>UTF-8</file.encoding>
-7
View File
@@ -35,7 +35,6 @@
<!-- network framework -->
<netty.version>4.1.84.Final</netty.version>
<jprotobuf.version>2.4.17</jprotobuf.version>
<kryo.version>5.3.0</kryo.version>
<!-- zookeeper -->
<curator.version>5.3.0</curator.version>
@@ -51,12 +50,6 @@
<poi.version>5.2.3</poi.version>
<csv.version>1.9.0</csv.version>
<!-- Toolkit(工具包) -->
<jctools.version>4.0.1</jctools.version>
<!-- elastic search -->
<elastic.search.version>8.4.3</elastic.search.version>
<java.version>11</java.version>
<file.encoding>UTF-8</file.encoding>
-7
View File
@@ -35,7 +35,6 @@
<!-- network framework -->
<netty.version>4.1.84.Final</netty.version>
<jprotobuf.version>2.4.17</jprotobuf.version>
<kryo.version>5.3.0</kryo.version>
<!-- zookeeper -->
<curator.version>5.3.0</curator.version>
@@ -51,12 +50,6 @@
<poi.version>5.2.3</poi.version>
<csv.version>1.9.0</csv.version>
<!-- Toolkit(工具包) -->
<jctools.version>4.0.1</jctools.version>
<!-- elastic search -->
<elastic.search.version>8.4.3</elastic.search.version>
<java.version>11</java.version>
<file.encoding>UTF-8</file.encoding>
-7
View File
@@ -35,7 +35,6 @@
<!-- network framework -->
<netty.version>4.1.84.Final</netty.version>
<jprotobuf.version>2.4.17</jprotobuf.version>
<kryo.version>5.3.0</kryo.version>
<!-- zookeeper -->
<curator.version>5.3.0</curator.version>
@@ -51,12 +50,6 @@
<poi.version>5.2.3</poi.version>
<csv.version>1.9.0</csv.version>
<!-- Toolkit(工具包) -->
<jctools.version>4.0.1</jctools.version>
<!-- elastic search -->
<elastic.search.version>8.4.3</elastic.search.version>
<java.version>11</java.version>
<file.encoding>UTF-8</file.encoding>
-7
View File
@@ -35,7 +35,6 @@
<!-- network framework -->
<netty.version>4.1.84.Final</netty.version>
<jprotobuf.version>2.4.17</jprotobuf.version>
<kryo.version>5.3.0</kryo.version>
<!-- zookeeper -->
<curator.version>5.3.0</curator.version>
@@ -51,12 +50,6 @@
<poi.version>5.2.3</poi.version>
<csv.version>1.9.0</csv.version>
<!-- Toolkit(工具包) -->
<jctools.version>4.0.1</jctools.version>
<!-- elastic search -->
<elastic.search.version>8.4.3</elastic.search.version>
<java.version>11</java.version>
<file.encoding>UTF-8</file.encoding>
@@ -27,8 +27,8 @@ import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.util.DomUtils;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.protocol.xml.XmlProtocols;
import com.zfoo.util.math.NumberUtils;
import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;
@@ -71,7 +71,9 @@ public class PacketService implements IPacketService {
public static final String NET_COMMON_MODULE = "common";
/** 内网协议最大id */
/**
* 内网协议最大id
*/
public static final short MAN_NATIVE_PROTOCOL_ID = 100;
private final Predicate<IProtocolRegistration> netGenerateProtocolFilter = registration
@@ -127,12 +129,10 @@ public class PacketService implements IPacketService {
/**
* 获取要生成协议列表
* @param codeLanguage
* @return
*/
private Set<CodeLanguage> getProtocolList(String codeLanguage) {
var languageSet = new HashSet<CodeLanguage>();
boolean isNumeric = StringUtils.isNumeric(codeLanguage);
var isNumeric = NumberUtils.isNumeric(codeLanguage);
for (var language : CodeLanguage.values()) {
if (isNumeric) {
var code = Integer.valueOf(codeLanguage);
-7
View File
@@ -35,7 +35,6 @@
<!-- network framework -->
<netty.version>4.1.84.Final</netty.version>
<jprotobuf.version>2.4.17</jprotobuf.version>
<kryo.version>5.3.0</kryo.version>
<!-- zookeeper -->
<curator.version>5.3.0</curator.version>
@@ -51,12 +50,6 @@
<poi.version>5.2.3</poi.version>
<csv.version>1.9.0</csv.version>
<!-- Toolkit(工具包) -->
<jctools.version>4.0.1</jctools.version>
<!-- elastic search -->
<elastic.search.version>8.4.3</elastic.search.version>
<java.version>11</java.version>
<file.encoding>UTF-8</file.encoding>
+1 -7
View File
@@ -59,7 +59,7 @@
<!-- network framework -->
<netty.version>4.1.84.Final</netty.version>
<jprotobuf.version>2.4.17</jprotobuf.version>
<kryo.version>5.3.0</kryo.version>
<!-- zookeeper -->
<curator.version>5.3.0</curator.version>
@@ -75,12 +75,6 @@
<poi.version>5.2.3</poi.version>
<csv.version>1.9.0</csv.version>
<!-- Toolkit(工具包) -->
<jctools.version>4.0.1</jctools.version>
<!-- elastic search -->
<elastic.search.version>8.4.3</elastic.search.version>
<java.version>11</java.version>
<file.encoding>UTF-8</file.encoding>
+1 -1
View File
@@ -5,7 +5,7 @@ English | [简体中文](./README_CN.md)
- [zfoo protocol](https://github.com/zfoo-project/zfoo/blob/main/protocol/README.md)
It is currently the fastest framework for binary serialization and deserialization in Java, and has the fewest
serialization bytes
- The protocol is currently natively supported **C++ Java Javascript C# Go Lua GDScript**It's easy to do cross-platform
- The protocol is currently natively supported **C++ Java Javascript C# Go Lua GDScript Pythont**It's easy to do cross-platform
- The protocol can customize the private protocol format to make your protocol more secure, and supports adding fields
and being compatible with previous and subsequent protocols
- Compatible with Protobuf, it supports the generation of Protobuf protocol files, and provides a generation method from
+1 -1
View File
@@ -4,7 +4,7 @@
- [zfoo protocol](https://github.com/zfoo-project/zfoo/blob/main/protocol/README.md)
是目前的Java二进制序列化和反序列化速度最快的框架,并且为序列化字节最少的框架
- 协议目前原生支持 **C++ Java Javascript C# Go Lua GDScript**,可以轻易实现跨平台
- 协议目前原生支持 **C++ Java Javascript C# Go Lua GDScript Python**,可以轻易实现跨平台
- 协议可以自定义私有协议格式,让你的协议更加安全,支持增加字段和兼容前后版本协议
- 兼容protobuf,支持生成protobuf协议文件,提供从pojo到proto的生成方式
- proto文件到生成pojo的方式已经有不少jetbrains插件可以做到,可以在idea的plugin中搜索proto
+1 -8
View File
@@ -35,7 +35,6 @@
<!-- network framework -->
<netty.version>4.1.84.Final</netty.version>
<jprotobuf.version>2.4.17</jprotobuf.version>
<kryo.version>5.3.0</kryo.version>
<!-- zookeeper -->
<curator.version>5.3.0</curator.version>
@@ -51,12 +50,6 @@
<poi.version>5.2.3</poi.version>
<csv.version>1.9.0</csv.version>
<!-- Toolkit(工具包) -->
<jctools.version>4.0.1</jctools.version>
<!-- elastic search -->
<elastic.search.version>8.4.3</elastic.search.version>
<java.version>11</java.version>
<file.encoding>UTF-8</file.encoding>
@@ -132,7 +125,7 @@
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>${kryo.version}</version>
<version>5.3.0</version>
<scope>test</scope>
</dependency>
@@ -25,6 +25,7 @@ import com.zfoo.protocol.serializer.go.GenerateGoUtils;
import com.zfoo.protocol.serializer.javascript.GenerateJsUtils;
import com.zfoo.protocol.serializer.lua.GenerateLuaUtils;
import com.zfoo.protocol.serializer.protobuf.GenerateProtobufUtils;
import com.zfoo.protocol.serializer.python.GeneratePyUtils;
import com.zfoo.protocol.serializer.typescript.GenerateTsUtils;
import java.io.IOException;
@@ -62,11 +63,8 @@ public abstract class GenerateProtocolFile {
}
/**
* 生成各种语言的协议文件
*
* @param generateOperation
* @throws IOException
* @throws ClassNotFoundException
* EN: Generate protocol files to various languages
* CN: 生成各种语言的协议文件
*/
public static void generate(GenerateOperation generateOperation) throws IOException, ClassNotFoundException {
var protocols = ProtocolManager.protocols;
@@ -172,6 +170,15 @@ public abstract class GenerateProtocolFile {
}
}
// 生成Python协议
if (generateLanguages.contains(CodeLanguage.Python)) {
GeneratePyUtils.init(generateOperation);
GeneratePyUtils.createProtocolManager(allSortedGenerateProtocols);
for (var protocolRegistration : allSortedGenerateProtocols) {
GeneratePyUtils.createPyProtocolFile((ProtocolRegistration) protocolRegistration);
}
}
// 生成Protobuf协议
if (generateLanguages.contains(CodeLanguage.Protobuf)) {
GenerateProtobufUtils.init(generateOperation);
@@ -28,7 +28,8 @@ import java.util.List;
import java.util.Map;
/**
* 生成协议的时候,协议的文档注释和字段注释会使用这个类
* EN: When generating the protocol, the document comments and field comments of the protocol will use this class
* CN: 生成协议的时候,协议的文档注释和字段注释会使用这个类
*
* @author godotg
* @version 3.0
@@ -38,14 +39,14 @@ public abstract class GenerateProtocolNote {
// 临时变量,启动完成就会销毁,协议的文档,外层map的key为协议类;pair的key为总的注释,value为属性字段的注释,value表示的map的key为属性名称
// 比如在Test中的ComplexObject生成的pari是如下格式
/**
* key docTitle:
* key is docTitle note:
* // 复杂的对象
* // 包括了各种复杂的结构,数组,List,Set,Map
* //
* // @author godotg
* // @version 1.0
* <p>
* value aa:
* value is field note:
* // byte的包装类型
* // 优先使用基础类型,包装类型会有装箱拆箱
*/
@@ -92,18 +93,19 @@ public abstract class GenerateProtocolNote {
case Lua:
fieldNote = StringUtils.format("-- {}", fieldNote).replace("\n", "\n-- ");
break;
case Python:
case GdScript:
fieldNote = StringUtils.format("# {}", fieldNote).replace("\n", "\n# ");
break;
case Enhance:
default:
throw new RunException("无法识别的枚举类型[{}]", language);
throw new RunException("unrecognized enum type [{}]", language);
}
return fieldNote;
}
public static void initProtocolNote(List<IProtocolRegistration> protocolRegistrations) {
AssertionUtils.notNull(protocolNoteMap, "[{}]已经初始完成,初始化完成过后不能调用initProtocolDocument", GenerateProtocolNote.class.getSimpleName());
AssertionUtils.notNull(protocolNoteMap, "[{}] duplicate initialization", GenerateProtocolNote.class.getSimpleName());
for (var protocolRegistration : protocolRegistrations) {
var protocolClazz = protocolRegistration.protocolConstructor().getDeclaringClass();
@@ -53,13 +53,29 @@ public abstract class GenerateProtocolPath {
switch (language) {
case Cpp:
break;
case Go:
break;
case JavaScript:
break;
case TypeScript:
break;
case CSharp:
break;
case Protobuf:
break;
case Lua:
break;
case GdScript:
break;
case Python:
if (StringUtils.isBlank(getProtocolPath(protocolId))) {
path = StringUtils.PERIOD;
} else {
path = StringUtils.substringBeforeLast(path, StringUtils.SLASH);
path = path.replaceAll(StringUtils.SLASH , StringUtils.PERIOD);
}
break;
case Enhance:
break;
default:
@@ -33,6 +33,7 @@ import com.zfoo.protocol.serializer.go.GenerateGoUtils;
import com.zfoo.protocol.serializer.javascript.GenerateJsUtils;
import com.zfoo.protocol.serializer.lua.GenerateLuaUtils;
import com.zfoo.protocol.serializer.protobuf.GenerateProtobufUtils;
import com.zfoo.protocol.serializer.python.GeneratePyUtils;
import com.zfoo.protocol.serializer.reflect.*;
import com.zfoo.protocol.serializer.typescript.GenerateTsUtils;
import com.zfoo.protocol.util.AssertionUtils;
@@ -347,6 +348,7 @@ public class ProtocolAnalysis {
GenerateTsUtils.clear();
GenerateLuaUtils.clear();
GenerateGdUtils.clear();
GeneratePyUtils.clear();
GenerateProtobufUtils.clear();
}
@@ -23,22 +23,23 @@ public enum CodeLanguage {
*/
Enhance(1),
Cpp(2),
Cpp(1 << 1),
Go(4),
Go(1 << 2),
JavaScript(8),
JavaScript(1 << 3),
TypeScript(16),
TypeScript(1 << 4),
Lua(32),
Lua(1 << 5),
CSharp(64),
CSharp(1 << 6),
GdScript(128),
GdScript(1 << 7),
Protobuf(256)
;
Python(1 << 8),
Protobuf(1 << 12);
public final int id;
@@ -52,6 +52,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeBooleanArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeBooleanArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -78,6 +79,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeBooleanBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeBooleanArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -104,6 +106,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeByteArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -130,6 +133,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeByteBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -156,6 +160,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeShortArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -182,6 +187,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeShortBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -208,6 +214,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeIntArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -234,6 +241,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeIntBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -260,6 +268,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeLongArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -286,6 +295,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeLongBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -312,6 +322,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeFloatArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -338,6 +349,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeFloatBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -364,6 +376,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeDoubleArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -390,6 +403,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeDoubleBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -416,6 +430,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeStringArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeStringArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -442,6 +457,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeCharArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeCharArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -468,6 +484,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeCharBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeCharArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -496,6 +513,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writePacketArray($1, {}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId)));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, protocolId)).append(LS);
break;
case Lua:
@@ -541,6 +559,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", array)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readBooleanArray()", array)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readBooleanArray()", array)).append(LS);
break;
@@ -569,6 +590,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", array)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readBooleanArray()", array)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readBooleanArray()", array)).append(LS);
break;
@@ -597,6 +621,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readByteArray()", array)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readByteArray()", array)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readByteArray()", array)).append(LS);
break;
@@ -625,6 +652,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readByteArray()", array)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readByteArray()", array)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readByteArray()", array)).append(LS);
break;
@@ -653,6 +683,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readShortArray()", array)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readShortArray()", array)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readShortArray()", array)).append(LS);
break;
@@ -681,6 +714,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readShortArray()", array)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readShortArray()", array)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readShortArray()", array)).append(LS);
break;
@@ -709,6 +745,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readIntArray()", array)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readIntArray()", array)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readIntArray()", array)).append(LS);
break;
@@ -737,6 +776,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readIntArray()", array)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readIntArray()", array)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readIntArray()", array)).append(LS);
break;
@@ -765,6 +807,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readLongArray()", array)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readLongArray()", array)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readLongArray()", array)).append(LS);
break;
@@ -793,6 +838,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readLongArray()", array)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readLongArray()", array)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readLongArray()", array)).append(LS);
break;
@@ -821,6 +869,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readFloatArray()", array)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readFloatArray()", array)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readFloatArray()", array)).append(LS);
break;
@@ -849,6 +900,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readFloatArray()", array)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readFloatArray()", array)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readFloatArray()", array)).append(LS);
break;
@@ -877,6 +931,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", array)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readDoubleArray()", array)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readDoubleArray()", array)).append(LS);
break;
@@ -905,6 +962,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", array)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readDoubleArray()", array)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readDoubleArray()", array)).append(LS);
break;
@@ -933,6 +993,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readStringArray()", array)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readStringArray()", array)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readStringArray()", array)).append(LS);
break;
@@ -961,6 +1024,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readCharArray()", array)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readCharArray()", array)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readCharArray()", array)).append(LS);
break;
@@ -989,6 +1055,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readCharArray()", array)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readCharArray()", array)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readCharArray()", array)).append(LS);
break;
@@ -1017,6 +1086,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", array, protocolId)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readPacketArray({})", array, protocolId)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readPacketArray({})", array, protocolId)).append(LS);
break;
@@ -51,6 +51,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeBooleanList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeBooleanArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -77,6 +78,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeByteList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -103,6 +105,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeShortList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -129,6 +132,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeIntList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -155,6 +159,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeLongList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -182,6 +187,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeFloatList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -209,6 +215,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeDoubleList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -236,6 +243,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeStringList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeStringArray({})", objectStr)).append(LS);
break;
case Lua:
@@ -265,6 +273,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writePacketList($1, (List){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId)));
break;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, protocolId)).append(LS);
break;
case Lua:
@@ -307,6 +316,9 @@ public class CutDownListSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", list)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readBooleanArray()", list)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readBooleanArray()", list)).append(LS);
break;
@@ -335,6 +347,9 @@ public class CutDownListSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readByteArray()", list)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readByteArray()", list)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readByteArray()", list)).append(LS);
break;
@@ -363,6 +378,9 @@ public class CutDownListSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readShortArray()", list)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readShortArray()", list)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readShortArray()", list)).append(LS);
break;
@@ -391,6 +409,9 @@ public class CutDownListSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readIntArray()", list)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readIntArray()", list)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readIntArray()", list)).append(LS);
break;
@@ -419,6 +440,9 @@ public class CutDownListSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readLongArray()", list)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readLongArray()", list)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readLongArray()", list)).append(LS);
break;
@@ -447,6 +471,9 @@ public class CutDownListSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readFloatArray()", list)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readFloatArray()", list)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readFloatArray()", list)).append(LS);
break;
@@ -475,6 +502,9 @@ public class CutDownListSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", list)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readDoubleArray()", list)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readDoubleArray()", list)).append(LS);
break;
@@ -503,6 +533,9 @@ public class CutDownListSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readStringArray()", list)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readStringArray()", list)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readStringArray()", list)).append(LS);
break;
@@ -533,6 +566,9 @@ public class CutDownListSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", list, protocolId)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readPacketArray({})", list, protocolId)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readPacketArray({})", list, protocolId)).append(LS);
break;
@@ -58,6 +58,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeIntIntMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeIntIntMap({})", objectStr)).append(LS);
return true;
case Lua:
@@ -81,6 +82,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeIntLongMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeIntLongMap({})", objectStr)).append(LS);
return true;
case Lua:
@@ -104,6 +106,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeIntStringMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeIntStringMap({})", objectStr)).append(LS);
return true;
case Lua:
@@ -127,6 +130,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeIntPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId())));
return true;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeIntPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return true;
case Lua:
@@ -152,6 +156,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeLongIntMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeLongIntMap({})", objectStr)).append(LS);
return true;
case Lua:
@@ -175,6 +180,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeLongLongMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeLongLongMap({})", objectStr)).append(LS);
return true;
case Lua:
@@ -198,6 +204,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeLongStringMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeLongStringMap({})", objectStr)).append(LS);
return true;
case Lua:
@@ -221,6 +228,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeLongPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId())));
return true;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeLongPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return true;
case Lua:
@@ -246,6 +254,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeStringIntMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeStringIntMap({})", objectStr)).append(LS);
return true;
case Lua:
@@ -269,6 +278,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeStringLongMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeStringLongMap({})", objectStr)).append(LS);
return true;
case Lua:
@@ -292,6 +302,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeStringStringMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeStringStringMap({})", objectStr)).append(LS);
return true;
case Lua:
@@ -315,6 +326,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("{}.writeStringPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId())));
return true;
case GdScript:
case Python:
builder.append(StringUtils.format("buffer.writeStringPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return true;
case Lua:
@@ -358,6 +370,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readIntIntMap()", map)).append(LS);
return map;
case Python:
builder.append(StringUtils.format("{} = buffer.readIntIntMap()", map)).append(LS);
return map;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readIntIntMap()", map)).append(LS);
return map;
@@ -383,6 +398,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readIntLongMap()", map)).append(LS);
return map;
case Python:
builder.append(StringUtils.format("{} = buffer.readIntLongMap()", map)).append(LS);
return map;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readIntLongMap()", map)).append(LS);
return map;
@@ -408,6 +426,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readIntStringMap()", map)).append(LS);
return map;
case Python:
builder.append(StringUtils.format("{} = buffer.readIntStringMap()", map)).append(LS);
return map;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readIntStringMap()", map)).append(LS);
return map;
@@ -434,6 +455,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readIntPacketMap({})", map, protocolId)).append(LS);
return map;
case Python:
builder.append(StringUtils.format("{} = buffer.readIntPacketMap({})", map, protocolId)).append(LS);
return map;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readIntPacketMap({})", map, protocolId)).append(LS);
return map;
@@ -460,6 +484,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readLongIntMap()", map)).append(LS);
return map;
case Python:
builder.append(StringUtils.format("{} = buffer.readLongIntMap()", map)).append(LS);
return map;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readLongIntMap()", map)).append(LS);
return map;
@@ -485,6 +512,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readLongLongMap()", map)).append(LS);
return map;
case Python:
builder.append(StringUtils.format("{} = buffer.readLongLongMap()", map)).append(LS);
return map;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readLongLongMap()", map)).append(LS);
return map;
@@ -510,6 +540,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readLongStringMap()", map)).append(LS);
return map;
case Python:
builder.append(StringUtils.format("{} = buffer.readLongStringMap()", map)).append(LS);
return map;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readLongStringMap()", map)).append(LS);
return map;
@@ -536,6 +569,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readLongPacketMap({})", map, protocolId)).append(LS);
return map;
case Python:
builder.append(StringUtils.format("{} = buffer.readLongPacketMap({})", map, protocolId)).append(LS);
return map;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readLongPacketMap({})", map, protocolId)).append(LS);
return map;
@@ -563,6 +599,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readStringIntMap()", map)).append(LS);
return map;
case Python:
builder.append(StringUtils.format("{} = buffer.readStringIntMap()", map)).append(LS);
return map;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readStringIntMap()", map)).append(LS);
return map;
@@ -588,6 +627,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readStringLongMap()", map)).append(LS);
return map;
case Python:
builder.append(StringUtils.format("{} = buffer.readStringLongMap()", map)).append(LS);
return map;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readStringLongMap()", map)).append(LS);
return map;
@@ -613,6 +655,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readStringStringMap()", map)).append(LS);
return map;
case Python:
builder.append(StringUtils.format("{} = buffer.readStringStringMap()", map)).append(LS);
return map;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readStringStringMap()", map)).append(LS);
return map;
@@ -639,6 +684,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readStringPacketMap({})", map, protocolId)).append(LS);
return map;
case Python:
builder.append(StringUtils.format("{} = buffer.readStringPacketMap({})", map, protocolId)).append(LS);
return map;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readStringPacketMap({})", map, protocolId)).append(LS);
return map;
@@ -54,6 +54,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeBooleanArray({})", objectStr)).append(LS);
break;
case Python:
builder.append(StringUtils.format("buffer.writeBooleanSet({})", objectStr)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("buffer:writeBooleanArray({})", objectStr)).append(LS);
break;
@@ -80,6 +83,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS);
break;
case Python:
builder.append(StringUtils.format("buffer.writeByteSet({})", objectStr)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("buffer:writeByteArray({})", objectStr)).append(LS);
break;
@@ -106,6 +112,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS);
break;
case Python:
builder.append(StringUtils.format("buffer.writeShortSet({})", objectStr)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("buffer:writeShortArray({})", objectStr)).append(LS);
break;
@@ -132,6 +141,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS);
break;
case Python:
builder.append(StringUtils.format("buffer.writeIntSet({})", objectStr)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("buffer:writeIntArray({})", objectStr)).append(LS);
break;
@@ -158,6 +170,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS);
break;
case Python:
builder.append(StringUtils.format("buffer.writeLongSet({})", objectStr)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("buffer:writeLongArray({})", objectStr)).append(LS);
break;
@@ -184,6 +199,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS);
break;
case Python:
builder.append(StringUtils.format("buffer.writeFloatSet({})", objectStr)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("buffer:writeFloatArray({})", objectStr)).append(LS);
break;
@@ -210,6 +228,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS);
break;
case Python:
builder.append(StringUtils.format("buffer.writeDoubleSet({})", objectStr)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("buffer:writeDoubleArray({})", objectStr)).append(LS);
break;
@@ -236,6 +257,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeStringArray({})", objectStr)).append(LS);
break;
case Python:
builder.append(StringUtils.format("buffer.writeStringSet({})", objectStr)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("buffer:writeStringArray({})", objectStr)).append(LS);
break;
@@ -265,6 +289,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, protocolId)).append(LS);
break;
case Python:
builder.append(StringUtils.format("buffer.writePacketSet({}, {})", objectStr, protocolId)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("buffer:writePacketArray({}, {})", objectStr, protocolId)).append(LS);
break;
@@ -305,6 +332,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", set)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readBooleanSet()", set)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readBooleanArray()", set)).append(LS);
break;
@@ -333,6 +363,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readByteArray()", set)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readByteSet()", set)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readByteArray()", set)).append(LS);
break;
@@ -361,6 +394,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readShortArray()", set)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readShortSet()", set)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readShortArray()", set)).append(LS);
break;
@@ -389,6 +425,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readIntArray()", set)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readIntSet()", set)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readIntArray()", set)).append(LS);
break;
@@ -417,6 +456,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readLongArray()", set)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readLongSet()", set)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readLongArray()", set)).append(LS);
break;
@@ -445,6 +487,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readFloatArray()", set)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readFloatSet()", set)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readFloatArray()", set)).append(LS);
break;
@@ -473,6 +518,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", set)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readDoubleSet()", set)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readDoubleArray()", set)).append(LS);
break;
@@ -501,6 +549,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readStringArray()", set)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readStringSet()", set)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readStringArray()", set)).append(LS);
break;
@@ -531,6 +582,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", set, protocolId)).append(LS);
break;
case Python:
builder.append(StringUtils.format("{} = buffer.readPacketSet({})", set, protocolId)).append(LS);
break;
case Lua:
builder.append(StringUtils.format("local {} = buffer:readPacketArray({})", set, protocolId)).append(LS);
break;
@@ -97,7 +97,7 @@ public abstract class GenerateCppUtils {
FileUtils.writeInputStreamToFile(createFile, fileInputStream);
}
var protocolManagerTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("cpp/ProtocolManagerTemplate.h")));
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("cpp/ProtocolManagerTemplate.h");
var headerBuilder = new StringBuilder();
var initProtocolBuilder = new StringBuilder();
@@ -120,7 +120,7 @@ public abstract class GenerateCppUtils {
var registrationConstructor = registration.getConstructor();
var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName();
var protocolTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("cpp/ProtocolTemplate.h")));
var protocolTemplate = ClassUtils.getFileFromClassPathToString("cpp/ProtocolTemplate.h");
// protocol object
var defineProtocolName = protocolClazzName.toUpperCase();
@@ -106,7 +106,7 @@ public abstract class GenerateCsUtils {
var registrationConstructor = registration.getConstructor();
var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName();
var protocolTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("csharp/ProtocolTemplate.cs")));
var protocolTemplate = ClassUtils.getFileFromClassPathToString("csharp/ProtocolTemplate.cs");
var classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.CSharp);
var fieldDefinition = fieldDefinition(registration);
@@ -91,10 +91,10 @@ public abstract class GenerateGdUtils {
public static void createProtocolManager(List<IProtocolRegistration> protocolList) throws IOException {
var byteBufferFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "ByteBuffer.gd"));
var byteBufferTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("gdscript/buffer/ByteBuffer.gd")));
var byteBufferTemplate = ClassUtils.getFileFromClassPathToString("gdscript/buffer/ByteBuffer.gd");
FileUtils.writeStringToFile(byteBufferFile, StringUtils.format(byteBufferTemplate, protocolOutputRootPath), false);
var protocolManagerTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("gdscript/ProtocolManagerTemplate.gd")));
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("gdscript/ProtocolManagerTemplate.gd");
// 生成ProtocolManager.gd文件
var importBuilder = new StringBuilder();
var initList = new ArrayList<String>();
@@ -125,7 +125,7 @@ public abstract class GenerateGdUtils {
var writeObject = writeObject(registration);
var readObject = readObject(registration);
var protocolTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("gdscript/ProtocolTemplate.gd")));
var protocolTemplate = ClassUtils.getFileFromClassPathToString("gdscript/ProtocolTemplate.gd");
protocolTemplate = StringUtils.format(protocolTemplate, protocolId, protocolClazzName, includeSubProtocol, classNote, fieldDefinition.trim(),
StringUtils.EMPTY_JSON, toStringMethod, writeObject.trim(), readObject.trim());
@@ -96,7 +96,7 @@ public abstract class GenerateGoUtils {
.filter(it -> Objects.nonNull(it))
.forEach(it -> initProtocolBuilder.append(TAB).append(StringUtils.format("Protocols[{}] = new({})", it.protocolId(), it.protocolConstructor().getDeclaringClass().getSimpleName())).append(LS));
var protocolManagerTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("go/ProtocolManagerTemplate.go")));
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("go/ProtocolManagerTemplate.go");
protocolManagerTemplate = StringUtils.format(protocolManagerTemplate, initProtocolBuilder.toString().trim());
FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "ProtocolManager.go")), protocolManagerTemplate, true);
}
@@ -109,8 +109,8 @@ public abstract class GenerateGoUtils {
var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName();
var protocolTemplate = ArrayUtils.isEmpty(registration.getFields())
? StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("go/ProtocolTemplateEmpty.go")))
: StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("go/ProtocolTemplate.go")));
? ClassUtils.getFileFromClassPathToString("go/ProtocolTemplateEmpty.go")
: ClassUtils.getFileFromClassPathToString("go/ProtocolTemplate.go");
var classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.Go);
var fieldDefinition = fieldDefinition(registration);
@@ -91,7 +91,7 @@ public abstract class GenerateJsUtils {
}
// 生成ProtocolManager.js文件
var protocolManagerTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("javascript/ProtocolManagerTemplate.js")));
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("javascript/ProtocolManagerTemplate.js");
var importBuilder = new StringBuilder();
var initProtocolBuilder = new StringBuilder();
@@ -115,7 +115,7 @@ public abstract class GenerateJsUtils {
var registrationConstructor = registration.getConstructor();
var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName();
var protocolTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("javascript/ProtocolTemplate.js")));
var protocolTemplate = ClassUtils.getFileFromClassPathToString("javascript/ProtocolTemplate.js");
var classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.JavaScript);
var valueOfMethod = valueOfMethod(registration);
@@ -90,7 +90,7 @@ public abstract class GenerateLuaUtils {
}
// 生成Protocol.lua文件
var protocolManagerTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("lua/ProtocolManagerTemplate.lua")));
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("lua/ProtocolManagerTemplate.lua");
var fieldBuilder = new StringBuilder();
var protocolBuilder = new StringBuilder();
for (var protocol : protocolList) {
@@ -117,7 +117,7 @@ public abstract class GenerateLuaUtils {
var protocolId = registration.protocolId();
var registrationConstructor = registration.getConstructor();
var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName();
var protocolTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("lua/ProtocolTemplate.lua")));
var protocolTemplate = ClassUtils.getFileFromClassPathToString("lua/ProtocolTemplate.lua");
var classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.Lua);
var valueOfMethod = valueOfMethod(registration);
@@ -0,0 +1,188 @@
/*
* 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.python;
import com.zfoo.protocol.generate.GenerateOperation;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.generate.GenerateProtocolNote;
import com.zfoo.protocol.generate.GenerateProtocolPath;
import com.zfoo.protocol.registration.IProtocolRegistration;
import com.zfoo.protocol.registration.ProtocolRegistration;
import com.zfoo.protocol.registration.anno.Compatible;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.serializer.csharp.GenerateCsUtils;
import com.zfoo.protocol.serializer.reflect.*;
import com.zfoo.protocol.util.ClassUtils;
import com.zfoo.protocol.util.FileUtils;
import com.zfoo.protocol.util.ReflectionUtils;
import com.zfoo.protocol.util.StringUtils;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.zfoo.protocol.util.FileUtils.LS;
import static com.zfoo.protocol.util.StringUtils.TAB;
/**
* @author godotg
* @version 3.0
*/
public abstract class GeneratePyUtils {
private static String protocolOutputRootPath = "pyProtocol/";
private static Map<ISerializer, IPySerializer> pySerializerMap;
public static IPySerializer pySerializer(ISerializer serializer) {
return pySerializerMap.get(serializer);
}
public static void init(GenerateOperation generateOperation) {
protocolOutputRootPath = FileUtils.joinPath(generateOperation.getProtocolPath(), protocolOutputRootPath);
FileUtils.deleteFile(new File(protocolOutputRootPath));
FileUtils.createDirectory(protocolOutputRootPath);
pySerializerMap = new HashMap<>();
pySerializerMap.put(BooleanSerializer.INSTANCE, new PyBooleanSerializer());
pySerializerMap.put(ByteSerializer.INSTANCE, new PyByteSerializer());
pySerializerMap.put(ShortSerializer.INSTANCE, new PyShortSerializer());
pySerializerMap.put(IntSerializer.INSTANCE, new PyIntSerializer());
pySerializerMap.put(LongSerializer.INSTANCE, new PyLongSerializer());
pySerializerMap.put(FloatSerializer.INSTANCE, new PyFloatSerializer());
pySerializerMap.put(DoubleSerializer.INSTANCE, new PyDoubleSerializer());
pySerializerMap.put(CharSerializer.INSTANCE, new PyCharSerializer());
pySerializerMap.put(StringSerializer.INSTANCE, new PyStringSerializer());
pySerializerMap.put(ArraySerializer.INSTANCE, new PyArraySerializer());
pySerializerMap.put(ListSerializer.INSTANCE, new PyListSerializer());
pySerializerMap.put(SetSerializer.INSTANCE, new PySetSerializer());
pySerializerMap.put(MapSerializer.INSTANCE, new PyMapSerializer());
pySerializerMap.put(ObjectProtocolSerializer.INSTANCE, new PyObjectProtocolSerializer());
}
public static void clear() {
pySerializerMap = null;
protocolOutputRootPath = null;
}
public static void createProtocolManager(List<IProtocolRegistration> protocolList) throws IOException {
var list = List.of("python/ByteBuffer.py");
for (var fileName : list) {
var fileInputStream = ClassUtils.getFileFromClassPath(fileName);
var createFile = new File(StringUtils.format("{}/{}", protocolOutputRootPath, StringUtils.substringAfterFirst(fileName, "python/")));
FileUtils.writeInputStreamToFile(createFile, fileInputStream);
}
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("python/ProtocolManagerTemplate.py");
var importBuilder = new StringBuilder();
var initProtocolBuilder = new StringBuilder();
for (var protocol : protocolList) {
var protocolId = protocol.protocolId();
var protocolName = protocol.protocolConstructor().getDeclaringClass().getSimpleName();
var path = GenerateProtocolPath.protocolAbsolutePath(protocolId, CodeLanguage.Python);
importBuilder.append(StringUtils.format("from {} import {}", path, protocolName)).append(LS);
initProtocolBuilder.append(StringUtils.format("protocols[{}] = {}.{}", protocolId, protocolName, protocolName)).append(LS);
}
protocolManagerTemplate = StringUtils.format(protocolManagerTemplate, importBuilder.toString().trim(), StringUtils.EMPTY_JSON, initProtocolBuilder.toString().trim());
FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "ProtocolManager.py")), protocolManagerTemplate, true);
}
public static void createPyProtocolFile(ProtocolRegistration registration) {
GenerateProtocolFile.index.set(0);
var protocolId = registration.protocolId();
var registrationConstructor = registration.getConstructor();
var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName();
var protocolTemplate = ClassUtils.getFileFromClassPathToString("python/ProtocolTemplate.py");
var classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.Python);
var fieldDefinition = fieldDefinition(registration);
var writeObject = writeObject(registration);
var readObject = readObject(registration);
protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName
, fieldDefinition.trim(), protocolId, writeObject.trim(), protocolClazzName, readObject.trim());
var protocolOutputPath = StringUtils.format("{}/{}/{}.py", protocolOutputRootPath
, GenerateProtocolPath.getProtocolPath(protocolId), protocolClazzName);
FileUtils.writeStringToFile(new File(protocolOutputPath), protocolTemplate, true);
}
private static String fieldDefinition(ProtocolRegistration registration) {
var protocolId = registration.getId();
var fields = registration.getFields();
var fieldRegistrations = registration.getFieldRegistrations();
var pyBuilder = new StringBuilder();
// 生成源代码字段的时候按照原始定义的方式生成
var sequencedFields = ReflectionUtils.notStaticAndTransientFields(registration.getConstructor().getDeclaringClass());
for (int i = 0; i < sequencedFields.size(); i++) {
var field = sequencedFields.get(i);
IFieldRegistration fieldRegistration = fieldRegistrations[GenerateProtocolFile.indexOf(fields, field)];
var fieldName = field.getName();
// 生成注释
var fieldNote = GenerateProtocolNote.fieldNote(protocolId, fieldName, CodeLanguage.Python);
if (StringUtils.isNotBlank(fieldNote)) {
pyBuilder.append(TAB).append(fieldNote).append(LS);
}
var fieldDefaultValue = pySerializer(fieldRegistration.serializer()).fieldDefaultValue(field, fieldRegistration);
// 生成类型的注释
pyBuilder.append(StringUtils.format("{}{} = {}", TAB, fieldName, fieldDefaultValue));
pyBuilder.append(StringUtils.format(" # {}", GenerateCsUtils.toCsClassName(field.getGenericType().getTypeName())));
pyBuilder.append(LS);
}
return pyBuilder.toString();
}
private static String writeObject(ProtocolRegistration registration) {
var fields = registration.getFields();
var fieldRegistrations = registration.getFieldRegistrations();
var pyBuilder = new StringBuilder();
for (var i = 0; i < fields.length; i++) {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
pySerializer(fieldRegistration.serializer()).writeObject(pyBuilder, "packet." + field.getName(), 2, field, fieldRegistration);
}
return pyBuilder.toString();
}
private static String readObject(ProtocolRegistration registration) {
var fields = registration.getFields();
var fieldRegistrations = registration.getFieldRegistrations();
var pyBuilder = new StringBuilder();
for (var i = 0; i < fields.length; i++) {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
if (field.isAnnotationPresent(Compatible.class)) {
pyBuilder.append(TAB + TAB).append("if not buffer.isReadable():").append(LS);
pyBuilder.append(TAB + TAB + TAB).append("return packet").append(LS);
pyBuilder.append(TAB + TAB).append("pass").append(LS);
}
var readObject = pySerializer(fieldRegistration.serializer()).readObject(pyBuilder, 2, field, fieldRegistration);
pyBuilder.append(TAB + TAB).append(StringUtils.format("packet.{} = {}", field.getName(), readObject)).append(LS);
}
return pyBuilder.toString();
}
public static StringBuilder addTab(StringBuilder builder, int deep) {
builder.append(TAB.repeat(Math.max(0, deep)));
return builder;
}
}
@@ -0,0 +1,32 @@
/*
* 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.python;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import java.lang.reflect.Field;
/**
* @author godotg
* @version 3.0
*/
public interface IPySerializer {
String fieldDefaultValue(Field field, IFieldRegistration fieldRegistration);
void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration);
String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration);
}
@@ -0,0 +1,92 @@
/*
* 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.python;
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 godotg
* @version 3.0
*/
public class PyArraySerializer implements IPySerializer {
@Override
public String fieldDefaultValue(Field field, IFieldRegistration fieldRegistration) {
return "[]";
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GeneratePyUtils.addTab(builder, deep);
if (CutDownArraySerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Python)) {
return;
}
ArrayField arrayField = (ArrayField) fieldRegistration;
builder.append(StringUtils.format("if {} is None:", objectStr)).append(LS);
GeneratePyUtils.addTab(builder, deep + 1);
builder.append("buffer.writeInt(0)").append(LS);
GeneratePyUtils.addTab(builder, deep);
builder.append("else:").append(LS);
GeneratePyUtils.addTab(builder, deep + 1);
builder.append(StringUtils.format("buffer.writeInt(len({}))", objectStr)).append(LS);
String element = "element" + GenerateProtocolFile.index.getAndIncrement();
GeneratePyUtils.addTab(builder, deep + 1);
builder.append(StringUtils.format("for {} in {}:", element, objectStr)).append(LS);
GeneratePyUtils.pySerializer(arrayField.getArrayElementRegistration().serializer())
.writeObject(builder, element, deep + 2, field, arrayField.getArrayElementRegistration());
}
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
GeneratePyUtils.addTab(builder, deep);
var cutDown = CutDownArraySerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Python);
if (cutDown != null) {
return cutDown;
}
ArrayField arrayField = (ArrayField) fieldRegistration;
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("{} = []", result)).append(LS);
String i = "index" + GenerateProtocolFile.index.getAndIncrement();
String size = "size" + GenerateProtocolFile.index.getAndIncrement();
GeneratePyUtils.addTab(builder, deep);
builder.append(StringUtils.format("{} = buffer.readInt()", size)).append(LS);
GeneratePyUtils.addTab(builder, deep);
builder.append(StringUtils.format("if {} > 0:", size)).append(LS);
GeneratePyUtils.addTab(builder, deep + 1);
builder.append(StringUtils.format("for {} in range({}):", i, size)).append(LS);
String readObject = GeneratePyUtils.pySerializer(arrayField.getArrayElementRegistration().serializer())
.readObject(builder, deep + 2, field, arrayField.getArrayElementRegistration());
GeneratePyUtils.addTab(builder, deep + 2);
builder.append(StringUtils.format("{}.append({})", result, readObject)).append(LS);
return result;
}
}
@@ -0,0 +1,49 @@
/*
* 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.python;
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 godotg
* @version 3.0
*/
public class PyBooleanSerializer implements IPySerializer {
@Override
public String fieldDefaultValue(Field field, IFieldRegistration fieldRegistration) {
return "False";
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GeneratePyUtils.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();
GeneratePyUtils.addTab(builder, deep);
builder.append(StringUtils.format("{} = buffer.readBool() ", result)).append(LS);
return result;
}
}
@@ -0,0 +1,49 @@
/*
* 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.python;
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 godotg
* @version 3.0
*/
public class PyByteSerializer implements IPySerializer {
@Override
public String fieldDefaultValue(Field field, IFieldRegistration fieldRegistration) {
return "0";
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GeneratePyUtils.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();
GeneratePyUtils.addTab(builder, deep);
builder.append(StringUtils.format("{} = buffer.readByte()", result)).append(LS);
return result;
}
}
@@ -0,0 +1,48 @@
/*
* 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.python;
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 godotg
* @version 3.0
*/
public class PyCharSerializer implements IPySerializer {
@Override
public String fieldDefaultValue(Field field, IFieldRegistration fieldRegistration) {
return "\"\"";
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GeneratePyUtils.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();
GeneratePyUtils.addTab(builder, deep);
builder.append(StringUtils.format("{} = buffer.readChar()", result)).append(LS);
return result;
}
}
@@ -0,0 +1,49 @@
/*
* 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.python;
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 godotg
* @version 3.0
*/
public class PyDoubleSerializer implements IPySerializer {
@Override
public String fieldDefaultValue(Field field, IFieldRegistration fieldRegistration) {
return "0.0";
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GeneratePyUtils.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();
GeneratePyUtils.addTab(builder, deep);
builder.append(StringUtils.format("{} = buffer.readDouble()", result)).append(LS);
return result;
}
}
@@ -0,0 +1,49 @@
/*
* 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.python;
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 godotg
* @version 3.0
*/
public class PyFloatSerializer implements IPySerializer {
@Override
public String fieldDefaultValue(Field field, IFieldRegistration fieldRegistration) {
return "0.0";
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GeneratePyUtils.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();
GeneratePyUtils.addTab(builder, deep);
builder.append(StringUtils.format("{} = buffer.readFloat()", result)).append(LS);
return result;
}
}
@@ -0,0 +1,49 @@
/*
* 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.python;
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 godotg
* @version 3.0
*/
public class PyIntSerializer implements IPySerializer {
@Override
public String fieldDefaultValue(Field field, IFieldRegistration fieldRegistration) {
return "0";
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GeneratePyUtils.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();
GeneratePyUtils.addTab(builder, deep);
builder.append(StringUtils.format("{} = buffer.readInt()", result)).append(LS);
return result;
}
}
@@ -0,0 +1,92 @@
/*
* 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.python;
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 godotg
* @version 3.0
*/
public class PyListSerializer implements IPySerializer {
@Override
public String fieldDefaultValue(Field field, IFieldRegistration fieldRegistration) {
return "[]";
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GeneratePyUtils.addTab(builder, deep);
if (CutDownListSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Python)) {
return;
}
ListField listField = (ListField) fieldRegistration;
builder.append(StringUtils.format("if {} is None:", objectStr)).append(LS);
GeneratePyUtils.addTab(builder, deep + 1);
builder.append("buffer.writeInt(0)").append(LS);
GeneratePyUtils.addTab(builder, deep);
builder.append("else:").append(LS);
GeneratePyUtils.addTab(builder, deep + 1);
builder.append(StringUtils.format("buffer.writeInt(len({}))", objectStr)).append(LS);
String element = "element" + GenerateProtocolFile.index.getAndIncrement();
GeneratePyUtils.addTab(builder, deep + 1);
builder.append(StringUtils.format("for {} in {}:", element, objectStr)).append(LS);
GeneratePyUtils.pySerializer(listField.getListElementRegistration().serializer())
.writeObject(builder, element, deep + 2, field, listField.getListElementRegistration());
}
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
GeneratePyUtils.addTab(builder, deep);
var cutDown = CutDownListSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Python);
if (cutDown != null) {
return cutDown;
}
ListField listField = (ListField) fieldRegistration;
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("{} = []", result)).append(LS);
String i = "index" + GenerateProtocolFile.index.getAndIncrement();
String size = "size" + GenerateProtocolFile.index.getAndIncrement();
GeneratePyUtils.addTab(builder, deep);
builder.append(StringUtils.format("{} = buffer.readInt()", size)).append(LS);
GeneratePyUtils.addTab(builder, deep);
builder.append(StringUtils.format("if {} > 0:", size)).append(LS);
GeneratePyUtils.addTab(builder, deep + 1);
builder.append(StringUtils.format("for {} in range({}):", i, size)).append(LS);
String readObject = GeneratePyUtils.pySerializer(listField.getListElementRegistration().serializer())
.readObject(builder, deep + 2, field, listField.getListElementRegistration());
GeneratePyUtils.addTab(builder, deep + 2);
builder.append(StringUtils.format("{}.append({})", result, readObject)).append(LS);
return result;
}
}
@@ -0,0 +1,49 @@
/*
* 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.python;
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 godotg
* @version 3.0
*/
public class PyLongSerializer implements IPySerializer {
@Override
public String fieldDefaultValue(Field field, IFieldRegistration fieldRegistration) {
return "0";
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GeneratePyUtils.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();
GeneratePyUtils.addTab(builder, deep);
builder.append(StringUtils.format("{} = buffer.readLong()", result)).append(LS);
return result;
}
}
@@ -0,0 +1,104 @@
/*
* 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.python;
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 godotg
* @version 3.0
*/
public class PyMapSerializer implements IPySerializer {
@Override
public String fieldDefaultValue(Field field, IFieldRegistration fieldRegistration) {
return "{}";
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GeneratePyUtils.addTab(builder, deep);
if (CutDownMapSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Python)) {
return;
}
MapField mapField = (MapField) fieldRegistration;
builder.append(StringUtils.format("if {} is None:", objectStr)).append(LS);
GeneratePyUtils.addTab(builder, deep + 1);
builder.append("buffer.writeInt(0)").append(LS);
GeneratePyUtils.addTab(builder, deep);
builder.append("else:").append(LS);
GeneratePyUtils.addTab(builder, deep + 1);
builder.append(StringUtils.format("buffer.writeInt(len({}))", objectStr)).append(LS);
String key = "key" + GenerateProtocolFile.index.getAndIncrement();
String value = "value" + GenerateProtocolFile.index.getAndIncrement();
GeneratePyUtils.addTab(builder, deep + 1);
builder.append(StringUtils.format("for {} in {}:", key, objectStr)).append(LS);
GeneratePyUtils.addTab(builder, deep + 2);
builder.append(StringUtils.format("{} = {}[{}]", value, objectStr, key)).append(LS);
GeneratePyUtils.pySerializer(mapField.getMapKeyRegistration().serializer())
.writeObject(builder, key, deep + 2, field, mapField.getMapKeyRegistration());
GeneratePyUtils.pySerializer(mapField.getMapValueRegistration().serializer())
.writeObject(builder, value, deep + 2, field, mapField.getMapValueRegistration());
}
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
GeneratePyUtils.addTab(builder, deep);
var cutDown = CutDownMapSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Python);
if (cutDown != null) {
return cutDown;
}
MapField mapField = (MapField) fieldRegistration;
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("{} = {}", result)).append(LS);
GeneratePyUtils.addTab(builder, deep);
String size = "size" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("{} = buffer.readInt()", size)).append(LS);
GeneratePyUtils.addTab(builder, deep);
builder.append(StringUtils.format("if {} > 0:", size)).append(LS);
String i = "index" + GenerateProtocolFile.index.getAndIncrement();
GeneratePyUtils.addTab(builder, deep + 1);
builder.append(StringUtils.format("for {} in range({}):", i, size)).append(LS);
String keyObject = GeneratePyUtils.pySerializer(mapField.getMapKeyRegistration().serializer())
.readObject(builder, deep + 2, field, mapField.getMapKeyRegistration());
String valueObject = GeneratePyUtils.pySerializer(mapField.getMapValueRegistration().serializer())
.readObject(builder, deep + 2, field, mapField.getMapValueRegistration());
GeneratePyUtils.addTab(builder, deep + 2);
builder.append(StringUtils.format("{}[{}] = {}", result, keyObject, valueObject)).append(LS);
return result;
}
}
@@ -0,0 +1,51 @@
/*
* 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.python;
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 godotg
* @version 3.0
*/
public class PyObjectProtocolSerializer implements IPySerializer {
@Override
public String fieldDefaultValue(Field field, IFieldRegistration fieldRegistration) {
return "None";
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
GeneratePyUtils.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();
GeneratePyUtils.addTab(builder, deep);
builder.append(StringUtils.format("{} = buffer.readPacket({})", result, objectProtocolField.getProtocolId())).append(LS);
return result;
}
}
@@ -0,0 +1,92 @@
/*
* 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.python;
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 godotg
* @version 3.0
*/
public class PySetSerializer implements IPySerializer {
@Override
public String fieldDefaultValue(Field field, IFieldRegistration fieldRegistration) {
return "{}";
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GeneratePyUtils.addTab(builder, deep);
if (CutDownSetSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Python)) {
return;
}
SetField setField = (SetField) fieldRegistration;
builder.append(StringUtils.format("if {} is None:", objectStr)).append(LS);
GeneratePyUtils.addTab(builder, deep + 1);
builder.append("buffer.writeInt(0)").append(LS);
GeneratePyUtils.addTab(builder, deep);
builder.append("else:").append(LS);
GeneratePyUtils.addTab(builder, deep + 1);
builder.append(StringUtils.format("buffer.writeInt(len({}))", objectStr)).append(LS);
String element = "element" + GenerateProtocolFile.index.getAndIncrement();
GeneratePyUtils.addTab(builder, deep + 1);
builder.append(StringUtils.format("for {} in {}:", element, objectStr)).append(LS);
GeneratePyUtils.pySerializer(setField.getSetElementRegistration().serializer())
.writeObject(builder, element, deep + 2, field, setField.getSetElementRegistration());
}
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
GeneratePyUtils.addTab(builder, deep);
var cutDown = CutDownSetSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Python);
if (cutDown != null) {
return cutDown;
}
SetField setField = (SetField) fieldRegistration;
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("{} = []", result)).append(LS);
String i = "index" + GenerateProtocolFile.index.getAndIncrement();
String size = "size" + GenerateProtocolFile.index.getAndIncrement();
GeneratePyUtils.addTab(builder, deep);
builder.append(StringUtils.format("{} = buffer.readInt()", size)).append(LS);
GeneratePyUtils.addTab(builder, deep);
builder.append(StringUtils.format("if {} > 0:", size)).append(LS);
GeneratePyUtils.addTab(builder, deep + 1);
builder.append(StringUtils.format("for {} in range({}):", i, size)).append(LS);
String readObject = GeneratePyUtils.pySerializer(setField.getSetElementRegistration().serializer())
.readObject(builder, deep + 2, field, setField.getSetElementRegistration());
GeneratePyUtils.addTab(builder, deep + 2);
builder.append(StringUtils.format("{}.append({})", result, readObject)).append(LS);
return result;
}
}
@@ -0,0 +1,49 @@
/*
* 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.python;
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 godotg
* @version 3.0
*/
public class PyShortSerializer implements IPySerializer {
@Override
public String fieldDefaultValue(Field field, IFieldRegistration fieldRegistration) {
return "0";
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GeneratePyUtils.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();
GeneratePyUtils.addTab(builder, deep);
builder.append(StringUtils.format("{} = buffer.readShort()", result)).append(LS);
return result;
}
}
@@ -0,0 +1,48 @@
/*
* 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.python;
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 godotg
* @version 3.0
*/
public class PyStringSerializer implements IPySerializer {
@Override
public String fieldDefaultValue(Field field, IFieldRegistration fieldRegistration) {
return "\"\"";
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GeneratePyUtils.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();
GeneratePyUtils.addTab(builder, deep);
builder.append(StringUtils.format("{} = buffer.readString()", result)).append(LS);
return result;
}
}
@@ -96,14 +96,14 @@ public abstract class GenerateTsUtils {
}
// 生成ProtocolManager.ts文件
var protocolManagerTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("typescript/ProtocolManagerTemplate.ts")));
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("typescript/ProtocolManagerTemplate.ts");
var importBuilder = new StringBuilder();
var initProtocolBuilder = new StringBuilder();
for (var protocol : protocolList) {
var protocolId = protocol.protocolId();
var name = protocol.protocolConstructor().getDeclaringClass().getSimpleName();
var path = GenerateProtocolPath.protocolAbsolutePath(protocolId, CodeLanguage.GdScript);
var path = GenerateProtocolPath.protocolAbsolutePath(protocolId, CodeLanguage.TypeScript);
importBuilder.append(StringUtils.format("import {} from './{}';", name, path)).append(LS);
initProtocolBuilder.append(StringUtils.format("protocols.set({}, {});", protocolId, name)).append(LS);
@@ -121,7 +121,7 @@ public abstract class GenerateTsUtils {
var registrationConstructor = registration.getConstructor();
var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName();
var protocolTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("typescript/ProtocolTemplate.ts")));
var protocolTemplate = ClassUtils.getFileFromClassPathToString("typescript/ProtocolTemplate.ts");
var importSubProtocol = importSubProtocol(registration);
var classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.TypeScript);
@@ -16,7 +16,6 @@ import com.zfoo.protocol.collection.ArrayUtils;
import java.nio.charset.Charset;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
@@ -80,7 +79,6 @@ public abstract class StringUtils {
public static final String EMPTY_JSON = "{}";
public static final String SEMICOLON_COMMA = ";|,"; //分号或者逗号
public static final String MULTIPLE_HYPHENS = "-----------------------------------------------------------------------";
@@ -485,15 +483,4 @@ public abstract class StringUtils {
return Character.isWhitespace(ch) || STOP_WORD.contains(ch);
}
/**
* 判断是否是正数
* @param str
* @return
*/
public static boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
return isNum.matches();
}
}
@@ -18,7 +18,7 @@ static func write(buffer, packet):
protocol.write(buffer, packet)
static func read(buffer):
var protocolId = buffer.readShort();
var protocolId = buffer.readShort()
var protocol = protocols[protocolId]
var packet = protocol.read(buffer);
return packet;
var packet = protocol.read(buffer)
return packet
@@ -277,7 +277,7 @@ func writeBooleanArray(array):
if (array == null):
writeInt(0)
else:
writeInt(array.size());
writeInt(array.size())
for element in array:
writeBool(element)
@@ -293,7 +293,7 @@ func writeByteArray(array):
if (array == null):
writeInt(0)
else:
writeInt(array.size());
writeInt(array.size())
for element in array:
writeByte(element)
@@ -309,7 +309,7 @@ func writeShortArray(array):
if (array == null):
writeInt(0)
else:
writeInt(array.size());
writeInt(array.size())
for element in array:
writeShort(element)
@@ -325,7 +325,7 @@ func writeIntArray(array):
if (array == null):
writeInt(0)
else:
writeInt(array.size());
writeInt(array.size())
for element in array:
writeInt(element)
@@ -341,7 +341,7 @@ func writeLongArray(array):
if (array == null):
writeInt(0)
else:
writeInt(array.size());
writeInt(array.size())
for element in array:
writeLong(element)
@@ -357,7 +357,7 @@ func writeFloatArray(array):
if (array == null):
writeInt(0)
else:
writeInt(array.size());
writeInt(array.size())
for element in array:
writeFloat(element)
@@ -373,7 +373,7 @@ func writeDoubleArray(array):
if (array == null):
writeInt(0)
else:
writeInt(array.size());
writeInt(array.size())
for element in array:
writeDouble(element)
@@ -389,7 +389,7 @@ func writeCharArray(array):
if (array == null):
writeInt(0)
else:
writeInt(array.size());
writeInt(array.size())
for element in array:
writeChar(element)
@@ -405,7 +405,7 @@ func writeStringArray(array):
if (array == null):
writeInt(0)
else:
writeInt(array.size());
writeInt(array.size())
for element in array:
writeString(element)
@@ -423,7 +423,7 @@ func writePacketArray(array, protocolId):
writeInt(0)
else:
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
writeInt(array.size());
writeInt(array.size())
for element in array:
protocolRegistration.write(self, element)
@@ -0,0 +1,907 @@
import struct
from . import ProtocolManager
maxSize = 655537
maxInt = 2147483647
minInt = -2147483648
maxLong = 9223372036854775807
minLong = -9223372036854775808
empty_str = ""
class ByteBuffer():
writeOffset = 0
readOffset = 0
buffer = bytearray(256)
def setWriteOffset(self, writeOffset):
if writeOffset > len(self.buffer):
raise ValueError("index out of bounds exception:readerIndex:" + str(self.readOffset) +
", writerIndex:" + str(self.writeOffset) +
"(expected:0 <= readerIndex <= writerIndex <= capacity:" + str(len(self.buffer)))
self.writeOffset = writeOffset
def setReadOffset(self, readOffset):
if readOffset > self.writeOffset:
raise ValueError("index out of bounds exception:readerIndex:" + str(self.readOffset) +
", writerIndex:" + str(self.writeOffset) +
"(expected:0 <= readerIndex <= writerIndex <= capacity:" + str(len(self.buffer)))
self.readOffset = readOffset
def isReadable(self):
return self.writeOffset > self.readOffset
pass
def getCapacity(self):
return len(self.buffer) - self.writeOffset
def ensureCapacity(self, minCapacity):
while (minCapacity - self.getCapacity() > 0):
newSize = len(self.buffer) * 2
if newSize > maxSize:
raise ValueError("out of memory error")
# auto grow capacity
self.buffer.extend(bytearray(len(self.buffer)))
def clear(self):
self.writeOffset = 0
self.readOffset = 0
self.buffer = bytearray(256)
pass
def toBytes(self):
return self.buffer[0:self.writeOffset]
def writeBool(self, value):
if value:
self.writeByte(1)
else:
self.writeByte(0)
def readBool(self):
if self.readByte():
return True
else:
return False
def writeByte(self, value):
self.ensureCapacity(1)
struct.pack_into('>b', self.buffer, self.writeOffset, value)
self.writeOffset += 1
def readByte(self):
value = struct.unpack_from('>b', self.buffer, self.readOffset)[0]
self.readOffset += 1
return value
def writeBytes(self, value):
length = len(value)
self.ensureCapacity(length)
self.buffer[self.writeOffset:length] = value[:]
self.writeOffset += length
def writeUByte(self, value):
self.ensureCapacity(1)
struct.pack_into('>B', self.buffer, self.writeOffset, value)
self.writeOffset += 1
def readUByte(self):
value = struct.unpack_from('>B', self.buffer, self.readOffset)[0]
self.readOffset += 1
return value
def writeShort(self, value):
self.ensureCapacity(2)
struct.pack_into('>h', self.buffer, self.writeOffset, value)
self.writeOffset += 2
def readShort(self):
value = struct.unpack_from('>h', self.buffer, self.readOffset)[0]
self.readOffset += 2
return value
def writeRawInt(self, value):
self.ensureCapacity(4)
struct.pack_into('>i', self.buffer, self.writeOffset, value)
self.writeOffset += 4
def readRawInt(self):
value = struct.unpack_from('>i', self.buffer, self.readOffset)[0]
self.readOffset += 4
return value
def writeInt(self, value):
if not (minInt <= value <= maxInt):
raise ValueError('value must range between minInt:-2147483648 and maxInt:2147483647')
value = (value << 1) ^ (value >> 31)
self.ensureCapacity(5)
if value >> 7 == 0:
self.writeUByte(value)
return
if value >> 14 == 0:
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte((value >> 7) & 0x7F)
return
if value >> 21 == 0:
self.writeUByte((value & 0x7F) | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte((value >> 14) & 0x7F)
return
if value >> 28 == 0:
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte(((value >> 14) & 0x7F | 0x80))
self.writeUByte((value >> 21) & 0x7F)
return
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte(((value >> 14) & 0x7F | 0x80))
self.writeUByte(((value >> 21) & 0x7F | 0x80))
self.writeUByte((value >> 28) & 0x7F)
pass
def readInt(self):
b = self.readUByte()
value = b & 0x7F
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 7
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 14
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 21
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 28
return (value >> 1) ^ -(value & 1)
def writeLong(self, value):
if not (minLong <= value <= maxLong):
raise ValueError('value must range between minLong:-9223372036854775808 and maxLong:9223372036854775807')
value = (value << 1) ^ (value >> 63)
self.ensureCapacity(9)
if value >> 7 == 0:
self.writeUByte(value)
return
if value >> 14 == 0:
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte((value >> 7) & 0x7F)
return
if value >> 21 == 0:
self.writeUByte((value & 0x7F) | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte((value >> 14) & 0x7F)
return
if value >> 28 == 0:
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte(((value >> 14) & 0x7F | 0x80))
self.writeUByte((value >> 21) & 0x7F)
return
if value >> 35 == 0:
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte(((value >> 14) & 0x7F | 0x80))
self.writeUByte(((value >> 21) & 0x7F | 0x80))
self.writeUByte((value >> 28) & 0x7F)
return
if value >> 42 == 0:
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte(((value >> 14) & 0x7F | 0x80))
self.writeUByte(((value >> 21) & 0x7F | 0x80))
self.writeUByte(((value >> 28) & 0x7F | 0x80))
self.writeUByte((value >> 35) & 0x7F)
return
if value >> 49 == 0:
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte(((value >> 14) & 0x7F | 0x80))
self.writeUByte(((value >> 21) & 0x7F | 0x80))
self.writeUByte(((value >> 28) & 0x7F | 0x80))
self.writeUByte(((value >> 35) & 0x7F | 0x80))
self.writeUByte((value >> 42) & 0x7F)
return
if value >> 56 == 0:
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte(((value >> 14) & 0x7F | 0x80))
self.writeUByte(((value >> 21) & 0x7F | 0x80))
self.writeUByte(((value >> 28) & 0x7F | 0x80))
self.writeUByte(((value >> 35) & 0x7F | 0x80))
self.writeUByte(((value >> 42) & 0x7F | 0x80))
self.writeUByte((value >> 49) & 0x7F)
return
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte(((value >> 14) & 0x7F | 0x80))
self.writeUByte(((value >> 21) & 0x7F | 0x80))
self.writeUByte(((value >> 28) & 0x7F | 0x80))
self.writeUByte(((value >> 35) & 0x7F | 0x80))
self.writeUByte(((value >> 42) & 0x7F | 0x80))
self.writeUByte(((value >> 49) & 0x7F | 0x80))
self.writeUByte(value >> 56)
pass
def readLong(self):
b = self.readUByte()
value = b & 0x7F
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 7
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 14
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 21
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 28
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 35
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 42
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 49
if (b & 0x80) != 0:
b = self.readUByte()
value |= b << 56
return (value >> 1) ^ -(value & 1)
def writeFloat(self, value):
self.ensureCapacity(4)
struct.pack_into('>f', self.buffer, self.writeOffset, value)
self.writeOffset += 4
def readFloat(self):
value = struct.unpack_from('>f', self.buffer, self.readOffset)[0]
self.readOffset += 4
return value
def writeDouble(self, value):
self.ensureCapacity(8)
struct.pack_into('>d', self.buffer, self.writeOffset, value)
self.writeOffset += 8
def readDouble(self):
value = struct.unpack_from('>d', self.buffer, self.readOffset)[0]
self.readOffset += 8
return value
def writeChar(self, value):
if len(value) == 0:
self.writeInt(0)
return
self.writeString(value[0])
def readChar(self):
return self.readString()
def writeString(self, value):
if len(value) == 0:
self.writeInt(0)
return
byte_array = bytearray(value, 'utf-8')
length = len(byte_array)
self.ensureCapacity(5 + length)
self.writeInt(length)
self.buffer[self.writeOffset:self.writeOffset + length] = byte_array[:]
self.writeOffset += length
def readString(self):
length = self.readInt()
if (length <= 0):
return empty_str
byte_array = self.buffer[self.readOffset:self.readOffset + length]
# 使用utf-8编码进行解码
value = byte_array.decode('utf-8')
self.readOffset += length
return value
def writePacketFlag(self, value):
if value is None:
self.writeBool(False)
else:
self.writeBool(True)
pass
def writePacket(self, packet, protocolId):
protocolRegistration = ProtocolManager.getProtocol(protocolId)
protocolRegistration.write(self, packet)
def readPacket(self, protocolId):
protocolRegistration = ProtocolManager.getProtocol(protocolId)
return protocolRegistration.read(self)
def writeBooleanArray(self, array):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writeBool(element)
pass
def readBooleanArray(self):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readBool())
return array
def writeByteArray(self, array):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writeByte(element)
pass
def readByteArray(self):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readByte())
return array
def writeShortArray(self, array):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writeShort(element)
pass
def readShortArray(self):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readShort())
return array
def writeIntArray(self, array):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writeInt(element)
pass
def readIntArray(self):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readInt())
return array
def writeLongArray(self, array):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writeLong(element)
pass
def readLongArray(self):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readLong())
return array
def writeFloatArray(self, array):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writeFloat(element)
pass
def readFloatArray(self):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readFloat())
return array
def writeDoubleArray(self, array):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writeDouble(element)
pass
def readDoubleArray(self):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readDouble())
return array
def writeCharArray(self, array):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writeChar(element)
pass
def readCharArray(self):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readChar())
return array
def writeStringArray(self, array):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writeString(element)
pass
def readStringArray(self):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readString())
return array
def writePacketArray(self, array, protocolId):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writePacket(element, protocolId)
pass
def readPacketArray(self, protocolId):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readPacket(protocolId))
return array
def writeIntIntMap(self, map):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeInt(key)
self.writeInt(value)
pass
def readIntIntMap(self):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readInt()
value = self.readInt()
map[key] = value
return map
def writeIntLongMap(self, map):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeInt(key)
self.writeLong(value)
pass
def readIntLongMap(self):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readInt()
value = self.readLong()
map[key] = value
return map
def writeIntStringMap(self, map):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeInt(key)
self.writeString(value)
pass
def readIntStringMap(self):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readInt()
value = self.readString()
map[key] = value
return map
def writeIntPacketMap(self, map, protocolId):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeInt(key)
self.writePacket(value, protocolId)
pass
def readIntPacketMap(self, protocolId):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readInt()
value = self.readPacket(protocolId)
map[key] = value
return map
def writeLongIntMap(self, map):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeLong(key)
self.writeInt(value)
pass
def readLongIntMap(self):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readLong()
value = self.readInt()
map[key] = value
return map
def writeLongLongMap(self, map):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeLong(key)
self.writeLong(value)
pass
def readLongLongMap(self):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readLong()
value = self.readLong()
map[key] = value
return map
def writeLongStringMap(self, map):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeLong(key)
self.writeString(value)
pass
def readLongStringMap(self):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readLong()
value = self.readString()
map[key] = value
return map
def writeLongPacketMap(self, map, protocolId):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeLong(key)
self.writePacket(value, protocolId)
pass
def readLongPacketMap(self, protocolId):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readLong()
value = self.readPacket(protocolId)
map[key] = value
return map
def writeStringIntMap(self, map):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeString(key)
self.writeInt(value)
pass
def readStringIntMap(self):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readString()
value = self.readInt()
map[key] = value
return map
def writeStringLongMap(self, map):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeString(key)
self.writeLong(value)
pass
def readStringLongMap(self):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readString()
value = self.readLong()
map[key] = value
return map
def writeStringStringMap(self, map):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeString(key)
self.writeString(value)
pass
def readStringStringMap(self):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readString()
value = self.readString()
map[key] = value
return map
def writeStringPacketMap(self, map, protocolId):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeString(key)
self.writePacket(value, protocolId)
pass
def readStringPacketMap(self, protocolId):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readString()
value = self.readPacket(protocolId)
map[key] = value
return map
def writeBooleanSet(self, value):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writeBool(element)
pass
def readBooleanSet(self):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readBool())
return value
def writeByteSet(self, value):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writeByte(element)
pass
def readByteSet(self):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readByte())
return value
def writeShortSet(self, value):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writeShort(element)
pass
def readShortSet(self):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readShort())
return value
def writeIntSet(self, value):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writeInt(element)
pass
def readIntSet(self):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readInt())
return value
def writeLongSet(self, value):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writeLong(element)
pass
def readLongSet(self):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readLong())
return value
def writeFloatSet(self, value):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writeFloat(element)
pass
def readFloatSet(self):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readFloat())
return value
def writeDoubleSet(self, value):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writeDouble(element)
pass
def readDoubleSet(self):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readDouble())
return value
def writeCharSet(self, value):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writeChar(element)
pass
def readCharValue(self):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readChar())
return value
def writeStringSet(self, value):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writeString(element)
pass
def readStringSet(self):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readString())
return value
def writePacketSet(self, value, protocolId):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writePacket(element, protocolId)
pass
def readPacketSet(self, protocolId):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readPacket(protocolId))
return value
@@ -0,0 +1,20 @@
{}
protocols = {}
{}
def getProtocol(protocolId):
return protocols[protocolId]
def write(buffer, packet):
protocolId = packet.protocolId()
buffer.writeShort(protocolId)
protocol = protocols[protocolId]
protocol.write(buffer, packet)
def read(buffer):
protocolId = buffer.readShort()
protocol = protocols[protocolId]
packet = protocol.read(buffer)
return packet
@@ -0,0 +1,23 @@
{}
class {}:
{}
def protocolId(self):
return {}
@classmethod
def write(cls, buffer, packet):
if buffer.writePacketFlag(packet):
return
{}
pass
@classmethod
def read(cls, buffer):
if not buffer.readBool():
return None
packet = {}()
{}
return packet
@@ -23,6 +23,7 @@ import com.google.protobuf.CodedOutputStream;
import com.zfoo.protocol.collection.ArrayUtils;
import com.zfoo.protocol.generate.GenerateOperation;
import com.zfoo.protocol.packet.*;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.util.StringUtils;
import io.fury.Fury;
import io.fury.Language;
@@ -375,6 +376,7 @@ public class SpeedTest {
// op.getGenerateLanguages().add(CodeLanguage.Lua);
// op.getGenerateLanguages().add(CodeLanguage.CSharp);
// op.getGenerateLanguages().add(CodeLanguage.GdScript);
op.getGenerateLanguages().add(CodeLanguage.Python);
// 需要protocol协议的字段里面都加上JProtobuf注解才能用
// op.setProtocolParam("protobuf=protobuf.xml");
+96
View File
@@ -0,0 +1,96 @@
# encoding: UTF-8
from unittest import TestCase
import struct
from pyProtocol import ProtocolManager
from pyProtocol import ByteBuffer
def print_bytearray(array):
signed_byte_array = struct.unpack('b' * len(array), array)
for byte in signed_byte_array:
print(byte) # 以字节形式输出每个字节的值
class ByteBufferTestCase(TestCase):
def test_complex_object(self):
# 打开文件并读取内容
with open('NormalObject.bytes', 'rb') as file:
content = file.read()
# 将内容转为bytearray
byte_array = bytearray(content)
buffer = ByteBuffer.ByteBuffer()
buffer.writeBytes(byte_array)
obj = ProtocolManager.read(buffer)
buffer.clear()
ProtocolManager.write(buffer, obj)
newObj = ProtocolManager.read(buffer)
bytes = buffer.toBytes()
# 打印bytearray
print(byte_array)
pass
def test_bytearray(self):
buffer = bytearray(10)
for byte in buffer:
print(byte) # 以字节形式输出每个字节的值
pass
def test_buffer_raw_int(self):
byteBuffer = ByteBuffer.ByteBuffer()
byteBuffer.writeRawInt(2147483647)
self.assertEqual(byteBuffer.readRawInt(), 2147483647)
def test_buffer_int(self):
byteBuffer = ByteBuffer.ByteBuffer()
byteBuffer.writeInt(2147483647)
self.assertEqual(byteBuffer.readInt(), 2147483647)
byteBuffer.writeInt(-2147483648)
self.assertEqual(byteBuffer.readInt(), -2147483648)
pass
def test_buffer_long(self):
byteBuffer = ByteBuffer.ByteBuffer()
byteBuffer.writeLong(9223372036854775807)
self.assertEqual(byteBuffer.readLong(), 9223372036854775807)
print_bytearray(byteBuffer.buffer)
byteBuffer.writeLong(-9223372036854775808)
self.assertEqual(byteBuffer.readLong(), -9223372036854775808)
pass
def test_buffer(self):
byteBuffer = ByteBuffer.ByteBuffer()
print(byteBuffer.writeOffset)
print(byteBuffer.readOffset)
print(byteBuffer.getCapacity())
byteBuffer.writeBool(True)
self.assertEqual(byteBuffer.readBool(), True)
byteBuffer.writeByte(-100)
self.assertEqual(byteBuffer.readByte(), -100)
byteBuffer.writeShort(100)
self.assertEqual(byteBuffer.readShort(), 100)
byteBuffer.writeInt(9999)
self.assertEqual(byteBuffer.readInt(), 9999)
byteBuffer.writeInt(-9999)
self.assertEqual(byteBuffer.readInt(), -9999)
byteBuffer.writeLong(9999)
self.assertEqual(byteBuffer.readLong(), 9999)
byteBuffer.writeLong(-9999)
self.assertEqual(byteBuffer.readLong(), -9999)
byteBuffer.writeFloat(99.9)
self.assertTrue(abs(byteBuffer.readFloat() - 99.9) < 0.001)
str = "Hello World!你好"
byteBuffer.writeString(str)
self.assertEqual(byteBuffer.readString(), str)
charStr = "A"
byteBuffer.writeChar(charStr)
self.assertEqual(byteBuffer.readChar(), charStr)
print("----------------------------------------------------------------")
print_bytearray(byteBuffer.buffer)
@@ -0,0 +1,907 @@
import struct
from . import ProtocolManager
maxSize = 655537
maxInt = 2147483647
minInt = -2147483648
maxLong = 9223372036854775807
minLong = -9223372036854775808
empty_str = ""
class ByteBuffer():
writeOffset = 0
readOffset = 0
buffer = bytearray(256)
def setWriteOffset(self, writeOffset):
if writeOffset > len(self.buffer):
raise ValueError("index out of bounds exception:readerIndex:" + str(self.readOffset) +
", writerIndex:" + str(self.writeOffset) +
"(expected:0 <= readerIndex <= writerIndex <= capacity:" + str(len(self.buffer)))
self.writeOffset = writeOffset
def setReadOffset(self, readOffset):
if readOffset > self.writeOffset:
raise ValueError("index out of bounds exception:readerIndex:" + str(self.readOffset) +
", writerIndex:" + str(self.writeOffset) +
"(expected:0 <= readerIndex <= writerIndex <= capacity:" + str(len(self.buffer)))
self.readOffset = readOffset
def isReadable(self):
return self.writeOffset > self.readOffset
pass
def getCapacity(self):
return len(self.buffer) - self.writeOffset
def ensureCapacity(self, minCapacity):
while (minCapacity - self.getCapacity() > 0):
newSize = len(self.buffer) * 2
if newSize > maxSize:
raise ValueError("out of memory error")
# auto grow capacity
self.buffer.extend(bytearray(len(self.buffer)))
def clear(self):
self.writeOffset = 0
self.readOffset = 0
self.buffer = bytearray(256)
pass
def toBytes(self):
return self.buffer[0:self.writeOffset]
def writeBool(self, value):
if value:
self.writeByte(1)
else:
self.writeByte(0)
def readBool(self):
if self.readByte():
return True
else:
return False
def writeByte(self, value):
self.ensureCapacity(1)
struct.pack_into('>b', self.buffer, self.writeOffset, value)
self.writeOffset += 1
def readByte(self):
value = struct.unpack_from('>b', self.buffer, self.readOffset)[0]
self.readOffset += 1
return value
def writeBytes(self, value):
length = len(value)
self.ensureCapacity(length)
self.buffer[self.writeOffset:length] = value[:]
self.writeOffset += length
def writeUByte(self, value):
self.ensureCapacity(1)
struct.pack_into('>B', self.buffer, self.writeOffset, value)
self.writeOffset += 1
def readUByte(self):
value = struct.unpack_from('>B', self.buffer, self.readOffset)[0]
self.readOffset += 1
return value
def writeShort(self, value):
self.ensureCapacity(2)
struct.pack_into('>h', self.buffer, self.writeOffset, value)
self.writeOffset += 2
def readShort(self):
value = struct.unpack_from('>h', self.buffer, self.readOffset)[0]
self.readOffset += 2
return value
def writeRawInt(self, value):
self.ensureCapacity(4)
struct.pack_into('>i', self.buffer, self.writeOffset, value)
self.writeOffset += 4
def readRawInt(self):
value = struct.unpack_from('>i', self.buffer, self.readOffset)[0]
self.readOffset += 4
return value
def writeInt(self, value):
if not (minInt <= value <= maxInt):
raise ValueError('value must range between minInt:-2147483648 and maxInt:2147483647')
value = (value << 1) ^ (value >> 31)
self.ensureCapacity(5)
if value >> 7 == 0:
self.writeUByte(value)
return
if value >> 14 == 0:
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte((value >> 7) & 0x7F)
return
if value >> 21 == 0:
self.writeUByte((value & 0x7F) | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte((value >> 14) & 0x7F)
return
if value >> 28 == 0:
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte(((value >> 14) & 0x7F | 0x80))
self.writeUByte((value >> 21) & 0x7F)
return
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte(((value >> 14) & 0x7F | 0x80))
self.writeUByte(((value >> 21) & 0x7F | 0x80))
self.writeUByte((value >> 28) & 0x7F)
pass
def readInt(self):
b = self.readUByte()
value = b & 0x7F
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 7
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 14
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 21
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 28
return (value >> 1) ^ -(value & 1)
def writeLong(self, value):
if not (minLong <= value <= maxLong):
raise ValueError('value must range between minLong:-9223372036854775808 and maxLong:9223372036854775807')
value = (value << 1) ^ (value >> 63)
self.ensureCapacity(9)
if value >> 7 == 0:
self.writeUByte(value)
return
if value >> 14 == 0:
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte((value >> 7) & 0x7F)
return
if value >> 21 == 0:
self.writeUByte((value & 0x7F) | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte((value >> 14) & 0x7F)
return
if value >> 28 == 0:
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte(((value >> 14) & 0x7F | 0x80))
self.writeUByte((value >> 21) & 0x7F)
return
if value >> 35 == 0:
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte(((value >> 14) & 0x7F | 0x80))
self.writeUByte(((value >> 21) & 0x7F | 0x80))
self.writeUByte((value >> 28) & 0x7F)
return
if value >> 42 == 0:
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte(((value >> 14) & 0x7F | 0x80))
self.writeUByte(((value >> 21) & 0x7F | 0x80))
self.writeUByte(((value >> 28) & 0x7F | 0x80))
self.writeUByte((value >> 35) & 0x7F)
return
if value >> 49 == 0:
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte(((value >> 14) & 0x7F | 0x80))
self.writeUByte(((value >> 21) & 0x7F | 0x80))
self.writeUByte(((value >> 28) & 0x7F | 0x80))
self.writeUByte(((value >> 35) & 0x7F | 0x80))
self.writeUByte((value >> 42) & 0x7F)
return
if value >> 56 == 0:
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte(((value >> 14) & 0x7F | 0x80))
self.writeUByte(((value >> 21) & 0x7F | 0x80))
self.writeUByte(((value >> 28) & 0x7F | 0x80))
self.writeUByte(((value >> 35) & 0x7F | 0x80))
self.writeUByte(((value >> 42) & 0x7F | 0x80))
self.writeUByte((value >> 49) & 0x7F)
return
self.writeUByte(value & 0x7F | 0x80)
self.writeUByte(((value >> 7) & 0x7F | 0x80))
self.writeUByte(((value >> 14) & 0x7F | 0x80))
self.writeUByte(((value >> 21) & 0x7F | 0x80))
self.writeUByte(((value >> 28) & 0x7F | 0x80))
self.writeUByte(((value >> 35) & 0x7F | 0x80))
self.writeUByte(((value >> 42) & 0x7F | 0x80))
self.writeUByte(((value >> 49) & 0x7F | 0x80))
self.writeUByte(value >> 56)
pass
def readLong(self):
b = self.readUByte()
value = b & 0x7F
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 7
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 14
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 21
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 28
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 35
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 42
if (b & 0x80) != 0:
b = self.readUByte()
value |= (b & 0x7F) << 49
if (b & 0x80) != 0:
b = self.readUByte()
value |= b << 56
return (value >> 1) ^ -(value & 1)
def writeFloat(self, value):
self.ensureCapacity(4)
struct.pack_into('>f', self.buffer, self.writeOffset, value)
self.writeOffset += 4
def readFloat(self):
value = struct.unpack_from('>f', self.buffer, self.readOffset)[0]
self.readOffset += 4
return value
def writeDouble(self, value):
self.ensureCapacity(8)
struct.pack_into('>d', self.buffer, self.writeOffset, value)
self.writeOffset += 8
def readDouble(self):
value = struct.unpack_from('>d', self.buffer, self.readOffset)[0]
self.readOffset += 8
return value
def writeChar(self, value):
if len(value) == 0:
self.writeInt(0)
return
self.writeString(value[0])
def readChar(self):
return self.readString()
def writeString(self, value):
if len(value) == 0:
self.writeInt(0)
return
byte_array = bytearray(value, 'utf-8')
length = len(byte_array)
self.ensureCapacity(5 + length)
self.writeInt(length)
self.buffer[self.writeOffset:self.writeOffset + length] = byte_array[:]
self.writeOffset += length
def readString(self):
length = self.readInt()
if (length <= 0):
return empty_str
byte_array = self.buffer[self.readOffset:self.readOffset + length]
# 使用utf-8编码进行解码
value = byte_array.decode('utf-8')
self.readOffset += length
return value
def writePacketFlag(self, value):
if value is None:
self.writeBool(False)
else:
self.writeBool(True)
pass
def writePacket(self, packet, protocolId):
protocolRegistration = ProtocolManager.getProtocol(protocolId)
protocolRegistration.write(self, packet)
def readPacket(self, protocolId):
protocolRegistration = ProtocolManager.getProtocol(protocolId)
return protocolRegistration.read(self)
def writeBooleanArray(self, array):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writeBool(element)
pass
def readBooleanArray(self):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readBool())
return array
def writeByteArray(self, array):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writeByte(element)
pass
def readByteArray(self):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readByte())
return array
def writeShortArray(self, array):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writeShort(element)
pass
def readShortArray(self):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readShort())
return array
def writeIntArray(self, array):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writeInt(element)
pass
def readIntArray(self):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readInt())
return array
def writeLongArray(self, array):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writeLong(element)
pass
def readLongArray(self):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readLong())
return array
def writeFloatArray(self, array):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writeFloat(element)
pass
def readFloatArray(self):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readFloat())
return array
def writeDoubleArray(self, array):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writeDouble(element)
pass
def readDoubleArray(self):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readDouble())
return array
def writeCharArray(self, array):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writeChar(element)
pass
def readCharArray(self):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readChar())
return array
def writeStringArray(self, array):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writeString(element)
pass
def readStringArray(self):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readString())
return array
def writePacketArray(self, array, protocolId):
if array is None:
self.writeInt(0)
else:
self.writeInt(len(array))
for element in array:
self.writePacket(element, protocolId)
pass
def readPacketArray(self, protocolId):
array = []
size = self.readInt()
if size > 0:
for index in range(size):
array.append(self.readPacket(protocolId))
return array
def writeIntIntMap(self, map):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeInt(key)
self.writeInt(value)
pass
def readIntIntMap(self):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readInt()
value = self.readInt()
map[key] = value
return map
def writeIntLongMap(self, map):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeInt(key)
self.writeLong(value)
pass
def readIntLongMap(self):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readInt()
value = self.readLong()
map[key] = value
return map
def writeIntStringMap(self, map):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeInt(key)
self.writeString(value)
pass
def readIntStringMap(self):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readInt()
value = self.readString()
map[key] = value
return map
def writeIntPacketMap(self, map, protocolId):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeInt(key)
self.writePacket(value, protocolId)
pass
def readIntPacketMap(self, protocolId):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readInt()
value = self.readPacket(protocolId)
map[key] = value
return map
def writeLongIntMap(self, map):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeLong(key)
self.writeInt(value)
pass
def readLongIntMap(self):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readLong()
value = self.readInt()
map[key] = value
return map
def writeLongLongMap(self, map):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeLong(key)
self.writeLong(value)
pass
def readLongLongMap(self):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readLong()
value = self.readLong()
map[key] = value
return map
def writeLongStringMap(self, map):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeLong(key)
self.writeString(value)
pass
def readLongStringMap(self):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readLong()
value = self.readString()
map[key] = value
return map
def writeLongPacketMap(self, map, protocolId):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeLong(key)
self.writePacket(value, protocolId)
pass
def readLongPacketMap(self, protocolId):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readLong()
value = self.readPacket(protocolId)
map[key] = value
return map
def writeStringIntMap(self, map):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeString(key)
self.writeInt(value)
pass
def readStringIntMap(self):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readString()
value = self.readInt()
map[key] = value
return map
def writeStringLongMap(self, map):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeString(key)
self.writeLong(value)
pass
def readStringLongMap(self):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readString()
value = self.readLong()
map[key] = value
return map
def writeStringStringMap(self, map):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeString(key)
self.writeString(value)
pass
def readStringStringMap(self):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readString()
value = self.readString()
map[key] = value
return map
def writeStringPacketMap(self, map, protocolId):
if map is None:
self.writeInt(0)
else:
self.writeInt(len(map))
for key, value in map.items():
self.writeString(key)
self.writePacket(value, protocolId)
pass
def readStringPacketMap(self, protocolId):
map = {}
size = self.readInt()
if size > 0:
for index in range(size):
key = self.readString()
value = self.readPacket(protocolId)
map[key] = value
return map
def writeBooleanSet(self, value):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writeBool(element)
pass
def readBooleanSet(self):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readBool())
return value
def writeByteSet(self, value):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writeByte(element)
pass
def readByteSet(self):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readByte())
return value
def writeShortSet(self, value):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writeShort(element)
pass
def readShortSet(self):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readShort())
return value
def writeIntSet(self, value):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writeInt(element)
pass
def readIntSet(self):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readInt())
return value
def writeLongSet(self, value):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writeLong(element)
pass
def readLongSet(self):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readLong())
return value
def writeFloatSet(self, value):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writeFloat(element)
pass
def readFloatSet(self):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readFloat())
return value
def writeDoubleSet(self, value):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writeDouble(element)
pass
def readDoubleSet(self):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readDouble())
return value
def writeCharSet(self, value):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writeChar(element)
pass
def readCharValue(self):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readChar())
return value
def writeStringSet(self, value):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writeString(element)
pass
def readStringSet(self):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readString())
return value
def writePacketSet(self, value, protocolId):
if value is None:
self.writeInt(0)
else:
self.writeInt(len(value))
for element in value:
self.writePacket(element, protocolId)
pass
def readPacketSet(self, protocolId):
value = set()
size = self.readInt()
if size > 0:
for index in range(size):
value.add(self.readPacket(protocolId))
return value
@@ -0,0 +1,411 @@
# 复杂的对象,包括了各种复杂的结构,数组,List,Set,Map
class ComplexObject:
# byte类型,最简单的整形
a = 0 # byte
# byte的包装类型,优先使用基础类型,包装类型会有装箱拆箱
aa = 0 # byte
# 数组类型
aaa = [] # byte[]
aaaa = [] # byte[]
b = 0 # short
bb = 0 # short
bbb = [] # short[]
bbbb = [] # short[]
c = 0 # int
cc = 0 # int
ccc = [] # int[]
cccc = [] # int[]
d = 0 # long
dd = 0 # long
ddd = [] # long[]
dddd = [] # long[]
e = 0.0 # float
ee = 0.0 # float
eee = [] # float[]
eeee = [] # float[]
f = 0.0 # double
ff = 0.0 # double
fff = [] # double[]
ffff = [] # double[]
g = False # bool
gg = False # bool
ggg = [] # bool[]
gggg = [] # bool[]
h = "" # char
hh = "" # char
hhh = [] # char[]
hhhh = [] # char[]
jj = "" # string
jjj = [] # string[]
kk = None # ObjectA
kkk = [] # ObjectA[]
l = [] # List<int>
ll = [] # List<List<List<int>>>
lll = [] # List<List<ObjectA>>
llll = [] # List<string>
lllll = [] # List<Dictionary<int, string>>
m = {} # Dictionary<int, string>
mm = {} # Dictionary<int, ObjectA>
mmm = {} # Dictionary<ObjectA, List<int>>
mmmm = {} # Dictionary<List<List<ObjectA>>, List<List<List<int>>>>
mmmmm = {} # Dictionary<List<Dictionary<int, string>>, HashSet<Dictionary<int, string>>>
s = {} # HashSet<int>
ss = {} # HashSet<HashSet<List<int>>>
sss = {} # HashSet<HashSet<ObjectA>>
ssss = {} # HashSet<string>
sssss = {} # HashSet<Dictionary<int, string>>
# 如果要修改协议并且兼容老协议,需要加上Compatible注解,按照增加的顺序添加order
myCompatible = 0 # int
myObject = None # ObjectA
def protocolId(self):
return 100
@classmethod
def write(cls, buffer, packet):
if buffer.writePacketFlag(packet):
return
buffer.writeByte(packet.a)
buffer.writeByte(packet.aa)
buffer.writeByteArray(packet.aaa)
buffer.writeByteArray(packet.aaaa)
buffer.writeShort(packet.b)
buffer.writeShort(packet.bb)
buffer.writeShortArray(packet.bbb)
buffer.writeShortArray(packet.bbbb)
buffer.writeInt(packet.c)
buffer.writeInt(packet.cc)
buffer.writeIntArray(packet.ccc)
buffer.writeIntArray(packet.cccc)
buffer.writeLong(packet.d)
buffer.writeLong(packet.dd)
buffer.writeLongArray(packet.ddd)
buffer.writeLongArray(packet.dddd)
buffer.writeFloat(packet.e)
buffer.writeFloat(packet.ee)
buffer.writeFloatArray(packet.eee)
buffer.writeFloatArray(packet.eeee)
buffer.writeDouble(packet.f)
buffer.writeDouble(packet.ff)
buffer.writeDoubleArray(packet.fff)
buffer.writeDoubleArray(packet.ffff)
buffer.writeBool(packet.g)
buffer.writeBool(packet.gg)
buffer.writeBooleanArray(packet.ggg)
buffer.writeBooleanArray(packet.gggg)
buffer.writeChar(packet.h)
buffer.writeChar(packet.hh)
buffer.writeCharArray(packet.hhh)
buffer.writeCharArray(packet.hhhh)
buffer.writeString(packet.jj)
buffer.writeStringArray(packet.jjj)
buffer.writePacket(packet.kk, 102)
buffer.writePacketArray(packet.kkk, 102)
buffer.writeIntArray(packet.l)
if packet.ll is None:
buffer.writeInt(0)
else:
buffer.writeInt(len(packet.ll))
for element0 in packet.ll:
if element0 is None:
buffer.writeInt(0)
else:
buffer.writeInt(len(element0))
for element1 in element0:
buffer.writeIntArray(element1)
if packet.lll is None:
buffer.writeInt(0)
else:
buffer.writeInt(len(packet.lll))
for element2 in packet.lll:
buffer.writePacketArray(element2, 102)
buffer.writeStringArray(packet.llll)
if packet.lllll is None:
buffer.writeInt(0)
else:
buffer.writeInt(len(packet.lllll))
for element3 in packet.lllll:
buffer.writeIntStringMap(element3)
buffer.writeIntStringMap(packet.m)
buffer.writeIntPacketMap(packet.mm, 102)
if packet.mmm is None:
buffer.writeInt(0)
else:
buffer.writeInt(len(packet.mmm))
for key4 in packet.mmm:
value5 = packet.mmm[key4]
buffer.writePacket(key4, 102)
buffer.writeIntArray(value5)
if packet.mmmm is None:
buffer.writeInt(0)
else:
buffer.writeInt(len(packet.mmmm))
for key6 in packet.mmmm:
value7 = packet.mmmm[key6]
if key6 is None:
buffer.writeInt(0)
else:
buffer.writeInt(len(key6))
for element8 in key6:
buffer.writePacketArray(element8, 102)
if value7 is None:
buffer.writeInt(0)
else:
buffer.writeInt(len(value7))
for element9 in value7:
if element9 is None:
buffer.writeInt(0)
else:
buffer.writeInt(len(element9))
for element10 in element9:
buffer.writeIntArray(element10)
if packet.mmmmm is None:
buffer.writeInt(0)
else:
buffer.writeInt(len(packet.mmmmm))
for key11 in packet.mmmmm:
value12 = packet.mmmmm[key11]
if key11 is None:
buffer.writeInt(0)
else:
buffer.writeInt(len(key11))
for element13 in key11:
buffer.writeIntStringMap(element13)
if value12 is None:
buffer.writeInt(0)
else:
buffer.writeInt(len(value12))
for element14 in value12:
buffer.writeIntStringMap(element14)
buffer.writeIntSet(packet.s)
if packet.ss is None:
buffer.writeInt(0)
else:
buffer.writeInt(len(packet.ss))
for element15 in packet.ss:
if element15 is None:
buffer.writeInt(0)
else:
buffer.writeInt(len(element15))
for element16 in element15:
buffer.writeIntArray(element16)
if packet.sss is None:
buffer.writeInt(0)
else:
buffer.writeInt(len(packet.sss))
for element17 in packet.sss:
buffer.writePacketSet(element17, 102)
buffer.writeStringSet(packet.ssss)
if packet.sssss is None:
buffer.writeInt(0)
else:
buffer.writeInt(len(packet.sssss))
for element18 in packet.sssss:
buffer.writeIntStringMap(element18)
buffer.writeInt(packet.myCompatible)
buffer.writePacket(packet.myObject, 102)
pass
@classmethod
def read(cls, buffer):
if not buffer.readBool():
return None
packet = ComplexObject()
result19 = buffer.readByte()
packet.a = result19
result20 = buffer.readByte()
packet.aa = result20
array21 = buffer.readByteArray()
packet.aaa = array21
array22 = buffer.readByteArray()
packet.aaaa = array22
result23 = buffer.readShort()
packet.b = result23
result24 = buffer.readShort()
packet.bb = result24
array25 = buffer.readShortArray()
packet.bbb = array25
array26 = buffer.readShortArray()
packet.bbbb = array26
result27 = buffer.readInt()
packet.c = result27
result28 = buffer.readInt()
packet.cc = result28
array29 = buffer.readIntArray()
packet.ccc = array29
array30 = buffer.readIntArray()
packet.cccc = array30
result31 = buffer.readLong()
packet.d = result31
result32 = buffer.readLong()
packet.dd = result32
array33 = buffer.readLongArray()
packet.ddd = array33
array34 = buffer.readLongArray()
packet.dddd = array34
result35 = buffer.readFloat()
packet.e = result35
result36 = buffer.readFloat()
packet.ee = result36
array37 = buffer.readFloatArray()
packet.eee = array37
array38 = buffer.readFloatArray()
packet.eeee = array38
result39 = buffer.readDouble()
packet.f = result39
result40 = buffer.readDouble()
packet.ff = result40
array41 = buffer.readDoubleArray()
packet.fff = array41
array42 = buffer.readDoubleArray()
packet.ffff = array42
result43 = buffer.readBool()
packet.g = result43
result44 = buffer.readBool()
packet.gg = result44
array45 = buffer.readBooleanArray()
packet.ggg = array45
array46 = buffer.readBooleanArray()
packet.gggg = array46
result47 = buffer.readChar()
packet.h = result47
result48 = buffer.readChar()
packet.hh = result48
array49 = buffer.readCharArray()
packet.hhh = array49
array50 = buffer.readCharArray()
packet.hhhh = array50
result51 = buffer.readString()
packet.jj = result51
array52 = buffer.readStringArray()
packet.jjj = array52
result53 = buffer.readPacket(102)
packet.kk = result53
array54 = buffer.readPacketArray(102)
packet.kkk = array54
list55 = buffer.readIntArray()
packet.l = list55
result56 = []
size58 = buffer.readInt()
if size58 > 0:
for index57 in range(size58):
result59 = []
size61 = buffer.readInt()
if size61 > 0:
for index60 in range(size61):
list62 = buffer.readIntArray()
result59.append(list62)
result56.append(result59)
packet.ll = result56
result63 = []
size65 = buffer.readInt()
if size65 > 0:
for index64 in range(size65):
list66 = buffer.readPacketArray(102)
result63.append(list66)
packet.lll = result63
list67 = buffer.readStringArray()
packet.llll = list67
result68 = []
size70 = buffer.readInt()
if size70 > 0:
for index69 in range(size70):
map71 = buffer.readIntStringMap()
result68.append(map71)
packet.lllll = result68
map72 = buffer.readIntStringMap()
packet.m = map72
map73 = buffer.readIntPacketMap(102)
packet.mm = map73
result74 = {}
size75 = buffer.readInt()
if size75 > 0:
for index76 in range(size75):
result77 = buffer.readPacket(102)
list78 = buffer.readIntArray()
result74[result77] = list78
packet.mmm = result74
result79 = {}
size80 = buffer.readInt()
if size80 > 0:
for index81 in range(size80):
result82 = []
size84 = buffer.readInt()
if size84 > 0:
for index83 in range(size84):
list85 = buffer.readPacketArray(102)
result82.append(list85)
result86 = []
size88 = buffer.readInt()
if size88 > 0:
for index87 in range(size88):
result89 = []
size91 = buffer.readInt()
if size91 > 0:
for index90 in range(size91):
list92 = buffer.readIntArray()
result89.append(list92)
result86.append(result89)
result79[result82] = result86
packet.mmmm = result79
result93 = {}
size94 = buffer.readInt()
if size94 > 0:
for index95 in range(size94):
result96 = []
size98 = buffer.readInt()
if size98 > 0:
for index97 in range(size98):
map99 = buffer.readIntStringMap()
result96.append(map99)
result100 = []
size102 = buffer.readInt()
if size102 > 0:
for index101 in range(size102):
map103 = buffer.readIntStringMap()
result100.append(map103)
result93[result96] = result100
packet.mmmmm = result93
set104 = buffer.readIntSet()
packet.s = set104
result105 = []
size107 = buffer.readInt()
if size107 > 0:
for index106 in range(size107):
result108 = []
size110 = buffer.readInt()
if size110 > 0:
for index109 in range(size110):
list111 = buffer.readIntArray()
result108.append(list111)
result105.append(result108)
packet.ss = result105
result112 = []
size114 = buffer.readInt()
if size114 > 0:
for index113 in range(size114):
set115 = buffer.readPacketSet(102)
result112.append(set115)
packet.sss = result112
set116 = buffer.readStringSet()
packet.ssss = set116
result117 = []
size119 = buffer.readInt()
if size119 > 0:
for index118 in range(size119):
map120 = buffer.readIntStringMap()
result117.append(map120)
packet.sssss = result117
if not buffer.isReadable():
return packet
pass
result121 = buffer.readInt()
packet.myCompatible = result121
if not buffer.isReadable():
return packet
pass
result122 = buffer.readPacket(102)
packet.myObject = result122
return packet
@@ -0,0 +1,92 @@
class NormalObject:
a = 0 # byte
aaa = [] # byte[]
b = 0 # short
c = 0 # int
d = 0 # long
e = 0.0 # float
f = 0.0 # double
g = False # bool
jj = "" # string
kk = None # ObjectA
l = [] # List<int>
ll = [] # List<long>
lll = [] # List<ObjectA>
llll = [] # List<string>
m = {} # Dictionary<int, string>
mm = {} # Dictionary<int, ObjectA>
s = {} # HashSet<int>
ssss = {} # HashSet<string>
def protocolId(self):
return 101
@classmethod
def write(cls, buffer, packet):
if buffer.writePacketFlag(packet):
return
buffer.writeByte(packet.a)
buffer.writeByteArray(packet.aaa)
buffer.writeShort(packet.b)
buffer.writeInt(packet.c)
buffer.writeLong(packet.d)
buffer.writeFloat(packet.e)
buffer.writeDouble(packet.f)
buffer.writeBool(packet.g)
buffer.writeString(packet.jj)
buffer.writePacket(packet.kk, 102)
buffer.writeIntArray(packet.l)
buffer.writeLongArray(packet.ll)
buffer.writePacketArray(packet.lll, 102)
buffer.writeStringArray(packet.llll)
buffer.writeIntStringMap(packet.m)
buffer.writeIntPacketMap(packet.mm, 102)
buffer.writeIntSet(packet.s)
buffer.writeStringSet(packet.ssss)
pass
@classmethod
def read(cls, buffer):
if not buffer.readBool():
return None
packet = NormalObject()
result0 = buffer.readByte()
packet.a = result0
array1 = buffer.readByteArray()
packet.aaa = array1
result2 = buffer.readShort()
packet.b = result2
result3 = buffer.readInt()
packet.c = result3
result4 = buffer.readLong()
packet.d = result4
result5 = buffer.readFloat()
packet.e = result5
result6 = buffer.readDouble()
packet.f = result6
result7 = buffer.readBool()
packet.g = result7
result8 = buffer.readString()
packet.jj = result8
result9 = buffer.readPacket(102)
packet.kk = result9
list10 = buffer.readIntArray()
packet.l = list10
list11 = buffer.readLongArray()
packet.ll = list11
list12 = buffer.readPacketArray(102)
packet.lll = list12
list13 = buffer.readStringArray()
packet.llll = list13
map14 = buffer.readIntStringMap()
packet.m = map14
map15 = buffer.readIntPacketMap(102)
packet.mm = map15
set16 = buffer.readIntSet()
packet.s = set16
set17 = buffer.readStringSet()
packet.ssss = set17
return packet
@@ -0,0 +1,32 @@
class ObjectA:
a = 0 # int
m = {} # Dictionary<int, string>
objectB = None # ObjectB
def protocolId(self):
return 102
@classmethod
def write(cls, buffer, packet):
if buffer.writePacketFlag(packet):
return
buffer.writeInt(packet.a)
buffer.writeIntStringMap(packet.m)
buffer.writePacket(packet.objectB, 103)
pass
@classmethod
def read(cls, buffer):
if not buffer.readBool():
return None
packet = ObjectA()
result0 = buffer.readInt()
packet.a = result0
map1 = buffer.readIntStringMap()
packet.m = map1
result2 = buffer.readPacket(103)
packet.objectB = result2
return packet
@@ -0,0 +1,24 @@
class ObjectB:
flag = False # bool
def protocolId(self):
return 103
@classmethod
def write(cls, buffer, packet):
if buffer.writePacketFlag(packet):
return
buffer.writeBool(packet.flag)
pass
@classmethod
def read(cls, buffer):
if not buffer.readBool():
return None
packet = ObjectB()
result0 = buffer.readBool()
packet.flag = result0
return packet
@@ -0,0 +1,30 @@
from . import VeryBigObject
from . import ComplexObject
from . import NormalObject
from . import ObjectA
from . import ObjectB
from . import SimpleObject
protocols = {}
protocols[0] = VeryBigObject.VeryBigObject
protocols[100] = ComplexObject.ComplexObject
protocols[101] = NormalObject.NormalObject
protocols[102] = ObjectA.ObjectA
protocols[103] = ObjectB.ObjectB
protocols[104] = SimpleObject.SimpleObject
def getProtocol(protocolId):
return protocols[protocolId]
def write(buffer, packet):
protocolId = packet.protocolId()
buffer.writeShort(protocolId)
protocol = protocols[protocolId]
protocol.write(buffer, packet)
def read(buffer):
protocolId = buffer.readShort()
protocol = protocols[protocolId]
packet = protocol.read(buffer)
return packet
@@ -0,0 +1,28 @@
class SimpleObject:
c = 0 # int
g = False # bool
def protocolId(self):
return 104
@classmethod
def write(cls, buffer, packet):
if buffer.writePacketFlag(packet):
return
buffer.writeInt(packet.c)
buffer.writeBool(packet.g)
pass
@classmethod
def read(cls, buffer):
if not buffer.readBool():
return None
packet = SimpleObject()
result0 = buffer.readInt()
packet.c = result0
result1 = buffer.readBool()
packet.g = result1
return packet
File diff suppressed because it is too large Load Diff
-7
View File
@@ -35,7 +35,6 @@
<!-- network framework -->
<netty.version>4.1.84.Final</netty.version>
<jprotobuf.version>2.4.17</jprotobuf.version>
<kryo.version>5.3.0</kryo.version>
<!-- zookeeper -->
<curator.version>5.3.0</curator.version>
@@ -51,12 +50,6 @@
<poi.version>5.2.3</poi.version>
<csv.version>1.9.0</csv.version>
<!-- Toolkit(工具包) -->
<jctools.version>4.0.1</jctools.version>
<!-- elastic search -->
<elastic.search.version>8.4.3</elastic.search.version>
<java.version>11</java.version>
<file.encoding>UTF-8</file.encoding>
-7
View File
@@ -35,7 +35,6 @@
<!-- network framework -->
<netty.version>4.1.84.Final</netty.version>
<jprotobuf.version>2.4.17</jprotobuf.version>
<kryo.version>5.3.0</kryo.version>
<!-- zookeeper -->
<curator.version>5.3.0</curator.version>
@@ -51,12 +50,6 @@
<poi.version>5.2.3</poi.version>
<csv.version>1.9.0</csv.version>
<!-- Toolkit(工具包) -->
<jctools.version>4.0.1</jctools.version>
<!-- elastic search -->
<elastic.search.version>8.4.3</elastic.search.version>
<java.version>11</java.version>
<file.encoding>UTF-8</file.encoding>
-7
View File
@@ -35,7 +35,6 @@
<!-- network framework -->
<netty.version>4.1.84.Final</netty.version>
<jprotobuf.version>2.4.17</jprotobuf.version>
<kryo.version>5.3.0</kryo.version>
<!-- zookeeper -->
<curator.version>5.3.0</curator.version>
@@ -51,12 +50,6 @@
<poi.version>5.2.3</poi.version>
<csv.version>1.9.0</csv.version>
<!-- Toolkit(工具包) -->
<jctools.version>4.0.1</jctools.version>
<!-- elastic search -->
<elastic.search.version>8.4.3</elastic.search.version>
<java.version>11</java.version>
<file.encoding>UTF-8</file.encoding>