Skip to content

Commit

Permalink
ref[lua]: refactor generate lua protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed May 23, 2024
1 parent 07d1b17 commit 7595ce5
Show file tree
Hide file tree
Showing 18 changed files with 428 additions and 411 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.zfoo.protocol.serializer.gdscript.GenerateGdUtils;
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.lua.CodeGenerateLua;
import com.zfoo.protocol.serializer.python.GeneratePyUtils;
import com.zfoo.protocol.serializer.typescript.GenerateTsUtils;
import com.zfoo.protocol.util.FileUtils;
Expand Down Expand Up @@ -172,20 +172,6 @@ public static void generate(GenerateOperation generateOperation) throws IOExcept
GenerateTsUtils.createProtocolManager(generateProtocols);
}

// 生成Lua协议
if (generateLanguages.contains(CodeLanguage.Lua)) {
GenerateLuaUtils.init(generateOperation);
if (generateOperation.isMergeProtocol()) {
GenerateLuaUtils.createProtocolManagerInOneFile(generateProtocols);
GenerateLuaUtils.createLuaProtocolsInOneFile(generateProtocols);
} else {
GenerateLuaUtils.createProtocolManager(generateProtocols);
for (var protocolRegistration : generateProtocols) {
GenerateLuaUtils.createLuaProtocolFile((ProtocolRegistration) protocolRegistration);
}
}
}

