test[protocol]: gdscript protocol generation test

This commit is contained in:
godotg committed 2025-04-12 22:18:51 +08:00
1 parent 1cbcaac1ed
commit edacbfd9fa
9 files changed
+607 -680

No files matched your search

+21 -38
View File
@@ -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
@@ -1,48 +1,20 @@
const EmptyObject = preload("./packet/EmptyObject.gd")
const VeryBigObject = preload("./packet/VeryBigObject.gd")
const ComplexObject = preload("./packet/ComplexObject.gd")
const NormalObject = preload("./packet/NormalObject.gd")
const ObjectA = preload("./packet/ObjectA.gd")
const ObjectB = preload("./packet/ObjectB.gd")
const SimpleObject = preload("./packet/SimpleObject.gd")
static var protocols: Dictionary[int, Object] = {
0 : EmptyObject,
1 : VeryBigObject,
100 : ComplexObject,
101 : NormalObject,
102 : ObjectA,
103 : ObjectB,
104 : SimpleObject
}
static var protocols: Dictionary[int, RefCounted] = {}
static var protocolClassMap: Dictionary[int, Object] = {}
static func initProtocol():
protocols[0] = EmptyObject.EmptyObjectRegistration.new()
protocolClassMap[0] = EmptyObject
protocols[1] = VeryBigObject.VeryBigObjectRegistration.new()
protocolClassMap[1] = VeryBigObject
protocols[100] = ComplexObject.ComplexObjectRegistration.new()
protocolClassMap[100] = ComplexObject
protocols[101] = NormalObject.NormalObjectRegistration.new()
protocolClassMap[101] = NormalObject
protocols[102] = ObjectA.ObjectARegistration.new()
protocolClassMap[102] = ObjectA
protocols[103] = ObjectB.ObjectBRegistration.new()
protocolClassMap[103] = ObjectB
protocols[104] = SimpleObject.SimpleObjectRegistration.new()
protocolClassMap[104] = SimpleObject
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,9 +1,5 @@
class_name ComplexObject
const ByteBuffer = preload("../ByteBuffer.gd")
const ObjectA = preload("./ObjectA.gd")
const ObjectB = preload("./ObjectB.gd")
# 复杂的对象,包括了各种复杂的结构,数组,List,Set,Map
# byte类型,最简单的整形
var a: int
@@ -67,8 +63,7 @@ func _to_string() -> String:
var params = [self.a, self.aa, JSON.stringify(self.aaa), JSON.stringify(self.aaaa), self.b, self.bb, JSON.stringify(self.bbb), JSON.stringify(self.bbbb), self.c, self.cc, JSON.stringify(self.ccc), JSON.stringify(self.cccc), self.d, self.dd, JSON.stringify(self.ddd), JSON.stringify(self.dddd), self.e, self.ee, JSON.stringify(self.eee), JSON.stringify(self.eeee), self.f, self.ff, JSON.stringify(self.fff), JSON.stringify(self.ffff), self.g, self.gg, JSON.stringify(self.ggg), JSON.stringify(self.gggg), self.jj, JSON.stringify(self.jjj), self.kk, JSON.stringify(self.kkk), JSON.stringify(self.l), JSON.stringify(self.ll), JSON.stringify(self.lll), JSON.stringify(self.llll), JSON.stringify(self.lllll), self.m, self.mm, JSON.stringify(self.mmm), JSON.stringify(self.mmmm), JSON.stringify(self.mmmmm), JSON.stringify(self.s), JSON.stringify(self.ss), JSON.stringify(self.sss), JSON.stringify(self.ssss), JSON.stringify(self.sssss), self.myCompatible, self.myObject]
return jsonTemplate.format(params, "{}")
class ComplexObjectRegistration:
func write(buffer: ByteBuffer, packet: ComplexObject):
static func write(buffer: ByteBuffer, packet: ComplexObject):
if (packet == null):
buffer.writeInt(0)
return
@@ -104,8 +99,8 @@ class ComplexObjectRegistration:
buffer.writeBoolArray(packet.gggg)
buffer.writeString(packet.jj)
buffer.writeStringArray(packet.jjj)
buffer.writePacket(packet.kk, 102)
buffer.writePacketArray(packet.kkk, 102)
buffer.writePacket(packet.kk, ObjectA)
buffer.writePacketArray(packet.kkk, ObjectA)
buffer.writeIntArray(packet.l)
if (packet.ll == null):
buffer.writeInt(0)
@@ -123,7 +118,7 @@ class ComplexObjectRegistration:
else:
buffer.writeInt(packet.lll.size())
for element2 in packet.lll:
buffer.writePacketArray(element2, 102)
buffer.writePacketArray(element2, ObjectA)
buffer.writeStringArray(packet.llll)
if (packet.lllll == null):
buffer.writeInt(0)
@@ -132,14 +127,14 @@ class ComplexObjectRegistration:
for element3 in packet.lllll:
buffer.writeIntStringMap(element3)
buffer.writeIntStringMap(packet.m)
buffer.writeIntPacketMap(packet.mm, 102)
buffer.writeIntPacketMap(packet.mm, ObjectA)
if (packet.mmm == null):
buffer.writeInt(0)
else:
buffer.writeInt(packet.mmm.size())
for key4 in packet.mmm:
var value5 = packet.mmm[key4]
buffer.writePacket(key4, 102)
buffer.writePacket(key4, ObjectA)
buffer.writeIntArray(value5)
if (packet.mmmm == null):
buffer.writeInt(0)
@@ -152,7 +147,7 @@ class ComplexObjectRegistration:
else:
buffer.writeInt(key6.size())
for element8 in key6:
buffer.writePacketArray(element8, 102)
buffer.writePacketArray(element8, ObjectA)
if (value7 == null):
buffer.writeInt(0)
else:
@@ -199,7 +194,7 @@ class ComplexObjectRegistration:
else:
buffer.writeInt(packet.sss.size())
for element17 in packet.sss:
buffer.writePacketArray(element17, 102)
buffer.writePacketArray(element17, ObjectA)
buffer.writeStringArray(packet.ssss)
if (packet.sssss == null):
buffer.writeInt(0)
@@ -208,16 +203,16 @@ class ComplexObjectRegistration:
for element18 in packet.sssss:
buffer.writeIntStringMap(element18)
buffer.writeInt(packet.myCompatible)
buffer.writePacket(packet.myObject, 102)
buffer.writePacket(packet.myObject, ObjectA)
buffer.adjustPadding(36962, beforeWriteIndex)
pass
func read(buffer: ByteBuffer) -> ComplexObject:
static func read(buffer: ByteBuffer) -> ComplexObject:
var length = buffer.readInt()
if (length == 0):
return null
var beforeReadIndex = buffer.getReadOffset()
var packet: ComplexObject = buffer.newInstance(100)
var packet: ComplexObject = ComplexObject.new()
var result0 = buffer.readByte()
packet.a = result0
var result1 = buffer.readByte()
@@ -278,9 +273,9 @@ class ComplexObjectRegistration:
packet.jj = result28
var array29 = buffer.readStringArray()
packet.jjj = array29
var result30 = buffer.readPacket(102)
var result30 = buffer.readPacket(ObjectA)
packet.kk = result30
var array31 = buffer.readPacketArray(102)
var array31 = buffer.readPacketArray(ObjectA)
packet.kkk = array31
var list32 = buffer.readIntArray()
packet.l = list32
@@ -300,7 +295,7 @@ class ComplexObjectRegistration:
var size42 = buffer.readInt()
if (size42 > 0):
for index41 in range(size42):
var list43 = buffer.readPacketArray(102)
var list43 = buffer.readPacketArray(ObjectA)
result40.append(list43)
packet.lll = result40
var list44 = buffer.readStringArray()
@@ -314,13 +309,13 @@ class ComplexObjectRegistration:
packet.lllll = result45
var map49 = buffer.readIntStringMap()
packet.m = map49
var map50 = buffer.readIntPacketMap(102)
var map50 = buffer.readIntPacketMap(ObjectA)
packet.mm = map50
var result51 = {}
var size52 = buffer.readInt()
if (size52 > 0):
for index53 in range(size52):
var result54 = buffer.readPacket(102)
var result54 = buffer.readPacket(ObjectA)
var list55 = buffer.readIntArray()
result51[result54] = list55
packet.mmm = result51
@@ -332,7 +327,7 @@ class ComplexObjectRegistration:
var size61 = buffer.readInt()
if (size61 > 0):
for index60 in range(size61):
var list62 = buffer.readPacketArray(102)
var list62 = buffer.readPacketArray(ObjectA)
result59.append(list62)
var result63 = []
var size65 = buffer.readInt()
@@ -383,7 +378,7 @@ class ComplexObjectRegistration:
var size91 = buffer.readInt()
if (size91 > 0):
for index90 in range(size91):
var set92 = buffer.readPacketArray(102)
var set92 = buffer.readPacketArray(ObjectA)
result89.append(set92)
packet.sss = result89
var set93 = buffer.readStringArray()
@@ -399,7 +394,7 @@ class ComplexObjectRegistration:
var result98 = buffer.readInt()
packet.myCompatible = result98
if buffer.compatibleRead(beforeReadIndex, length):
var result99 = buffer.readPacket(102)
var result99 = buffer.readPacket(ObjectA)
packet.myObject = result99
if (length > 0):
buffer.setReadOffset(beforeReadIndex + length)
@@ -1,7 +1,5 @@
class_name EmptyObject
const ByteBuffer = preload("../ByteBuffer.gd")
@@ -13,20 +11,19 @@ func _to_string() -> String:
var params = []
return jsonTemplate.format(params, "{}")
class EmptyObjectRegistration:
func write(buffer: ByteBuffer, packet: EmptyObject):
static func write(buffer: ByteBuffer, packet: EmptyObject):
if (packet == null):
buffer.writeInt(0)
return
buffer.writeInt(-1)
pass
func read(buffer: ByteBuffer) -> EmptyObject:
static func read(buffer: ByteBuffer) -> EmptyObject:
var length = buffer.readInt()
if (length == 0):
return null
var beforeReadIndex = buffer.getReadOffset()
var packet: EmptyObject = buffer.newInstance(0)
var packet: EmptyObject = EmptyObject.new()
if (length > 0):
buffer.setReadOffset(beforeReadIndex + length)
@@ -1,9 +1,5 @@
class_name NormalObject
const ByteBuffer = preload("../ByteBuffer.gd")
const ObjectA = preload("./ObjectA.gd")
const ObjectB = preload("./ObjectB.gd")
# 常规的对象,取所有语言语法的交集,基本上所有语言都支持下面的语法
var a: int
var aaa: Array[int]
@@ -33,8 +29,7 @@ func _to_string() -> String:
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), self.m, self.mm, JSON.stringify(self.s), JSON.stringify(self.ssss)]
return jsonTemplate.format(params, "{}")
class NormalObjectRegistration:
func write(buffer: ByteBuffer, packet: NormalObject):
static func write(buffer: ByteBuffer, packet: NormalObject):
if (packet == null):
buffer.writeInt(0)
return
@@ -48,23 +43,23 @@ class NormalObjectRegistration:
buffer.writeDouble(packet.f)
buffer.writeBool(packet.g)
buffer.writeString(packet.jj)
buffer.writePacket(packet.kk, 102)
buffer.writePacket(packet.kk, ObjectA)
buffer.writeIntArray(packet.l)
buffer.writeLongArray(packet.ll)
buffer.writePacketArray(packet.lll, 102)
buffer.writePacketArray(packet.lll, ObjectA)
buffer.writeStringArray(packet.llll)
buffer.writeIntStringMap(packet.m)
buffer.writeIntPacketMap(packet.mm, 102)
buffer.writeIntPacketMap(packet.mm, ObjectA)
buffer.writeIntArray(packet.s)
buffer.writeStringArray(packet.ssss)
pass
func read(buffer: ByteBuffer) -> NormalObject:
static func read(buffer: ByteBuffer) -> NormalObject:
var length = buffer.readInt()
if (length == 0):
return null
var beforeReadIndex = buffer.getReadOffset()
var packet: NormalObject = buffer.newInstance(101)
var packet: NormalObject = NormalObject.new()
var result0 = buffer.readByte()
packet.a = result0
var array1 = buffer.readByteArray()
@@ -83,19 +78,19 @@ class NormalObjectRegistration:
packet.g = result7
var result8 = buffer.readString()
packet.jj = result8
var result9 = buffer.readPacket(102)
var result9 = buffer.readPacket(ObjectA)
packet.kk = result9
var list10 = buffer.readIntArray()
packet.l = list10
var list11 = buffer.readLongArray()
packet.ll = list11
var list12 = buffer.readPacketArray(102)
var list12 = buffer.readPacketArray(ObjectA)
packet.lll = list12
var list13 = buffer.readStringArray()
packet.llll = list13
var map14 = buffer.readIntStringMap()
packet.m = map14
var map15 = buffer.readIntPacketMap(102)
var map15 = buffer.readIntPacketMap(ObjectA)
packet.mm = map15
var set16 = buffer.readIntArray()
packet.s = set16
@@ -1,8 +1,5 @@
class_name ObjectA
const ByteBuffer = preload("../ByteBuffer.gd")
const ObjectB = preload("./ObjectB.gd")
var a: int
var m: Dictionary[int, String]
@@ -16,28 +13,27 @@ func _to_string() -> String:
var params = [self.a, self.m, self.objectB]
return jsonTemplate.format(params, "{}")
class ObjectARegistration:
func write(buffer: ByteBuffer, packet: ObjectA):
static func write(buffer: ByteBuffer, packet: ObjectA):
if (packet == null):
buffer.writeInt(0)
return
buffer.writeInt(-1)
buffer.writeInt(packet.a)
buffer.writeIntStringMap(packet.m)
buffer.writePacket(packet.objectB, 103)
buffer.writePacket(packet.objectB, ObjectB)
pass
func read(buffer: ByteBuffer) -> ObjectA:
static func read(buffer: ByteBuffer) -> ObjectA:
var length = buffer.readInt()
if (length == 0):
return null
var beforeReadIndex = buffer.getReadOffset()
var packet: ObjectA = buffer.newInstance(102)
var packet: ObjectA = ObjectA.new()
var result0 = buffer.readInt()
packet.a = result0
var map1 = buffer.readIntStringMap()
packet.m = map1
var result2 = buffer.readPacket(103)
var result2 = buffer.readPacket(ObjectB)
packet.objectB = result2
if (length > 0):
buffer.setReadOffset(beforeReadIndex + length)
@@ -1,7 +1,5 @@
class_name ObjectB
const ByteBuffer = preload("../ByteBuffer.gd")
var flag: bool
@@ -13,8 +11,7 @@ func _to_string() -> String:
var params = [self.flag]
return jsonTemplate.format(params, "{}")
class ObjectBRegistration:
func write(buffer: ByteBuffer, packet: ObjectB):
static func write(buffer: ByteBuffer, packet: ObjectB):
if (packet == null):
buffer.writeInt(0)
return
@@ -22,12 +19,12 @@ class ObjectBRegistration:
buffer.writeBool(packet.flag)
pass
func read(buffer: ByteBuffer) -> ObjectB:
static func read(buffer: ByteBuffer) -> ObjectB:
var length = buffer.readInt()
if (length == 0):
return null
var beforeReadIndex = buffer.getReadOffset()
var packet: ObjectB = buffer.newInstance(103)
var packet: ObjectB = ObjectB.new()
var result0 = buffer.readBool()
packet.flag = result0
if (length > 0):
@@ -1,7 +1,5 @@
class_name SimpleObject
const ByteBuffer = preload("../ByteBuffer.gd")
var c: int
var g: bool
@@ -14,8 +12,7 @@ func _to_string() -> String:
var params = [self.c, self.g]
return jsonTemplate.format(params, "{}")
class SimpleObjectRegistration:
func write(buffer: ByteBuffer, packet: SimpleObject):
static func write(buffer: ByteBuffer, packet: SimpleObject):
if (packet == null):
buffer.writeInt(0)
return
@@ -24,12 +21,12 @@ class SimpleObjectRegistration:
buffer.writeBool(packet.g)
pass
func read(buffer: ByteBuffer) -> SimpleObject:
static func read(buffer: ByteBuffer) -> SimpleObject:
var length = buffer.readInt()
if (length == 0):
return null
var beforeReadIndex = buffer.getReadOffset()
var packet: SimpleObject = buffer.newInstance(104)
var packet: SimpleObject = SimpleObject.new()
var result0 = buffer.readInt()
packet.c = result0
var result1 = buffer.readBool()
File diff suppressed because it is too large. Load diff