diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/gdscript/CodeGenerateGdScript.java b/protocol/src/main/java/com/zfoo/protocol/serializer/gdscript/CodeGenerateGdScript.java index a6acd8518..1893e4766 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/gdscript/CodeGenerateGdScript.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/gdscript/CodeGenerateGdScript.java @@ -96,7 +96,6 @@ public void mergerProtocol(List registrations) throws IOEx protocol_manager_registrations.append(StringUtils.format("protocols[{}] = Protocols.{}Registration.new()", protocol_id, protocol_name)).append(LS); protocol_manager_registrations.append(StringUtils.format("protocolClassMap[{}] = Protocols.{}", protocol_id, protocol_name)).append(LS); - protocol_manager_registrations.append(StringUtils.format("protocolIdMap[Protocols.{}] = {}", protocol_name, protocol_id)).append(LS); } var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports.toString() , CodeTemplatePlaceholder.protocol_manager_registrations, StringUtils.substringBeforeLast(protocol_manager_registrations.toString(), StringUtils.COMMA)); @@ -140,8 +139,7 @@ public void foldProtocol(List registrations) throws IOExce var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); protocol_imports.append(StringUtils.format("const {} = preload(\"res://{}/{}/{}.gd\")", protocol_name, protocolOutputRootPath, GenerateProtocolPath.protocolPathSlash(protocol_id), protocol_name)).append(LS); protocol_manager_registrations.append(StringUtils.format("protocols[{}] = {}.{}Registration.new()", protocol_id, protocol_name, protocol_name)).append(LS); - protocol_manager_registrations.append(StringUtils.format("protocolClassMap[{}] = {}.{}", protocol_id, protocol_name, protocol_name)).append(LS); - protocol_manager_registrations.append(StringUtils.format("protocolIdMap[{}.{}] = {}", protocol_name, protocol_name, protocol_id)).append(LS); + protocol_manager_registrations.append(StringUtils.format("protocolClassMap[{}] = {}", protocol_id, protocol_name)).append(LS); } var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports.toString() , CodeTemplatePlaceholder.protocol_manager_registrations, StringUtils.substringBeforeLast(protocol_manager_registrations.toString(), StringUtils.COMMA)); @@ -159,7 +157,7 @@ public void foldProtocol(List registrations) throws IOExce CodeTemplatePlaceholder.protocol_id, String.valueOf(protocol_id) , CodeTemplatePlaceholder.protocol_name, protocol_name , CodeTemplatePlaceholder.protocol_imports, protocol_imports_fold(registration) - , CodeTemplatePlaceholder.protocol_class, protocol_class(registration) + , CodeTemplatePlaceholder.protocol_class, protocol_class_default(registration) , CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration) )); var outputPath = StringUtils.format("{}/{}/{}.gd", protocolOutputPath, GenerateProtocolPath.protocolPathSlash(protocol_id), protocol_name); @@ -184,8 +182,7 @@ public void defaultProtocol(List registrations) throws IOE var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); protocol_imports.append(StringUtils.format("const {} = preload(\"res://{}/{}.gd\")", protocol_name, protocolOutputRootPath, protocol_name)).append(LS); protocol_manager_registrations.append(StringUtils.format("protocols[{}] = {}.{}Registration.new()", protocol_id, protocol_name, protocol_name)).append(LS); - protocol_manager_registrations.append(StringUtils.format("protocolClassMap[{}] = {}.{}", protocol_id, protocol_name, protocol_name)).append(LS); - protocol_manager_registrations.append(StringUtils.format("protocolIdMap[{}.{}] = {}", protocol_name, protocol_name, protocol_id)).append(LS); + protocol_manager_registrations.append(StringUtils.format("protocolClassMap[{}] = {}", protocol_id, protocol_name)).append(LS); } var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports.toString() , CodeTemplatePlaceholder.protocol_manager_registrations, StringUtils.substringBeforeLast(protocol_manager_registrations.toString(), StringUtils.COMMA)); @@ -203,7 +200,7 @@ public void defaultProtocol(List registrations) throws IOE CodeTemplatePlaceholder.protocol_id, String.valueOf(protocol_id) , CodeTemplatePlaceholder.protocol_name, protocol_name , CodeTemplatePlaceholder.protocol_imports, protocol_imports_default(registration) - , CodeTemplatePlaceholder.protocol_class, protocol_class(registration) + , CodeTemplatePlaceholder.protocol_class, protocol_class_default(registration) , CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration) )); var outputPath = StringUtils.format("{}/{}.gd", protocolOutputPath, protocol_name); @@ -234,6 +231,21 @@ private String protocol_class(ProtocolRegistration registration) { return formatProtocolTemplate; } + private String protocol_class_default(ProtocolRegistration registration) { + var protocol_id = registration.protocolId(); + var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); + var protocolTemplate = ClassUtils.getFileFromClassPathToString("gdscript/ProtocolClassDefaultTemplate.gd"); + var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of( + CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocol_id, CodeLanguage.GdScript) + , CodeTemplatePlaceholder.protocol_name, protocol_name + , CodeTemplatePlaceholder.protocol_id, String.valueOf(protocol_id) + , CodeTemplatePlaceholder.protocol_field_definition, protocol_field_definition(registration) + , CodeTemplatePlaceholder.protocol_json, protocol_json(registration) + , CodeTemplatePlaceholder.protocol_to_string, protocol_to_string(registration) + )); + return formatProtocolTemplate; + } + private String protocol_registration(ProtocolRegistration registration) { var protocol_id = registration.protocolId(); var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); @@ -254,8 +266,7 @@ private String protocol_imports_fold(ProtocolRegistration registration) { importBuilder.append(StringUtils.format("const ByteBuffer = preload(\"res://{}/ByteBuffer.gd\")", protocolOutputRootPath)).append(LS); for (var subProtocolId : subProtocols) { var protocolName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(subProtocolId); - importBuilder.append(StringUtils.format("const {} = preload(\"res://{}/{}/{}.gd\").{}" - , protocolName, protocolOutputRootPath, GenerateProtocolPath.protocolPathSlash(protocolId), protocolName, protocolName)).append(LS); + importBuilder.append(StringUtils.format("const {} = preload(\"res://{}/{}/{}.gd\")", protocolName, protocolOutputRootPath, GenerateProtocolPath.protocolPathSlash(protocolId), protocolName)).append(LS); } return importBuilder.toString(); } @@ -267,7 +278,7 @@ private String protocol_imports_default(ProtocolRegistration registration) { importBuilder.append(StringUtils.format("const ByteBuffer = preload(\"res://{}/ByteBuffer.gd\")", protocolOutputRootPath)).append(LS); for (var subProtocolId : subProtocols) { var protocolName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(subProtocolId); - importBuilder.append(StringUtils.format("const {} = preload(\"res://{}/{}.gd\").{}", protocolName, protocolOutputRootPath, protocolName, protocolName)).append(LS); + importBuilder.append(StringUtils.format("const {} = preload(\"res://{}/{}.gd\")", protocolName, protocolOutputRootPath, protocolName)).append(LS); } return importBuilder.toString(); } diff --git a/protocol/src/main/resources/gdscript/ByteBuffer.gd b/protocol/src/main/resources/gdscript/ByteBuffer.gd index 7dd681325..2cee0e706 100644 --- a/protocol/src/main/resources/gdscript/ByteBuffer.gd +++ b/protocol/src/main/resources/gdscript/ByteBuffer.gd @@ -70,6 +70,9 @@ func isReadable() -> bool: func toBytes() -> PackedByteArray: return buffer.data_array.slice(0, writeOffset) +func newInstance(protocolId: int): + return ProtocolManager.newInstance(protocolId) + # -------------------------------------------------write/read------------------------------------------------- func writeBytes(value: PackedByteArray): var length: int = value.size() diff --git a/protocol/src/main/resources/gdscript/ProtocolClassDefaultTemplate.gd b/protocol/src/main/resources/gdscript/ProtocolClassDefaultTemplate.gd new file mode 100644 index 000000000..6aa220c59 --- /dev/null +++ b/protocol/src/main/resources/gdscript/ProtocolClassDefaultTemplate.gd @@ -0,0 +1,13 @@ +${protocol_note} +${protocol_field_definition} + +func protocolId() -> int: + return ${protocol_id} + +func get_class_name() -> String: + return "${protocol_name}" + +func _to_string() -> String: + const jsonTemplate = "${protocol_json}" + var params = [${protocol_to_string}] + return jsonTemplate.format(params, "{}") \ No newline at end of file diff --git a/protocol/src/main/resources/gdscript/ProtocolClassTemplate.gd b/protocol/src/main/resources/gdscript/ProtocolClassTemplate.gd index bc21d24eb..8298cbdbf 100644 --- a/protocol/src/main/resources/gdscript/ProtocolClassTemplate.gd +++ b/protocol/src/main/resources/gdscript/ProtocolClassTemplate.gd @@ -2,6 +2,12 @@ ${protocol_note} class ${protocol_name}: ${protocol_field_definition} + func protocolId() -> int: + return ${protocol_id} + + func get_class_name() -> String: + return "${protocol_name}" + func _to_string() -> String: const jsonTemplate = "${protocol_json}" var params = [${protocol_to_string}] diff --git a/protocol/src/main/resources/gdscript/ProtocolManagerTemplate.gd b/protocol/src/main/resources/gdscript/ProtocolManagerTemplate.gd index 7d2863790..8465fbf1d 100644 --- a/protocol/src/main/resources/gdscript/ProtocolManagerTemplate.gd +++ b/protocol/src/main/resources/gdscript/ProtocolManagerTemplate.gd @@ -2,7 +2,6 @@ ${protocol_imports} static var protocols: Dictionary = {} static var protocolClassMap: Dictionary = {} -static var protocolIdMap: Dictionary = {} static func initProtocol(): ${protocol_manager_registrations} @@ -14,8 +13,12 @@ static func getProtocol(protocolId: int): static func getProtocolClass(protocolId: int): return protocolClassMap[protocolId] +static func newInstance(protocolId: int): + var protocol = protocolClassMap[protocolId] + return protocol.new() + static func write(buffer, packet): - var protocolId: int = protocolIdMap[packet.get_script()] + var protocolId: int = packet.protocolId() buffer.writeShort(protocolId) var protocol = protocols[protocolId] protocol.write(buffer, packet) diff --git a/protocol/src/main/resources/gdscript/ProtocolRegistrationTemplate.gd b/protocol/src/main/resources/gdscript/ProtocolRegistrationTemplate.gd index a050ca0fa..59af87ba3 100644 --- a/protocol/src/main/resources/gdscript/ProtocolRegistrationTemplate.gd +++ b/protocol/src/main/resources/gdscript/ProtocolRegistrationTemplate.gd @@ -1,8 +1,5 @@ class ${protocol_name}Registration: - func getProtocolId(): - return ${protocol_id} - - func write(buffer: ByteBuffer, packet: ${protocol_name}): + func write(buffer: ByteBuffer, packet: Object): if (packet == null): buffer.writeInt(0) return @@ -14,7 +11,7 @@ class ${protocol_name}Registration: if (length == 0): return null var beforeReadIndex = buffer.getReadOffset() - var packet = ${protocol_name}.new() + var packet = buffer.newInstance(${protocol_id}) ${protocol_read_deserialization} if (length > 0): buffer.setReadOffset(beforeReadIndex + length)