Skip to content

Commit

Permalink
test[gdscript]: compatible field of inside protocol class
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Oct 15, 2023
1 parent 778e1a5 commit 1475e64
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
9 changes: 6 additions & 3 deletions protocol/src/main/resources/gdscript/buffer/ByteBuffer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 15 additions & 3 deletions protocol/src/test/gdscript/zfoogd/packet/NormalObject.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ var m: Dictionary # Map<number, string>
var mm: Dictionary # Map<number, ObjectA>
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)
Expand All @@ -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):
Expand Down Expand Up @@ -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
13 changes: 10 additions & 3 deletions protocol/src/test/gdscript/zfoogd/packet/ObjectA.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@ const ObjectB = preload("res://zfoogd/packet/ObjectB.gd")
var a: int
var m: Dictionary # Map<number, string>
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):
Expand All @@ -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
13 changes: 10 additions & 3 deletions protocol/src/test/gdscript/zfoogd/packet/ObjectB.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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

0 comments on commit 1475e64

Please sign in to comment.