mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-11 03:13:51 +00:00
ref[protocol]: refactor GdScript generation
This commit is contained in:
1 parent
5fc3dbbf53
commit
b8725f4408
7 files changed
+46
-72
No files matched your search
@@ -50,10 +50,6 @@ public enum CodeTemplatePlaceholder {
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
protocol_field_accessor("${protocol_field_accessor}"),
|
||||
|
||||
protocol_json("${protocol_json}"),
|
||||
|
||||
protocol_to_string("${protocol_to_string}"),
|
||||
|
||||
;
|
||||
public final String placeholder;
|
||||
|
||||
|
||||
-49
@@ -190,8 +190,6 @@ public class CodeGenerateGdScript implements ICodeGenerate {
|
||||
, 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)
|
||||
, CodeTemplatePlaceholder.protocol_write_serialization, protocol_write_serialization(registration)
|
||||
, CodeTemplatePlaceholder.protocol_read_deserialization, protocol_read_deserialization(registration)
|
||||
));
|
||||
@@ -207,8 +205,6 @@ public class CodeGenerateGdScript implements ICodeGenerate {
|
||||
, 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;
|
||||
}
|
||||
@@ -226,51 +222,6 @@ public class CodeGenerateGdScript implements ICodeGenerate {
|
||||
return formatProtocolTemplate;
|
||||
}
|
||||
|
||||
|
||||
private String protocol_json(ProtocolRegistration registration) {
|
||||
var fields = registration.getFields();
|
||||
var fieldRegistrations = registration.getFieldRegistrations();
|
||||
var gdBuilder = new StringBuilder();
|
||||
gdBuilder.append("{");
|
||||
// when generate source code fields, use origin fields sort
|
||||
var sequencedFields = ReflectionUtils.notStaticAndTransientFields(registration.getConstructor().getDeclaringClass());
|
||||
var params = new ArrayList<String>();
|
||||
for (var field : sequencedFields) {
|
||||
var fieldRegistration = fieldRegistrations[GenerateProtocolFile.indexOf(fields, field)];
|
||||
var fieldName = field.getName();
|
||||
var fieldType = gdSerializer(fieldRegistration.serializer()).fieldType(field, fieldRegistration);
|
||||
if (fieldType.equals("String")) {
|
||||
params.add(StringUtils.format("{}:'{}'", fieldName));
|
||||
} else {
|
||||
params.add(StringUtils.format("{}:{}", fieldName));
|
||||
}
|
||||
}
|
||||
gdBuilder.append(StringUtils.joinWith(", ", params.toArray()));
|
||||
gdBuilder.append("}");
|
||||
return gdBuilder.toString();
|
||||
}
|
||||
|
||||
private String protocol_to_string(ProtocolRegistration registration) {
|
||||
var fields = registration.getFields();
|
||||
var fieldRegistrations = registration.getFieldRegistrations();
|
||||
var gdBuilder = new StringBuilder();
|
||||
// when generate source code fields, use origin fields sort
|
||||
var sequencedFields = ReflectionUtils.notStaticAndTransientFields(registration.getConstructor().getDeclaringClass());
|
||||
var params = new ArrayList<String>();
|
||||
for (var field : sequencedFields) {
|
||||
var fieldRegistration = fieldRegistrations[GenerateProtocolFile.indexOf(fields, field)];
|
||||
var fieldName = field.getName();
|
||||
var fieldType = gdSerializer(fieldRegistration.serializer()).fieldType(field, fieldRegistration);
|
||||
if (fieldType.equals("Dictionary") || fieldType.startsWith("Array")) {
|
||||
params.add(StringUtils.format("JSON.stringify(self.{})", field.getName()));
|
||||
} else {
|
||||
params.add(StringUtils.format("self.{}", field.getName()));
|
||||
}
|
||||
}
|
||||
gdBuilder.append(StringUtils.joinWith(", ", params.toArray()));
|
||||
return gdBuilder.toString();
|
||||
}
|
||||
|
||||
private String protocol_field_definition(ProtocolRegistration registration) {
|
||||
var protocolId = registration.getId();
|
||||
var fields = registration.getFields();
|
||||
|
||||
@@ -39,6 +39,47 @@ func adjustPadding(predictionLength: int, beforeWriteIndex: int):
|
||||
func compatibleRead(beforeReadIndex: int, length: int) -> bool:
|
||||
return length != -1 && getReadOffset() < length + beforeReadIndex
|
||||
|
||||
static func object_to_json(obj) -> String:
|
||||
var type = typeof(obj)
|
||||
match type:
|
||||
TYPE_NIL:
|
||||
return "null"
|
||||
TYPE_BOOL:
|
||||
return str(obj)
|
||||
TYPE_INT:
|
||||
return str(obj)
|
||||
TYPE_FLOAT:
|
||||
return str(obj)
|
||||
TYPE_STRING:
|
||||
return "\"" + obj as String + "\""
|
||||
TYPE_ARRAY:
|
||||
var array: PackedStringArray = PackedStringArray()
|
||||
for element in obj:
|
||||
array.push_back(object_to_json(element))
|
||||
return "[" + ",".join(array) + "]"
|
||||
TYPE_DICTIONARY:
|
||||
var array: PackedStringArray = PackedStringArray()
|
||||
for key in obj:
|
||||
var value = obj.get(key)
|
||||
array.push_back("\"" + object_to_json(key) + "\":" + object_to_json(value))
|
||||
return "{" + ",".join(array) + "}"
|
||||
TYPE_OBJECT:
|
||||
var properties = obj.get_property_list()
|
||||
var array: PackedStringArray = PackedStringArray()
|
||||
for property in properties:
|
||||
var propertyType = property.type
|
||||
var propertyName = property.name
|
||||
if propertyType == TYPE_NIL:
|
||||
continue
|
||||
if propertyName == "script":
|
||||
continue
|
||||
var value = obj.get(propertyName)
|
||||
array.push_back("\"" + propertyName + "\":" + object_to_json(value))
|
||||
return "{" + ",".join(array) + "}"
|
||||
_:
|
||||
printerr("unknow type " + type)
|
||||
return ""
|
||||
|
||||
# -------------------------------------------------get/set-------------------------------------------------
|
||||
func getBuffer() -> StreamPeerBuffer:
|
||||
return buffer
|
||||
|
||||
@@ -1,10 +1,3 @@
|
||||
const PROTOCOL_ID: int = ${protocol_id}
|
||||
${protocol_note}
|
||||
${protocol_field_definition}
|
||||
|
||||
func protocolId() -> int:
|
||||
return ${protocol_id}
|
||||
|
||||
func _to_string() -> String:
|
||||
const jsonTemplate = "${protocol_json}"
|
||||
var params = [${protocol_to_string}]
|
||||
return jsonTemplate.format(params, "{}")
|
||||
${protocol_field_definition}
|
||||
@@ -1,15 +1,9 @@
|
||||
${protocol_note}
|
||||
class ${protocol_name}:
|
||||
const PROTOCOL_ID: int = ${protocol_id}
|
||||
|
||||
${protocol_field_definition}
|
||||
|
||||
func protocolId() -> int:
|
||||
return ${protocol_id}
|
||||
|
||||
func _to_string() -> String:
|
||||
const jsonTemplate = "${protocol_json}"
|
||||
var params = [${protocol_to_string}]
|
||||
return jsonTemplate.format(params, "{}")
|
||||
|
||||
static func write(buffer: ByteBuffer, packet: ${protocol_name}):
|
||||
if (packet == null):
|
||||
buffer.writeInt(0)
|
||||
|
||||
@@ -3,7 +3,7 @@ ${protocol_manager_registrations}
|
||||
}
|
||||
|
||||
static func write(buffer: ByteBuffer, packet: Object) -> void:
|
||||
var protocolId: int = packet.protocolId()
|
||||
var protocolId: int = packet.PROTOCOL_ID
|
||||
buffer.writeShort(protocolId)
|
||||
packet.write(buffer, packet)
|
||||
pass
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
class_name ${protocol_name}
|
||||
|
||||
${protocol_class}
|
||||
|
||||
${protocol_registration}
|
||||
Reference in new issue
Block a user