Skip to content

Commit edacbfd

Browse files
committed
test[protocol]: gdscript protocol generation test
1 parent 1cbcaac commit edacbfd

File tree

9 files changed

+10565
-10638
lines changed

9 files changed

+10565
-10638
lines changed

protocol/src/test/gdscript/zfoogd/ByteBuffer.gd

+21-38
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const ProtocolManager = preload("./ProtocolManager.gd")
1+
class_name ByteBuffer
22

33
const EMPTY: String = ""
44

@@ -71,9 +71,6 @@ func isReadable() -> bool:
7171
func toBytes() -> PackedByteArray:
7272
return buffer.data_array.slice(0, writeOffset)
7373

74-
func newInstance(protocolId: int) -> Object:
75-
return ProtocolManager.newInstance(protocolId)
76-
7774
# -------------------------------------------------write/read-------------------------------------------------
7875
func writeBytes(value: PackedByteArray):
7976
var length: int = value.size()
@@ -304,14 +301,12 @@ func readString() -> String:
304301
readOffset += length
305302
return value
306303

307-
func writePacket(packet, protocolId):
308-
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
309-
protocolRegistration.write(self, packet)
304+
func writePacket(packet: Object, protocol: Object):
305+
protocol.write(self, packet)
310306
pass
311307

312-
func readPacket(protocolId) -> Object:
313-
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
314-
return protocolRegistration.read(self)
308+
func readPacket(protocol: Object) -> Object:
309+
return protocol.read(self)
315310

316311
func writeBoolArray(array):
317312
if (array == null):
@@ -449,24 +444,21 @@ func readStringArray() -> Array[String]:
449444
array.append(readString())
450445
return array
451446

452-
func writePacketArray(array, protocolId):
447+
func writePacketArray(array: Array, protocol: Object):
453448
if (array == null):
454449
writeInt(0)
455450
else:
456-
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
457451
writeInt(array.size())
458452
for element in array:
459-
protocolRegistration.write(self, element)
453+
protocol.write(self, element)
460454
pass
461455

462-
func readPacketArray(protocolId) -> Array:
463-
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
464-
var protocol = ProtocolManager.getProtocolClass(protocolId)
456+
func readPacketArray(protocol: Object) -> Array:
465457
var array = Array([], typeof(protocol), StringName("RefCounted"), protocol)
466458
var size = readInt()
467459
if (size > 0):
468460
for index in range(size):
469-
array.append(protocolRegistration.read(self))
461+
array.append(protocol.read(self))
470462
#var a = array.get_typed_class_name()
471463
#var b = array.get_typed_script()
472464
#var c = array.get_typed_builtin()
@@ -537,26 +529,23 @@ func readIntStringMap() -> Dictionary[int, String]:
537529
map[key] = value
538530
return map
539531

540-
func writeIntPacketMap(map, protocolId):
532+
func writeIntPacketMap(map: Dictionary, protocol: Object):
541533
if (map == null):
542534
writeInt(0)
543535
else:
544-
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
545536
writeInt(map.size())
546537
for key in map:
547538
writeInt(key)
548-
protocolRegistration.write(self, map[key])
539+
protocol.write(self, map[key])
549540
pass
550541

551-
func readIntPacketMap(protocolId) -> Dictionary:
552-
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
553-
var protocol = ProtocolManager.getProtocolClass(protocolId)
542+
func readIntPacketMap(protocol: Object) -> Dictionary:
554543
var map = Dictionary({}, TYPE_INT, "", null, typeof(protocol), StringName("RefCounted"), protocol)
555544
var size = readInt()
556545
if (size > 0):
557546
for index in range(size):
558547
var key = readInt()
559-
var value = protocolRegistration.read(self)
548+
var value = protocol.read(self)
560549
map[key] = value
561550
return map
562551

@@ -620,26 +609,23 @@ func readLongStringMap() -> Dictionary[int, String]:
620609
map[key] = value
621610
return map
622611

623-
func writeLongPacketMap(map, protocolId):
612+
func writeLongPacketMap(map: Dictionary, protocol: Object):
624613
if (map == null):
625614
writeInt(0)
626615
else:
627-
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
628616
writeInt(map.size())
629617
for key in map:
630618
writeLong(key)
631-
protocolRegistration.write(self, map[key])
619+
protocol.write(self, map[key])
632620
pass
633621

