diff --git a/gdTest/GdProtocolTest.gd b/gdTest/GdProtocolTest.gd new file mode 100644 index 0000000..4a2f18e --- /dev/null +++ b/gdTest/GdProtocolTest.gd @@ -0,0 +1,84 @@ +extends Node2D + + +const ByteBuffer = preload("res://gdTest/gdProtocol/buffer/ByteBuffer.gd") + + +func _ready(): + var buffer = ByteBuffer.new() + + buffer.writeBool(true) + buffer.writeBool(false) + assert(true == buffer.readBool()) + assert(false == buffer.readBool()) + + buffer.writeByte(-128) + buffer.writeByte(99) + buffer.writeByte(127) + assert(-128 == buffer.readByte()) + assert(99 == buffer.readByte()) + assert(127 == buffer.readByte()) + + buffer.writeShort(-32768) + buffer.writeShort(0) + buffer.writeShort(32767) + assert(-32768 == buffer.readShort()) + assert(0 == buffer.readShort()) + assert(32767 == buffer.readShort()) + + buffer.writeInt(-2147483648) + buffer.writeInt(-999999) + buffer.writeInt(0) + buffer.writeInt(999999) + buffer.writeInt(2147483647) + assert(-2147483648 == buffer.readInt()) + assert(-999999 == buffer.readInt()) + assert(0 == buffer.readInt()) + assert(999999 == buffer.readInt()) + assert(2147483647 == buffer.readInt()) + + var maxLong = int("9223372036854775807") + var minLong = int("-9223372036854775808") + buffer.writeLong(maxLong) + buffer.writeLong(9999999999999999) + buffer.writeLong(99999999) + buffer.writeLong(0) + buffer.writeLong(-99999999) + buffer.writeLong(-9999999999999999) + buffer.writeLong(minLong) + + assert(maxLong == buffer.readLong()) + assert(9999999999999999 == buffer.readLong()) + assert(99999999 == buffer.readLong()) + assert(0 == buffer.readLong()) + assert(-99999999 == buffer.readLong()) + assert(-9999999999999999 == buffer.readLong()) + assert(minLong == buffer.readLong()) + + buffer.writeFloat(-1234.5678) + buffer.writeFloat(0) + buffer.writeFloat(1234.5678) + assert(abs(-1234.5678 - buffer.readFloat()) < 0.001) + assert(0 == buffer.readFloat()) + assert(abs(1234.5678 - buffer.readFloat()) < 0.001) + + buffer.writeDouble(-1234.5678) + buffer.writeDouble(0) + buffer.writeDouble(1234.5678) + assert(abs(-1234.5678 - buffer.readDouble()) < 0.001) + assert(0 == buffer.readDouble()) + assert(abs(1234.5678 - buffer.readDouble()) < 0.001) + + var strValue = "你好 hello world" + buffer.writeString(strValue) + assert(strValue == buffer.readString()) + + buffer.writeChar(strValue) + assert(strValue[0] == buffer.readChar()) + buffer.writeChar("") + assert(ByteBuffer.EMPTY == buffer.readChar()) + + + + + diff --git a/gdTest/gdProtocol/buffer/ByteBuffer.gd b/gdTest/gdProtocol/buffer/ByteBuffer.gd new file mode 100644 index 0000000..9bacd79 --- /dev/null +++ b/gdTest/gdProtocol/buffer/ByteBuffer.gd @@ -0,0 +1,237 @@ +const EMPTY: String = "" + +var buffer = StreamPeerBuffer.new() + +var writeOffset: int = 0 setget setWriteOffset, getWriteOffset +var readOffset: int = 0 setget setReadOffset, getReadOffset + +func _init(): + buffer.big_endian = true + +# -------------------------------------------------get/set------------------------------------------------- +func setWriteOffset(writeIndex: int) -> void: + if (writeIndex > buffer.get_size()): + var template = "writeIndex[{}] out of bounds exception: readerIndex: {}, writerIndex: {} (expected: 0 <= readerIndex <= writerIndex <= capacity: {})" + printerr(template.format([writeIndex, readOffset, writeOffset, buffer.size()], "{}")) + return + writeOffset = writeIndex + +func getWriteOffset() -> int: + return writeOffset + +func setReadOffset(readIndex: int) -> void: + if (readIndex > writeOffset): + var template = "readIndex[{}] out of bounds exception: readerIndex: {}, writerIndex: {} (expected: 0 <= readerIndex <= writerIndex <= capacity: {})" + printerr(template.format([readIndex, readOffset, writeOffset, buffer.size()], "{}")) + return + readOffset = readIndex + +func getReadOffset() -> int: + return readOffset + +# -------------------------------------------------write/read------------------------------------------------- + +func writeBool(value: bool) -> void: + var byte = 0 + if (value): + byte = 1 + buffer.seek(writeOffset) + buffer.put_8(byte) + writeOffset += 1 + +func readBool() -> bool: + buffer.seek(readOffset) + var byte = buffer.get_8() + readOffset += 1 + return byte == 1 + +func writeByte(value: int) -> void: + buffer.seek(writeOffset) + buffer.put_8(value) + writeOffset += 1 + +func readByte() -> int: + buffer.seek(readOffset) + var value = buffer.get_8() + readOffset += 1 + return value + +func writeShort(value: int) -> void: + buffer.seek(writeOffset) + buffer.put_16(value) + writeOffset += 2 + +func readShort() -> int: + buffer.seek(readOffset) + var value = buffer.get_16() + readOffset += 2 + return value + +func writeInt(value) -> void: + writeLong(value) + +func readInt() -> int: + return readLong() + +func writeLong(longValue: int) -> void: + var value = (longValue << 1) ^ (longValue >> 63) + + if (value >> 7 == 0): + writeByte(value) + return + + if (value >> 14 == 0): + writeByte(value | 0x80) + writeByte(value >> 7) + return + + if (value >> 21 == 0): + writeByte(value | 0x80) + writeByte((value >> 7) | 0x80) + writeByte(value >> 14) + return + + if (value >> 28 == 0): + writeByte(value | 0x80) + writeByte((value >> 7) | 0x80) + writeByte((value >> 14) | 0x80) + writeByte(value >> 21) + return + + if (value >> 35 == 0): + writeByte(value | 0x80) + writeByte((value >> 7) | 0x80) + writeByte((value >> 14) | 0x80) + writeByte((value >> 21) | 0x80) + writeByte(value >> 28) + return + + if (value >> 42 == 0): + writeByte(value | 0x80) + writeByte((value >> 7) | 0x80) + writeByte((value >> 14) | 0x80) + writeByte((value >> 21) | 0x80) + writeByte((value >> 28) | 0x80) + writeByte(value >> 35) + return + + if (value >> 49 == 0): + writeByte(value | 0x80) + writeByte((value >> 7) | 0x80) + writeByte((value >> 14) | 0x80) + writeByte((value >> 21) | 0x80) + writeByte((value >> 28) | 0x80) + writeByte((value >> 35) | 0x80) + writeByte(value >> 42) + return + + if (value >> 56 == 0): + writeByte(value | 0x80) + writeByte((value >> 7) | 0x80) + writeByte((value >> 14) | 0x80) + writeByte((value >> 21) | 0x80) + writeByte((value >> 28) | 0x80) + writeByte((value >> 35) | 0x80) + writeByte((value >> 42) | 0x80) + writeByte(value >> 49) + return + + writeByte(value | 0x80) + writeByte((value >> 7) | 0x80) + writeByte((value >> 14) | 0x80) + writeByte((value >> 21) | 0x80) + writeByte((value >> 28) | 0x80) + writeByte((value >> 35) | 0x80) + writeByte((value >> 42) | 0x80) + writeByte((value >> 49) | 0x80) + writeByte(value >> 56) + +func readLong() -> int: + var byte: int = readByte() + var value: int = byte + if (byte < 0): + byte = readByte() + value = value & 0x00000000_0000007F | byte << 7 + if (byte < 0): + byte = readByte() + value = value & 0x00000000_00003FFF | byte << 14 + if (byte < 0): + byte = readByte() + value = value & 0x00000000_001FFFFF | byte << 21 + if (byte < 0): + byte = readByte() + value = value & 0x00000000_0FFFFFFF | byte << 28 + if (byte < 0): + byte = readByte() + value = value & 0x00000007_FFFFFFFF | byte << 35 + if (byte < 0): + byte = readByte() + value = value & 0x000003FF_FFFFFFFF | byte << 42 + if (byte < 0): + byte = readByte() + value = value & 0x0001FFFF_FFFFFFFF | byte << 49 + if (byte < 0): + byte = readByte() + value = value & 0x00FFFFFF_FFFFFFFF | byte << 56 + + var mask = value >> 1 + if (mask < 0): + mask = mask & 0x7FFFFFFF_FFFFFFFF + return mask ^ -(value & 1) + + +func writeFloat(value: float) -> void: + buffer.seek(writeOffset) + buffer.put_float(value) + writeOffset += 4 + +func readFloat() -> float: + buffer.seek(readOffset) + var value = buffer.get_float() + readOffset += 4 + return value + +func writeDouble(value: float) -> void: + buffer.seek(writeOffset) + buffer.put_double(value) + writeOffset += 8 + +func readDouble() -> float: + buffer.seek(readOffset) + var value = buffer.get_double() + readOffset += 8 + return value + + +func writeString(value: String) -> void: + if (value == null || value.length() ==0): + writeInt(0) + return + + buffer.seek(writeOffset) + + var strBytes = value.to_utf8() + var length = strBytes.size() + writeInt(length) + buffer.put_partial_data(strBytes) + writeOffset += length + +func readString() -> String: + var length = readInt() + if (length <= 0): + return EMPTY + + buffer.seek(readOffset) + var value = buffer.get_utf8_string(length) + var strBytes = value.to_utf8() + readOffset += length + return value + +func writeChar(value) -> void: + if (value == null || value.length() == 0): + writeString(EMPTY) + return + writeString(value[0]) + +func readChar() -> String: + return readString() diff --git a/gdTest/protocolTest.tscn b/gdTest/protocolTest.tscn new file mode 100644 index 0000000..399f19f --- /dev/null +++ b/gdTest/protocolTest.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://gdTest/GdProtocolTest.gd" type="Script" id=1] + +[node name="Node2D" type="Node2D"] +script = ExtResource( 1 )