diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/ecmascript/CodeGenerateEcmaScript.java b/protocol/src/main/java/com/zfoo/protocol/serializer/ecmascript/CodeGenerateEcmaScript.java index c16b47aa4..34c017409 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/ecmascript/CodeGenerateEcmaScript.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/ecmascript/CodeGenerateEcmaScript.java @@ -83,11 +83,12 @@ public void mergerProtocol(List registrations) throws IOEx var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolManagerTemplate.mjs"); var protocol_imports = new StringBuilder(); var protocol_manager_registrations = new StringBuilder(); - protocol_imports.append("import Protocols from './Protocols.mjs';").append(LS); + protocol_imports.append("import * as Protocols from './Protocols.mjs';").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.set({}, Protocols.{});", protocol_id, protocol_name)).append(LS); + protocol_manager_registrations.append(StringUtils.format("protocols.set({}, new Protocols.{}Registration());", protocol_id, protocol_name)).append(LS); + protocol_manager_registrations.append(StringUtils.format("protocolIdMap.set(Protocols.{}, {});", protocol_name, protocol_id)).append(LS); } var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports.toString() , CodeTemplatePlaceholder.protocol_manager_registrations, protocol_manager_registrations.toString()); @@ -103,8 +104,8 @@ public void mergerProtocol(List registrations) throws IOEx var protocol_id = registration.protocolId(); var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); // protocol - protocol_class.append(formatProtocolClassTemplate(registration)).append(LS); - protocol_registration.append(StringUtils.format("Protocols.{} = {}", protocol_name, protocol_name)).append(LS); + protocol_class.append(protocol_class(registration).replace("class ", "export class ")).append(LS); + protocol_registration.append(protocol_registration(registration)).append(LS); } var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolsTemplate.mjs"); var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of( @@ -129,8 +130,11 @@ public void foldProtocol(List registrations) throws IOExce for (var protocol : registrations) { var protocol_id = protocol.protocolId(); var protocol_name = protocol.protocolConstructor().getDeclaringClass().getSimpleName(); + protocol_imports.append(StringUtils.format("import {} from './{}/{}.mjs';", protocol_name, GenerateProtocolPath.protocolPathSlash(protocol.protocolId()), protocol_name)).append(LS); - protocol_manager_registrations.append(StringUtils.format("protocols.set({}, {});", protocol_id, protocol_name)).append(LS); + protocol_imports.append(StringUtils.format("import { {}Registration } from './{}/{}.mjs';", protocol_name, GenerateProtocolPath.protocolPathSlash(protocol.protocolId()), protocol_name)).append(LS); + protocol_manager_registrations.append(StringUtils.format("protocols.set({}, new {}Registration());", protocol_id, protocol_name)).append(LS); + protocol_manager_registrations.append(StringUtils.format("protocolIdMap.set({}, {});", protocol_name, protocol_id)).append(LS); } var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports.toString() , CodeTemplatePlaceholder.protocol_manager_registrations, protocol_manager_registrations.toString()); @@ -143,7 +147,12 @@ public void foldProtocol(List registrations) throws IOExce for (var registration : registrations) { var protocol_id = registration.protocolId(); var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); - var formatProtocolTemplate = formatProtocolTemplate(registration); + var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolTemplate.mjs"); + var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of( + CodeTemplatePlaceholder.protocol_name, protocol_name + , CodeTemplatePlaceholder.protocol_class, protocol_class(registration) + , CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration) + )); var outputPath = StringUtils.format("{}/{}/{}.mjs", protocolOutputPath, GenerateProtocolPath.protocolPathSlash(protocol_id), protocol_name); var file = new File(outputPath); FileUtils.writeStringToFile(file, formatProtocolTemplate, true); @@ -155,16 +164,17 @@ public void foldProtocol(List registrations) throws IOExce public void defaultProtocol(List registrations) throws IOException { createTemplateFile(); - // 生成ProtocolManager.mjs文件 var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolManagerTemplate.mjs"); var protocol_imports = new StringBuilder(); var protocol_manager_registrations = new StringBuilder(); - for (var protocol : registrations) { - var protocol_id = protocol.protocolId(); - var protocol_name = protocol.protocolConstructor().getDeclaringClass().getSimpleName(); + for (var registration : registrations) { + var protocol_id = registration.protocolId(); + var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); protocol_imports.append(StringUtils.format("import {} from './{}.mjs';", protocol_name, protocol_name)).append(LS); - protocol_manager_registrations.append(StringUtils.format("protocols.set({}, {});", protocol_id, protocol_name)).append(LS); + protocol_imports.append(StringUtils.format("import { {}Registration } from './{}.mjs';", protocol_name, protocol_name)).append(LS); + protocol_manager_registrations.append(StringUtils.format("protocols.set({}, new {}Registration());", protocol_id, protocol_name)).append(LS); + protocol_manager_registrations.append(StringUtils.format("protocolIdMap.set({}, {});", protocol_name, protocol_id)).append(LS); } var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports.toString() , CodeTemplatePlaceholder.protocol_manager_registrations, protocol_manager_registrations.toString()); @@ -177,8 +187,13 @@ public void defaultProtocol(List registrations) throws IOE for (var registration : registrations) { var protocol_id = registration.protocolId(); var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); - var formatProtocolTemplate = formatProtocolTemplate(registration); - var outputPath = StringUtils.format("{}/{}.mjs", protocolOutputPath, protocol_name, protocol_name); + var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolTemplate.mjs"); + var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of( + CodeTemplatePlaceholder.protocol_name, protocol_name + , CodeTemplatePlaceholder.protocol_class, protocol_class(registration) + , CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration) + )); + var outputPath = StringUtils.format("{}/{}.mjs", protocolOutputPath, protocol_name); var file = new File(outputPath); FileUtils.writeStringToFile(file, formatProtocolTemplate, true); logger.info("Generated EcmaScript protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath()); @@ -195,30 +210,27 @@ private void createTemplateFile() throws IOException { } } - public String formatProtocolTemplate(ProtocolRegistration registration) { + public String protocol_class(ProtocolRegistration registration) { var protocol_id = registration.protocolId(); var protocol_name = registration.getConstructor().getDeclaringClass().getSimpleName(); - var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolTemplate.mjs"); + var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolClassTemplate.mjs"); var placeholderMap = Map.of( CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocol_id, CodeLanguage.EcmaScript) , CodeTemplatePlaceholder.protocol_name, protocol_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(protocolTemplate, placeholderMap); } - public String formatProtocolClassTemplate(ProtocolRegistration registration) { + public String protocol_registration(ProtocolRegistration registration) { var protocol_id = registration.protocolId(); var protocol_name = registration.getConstructor().getDeclaringClass().getSimpleName(); - var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolClassTemplate.mjs"); + var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolRegistrationTemplate.mjs"); var placeholderMap = Map.of( CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocol_id, CodeLanguage.EcmaScript) , CodeTemplatePlaceholder.protocol_name, protocol_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/resources/ecmascript/ProtocolClassTemplate.mjs b/protocol/src/main/resources/ecmascript/ProtocolClassTemplate.mjs index dc869073e..c80081c2c 100644 --- a/protocol/src/main/resources/ecmascript/ProtocolClassTemplate.mjs +++ b/protocol/src/main/resources/ecmascript/ProtocolClassTemplate.mjs @@ -1,32 +1,4 @@ ${protocol_note} class ${protocol_name} { ${protocol_field_definition} - - static PROTOCOL_ID = ${protocol_id}; - - protocolId() { - return ${protocol_name}.PROTOCOL_ID; - } - - static write(buffer, packet) { - if (packet === null) { - buffer.writeInt(0); - return; - } - ${protocol_write_serialization} - } - - static read(buffer) { - const length = buffer.readInt(); - if (length === 0) { - return null; - } - const beforeReadIndex = buffer.getReadOffset(); - const packet = new ${protocol_name}(); - ${protocol_read_deserialization} - if (length > 0) { - buffer.setReadOffset(beforeReadIndex + length); - } - return packet; - } } \ No newline at end of file diff --git a/protocol/src/main/resources/ecmascript/ProtocolManagerTemplate.mjs b/protocol/src/main/resources/ecmascript/ProtocolManagerTemplate.mjs index 4b2f96f94..f17b78ef3 100644 --- a/protocol/src/main/resources/ecmascript/ProtocolManagerTemplate.mjs +++ b/protocol/src/main/resources/ecmascript/ProtocolManagerTemplate.mjs @@ -1,11 +1,19 @@ ${protocol_imports} const protocols = new Map(); +const protocolIdMap = new Map(); // initProtocol ${protocol_manager_registrations} class ProtocolManager { + static getProtocolId(clazz) { + const protocolId = protocolIdMap.get(clazz); + if (protocolId === null || protocolId === undefined) { + throw '[protocol:' + clazz + '] not exist'; + } + return protocolId; + } static getProtocol(protocolId) { const protocol = protocols.get(protocolId); if (protocol === null) { @@ -15,7 +23,7 @@ class ProtocolManager { } static write(buffer, packet) { - const protocolId = packet.protocolId(); + const protocolId = ProtocolManager.getProtocolId(packet.constructor); buffer.writeShort(protocolId); const protocol = ProtocolManager.getProtocol(protocolId); protocol.write(buffer, packet); diff --git a/protocol/src/main/resources/ecmascript/ProtocolRegistrationTemplate.mjs b/protocol/src/main/resources/ecmascript/ProtocolRegistrationTemplate.mjs new file mode 100644 index 000000000..284ba789c --- /dev/null +++ b/protocol/src/main/resources/ecmascript/ProtocolRegistrationTemplate.mjs @@ -0,0 +1,27 @@ +export class ${protocol_name}Registration { + protocolId() { + return ${protocol_id}; + } + + write(buffer, packet) { + if (packet === null) { + buffer.writeInt(0); + return; + } + ${protocol_write_serialization} + } + + read(buffer) { + const length = buffer.readInt(); + if (length === 0) { + return null; + } + const beforeReadIndex = buffer.getReadOffset(); + const packet = new ${protocol_name}(); + ${protocol_read_deserialization} + if (length > 0) { + buffer.setReadOffset(beforeReadIndex + length); + } + return packet; + } +} \ No newline at end of file diff --git a/protocol/src/main/resources/ecmascript/ProtocolTemplate.mjs b/protocol/src/main/resources/ecmascript/ProtocolTemplate.mjs index 0f722ed4c..5defd9fe1 100644 --- a/protocol/src/main/resources/ecmascript/ProtocolTemplate.mjs +++ b/protocol/src/main/resources/ecmascript/ProtocolTemplate.mjs @@ -1,33 +1,5 @@ -${protocol_note} -class ${protocol_name} { - ${protocol_field_definition} +${protocol_class} - static PROTOCOL_ID = ${protocol_id}; +${protocol_registration} - protocolId() { - return ${protocol_name}.PROTOCOL_ID; - } - - static write(buffer, packet) { - if (packet === null) { - buffer.writeInt(0); - return; - } - ${protocol_write_serialization} - } - - static read(buffer) { - const length = buffer.readInt(); - if (length === 0) { - return null; - } - const beforeReadIndex = buffer.getReadOffset(); - const packet = new ${protocol_name}(); - ${protocol_read_deserialization} - if (length > 0) { - buffer.setReadOffset(beforeReadIndex + length); - } - return packet; - } -} export default ${protocol_name}; \ No newline at end of file diff --git a/protocol/src/main/resources/ecmascript/ProtocolsTemplate.mjs b/protocol/src/main/resources/ecmascript/ProtocolsTemplate.mjs index f94233da3..f1867defb 100644 --- a/protocol/src/main/resources/ecmascript/ProtocolsTemplate.mjs +++ b/protocol/src/main/resources/ecmascript/ProtocolsTemplate.mjs @@ -1,8 +1,3 @@ -class Protocols { -} - ${protocol_class} -${protocol_registration} - -export default Protocols; +${protocol_registration} \ No newline at end of file