// 生成GdScript协议
if (generateLanguages.contains(CodeLanguage.GdScript)) {
GenerateGdUtils.init(generateOperation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

import java.util.*;

import static com.zfoo.protocol.util.StringUtils.TAB;

/**
* EN: When generating the protocol, the document comments and field comments of the protocol will use this class
* CN: 生成协议的时候,协议的文档注释和字段注释会使用这个类
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public static String protocolAbsolutePath(short protocolId, CodeLanguage languag
/**
* 获取协议生成的路径
*/
public static String protocolPath(short protocolId) {
return protocolPathMap.get(protocolId);
}

public static String getProtocolPath(short protocolId) {
AssertionUtils.notNull(protocolPathMap
, "[{}]The initialization has been completed. Get Protocol Path cannot be called after the initialization is completed."
Expand Down Expand Up @@ -134,7 +138,7 @@ public static String getRelativePath(short protocolId, short relativeProtocolId)
return builder.toString();
}

public static Map<String, Set<Short>> getOneProtocolPathMap() {
public static Map<String, Set<Short>> mergerProtocolPathMap() {
var oneProtocolMap = new HashMap<String, Set<Short>>();
for(var entry : protocolPathMap.entrySet()) {
var protocolId = entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import com.zfoo.protocol.serializer.gdscript.GenerateGdUtils;
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.lua.CodeGenerateLua;
import com.zfoo.protocol.serializer.python.GeneratePyUtils;
import com.zfoo.protocol.serializer.reflect.*;
import com.zfoo.protocol.serializer.typescript.GenerateTsUtils;
Expand Down Expand Up @@ -71,7 +71,7 @@ public class ProtocolAnalysis {
, "Boolean", "Byte", "Short", "Integer", "Long", "Float", "Double", "String", "Character", "Object"
, "Collections", "Iterator", "List", "ArrayList", "Map", "HashMap", "Set", "HashSet"
, "DecodedPacketInfo", "EncodedPacketInfo"
, "Protocols", "ProtocolBase", "ProtocolWriter", "ProtocolReader");
, "Protocols");

/**
* EN: Temp field, unsupported type
Expand Down Expand Up @@ -404,7 +404,6 @@ private static void enhanceProtocolAfter(GenerateOperation generateOperation) {
GenerateGoUtils.clear();
GenerateJsUtils.clear();
GenerateTsUtils.clear();
GenerateLuaUtils.clear();
GenerateGdUtils.clear();
GeneratePyUtils.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package com.zfoo.protocol.serializer;

import com.zfoo.protocol.serializer.csharp.CodeGenerateCsharp;
import com.zfoo.protocol.serializer.lua.CodeGenerateLua;

/**
* @author godotg
Expand All @@ -34,7 +35,7 @@ public enum CodeLanguage {

TypeScript(1 << 5, null),

Lua(1 << 10, null),
Lua(1 << 10, CodeGenerateLua.class),

CSharp(1 << 11, CodeGenerateCsharp.class),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,43 +81,43 @@ public void init(GenerateOperation generateOperation) {
@Override
public void mergerProtocol(List<IProtocolRegistration> registrations) throws IOException {
createProtocolManagerFile(registrations);
var oneProtocolPathMap = GenerateProtocolPath.getOneProtocolPathMap();
for (var entry : oneProtocolPathMap.entrySet()) {
var path = entry.getKey();
var mergerProtocolPathMap = GenerateProtocolPath.mergerProtocolPathMap();
for (var entry : mergerProtocolPathMap.entrySet()) {
var protocol_merger_name = StringUtils.capitalize(entry.getKey());
var protocolIds = entry.getValue().stream().sorted().toList();
var protocolBuilder = new StringBuilder();
var protocolRegistrationBuilder = new StringBuilder();
for (var protocolId : protocolIds) {
var protocol_class = new StringBuilder();
var protocol_registration = new StringBuilder();
for (var protocol_id : protocolIds) {
GenerateProtocolFile.index.set(0);
var registration = (ProtocolRegistration) ProtocolManager.getProtocol(protocolId);
var protocolClazzName = registration.protocolConstructor().getDeclaringClass().getSimpleName();
var registration = (ProtocolRegistration) ProtocolManager.getProtocol(protocol_id);
var protocol_class_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();

// protocol
var protocolClassTemplate = ClassUtils.getFileFromClassPathToString("csharp/ProtocolClassTemplate.cs");
var placeholderMap = Map.of(
CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocolId, CodeLanguage.CSharp)
, CodeTemplatePlaceholder.protocol_name, protocolClazzName
, CodeTemplatePlaceholder.protocol_id, String.valueOf(protocolId)
CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocol_id, CodeLanguage.CSharp)
, CodeTemplatePlaceholder.protocol_name, protocol_class_name
, CodeTemplatePlaceholder.protocol_id, String.valueOf(protocol_id)
, CodeTemplatePlaceholder.protocol_field_definition, protocol_field_definition(registration)
, CodeTemplatePlaceholder.protocol_write_serialization, protocol_write_serialization(registration)
, CodeTemplatePlaceholder.protocol_read_deserialization, protocol_read_deserialization(registration)
);
var formatProtocolClassTemplate = CodeTemplatePlaceholder.formatTemplate(protocolClassTemplate, placeholderMap);
protocolBuilder.append(formatProtocolClassTemplate).append(LS);
protocol_class.append(formatProtocolClassTemplate).append(LS);

// registration
var protocolRegistrationTemplate = ClassUtils.getFileFromClassPathToString("csharp/ProtocolRegistrationTemplate.cs");
var formatProtocolRegistrationTemplate = CodeTemplatePlaceholder.formatTemplate(protocolRegistrationTemplate, placeholderMap);
protocolRegistrationBuilder.append(formatProtocolRegistrationTemplate);
protocol_registration.append(formatProtocolRegistrationTemplate);

}

var protocolTemplate = ClassUtils.getFileFromClassPathToString("csharp/ProtocolTemplate.cs");
var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of(
CodeTemplatePlaceholder.protocol_class, protocolBuilder.toString()
, CodeTemplatePlaceholder.protocol_registration, protocolRegistrationBuilder.toString()
CodeTemplatePlaceholder.protocol_class, protocol_class.toString()
, CodeTemplatePlaceholder.protocol_registration, protocol_registration.toString()
));
var outputPath = StringUtils.format("{}/{}.cs", protocolOutputPath, StringUtils.capitalize(path));
var outputPath = StringUtils.format("{}/{}.cs", protocolOutputPath, protocol_merger_name);
var file = new File(outputPath);
FileUtils.writeStringToFile(file, formatProtocolTemplate, true);
logger.info("Generated C# protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath());
Expand All @@ -131,7 +131,7 @@ public void foldProtocol(List<IProtocolRegistration> registrations) throws IOExc
for (var registration : registrations) {
var protocolId = registration.protocolId();
var protocolClazzName = registration.protocolConstructor().getDeclaringClass().getSimpleName();
var formatProtocolTemplate = protocolTemplate((ProtocolRegistration) registration);
var formatProtocolTemplate = formatProtocolTemplate((ProtocolRegistration) registration);
var outputPath = StringUtils.format("{}/{}/{}.cs", protocolOutputPath, GenerateProtocolPath.getCapitalizeProtocolPath(protocolId), protocolClazzName);
var file = new File(outputPath);
FileUtils.writeStringToFile(file, formatProtocolTemplate, true);
Expand All @@ -144,9 +144,9 @@ public void defaultProtocol(List<IProtocolRegistration> registrations) throws IO
createProtocolManagerFile(registrations);

for (var registration : registrations) {
var protocolClazzName = registration.protocolConstructor().getDeclaringClass().getSimpleName();
var formatProtocolTemplate = protocolTemplate((ProtocolRegistration) registration);
var outputPath = StringUtils.format("{}/{}.cs", protocolOutputPath, protocolClazzName);
var protocol_class_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
var formatProtocolTemplate = formatProtocolTemplate((ProtocolRegistration) registration);
var outputPath = StringUtils.format("{}/{}.cs", protocolOutputPath, protocol_class_name);
var file = new File(outputPath);
FileUtils.writeStringToFile(file, formatProtocolTemplate, true);
logger.info("Generated C# protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath());
Expand All @@ -165,29 +165,29 @@ private void createProtocolManagerFile(List<IProtocolRegistration> registrations
FileUtils.writeInputStreamToFile(createFile, fileInputStream);
}

var template = ClassUtils.getFileFromClassPathToString("csharp/ProtocolManagerTemplate.cs");
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("csharp/ProtocolManagerTemplate.cs");
var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_manager_registrations, protocol_manager_registrations(registrations));
var formatTemplate = CodeTemplatePlaceholder.formatTemplate(template, placeholderMap);
var formatProtocolManagerTemplate = CodeTemplatePlaceholder.formatTemplate(protocolManagerTemplate, placeholderMap);

var file = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.cs"));
FileUtils.writeStringToFile(file, formatTemplate, true);
FileUtils.writeStringToFile(file, formatProtocolManagerTemplate, true);
logger.info("Generated C# protocol manager file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath());
}


private String protocolTemplate(ProtocolRegistration registration) {
private String formatProtocolTemplate(ProtocolRegistration registration) {
GenerateProtocolFile.index.set(0);

var protocolId = registration.protocolId();
var protocolClazzName = registration.getConstructor().getDeclaringClass().getSimpleName();
var protocol_id = registration.protocolId();
var protocol_class_name = registration.getConstructor().getDeclaringClass().getSimpleName();

var protocolClassTemplate = ClassUtils.getFileFromClassPathToString("csharp/ProtocolClassTemplate.cs");
var protocolRegistrationTemplate = ClassUtils.getFileFromClassPathToString("csharp/ProtocolRegistrationTemplate.cs");
var protocolTemplate = ClassUtils.getFileFromClassPathToString("csharp/ProtocolTemplate.cs");
var placeholderMap = Map.of(
CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocolId, CodeLanguage.CSharp)
, CodeTemplatePlaceholder.protocol_name, protocolClazzName
, CodeTemplatePlaceholder.protocol_id, String.valueOf(protocolId)
CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocol_id, CodeLanguage.CSharp)
, CodeTemplatePlaceholder.protocol_name, protocol_class_name
, CodeTemplatePlaceholder.protocol_id, String.valueOf(protocol_id)
, CodeTemplatePlaceholder.protocol_field_definition, protocol_field_definition(registration)
, CodeTemplatePlaceholder.protocol_write_serialization, protocol_write_serialization(registration)
, CodeTemplatePlaceholder.protocol_read_deserialization, protocol_read_deserialization(registration)
Expand Down
Loading

0 comments on commit 7595ce5

Please sign in to comment.