mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-20 04:24:13 +00:00
test[protocol]: test GdScript protocol
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
extends Node2D
|
||||
|
||||
|
||||
const ProtocolManager = preload("res://zfoogd/ProtocolManager.gd")
|
||||
const ByteBuffer = preload("res://zfoogd/ByteBuffer.gd")
|
||||
|
||||
# 测试参数
|
||||
#op.setFoldProtocol(true);
|
||||
@@ -82,7 +80,6 @@ func _ready():
|
||||
|
||||
|
||||
func test():
|
||||
ProtocolManager.initProtocol()
|
||||
var buffer = ByteBuffer.new()
|
||||
|
||||
var filePath = "res://compatible/normal-out-inner-inner-compatible.bytes"
|
||||
@@ -96,14 +93,12 @@ func test():
|
||||
buffer.writeBytes(poolByteArray)
|
||||
|
||||
var packet = ProtocolManager.read(buffer)
|
||||
print(packet)
|
||||
print(ByteBuffer.object_to_json(packet))
|
||||
|
||||
var newByteBuffer = ByteBuffer.new()
|
||||
ProtocolManager.write(newByteBuffer, packet);
|
||||
print(newByteBuffer.getWriteOffset())
|
||||
print("-----------------------------------------------size:" + str(newByteBuffer.getWriteOffset()))
|
||||
|
||||
var newPacket = ProtocolManager.read(newByteBuffer);
|
||||
print(newPacket)
|
||||
print(ByteBuffer.object_to_json(newPacket))
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@@ -39,6 +39,47 @@ func adjustPadding(predictionLength: int, beforeWriteIndex: int):
|
||||
func compatibleRead(beforeReadIndex: int, length: int) -> bool:
|
||||
return length != -1 && getReadOffset() < length + beforeReadIndex
|
||||
|
||||
static func object_to_json(obj) -> String:
|
||||
var type = typeof(obj)
|
||||
match type:
|
||||
TYPE_NIL:
|
||||
return "null"
|
||||
TYPE_BOOL:
|
||||
return str(obj)
|
||||
TYPE_INT:
|
||||
return str(obj)
|
||||
TYPE_FLOAT:
|
||||
return str(obj)
|
||||
TYPE_STRING:
|
||||
return "\"" + obj as String + "\""
|
||||
TYPE_ARRAY:
|
||||
var array: PackedStringArray = PackedStringArray()
|
||||
for element in obj:
|
||||
array.push_back(object_to_json(element))
|
||||
return "[" + ",".join(array) + "]"
|
||||
TYPE_DICTIONARY:
|
||||
var array: PackedStringArray = PackedStringArray()
|
||||
for key in obj:
|
||||
var value = obj.get(key)
|
||||
array.push_back("\"" + object_to_json(key) + "\":" + object_to_json(value))
|
||||
return "{" + ",".join(array) + "}"
|
||||
TYPE_OBJECT:
|
||||
var properties = obj.get_property_list()
|
||||
var array: PackedStringArray = PackedStringArray()
|
||||
for property in properties:
|
||||
var propertyType = property.type
|
||||
var propertyName = property.name
|
||||
if propertyType == TYPE_NIL:
|
||||
continue
|
||||
if propertyName == "script":
|
||||
continue
|
||||
var value = obj.get(propertyName)
|
||||
array.push_back("\"" + propertyName + "\":" + object_to_json(value))
|
||||
return "{" + ",".join(array) + "}"
|
||||
_:
|
||||
printerr("unknow type " + type)
|
||||
return ""
|
||||
|
||||
# -------------------------------------------------get/set-------------------------------------------------
|
||||
func getBuffer() -> StreamPeerBuffer:
|
||||
return buffer
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
static var protocols: Dictionary[int, Object] = {
|
||||
0 : EmptyObject,
|
||||
1 : VeryBigObject,
|
||||
100 : ComplexObject,
|
||||
101 : NormalObject,
|
||||
102 : ObjectA,
|
||||
@@ -9,7 +8,7 @@ static var protocols: Dictionary[int, Object] = {
|
||||
}
|
||||
|
||||
static func write(buffer: ByteBuffer, packet: Object) -> void:
|
||||
var protocolId: int = packet.protocolId()
|
||||
var protocolId: int = packet.PROTOCOL_ID
|
||||
buffer.writeShort(protocolId)
|
||||
packet.write(buffer, packet)
|
||||
pass
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
class_name ComplexObject
|
||||
|
||||
const PROTOCOL_ID: int = 100
|
||||
# 复杂的对象,包括了各种复杂的结构,数组,List,Set,Map
|
||||
# byte类型,最简单的整形
|
||||
var a: int
|
||||
@@ -55,14 +55,6 @@ var sssss: Array # Set<Map<number, string>>
|
||||
var myCompatible: int
|
||||
var myObject: ObjectA
|
||||
|
||||
func protocolId() -> int:
|
||||
return 100
|
||||
|
||||
func _to_string() -> String:
|
||||
const jsonTemplate = "{a:{}, aa:{}, aaa:{}, aaaa:{}, b:{}, bb:{}, bbb:{}, bbbb:{}, c:{}, cc:{}, ccc:{}, cccc:{}, d:{}, dd:{}, ddd:{}, dddd:{}, e:{}, ee:{}, eee:{}, eeee:{}, f:{}, ff:{}, fff:{}, ffff:{}, g:{}, gg:{}, ggg:{}, gggg:{}, jj:'{}', jjj:{}, kk:{}, kkk:{}, l:{}, ll:{}, lll:{}, llll:{}, lllll:{}, m:{}, mm:{}, mmm:{}, mmmm:{}, mmmmm:{}, s:{}, ss:{}, sss:{}, ssss:{}, sssss:{}, myCompatible:{}, myObject:{}}"
|
||||
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, "{}")
|
||||
|
||||
static func write(buffer: ByteBuffer, packet: ComplexObject):
|
||||
if (packet == null):
|
||||
buffer.writeInt(0)
|
||||
|
||||
@@ -1,15 +1,5 @@
|
||||
class_name EmptyObject
|
||||
|
||||
|
||||
|
||||
|
||||
func protocolId() -> int:
|
||||
return 0
|
||||
|
||||
func _to_string() -> String:
|
||||
const jsonTemplate = "{}"
|
||||
var params = []
|
||||
return jsonTemplate.format(params, "{}")
|
||||
const PROTOCOL_ID: int = 0
|
||||
|
||||
static func write(buffer: ByteBuffer, packet: EmptyObject):
|
||||
if (packet == null):
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
class_name NormalObject
|
||||
|
||||
const PROTOCOL_ID: int = 101
|
||||
# 常规的对象,取所有语言语法的交集,基本上所有语言都支持下面的语法
|
||||
var a: int
|
||||
var aaa: Array[int]
|
||||
@@ -21,14 +21,6 @@ var mm: Dictionary[int, ObjectA]
|
||||
var s: Array[int]
|
||||
var ssss: Array[String]
|
||||
|
||||
func protocolId() -> int:
|
||||
return 101
|
||||
|
||||
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), self.m, self.mm, JSON.stringify(self.s), JSON.stringify(self.ssss)]
|
||||
return jsonTemplate.format(params, "{}")
|
||||
|
||||
static func write(buffer: ByteBuffer, packet: NormalObject):
|
||||
if (packet == null):
|
||||
buffer.writeInt(0)
|
||||
|
||||
@@ -1,18 +1,10 @@
|
||||
class_name ObjectA
|
||||
|
||||
const PROTOCOL_ID: int = 102
|
||||
|
||||
var a: int
|
||||
var m: Dictionary[int, String]
|
||||
var objectB: ObjectB
|
||||
|
||||
func protocolId() -> int:
|
||||
return 102
|
||||
|
||||
func _to_string() -> String:
|
||||
const jsonTemplate = "{a:{}, m:{}, objectB:{}}"
|
||||
var params = [self.a, self.m, self.objectB]
|
||||
return jsonTemplate.format(params, "{}")
|
||||
|
||||
static func write(buffer: ByteBuffer, packet: ObjectA):
|
||||
if (packet == null):
|
||||
buffer.writeInt(0)
|
||||
|
||||
@@ -1,16 +1,8 @@
|
||||
class_name ObjectB
|
||||
|
||||
const PROTOCOL_ID: int = 103
|
||||
|
||||
var flag: bool
|
||||
|
||||
func protocolId() -> int:
|
||||
return 103
|
||||
|
||||
func _to_string() -> String:
|
||||
const jsonTemplate = "{flag:{}}"
|
||||
var params = [self.flag]
|
||||
return jsonTemplate.format(params, "{}")
|
||||
|
||||
static func write(buffer: ByteBuffer, packet: ObjectB):
|
||||
if (packet == null):
|
||||
buffer.writeInt(0)
|
||||
|
||||
@@ -1,17 +1,9 @@
|
||||
class_name SimpleObject
|
||||
|
||||
const PROTOCOL_ID: int = 104
|
||||
|
||||
var c: int
|
||||
var g: bool
|
||||
|
||||
func protocolId() -> int:
|
||||
return 104
|
||||
|
||||
func _to_string() -> String:
|
||||
const jsonTemplate = "{c:{}, g:{}}"
|
||||
var params = [self.c, self.g]
|
||||
return jsonTemplate.format(params, "{}")
|
||||
|
||||
static func write(buffer: ByteBuffer, packet: SimpleObject):
|
||||
if (packet == null):
|
||||
buffer.writeInt(0)
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user