mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-19 15:27:45 +00:00
feat[golang]: compatible field of inside protocol class
This commit is contained in:
@@ -141,11 +141,11 @@ public abstract class GenerateCppUtils {
|
||||
, valueOfMethod.getValue().trim(), protocolId, protocolClazzName, operator.trim(),
|
||||
protocolClazzName, protocolId, protocolClazzName, writeObject.trim(), protocolClazzName, readObject.trim());
|
||||
|
||||
var protocolOutputPath = StringUtils.format("{}/{}/{}.h"
|
||||
var outputPath = StringUtils.format("{}/{}/{}.h"
|
||||
, GenerateCppUtils.protocolOutputPath
|
||||
, GenerateProtocolPath.getProtocolPath(protocolId)
|
||||
, protocolClazzName);
|
||||
FileUtils.writeStringToFile(new File(protocolOutputPath), protocolTemplate, true);
|
||||
FileUtils.writeStringToFile(new File(outputPath), protocolTemplate, true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -118,11 +118,11 @@ public abstract class GenerateCsUtils {
|
||||
, protocolId, protocolClazzName, protocolId, protocolClazzName, protocolClazzName, writeObject.trim()
|
||||
, protocolClazzName, protocolClazzName, readObject.trim());
|
||||
|
||||
var protocolOutputPath = StringUtils.format("{}/{}/{}.cs"
|
||||
var outputPath = StringUtils.format("{}/{}/{}.cs"
|
||||
, protocolOutputRootPath
|
||||
, GenerateProtocolPath.getCapitalizeProtocolPath(protocolId)
|
||||
, protocolClazzName);
|
||||
FileUtils.writeStringToFile(new File(protocolOutputPath), protocolTemplate, true);
|
||||
FileUtils.writeStringToFile(new File(outputPath), protocolTemplate, true);
|
||||
}
|
||||
|
||||
private static String fieldDefinition(ProtocolRegistration registration) {
|
||||
|
||||
@@ -23,6 +23,7 @@ import com.zfoo.protocol.registration.IProtocolRegistration;
|
||||
import com.zfoo.protocol.registration.ProtocolRegistration;
|
||||
import com.zfoo.protocol.registration.field.IFieldRegistration;
|
||||
import com.zfoo.protocol.serializer.CodeLanguage;
|
||||
import com.zfoo.protocol.serializer.cpp.GenerateCppUtils;
|
||||
import com.zfoo.protocol.serializer.reflect.*;
|
||||
import com.zfoo.protocol.util.ClassUtils;
|
||||
import com.zfoo.protocol.util.FileUtils;
|
||||
@@ -114,39 +115,25 @@ public abstract class GenerateGoUtils {
|
||||
var registrationConstructor = registration.getConstructor();
|
||||
var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName();
|
||||
|
||||
var protocolTemplate = ArrayUtils.isEmpty(registration.getFields())
|
||||
? ClassUtils.getFileFromClassPathToString("go/ProtocolTemplateEmpty.go")
|
||||
: ClassUtils.getFileFromClassPathToString("go/ProtocolTemplate.go");
|
||||
var protocolTemplate = ClassUtils.getFileFromClassPathToString("go/ProtocolTemplate.go");
|
||||
|
||||
var classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.Go);
|
||||
var fieldDefinition = fieldDefinition(registration);
|
||||
var writeObject = writeObject(registration);
|
||||
var readObject = readObject(registration);
|
||||
if (ArrayUtils.isEmpty(registration.getFields())) {
|
||||
protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName, fieldDefinition.trim()
|
||||
, protocolClazzName, protocolId, protocolClazzName, protocolClazzName, protocolClazzName);
|
||||
} else {
|
||||
protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName, fieldDefinition.trim()
|
||||
, protocolClazzName, protocolId, protocolClazzName, protocolClazzName
|
||||
, writeObject.trim(), protocolClazzName, protocolClazzName, readObject.trim());
|
||||
}
|
||||
|
||||
var protocolPath = GenerateProtocolPath.getProtocolPath(protocolId);
|
||||
protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName, fieldDefinition.trim()
|
||||
, protocolClazzName, protocolId, protocolClazzName, protocolClazzName
|
||||
, writeObject.trim(), protocolClazzName, protocolClazzName, readObject.trim());
|
||||
|
||||
var protocolPath = GenerateProtocolPath.protocolAbsolutePath(protocolId);
|
||||
if (StringUtils.isEmpty(protocolPath)) {
|
||||
protocolPath = protocolClazzName;
|
||||
} else if (protocolPath.contains(StringUtils.SLASH)) {
|
||||
protocolPath = StringUtils.substringAfterLast(protocolPath, StringUtils.SLASH);
|
||||
}
|
||||
|
||||
var protocolOutputPath = StringUtils.format("{}/{}.go"
|
||||
, protocolOutputRootPath
|
||||
, protocolPath);
|
||||
var protocolFile = new File(protocolOutputPath);
|
||||
if (protocolFile.exists()) {
|
||||
FileUtils.writeStringToFile(protocolFile, StringUtils.substringAfterFirst(protocolTemplate, "package protocol"), true);
|
||||
} else {
|
||||
FileUtils.writeStringToFile(protocolFile, protocolTemplate, true);
|
||||
}
|
||||
var outputPath = StringUtils.format("{}/{}/{}.go", protocolOutputPath, protocolOutputRootPath, protocolClazzName);
|
||||
FileUtils.writeStringToFile(outputPath, protocolTemplate, true);
|
||||
}
|
||||
|
||||
private static String fieldDefinition(ProtocolRegistration registration) {
|
||||
|
||||
@@ -124,9 +124,9 @@ public abstract class GenerateLuaUtils {
|
||||
, valueOfMethod.getKey().trim(), valueOfMethod.getValue().trim(), protocolClazzName, protocolId
|
||||
, protocolClazzName, writePacket.trim(), protocolClazzName, protocolClazzName, readPacket.trim(), protocolClazzName);
|
||||
|
||||
var protocolOutputPath = StringUtils.format("{}/{}/{}.lua"
|
||||
var outputPath = StringUtils.format("{}/{}/{}.lua"
|
||||
, protocolOutputRootPath, GenerateProtocolPath.getCapitalizeProtocolPath(protocolId), protocolClazzName);
|
||||
FileUtils.writeStringToFile(new File(protocolOutputPath), protocolTemplate, true);
|
||||
FileUtils.writeStringToFile(new File(outputPath), protocolTemplate, true);
|
||||
}
|
||||
|
||||
private static Pair<String, String> valueOfMethod(ProtocolRegistration registration) {
|
||||
|
||||
+4
-4
@@ -119,8 +119,8 @@ public abstract class GenerateProtobufUtils {
|
||||
|
||||
builder.append("}").append(LS);
|
||||
|
||||
var protocolOutputPath = StringUtils.format("{}/{}.proto", protocolOutputRootPath, protocolManagerName);
|
||||
FileUtils.writeStringToFile(new File(protocolOutputPath), builder.toString(), true);
|
||||
var outputPath = StringUtils.format("{}/{}.proto", protocolOutputRootPath, protocolManagerName);
|
||||
FileUtils.writeStringToFile(new File(outputPath), builder.toString(), true);
|
||||
}
|
||||
|
||||
public static void createProtocols(List<IProtocolRegistration> allGenerateProtocols) {
|
||||
@@ -168,8 +168,8 @@ public abstract class GenerateProtobufUtils {
|
||||
}
|
||||
|
||||
String protoName = StringUtils.substringAfterLast(protos.getKey(), StringUtils.PERIOD);
|
||||
var protocolOutputPath = StringUtils.format("{}/{}.proto", protocolOutputRootPath, protoName);
|
||||
FileUtils.writeStringToFile(new File(protocolOutputPath), builder.toString(), true);
|
||||
var outputPath = StringUtils.format("{}/{}.proto", protocolOutputRootPath, protoName);
|
||||
FileUtils.writeStringToFile(new File(outputPath), builder.toString(), true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
package protocol
|
||||
{}
|
||||
type {} struct {
|
||||
{}
|
||||
}
|
||||
|
||||
func (protocol {}) ProtocolId() int16 {
|
||||
return {}
|
||||
}
|
||||
|
||||
func (protocol {}) write(buffer *ByteBuffer, packet any) {
|
||||
if buffer.WritePacketFlag(packet) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (protocol {}) read(buffer *ByteBuffer) any {
|
||||
var packet = new({})
|
||||
if !buffer.ReadBool() {
|
||||
return packet
|
||||
}
|
||||
return packet
|
||||
}
|
||||
@@ -34,18 +34,7 @@ public class GenerateTesting {
|
||||
var generateLanguages = op.getGenerateLanguages();
|
||||
|
||||
// generate the jsProtocol folder and its corresponding js protocol file in the current protocol directory
|
||||
generateLanguages.add(CodeLanguage.Cpp);
|
||||
generateLanguages.add(CodeLanguage.Go);
|
||||
generateLanguages.add(CodeLanguage.JavaScript);
|
||||
generateLanguages.add(CodeLanguage.TypeScript);
|
||||
generateLanguages.add(CodeLanguage.Lua);
|
||||
generateLanguages.add(CodeLanguage.CSharp);
|
||||
generateLanguages.add(CodeLanguage.GdScript);
|
||||
generateLanguages.add(CodeLanguage.Python);
|
||||
|
||||
// Protobuf needs to specify protobuf.xml to generate a protocol
|
||||
op.setProtocolParam("protobuf=protobuf.xml");
|
||||
generateLanguages.add(CodeLanguage.Protobuf);
|
||||
|
||||
// Initialize and then generate the protocol
|
||||
ProtocolManager.initProtocolAuto(Set.of(ComplexObject.class, NormalObject.class, SimpleObject.class, EmptyObject.class, VeryBigObject.class), op);
|
||||
|
||||
Reference in New Issue
Block a user