From 1475e640c6b1e7404a2810ecd1d25f55471200f9 Mon Sep 17 00:00:00 2001 From: godotg Date: Sun, 15 Oct 2023 23:54:51 +0800 Subject: [PATCH] test[gdscript]: compatible field of inside protocol class --- .../resources/gdscript/buffer/ByteBuffer.gd | 9 ++++++--- .../gdscript/zfoogd/packet/NormalObject.gd | 18 +++++++++++++++--- .../src/test/gdscript/zfoogd/packet/ObjectA.gd | 13 ++++++++++--- .../src/test/gdscript/zfoogd/packet/ObjectB.gd | 13 ++++++++++--- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/protocol/src/main/resources/gdscript/buffer/ByteBuffer.gd b/protocol/src/main/resources/gdscript/buffer/ByteBuffer.gd index f86aeec4c..d52e8d843 100644 --- a/protocol/src/main/resources/gdscript/buffer/ByteBuffer.gd +++ b/protocol/src/main/resources/gdscript/buffer/ByteBuffer.gd @@ -25,11 +25,14 @@ func adjustPadding(predictionLength: int, beforeWriteIndex: int) -> void: setWriteOffset(currentWriteIndex) else: buffer.seek(currentWriteIndex - length) - var retainedByteBuf = buffer.get_partial_data(length) - buffer.seek(beforeWriteIndex) + var retainedByteBuf = buffer.get_partial_data(length)[1] + setWriteOffset(beforeWriteIndex) writeInt(length) + buffer.seek(beforeWriteIndex + lengthCount) buffer.put_partial_data(retainedByteBuf) - setWriteOffset(getWriteOffset() + length) + var count = beforeWriteIndex + lengthCount + length + buffer.seek(count) + setWriteOffset(count) pass func compatibleRead(beforeReadIndex: int, length: int) -> bool: diff --git a/protocol/src/test/gdscript/zfoogd/packet/NormalObject.gd b/protocol/src/test/gdscript/zfoogd/packet/NormalObject.gd index 8f60f1d26..524ebaeb6 100644 --- a/protocol/src/test/gdscript/zfoogd/packet/NormalObject.gd +++ b/protocol/src/test/gdscript/zfoogd/packet/NormalObject.gd @@ -22,17 +22,20 @@ var m: Dictionary # Map var mm: Dictionary # Map var s: Array[int] var ssss: Array[String] +var outCompatibleValue: int +var outCompatibleValue2: int func _to_string() -> String: - const jsonTemplate = "{a:{}, aaa:{}, b:{}, c:{}, d:{}, e:{}, f:{}, g:{}, jj:'{}', kk:{}, l:{}, ll:{}, lll:{}, llll:{}, m:{}, mm:{}, s:{}, ssss:{}}" - var params = [self.a, JSON.stringify(self.aaa), self.b, self.c, self.d, self.e, self.f, self.g, self.jj, self.kk, JSON.stringify(self.l), JSON.stringify(self.ll), JSON.stringify(self.lll), JSON.stringify(self.llll), JSON.stringify(self.m), JSON.stringify(self.mm), JSON.stringify(self.s), JSON.stringify(self.ssss)] + const jsonTemplate = "{a:{}, aaa:{}, b:{}, c:{}, d:{}, e:{}, f:{}, g:{}, jj:'{}', kk:{}, l:{}, ll:{}, lll:{}, llll:{}, m:{}, mm:{}, s:{}, ssss:{}, outCompatibleValue:{}, outCompatibleValue2:{}}" + var params = [self.a, JSON.stringify(self.aaa), self.b, self.c, self.d, self.e, self.f, self.g, self.jj, self.kk, JSON.stringify(self.l), JSON.stringify(self.ll), JSON.stringify(self.lll), JSON.stringify(self.llll), JSON.stringify(self.m), JSON.stringify(self.mm), JSON.stringify(self.s), JSON.stringify(self.ssss), self.outCompatibleValue, self.outCompatibleValue2] return jsonTemplate.format(params, "{}") static func write(buffer, packet): if (packet == null): buffer.writeInt(0) return - buffer.writeInt(-1) + var beforeWriteIndex = buffer.getWriteOffset() + buffer.writeInt(857) buffer.writeByte(packet.a) buffer.writeByteArray(packet.aaa) buffer.writeShort(packet.b) @@ -51,6 +54,9 @@ static func write(buffer, packet): buffer.writeIntPacketMap(packet.mm, 102) buffer.writeIntArray(packet.s) buffer.writeStringArray(packet.ssss) + buffer.writeInt(packet.outCompatibleValue) + buffer.writeInt(packet.outCompatibleValue2) + buffer.adjustPadding(857, beforeWriteIndex) pass static func read(buffer): @@ -95,6 +101,12 @@ static func read(buffer): packet.s = set16 var set17 = buffer.readStringArray() packet.ssss = set17 + if buffer.compatibleRead(beforeReadIndex, length): + var result18 = buffer.readInt() + packet.outCompatibleValue = result18; + if buffer.compatibleRead(beforeReadIndex, length): + var result19 = buffer.readInt() + packet.outCompatibleValue2 = result19; if (length > 0): buffer.setReadOffset(beforeReadIndex + length) return packet diff --git a/protocol/src/test/gdscript/zfoogd/packet/ObjectA.gd b/protocol/src/test/gdscript/zfoogd/packet/ObjectA.gd index c795e1871..84b71e798 100644 --- a/protocol/src/test/gdscript/zfoogd/packet/ObjectA.gd +++ b/protocol/src/test/gdscript/zfoogd/packet/ObjectA.gd @@ -6,20 +6,24 @@ const ObjectB = preload("res://zfoogd/packet/ObjectB.gd") var a: int var m: Dictionary # Map var objectB: ObjectB +var innerCompatibleValue: int func _to_string() -> String: - const jsonTemplate = "{a:{}, m:{}, objectB:{}}" - var params = [self.a, JSON.stringify(self.m), self.objectB] + const jsonTemplate = "{a:{}, m:{}, objectB:{}, innerCompatibleValue:{}}" + var params = [self.a, JSON.stringify(self.m), self.objectB, self.innerCompatibleValue] return jsonTemplate.format(params, "{}") static func write(buffer, packet): if (packet == null): buffer.writeInt(0) return - buffer.writeInt(-1) + var beforeWriteIndex = buffer.getWriteOffset() + buffer.writeInt(201) buffer.writeInt(packet.a) buffer.writeIntStringMap(packet.m) buffer.writePacket(packet.objectB, 103) + buffer.writeInt(packet.innerCompatibleValue) + buffer.adjustPadding(201, beforeWriteIndex) pass static func read(buffer): @@ -34,6 +38,9 @@ static func read(buffer): packet.m = map1 var result2 = buffer.readPacket(103) packet.objectB = result2 + if buffer.compatibleRead(beforeReadIndex, length): + var result3 = buffer.readInt() + packet.innerCompatibleValue = result3; if (length > 0): buffer.setReadOffset(beforeReadIndex + length) return packet diff --git a/protocol/src/test/gdscript/zfoogd/packet/ObjectB.gd b/protocol/src/test/gdscript/zfoogd/packet/ObjectB.gd index ccf8135fb..8e7a71a15 100644 --- a/protocol/src/test/gdscript/zfoogd/packet/ObjectB.gd +++ b/protocol/src/test/gdscript/zfoogd/packet/ObjectB.gd @@ -3,18 +3,22 @@ const PROTOCOL_CLASS_NAME = "ObjectB" var flag: bool +var innerCompatibleValue: int func _to_string() -> String: - const jsonTemplate = "{flag:{}}" - var params = [self.flag] + const jsonTemplate = "{flag:{}, innerCompatibleValue:{}}" + var params = [self.flag, self.innerCompatibleValue] return jsonTemplate.format(params, "{}") static func write(buffer, packet): if (packet == null): buffer.writeInt(0) return - buffer.writeInt(-1) + var beforeWriteIndex = buffer.getWriteOffset() + buffer.writeInt(4) buffer.writeBool(packet.flag) + buffer.writeInt(packet.innerCompatibleValue) + buffer.adjustPadding(4, beforeWriteIndex) pass static func read(buffer): @@ -25,6 +29,9 @@ static func read(buffer): var packet = buffer.newInstance(PROTOCOL_ID) var result0 = buffer.readBool() packet.flag = result0 + if buffer.compatibleRead(beforeReadIndex, length): + var result1 = buffer.readInt() + packet.innerCompatibleValue = result1; if (length > 0): buffer.setReadOffset(beforeReadIndex + length) return packet