From 7595ce584dd9abd7b6495a5e74e00ed5d1e14a32 Mon Sep 17 00:00:00 2001 From: godotg Date: Thu, 23 May 2024 14:27:32 +0800 Subject: [PATCH] ref[lua]: refactor generate lua protocol --- .../generate/GenerateProtocolFile.java | 16 +- .../generate/GenerateProtocolNote.java | 2 - .../generate/GenerateProtocolPath.java | 6 +- .../registration/ProtocolAnalysis.java | 5 +- .../protocol/serializer/CodeLanguage.java | 3 +- .../serializer/csharp/CodeGenerateCsharp.java | 58 ++-- .../serializer/lua/CodeGenerateLua.java | 323 ++++++++++++++++++ .../serializer/lua/GenerateLuaUtils.java | 300 ---------------- .../serializer/lua/LuaArraySerializer.java | 4 +- .../serializer/lua/LuaListSerializer.java | 4 +- .../serializer/lua/LuaMapSerializer.java | 8 +- .../serializer/lua/LuaSetSerializer.java | 4 +- .../resources/lua/ProtocolClassTemplate.lua | 11 + .../resources/lua/ProtocolManagerTemplate.lua | 6 +- .../lua/ProtocolRegistrationTemplate.lua | 33 ++ .../main/resources/lua/ProtocolTemplate.lua | 48 +-- .../main/resources/lua/ProtocolsTemplate.lua | 7 + protocol/src/test/lua/Main.cs | 1 - 18 files changed, 428 insertions(+), 411 deletions(-) create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/lua/CodeGenerateLua.java delete mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/lua/GenerateLuaUtils.java create mode 100644 protocol/src/main/resources/lua/ProtocolClassTemplate.lua create mode 100644 protocol/src/main/resources/lua/ProtocolRegistrationTemplate.lua create mode 100644 protocol/src/main/resources/lua/ProtocolsTemplate.lua diff --git a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java index a8fe7d9f8..17ed9962f 100644 --- a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java +++ b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java @@ -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; @@ -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); diff --git a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolNote.java b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolNote.java index ac9307343..0dacd79ca 100644 --- a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolNote.java +++ b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolNote.java @@ -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: 生成协议的时候,协议的文档注释和字段注释会使用这个类 diff --git a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolPath.java b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolPath.java index 3a45ebb87..3ad54b678 100644 --- a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolPath.java +++ b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolPath.java @@ -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." @@ -134,7 +138,7 @@ public static String getRelativePath(short protocolId, short relativeProtocolId) return builder.toString(); } - public static Map> getOneProtocolPathMap() { + public static Map> mergerProtocolPathMap() { var oneProtocolMap = new HashMap>(); for(var entry : protocolPathMap.entrySet()) { var protocolId = entry.getKey(); diff --git a/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java b/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java index 0697e8792..1981bf5b5 100644 --- a/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java +++ b/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java @@ -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; @@ -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 @@ -404,7 +404,6 @@ private static void enhanceProtocolAfter(GenerateOperation generateOperation) { GenerateGoUtils.clear(); GenerateJsUtils.clear(); GenerateTsUtils.clear(); - GenerateLuaUtils.clear(); GenerateGdUtils.clear(); GeneratePyUtils.clear(); } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java index e08f70266..4951a743b 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java @@ -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 @@ -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), diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/csharp/CodeGenerateCsharp.java b/protocol/src/main/java/com/zfoo/protocol/serializer/csharp/CodeGenerateCsharp.java index f30f3e8db..8458f98a8 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/csharp/CodeGenerateCsharp.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/csharp/CodeGenerateCsharp.java @@ -81,43 +81,43 @@ public void init(GenerateOperation generateOperation) { @Override public void mergerProtocol(List 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()); @@ -131,7 +131,7 @@ public void foldProtocol(List 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); @@ -144,9 +144,9 @@ public void defaultProtocol(List 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()); @@ -165,29 +165,29 @@ private void createProtocolManagerFile(List 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) diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/CodeGenerateLua.java b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/CodeGenerateLua.java new file mode 100644 index 000000000..676e8137f --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/CodeGenerateLua.java @@ -0,0 +1,323 @@ +/* + * 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.lua; + +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.IProtocolRegistration; +import com.zfoo.protocol.registration.ProtocolRegistration; +import com.zfoo.protocol.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.CodeTemplatePlaceholder; +import com.zfoo.protocol.serializer.ICodeGenerate; +import com.zfoo.protocol.serializer.csharp.CodeGenerateCsharp; +import com.zfoo.protocol.serializer.reflect.*; +import com.zfoo.protocol.util.ClassUtils; +import com.zfoo.protocol.util.FileUtils; +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 CodeGenerateLua implements ICodeGenerate { + private static final Logger logger = LoggerFactory.getLogger(CodeGenerateLua.class); + // custom configuration + public static String protocolOutputRootPath = "zfoolua"; + private static String protocolOutputPath = StringUtils.EMPTY; + + private static final Map luaSerializerMap = new HashMap<>(); + + public static ILuaSerializer luaSerializer(ISerializer serializer) { + return luaSerializerMap.get(serializer); + } + + @Override + public void init(GenerateOperation generateOperation) { + protocolOutputPath = FileUtils.joinPath(generateOperation.getProtocolPath(), protocolOutputRootPath); + FileUtils.deleteFile(new File(protocolOutputPath)); + + luaSerializerMap.put(BooleanSerializer.INSTANCE, new LuaBooleanSerializer()); + luaSerializerMap.put(ByteSerializer.INSTANCE, new LuaByteSerializer()); + luaSerializerMap.put(ShortSerializer.INSTANCE, new LuaShortSerializer()); + luaSerializerMap.put(IntSerializer.INSTANCE, new LuaIntSerializer()); + luaSerializerMap.put(LongSerializer.INSTANCE, new LuaLongSerializer()); + luaSerializerMap.put(FloatSerializer.INSTANCE, new LuaFloatSerializer()); + luaSerializerMap.put(DoubleSerializer.INSTANCE, new LuaDoubleSerializer()); + luaSerializerMap.put(StringSerializer.INSTANCE, new LuaStringSerializer()); + luaSerializerMap.put(ArraySerializer.INSTANCE, new LuaArraySerializer()); + luaSerializerMap.put(ListSerializer.INSTANCE, new LuaListSerializer()); + luaSerializerMap.put(SetSerializer.INSTANCE, new LuaSetSerializer()); + luaSerializerMap.put(MapSerializer.INSTANCE, new LuaMapSerializer()); + luaSerializerMap.put(ObjectProtocolSerializer.INSTANCE, new LuaObjectProtocolSerializer()); + } + + @Override + public void mergerProtocol(List registrations) throws IOException { + createTemplateFile(); + + var protocol_manager_registrations = new StringBuilder(); + protocol_manager_registrations.append(StringUtils.format("local Protocols = require(\"{}.Protocols\")", protocolOutputRootPath)).append(LS); + for (var registration : registrations) { + var protocol_id = registration.protocolId(); + var protocol_class_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); + protocol_manager_registrations.append(StringUtils.format("protocols[{}] = Protocols.{}", protocol_id, protocol_class_name)).append(LS); + } + var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("lua/ProtocolManagerTemplate.lua"); + var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_manager_registrations, protocol_manager_registrations.toString()); + var formatProtocolManagerTemplate = CodeTemplatePlaceholder.formatTemplate(protocolManagerTemplate, placeholderMap); + var protocolManagerFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.lua")); + FileUtils.writeStringToFile(protocolManagerFile, formatProtocolManagerTemplate, true); + logger.info("Generated Lua protocol manager file:[{}] is in path:[{}]", protocolManagerFile.getName(), protocolManagerFile.getAbsolutePath()); + + + var protocol_class = new StringBuilder(); + var protocol_registration = new StringBuilder(); + for (var registration : registrations) { + GenerateProtocolFile.index.set(0); + var protocol_id = registration.protocolId(); + var protocol_class_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); + + // protocol + protocol_class.append(protocol_class((ProtocolRegistration) registration)).append(LS); + + // registration + protocol_registration.append(protocol_registration((ProtocolRegistration) registration)).append(LS); + } + + protocol_registration.append(LS); + for (var registration : registrations) { + var protocol_class_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); + protocol_registration.append(StringUtils.format("Protocols.{} = {}", protocol_class_name, protocol_class_name)).append(LS); + } + + var protocolTemplate = ClassUtils.getFileFromClassPathToString("lua/ProtocolsTemplate.lua"); + var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of( + CodeTemplatePlaceholder.protocol_class, protocol_class.toString() + , CodeTemplatePlaceholder.protocol_registration, protocol_registration.toString() + )); + var outputPath = StringUtils.format("{}/Protocols.lua", protocolOutputPath); + var file = new File(outputPath); + FileUtils.writeStringToFile(file, formatProtocolTemplate, true); + logger.info("Generated C# protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath()); + + } + + @Override + public void foldProtocol(List registrations) throws IOException { + createTemplateFile(); + + var protocolManagerRequireBuilder = new StringBuilder(); + var protocol_manager_registrations = new StringBuilder(); + for (var registration : registrations) { + var protocol_id = registration.protocolId(); + var protocol_class_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); + var path = GenerateProtocolPath.protocolPath(protocol_id); + protocolManagerRequireBuilder.append(StringUtils.format("local {} = require(\"{}.{}.{}\")", protocol_class_name, protocolOutputRootPath, path.replace(StringUtils.SLASH, StringUtils.PERIOD), protocol_class_name)).append(LS); + protocol_manager_registrations.append(StringUtils.format("protocols[{}] = {}", protocol_id, protocol_class_name)).append(LS); + } + + var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("lua/ProtocolManagerTemplate.lua"); + var pmPlaceholderMap = Map.of(CodeTemplatePlaceholder.protocol_manager_registrations, protocolManagerRequireBuilder.toString() + protocol_manager_registrations); + var formatProtocolManagerTemplate = CodeTemplatePlaceholder.formatTemplate(protocolManagerTemplate, pmPlaceholderMap); + var protocolManagerFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.lua")); + FileUtils.writeStringToFile(protocolManagerFile, formatProtocolManagerTemplate, true); + logger.info("Generated Lua protocol manager file:[{}] is in path:[{}]", protocolManagerFile.getName(), protocolManagerFile.getAbsolutePath()); + + + for (var registration : registrations) { + var protocol_id = registration.protocolId(); + var protocol_class_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); + var formatProtocolTemplate = formatProtocolTemplate((ProtocolRegistration) registration); + var outputPath = StringUtils.format("{}/{}/{}.lua", protocolOutputPath, GenerateProtocolPath.protocolPath(protocol_id), protocol_class_name); + var file = new File(outputPath); + FileUtils.writeStringToFile(file, formatProtocolTemplate, true); + logger.info("Generated Lua protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath()); + } + } + + @Override + public void defaultProtocol(List registrations) throws IOException { + createTemplateFile(); + + var protocolManagerRequireBuilder = new StringBuilder(); + var protocol_manager_registrations = new StringBuilder(); + for (var registration : registrations) { + var protocol_id = registration.protocolId(); + var protocol_class_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); + protocolManagerRequireBuilder.append(StringUtils.format("local {} = require(\"{}.{}\")", protocol_class_name, protocolOutputRootPath, protocol_class_name)).append(LS); + protocol_manager_registrations.append(StringUtils.format("protocols[{}] = {}", protocol_id, protocol_class_name)).append(LS); + } + var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("lua/ProtocolManagerTemplate.lua"); + var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_manager_registrations, protocolManagerRequireBuilder.toString() + protocol_manager_registrations); + var formatProtocolManagerTemplate = CodeTemplatePlaceholder.formatTemplate(protocolManagerTemplate, placeholderMap); + var protocolManagerFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.lua")); + FileUtils.writeStringToFile(protocolManagerFile, formatProtocolManagerTemplate, true); + logger.info("Generated Lua protocol manager file:[{}] is in path:[{}]", protocolManagerFile.getName(), protocolManagerFile.getAbsolutePath()); + + + for (var registration : registrations) { + var protocol_class_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); + var formatProtocolTemplate = formatProtocolTemplate((ProtocolRegistration) registration); + var outputPath = StringUtils.format("{}/{}.lua", protocolOutputPath, protocol_class_name); + var file = new File(outputPath); + FileUtils.writeStringToFile(file, formatProtocolTemplate, true); + logger.info("Generated Lua protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath()); + } + } + + private void createTemplateFile() throws IOException { + var list = List.of("lua/Long.lua"); + for (var fileName : list) { + var fileInputStream = ClassUtils.getFileFromClassPath(fileName); + var createFile = new File(StringUtils.format("{}/{}", protocolOutputPath, StringUtils.substringAfterFirst(fileName, "lua/"))); + FileUtils.writeInputStreamToFile(createFile, fileInputStream); + } + + var byteBufferFileName = "lua/ByteBuffer.lua"; + var byteBufferTemplate = ClassUtils.getFileFromClassPathToString(byteBufferFileName); + var byteBufferFile = new File(StringUtils.format("{}/{}", protocolOutputPath, StringUtils.substringAfterFirst(byteBufferFileName, "lua/"))); + FileUtils.writeStringToFile(byteBufferFile, StringUtils.format(byteBufferTemplate, protocolOutputRootPath), false); + } + + public String formatProtocolTemplate(ProtocolRegistration registration) { + // 初始化index + GenerateProtocolFile.index.set(0); + + var protocol_class_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); + var protocolTemplate = ClassUtils.getFileFromClassPathToString("lua/ProtocolTemplate.lua"); + + return CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of( + CodeTemplatePlaceholder.protocol_class, protocol_class(registration) + , CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration) + , CodeTemplatePlaceholder.protocol_name, protocol_class_name + )); + } + + private String protocol_class(ProtocolRegistration registration) { + var protocol_id = registration.protocolId(); + var protocol_class_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); + + var protocolClassTemplate = ClassUtils.getFileFromClassPathToString("lua/ProtocolClassTemplate.lua"); + var placeholderMap = Map.of( + CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocol_id, CodeLanguage.Lua) + , 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) + ); + return CodeTemplatePlaceholder.formatTemplate(protocolClassTemplate, placeholderMap); + } + + private String protocol_registration(ProtocolRegistration registration) { + var protocol_id = registration.protocolId(); + var protocol_class_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); + + var protocolRegistrationTemplate = ClassUtils.getFileFromClassPathToString("lua/ProtocolRegistrationTemplate.lua"); + var placeholderMap = Map.of( + CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocol_id, CodeLanguage.Lua) + , 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) + ); + return CodeTemplatePlaceholder.formatTemplate(protocolRegistrationTemplate, placeholderMap); + } + + + private String protocol_field_definition(ProtocolRegistration registration) { + var protocolId = registration.getId(); + var fields = registration.getFields(); + var fieldRegistrations = registration.getFieldRegistrations(); + var luaBuilder = new StringBuilder(); + + for (var i = 0; i < fields.length; i++) { + var field = fields[i]; + var fieldRegistration = fieldRegistrations[i]; + var fieldName = field.getName(); + // 生成注释 + var fieldNotes = GenerateProtocolNote.fieldNotes(protocolId, fieldName, CodeLanguage.Lua); + for (var fieldNote : fieldNotes) { + luaBuilder.append(fieldNote).append(LS); + } + var fieldDefaultValue = luaSerializer(fieldRegistration.serializer()).fieldDefaultValue(field, fieldRegistration); + if (i == fields.length - 1) { + luaBuilder.append(StringUtils.format("{} = {}", fieldName, fieldDefaultValue)); + } else { + luaBuilder.append(StringUtils.format("{} = {},", fieldName, fieldDefaultValue)); + } + var typeNote = CodeGenerateCsharp.toCsClassName(field.getGenericType().getTypeName()); + // 生成类型的注释 + luaBuilder.append(" -- ").append(typeNote).append(LS); + } + return luaBuilder.toString(); + } + + private String protocol_write_serialization(ProtocolRegistration registration) { + var fields = registration.getFields(); + var fieldRegistrations = registration.getFieldRegistrations(); + var luaBuilder = new StringBuilder(); + if (registration.isCompatible()) { + luaBuilder.append("local beforeWriteIndex = buffer:getWriteOffset()").append(LS); + luaBuilder.append(StringUtils.format("buffer:writeInt({})", registration.getPredictionLength())).append(LS); + } else { + luaBuilder.append("buffer:writeInt(-1)").append(LS); + } + for (var i = 0; i < fields.length; i++) { + var field = fields[i]; + var fieldRegistration = fieldRegistrations[i]; + + luaSerializer(fieldRegistration.serializer()).writeObject(luaBuilder, "packet." + field.getName(), 0, field, fieldRegistration); + } + if (registration.isCompatible()) { + luaBuilder.append(StringUtils.format("buffer:adjustPadding({}, beforeWriteIndex)", registration.getPredictionLength())).append(LS); + } + return luaBuilder.toString(); + } + + private String protocol_read_deserialization(ProtocolRegistration registration) { + var fields = registration.getFields(); + var fieldRegistrations = registration.getFieldRegistrations(); + var luaBuilder = new StringBuilder(); + for (int i = 0; i < fields.length; i++) { + var field = fields[i]; + var fieldRegistration = fieldRegistrations[i]; + if (field.isAnnotationPresent(Compatible.class)) { + luaBuilder.append("if buffer:compatibleRead(beforeReadIndex, length) then").append(LS); + var compatibleReadObject = luaSerializer(fieldRegistration.serializer()).readObject(luaBuilder, 1, field, fieldRegistration); + luaBuilder.append(TAB).append(StringUtils.format("packet.{} = {}", field.getName(), compatibleReadObject)).append(LS); + luaBuilder.append("end").append(LS); + continue; + } + var readObject = luaSerializer(fieldRegistration.serializer()).readObject(luaBuilder, 0, field, fieldRegistration); + luaBuilder.append(StringUtils.format("packet.{} = {}", field.getName(), readObject)).append(LS); + } + return luaBuilder.toString(); + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/GenerateLuaUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/GenerateLuaUtils.java deleted file mode 100644 index 8700d37ff..000000000 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/GenerateLuaUtils.java +++ /dev/null @@ -1,300 +0,0 @@ -/* - * 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.lua; - -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.IProtocolRegistration; -import com.zfoo.protocol.registration.ProtocolRegistration; -import com.zfoo.protocol.serializer.CodeLanguage; -import com.zfoo.protocol.serializer.csharp.CodeGenerateCsharp; -import com.zfoo.protocol.serializer.reflect.*; -import com.zfoo.protocol.util.ClassUtils; -import com.zfoo.protocol.util.FileUtils; -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 abstract class GenerateLuaUtils { - private static final Logger logger = LoggerFactory.getLogger(GenerateLuaUtils.class); - // custom configuration - public static String protocolOutputRootPath = "zfoolua"; - private static String protocolOutputPath = StringUtils.EMPTY; - - private static Map luaSerializerMap; - - public static ILuaSerializer luaSerializer(ISerializer serializer) { - return luaSerializerMap.get(serializer); - } - - public static void init(GenerateOperation generateOperation) { - protocolOutputPath = FileUtils.joinPath(generateOperation.getProtocolPath(), protocolOutputRootPath); - FileUtils.deleteFile(new File(protocolOutputPath)); - - luaSerializerMap = new HashMap<>(); - luaSerializerMap.put(BooleanSerializer.INSTANCE, new LuaBooleanSerializer()); - luaSerializerMap.put(ByteSerializer.INSTANCE, new LuaByteSerializer()); - luaSerializerMap.put(ShortSerializer.INSTANCE, new LuaShortSerializer()); - luaSerializerMap.put(IntSerializer.INSTANCE, new LuaIntSerializer()); - luaSerializerMap.put(LongSerializer.INSTANCE, new LuaLongSerializer()); - luaSerializerMap.put(FloatSerializer.INSTANCE, new LuaFloatSerializer()); - luaSerializerMap.put(DoubleSerializer.INSTANCE, new LuaDoubleSerializer()); - luaSerializerMap.put(StringSerializer.INSTANCE, new LuaStringSerializer()); - luaSerializerMap.put(ArraySerializer.INSTANCE, new LuaArraySerializer()); - luaSerializerMap.put(ListSerializer.INSTANCE, new LuaListSerializer()); - luaSerializerMap.put(SetSerializer.INSTANCE, new LuaSetSerializer()); - luaSerializerMap.put(MapSerializer.INSTANCE, new LuaMapSerializer()); - luaSerializerMap.put(ObjectProtocolSerializer.INSTANCE, new LuaObjectProtocolSerializer()); - } - - public static void clear() { - luaSerializerMap = null; - protocolOutputRootPath = null; - protocolOutputPath = null; - } - - // All protocol files are generated in a single protocol file. - public static void createProtocolManagerInOneFile(List protocolList) throws IOException { - createTemplateFile(); - - // 生成Protocol.lua文件 - var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("lua-one/ProtocolManagerTemplate.lua"); - var builderImports = new StringBuilder(); - builderImports.append(TAB).append(StringUtils.format("local Protocols = require(\"{}.Protocols\")", protocolOutputRootPath)).append(LS); - builderImports.append(TAB).append(StringUtils.format("local ProtocolBase = require(\"{}.ProtocolBase\")", protocolOutputRootPath)).append(LS); - builderImports.append(TAB).append(StringUtils.format("local ProtocolWriter = require(\"{}.ProtocolWriter\")", protocolOutputRootPath)).append(LS); - builderImports.append(TAB).append(StringUtils.format("local ProtocolReader = require(\"{}.ProtocolReader\")", protocolOutputRootPath)).append(LS); - var protocolBuilder = new StringBuilder(); - for (var protocol : protocolList) { - var protocolId = protocol.protocolId(); - var protocolName = protocol.protocolConstructor().getDeclaringClass().getSimpleName(); - protocolBuilder.append(TAB).append(StringUtils.format("protocols[{}] = Protocols.{}", protocolId, protocolName)).append(LS); - } - protocolManagerTemplate = StringUtils.format(protocolManagerTemplate, StringUtils.EMPTY_JSON, StringUtils.EMPTY_JSON, builderImports.toString().trim(), protocolBuilder.toString().trim()); - var file = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.lua")); - FileUtils.writeStringToFile(file, protocolManagerTemplate, true); - logger.info("Generated Lua protocol manager file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath()); - } - - public static void createLuaProtocolsInOneFile(List registrations) { - var protocolTemplate = ClassUtils.getFileFromClassPathToString("lua-one/ProtocolTemplate.lua"); - var protocolBaseTemplate = ClassUtils.getFileFromClassPathToString("lua-one/ProtocolBaseTemplate.lua"); - var protocolWriterTemplate = ClassUtils.getFileFromClassPathToString("lua-one/ProtocolWriterTemplate.lua"); - var protocolReaderTemplate = ClassUtils.getFileFromClassPathToString("lua-one/ProtocolReaderTemplate.lua"); - var builderProtocol = new StringBuilder(); - var builderProtocolBase = new StringBuilder(); - var builderProtocolWriter = new StringBuilder(); - var builderProtocolReader = new StringBuilder(); - - var requireProtocols = StringUtils.format("local Protocols = require(\"{}.Protocols\")", protocolOutputRootPath); - builderProtocolBase.append(requireProtocols).append(LS).append(LS); - builderProtocolWriter.append(requireProtocols).append(LS).append(LS); - builderProtocolReader.append(requireProtocols).append(LS).append(LS); - - for (var protocolRegistration : registrations) { - var registration = (ProtocolRegistration) protocolRegistration; - var protocolId = registration.protocolId(); - var registrationConstructor = registration.getConstructor(); - var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName(); - GenerateProtocolFile.index.set(0); - - var classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.Lua, TAB, 0); - var valueOfMethod = valueOfMethod(registration); - var writePacket = writePacket(registration); - var readPacket = readPacket(registration); - - var protocol = StringUtils.format(protocolTemplate, classNote, protocolClazzName, StringUtils.EMPTY_JSON, protocolClazzName, valueOfMethod.trim()); - var protocolBase = StringUtils.format(protocolBaseTemplate, protocolClazzName, protocolId, protocolClazzName, protocolClazzName, protocolClazzName); - var protocolWriter = StringUtils.format(protocolWriterTemplate, protocolClazzName, writePacket.trim()); - var protocolReader = StringUtils.format(protocolReaderTemplate, protocolClazzName, protocolClazzName, readPacket.trim()); - - builderProtocol.append(protocol).append(LS); - builderProtocolBase.append(protocolBase).append(LS).append(LS); - builderProtocolWriter.append(protocolWriter).append(LS).append(LS); - builderProtocolReader.append(protocolReader).append(LS).append(LS); - } - - builderProtocol.append(LS).append("local Protocols = {}").append(LS); - for (var protocolRegistration : registrations) { - var registration = (ProtocolRegistration) protocolRegistration; - var registrationConstructor = registration.getConstructor(); - var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName(); - builderProtocol.append(StringUtils.format("Protocols.{} = {}", protocolClazzName, protocolClazzName)).append(LS); - } - builderProtocol.append("return Protocols"); - var protocolsFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "Protocols.lua")); - var protocolBaseFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolBase.lua")); - var protocolWriterFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolWriter.lua")); - var protocolReaderFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolReader.lua")); - FileUtils.writeStringToFile(protocolsFile, builderProtocol.toString(), true); - FileUtils.writeStringToFile(protocolBaseFile, builderProtocolBase.toString(), true); - FileUtils.writeStringToFile(protocolWriterFile, builderProtocolWriter.toString(), true); - FileUtils.writeStringToFile(protocolReaderFile, builderProtocolReader.toString(), true); - logger.info("Generated Lua protocols file:[{}] is in path:[{}]", protocolsFile.getName(), protocolsFile.getAbsolutePath()); - logger.info("Generated Lua protocol base file:[{}] is in path:[{}]", protocolBaseFile.getName(), protocolBaseFile.getAbsolutePath()); - logger.info("Generated Lua protocol writer file:[{}] is in path:[{}]", protocolWriterFile.getName(), protocolWriterFile.getAbsolutePath()); - logger.info("Generated Lua protocol reader file:[{}] is in path:[{}]", protocolReaderFile.getName(), protocolReaderFile.getAbsolutePath()); - } - - public static void createProtocolManager(List protocolList) throws IOException { - createTemplateFile(); - - // 生成Protocol.lua文件 - var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("lua/ProtocolManagerTemplate.lua"); - var fieldBuilder = new StringBuilder(); - var protocolBuilder = new StringBuilder(); - for (var protocol : protocolList) { - var protocolId = protocol.protocolId(); - var protocolName = protocol.protocolConstructor().getDeclaringClass().getSimpleName(); - var path = GenerateProtocolPath.getProtocolPath(protocolId); - if (StringUtils.isBlank(path)) { - fieldBuilder.append(TAB).append(StringUtils.format("local {} = require(\"{}.{}\")", protocolName, protocolOutputRootPath, protocolName)).append(LS); - } else { - fieldBuilder.append(TAB).append(StringUtils.format("local {} = require(\"{}.{}.{}\")" - , protocolName, protocolOutputRootPath, path.replaceAll(StringUtils.SLASH, StringUtils.PERIOD), protocolName)).append(LS); - } - - protocolBuilder.append(TAB).append(StringUtils.format("protocols[{}] = {}", protocolId, protocolName)).append(LS); - } - protocolManagerTemplate = StringUtils.format(protocolManagerTemplate, StringUtils.EMPTY_JSON, StringUtils.EMPTY_JSON, fieldBuilder.toString().trim(), protocolBuilder.toString().trim()); - var file = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.lua")); - FileUtils.writeStringToFile(file, protocolManagerTemplate, true); - logger.info("Generated Lua protocol manager file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath()); - } - - private static void createTemplateFile() throws IOException { - var list = List.of("lua/Long.lua"); - for (var fileName : list) { - var fileInputStream = ClassUtils.getFileFromClassPath(fileName); - var createFile = new File(StringUtils.format("{}/{}", protocolOutputPath, StringUtils.substringAfterFirst(fileName, "lua/"))); - FileUtils.writeInputStreamToFile(createFile, fileInputStream); - } - - var byteBufferFileName = "lua/ByteBuffer.lua"; - var byteBufferTemplate = ClassUtils.getFileFromClassPathToString(byteBufferFileName); - var byteBufferFile = new File(StringUtils.format("{}/{}", protocolOutputPath, StringUtils.substringAfterFirst(byteBufferFileName, "lua/"))); - FileUtils.writeStringToFile(byteBufferFile, StringUtils.format(byteBufferTemplate, protocolOutputRootPath), false); - } - - public static void createLuaProtocolFile(ProtocolRegistration registration) { - // 初始化index - GenerateProtocolFile.index.set(0); - - var protocolId = registration.protocolId(); - var registrationConstructor = registration.getConstructor(); - var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName(); - var protocolTemplate = ClassUtils.getFileFromClassPathToString("lua/ProtocolTemplate.lua"); - - var classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.Lua, TAB, 0); - var valueOfMethod = valueOfMethod(registration); - var writePacket = writePacket(registration); - var readPacket = readPacket(registration); - - protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName, StringUtils.EMPTY_JSON, protocolClazzName - , valueOfMethod.trim(), protocolClazzName, protocolId, protocolClazzName, protocolClazzName - , protocolClazzName, protocolClazzName, writePacket.trim(), protocolClazzName, protocolClazzName, readPacket.trim(), protocolClazzName); - - var outputPath = StringUtils.format("{}/{}/{}.lua" - , protocolOutputPath, GenerateProtocolPath.getProtocolPath(protocolId), protocolClazzName); - var file = new File(outputPath); - FileUtils.writeStringToFile(file, protocolTemplate, true); - logger.info("Generated Lua protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath()); - } - - private static String valueOfMethod(ProtocolRegistration registration) { - var protocolId = registration.getId(); - var fields = registration.getFields(); - var fieldRegistrations = registration.getFieldRegistrations(); - var luaBuilder = new StringBuilder(); - - for (var i = 0; i < fields.length; i++) { - var field = fields[i]; - var fieldRegistration = fieldRegistrations[i]; - var fieldName = field.getName(); - // 生成注释 - var fieldNotes = GenerateProtocolNote.fieldNotes(protocolId, fieldName, CodeLanguage.Lua); - for(var fieldNote : fieldNotes) { - luaBuilder.append(TAB + TAB).append(fieldNote).append(LS); - } - var fieldDefaultValue = luaSerializer(fieldRegistration.serializer()).fieldDefaultValue(field, fieldRegistration); - if (i == fields.length - 1) { - luaBuilder.append(TAB + TAB).append(StringUtils.format("{} = {}", fieldName, fieldDefaultValue)); - } else { - luaBuilder.append(TAB + TAB).append(StringUtils.format("{} = {},", fieldName, fieldDefaultValue)); - } - var typeNote = CodeGenerateCsharp.toCsClassName(field.getGenericType().getTypeName()); - // 生成类型的注释 - luaBuilder.append(" -- ").append(typeNote).append(LS); - } - return luaBuilder.toString(); - } - - private static String writePacket(ProtocolRegistration registration) { - var fields = registration.getFields(); - var fieldRegistrations = registration.getFieldRegistrations(); - var luaBuilder = new StringBuilder(); - if (registration.isCompatible()) { - luaBuilder.append("local beforeWriteIndex = buffer:getWriteOffset()").append(LS); - luaBuilder.append(TAB).append(StringUtils.format("buffer:writeInt({})", registration.getPredictionLength())).append(LS); - } else { - luaBuilder.append(TAB).append("buffer:writeInt(-1)").append(LS); - } - for (var i = 0; i < fields.length; i++) { - var field = fields[i]; - var fieldRegistration = fieldRegistrations[i]; - - luaSerializer(fieldRegistration.serializer()).writeObject(luaBuilder, "packet." + field.getName(), 1, field, fieldRegistration); - } - if (registration.isCompatible()) { - luaBuilder.append(TAB).append(StringUtils.format("buffer:adjustPadding({}, beforeWriteIndex)", registration.getPredictionLength())).append(LS); - } - return luaBuilder.toString(); - } - - private static String readPacket(ProtocolRegistration registration) { - var fields = registration.getFields(); - var fieldRegistrations = registration.getFieldRegistrations(); - var luaBuilder = new StringBuilder(); - for (int i = 0; i < fields.length; i++) { - var field = fields[i]; - var fieldRegistration = fieldRegistrations[i]; - if (field.isAnnotationPresent(Compatible.class)) { - luaBuilder.append(TAB).append("if buffer:compatibleRead(beforeReadIndex, length) then").append(LS); - var compatibleReadObject = luaSerializer(fieldRegistration.serializer()).readObject(luaBuilder, 2, field, fieldRegistration); - luaBuilder.append(TAB + TAB).append(StringUtils.format("packet.{} = {}", field.getName(), compatibleReadObject)).append(LS); - luaBuilder.append(TAB).append("end").append(LS); - continue; - } - var readObject = luaSerializer(fieldRegistration.serializer()).readObject(luaBuilder, 1, field, fieldRegistration); - luaBuilder.append(TAB).append(StringUtils.format("packet.{} = {}", field.getName(), readObject)).append(LS); - } - return luaBuilder.toString(); - } - -} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaArraySerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaArraySerializer.java index 99964c4ac..50ea36dbf 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaArraySerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaArraySerializer.java @@ -56,7 +56,7 @@ public void writeObject(StringBuilder builder, String objectStr, int deep, Field String element = "element" + GenerateProtocolFile.index.getAndIncrement(); GenerateProtocolFile.addTab(builder, deep + 1); builder.append(StringUtils.format("for {}, {} in pairs({}) do", index, element, objectStr)).append(LS); - GenerateLuaUtils.luaSerializer(arrayField.getArrayElementRegistration().serializer()) + CodeGenerateLua.luaSerializer(arrayField.getArrayElementRegistration().serializer()) .writeObject(builder, element, deep + 2, field, arrayField.getArrayElementRegistration()); GenerateProtocolFile.addTab(builder, deep + 1); builder.append("end").append(LS); @@ -86,7 +86,7 @@ public String readObject(StringBuilder builder, int deep, Field field, IFieldReg builder.append(StringUtils.format("if {} > 0 then", size)).append(LS); GenerateProtocolFile.addTab(builder, deep + 1); builder.append(StringUtils.format("for {} = 1, {} do", i, size)).append(LS); - String readObject = GenerateLuaUtils.luaSerializer(arrayField.getArrayElementRegistration().serializer()) + String readObject = CodeGenerateLua.luaSerializer(arrayField.getArrayElementRegistration().serializer()) .readObject(builder, deep + 2, field, arrayField.getArrayElementRegistration()); GenerateProtocolFile.addTab(builder, deep + 2); builder.append(StringUtils.format("table.insert({}, {})", result, readObject)).append(LS); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaListSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaListSerializer.java index fbe0cb7ab..d4405a6f5 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaListSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaListSerializer.java @@ -55,7 +55,7 @@ public void writeObject(StringBuilder builder, String objectStr, int deep, Field String element = "element" + GenerateProtocolFile.index.getAndIncrement(); GenerateProtocolFile.addTab(builder, deep + 1); builder.append(StringUtils.format("for {}, {} in pairs({}) do", index, element, objectStr)).append(LS); - GenerateLuaUtils.luaSerializer(listField.getListElementRegistration().serializer()) + CodeGenerateLua.luaSerializer(listField.getListElementRegistration().serializer()) .writeObject(builder, element, deep + 2, field, listField.getListElementRegistration()); GenerateProtocolFile.addTab(builder, deep + 1); builder.append("end").append(LS); @@ -85,7 +85,7 @@ public String readObject(StringBuilder builder, int deep, Field field, IFieldReg GenerateProtocolFile.addTab(builder, deep + 1); String i = "index" + GenerateProtocolFile.index.getAndIncrement(); builder.append(StringUtils.format("for {} = 1, {} do", i, size)).append(LS); - String readObject = GenerateLuaUtils.luaSerializer(listField.getListElementRegistration().serializer()) + String readObject = CodeGenerateLua.luaSerializer(listField.getListElementRegistration().serializer()) .readObject(builder, deep + 2, field, listField.getListElementRegistration()); GenerateProtocolFile.addTab(builder, deep + 2); builder.append(StringUtils.format("table.insert({}, {})", result, readObject)).append(LS); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaMapSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaMapSerializer.java index 4ebdd9ba1..6029188fb 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaMapSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaMapSerializer.java @@ -56,9 +56,9 @@ public void writeObject(StringBuilder builder, String objectStr, int deep, Field GenerateProtocolFile.addTab(builder, deep + 1); builder.append(StringUtils.format("for {}, {} in pairs({}) do", key, value, objectStr)).append(LS); - GenerateLuaUtils.luaSerializer(mapField.getMapKeyRegistration().serializer()) + CodeGenerateLua.luaSerializer(mapField.getMapKeyRegistration().serializer()) .writeObject(builder, key, deep + 2, field, mapField.getMapKeyRegistration()); - GenerateLuaUtils.luaSerializer(mapField.getMapValueRegistration().serializer()) + CodeGenerateLua.luaSerializer(mapField.getMapValueRegistration().serializer()) .writeObject(builder, value, deep + 2, field, mapField.getMapValueRegistration()); GenerateProtocolFile.addTab(builder, deep + 1); builder.append("end").append(LS); @@ -89,11 +89,11 @@ public String readObject(StringBuilder builder, int deep, Field field, IFieldReg GenerateProtocolFile.addTab(builder, deep + 1); builder.append(StringUtils.format("for {} = 1, {} do", i, size, i)).append(LS); - String keyObject = GenerateLuaUtils.luaSerializer(mapField.getMapKeyRegistration().serializer()) + String keyObject = CodeGenerateLua.luaSerializer(mapField.getMapKeyRegistration().serializer()) .readObject(builder, deep + 2, field, mapField.getMapKeyRegistration()); - String valueObject = GenerateLuaUtils.luaSerializer(mapField.getMapValueRegistration().serializer()) + String valueObject = CodeGenerateLua.luaSerializer(mapField.getMapValueRegistration().serializer()) .readObject(builder, deep + 2, field, mapField.getMapValueRegistration()); GenerateProtocolFile.addTab(builder, deep + 2); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaSetSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaSetSerializer.java index 1319258c2..ec5bbe142 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaSetSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/LuaSetSerializer.java @@ -56,7 +56,7 @@ public void writeObject(StringBuilder builder, String objectStr, int deep, Field String element = "element" + GenerateProtocolFile.index.getAndIncrement(); GenerateProtocolFile.addTab(builder, deep + 1); builder.append(StringUtils.format("for {}, {} in pairs({}) do", index, element, objectStr)).append(LS); - GenerateLuaUtils.luaSerializer(setField.getSetElementRegistration().serializer()) + CodeGenerateLua.luaSerializer(setField.getSetElementRegistration().serializer()) .writeObject(builder, element, deep + 2, field, setField.getSetElementRegistration()); GenerateProtocolFile.addTab(builder, deep + 1); builder.append("end").append(LS); @@ -86,7 +86,7 @@ public String readObject(StringBuilder builder, int deep, Field field, IFieldReg GenerateProtocolFile.addTab(builder, deep + 1); String i = "index" + GenerateProtocolFile.index.getAndIncrement(); builder.append(StringUtils.format("for {} = 1, {} do", i, size)).append(LS); - String readObject = GenerateLuaUtils.luaSerializer(setField.getSetElementRegistration().serializer()) + String readObject = CodeGenerateLua.luaSerializer(setField.getSetElementRegistration().serializer()) .readObject(builder, deep + 2, field, setField.getSetElementRegistration()); GenerateProtocolFile.addTab(builder, deep + 2); builder.append(StringUtils.format("table.insert({}, {})", result, readObject)).append(LS); diff --git a/protocol/src/main/resources/lua/ProtocolClassTemplate.lua b/protocol/src/main/resources/lua/ProtocolClassTemplate.lua new file mode 100644 index 000000000..d952f55ec --- /dev/null +++ b/protocol/src/main/resources/lua/ProtocolClassTemplate.lua @@ -0,0 +1,11 @@ +${protocol_note} +local ${protocol_name} = {} + +function ${protocol_name}:new() + local obj = { + ${protocol_field_definition} + } + setmetatable(obj, self) + self.__index = self + return obj +end \ No newline at end of file diff --git a/protocol/src/main/resources/lua/ProtocolManagerTemplate.lua b/protocol/src/main/resources/lua/ProtocolManagerTemplate.lua index 0c173653f..9e9d44809 100644 --- a/protocol/src/main/resources/lua/ProtocolManagerTemplate.lua +++ b/protocol/src/main/resources/lua/ProtocolManagerTemplate.lua @@ -32,10 +32,8 @@ function ProtocolManager.read(buffer) return ProtocolManager.getProtocol(protocolId):read(buffer) end -function initProtocol() - {} - {} +function ProtocolManager.initProtocol() + ${protocol_manager_registrations} end -ProtocolManager.initProtocol = initProtocol return ProtocolManager diff --git a/protocol/src/main/resources/lua/ProtocolRegistrationTemplate.lua b/protocol/src/main/resources/lua/ProtocolRegistrationTemplate.lua new file mode 100644 index 000000000..bdea4d012 --- /dev/null +++ b/protocol/src/main/resources/lua/ProtocolRegistrationTemplate.lua @@ -0,0 +1,33 @@ +function ${protocol_name}:protocolId() + return ${protocol_id} +end + +function ${protocol_name}:protocolName() + return "${protocol_name}" +end + +function ${protocol_name}:__tostring() + return table.serializeTableToJson(self) +end + +function ${protocol_name}:write(buffer, packet) + if packet == nil then + buffer:writeInt(0) + return + end + ${protocol_write_serialization} +end + +function ${protocol_name}:read(buffer) + local length = buffer:readInt() + if length == 0 then + return nil + end + local beforeReadIndex = buffer:getReadOffset() + local packet = ${protocol_name}:new() + ${protocol_read_deserialization} + if length > 0 then + buffer:setReadOffset(beforeReadIndex + length) + end + return packet +end \ No newline at end of file diff --git a/protocol/src/main/resources/lua/ProtocolTemplate.lua b/protocol/src/main/resources/lua/ProtocolTemplate.lua index 842955e81..152ce67ee 100644 --- a/protocol/src/main/resources/lua/ProtocolTemplate.lua +++ b/protocol/src/main/resources/lua/ProtocolTemplate.lua @@ -1,47 +1,5 @@ -{} -local {} = {} +${protocol_class} -function {}:new() - local obj = { - {} - } - setmetatable(obj, self) - self.__index = self - return obj -end +${protocol_registration} -function {}:protocolId() - return {} -end - -function {}:protocolName() - return "{}" -end - -function {}:__tostring() - return table.serializeTableToJson(self) -end - -function {}:write(buffer, packet) - if packet == nil then - buffer:writeInt(0) - return - end - {} -end - -function {}:read(buffer) - local length = buffer:readInt() - if length == 0 then - return nil - end - local beforeReadIndex = buffer:getReadOffset() - local packet = {}:new() - {} - if length > 0 then - buffer:setReadOffset(beforeReadIndex + length) - end - return packet -end - -return {} +return ${protocol_name} \ No newline at end of file diff --git a/protocol/src/main/resources/lua/ProtocolsTemplate.lua b/protocol/src/main/resources/lua/ProtocolsTemplate.lua new file mode 100644 index 000000000..d91c707ca --- /dev/null +++ b/protocol/src/main/resources/lua/ProtocolsTemplate.lua @@ -0,0 +1,7 @@ +local Protocols = {} + +${protocol_class} + +${protocol_registration} + +return Protocols \ No newline at end of file diff --git a/protocol/src/test/lua/Main.cs b/protocol/src/test/lua/Main.cs index 2338823c6..83295a986 100644 --- a/protocol/src/test/lua/Main.cs +++ b/protocol/src/test/lua/Main.cs @@ -5,7 +5,6 @@ using System.Text; using UnityEngine; using XLua; -using zfoocs; public class Main : MonoBehaviour {