mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-06 14:25:22 +00:00
ref[protocol]: simplify gdscript generation
This commit is contained in:
@@ -503,7 +503,10 @@ public class CutDownArraySerializer implements ICutDownSerializer {
|
||||
case Enhance:
|
||||
builder.append(StringUtils.format("{}.writePacketArray($1, {}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId)));
|
||||
break;
|
||||
case GdScript, Python, Ruby, Swift:
|
||||
case GdScript:
|
||||
builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, protocolName)).append(LS);
|
||||
break;
|
||||
case Python, Ruby, Swift:
|
||||
builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, protocolId)).append(LS);
|
||||
break;
|
||||
case Lua:
|
||||
@@ -1287,7 +1290,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
|
||||
builder.append(StringUtils.format("let {} = buffer.readPacketArray({}) as! Array<{}>", array, protocolId, protocolName)).append(LS);
|
||||
break;
|
||||
case GdScript:
|
||||
builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", array, protocolId)).append(LS);
|
||||
builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", array, protocolName)).append(LS);
|
||||
break;
|
||||
case Python, Ruby:
|
||||
builder.append(StringUtils.format("{} = buffer.readPacketArray({})", array, protocolId)).append(LS);
|
||||
|
||||
@@ -316,11 +316,15 @@ public class CutDownListSerializer implements ICutDownSerializer {
|
||||
// List<IProtocol>
|
||||
if (listField.getListElementRegistration() instanceof ObjectProtocolField) {
|
||||
var protocolId = ((ObjectProtocolField) listField.getListElementRegistration()).getProtocolId();
|
||||
var protocolName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId);
|
||||
switch (language) {
|
||||
case Enhance:
|
||||
builder.append(StringUtils.format("{}.writePacketList($1, (List){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId)));
|
||||
break;
|
||||
case GdScript, Python, Ruby, Swift:
|
||||
case GdScript:
|
||||
builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, protocolName)).append(LS);
|
||||
break;
|
||||
case Python, Ruby, Swift:
|
||||
builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, protocolId)).append(LS);
|
||||
break;
|
||||
case Lua:
|
||||
@@ -765,7 +769,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
|
||||
builder.append(StringUtils.format("let {} = buffer.readPacketArray({}) as! Array<{}>", list, protocolId, protocolName)).append(LS);
|
||||
break;
|
||||
case GdScript:
|
||||
builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", list, protocolId)).append(LS);
|
||||
builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", list, protocolName)).append(LS);
|
||||
break;
|
||||
case Python, Ruby:
|
||||
builder.append(StringUtils.format("{} = buffer.readPacketArray({})", list, protocolId)).append(LS);
|
||||
|
||||
@@ -134,11 +134,15 @@ public class CutDownMapSerializer implements ICutDownSerializer {
|
||||
}
|
||||
} else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) {
|
||||
var protocolId = ((ObjectProtocolField) valueRegistration).getProtocolId();
|
||||
var protocolName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId);
|
||||
switch (language) {
|
||||
case Enhance:
|
||||
builder.append(StringUtils.format("{}.writeIntPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId)));
|
||||
return true;
|
||||
case Kotlin, Scala, GdScript, Python, Ruby, Swift:
|
||||
case GdScript:
|
||||
builder.append(StringUtils.format("buffer.writeIntPacketMap({}, {})", objectStr, protocolName)).append(LS);
|
||||
return true;
|
||||
case Kotlin, Scala, Python, Ruby, Swift:
|
||||
builder.append(StringUtils.format("buffer.writeIntPacketMap({}, {})", objectStr, protocolId)).append(LS);
|
||||
return true;
|
||||
case Lua:
|
||||
@@ -244,11 +248,15 @@ public class CutDownMapSerializer implements ICutDownSerializer {
|
||||
}
|
||||
} else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) {
|
||||
var protocolId = ((ObjectProtocolField) valueRegistration).getProtocolId();
|
||||
var protocolName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId);
|
||||
switch (language) {
|
||||
case Enhance:
|
||||
builder.append(StringUtils.format("{}.writeLongPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId)));
|
||||
return true;
|
||||
case Kotlin, Scala, GdScript, Python, Ruby, Swift:
|
||||
case GdScript:
|
||||
builder.append(StringUtils.format("buffer.writeLongPacketMap({}, {})", objectStr, protocolName)).append(LS);
|
||||
return true;
|
||||
case Kotlin, Scala, Python, Ruby, Swift:
|
||||
builder.append(StringUtils.format("buffer.writeLongPacketMap({}, {})", objectStr, protocolId)).append(LS);
|
||||
return true;
|
||||
case Lua:
|
||||
@@ -354,11 +362,15 @@ public class CutDownMapSerializer implements ICutDownSerializer {
|
||||
}
|
||||
} else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) {
|
||||
var protocolId = ((ObjectProtocolField) valueRegistration).getProtocolId();
|
||||
var protocolName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId);
|
||||
switch (language) {
|
||||
case Enhance:
|
||||
builder.append(StringUtils.format("{}.writeStringPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId)));
|
||||
return true;
|
||||
case Kotlin, Scala, GdScript, Python, Ruby, Swift:
|
||||
case GdScript:
|
||||
builder.append(StringUtils.format("buffer.writeStringPacketMap({}, {})", objectStr, protocolName)).append(LS);
|
||||
return true;
|
||||
case Kotlin, Scala, Python, Ruby, Swift:
|
||||
builder.append(StringUtils.format("buffer.writeStringPacketMap({}, {})", objectStr, protocolId)).append(LS);
|
||||
return true;
|
||||
case Lua:
|
||||
@@ -546,7 +558,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
|
||||
builder.append(StringUtils.format("let {} = buffer.readIntPacketMap({}) as! Dictionary<Int, {}>", map, protocolId, protocolName)).append(LS);
|
||||
return map;
|
||||
case GdScript:
|
||||
builder.append(StringUtils.format("var {} = buffer.readIntPacketMap({})", map, protocolId)).append(LS);
|
||||
builder.append(StringUtils.format("var {} = buffer.readIntPacketMap({})", map, protocolName)).append(LS);
|
||||
return map;
|
||||
case Python, Ruby:
|
||||
builder.append(StringUtils.format("{} = buffer.readIntPacketMap({})", map, protocolId)).append(LS);
|
||||
@@ -732,7 +744,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
|
||||
builder.append(StringUtils.format("let {} = buffer.readLongPacketMap({}) as! Dictionary<Int64, {}>", map, protocolId, protocolName)).append(LS);
|
||||
return map;
|
||||
case GdScript:
|
||||
builder.append(StringUtils.format("var {} = buffer.readLongPacketMap({})", map, protocolId)).append(LS);
|
||||
builder.append(StringUtils.format("var {} = buffer.readLongPacketMap({})", map, protocolName)).append(LS);
|
||||
return map;
|
||||
case Python, Ruby:
|
||||
builder.append(StringUtils.format("{} = buffer.readLongPacketMap({})", map, protocolId)).append(LS);
|
||||
@@ -919,7 +931,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
|
||||
builder.append(StringUtils.format("let {} = buffer.readStringPacketMap({}) as! Dictionary<String, {}>", map, protocolId, protocolName)).append(LS);
|
||||
return map;
|
||||
case GdScript:
|
||||
builder.append(StringUtils.format("var {} = buffer.readStringPacketMap({})", map, protocolId)).append(LS);
|
||||
builder.append(StringUtils.format("var {} = buffer.readStringPacketMap({})", map, protocolName)).append(LS);
|
||||
return map;
|
||||
case Python, Ruby:
|
||||
builder.append(StringUtils.format("{} = buffer.readStringPacketMap({})", map, protocolId)).append(LS);
|
||||
|
||||
@@ -309,12 +309,13 @@ public class CutDownSetSerializer implements ICutDownSerializer {
|
||||
// Set<IProtocol>
|
||||
if (setField.getSetElementRegistration() instanceof ObjectProtocolField) {
|
||||
var protocolId = ((ObjectProtocolField) setField.getSetElementRegistration()).getProtocolId();
|
||||
var protocolName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId);
|
||||
switch (language) {
|
||||
case Enhance:
|
||||
builder.append(StringUtils.format("{}.writePacketSet($1, (Set){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId)));
|
||||
break;
|
||||
case GdScript:
|
||||
builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, protocolId)).append(LS);
|
||||
builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, protocolName)).append(LS);
|
||||
break;
|
||||
case Kotlin, Scala, Python, Ruby:
|
||||
builder.append(StringUtils.format("buffer.writePacketSet({}, {})", objectStr, protocolId)).append(LS);
|
||||
@@ -751,7 +752,7 @@ public class CutDownSetSerializer implements ICutDownSerializer {
|
||||
builder.append(StringUtils.format("Set {} = {}.readPacketSet($1, {});", set, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId)));
|
||||
break;
|
||||
case GdScript:
|
||||
builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", set, protocolId)).append(LS);
|
||||
builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", set, protocolName)).append(LS);
|
||||
break;
|
||||
case Python, Ruby:
|
||||
builder.append(StringUtils.format("{} = buffer.readPacketSet({})", set, protocolId)).append(LS);
|
||||
|
||||
+27
-63
@@ -82,43 +82,31 @@ public class CodeGenerateGdScript implements ICodeGenerate {
|
||||
|
||||
@Override
|
||||
public void mergerProtocol(List<ProtocolRegistration> registrations) throws IOException {
|
||||
createTemplateFile();
|
||||
|
||||
var createFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "ByteBuffer.gd"));
|
||||
var fileInputStream = ClassUtils.getFileFromClassPath("gdscript/ByteBuffer.gd");
|
||||
FileUtils.writeInputStreamToFile(createFile, fileInputStream);
|
||||
|
||||
// 生成ProtocolManager.gd文件
|
||||
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("gdscript/ProtocolManagerTemplate.gd");
|
||||
var protocol_imports = new StringBuilder();
|
||||
var protocol_manager_registrations = new StringBuilder();
|
||||
protocol_imports.append("const Protocols = preload(\"./Protocols.gd\")").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[{}] = 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("{}{} : Protocols.{},", TAB_ASCII, 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));
|
||||
var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_manager_registrations, StringUtils.substringBeforeLast(protocol_manager_registrations.toString(), StringUtils.COMMA));
|
||||
var formatProtocolManagerTemplate = CodeTemplatePlaceholder.formatTemplate(protocolManagerTemplate, placeholderMap);
|
||||
var protocolManagerFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.gd"));
|
||||
FileUtils.writeStringToFile(protocolManagerFile, formatProtocolManagerTemplate, true);
|
||||
logger.info("Generated GdScript protocol manager file:[{}] is in path:[{}]", protocolManagerFile.getName(), protocolManagerFile.getAbsolutePath());
|
||||
|
||||
|
||||
protocol_imports = new StringBuilder();
|
||||
var protocol_class = new StringBuilder();
|
||||
var protocol_registration = new StringBuilder();
|
||||
protocol_imports.append("const ByteBuffer = preload(\"./ByteBuffer.gd\")").append(LS);
|
||||
for (var registration : registrations) {
|
||||
protocol_class.append(protocol_class(registration)).append(LS).append(LS);
|
||||
protocol_registration.append(protocol_registration(registration)).append(LS).append(LS).append(LS);
|
||||
protocol_class.append(protocol_class_merge(registration)).append(LS).append(LS);
|
||||
}
|
||||
var protocolTemplate = ClassUtils.getFileFromClassPathToString("gdscript/ProtocolsTemplate.gd");
|
||||
var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of(
|
||||
CodeTemplatePlaceholder.protocol_imports, protocol_imports.toString()
|
||||
, CodeTemplatePlaceholder.protocol_class, protocol_class.toString()
|
||||
, CodeTemplatePlaceholder.protocol_registration, protocol_registration.toString()
|
||||
));
|
||||
var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of(CodeTemplatePlaceholder.protocol_class, protocol_class.toString()));
|
||||
var outputPath = StringUtils.format("{}/Protocols.gd", protocolOutputPath);
|
||||
var file = new File(outputPath);
|
||||
FileUtils.writeStringToFile(file, formatProtocolTemplate, true);
|
||||
@@ -127,26 +115,7 @@ public class CodeGenerateGdScript implements ICodeGenerate {
|
||||
|
||||
@Override
|
||||
public void foldProtocol(List<ProtocolRegistration> registrations) throws IOException {
|
||||
createTemplateFile();
|
||||
|
||||
|
||||
// 生成ProtocolManager.gd文件
|
||||
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("gdscript/ProtocolManagerTemplate.gd");
|
||||
var protocol_imports = new StringBuilder();
|
||||
var protocol_manager_registrations = new StringBuilder();
|
||||
for (var registration : registrations) {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
protocol_imports.append(StringUtils.format("const {} = preload(\"./{}/{}.gd\")", protocol_name, 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)).append(LS);
|
||||
}
|
||||
var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports.toString()
|
||||
, CodeTemplatePlaceholder.protocol_manager_registrations, StringUtils.substringBeforeLast(protocol_manager_registrations.toString(), StringUtils.COMMA));
|
||||
var formatProtocolManagerTemplate = CodeTemplatePlaceholder.formatTemplate(protocolManagerTemplate, placeholderMap);
|
||||
var protocolManagerFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.gd"));
|
||||
FileUtils.writeStringToFile(protocolManagerFile, formatProtocolManagerTemplate, true);
|
||||
logger.info("Generated GdScript protocol manager file:[{}] is in path:[{}]", protocolManagerFile.getName(), protocolManagerFile.getAbsolutePath());
|
||||
createTemplateFile(registrations);
|
||||
|
||||
|
||||
for (var registration : registrations) {
|
||||
@@ -169,28 +138,7 @@ public class CodeGenerateGdScript implements ICodeGenerate {
|
||||
|
||||
@Override
|
||||
public void defaultProtocol(List<ProtocolRegistration> registrations) throws IOException {
|
||||
createTemplateFile();
|
||||
|
||||
|
||||
// 生成ProtocolManager.gd文件
|
||||
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("gdscript/ProtocolManagerTemplate.gd");
|
||||
var protocol_imports = new StringBuilder();
|
||||
var protocol_manager_registrations = new StringBuilder();
|
||||
protocol_imports.append("const ByteBuffer = preload(\"./ByteBuffer.gd\")").append(LS);
|
||||
for (var registration : registrations) {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
protocol_imports.append(StringUtils.format("const {} = preload(\"./{}.gd\")", protocol_name, 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)).append(LS);
|
||||
}
|
||||
var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports.toString()
|
||||
, CodeTemplatePlaceholder.protocol_manager_registrations, StringUtils.substringBeforeLast(protocol_manager_registrations.toString(), StringUtils.COMMA));
|
||||
var formatProtocolManagerTemplate = CodeTemplatePlaceholder.formatTemplate(protocolManagerTemplate, placeholderMap);
|
||||
var protocolManagerFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.gd"));
|
||||
FileUtils.writeStringToFile(protocolManagerFile, formatProtocolManagerTemplate, true);
|
||||
logger.info("Generated GdScript protocol manager file:[{}] is in path:[{}]", protocolManagerFile.getName(), protocolManagerFile.getAbsolutePath());
|
||||
|
||||
createTemplateFile(registrations);
|
||||
|
||||
for (var registration : registrations) {
|
||||
var protocol_id = registration.protocolId();
|
||||
@@ -210,13 +158,27 @@ public class CodeGenerateGdScript implements ICodeGenerate {
|
||||
}
|
||||
}
|
||||
|
||||
private void createTemplateFile() throws IOException {
|
||||
private void createTemplateFile(List<ProtocolRegistration> registrations) throws IOException {
|
||||
var createFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "ByteBuffer.gd"));
|
||||
var fileInputStream = ClassUtils.getFileFromClassPath("gdscript/ByteBuffer.gd");
|
||||
FileUtils.writeInputStreamToFile(createFile, fileInputStream);
|
||||
|
||||
// 生成ProtocolManager.gd文件
|
||||
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("gdscript/ProtocolManagerTemplate.gd");
|
||||
var protocol_manager_registrations = new StringBuilder();
|
||||
for (var registration : registrations) {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
protocol_manager_registrations.append(StringUtils.format("{}{} : {},", TAB_ASCII, protocol_id, protocol_name)).append(LS);
|
||||
}
|
||||
var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_manager_registrations, StringUtils.substringBeforeLast(protocol_manager_registrations.toString(), StringUtils.COMMA));
|
||||
var formatProtocolManagerTemplate = CodeTemplatePlaceholder.formatTemplate(protocolManagerTemplate, placeholderMap);
|
||||
var protocolManagerFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.gd"));
|
||||
FileUtils.writeStringToFile(protocolManagerFile, formatProtocolManagerTemplate, true);
|
||||
logger.info("Generated GdScript protocol manager file:[{}] is in path:[{}]", protocolManagerFile.getName(), protocolManagerFile.getAbsolutePath());
|
||||
}
|
||||
|
||||
private String protocol_class(ProtocolRegistration registration) {
|
||||
private String protocol_class_merge(ProtocolRegistration registration) {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
var protocolTemplate = ClassUtils.getFileFromClassPathToString("gdscript/ProtocolClassTemplate.gd");
|
||||
@@ -227,6 +189,8 @@ public class CodeGenerateGdScript implements ICodeGenerate {
|
||||
, 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)
|
||||
));
|
||||
return formatProtocolTemplate;
|
||||
}
|
||||
|
||||
+2
-2
@@ -38,7 +38,7 @@ public class GdObjectProtocolSerializer implements IGdSerializer {
|
||||
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
|
||||
GenerateProtocolFile.addTabAscii(builder, deep);
|
||||
builder.append(StringUtils.format("buffer.writePacket({}, {})", objectStr, objectProtocolField.getProtocolId())).append(LS);
|
||||
builder.append(StringUtils.format("buffer.writePacket({}, {})", objectStr, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId()))).append(LS);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -46,7 +46,7 @@ public class GdObjectProtocolSerializer implements IGdSerializer {
|
||||
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
|
||||
var result = "result" + GenerateProtocolFile.localVariableId++;
|
||||
GenerateProtocolFile.addTabAscii(builder, deep);
|
||||
builder.append(StringUtils.format("var {} = buffer.readPacket({})", result, objectProtocolField.getProtocolId())).append(LS);
|
||||
builder.append(StringUtils.format("var {} = buffer.readPacket({})", result, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId()))).append(LS);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const ProtocolManager = preload("./ProtocolManager.gd")
|
||||
class_name ByteBuffer
|
||||
|
||||
const EMPTY: String = ""
|
||||
|
||||
@@ -71,9 +71,6 @@ func isReadable() -> bool:
|
||||
func toBytes() -> PackedByteArray:
|
||||
return buffer.data_array.slice(0, writeOffset)
|
||||
|
||||
func newInstance(protocolId: int) -> Object:
|
||||
return ProtocolManager.newInstance(protocolId)
|
||||
|
||||
# -------------------------------------------------write/read-------------------------------------------------
|
||||
func writeBytes(value: PackedByteArray):
|
||||
var length: int = value.size()
|
||||
@@ -304,14 +301,12 @@ func readString() -> String:
|
||||
readOffset += length
|
||||
return value
|
||||
|
||||
func writePacket(packet, protocolId):
|
||||
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
|
||||
protocolRegistration.write(self, packet)
|
||||
func writePacket(packet: Object, protocol: Object):
|
||||
protocol.write(self, packet)
|
||||
pass
|
||||
|
||||
func readPacket(protocolId) -> Object:
|
||||
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
|
||||
return protocolRegistration.read(self)
|
||||
func readPacket(protocol: Object) -> Object:
|
||||
return protocol.read(self)
|
||||
|
||||
func writeBoolArray(array):
|
||||
if (array == null):
|
||||
@@ -449,24 +444,21 @@ func readStringArray() -> Array[String]:
|
||||
array.append(readString())
|
||||
return array
|
||||
|
||||
func writePacketArray(array, protocolId):
|
||||
func writePacketArray(array: Array, protocol: Object):
|
||||
if (array == null):
|
||||
writeInt(0)
|
||||
else:
|
||||
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
|
||||
writeInt(array.size())
|
||||
for element in array:
|
||||
protocolRegistration.write(self, element)
|
||||
protocol.write(self, element)
|
||||
pass
|
||||
|
||||
func readPacketArray(protocolId) -> Array:
|
||||
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
|
||||
var protocol = ProtocolManager.getProtocolClass(protocolId)
|
||||
func readPacketArray(protocol: Object) -> Array:
|
||||
var array = Array([], typeof(protocol), StringName("RefCounted"), protocol)
|
||||
var size = readInt()
|
||||
if (size > 0):
|
||||
for index in range(size):
|
||||
array.append(protocolRegistration.read(self))
|
||||
array.append(protocol.read(self))
|
||||
#var a = array.get_typed_class_name()
|
||||
#var b = array.get_typed_script()
|
||||
#var c = array.get_typed_builtin()
|
||||
@@ -537,26 +529,23 @@ func readIntStringMap() -> Dictionary[int, String]:
|
||||
map[key] = value
|
||||
return map
|
||||
|
||||
func writeIntPacketMap(map, protocolId):
|
||||
func writeIntPacketMap(map: Dictionary, protocol: Object):
|
||||
if (map == null):
|
||||
writeInt(0)
|
||||
else:
|
||||
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
|
||||
writeInt(map.size())
|
||||
for key in map:
|
||||
writeInt(key)
|
||||
protocolRegistration.write(self, map[key])
|
||||
protocol.write(self, map[key])
|
||||
pass
|
||||
|
||||
func readIntPacketMap(protocolId) -> Dictionary:
|
||||
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
|
||||
var protocol = ProtocolManager.getProtocolClass(protocolId)
|
||||
func readIntPacketMap(protocol: Object) -> Dictionary:
|
||||
var map = Dictionary({}, TYPE_INT, "", null, typeof(protocol), StringName("RefCounted"), protocol)
|
||||
var size = readInt()
|
||||
if (size > 0):
|
||||
for index in range(size):
|
||||
var key = readInt()
|
||||
var value = protocolRegistration.read(self)
|
||||
var value = protocol.read(self)
|
||||
map[key] = value
|
||||
return map
|
||||
|
||||
@@ -620,26 +609,23 @@ func readLongStringMap() -> Dictionary[int, String]:
|
||||
map[key] = value
|
||||
return map
|
||||
|
||||
func writeLongPacketMap(map, protocolId):
|
||||
func writeLongPacketMap(map: Dictionary, protocol: Object):
|
||||
if (map == null):
|
||||
writeInt(0)
|
||||
else:
|
||||
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
|
||||
writeInt(map.size())
|
||||
for key in map:
|
||||
writeLong(key)
|
||||
protocolRegistration.write(self, map[key])
|
||||
protocol.write(self, map[key])
|
||||
pass
|
||||
|
||||
func readLongPacketMap(protocolId) -> Dictionary:
|
||||
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
|
||||
var protocol = ProtocolManager.getProtocolClass(protocolId)
|
||||
func readLongPacketMap(protocol: Object) -> Dictionary:
|
||||
var map = Dictionary({}, TYPE_INT, "", null, typeof(protocol), StringName("RefCounted"), protocol)
|
||||
var size = readInt()
|
||||
if (size > 0):
|
||||
for index in range(size):
|
||||
var key = readLong()
|
||||
var value = protocolRegistration.read(self)
|
||||
var value = protocol.read(self)
|
||||
map[key] = value
|
||||
return map
|
||||
|
||||
@@ -703,25 +689,22 @@ func readStringStringMap() -> Dictionary[String, String]:
|
||||
map[key] = value
|
||||
return map
|
||||
|
||||
func writeStringPacketMap(map, protocolId):
|
||||
func writeStringPacketMap(map: Dictionary, protocol: Object):
|
||||
if (map == null):
|
||||
writeInt(0)
|
||||
else:
|
||||
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
|
||||
writeInt(map.size())
|
||||
for key in map:
|
||||
writeString(key)
|
||||
protocolRegistration.write(self, map[key])
|
||||
protocol.write(self, map[key])
|
||||
pass
|
||||
|
||||
func readStringPacketMap(protocolId) -> Dictionary:
|
||||
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
|
||||
var protocol = ProtocolManager.getProtocolClass(protocolId)
|
||||
func readStringPacketMap(protocol: Object) -> Dictionary:
|
||||
var map = Dictionary({}, TYPE_STRING, "", null, typeof(protocol), StringName("RefCounted"), protocol)
|
||||
var size = readInt()
|
||||
if (size > 0):
|
||||
for index in range(size):
|
||||
var key = readString()
|
||||
var value = protocolRegistration.read(self)
|
||||
var value = protocol.read(self)
|
||||
map[key] = value
|
||||
return map
|
||||
|
||||
@@ -8,4 +8,22 @@ class ${protocol_name}:
|
||||
func _to_string() -> String:
|
||||
const jsonTemplate = "${protocol_json}"
|
||||
var params = [${protocol_to_string}]
|
||||
return jsonTemplate.format(params, "{}")
|
||||
return jsonTemplate.format(params, "{}")
|
||||
|
||||
static func write(buffer: ByteBuffer, packet: ${protocol_name}):
|
||||
if (packet == null):
|
||||
buffer.writeInt(0)
|
||||
return
|
||||
${protocol_write_serialization}
|
||||
pass
|
||||
|
||||
static func read(buffer: ByteBuffer) -> ${protocol_name}:
|
||||
var length = buffer.readInt()
|
||||
if (length == 0):
|
||||
return null
|
||||
var beforeReadIndex = buffer.getReadOffset()
|
||||
var packet: ${protocol_name} = ${protocol_name}.new()
|
||||
${protocol_read_deserialization}
|
||||
if (length > 0):
|
||||
buffer.setReadOffset(beforeReadIndex + length)
|
||||
return packet
|
||||
@@ -1,29 +1,14 @@
|
||||
${protocol_imports}
|
||||
static var protocols: Dictionary[int, Object] = {
|
||||
${protocol_manager_registrations}
|
||||
}
|
||||
|
||||
static var protocols: Dictionary[int, RefCounted] = {}
|
||||
static var protocolClassMap: Dictionary[int, Object] = {}
|
||||
|
||||
static func initProtocol():
|
||||
${protocol_manager_registrations}
|
||||
pass
|
||||
|
||||
static func getProtocol(protocolId: int):
|
||||
return protocols[protocolId]
|
||||
|
||||
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):
|
||||
static func write(buffer: ByteBuffer, packet: Object) -> void:
|
||||
var protocolId: int = packet.protocolId()
|
||||
buffer.writeShort(protocolId)
|
||||
var protocol = protocols[protocolId]
|
||||
protocol.write(buffer, packet)
|
||||
packet.write(buffer, packet)
|
||||
pass
|
||||
|
||||
static func read(buffer):
|
||||
static func read(buffer: ByteBuffer) -> Object:
|
||||
var protocolId = buffer.readShort()
|
||||
var protocol = protocols[protocolId]
|
||||
var packet = protocol.read(buffer)
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
class ${protocol_name}Registration:
|
||||
func write(buffer: ByteBuffer, packet: _${protocol_name}):
|
||||
if (packet == null):
|
||||
buffer.writeInt(0)
|
||||
return
|
||||
${protocol_write_serialization}
|
||||
pass
|
||||
static func write(buffer: ByteBuffer, packet: ${protocol_name}):
|
||||
if (packet == null):
|
||||
buffer.writeInt(0)
|
||||
return
|
||||
${protocol_write_serialization}
|
||||
pass
|
||||
|
||||
func read(buffer: ByteBuffer) -> _${protocol_name}:
|
||||
var length = buffer.readInt()
|
||||
if (length == 0):
|
||||
return null
|
||||
var beforeReadIndex = buffer.getReadOffset()
|
||||
var packet: _${protocol_name} = buffer.newInstance(${protocol_id})
|
||||
${protocol_read_deserialization}
|
||||
if (length > 0):
|
||||
buffer.setReadOffset(beforeReadIndex + length)
|
||||
return packet
|
||||
static func read(buffer: ByteBuffer) -> ${protocol_name}:
|
||||
var length = buffer.readInt()
|
||||
if (length == 0):
|
||||
return null
|
||||
var beforeReadIndex = buffer.getReadOffset()
|
||||
var packet: ${protocol_name} = ${protocol_name}.new()
|
||||
${protocol_read_deserialization}
|
||||
if (length > 0):
|
||||
buffer.setReadOffset(beforeReadIndex + length)
|
||||
return packet
|
||||
@@ -1,6 +1,4 @@
|
||||
class_name _${protocol_name}
|
||||
|
||||
${protocol_imports}
|
||||
class_name ${protocol_name}
|
||||
|
||||
${protocol_class}
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
${protocol_imports}
|
||||
class_name Protocols
|
||||
|
||||
${protocol_class}
|
||||
|
||||
${protocol_registration}
|
||||
${protocol_class}
|
||||
Reference in New Issue
Block a user