634-
func readLongPacketMap(protocolId) -> Dictionary:
635-
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
636-
var protocol = ProtocolManager.getProtocolClass(protocolId)
622+
func readLongPacketMap(protocol: Object) -> Dictionary:
637623
var map = Dictionary({}, TYPE_INT, "", null, typeof(protocol), StringName("RefCounted"), protocol)
638624
var size = readInt()
639625
if (size > 0):
640626
for index in range(size):
641627
var key = readLong()
642-
var value = protocolRegistration.read(self)
628+
var value = protocol.read(self)
643629
map[key] = value
644630
return map
645631

@@ -703,25 +689,22 @@ func readStringStringMap() -> Dictionary[String, String]:
703689
map[key] = value
704690
return map
705691

706-
func writeStringPacketMap(map, protocolId):
692+
func writeStringPacketMap(map: Dictionary, protocol: Object):
707693
if (map == null):
708694
writeInt(0)
709695
else:
710-
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
711696
writeInt(map.size())
712697
for key in map:
713698
writeString(key)
714-
protocolRegistration.write(self, map[key])
699+
protocol.write(self, map[key])
715700
pass
716701

717-
func readStringPacketMap(protocolId) -> Dictionary:
718-
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
719-
var protocol = ProtocolManager.getProtocolClass(protocolId)
702+
func readStringPacketMap(protocol: Object) -> Dictionary:
720703
var map = Dictionary({}, TYPE_STRING, "", null, typeof(protocol), StringName("RefCounted"), protocol)
721704
var size = readInt()
722705
if (size > 0):
723706
for index in range(size):
724707
var key = readString()
725-
var value = protocolRegistration.read(self)
708+
var value = protocol.read(self)
726709
map[key] = value
727710
return map

protocol/src/test/gdscript/zfoogd/ProtocolManager.gd

+13-41
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,20 @@
1-
const EmptyObject = preload("./packet/EmptyObject.gd")
2-
const VeryBigObject = preload("./packet/VeryBigObject.gd")
3-
const ComplexObject = preload("./packet/ComplexObject.gd")
4-
const NormalObject = preload("./packet/NormalObject.gd")
5-
const ObjectA = preload("./packet/ObjectA.gd")
6-
const ObjectB = preload("./packet/ObjectB.gd")
7-
const SimpleObject = preload("./packet/SimpleObject.gd")
1+
static var protocols: Dictionary[int, Object] = {
2+
0 : EmptyObject,
3+
1 : VeryBigObject,
4+
100 : ComplexObject,
5+
101 : NormalObject,
6+
102 : ObjectA,
7+
103 : ObjectB,
8+
104 : SimpleObject
9+
}
810

9-
static var protocols: Dictionary[int, RefCounted] = {}
10-
static var protocolClassMap: Dictionary[int, Object] = {}
11-
12-
static func initProtocol():
13-
protocols[0] = EmptyObject.EmptyObjectRegistration.new()
14-
protocolClassMap[0] = EmptyObject
15-
protocols[1] = VeryBigObject.VeryBigObjectRegistration.new()
16-
protocolClassMap[1] = VeryBigObject
17-
protocols[100] = ComplexObject.ComplexObjectRegistration.new()
18-
protocolClassMap[100] = ComplexObject
19-
protocols[101] = NormalObject.NormalObjectRegistration.new()
20-
protocolClassMap[101] = NormalObject
21-
protocols[102] = ObjectA.ObjectARegistration.new()
22-
protocolClassMap[102] = ObjectA
23-
protocols[103] = ObjectB.ObjectBRegistration.new()
24-
protocolClassMap[103] = ObjectB
25-
protocols[104] = SimpleObject.SimpleObjectRegistration.new()
26-
protocolClassMap[104] = SimpleObject
27-
pass
28-
29-
static func getProtocol(protocolId: int):
30-
return protocols[protocolId]
31-
32-
static func getProtocolClass(protocolId: int):
33-
return protocolClassMap[protocolId]
34-
35-
static func newInstance(protocolId: int):
36-
var protocol = protocolClassMap[protocolId]
37-
return protocol.new()
38-
39-
static func write(buffer, packet):
11+
static func write(buffer: ByteBuffer, packet: Object) -> void:
4012
var protocolId: int = packet.protocolId()
4113
buffer.writeShort(protocolId)
42-
var protocol = protocols[protocolId]
43-
protocol.write(buffer, packet)
14+
packet.write(buffer, packet)
15+
pass
4416

45-
static func read(buffer):
17+
static func read(buffer: ByteBuffer) -> Object:
4618
var protocolId = buffer.readShort()
4719
var protocol = protocols[protocolId]
4820
var packet = protocol.read(buffer)

0 commit comments

Comments
 (0)