Skip to content

Commit

Permalink
ref[gdscript]: gdscript protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 17, 2024
1 parent 1c1b09f commit d4f6ae7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public void mergerProtocol(List<ProtocolRegistration> 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));
Expand Down Expand Up @@ -140,8 +139,7 @@ public void foldProtocol(List<ProtocolRegistration> 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));
Expand All @@ -159,7 +157,7 @@ public void foldProtocol(List<ProtocolRegistration> 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);
Expand All @@ -184,8 +182,7 @@ public void defaultProtocol(List<ProtocolRegistration> 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));
Expand All @@ -203,7 +200,7 @@ public void defaultProtocol(List<ProtocolRegistration> 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);
Expand Down Expand Up @@ -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();
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down
3 changes: 3 additions & 0 deletions protocol/src/main/resources/gdscript/ByteBuffer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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, "{}")
6 changes: 6 additions & 0 deletions protocol/src/main/resources/gdscript/ProtocolClassTemplate.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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}]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ ${protocol_imports}

static var protocols: Dictionary = {}
static var protocolClassMap: Dictionary = {}
static var protocolIdMap: Dictionary = {}

static func initProtocol():
${protocol_manager_registrations}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down

0 comments on commit d4f6ae7

Please sign in to comment.