From c9299ae99a25fcc061ff17bd5df6c63471d06ad0 Mon Sep 17 00:00:00 2001 From: godotg Date: Mon, 27 May 2024 17:47:44 +0800 Subject: [PATCH] ref[EcmaScript]: refactor generate EcmaScript protocol --- .../ecmascript/zfooes/ProtocolManager.mjs | 4 +- .../ecmascript/zfooes/buffer/ByteBuffer.mjs | 1121 +++++++++++++++++ .../zfooes/packet/ComplexObject.mjs | 365 +++--- .../ecmascript/zfooes/packet/EmptyObject.mjs | 3 +- .../ecmascript/zfooes/packet/NormalObject.mjs | 3 +- .../test/ecmascript/zfooes/packet/ObjectA.mjs | 3 +- .../test/ecmascript/zfooes/packet/ObjectB.mjs | 3 +- .../ecmascript/zfooes/packet/SimpleObject.mjs | 3 +- .../zfooes/packet/VeryBigObject.mjs | 3 +- 9 files changed, 1311 insertions(+), 197 deletions(-) create mode 100644 protocol/src/test/ecmascript/zfooes/buffer/ByteBuffer.mjs diff --git a/protocol/src/test/ecmascript/zfooes/ProtocolManager.mjs b/protocol/src/test/ecmascript/zfooes/ProtocolManager.mjs index 54737d430..1a6af9c18 100644 --- a/protocol/src/test/ecmascript/zfooes/ProtocolManager.mjs +++ b/protocol/src/test/ecmascript/zfooes/ProtocolManager.mjs @@ -21,7 +21,7 @@ class ProtocolManager { static getProtocol(protocolId) { const protocol = protocols.get(protocolId); if (protocol === null) { - throw new Error('[protocolId:' + protocolId + ']协议不存在'); + throw new Error('[protocolId:' + protocolId + '] not exist'); } return protocol; } @@ -42,4 +42,4 @@ class ProtocolManager { } -export default ProtocolManager; +export default ProtocolManager; \ No newline at end of file diff --git a/protocol/src/test/ecmascript/zfooes/buffer/ByteBuffer.mjs b/protocol/src/test/ecmascript/zfooes/buffer/ByteBuffer.mjs new file mode 100644 index 000000000..127ac5bc6 --- /dev/null +++ b/protocol/src/test/ecmascript/zfooes/buffer/ByteBuffer.mjs @@ -0,0 +1,1121 @@ +import {readInt64, writeInt64} from './longbits.mjs'; +import ProtocolManager from '../ProtocolManager.mjs'; + +const empty_str = ''; +const initSize = 128; +const maxSize = 655537; + +const maxShort = 32767; +const minShort = -32768; + +const maxInt = 2147483647; +const minInt = -2147483648; + +// UTF-8编码与解码 +const encoder = new TextEncoder('utf-8'); +const decoder = new TextDecoder('utf-8'); + +// nodejs的测试环境需要用以下方式特殊处理 +// const util = require('util'); +// const encoder = new util.TextEncoder('utf-8'); +// const decoder = new util.TextDecoder('utf-8'); + +// 现在所有主流浏览器都支持TextDecoder,只有微信小程序不支持TextDecoder(微信浏览器也支持,微信的小程序和浏览器不是同一个js环境) +// https://developers.weixin.qq.com/community/develop/doc/000ca85023ce78c8484e0d1d256400 +// 如果在微信小程序中使用,需要按照上面的链接全局引入TextEncoder相关依赖 + +// 在js中long可以支持的最大值 +// const maxLong = 9007199254740992; +// const minLong = -9007199254740992; + +const copy = function copy(original, newLength) { + if (original.byteLength > newLength) { + throw new Error('newLength is too small'); + } + const dst = new ArrayBuffer(newLength); + new Uint8Array(dst).set(new Uint8Array(original)); + return dst; +}; + +function encodeZigzagInt(n) { + // 有效位左移一位+符号位右移31位 + return (n << 1) ^ (n >> 31); +} + +function decodeZigzagInt(n) { + return (n >>> 1) ^ -(n & 1); +} + + +class ByteBuffer { + writeOffset = 0; + readOffset = 0; + buffer = new ArrayBuffer(initSize); + bufferView = new DataView(this.buffer, 0, this.buffer.byteLength); + + adjustPadding(predictionLength, beforeWriteIndex) { + const currentWriteIndex = this.writeOffset; + const predictionCount = this.writeIntCount(predictionLength); + const length = currentWriteIndex - beforeWriteIndex - predictionCount; + const lengthCount = this.writeIntCount(length); + const padding = lengthCount - predictionCount; + if (padding === 0) { + this.setWriteOffset(beforeWriteIndex); + this.writeInt(length); + this.setWriteOffset(currentWriteIndex); + } else { + const retainedByteBuf = this.buffer.slice(currentWriteIndex - length, currentWriteIndex); + this.setWriteOffset(beforeWriteIndex); + this.writeInt(length); + this.writeBytes(retainedByteBuf); + } + } + + compatibleRead(beforeReadIndex, length) { + return length !== -1 && this.getReadOffset() < length + beforeReadIndex; + } + + getWriteOffset() { + return this.writeOffset; + } + + setWriteOffset(writeOffset) { + if (writeOffset > this.buffer.byteLength) { + throw new Error('index out of bounds exception: readerIndex: ' + this.readOffset + + ', writerIndex: ' + this.writeOffset + + '(expected: 0 <= readerIndex <= writerIndex <= capacity:' + this.buffer.byteLength); + } + this.writeOffset = writeOffset; + } + + getReadOffset() { + return this.readOffset; + } + + setReadOffset(readOffset) { + if (readOffset > this.writeOffset) { + throw new Error('index out of bounds exception: readerIndex: ' + this.readOffset + + ', writerIndex: ' + this.writeOffset + + '(expected: 0 <= readerIndex <= writerIndex <= capacity:' + this.buffer.byteLength); + } + this.readOffset = readOffset; + } + + getCapacity() { + return this.buffer.byteLength - this.writeOffset; + } + + ensureCapacity(minCapacity) { + while (minCapacity - this.getCapacity() > 0) { + const newSize = this.buffer.byteLength * 2; + if (newSize > maxSize) { + throw new Error('out of memory error'); + } + this.buffer = copy(this.buffer, newSize); + this.bufferView = new DataView(this.buffer, 0, this.buffer.byteLength); + } + } + + isReadable() { + return this.writeOffset > this.readOffset; + } + + writeBoolean(value) { + if (!(value === true || value === false)) { + throw new Error('value must be true of false'); + } + this.ensureCapacity(1); + if (value === true) { + this.bufferView.setInt8(this.writeOffset, 1); + } else { + this.bufferView.setInt8(this.writeOffset, 0); + } + this.writeOffset++; + } + + readBoolean() { + const value = this.bufferView.getInt8(this.readOffset); + this.readOffset++; + return (value === 1); + } + + writeBytes(byteArray) { + const length = byteArray.byteLength; + this.ensureCapacity(length); + new Uint8Array(this.buffer).set(new Uint8Array(byteArray), this.writeOffset); + this.writeOffset += length; + } + + toBytes() { + const result = new ArrayBuffer(this.writeOffset); + new Uint8Array(result).set(new Uint8Array(this.buffer.slice(0, this.writeOffset))); + return result; + } + + writeByte(value) { + this.ensureCapacity(1); + this.bufferView.setInt8(this.writeOffset, value); + this.writeOffset++; + } + + readByte() { + const value = this.bufferView.getInt8(this.readOffset); + this.readOffset++; + return value; + } + + writeShort(value) { + if (!(minShort <= value && value <= maxShort)) { + throw new Error('value must range between minShort:-32768 and maxShort:32767'); + } + this.ensureCapacity(2); + this.bufferView.setInt16(this.writeOffset, value); + this.writeOffset += 2; + } + + readShort() { + const value = this.bufferView.getInt16(this.readOffset); + this.readOffset += 2; + return value; + } + + writeRawInt(value) { + if (!(minInt <= value && value <= maxInt)) { + throw new Error('value must range between minInt:-2147483648 and maxInt:2147483647'); + } + this.ensureCapacity(4); + this.bufferView.setInt32(this.writeOffset, value); + this.writeOffset += 4; + } + + readRawInt() { + const value = this.bufferView.getInt32(this.readOffset); + this.readOffset += 4; + return value; + } + + writeInt(value) { + if (!(minInt <= value && value <= maxInt)) { + throw new Error('value must range between minInt:-2147483648 and maxInt:2147483647'); + } + this.ensureCapacity(5); + + value = encodeZigzagInt(value); + + if (value >>> 7 === 0) { + this.writeByte(value); + return; + } + + if (value >>> 14 === 0) { + this.writeByte((value & 0x7F) | 0x80); + this.writeByte((value >>> 7)); + return; + } + + if (value >>> 21 === 0) { + this.writeByte((value & 0x7F) | 0x80); + this.writeByte((value >>> 7 | 0x80)); + this.writeByte(value >>> 14); + return; + } + + if (value >>> 28 === 0) { + this.writeByte((value & 0x7F) | 0x80); + this.writeByte((value >>> 7 | 0x80)); + this.writeByte((value >>> 14 | 0x80)); + this.writeByte(value >>> 21); + return; + } + + this.writeByte((value & 0x7F) | 0x80); + this.writeByte((value >>> 7 | 0x80)); + this.writeByte((value >>> 14 | 0x80)); + this.writeByte((value >>> 21 | 0x80)); + this.writeByte(value >>> 28); + } + + writeIntCount(value) { + if (!(minInt <= value && value <= maxInt)) { + throw new Error('value must range between minInt:-2147483648 and maxInt:2147483647'); + } + value = encodeZigzagInt(value); + 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; + } + + readInt() { + let b = this.readByte(); + let value = b & 0x7F; + if ((b & 0x80) !== 0) { + b = this.readByte(); + value |= (b & 0x7F) << 7; + if ((b & 0x80) !== 0) { + b = this.readByte(); + value |= (b & 0x7F) << 14; + if ((b & 0x80) !== 0) { + b = this.readByte(); + value |= (b & 0x7F) << 21; + if ((b & 0x80) !== 0) { + b = this.readByte(); + value |= (b & 0x7F) << 28; + } + } + } + } + + return decodeZigzagInt(value); + } + + writeLong(value) { + if (value === null || value === undefined) { + throw new Error('value must not be null'); + } + this.ensureCapacity(9); + + writeInt64(this, value); + } + + readLong() { + const buffer = new ArrayBuffer(9); + const bufferView = new DataView(buffer, 0, buffer.byteLength); + + let count = 0; + let b = this.readByte(); + bufferView.setUint8(count++, b); + if ((b & 0x80) !== 0) { + b = this.readByte(); + bufferView.setUint8(count++, b); + if ((b & 0x80) !== 0) { + b = this.readByte(); + bufferView.setUint8(count++, b); + if ((b & 0x80) !== 0) { + b = this.readByte(); + bufferView.setUint8(count++, b); + if ((b & 0x80) !== 0) { + b = this.readByte(); + bufferView.setUint8(count++, b); + if ((b & 0x80) !== 0) { + b = this.readByte(); + bufferView.setUint8(count++, b); + if ((b & 0x80) !== 0) { + b = this.readByte(); + bufferView.setUint8(count++, b); + if ((b & 0x80) !== 0) { + b = this.readByte(); + bufferView.setUint8(count++, b); + if ((b & 0x80) !== 0) { + b = this.readByte(); + bufferView.setUint8(count++, b); + } + } + } + } + } + } + } + } + return readInt64(new Uint8Array(buffer.slice(0, count))).toNumber(); + } + + writeFloat(value) { + if (value === null || value === undefined) { + throw new Error('value must not be null'); + } + this.ensureCapacity(4); + this.bufferView.setFloat32(this.writeOffset, value); + this.writeOffset += 4; + } + + readFloat() { + const value = this.bufferView.getFloat32(this.readOffset); + this.readOffset += 4; + return value; + } + + writeDouble(value) { + if (value === null || value === undefined) { + throw new Error('value must not be null'); + } + this.ensureCapacity(8); + this.bufferView.setFloat64(this.writeOffset, value); + this.writeOffset += 8; + } + + readDouble() { + const value = this.bufferView.getFloat64(this.readOffset); + this.readOffset += 8; + return value; + } + + writeString(value) { + if (value === null || value === undefined || value.trim().length === 0) { + this.writeInt(0); + return; + } + + const uint8Array = encoder.encode(value); + + this.ensureCapacity(5 + uint8Array.length); + + this.writeInt(uint8Array.length); + uint8Array.forEach((value) => this.writeByte(value)); + } + + readString() { + const length = this.readInt(); + if (length <= 0) { + return empty_str; + } + const uint8Array = new Uint8Array(this.buffer.slice(this.readOffset, this.readOffset + length)); + const value = decoder.decode(uint8Array); + this.readOffset += length; + return value; + } + + writePacketFlag(value) { + const flag = (value === null) || (value === undefined); + this.writeBoolean(!flag); + return flag; + } + + writePacket(packet, protocolId) { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + protocolRegistration.write(this, packet); + } + + readPacket(protocolId) { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + return protocolRegistration.read(this); + } + + writeBooleanArray(array) { + if (array === null) { + this.writeInt(0); + } else { + this.writeInt(array.length); + array.forEach(element => { + this.writeBoolean(element); + }); + } + } + + readBooleanArray() { + const array = []; + const length = this.readInt(); + if (length > 0) { + for (let index = 0; index < length; index++) { + array.push(this.readBoolean()); + } + } + return array; + } + + writeByteArray(array) { + if (array === null) { + this.writeInt(0); + } else { + this.writeInt(array.length); + array.forEach(element => { + this.writeByte(element); + }); + } + } + + readByteArray() { + const array = []; + const length = this.readInt(); + if (length > 0) { + for (let index = 0; index < length; index++) { + array.push(this.readByte()); + } + } + return array; + } + + writeShortArray(array) { + if (array === null) { + this.writeInt(0); + } else { + this.writeInt(array.length); + array.forEach(element => { + this.writeShort(element); + }); + } + } + + readShortArray() { + const array = []; + const length = this.readInt(); + if (length > 0) { + for (let index = 0; index < length; index++) { + array.push(this.readShort()); + } + } + return array; + } + + writeIntArray(array) { + if (array === null) { + this.writeInt(0); + } else { + this.writeInt(array.length); + array.forEach(element => { + this.writeInt(element); + }); + } + } + + readIntArray() { + const array = []; + const length = this.readInt(); + if (length > 0) { + for (let index = 0; index < length; index++) { + array.push(this.readInt()); + } + } + return array; + } + + writeLongArray(array) { + if (array === null) { + this.writeInt(0); + } else { + this.writeInt(array.length); + array.forEach(element => { + this.writeLong(element); + }); + } + } + + readLongArray() { + const array = []; + const length = this.readInt(); + if (length > 0) { + for (let index = 0; index < length; index++) { + array.push(this.readLong()); + } + } + return array; + } + + writeFloatArray(array) { + if (array === null) { + this.writeInt(0); + } else { + this.writeInt(array.length); + array.forEach(element => { + this.writeFloat(element); + }); + } + } + + readFloatArray() { + const array = []; + const length = this.readInt(); + if (length > 0) { + for (let index = 0; index < length; index++) { + array.push(this.readFloat()); + } + } + return array; + } + + writeDoubleArray(array) { + if (array === null) { + this.writeInt(0); + } else { + this.writeInt(array.length); + array.forEach(element => { + this.writeDouble(element); + }); + } + } + + readDoubleArray() { + const array = []; + const length = this.readInt(); + if (length > 0) { + for (let index = 0; index < length; index++) { + array.push(this.readDouble()); + } + } + return array; + } + + writeStringArray(array) { + if (array === null) { + this.writeInt(0); + } else { + this.writeInt(array.length); + array.forEach(element => { + this.writeString(element); + }); + } + } + + readStringArray() { + const array = []; + const length = this.readInt(); + if (length > 0) { + for (let index = 0; index < length; index++) { + array.push(this.readString()); + } + } + return array; + } + + writePacketArray(array, protocolId) { + if (array === null) { + this.writeInt(0); + } else { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + this.writeInt(array.length); + array.forEach(element => { + protocolRegistration.write(this, element); + }); + } + } + + readPacketArray(protocolId) { + const array = []; + const length = this.readInt(); + if (length > 0) { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + for (let index = 0; index < length; index++) { + array.push(protocolRegistration.read(this)); + } + } + return array; + } + + // ---------------------------------------------list------------------------------------------- + writeBooleanList(list) { + this.writeBooleanArray(list); + } + + readBooleanList() { + return this.readBooleanArray(); + } + + writeByteList(list) { + this.writeByteArray(list); + } + + readByteList() { + return this.readByteArray(); + } + + writeShortList(list) { + this.writeShortArray(list); + } + + readShortList() { + return this.readShortArray(); + } + + writeIntList(list) { + this.writeIntArray(list); + } + + readIntList() { + return this.readIntArray(); + } + + writeLongList(list) { + this.writeLongArray(list); + } + + readLongList() { + return this.readLongArray(); + } + + writeFloatList(list) { + this.writeFloatArray(list); + } + + readFloatList() { + return this.readFloatArray(); + } + + writeDoubleList(list) { + this.writeDoubleArray(list); + } + + readDoubleList() { + return this.readDoubleArray(); + } + + writeStringList(list) { + this.writeStringArray(list); + } + + readStringList() { + return this.readStringArray(); + } + + writePacketList(list, protocolId) { + this.writePacketArray(list, protocolId); + } + + readPacketList(protocolId) { + return this.readPacketArray(protocolId); + } + + // ---------------------------------------------set------------------------------------------- + writeBooleanSet(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeBoolean(element); + }); + } + } + + readBooleanSet() { + return new Set(this.readBooleanArray()); + } + + writeByteSet(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeByte(element); + }); + } + } + + readByteSet() { + return new Set(this.readByteArray()); + } + + writeShortSet(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeShort(element); + }); + } + } + + readShortSet() { + return new Set(this.readShortArray()); + } + + writeIntSet(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeInt(element); + }); + } + } + + readIntSet() { + return new Set(this.readIntArray()); + } + + writeLongSet(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeLong(element); + }); + } + } + + readLongSet() { + return new Set(this.readLongArray()); + } + + writeFloatSet(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeFloat(element); + }); + } + } + + readFloatSet() { + return new Set(this.readFloatArray()); + } + + writeDoubleSet(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeDouble(element); + }); + } + } + + readDoubleSet() { + return new Set(this.readDoubleArray()); + } + + writeStringSet(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeString(element); + }); + } + } + + readStringSet() { + return new Set(this.readStringArray()); + } + + writePacketSet(set, protocolId) { + if (set === null) { + this.writeInt(0); + } else { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + this.writeInt(set.size); + set.forEach(element => { + protocolRegistration.write(this, element); + }); + } + } + + readPacketSet(protocolId) { + return new Set(this.readPacketArray(protocolId)); + } + + // ---------------------------------------------map------------------------------------------- + writeIntIntMap(map) { + if (map === null) { + this.writeInt(0); + } else { + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeInt(key); + this.writeInt(value); + }); + } + } + + readIntIntMap() { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + for (let index = 0; index < size; index++) { + const key = this.readInt(); + const value = this.readInt(); + map.set(key, value); + } + } + return map; + } + + writeIntLongMap(map) { + if (map === null) { + this.writeInt(0); + } else { + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeInt(key); + this.writeLong(value); + }); + } + } + + readIntLongMap() { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + for (let index = 0; index < size; index++) { + const key = this.readInt(); + const value = this.readLong(); + map.set(key, value); + } + } + return map; + } + + writeIntStringMap(map) { + if (map === null) { + this.writeInt(0); + } else { + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeInt(key); + this.writeString(value); + }); + } + } + + readIntStringMap() { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + for (let index = 0; index < size; index++) { + const key = this.readInt(); + const value = this.readString(); + map.set(key, value); + } + } + return map; + } + + writeIntPacketMap(map, protocolId) { + if (map === null) { + this.writeInt(0); + } else { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeInt(key); + protocolRegistration.write(this, value); + }); + } + } + + readIntPacketMap(protocolId) { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + for (let index = 0; index < size; index++) { + const key = this.readInt(); + const value = protocolRegistration.read(this); + map.set(key, value); + } + } + return map; + } + + writeLongIntMap(map) { + if (map === null) { + this.writeInt(0); + } else { + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeLong(key); + this.writeInt(value); + }); + } + } + + readLongIntMap() { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + for (let index = 0; index < size; index++) { + const key = this.readLong(); + const value = this.readInt(); + map.set(key, value); + } + } + return map; + } + + writeLongLongMap(map) { + if (map === null) { + this.writeInt(0); + } else { + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeLong(key); + this.writeLong(value); + }); + } + } + + readLongLongMap() { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + for (let index = 0; index < size; index++) { + const key = this.readLong(); + const value = this.readLong(); + map.set(key, value); + } + } + return map; + } + + writeLongStringMap(map) { + if (map === null) { + this.writeInt(0); + } else { + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeLong(key); + this.writeString(value); + }); + } + } + + readLongStringMap() { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + for (let index = 0; index < size; index++) { + const key = this.readLong(); + const value = this.readString(); + map.set(key, value); + } + } + return map; + } + + writeLongPacketMap(map, protocolId) { + if (map === null) { + this.writeInt(0); + } else { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeLong(key); + protocolRegistration.write(this, value); + }); + } + } + + readLongPacketMap(protocolId) { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + for (let index = 0; index < size; index++) { + const key = this.readLong(); + const value = protocolRegistration.read(this); + map.set(key, value); + } + } + return map; + } + + writeStringIntMap(map) { + if (map === null) { + this.writeInt(0); + } else { + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeString(key); + this.writeInt(value); + }); + } + } + + readStringIntMap() { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + for (let index = 0; index < size; index++) { + const key = this.readString(); + const value = this.readInt(); + map.set(key, value); + } + } + return map; + } + + writeStringLongMap(map) { + if (map === null) { + this.writeInt(0); + } else { + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeString(key); + this.writeLong(value); + }); + } + } + + readStringLongMap() { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + for (let index = 0; index < size; index++) { + const key = this.readString(); + const value = this.readLong(); + map.set(key, value); + } + } + return map; + } + + writeStringStringMap(map) { + if (map === null) { + this.writeInt(0); + } else { + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeString(key); + this.writeString(value); + }); + } + } + + readStringStringMap() { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + for (let index = 0; index < size; index++) { + const key = this.readString(); + const value = this.readString(); + map.set(key, value); + } + } + return map; + } + + writeStringPacketMap(map, protocolId) { + if (map === null) { + this.writeInt(0); + } else { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeString(key); + protocolRegistration.write(this, value); + }); + } + } + + readStringPacketMap(protocolId) { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + for (let index = 0; index < size; index++) { + const key = this.readString(); + const value = protocolRegistration.read(this); + map.set(key, value); + } + } + return map; + } +} + +export default ByteBuffer; diff --git a/protocol/src/test/ecmascript/zfooes/packet/ComplexObject.mjs b/protocol/src/test/ecmascript/zfooes/packet/ComplexObject.mjs index 1bcb0412e..b2a36c950 100644 --- a/protocol/src/test/ecmascript/zfooes/packet/ComplexObject.mjs +++ b/protocol/src/test/ecmascript/zfooes/packet/ComplexObject.mjs @@ -241,228 +241,227 @@ class ComplexObject { } const beforeReadIndex = buffer.getReadOffset(); const packet = new ComplexObject(); - const result19 = buffer.readByte(); - packet.a = result19; - const result20 = buffer.readByte(); - packet.aa = result20; - const array21 = buffer.readByteArray(); - packet.aaa = array21; - const array22 = buffer.readByteArray(); - packet.aaaa = array22; - const result23 = buffer.readShort(); - packet.b = result23; - const result24 = buffer.readShort(); - packet.bb = result24; - const array25 = buffer.readShortArray(); - packet.bbb = array25; - const array26 = buffer.readShortArray(); - packet.bbbb = array26; - const result27 = buffer.readInt(); - packet.c = result27; - const result28 = buffer.readInt(); - packet.cc = result28; - const array29 = buffer.readIntArray(); - packet.ccc = array29; - const array30 = buffer.readIntArray(); - packet.cccc = array30; - const result31 = buffer.readLong(); - packet.d = result31; - const result32 = buffer.readLong(); - packet.dd = result32; - const array33 = buffer.readLongArray(); - packet.ddd = array33; - const array34 = buffer.readLongArray(); - packet.dddd = array34; - const result35 = buffer.readFloat(); - packet.e = result35; - const result36 = buffer.readFloat(); - packet.ee = result36; - const array37 = buffer.readFloatArray(); - packet.eee = array37; - const array38 = buffer.readFloatArray(); - packet.eeee = array38; - const result39 = buffer.readDouble(); - packet.f = result39; - const result40 = buffer.readDouble(); - packet.ff = result40; - const array41 = buffer.readDoubleArray(); - packet.fff = array41; - const array42 = buffer.readDoubleArray(); - packet.ffff = array42; - const result43 = buffer.readBoolean(); - packet.g = result43; - const result44 = buffer.readBoolean(); - packet.gg = result44; - const array45 = buffer.readBooleanArray(); - packet.ggg = array45; - const array46 = buffer.readBooleanArray(); - packet.gggg = array46; - const result47 = buffer.readString(); - packet.jj = result47; - const array48 = buffer.readStringArray(); - packet.jjj = array48; - const result49 = buffer.readPacket(102); - packet.kk = result49; - const array50 = buffer.readPacketArray(102); - packet.kkk = array50; - const list51 = buffer.readIntList(); - packet.l = list51; - const result52 = []; - const size53 = buffer.readInt(); - if (size53 > 0) { - for (let index54 = 0; index54 < size53; index54++) { - const result55 = []; - const size56 = buffer.readInt(); - if (size56 > 0) { - for (let index57 = 0; index57 < size56; index57++) { - const list58 = buffer.readIntList(); - result55.push(list58); + const result0 = buffer.readByte(); + packet.a = result0; + const result1 = buffer.readByte(); + packet.aa = result1; + const array2 = buffer.readByteArray(); + packet.aaa = array2; + const array3 = buffer.readByteArray(); + packet.aaaa = array3; + const result4 = buffer.readShort(); + packet.b = result4; + const result5 = buffer.readShort(); + packet.bb = result5; + const array6 = buffer.readShortArray(); + packet.bbb = array6; + const array7 = buffer.readShortArray(); + packet.bbbb = array7; + const result8 = buffer.readInt(); + packet.c = result8; + const result9 = buffer.readInt(); + packet.cc = result9; + const array10 = buffer.readIntArray(); + packet.ccc = array10; + const array11 = buffer.readIntArray(); + packet.cccc = array11; + const result12 = buffer.readLong(); + packet.d = result12; + const result13 = buffer.readLong(); + packet.dd = result13; + const array14 = buffer.readLongArray(); + packet.ddd = array14; + const array15 = buffer.readLongArray(); + packet.dddd = array15; + const result16 = buffer.readFloat(); + packet.e = result16; + const result17 = buffer.readFloat(); + packet.ee = result17; + const array18 = buffer.readFloatArray(); + packet.eee = array18; + const array19 = buffer.readFloatArray(); + packet.eeee = array19; + const result20 = buffer.readDouble(); + packet.f = result20; + const result21 = buffer.readDouble(); + packet.ff = result21; + const array22 = buffer.readDoubleArray(); + packet.fff = array22; + const array23 = buffer.readDoubleArray(); + packet.ffff = array23; + const result24 = buffer.readBoolean(); + packet.g = result24; + const result25 = buffer.readBoolean(); + packet.gg = result25; + const array26 = buffer.readBooleanArray(); + packet.ggg = array26; + const array27 = buffer.readBooleanArray(); + packet.gggg = array27; + const result28 = buffer.readString(); + packet.jj = result28; + const array29 = buffer.readStringArray(); + packet.jjj = array29; + const result30 = buffer.readPacket(102); + packet.kk = result30; + const array31 = buffer.readPacketArray(102); + packet.kkk = array31; + const list32 = buffer.readIntList(); + packet.l = list32; + const result33 = []; + const size34 = buffer.readInt(); + if (size34 > 0) { + for (let index35 = 0; index35 < size34; index35++) { + const result36 = []; + const size37 = buffer.readInt(); + if (size37 > 0) { + for (let index38 = 0; index38 < size37; index38++) { + const list39 = buffer.readIntList(); + result36.push(list39); } } - result52.push(result55); + result33.push(result36); } } - packet.ll = result52; - const result59 = []; - const size60 = buffer.readInt(); - if (size60 > 0) { - for (let index61 = 0; index61 < size60; index61++) { - const list62 = buffer.readPacketList(102); - result59.push(list62); + packet.ll = result33; + const result40 = []; + const size41 = buffer.readInt(); + if (size41 > 0) { + for (let index42 = 0; index42 < size41; index42++) { + const list43 = buffer.readPacketList(102); + result40.push(list43); } } - packet.lll = result59; - const list63 = buffer.readStringList(); - packet.llll = list63; - const result64 = []; - const size65 = buffer.readInt(); - if (size65 > 0) { - for (let index66 = 0; index66 < size65; index66++) { - const map67 = buffer.readIntStringMap(); - result64.push(map67); + packet.lll = result40; + const list44 = buffer.readStringList(); + packet.llll = list44; + const result45 = []; + const size46 = buffer.readInt(); + if (size46 > 0) { + for (let index47 = 0; index47 < size46; index47++) { + const map48 = buffer.readIntStringMap(); + result45.push(map48); } } - packet.lllll = result64; - const map68 = buffer.readIntStringMap(); - packet.m = map68; - const map69 = buffer.readIntPacketMap(102); - packet.mm = map69; - const result70 = new Map(); - const size71 = buffer.readInt(); - if (size71 > 0) { - for (let index72 = 0; index72 < size71; index72++) { - const result73 = buffer.readPacket(102); - const list74 = buffer.readIntList(); - result70.set(result73, list74); + packet.lllll = result45; + const map49 = buffer.readIntStringMap(); + packet.m = map49; + const map50 = buffer.readIntPacketMap(102); + packet.mm = map50; + const result51 = new Map(); + const size52 = buffer.readInt(); + if (size52 > 0) { + for (let index53 = 0; index53 < size52; index53++) { + const result54 = buffer.readPacket(102); + const list55 = buffer.readIntList(); + result51.set(result54, list55); } } - packet.mmm = result70; - const result75 = new Map(); - const size76 = buffer.readInt(); - if (size76 > 0) { - for (let index77 = 0; index77 < size76; index77++) { - const result78 = []; - const size79 = buffer.readInt(); - if (size79 > 0) { - for (let index80 = 0; index80 < size79; index80++) { - const list81 = buffer.readPacketList(102); - result78.push(list81); + packet.mmm = result51; + const result56 = new Map(); + const size57 = buffer.readInt(); + if (size57 > 0) { + for (let index58 = 0; index58 < size57; index58++) { + const result59 = []; + const size60 = buffer.readInt(); + if (size60 > 0) { + for (let index61 = 0; index61 < size60; index61++) { + const list62 = buffer.readPacketList(102); + result59.push(list62); } } - const result82 = []; - const size83 = buffer.readInt(); - if (size83 > 0) { - for (let index84 = 0; index84 < size83; index84++) { - const result85 = []; - const size86 = buffer.readInt(); - if (size86 > 0) { - for (let index87 = 0; index87 < size86; index87++) { - const list88 = buffer.readIntList(); - result85.push(list88); + const result63 = []; + const size64 = buffer.readInt(); + if (size64 > 0) { + for (let index65 = 0; index65 < size64; index65++) { + const result66 = []; + const size67 = buffer.readInt(); + if (size67 > 0) { + for (let index68 = 0; index68 < size67; index68++) { + const list69 = buffer.readIntList(); + result66.push(list69); } } - result82.push(result85); + result63.push(result66); } } - result75.set(result78, result82); + result56.set(result59, result63); } } - packet.mmmm = result75; - const result89 = new Map(); - const size90 = buffer.readInt(); - if (size90 > 0) { - for (let index91 = 0; index91 < size90; index91++) { - const result92 = []; - const size93 = buffer.readInt(); - if (size93 > 0) { - for (let index94 = 0; index94 < size93; index94++) { - const map95 = buffer.readIntStringMap(); - result92.push(map95); + packet.mmmm = result56; + const result70 = new Map(); + const size71 = buffer.readInt(); + if (size71 > 0) { + for (let index72 = 0; index72 < size71; index72++) { + const result73 = []; + const size74 = buffer.readInt(); + if (size74 > 0) { + for (let index75 = 0; index75 < size74; index75++) { + const map76 = buffer.readIntStringMap(); + result73.push(map76); } } - const result96 = new Set(); - const size97 = buffer.readInt(); - if (size97 > 0) { - for (let index98 = 0; index98 < size97; index98++) { - const map99 = buffer.readIntStringMap(); - result96.add(map99); + const result77 = new Set(); + const size78 = buffer.readInt(); + if (size78 > 0) { + for (let index79 = 0; index79 < size78; index79++) { + const map80 = buffer.readIntStringMap(); + result77.add(map80); } } - result89.set(result92, result96); + result70.set(result73, result77); } } - packet.mmmmm = result89; - const set100 = buffer.readIntSet(); - packet.s = set100; - const result101 = new Set(); - const size102 = buffer.readInt(); - if (size102 > 0) { - for (let index103 = 0; index103 < size102; index103++) { - const result104 = new Set(); - const size105 = buffer.readInt(); - if (size105 > 0) { - for (let index106 = 0; index106 < size105; index106++) { - const list107 = buffer.readIntList(); - result104.add(list107); + packet.mmmmm = result70; + const set81 = buffer.readIntSet(); + packet.s = set81; + const result82 = new Set(); + const size83 = buffer.readInt(); + if (size83 > 0) { + for (let index84 = 0; index84 < size83; index84++) { + const result85 = new Set(); + const size86 = buffer.readInt(); + if (size86 > 0) { + for (let index87 = 0; index87 < size86; index87++) { + const list88 = buffer.readIntList(); + result85.add(list88); } } - result101.add(result104); + result82.add(result85); } } - packet.ss = result101; - const result108 = new Set(); - const size109 = buffer.readInt(); - if (size109 > 0) { - for (let index110 = 0; index110 < size109; index110++) { - const set111 = buffer.readPacketSet(102); - result108.add(set111); + packet.ss = result82; + const result89 = new Set(); + const size90 = buffer.readInt(); + if (size90 > 0) { + for (let index91 = 0; index91 < size90; index91++) { + const set92 = buffer.readPacketSet(102); + result89.add(set92); } } - packet.sss = result108; - const set112 = buffer.readStringSet(); - packet.ssss = set112; - const result113 = new Set(); - const size114 = buffer.readInt(); - if (size114 > 0) { - for (let index115 = 0; index115 < size114; index115++) { - const map116 = buffer.readIntStringMap(); - result113.add(map116); + packet.sss = result89; + const set93 = buffer.readStringSet(); + packet.ssss = set93; + const result94 = new Set(); + const size95 = buffer.readInt(); + if (size95 > 0) { + for (let index96 = 0; index96 < size95; index96++) { + const map97 = buffer.readIntStringMap(); + result94.add(map97); } } - packet.sssss = result113; + packet.sssss = result94; if (buffer.compatibleRead(beforeReadIndex, length)) { - const result117 = buffer.readInt(); - packet.myCompatible = result117; + const result98 = buffer.readInt(); + packet.myCompatible = result98; } if (buffer.compatibleRead(beforeReadIndex, length)) { - const result118 = buffer.readPacket(102); - packet.myObject = result118; + const result99 = buffer.readPacket(102); + packet.myObject = result99; } if (length > 0) { buffer.setReadOffset(beforeReadIndex + length); } return packet; } - } -export default ComplexObject; +export default ComplexObject; \ No newline at end of file diff --git a/protocol/src/test/ecmascript/zfooes/packet/EmptyObject.mjs b/protocol/src/test/ecmascript/zfooes/packet/EmptyObject.mjs index 3db4acbc5..15c74f4ba 100644 --- a/protocol/src/test/ecmascript/zfooes/packet/EmptyObject.mjs +++ b/protocol/src/test/ecmascript/zfooes/packet/EmptyObject.mjs @@ -29,6 +29,5 @@ class EmptyObject { } return packet; } - } -export default EmptyObject; +export default EmptyObject; \ No newline at end of file diff --git a/protocol/src/test/ecmascript/zfooes/packet/NormalObject.mjs b/protocol/src/test/ecmascript/zfooes/packet/NormalObject.mjs index ec5956e64..8c4e0bfdd 100644 --- a/protocol/src/test/ecmascript/zfooes/packet/NormalObject.mjs +++ b/protocol/src/test/ecmascript/zfooes/packet/NormalObject.mjs @@ -99,6 +99,5 @@ class NormalObject { } return packet; } - } -export default NormalObject; +export default NormalObject; \ No newline at end of file diff --git a/protocol/src/test/ecmascript/zfooes/packet/ObjectA.mjs b/protocol/src/test/ecmascript/zfooes/packet/ObjectA.mjs index 8e0563c63..ea52a6916 100644 --- a/protocol/src/test/ecmascript/zfooes/packet/ObjectA.mjs +++ b/protocol/src/test/ecmascript/zfooes/packet/ObjectA.mjs @@ -39,6 +39,5 @@ class ObjectA { } return packet; } - } -export default ObjectA; +export default ObjectA; \ No newline at end of file diff --git a/protocol/src/test/ecmascript/zfooes/packet/ObjectB.mjs b/protocol/src/test/ecmascript/zfooes/packet/ObjectB.mjs index 9e6142b30..af88d5c72 100644 --- a/protocol/src/test/ecmascript/zfooes/packet/ObjectB.mjs +++ b/protocol/src/test/ecmascript/zfooes/packet/ObjectB.mjs @@ -31,6 +31,5 @@ class ObjectB { } return packet; } - } -export default ObjectB; +export default ObjectB; \ No newline at end of file diff --git a/protocol/src/test/ecmascript/zfooes/packet/SimpleObject.mjs b/protocol/src/test/ecmascript/zfooes/packet/SimpleObject.mjs index 9adaf6bd7..7c9d7549a 100644 --- a/protocol/src/test/ecmascript/zfooes/packet/SimpleObject.mjs +++ b/protocol/src/test/ecmascript/zfooes/packet/SimpleObject.mjs @@ -35,6 +35,5 @@ class SimpleObject { } return packet; } - } -export default SimpleObject; +export default SimpleObject; \ No newline at end of file diff --git a/protocol/src/test/ecmascript/zfooes/packet/VeryBigObject.mjs b/protocol/src/test/ecmascript/zfooes/packet/VeryBigObject.mjs index 3c9d5a074..b2e0704ac 100644 --- a/protocol/src/test/ecmascript/zfooes/packet/VeryBigObject.mjs +++ b/protocol/src/test/ecmascript/zfooes/packet/VeryBigObject.mjs @@ -13403,6 +13403,5 @@ class VeryBigObject { } return packet; } - } -export default VeryBigObject; +export default VeryBigObject; \ No newline at end of file