mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-20 06:24:12 +00:00
feat[protocol]: support dart protocol
This commit is contained in:
@@ -92,6 +92,7 @@ public abstract class GenerateProtocolNote {
|
||||
case TypeScript:
|
||||
case CSharp:
|
||||
case Php:
|
||||
case Dart:
|
||||
case Protobuf:
|
||||
note = StringUtils.format("// {}", note);
|
||||
break;
|
||||
|
||||
@@ -14,6 +14,7 @@ package com.zfoo.protocol.serializer;
|
||||
|
||||
import com.zfoo.protocol.serializer.cpp.CodeGenerateCpp;
|
||||
import com.zfoo.protocol.serializer.csharp.CodeGenerateCsharp;
|
||||
import com.zfoo.protocol.serializer.dart.CodeGenerateDart;
|
||||
import com.zfoo.protocol.serializer.ecmascript.CodeGenerateEcmaScript;
|
||||
import com.zfoo.protocol.serializer.gdscript.CodeGenerateGdScript;
|
||||
import com.zfoo.protocol.serializer.golang.CodeGenerateGolang;
|
||||
@@ -44,6 +45,8 @@ public enum CodeLanguage {
|
||||
|
||||
Scala(1 << 3, CodeGenerateScala.class),
|
||||
|
||||
Dart(1 << 3, CodeGenerateDart.class),
|
||||
|
||||
Cpp(1 << 7, CodeGenerateCpp.class),
|
||||
|
||||
Rust(1 << 8, CodeGenerateRust.class),
|
||||
|
||||
@@ -0,0 +1,423 @@
|
||||
/*
|
||||
* 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.dart;
|
||||
|
||||
import com.zfoo.protocol.anno.Compatible;
|
||||
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.ProtocolAnalysis;
|
||||
import com.zfoo.protocol.registration.ProtocolRegistration;
|
||||
import com.zfoo.protocol.registration.field.IFieldRegistration;
|
||||
import com.zfoo.protocol.serializer.CodeLanguage;
|
||||
import com.zfoo.protocol.serializer.CodeTemplatePlaceholder;
|
||||
import com.zfoo.protocol.serializer.ICodeGenerate;
|
||||
import com.zfoo.protocol.serializer.enhance.EnhanceObjectProtocolSerializer;
|
||||
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 org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
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
|
||||
*/
|
||||
public class CodeGenerateDart implements ICodeGenerate {
|
||||
private static final Logger logger = LoggerFactory.getLogger(CodeGenerateDart.class);
|
||||
|
||||
// custom configuration
|
||||
public static String protocolOutputRootPath = "zfoodart";
|
||||
private static String protocolOutputPath = StringUtils.EMPTY;
|
||||
|
||||
private static final Map<ISerializer, IDartSerializer> dartSerializerMap = new HashMap<>();
|
||||
|
||||
public static IDartSerializer dartSerializer(ISerializer serializer) {
|
||||
return dartSerializerMap.get(serializer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(GenerateOperation generateOperation) {
|
||||
protocolOutputPath = FileUtils.joinPath(generateOperation.getProtocolPath(), protocolOutputRootPath);
|
||||
FileUtils.deleteFile(new File(protocolOutputPath));
|
||||
|
||||
dartSerializerMap.put(BoolSerializer.INSTANCE, new DartBoolSerializer());
|
||||
dartSerializerMap.put(ByteSerializer.INSTANCE, new DartByteSerializer());
|
||||
dartSerializerMap.put(ShortSerializer.INSTANCE, new DartShortSerializer());
|
||||
dartSerializerMap.put(IntSerializer.INSTANCE, new DartIntSerializer());
|
||||
dartSerializerMap.put(LongSerializer.INSTANCE, new DartLongSerializer());
|
||||
dartSerializerMap.put(FloatSerializer.INSTANCE, new DartFloatSerializer());
|
||||
dartSerializerMap.put(DoubleSerializer.INSTANCE, new DartDoubleSerializer());
|
||||
dartSerializerMap.put(StringSerializer.INSTANCE, new DartStringSerializer());
|
||||
dartSerializerMap.put(ArraySerializer.INSTANCE, new DartArraySerializer());
|
||||
dartSerializerMap.put(ListSerializer.INSTANCE, new DartListSerializer());
|
||||
dartSerializerMap.put(SetSerializer.INSTANCE, new DartSetSerializer());
|
||||
dartSerializerMap.put(MapSerializer.INSTANCE, new DartMapSerializer());
|
||||
dartSerializerMap.put(ObjectProtocolSerializer.INSTANCE, new DartObjectProtocolSerializer());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mergerProtocol(List<ProtocolRegistration> registrations) throws IOException {
|
||||
createTemplateFile();
|
||||
|
||||
|
||||
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("dart/ProtocolManagerTemplate.dart");
|
||||
var protocol_imports_manager = new StringBuilder();
|
||||
var protocol_manager_registrations = new StringBuilder();
|
||||
protocol_imports_manager.append("import 'Protocols.dart';").append(LS);
|
||||
for (var registration : registrations) {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
protocol_manager_registrations.append(StringUtils.format("protocols[{}] = {}Registration();", protocol_id, protocol_name)).append(LS);
|
||||
protocol_manager_registrations.append(StringUtils.format("protocolIdMap[{}] = {};", protocol_name, protocol_id)).append(LS);
|
||||
}
|
||||
var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports_manager.toString()
|
||||
, CodeTemplatePlaceholder.protocol_manager_registrations, protocol_manager_registrations.toString());
|
||||
var formatProtocolManagerTemplate = CodeTemplatePlaceholder.formatTemplate(protocolManagerTemplate, placeholderMap);
|
||||
var protocolManagerFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.dart"));
|
||||
FileUtils.writeStringToFile(protocolManagerFile, formatProtocolManagerTemplate, true);
|
||||
logger.info("Generated Dart protocol manager file:[{}] is in path:[{}]", protocolManagerFile.getName(), protocolManagerFile.getAbsolutePath());
|
||||
|
||||
|
||||
var protocol_imports_protocols = new StringBuilder();
|
||||
protocol_imports_protocols.append("import './IByteBuffer.dart';").append(LS);
|
||||
protocol_imports_protocols.append("import './IProtocolRegistration.dart';").append(LS);
|
||||
var protocol_class = new StringBuilder();
|
||||
var protocol_registration = new StringBuilder();
|
||||
for (var registration : registrations) {
|
||||
protocol_class.append(protocol_class(registration)).append(LS);
|
||||
protocol_registration.append(protocol_registration(registration)).append(LS);
|
||||
}
|
||||
var protocolTemplate = ClassUtils.getFileFromClassPathToString("dart/ProtocolsTemplate.dart");
|
||||
var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of(
|
||||
CodeTemplatePlaceholder.protocol_imports, protocol_imports_protocols.toString()
|
||||
, CodeTemplatePlaceholder.protocol_class, protocol_class.toString()
|
||||
, CodeTemplatePlaceholder.protocol_registration, protocol_registration.toString()
|
||||
));
|
||||
var outputPath = StringUtils.format("{}/Protocols.dart", protocolOutputPath);
|
||||
var file = new File(outputPath);
|
||||
FileUtils.writeStringToFile(file, formatProtocolTemplate, true);
|
||||
logger.info("Generated Dart protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void foldProtocol(List<ProtocolRegistration> registrations) throws IOException {
|
||||
createTemplateFile();
|
||||
|
||||
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("dart/ProtocolManagerTemplate.dart");
|
||||
var protocol_manager_registrations = new StringBuilder();
|
||||
var protocol_imports = new StringBuilder();
|
||||
for (var registration : registrations) {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
protocol_imports.append(StringUtils.format("import '{}/{}.dart';", GenerateProtocolPath.protocolPathSlash(protocol_id), protocol_name, protocol_name)).append(LS);
|
||||
protocol_manager_registrations.append(StringUtils.format("protocols[{}] = {}Registration();", protocol_id, protocol_name)).append(LS);
|
||||
protocol_manager_registrations.append(StringUtils.format("protocolIdMap[{}] = {};", protocol_name, protocol_id)).append(LS);
|
||||
}
|
||||
|
||||
var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports.toString()
|
||||
, CodeTemplatePlaceholder.protocol_manager_registrations, protocol_manager_registrations.toString());
|
||||
var formatProtocolManagerTemplate = CodeTemplatePlaceholder.formatTemplate(protocolManagerTemplate, placeholderMap);
|
||||
var protocolManagerFile = new File(StringUtils.format("{}/{}", protocolOutputRootPath, "ProtocolManager.dart"));
|
||||
FileUtils.writeStringToFile(protocolManagerFile, formatProtocolManagerTemplate, true);
|
||||
logger.info("Generated Dart protocol manager file:[{}] is in path:[{}]", protocolManagerFile.getName(), protocolManagerFile.getAbsolutePath());
|
||||
|
||||
for (var registration : registrations) {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
var protocolTemplate = ClassUtils.getFileFromClassPathToString("dart/ProtocolTemplate.dart");
|
||||
var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of(
|
||||
CodeTemplatePlaceholder.protocol_imports, protocol_imports_fold(registration)
|
||||
, CodeTemplatePlaceholder.protocol_class, protocol_class(registration)
|
||||
, CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration)
|
||||
));
|
||||
var outputPath = StringUtils.format("{}/{}/{}.dart", protocolOutputPath, GenerateProtocolPath.protocolPathSlash(protocol_id), protocol_name);
|
||||
var file = new File(outputPath);
|
||||
FileUtils.writeStringToFile(file, formatProtocolTemplate, true);
|
||||
logger.info("Generated Dart protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defaultProtocol(List<ProtocolRegistration> registrations) throws IOException {
|
||||
createTemplateFile();
|
||||
|
||||
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("dart/ProtocolManagerTemplate.dart");
|
||||
var protocol_manager_registrations = new StringBuilder();
|
||||
var protocol_imports = new StringBuilder();
|
||||
for (var registration : registrations) {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
protocol_imports.append(StringUtils.format("import '{}.dart';", protocol_name, protocol_name)).append(LS);
|
||||
protocol_manager_registrations.append(StringUtils.format("protocols[{}] = {}Registration();", protocol_id, protocol_name)).append(LS);
|
||||
protocol_manager_registrations.append(StringUtils.format("protocolIdMap[{}] = {};", protocol_name, protocol_id)).append(LS);
|
||||
}
|
||||
|
||||
var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports.toString()
|
||||
, CodeTemplatePlaceholder.protocol_manager_registrations, protocol_manager_registrations.toString());
|
||||
var formatProtocolManagerTemplate = CodeTemplatePlaceholder.formatTemplate(protocolManagerTemplate, placeholderMap);
|
||||
var protocolManagerFile = new File(StringUtils.format("{}/{}", protocolOutputRootPath, "ProtocolManager.dart"));
|
||||
FileUtils.writeStringToFile(protocolManagerFile, formatProtocolManagerTemplate, true);
|
||||
logger.info("Generated Dart protocol manager file:[{}] is in path:[{}]", protocolManagerFile.getName(), protocolManagerFile.getAbsolutePath());
|
||||
|
||||
for (var registration : registrations) {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
var protocolTemplate = ClassUtils.getFileFromClassPathToString("dart/ProtocolTemplate.dart");
|
||||
var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of(
|
||||
CodeTemplatePlaceholder.protocol_imports, protocol_imports_default(registration)
|
||||
, CodeTemplatePlaceholder.protocol_class, protocol_class(registration)
|
||||
, CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration)
|
||||
));
|
||||
var outputPath = StringUtils.format("{}/{}.dart", protocolOutputPath, protocol_name);
|
||||
var file = new File(outputPath);
|
||||
FileUtils.writeStringToFile(file, formatProtocolTemplate, true);
|
||||
logger.info("Generated Dart protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
private void createTemplateFile() throws IOException {
|
||||
var list = List.of("dart/IProtocolRegistration.dart", "dart/IByteBuffer.dart", "dart/ByteBuffer.dart");
|
||||
for (var fileName : list) {
|
||||
var fileInputStream = ClassUtils.getFileFromClassPath(fileName);
|
||||
var createFile = new File(StringUtils.format("{}/{}", protocolOutputPath, StringUtils.substringAfterFirst(fileName, "dart/")));
|
||||
FileUtils.writeInputStreamToFile(createFile, fileInputStream);
|
||||
}
|
||||
}
|
||||
|
||||
private String protocol_class(ProtocolRegistration registration) {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
var protocolTemplate = ClassUtils.getFileFromClassPathToString("dart/ProtocolClassTemplate.dart");
|
||||
var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of(
|
||||
CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocol_id, CodeLanguage.Dart)
|
||||
, CodeTemplatePlaceholder.protocol_name, protocol_name
|
||||
, CodeTemplatePlaceholder.protocol_id, String.valueOf(protocol_id)
|
||||
, CodeTemplatePlaceholder.protocol_field_definition, protocol_field_definition(registration)
|
||||
, CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration)
|
||||
));
|
||||
return formatProtocolTemplate;
|
||||
}
|
||||
|
||||
private String protocol_registration(ProtocolRegistration registration) {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
var protocolTemplate = ClassUtils.getFileFromClassPathToString("dart/ProtocolRegistrationTemplate.dart");
|
||||
var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of(
|
||||
CodeTemplatePlaceholder.protocol_name, protocol_name
|
||||
, CodeTemplatePlaceholder.protocol_id, String.valueOf(protocol_id)
|
||||
, CodeTemplatePlaceholder.protocol_write_serialization, protocol_write_serialization(registration)
|
||||
, CodeTemplatePlaceholder.protocol_read_deserialization, protocol_read_deserialization(registration)
|
||||
));
|
||||
return formatProtocolTemplate;
|
||||
}
|
||||
|
||||
private String protocol_imports_default(ProtocolRegistration registration) {
|
||||
var protocolId = registration.getId();
|
||||
var subProtocols = ProtocolAnalysis.getAllSubProtocolIds(protocolId);
|
||||
var importBuilder = new StringBuilder();
|
||||
importBuilder.append("import 'IProtocolRegistration.dart';").append(LS);
|
||||
importBuilder.append("import 'IByteBuffer.dart';").append(LS);
|
||||
for (var subProtocolId : subProtocols) {
|
||||
var protocolName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(subProtocolId);
|
||||
var subProtocolPath = StringUtils.format("import '{}.dart';", protocolName);
|
||||
importBuilder.append(subProtocolPath).append(LS);
|
||||
}
|
||||
return importBuilder.toString();
|
||||
}
|
||||
|
||||
private String protocol_imports_fold(ProtocolRegistration registration) {
|
||||
var protocolId = registration.getId();
|
||||
var subProtocols = ProtocolAnalysis.getAllSubProtocolIds(protocolId);
|
||||
var importBuilder = new StringBuilder();
|
||||
var protocolPath = GenerateProtocolPath.protocolPathPeriod(protocolId);
|
||||
var splits = protocolPath.split(StringUtils.PERIOD_REGEX);
|
||||
importBuilder.append(StringUtils.format("import '{}IProtocolRegistration.dart';", "../".repeat(splits.length))).append(LS);
|
||||
importBuilder.append(StringUtils.format("import '{}IByteBuffer.dart';", "../".repeat(splits.length))).append(LS);
|
||||
for (var subProtocolId : subProtocols) {
|
||||
var protocolName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(subProtocolId);
|
||||
var path = GenerateProtocolPath.relativePath(protocolId, subProtocolId);
|
||||
var subProtocolPath = StringUtils.format("import '{}/{}.dart';", path, protocolName);
|
||||
importBuilder.append(subProtocolPath).append(LS);
|
||||
}
|
||||
return importBuilder.toString();
|
||||
}
|
||||
|
||||
private String protocol_field_definition(ProtocolRegistration registration) {
|
||||
var protocolId = registration.getId();
|
||||
var fields = registration.getFields();
|
||||
var fieldRegistrations = registration.getFieldRegistrations();
|
||||
var dartBuilder = 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 fieldNotes = GenerateProtocolNote.fieldNotes(protocolId, fieldName, CodeLanguage.Dart);
|
||||
for (var fieldNote : fieldNotes) {
|
||||
dartBuilder.append(fieldNote).append(LS);
|
||||
}
|
||||
var fieldTypeDefaultValue = dartSerializer(fieldRegistration.serializer()).fieldTypeDefaultValue(field, fieldRegistration);
|
||||
var fieldType = fieldTypeDefaultValue.getKey();
|
||||
var fieldDefaultValue = fieldTypeDefaultValue.getValue();
|
||||
dartBuilder.append(StringUtils.format("{} {} = {};", fieldType, fieldName, fieldDefaultValue)).append(LS);
|
||||
}
|
||||
return dartBuilder.toString();
|
||||
}
|
||||
|
||||
|
||||
private String protocol_write_serialization(ProtocolRegistration registration) {
|
||||
GenerateProtocolFile.localVariableId = 0;
|
||||
var fields = registration.getFields();
|
||||
var fieldRegistrations = registration.getFieldRegistrations();
|
||||
var dartBuilder = new StringBuilder();
|
||||
if (registration.isCompatible()) {
|
||||
dartBuilder.append("var beforeWriteIndex = buffer.getWriteOffset();").append(LS);
|
||||
dartBuilder.append(StringUtils.format("buffer.writeInt({});", registration.getPredictionLength())).append(LS);
|
||||
} else {
|
||||
dartBuilder.append("buffer.writeInt(-1);").append(LS);
|
||||
}
|
||||
for (var i = 0; i < fields.length; i++) {
|
||||
var field = fields[i];
|
||||
var fieldRegistration = fieldRegistrations[i];
|
||||
dartSerializer(fieldRegistration.serializer()).writeObject(dartBuilder, "packet." + field.getName(), 0, field, fieldRegistration);
|
||||
}
|
||||
if (registration.isCompatible()) {
|
||||
dartBuilder.append(StringUtils.format("buffer.adjustPadding({}, beforeWriteIndex);", registration.getPredictionLength())).append(LS);
|
||||
}
|
||||
return dartBuilder.toString();
|
||||
}
|
||||
|
||||
|
||||
private String protocol_read_deserialization(ProtocolRegistration registration) {
|
||||
GenerateProtocolFile.localVariableId = 0;
|
||||
var fields = registration.getFields();
|
||||
var fieldRegistrations = registration.getFieldRegistrations();
|
||||
var dartBuilder = new StringBuilder();
|
||||
for (var i = 0; i < fields.length; i++) {
|
||||
var field = fields[i];
|
||||
var fieldRegistration = fieldRegistrations[i];
|
||||
|
||||
if (field.isAnnotationPresent(Compatible.class)) {
|
||||
dartBuilder.append("if (buffer.compatibleRead(beforeReadIndex, length)) {").append(LS);
|
||||
var compatibleReadObject = dartSerializer(fieldRegistration.serializer()).readObject(dartBuilder, 1, field, fieldRegistration);
|
||||
dartBuilder.append(TAB).append(StringUtils.format("packet.{} = {};", field.getName(), compatibleReadObject)).append(LS);
|
||||
dartBuilder.append("}").append(LS);
|
||||
continue;
|
||||
}
|
||||
var readObject = dartSerializer(fieldRegistration.serializer()).readObject(dartBuilder, 0, field, fieldRegistration);
|
||||
dartBuilder.append(StringUtils.format("packet.{} = {};", field.getName(), readObject)).append(LS);
|
||||
}
|
||||
return dartBuilder.toString();
|
||||
}
|
||||
|
||||
public static String toDartClassName(String typeName) {
|
||||
typeName = typeName.replaceAll("java.util.|java.lang.", StringUtils.EMPTY);
|
||||
typeName = typeName.replaceAll("[a-zA-Z0-9_.]*\\.", StringUtils.EMPTY);
|
||||
|
||||
switch (typeName) {
|
||||
case "boolean":
|
||||
case "Boolean":
|
||||
typeName = "bool";
|
||||
return typeName;
|
||||
case "byte":
|
||||
case "Byte":
|
||||
case "short":
|
||||
case "Short":
|
||||
case "int":
|
||||
case "Integer":
|
||||
case "long":
|
||||
case "Long":
|
||||
typeName = "int";
|
||||
return typeName;
|
||||
case "float":
|
||||
case "Float":
|
||||
case "double":
|
||||
case "Double":
|
||||
typeName = "double";
|
||||
return typeName;
|
||||
case "char":
|
||||
case "Character":
|
||||
case "String":
|
||||
typeName = "String";
|
||||
return typeName;
|
||||
default:
|
||||
}
|
||||
|
||||
// 将boolean转为bool
|
||||
typeName = typeName.replaceAll("[B|b]oolean\\[", "bool");
|
||||
typeName = typeName.replace("<Boolean", "<bool");
|
||||
typeName = typeName.replace("Boolean>", "bool>");
|
||||
|
||||
// 将Byte转为byte
|
||||
typeName = typeName.replace("Byte[", "int");
|
||||
typeName = typeName.replace("Byte>", "int>");
|
||||
typeName = typeName.replace("<Byte", "<int");
|
||||
|
||||
// 将Short转为short
|
||||
typeName = typeName.replace("Short[", "int");
|
||||
typeName = typeName.replace("Short>", "int>");
|
||||
typeName = typeName.replace("<Short", "<int");
|
||||
|
||||
// 将Integer转为int
|
||||
typeName = typeName.replace("Integer[", "int");
|
||||
typeName = typeName.replace("Integer>", "int>");
|
||||
typeName = typeName.replace("<Integer", "<int");
|
||||
|
||||
// 将Long转为long
|
||||
typeName = typeName.replace("Long[", "int");
|
||||
typeName = typeName.replace("Long>", "int>");
|
||||
typeName = typeName.replace("<Long", "<int");
|
||||
|
||||
// 将Float转为float
|
||||
typeName = typeName.replace("Float[", "double");
|
||||
typeName = typeName.replace("Float>", "double>");
|
||||
typeName = typeName.replace("<Float", "<double");
|
||||
|
||||
// 将Double转为double
|
||||
typeName = typeName.replace("Double[", "double");
|
||||
typeName = typeName.replace("Double>", "double>");
|
||||
typeName = typeName.replace("<Double", "<double");
|
||||
|
||||
// 将Character转为Char
|
||||
typeName = typeName.replace("Character[", "String");
|
||||
typeName = typeName.replace("Character>", "String>");
|
||||
typeName = typeName.replace("<Character", "<String");
|
||||
|
||||
// 将String转为string
|
||||
typeName = typeName.replace("String[", "String");
|
||||
typeName = typeName.replace("String>", "String>");
|
||||
typeName = typeName.replace("<String", "<String");
|
||||
|
||||
typeName = typeName.replace("Map<", "Map<");
|
||||
typeName = typeName.replace("Set<", "Set<");
|
||||
typeName = typeName.replace("List<", "List<");
|
||||
|
||||
return typeName;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* 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.dart;
|
||||
|
||||
import com.zfoo.protocol.generate.GenerateProtocolFile;
|
||||
import com.zfoo.protocol.model.Pair;
|
||||
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
|
||||
*/
|
||||
public class DartArraySerializer implements IDartSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
var type = StringUtils.format("List<{}>", CodeGenerateDart.toDartClassName(field.getType().getComponentType().getSimpleName()));
|
||||
return new Pair<>(type, "List.empty()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
if (CutDownArraySerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Dart)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ArrayField arrayField = (ArrayField) fieldRegistration;
|
||||
|
||||
builder.append(StringUtils.format("buffer.writeInt({}.length);", objectStr)).append(LS);
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
String length = "length" + GenerateProtocolFile.localVariableId++;
|
||||
builder.append(StringUtils.format("var {} = {}.length;", length, objectStr)).append(LS);
|
||||
|
||||
String i = "i" + GenerateProtocolFile.localVariableId++;
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("for (var {} = 0; {} < {}; {}++) {", i, i, length, i)).append(LS);
|
||||
GenerateProtocolFile.addTab(builder, deep + 1);
|
||||
String element = "element" + GenerateProtocolFile.localVariableId++;
|
||||
builder.append(StringUtils.format("var {} = {}[{}];", element, objectStr, i)).append(LS);
|
||||
|
||||
CodeGenerateDart.dartSerializer(arrayField.getArrayElementRegistration().serializer())
|
||||
.writeObject(builder, element, deep + 1, field, arrayField.getArrayElementRegistration());
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append("}").append(LS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
var cutDown = CutDownArraySerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Dart);
|
||||
if (cutDown != null) {
|
||||
return cutDown;
|
||||
}
|
||||
|
||||
|
||||
var arrayField = (ArrayField) fieldRegistration;
|
||||
var result = "result" + GenerateProtocolFile.localVariableId++;
|
||||
|
||||
var typeName = CodeGenerateDart.toDartClassName(arrayField.getType().getSimpleName());
|
||||
|
||||
var i = "index" + GenerateProtocolFile.localVariableId++;
|
||||
var size = "size" + GenerateProtocolFile.localVariableId++;
|
||||
builder.append(StringUtils.format("var {} = buffer.readInt();", size)).append(LS);
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("List<{}> {} = List.empty(growable: true);", typeName,result)).append(LS);
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("if ({} > 0) {", size)).append(LS);
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep + 1);
|
||||
builder.append(StringUtils.format("for (var {} = 0; {} < {}; {}++) {", i, i, size, i)).append(LS);
|
||||
var readObject = CodeGenerateDart.dartSerializer(arrayField.getArrayElementRegistration().serializer())
|
||||
.readObject(builder, deep + 2, field, arrayField.getArrayElementRegistration());
|
||||
GenerateProtocolFile.addTab(builder, deep + 2);
|
||||
builder.append(StringUtils.format("{}.add({});", result, i, readObject));
|
||||
builder.append(LS);
|
||||
GenerateProtocolFile.addTab(builder, deep + 1);
|
||||
builder.append("}").append(LS);
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append("}").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.dart;
|
||||
|
||||
import com.zfoo.protocol.generate.GenerateProtocolFile;
|
||||
import com.zfoo.protocol.model.Pair;
|
||||
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
|
||||
*/
|
||||
public class DartBoolSerializer implements IDartSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("bool", "false");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("buffer.writeBool({});", objectStr)).append(LS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
String result = "result" + GenerateProtocolFile.localVariableId++;
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("var {} = buffer.readBool();", result)).append(LS);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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.dart;
|
||||
|
||||
import com.zfoo.protocol.generate.GenerateProtocolFile;
|
||||
import com.zfoo.protocol.model.Pair;
|
||||
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
|
||||
*/
|
||||
public class DartByteSerializer implements IDartSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("int", "0");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("buffer.writeByte({});", objectStr)).append(LS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
String result = "result" + GenerateProtocolFile.localVariableId++;
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("var {} = buffer.readByte();", result)).append(LS);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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.dart;
|
||||
|
||||
import com.zfoo.protocol.generate.GenerateProtocolFile;
|
||||
import com.zfoo.protocol.model.Pair;
|
||||
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
|
||||
*/
|
||||
public class DartDoubleSerializer implements IDartSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("double", "0.0");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("buffer.writeDouble({});", objectStr)).append(LS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
String result = "result" + GenerateProtocolFile.localVariableId++;
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("var {} = buffer.readDouble();", result)).append(LS);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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.dart;
|
||||
|
||||
import com.zfoo.protocol.generate.GenerateProtocolFile;
|
||||
import com.zfoo.protocol.model.Pair;
|
||||
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
|
||||
*/
|
||||
public class DartFloatSerializer implements IDartSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("double", "0.0");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("buffer.writeFloat({});", objectStr)).append(LS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
String result = "result" + GenerateProtocolFile.localVariableId++;
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("var {} = 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.dart;
|
||||
|
||||
import com.zfoo.protocol.generate.GenerateProtocolFile;
|
||||
import com.zfoo.protocol.model.Pair;
|
||||
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
|
||||
*/
|
||||
public class DartIntSerializer implements IDartSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("int", "0");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("buffer.writeInt({});", objectStr)).append(LS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
String result = "result" + GenerateProtocolFile.localVariableId++;
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("var {} = buffer.readInt();", result)).append(LS);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.protocol.serializer.dart;
|
||||
|
||||
import com.zfoo.protocol.generate.GenerateProtocolFile;
|
||||
import com.zfoo.protocol.model.Pair;
|
||||
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
|
||||
*/
|
||||
public class DartListSerializer implements IDartSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
var type = StringUtils.format("{}", CodeGenerateDart.toDartClassName(field.getGenericType().toString()));
|
||||
return new Pair<>(type, "List.empty()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
if (CutDownListSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Dart)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ListField listField = (ListField) fieldRegistration;
|
||||
|
||||
builder.append(StringUtils.format("buffer.writeInt({}.length);", objectStr)).append(LS);
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
String element = "element" + GenerateProtocolFile.localVariableId++;
|
||||
builder.append(StringUtils.format("for (var {} in {}) {", element, objectStr)).append(LS);
|
||||
|
||||
CodeGenerateDart.dartSerializer(listField.getListElementRegistration().serializer())
|
||||
.writeObject(builder, element, deep + 1, field, listField.getListElementRegistration());
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append("}").append(LS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
var cutDown = CutDownListSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Dart);
|
||||
if (cutDown != null) {
|
||||
return cutDown;
|
||||
}
|
||||
|
||||
var listField = (ListField) fieldRegistration;
|
||||
var result = "result" + GenerateProtocolFile.localVariableId++;
|
||||
|
||||
var typeName = CodeGenerateDart.toDartClassName(listField.getType().toString());
|
||||
|
||||
var i = "index" + GenerateProtocolFile.localVariableId++;
|
||||
var size = "size" + GenerateProtocolFile.localVariableId++;
|
||||
|
||||
builder.append(StringUtils.format("var {} = buffer.readInt();", size)).append(LS);
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("{} {} = List.empty(growable: true);", typeName, result)).append(LS);
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("if ({} > 0) {", size)).append(LS);
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep + 1);
|
||||
builder.append(StringUtils.format("for (var {} = 0; {} < {}; {}++) {", i, i, size, i)).append(LS);
|
||||
var readObject = CodeGenerateDart.dartSerializer(listField.getListElementRegistration().serializer())
|
||||
.readObject(builder, deep + 2, field, listField.getListElementRegistration());
|
||||
GenerateProtocolFile.addTab(builder, deep + 2);
|
||||
builder.append(StringUtils.format("{}.add({});", result, readObject)).append(LS);
|
||||
GenerateProtocolFile.addTab(builder, deep + 1);
|
||||
builder.append("}").append(LS);
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append("}").append(LS);
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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.dart;
|
||||
|
||||
import com.zfoo.protocol.generate.GenerateProtocolFile;
|
||||
import com.zfoo.protocol.model.Pair;
|
||||
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
|
||||
*/
|
||||
public class DartLongSerializer implements IDartSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("int", "0");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("buffer.writeLong({});", objectStr)).append(LS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
String result = "result" + GenerateProtocolFile.localVariableId++;
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("var {} = buffer.readLong();", result)).append(LS);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* 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.dart;
|
||||
|
||||
import com.zfoo.protocol.generate.GenerateProtocolFile;
|
||||
import com.zfoo.protocol.model.Pair;
|
||||
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
|
||||
*/
|
||||
public class DartMapSerializer implements IDartSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
var type = StringUtils.format("{}", CodeGenerateDart.toDartClassName(field.getGenericType().toString()));
|
||||
return new Pair<>(type, "Map()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
if (CutDownMapSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Dart)) {
|
||||
return;
|
||||
}
|
||||
|
||||
MapField mapField = (MapField) fieldRegistration;
|
||||
|
||||
builder.append(StringUtils.format("buffer.writeInt({}.length);", objectStr)).append(LS);
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
String key = "keyElement" + GenerateProtocolFile.localVariableId++;
|
||||
String value = "valueElement" + GenerateProtocolFile.localVariableId++;
|
||||
builder.append(StringUtils.format("{}.forEach(({}, {}) {", objectStr, key, value)).append(LS);
|
||||
|
||||
CodeGenerateDart.dartSerializer(mapField.getMapKeyRegistration().serializer())
|
||||
.writeObject(builder, key, deep + 1, field, mapField.getMapKeyRegistration());
|
||||
CodeGenerateDart.dartSerializer(mapField.getMapValueRegistration().serializer())
|
||||
.writeObject(builder, value, deep + 1, field, mapField.getMapValueRegistration());
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append("});").append(LS);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
var cutDown = CutDownMapSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Dart);
|
||||
if (cutDown != null) {
|
||||
return cutDown;
|
||||
}
|
||||
|
||||
MapField mapField = (MapField) fieldRegistration;
|
||||
String result = "result" + GenerateProtocolFile.localVariableId++;
|
||||
|
||||
var typeName = CodeGenerateDart.toDartClassName(mapField.getType().toString());
|
||||
|
||||
String size = "size" + GenerateProtocolFile.localVariableId++;
|
||||
builder.append(StringUtils.format("var {} = buffer.readInt();", size)).append(LS);
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("{} {} = Map();", typeName, result, size)).append(LS);
|
||||
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("if ({} > 0) {", size)).append(LS);
|
||||
|
||||
String i = "index" + GenerateProtocolFile.localVariableId++;
|
||||
GenerateProtocolFile.addTab(builder, deep + 1);
|
||||
builder.append(StringUtils.format("for (var {} = 0; {} < {}; {}++) {", i, i, size, i)).append(LS);
|
||||
|
||||
String keyObject = CodeGenerateDart.dartSerializer(mapField.getMapKeyRegistration().serializer())
|
||||
.readObject(builder, deep + 2, field, mapField.getMapKeyRegistration());
|
||||
|
||||
|
||||
String valueObject = CodeGenerateDart.dartSerializer(mapField.getMapValueRegistration().serializer())
|
||||
.readObject(builder, deep + 2, field, mapField.getMapValueRegistration());
|
||||
GenerateProtocolFile.addTab(builder, deep + 2);
|
||||
|
||||
builder.append(StringUtils.format("{}[{}] = {};", result, keyObject, valueObject)).append(LS);
|
||||
GenerateProtocolFile.addTab(builder, deep + 1);
|
||||
builder.append("}").append(LS);
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append("}").append(LS);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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.dart;
|
||||
|
||||
import com.zfoo.protocol.generate.GenerateProtocolFile;
|
||||
import com.zfoo.protocol.model.Pair;
|
||||
import com.zfoo.protocol.registration.field.IFieldRegistration;
|
||||
import com.zfoo.protocol.registration.field.ObjectProtocolField;
|
||||
import com.zfoo.protocol.serializer.enhance.EnhanceObjectProtocolSerializer;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
*/
|
||||
public class DartObjectProtocolSerializer implements IDartSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
|
||||
var protocolSimpleName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId());
|
||||
var type = StringUtils.format("{}?", protocolSimpleName);
|
||||
return new Pair<>(type, "null");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("buffer.writePacket({}, {});", objectStr, objectProtocolField.getProtocolId()))
|
||||
.append(LS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
|
||||
String result = "result" + GenerateProtocolFile.localVariableId++;
|
||||
|
||||
var protocolSimpleName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId());
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("var {} = buffer.readPacket({}) as {};", result, objectProtocolField.getProtocolId(), protocolSimpleName))
|
||||
.append(LS);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* 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.dart;
|
||||
|
||||
import com.zfoo.protocol.generate.GenerateProtocolFile;
|
||||
import com.zfoo.protocol.model.Pair;
|
||||
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
|
||||
*/
|
||||
public class DartSetSerializer implements IDartSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
var type = StringUtils.format("{}", CodeGenerateDart.toDartClassName(field.getGenericType().toString()));
|
||||
return new Pair<>(type, "Set()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
if (CutDownSetSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Dart)) {
|
||||
return;
|
||||
}
|
||||
|
||||
SetField setField = (SetField) fieldRegistration;
|
||||
|
||||
builder.append(StringUtils.format("buffer.writeInt({}.length);", objectStr)).append(LS);
|
||||
|
||||
String element = "i" + GenerateProtocolFile.localVariableId++;
|
||||
GenerateProtocolFile.addTab(builder, deep );
|
||||
builder.append(StringUtils.format("for (var {} in {}) {", element, objectStr)).append(LS);
|
||||
|
||||
CodeGenerateDart.dartSerializer(setField.getSetElementRegistration().serializer())
|
||||
.writeObject(builder, element, deep + 1, field, setField.getSetElementRegistration());
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append("}").append(LS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
var cutDown = CutDownSetSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Dart);
|
||||
if (cutDown != null) {
|
||||
return cutDown;
|
||||
}
|
||||
|
||||
SetField setField = (SetField) fieldRegistration;
|
||||
var result = "result" + GenerateProtocolFile.localVariableId++;
|
||||
|
||||
var typeName = CodeGenerateDart.toDartClassName(setField.getType().toString());
|
||||
|
||||
var i = "index" + GenerateProtocolFile.localVariableId++;
|
||||
var size = "size" + GenerateProtocolFile.localVariableId++;
|
||||
builder.append(StringUtils.format("var {} = buffer.readInt();", size)).append(LS);
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("{} {} = Set();", typeName, result)).append(LS);
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("if ({} > 0) {", size)).append(LS);
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep + 1);
|
||||
builder.append(StringUtils.format("for (var {} = 0; {} < {}; {}++) {", i, i, size, i)).append(LS);
|
||||
|
||||
var readObject = CodeGenerateDart.dartSerializer(setField.getSetElementRegistration().serializer())
|
||||
.readObject(builder, deep + 2, field, setField.getSetElementRegistration());
|
||||
GenerateProtocolFile.addTab(builder, deep + 2);
|
||||
builder.append(StringUtils.format("{}.add({});", result, readObject)).append(LS);
|
||||
GenerateProtocolFile.addTab(builder, deep + 1);
|
||||
builder.append("}").append(LS);
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append("}").append(LS);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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.dart;
|
||||
|
||||
import com.zfoo.protocol.generate.GenerateProtocolFile;
|
||||
import com.zfoo.protocol.model.Pair;
|
||||
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
|
||||
*/
|
||||
public class DartShortSerializer implements IDartSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("int", "0");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("buffer.writeShort({});", objectStr)).append(LS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
String result = "result" + GenerateProtocolFile.localVariableId++;
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("var {} = buffer.readShort();", result)).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.dart;
|
||||
|
||||
import com.zfoo.protocol.generate.GenerateProtocolFile;
|
||||
import com.zfoo.protocol.model.Pair;
|
||||
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
|
||||
*/
|
||||
public class DartStringSerializer implements IDartSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("String", "\"\"");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("buffer.writeString({});", objectStr)).append(LS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
String result = "result" + GenerateProtocolFile.localVariableId++;
|
||||
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("var {} = buffer.readString();", result)).append(LS);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.dart;
|
||||
|
||||
import com.zfoo.protocol.model.Pair;
|
||||
import com.zfoo.protocol.registration.field.IFieldRegistration;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
*/
|
||||
public interface IDartSerializer {
|
||||
/**
|
||||
* 获取属性的类型,默认值
|
||||
*/
|
||||
Pair<String, String> fieldTypeDefaultValue(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,326 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
import 'IByteBuffer.dart';
|
||||
import 'ProtocolManager.dart';
|
||||
|
||||
class ByteBuffer implements IByteBuffer {
|
||||
Int8List buffer = Int8List(128);
|
||||
int writeOffset = 0;
|
||||
int readOffset = 0;
|
||||
|
||||
@override
|
||||
Int8List getBuffer() {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
@override
|
||||
int getWriteOffset() {
|
||||
return writeOffset;
|
||||
}
|
||||
|
||||
@override
|
||||
void setWriteOffset(int writeIndex) {
|
||||
if (writeIndex > buffer.length) {
|
||||
throw Exception(
|
||||
"writeIndex[${writeIndex}] out of bounds exception: readOffset: [${readOffset}], writeOffset: [${writeOffset}](expected: 0 <= readOffset <= writeOffset <= capacity:${buffer.length})");
|
||||
}
|
||||
writeOffset = writeIndex;
|
||||
}
|
||||
|
||||
@override
|
||||
int getReadOffset() {
|
||||
return readOffset;
|
||||
}
|
||||
|
||||
@override
|
||||
void setReadOffset(int readIndex) {
|
||||
if (readIndex > writeOffset) {
|
||||
throw Exception(
|
||||
"readIndex[${readIndex}] out of bounds exception: readOffset: [${readOffset}], writeOffset: [${writeOffset}](expected: 0 <= readOffset <= writeOffset <= capacity:${buffer.length})");
|
||||
}
|
||||
readOffset = readIndex;
|
||||
}
|
||||
|
||||
@override
|
||||
bool isReadable() {
|
||||
return writeOffset > readOffset;
|
||||
}
|
||||
|
||||
@override
|
||||
int getCapacity() {
|
||||
return buffer.length - writeOffset;
|
||||
}
|
||||
|
||||
@override
|
||||
void ensureCapacity(int capacity) {
|
||||
while (capacity - getCapacity() > 0) {
|
||||
var newSize = buffer.length * 2;
|
||||
var newBytes = Int8List(newSize);
|
||||
newBytes.setRange(0, buffer.length, buffer);
|
||||
buffer = newBytes;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void writeBytes(Int8List bytes) {
|
||||
var length = bytes.length;
|
||||
ensureCapacity(length);
|
||||
buffer.setAll(writeOffset, bytes);
|
||||
writeOffset += length;
|
||||
}
|
||||
|
||||
@override
|
||||
Int8List readBytes(int length) {
|
||||
var value = buffer.sublist(readOffset, readOffset + length);
|
||||
readOffset += length;
|
||||
return value;
|
||||
}
|
||||
|
||||
@override
|
||||
void writeBool(bool value) {
|
||||
ensureCapacity(1);
|
||||
buffer[writeOffset++] = value ? 1 : 0;
|
||||
}
|
||||
|
||||
@override
|
||||
bool readBool() {
|
||||
return buffer[readOffset++] == 1;
|
||||
}
|
||||
|
||||
@override
|
||||
void writeByte(int value) {
|
||||
ensureCapacity(1);
|
||||
buffer.buffer.asByteData().setInt8(writeOffset, value);
|
||||
writeOffset += 1;
|
||||
}
|
||||
|
||||
@override
|
||||
int readByte() {
|
||||
var value = buffer.buffer.asByteData().getInt8(readOffset);
|
||||
readOffset += 1;
|
||||
return value;
|
||||
}
|
||||
|
||||
@override
|
||||
void writeShort(int value) {
|
||||
ensureCapacity(2);
|
||||
buffer.buffer.asByteData().setInt16(writeOffset, value);
|
||||
writeOffset += 2;
|
||||
}
|
||||
|
||||
@override
|
||||
int readShort() {
|
||||
var value = buffer.buffer.asByteData().getInt16(readOffset);
|
||||
readOffset += 2;
|
||||
return value;
|
||||
}
|
||||
|
||||
@override
|
||||
void writeRawInt(int value) {
|
||||
ensureCapacity(4);
|
||||
buffer.buffer.asByteData().setInt32(writeOffset, value);
|
||||
writeOffset += 4;
|
||||
}
|
||||
|
||||
@override
|
||||
int readRawInt() {
|
||||
var value = buffer.buffer.asByteData().getInt32(readOffset);
|
||||
readOffset += 4;
|
||||
return value;
|
||||
}
|
||||
|
||||
@override
|
||||
void writeInt(int value) {
|
||||
writeLong(value);
|
||||
}
|
||||
|
||||
@override
|
||||
int writeVarInt(int value) {
|
||||
int a = value >>> 7;
|
||||
if (a == 0) {
|
||||
writeByte(value);
|
||||
return 1;
|
||||
}
|
||||
|
||||
ensureCapacity(5);
|
||||
|
||||
writeByte(value | 0x80);
|
||||
int b = value >>> 14;
|
||||
if (b == 0) {
|
||||
writeByte(a);
|
||||
return 2;
|
||||
}
|
||||
|
||||
writeByte(a | 0x80);
|
||||
a = value >>> 21;
|
||||
if (a == 0) {
|
||||
writeByte(b);
|
||||
return 3;
|
||||
}
|
||||
|
||||
writeByte(b | 0x80);
|
||||
b = value >>> 28;
|
||||
if (b == 0) {
|
||||
writeByte(a);
|
||||
return 4;
|
||||
}
|
||||
|
||||
writeByte(a | 0x80);
|
||||
writeByte(b);
|
||||
return 5;
|
||||
}
|
||||
|
||||
@override
|
||||
int readInt() {
|
||||
return readLong();
|
||||
}
|
||||
|
||||
@override
|
||||
void writeLong(int value) {
|
||||
int mask = (value << 1) ^ (value >> 63);
|
||||
|
||||
if (mask >>> 32 == 0) {
|
||||
writeVarInt(mask);
|
||||
return;
|
||||
}
|
||||
|
||||
writeByte(mask | 0x80);
|
||||
writeByte(mask >>> 7 | 0x80);
|
||||
writeByte(mask >>> 14 | 0x80);
|
||||
writeByte(mask >>> 21 | 0x80);
|
||||
|
||||
int a = mask >>> 28;
|
||||
int b = mask >>> 35;
|
||||
if (b == 0) {
|
||||
writeByte(a);
|
||||
return;
|
||||
}
|
||||
|
||||
writeByte(a | 0x80);
|
||||
a = mask >>> 42;
|
||||
if (a == 0) {
|
||||
writeByte(b);
|
||||
return;
|
||||
}
|
||||
|
||||
writeByte(b | 0x80);
|
||||
b = mask >>> 49;
|
||||
if (b == 0) {
|
||||
writeByte(a);
|
||||
return;
|
||||
}
|
||||
|
||||
writeByte(a | 0x80);
|
||||
a = mask >>> 56;
|
||||
if (a == 0) {
|
||||
writeByte(b);
|
||||
return;
|
||||
}
|
||||
|
||||
writeByte(b | 0x80);
|
||||
writeByte(a);
|
||||
}
|
||||
|
||||
@override
|
||||
int readLong() {
|
||||
int b = readByte();
|
||||
int value = b;
|
||||
if (b < 0) {
|
||||
b = readByte();
|
||||
value = value & 0x000000000000007F | b << 7;
|
||||
if (b < 0) {
|
||||
b = readByte();
|
||||
value = value & 0x0000000000003FFF | b << 14;
|
||||
if (b < 0) {
|
||||
b = readByte();
|
||||
value = value & 0x00000000001FFFFF | b << 21;
|
||||
if (b < 0) {
|
||||
b = readByte();
|
||||
value = value & 0x000000000FFFFFFF | b << 28;
|
||||
if (b < 0) {
|
||||
b = readByte();
|
||||
value = value & 0x00000007FFFFFFFF | b << 35;
|
||||
if (b < 0) {
|
||||
b = readByte();
|
||||
value = value & 0x000003FFFFFFFFFF | b << 42;
|
||||
if (b < 0) {
|
||||
b = readByte();
|
||||
value = value & 0x0001FFFFFFFFFFFF | b << 49;
|
||||
if (b < 0) {
|
||||
b = readByte();
|
||||
value = value & 0x00FFFFFFFFFFFFFF | b << 56;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ((value >>> 1) ^ -(value & 1));
|
||||
}
|
||||
|
||||
@override
|
||||
void writeFloat(double value) {
|
||||
ensureCapacity(4);
|
||||
buffer.buffer.asByteData().setFloat32(writeOffset, value);
|
||||
writeOffset += 4;
|
||||
}
|
||||
|
||||
@override
|
||||
double readFloat() {
|
||||
var value = buffer.buffer.asByteData().getFloat32(readOffset);
|
||||
readOffset += 4;
|
||||
return value;
|
||||
}
|
||||
|
||||
@override
|
||||
void writeDouble(double value) {
|
||||
ensureCapacity(8);
|
||||
buffer.buffer.asByteData().setFloat64(writeOffset, value);
|
||||
writeOffset += 8;
|
||||
}
|
||||
|
||||
@override
|
||||
double readDouble() {
|
||||
var value = buffer.buffer.asByteData().getFloat64(readOffset);
|
||||
readOffset += 8;
|
||||
return value;
|
||||
}
|
||||
|
||||
@override
|
||||
void writeString(String value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
writeInt(0);
|
||||
return;
|
||||
}
|
||||
Uint8List uint8list = utf8.encode(value);
|
||||
Int8List bytes = Int8List.view(uint8list.buffer);
|
||||
writeInt(bytes.length);
|
||||
writeBytes(bytes);
|
||||
}
|
||||
|
||||
@override
|
||||
String readString() {
|
||||
var length = readInt();
|
||||
if (length <= 0) {
|
||||
return "";
|
||||
}
|
||||
Int8List bytes = readBytes(length);
|
||||
Uint8List uint8list = Uint8List.view(bytes.buffer);
|
||||
return utf8.decode(uint8list);
|
||||
}
|
||||
|
||||
@override
|
||||
void writePacket(Object? packet, int protocolId) {
|
||||
var protocol = Protocolmanager.getProtocol(protocolId);
|
||||
protocol.write(this, packet);
|
||||
}
|
||||
|
||||
@override
|
||||
Object readPacket(int protocolId) {
|
||||
var protocol = Protocolmanager.getProtocol(protocolId);
|
||||
return protocol.read(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
abstract class IByteBuffer {
|
||||
Int8List getBuffer();
|
||||
|
||||
int getWriteOffset();
|
||||
|
||||
void setWriteOffset(int writeIndex);
|
||||
|
||||
int getReadOffset();
|
||||
|
||||
void setReadOffset(int readIndex);
|
||||
|
||||
bool isReadable();
|
||||
|
||||
int getCapacity();
|
||||
|
||||
void ensureCapacity(int capacity);
|
||||
|
||||
void writeBytes(Int8List bytes);
|
||||
|
||||
Int8List readBytes(int length);
|
||||
|
||||
void writeBool(bool value);
|
||||
|
||||
bool readBool();
|
||||
|
||||
void writeByte(int value);
|
||||
|
||||
int readByte();
|
||||
|
||||
void writeShort(int value);
|
||||
|
||||
int readShort();
|
||||
|
||||
void writeRawInt(int value);
|
||||
|
||||
int readRawInt();
|
||||
|
||||
void writeInt(int value);
|
||||
|
||||
int writeVarInt(int value);
|
||||
|
||||
int readInt();
|
||||
|
||||
void writeLong(int value);
|
||||
|
||||
int readLong();
|
||||
|
||||
void writeFloat(double value);
|
||||
|
||||
double readFloat();
|
||||
|
||||
void writeDouble(double value);
|
||||
|
||||
double readDouble();
|
||||
|
||||
void writeString(String value);
|
||||
|
||||
String readString();
|
||||
|
||||
void writePacket(Object? packet, int protocolId);
|
||||
|
||||
Object readPacket(int protocolId);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import './IByteBuffer.dart';
|
||||
|
||||
abstract class IProtocolRegistration<T> {
|
||||
|
||||
int protocolId();
|
||||
|
||||
void write(IByteBuffer buffer, T? packet);
|
||||
|
||||
T read(IByteBuffer buffer);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
${protocol_note}
|
||||
class ${protocol_name} {
|
||||
${protocol_field_definition}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import './IProtocolRegistration.dart';
|
||||
import 'IByteBuffer.dart';
|
||||
${protocol_imports}
|
||||
|
||||
|
||||
class Protocolmanager {
|
||||
static Map<int, IProtocolRegistration> protocols = Map();
|
||||
static Map<Object, int> protocolIdMap = Map();
|
||||
|
||||
|
||||
static void initProtocol() {
|
||||
// initProtocol
|
||||
${protocol_manager_registrations}
|
||||
}
|
||||
|
||||
static int getProtocolId(Object clazz) {
|
||||
var protocolId = protocolIdMap[clazz];
|
||||
if (protocolId == null) {
|
||||
throw Exception("protocol:[$protocolId] not exist");
|
||||
}
|
||||
return protocolId;
|
||||
}
|
||||
|
||||
static IProtocolRegistration getProtocol(int protocolId) {
|
||||
var protocol = protocols[protocolId];
|
||||
if (protocol == null) {
|
||||
throw Exception("protocol:[$protocolId] not exist");
|
||||
}
|
||||
return protocol;
|
||||
}
|
||||
|
||||
static void write(IByteBuffer buffer, Object packet) {
|
||||
var protocolId = getProtocolId(packet.runtimeType);
|
||||
buffer.writeShort(protocolId);
|
||||
var protocol = getProtocol(protocolId);
|
||||
protocol.write(buffer, packet);
|
||||
}
|
||||
|
||||
static Object read(IByteBuffer buffer) {
|
||||
var protocolId = buffer.readShort();
|
||||
var protocol = getProtocol(protocolId);
|
||||
var packet = protocol.read(buffer);
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
class ${protocol_name}Registration implements IProtocolRegistration<${protocol_name}> {
|
||||
@override
|
||||
int protocolId() {
|
||||
return ${protocol_id};
|
||||
}
|
||||
|
||||
@override
|
||||
void write(IByteBuffer buffer, ${protocol_name}? packet) {
|
||||
if (packet == null) {
|
||||
buffer.writeInt(0);
|
||||
return;
|
||||
}
|
||||
${protocol_write_serialization}
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
${protocol_name} read(IByteBuffer buffer) {
|
||||
var length = buffer.readInt();
|
||||
var packet = ${protocol_name}();
|
||||
if (length == 0) {
|
||||
return packet;
|
||||
}
|
||||
var beforeReadIndex = buffer.getReadOffset();
|
||||
${protocol_read_deserialization}
|
||||
if (length > 0) {
|
||||
buffer.setReadOffset(beforeReadIndex + length);
|
||||
}
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
${protocol_imports}
|
||||
${protocol_class}
|
||||
|
||||
${protocol_registration}
|
||||
@@ -0,0 +1,5 @@
|
||||
${protocol_imports}
|
||||
|
||||
${protocol_class}
|
||||
|
||||
${protocol_registration}
|
||||
Reference in New Issue
Block a user