diff --git a/protocol/src/main/resources/gdscript/buffer/ByteBuffer.gd b/protocol/src/main/resources/gdscript/buffer/ByteBuffer.gd index f0240a12..e9f4aaf2 100644 --- a/protocol/src/main/resources/gdscript/buffer/ByteBuffer.gd +++ b/protocol/src/main/resources/gdscript/buffer/ByteBuffer.gd @@ -2,6 +2,9 @@ const ProtocolManager = preload("res://{}/ProtocolManager.gd") const EMPTY: String = "" +const maxInt: int = 2147483647 +const minInt: int = -2147483648 + var buffer = StreamPeerBuffer.new() var writeOffset: int = 0 @@ -10,6 +13,28 @@ var readOffset: int = 0 func _init(): buffer.big_endian = true +func adjustPadding(predictionLength: int, beforeWriteIndex: int) -> void: + var currentWriteIndex = writeOffset + var predictionCount = writeIntCount(predictionLength) + var length = currentWriteIndex - beforeWriteIndex - predictionCount + var lengthCount = writeIntCount(length) + var padding = lengthCount - predictionCount + if padding == 0: + setWriteOffset(beforeWriteIndex) + writeInt(length) + setWriteOffset(currentWriteIndex) + else: + buffer.seek(currentWriteIndex - length) + var retainedByteBuf = buffer.get_partial_data(length) + buffer.seek(beforeWriteIndex) + writeInt(length) + buffer.put_partial_data(retainedByteBuf) + setWriteOffset(getWriteOffset() + length) + pass + +func compatibleRead(beforeReadIndex: int, length: int) -> bool: + return length != -1 && getReadOffset() < length + beforeReadIndex + # -------------------------------------------------get/set------------------------------------------------- func setWriteOffset(writeIndex: int) -> void: if (writeIndex > buffer.get_size()): @@ -17,6 +42,7 @@ func setWriteOffset(writeIndex: int) -> void: printerr(template.format([writeIndex, readOffset, writeOffset, buffer.get_size()], "{}")) return writeOffset = writeIndex + pass func getWriteOffset() -> int: return writeOffset @@ -27,6 +53,7 @@ func setReadOffset(readIndex: int) -> void: printerr(template.format([readIndex, readOffset, writeOffset, buffer.size()], "{}")) return readOffset = readIndex + pass func getReadOffset() -> int: return readOffset @@ -39,6 +66,7 @@ func writePackedByteArray(value: PackedByteArray): var length: int = value.size() buffer.put_partial_data(value) writeOffset += length + pass func toPackedByteArray() -> PackedByteArray: return buffer.data_array.slice(0, writeOffset) @@ -48,6 +76,7 @@ func writeBool(value: bool) -> void: buffer.seek(writeOffset) buffer.put_8(byte) writeOffset += 1 + pass func readBool() -> bool: buffer.seek(readOffset) @@ -59,6 +88,7 @@ func writeByte(value: int) -> void: buffer.seek(writeOffset) buffer.put_8(value) writeOffset += 1 + pass func readByte() -> int: buffer.seek(readOffset) @@ -70,6 +100,7 @@ func writeShort(value: int) -> void: buffer.seek(writeOffset) buffer.put_16(value) writeOffset += 2 + pass func readShort() -> int: buffer.seek(readOffset) @@ -81,6 +112,7 @@ func writeRawInt(value) -> void: buffer.seek(writeOffset) buffer.put_32(value) writeOffset += 4 + pass func readRawInt() -> int: buffer.seek(readOffset) @@ -89,7 +121,25 @@ func readRawInt() -> int: return value func writeInt(value) -> void: + if !(minInt <= value && value <= maxInt): + printerr("value must range between minInt:-2147483648 and maxInt:2147483647") + return writeLong(value) + pass + +func writeIntCount(value: int) -> int: + if !(minInt <= value && value <= maxInt): + printerr("value must range between minInt:-2147483648 and maxInt:2147483647") + return 0 + if value >> 7 == 0: + return 1 + if value >> 14 == 0: + return 2 + if value >> 21 == 0: + return 3 + if value >> 28 == 0: + return 4 + return 5 func readInt() -> int: return readLong() @@ -100,25 +150,21 @@ func writeLong(longValue: int) -> void: 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) @@ -126,7 +172,6 @@ func writeLong(longValue: int) -> void: writeByte((value >> 21) | 0x80) writeByte(value >> 28) return - if (value >> 42 == 0): writeByte(value | 0x80) writeByte((value >> 7) | 0x80) @@ -135,7 +180,6 @@ func writeLong(longValue: int) -> void: writeByte((value >> 28) | 0x80) writeByte(value >> 35) return - if (value >> 49 == 0): writeByte(value | 0x80) writeByte((value >> 7) | 0x80) @@ -145,7 +189,6 @@ func writeLong(longValue: int) -> void: writeByte((value >> 35) | 0x80) writeByte(value >> 42) return - if (value >> 56 == 0): writeByte(value | 0x80) writeByte((value >> 7) | 0x80) @@ -156,7 +199,6 @@ func writeLong(longValue: int) -> void: writeByte((value >> 42) | 0x80) writeByte(value >> 49) return - writeByte(value | 0x80) writeByte((value >> 7) | 0x80) writeByte((value >> 14) | 0x80) @@ -166,6 +208,7 @@ func writeLong(longValue: int) -> void: writeByte((value >> 42) | 0x80) writeByte((value >> 49) | 0x80) writeByte(value >> 56) + pass func readLong() -> int: var byte: int = readByte() @@ -194,7 +237,6 @@ func readLong() -> int: if (byte < 0): byte = readByte() value = value & 0x00FFFFFF_FFFFFFFF | byte << 56 - var mask = value >> 1 if (mask < 0): mask = mask & 0x7FFFFFFF_FFFFFFFF @@ -205,6 +247,7 @@ func writeFloat(value: float) -> void: buffer.seek(writeOffset) buffer.put_float(value) writeOffset += 4 + pass func readFloat() -> float: buffer.seek(readOffset) @@ -216,6 +259,7 @@ func writeDouble(value: float) -> void: buffer.seek(writeOffset) buffer.put_double(value) writeOffset += 8 + pass func readDouble() -> float: buffer.seek(readOffset) @@ -228,7 +272,6 @@ func writeString(value: String) -> void: if (value == null || value.length() ==0): writeInt(0) return - buffer.seek(writeOffset) var strBytes: PackedByteArray = value.to_utf8_buffer() @@ -236,35 +279,22 @@ func writeString(value: String) -> void: writeInt(length) buffer.put_partial_data(strBytes) writeOffset += length + pass func readString() -> String: var length: int = readInt() if (length <= 0): return EMPTY - buffer.seek(readOffset) var value: String = buffer.get_utf8_string(length) var strBytes: PackedByteArray = value.to_utf8_buffer() 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() - -func writePacketFlag(packet) -> bool: - var flag = (packet == null) - writeBool(!flag) - return flag - func writePacket(packet, protocolId): var protocolRegistration = ProtocolManager.getProtocol(protocolId) protocolRegistration.write(self, packet) + pass func readPacket(protocolId): var protocolRegistration = ProtocolManager.getProtocol(protocolId) @@ -280,7 +310,8 @@ func writeBooleanArray(array): writeInt(array.size()) for element in array: writeBool(element) - + pass + func readBooleanArray() -> Array[bool]: var array: Array[bool] = [] var size = readInt() @@ -288,7 +319,7 @@ func readBooleanArray() -> Array[bool]: for index in range(size): array.append(readBool()) return array - + func writeByteArray(array): if (array == null): writeInt(0) @@ -296,7 +327,8 @@ func writeByteArray(array): writeInt(array.size()) for element in array: writeByte(element) - + pass + func readByteArray() -> Array[int]: var array: Array[int] = [] var size = readInt() @@ -304,7 +336,7 @@ func readByteArray() -> Array[int]: for index in range(size): array.append(readByte()) return array - + func writeShortArray(array): if (array == null): writeInt(0) @@ -312,7 +344,8 @@ func writeShortArray(array): writeInt(array.size()) for element in array: writeShort(element) - + pass + func readShortArray(): var array: Array[int] = [] var size = readInt() @@ -320,7 +353,7 @@ func readShortArray(): for index in range(size): array.append(readShort()) return array - + func writeIntArray(array): if (array == null): writeInt(0) @@ -328,7 +361,8 @@ func writeIntArray(array): writeInt(array.size()) for element in array: writeInt(element) - + pass + func readIntArray(): var array: Array[int] = [] var size = readInt() @@ -336,7 +370,7 @@ func readIntArray(): for index in range(size): array.append(readInt()) return array - + func writeLongArray(array): if (array == null): writeInt(0) @@ -344,7 +378,8 @@ func writeLongArray(array): writeInt(array.size()) for element in array: writeLong(element) - + pass + func readLongArray(): var array: Array[int] = [] var size = readInt() @@ -352,7 +387,7 @@ func readLongArray(): for index in range(size): array.append(readLong()) return array - + func writeFloatArray(array): if (array == null): writeInt(0) @@ -360,7 +395,8 @@ func writeFloatArray(array): writeInt(array.size()) for element in array: writeFloat(element) - + pass + func readFloatArray(): var array: Array[float] = [] var size = readInt() @@ -368,7 +404,7 @@ func readFloatArray(): for index in range(size): array.append(readFloat()) return array - + func writeDoubleArray(array): if (array == null): writeInt(0) @@ -376,7 +412,8 @@ func writeDoubleArray(array): writeInt(array.size()) for element in array: writeDouble(element) - + pass + func readDoubleArray(): var array: Array[float] = [] var size = readInt() @@ -384,23 +421,7 @@ func readDoubleArray(): for index in range(size): array.append(readDouble()) return array - -func writeCharArray(array): - if (array == null): - writeInt(0) - else: - writeInt(array.size()) - for element in array: - writeChar(element) - -func readCharArray(): - var array: Array[String] = [] - var size = readInt() - if (size > 0): - for index in range(size): - array.append(readChar()) - return array - + func writeStringArray(array): if (array == null): writeInt(0) @@ -408,7 +429,8 @@ func writeStringArray(array): writeInt(array.size()) for element in array: writeString(element) - + pass + func readStringArray(): var array: Array[String] = [] var size = readInt() @@ -417,7 +439,6 @@ func readStringArray(): array.append(readString()) return array - func writePacketArray(array, protocolId): if (array == null): writeInt(0) @@ -426,7 +447,8 @@ func writePacketArray(array, protocolId): writeInt(array.size()) for element in array: protocolRegistration.write(self, element) - + pass + func readPacketArray(protocolId): var protocolRegistration = ProtocolManager.getProtocol(protocolId) var array = Array([], typeof(protocolRegistration), StringName("RefCounted"), protocolRegistration) @@ -452,7 +474,8 @@ func writeIntIntMap(map): for key in map: writeInt(key) writeInt(map[key]) - + pass + func readIntIntMap(): var map = {} var size = readInt() @@ -462,7 +485,7 @@ func readIntIntMap(): var value = readInt() map[key] = value return map - + func writeIntLongMap(map): if (map == null): writeInt(0) @@ -471,7 +494,8 @@ func writeIntLongMap(map): for key in map: writeInt(key) writeLong(map[key]) - + pass + func readIntLongMap(): var map = {} var size = readInt() @@ -481,7 +505,7 @@ func readIntLongMap(): var value = readLong() map[key] = value return map - + func writeIntStringMap(map): if (map == null): writeInt(0) @@ -490,7 +514,8 @@ func writeIntStringMap(map): for key in map: writeInt(key) writeString(map[key]) - + pass + func readIntStringMap(): var map = {} var size = readInt() @@ -501,7 +526,6 @@ func readIntStringMap(): map[key] = value return map - func writeIntPacketMap(map, protocolId): if (map == null): writeInt(0) @@ -511,6 +535,7 @@ func writeIntPacketMap(map, protocolId): for key in map: writeInt(key) protocolRegistration.write(self, map[key]) + pass func readIntPacketMap(protocolId): var map = {} @@ -522,8 +547,7 @@ func readIntPacketMap(protocolId): var value = protocolRegistration.read(self) map[key] = value return map - - + func writeLongIntMap(map): if (map == null): writeInt(0) @@ -532,7 +556,8 @@ func writeLongIntMap(map): for key in map: writeLong(key) writeInt(map[key]) - + pass + func readLongIntMap(): var map = {} var size = readInt() @@ -542,7 +567,7 @@ func readLongIntMap(): var value = readInt() map[key] = value return map - + func writeLongLongMap(map): if (map == null): writeInt(0) @@ -551,7 +576,8 @@ func writeLongLongMap(map): for key in map: writeLong(key) writeLong(map[key]) - + pass + func readLongLongMap(): var map = {} var size = readInt() @@ -561,7 +587,7 @@ func readLongLongMap(): var value = readLong() map[key] = value return map - + func writeLongStringMap(map): if (map == null): writeInt(0) @@ -570,7 +596,8 @@ func writeLongStringMap(map): for key in map: writeLong(key) writeString(map[key]) - + pass + func readLongStringMap(): var map = {} var size = readInt() @@ -581,7 +608,6 @@ func readLongStringMap(): map[key] = value return map - func writeLongPacketMap(map, protocolId): if (map == null): writeInt(0) @@ -591,6 +617,7 @@ func writeLongPacketMap(map, protocolId): for key in map: writeLong(key) protocolRegistration.write(self, map[key]) + pass func readLongPacketMap(protocolId): var map = {} @@ -602,7 +629,6 @@ func readLongPacketMap(protocolId): var value = protocolRegistration.read(self) map[key] = value return map - func writeStringIntMap(map): if (map == null): @@ -612,7 +638,8 @@ func writeStringIntMap(map): for key in map: writeString(key) writeInt(map[key]) - + pass + func readStringIntMap(): var map = {} var size = readInt() @@ -622,7 +649,7 @@ func readStringIntMap(): var value = readInt() map[key] = value return map - + func writeStringLongMap(map): if (map == null): writeInt(0) @@ -631,7 +658,8 @@ func writeStringLongMap(map): for key in map: writeString(key) writeLong(map[key]) - + pass + func readStringLongMap(): var map = {} var size = readInt() @@ -641,7 +669,7 @@ func readStringLongMap(): var value = readLong() map[key] = value return map - + func writeStringStringMap(map): if (map == null): writeInt(0) @@ -650,7 +678,8 @@ func writeStringStringMap(map): for key in map: writeString(key) writeString(map[key]) - + pass + func readStringStringMap(): var map = {} var size = readInt() @@ -661,7 +690,6 @@ func readStringStringMap(): map[key] = value return map - func writeStringPacketMap(map, protocolId): if (map == null): writeInt(0) @@ -671,6 +699,7 @@ func writeStringPacketMap(map, protocolId): for key in map: writeString(key) protocolRegistration.write(self, map[key]) + pass func readStringPacketMap(protocolId): var map = {}