From 01d949997098cec85650b39ad7f9a31946bd09e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E6=B2=90=E5=91=80?= <163636894+zimuya4153@users.noreply.github.com> Date: Thu, 10 Oct 2024 18:45:02 +0800 Subject: [PATCH] feat: modify GMLIB_BinaryStream API to return the BinaryStream object itself after each method call --- lib/GMLIB_API-JS.d.ts | 145 ++++++++++++++++++++++++++++++---------- lib/GMLIB_API-JS.js | 94 ++++++++++++++++++++++++-- src/BinaryStreamApi.cpp | 6 +- 3 files changed, 203 insertions(+), 42 deletions(-) diff --git a/lib/GMLIB_API-JS.d.ts b/lib/GMLIB_API-JS.d.ts index d72233b..ebdbf5c 100644 --- a/lib/GMLIB_API-JS.d.ts +++ b/lib/GMLIB_API-JS.d.ts @@ -1054,9 +1054,78 @@ declare class UserCache { static getAllPlayerInfo(): { Xuid: String; Uuid: string; Name: string; }[] } +type PacketDataItem = ( + { + id: number, + type: 0x0 | 0x1 | 0x2 | 0x3 | 0x7, + value: number + } + | { + id: number, + type: 0x4, + value: string + } + | { + id: number, + type: 0x5, + value: NbtCompound + } + | { + id: number, + type: 0x6 | 0x8, + value: IntPos | FloatPos + } +); + +type PacketData = ( + { type: "Item", value: Item } + | { type: "CompoundTag", value: NbtCompound } + | { + type: "DataItem", + value: PacketDataItem[], + } + | { + type: "ActorLink", + value: { + mA: number, + mB: number, + mType: number, + mImmediate: boolean, + mPassengerInitiated: boolean, + }, + } + | { type: "Vec3" | "BlockPos", value: FloatPos | IntPos } + | { type: "Vec2", value: DirectionAngle } + | { type: "String" | "Uuid", value: string } + | { type: "Bool", value: boolean } + | { + type: + "PacketHeader" + | "Byte" + | "Double" + | "Float" + | "SignedBigEndianInt" + | "SignedInt" + | "SignedInt64" + | "SignedShort" + | "UnsignedChar" + | "UnsignedInt" + | "UnsignedInt64" + | "UnsignedShort" + | "UnsignedVarInt" + | "UnsignedVarInt64" + | "VarInt" + | "VarInt64", + value: number, + } +); + /** 二进制流数据包类 */ declare class GMLIB_BinaryStream { - constructor(); + constructor( + /** 数据包数据 */ + data: PacketData[]? + ); /** 发送数据包 */ sendTo( @@ -1064,7 +1133,7 @@ declare class GMLIB_BinaryStream { player: Player, /** 数据包发送后是否销毁 */ free: boolean = true - ): void; + ): GMLIB_BinaryStream; /** 发送数据包到玩家数组 */ sendToPlayers( @@ -1072,13 +1141,13 @@ declare class GMLIB_BinaryStream { players: Player[], /** 数据包发送后是否销毁 */ free: boolean = true - ): void; + ): GMLIB_BinaryStream; /** 发送数据包到所有玩家 */ sendToAll( /** 数据包发送后是否销毁 */ free: boolean = true - ): void; + ): GMLIB_BinaryStream; /** 发送数据包到维度 */ sendToDimension( @@ -1086,168 +1155,174 @@ declare class GMLIB_BinaryStream { dimid: number, /** 数据包发送后是否销毁 */ free: boolean = true - ): void; + ): GMLIB_BinaryStream; /** 销毁数据包 */ - destroy(): void; + destroy(): GMLIB_BinaryStream; /** 重置数据包 */ - reset(): void; + reset(): GMLIB_BinaryStream; + + /** 写入数据 */ + write( + /** 数据 */ + data: PacketData | PacketData[]? + ): GMLIB_BinaryStream; /** 写入数据包头部 */ writePacketHeader( /** 数据包ID */ id: number - ): void; + ): GMLIB_BinaryStream; /** 写入UUID */ writeUuid( /** UUID */ value: string - ): void; + ): GMLIB_BinaryStream; /** 写入物品 */ writeItem( /** 物品对象 */ value: Item - ): void; + ): GMLIB_BinaryStream; /** 写入NBT */ writeCompoundTag( /** NBT */ value: NbtCompound - ): void; + ): GMLIB_BinaryStream; /** */ writeDataItem( /** 数据项 */ - value: { "id": number, "type": number, "value": string | IntPos | FloatPos | number | NbtCompound }[] - ): void; + value: PacketDataItem[] + ): GMLIB_BinaryStream; writeActorLink( /** 数据项 */ - value: {"mA":number,"mB":number,"mType":number,"mImmediate":boolean,"mPassengerInitiated":boolean} - ): void; + value: { "mA": number, "mB": number, "mType": number, "mImmediate": boolean, "mPassengerInitiated": boolean } + ): GMLIB_BinaryStream; /** 写入浮点数坐标对象 */ writeVec3( /** 浮点数坐标对象 */ value: FloatPos - ) : void; + ): GMLIB_BinaryStream; /** 写入整数坐标对象 */ writeBlockPos( /** 整数坐标对象 */ value: IntPos - ) : void; + ): GMLIB_BinaryStream; /** 写入方向角对象 */ writeVec2( /** 方向角对象 */ value: DirectionAngle - ) : void; + ): GMLIB_BinaryStream; /** 写入字符串 */ writeString( /** 字符串 */ value: string - ): void; + ): GMLIB_BinaryStream; /** 写入布尔值 */ writeBool( /** 布尔值 */ value: boolean - ): void; + ): GMLIB_BinaryStream; /** 写入字节 */ writeByte( /** 字节 */ value: number - ): void; + ): GMLIB_BinaryStream; /** 写入(双精度)浮点数 */ writeDouble( /** 浮点数 */ value: number - ): void; + ): GMLIB_BinaryStream; /** 写入(单精度)浮点数 */ writeFloat( /** 浮点数 */ value: number - ): void; + ): GMLIB_BinaryStream; /** */ writeSignedBigEndianInt( /** 数值 */ value: number - ): void; + ): GMLIB_BinaryStream; /** */ writeSignedInt( /** 数值 */ value: number - ): void; + ): GMLIB_BinaryStream; /** 写入 */ writeSignedInt64( /** 数值 */ value: number - ): void; + ): GMLIB_BinaryStream; /** */ writeSignedShort( /** 数值 */ value: number - ): void; + ): GMLIB_BinaryStream; /** */ writeUnsignedChar( /** 数值 */ value: number - ): void; + ): GMLIB_BinaryStream; /** */ writeUnsignedInt( /** 数值 */ value: number - ): void; + ): GMLIB_BinaryStream; /** */ writeUnsignedInt64( /** 数值 */ value: number - ): void; + ): GMLIB_BinaryStream; /** */ writeUnsignedShort( /** 数值 */ value: number - ): void; + ): GMLIB_BinaryStream; /** */ writeUnsignedVarInt( /** 数值 */ value: number - ): void; + ): GMLIB_BinaryStream; /** */ writeUnsignedVarInt64( /** 数值 */ value: number - ): void; + ): GMLIB_BinaryStream; /** */ writeVarInt( /** 数值 */ value: number - ): void; + ): GMLIB_BinaryStream; /** */ writeVarInt64( /** 数值 */ value: number - ): void; + ): GMLIB_BinaryStream; } interface Player { diff --git a/lib/GMLIB_API-JS.js b/lib/GMLIB_API-JS.js index d7720e5..87cf26b 100644 --- a/lib/GMLIB_API-JS.js +++ b/lib/GMLIB_API-JS.js @@ -380,7 +380,7 @@ const GMLIB_API = { const GMLIB_BinaryStream_API = { /** 创建二进制流数据包 @type {function():number} */ create: ll.import("GMLIB_BinaryStream_API", "create"), - /** 发送二进制流数据包 @type {function(number,player, boolean):void} */ + /** 发送二进制流数据包 @type {function(number,player):void} */ sendTo: ll.import("GMLIB_BinaryStream_API", "sendTo"), /** 销毁二进制流数据包 @type {function(number):void} */ destroy: ll.import("GMLIB_BinaryStream_API", "destroy"), @@ -2114,107 +2114,146 @@ class GMLIB_BinaryStream { /** 数据包是否被销毁 */ #destroy = false; - constructor() { + /** + * 创建二进制流数据包 + * @param {{"type":string,"value":any}[]?} value 写入数据 + * @returns {GMLIB_BinaryStream} + */ + constructor(value) { this.#id = GMLIB_BinaryStream_API.create(); + if (Array.isArray(value)) this.write(value); } /** * 发送数据包 * @param {Player} player 玩家 * @param {boolean} [free=true] 发送后销毁数据包 + * @returns {GMLIB_BinaryStream} */ sendTo(player, free = true) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); - GMLIB_BinaryStream_API.sendTo(this.#id, player, free); + GMLIB_BinaryStream_API.sendTo(this.#id, player); if (free) this.destroy(); + return this; } /** * 发送数据包到玩家 * @param {Player[]} players 玩家对象数组 * @param {boolean} free 发送后销毁数据包 + * @returns {GMLIB_BinaryStream} */ sendToPlayers(players, free = true) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); players.forEach(player => this.sendTo(player, false)); if (free) this.destroy(); + return this; } /** * 发送数据包到所有玩家 * @param {boolean} [free=true] 发送后销毁数据包 + * @returns {GMLIB_BinaryStream} */ sendToAll(free = true) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); mc.getOnlinePlayers().forEach(player => this.sendTo(player, false)); if (free) this.destroy(); + return this; } /** * 发送数据包到维度 * @param {number} dimid 维度ID * @param {boolean} [free=true] 发送后销毁数据包 + * @returns {GMLIB_BinaryStream} */ sendToDimension(dimid, free = true) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); mc.getOnlinePlayers().filter(player => player.pos.dimid === dimid).forEach(player => this.sendTo(player, false)); if (free) this.destroy(); + return this; } /** * 销毁数据包 + * @returns {GMLIB_BinaryStream} */ destroy() { GMLIB_BinaryStream_API.destroy(this.#id); this.#destroy = true; + return this; } /** * 重置数据包 + * @returns {GMLIB_BinaryStream} */ reset() { GMLIB_BinaryStream_API.reset(this.#id); this.#destroy = false; + return this; + } + + /** + * 写入数据 + * @param {{"type":string,"value":any}[]|{"type":string,"value":any}} value 数据 + * @returns {GMLIB_BinaryStream} + */ + write(value) { + if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); + if (Array.isArray(value)) value.forEach(v => this.write(v)); + if (value?.type != null && value?.value != null) this[`write${value.type}`]?.(value.value); + return this; } /** * 写入数据包头部 * @param {number} id 数据包ID + * @returns {GMLIB_BinaryStream} */ writePacketHeader(id) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writePacketHeader(this.#id, id); + return this; } /** * 写入UUID * @param {string} value UUID + * @returns {GMLIB_BinaryStream} */ writeUuid(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeUuid(this.#id, value); + return this; } /** * 写入物品 * @param {Item} value 物品 + * @returns {GMLIB_BinaryStream} */ - writeItem(value){ + writeItem(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeItem(this.#id, value); + return this; } /** * 写入NBT * @param {NbtCompound} value NBT + * @returns {GMLIB_BinaryStream} */ - writeCompoundTag(value){ + writeCompoundTag(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeCompoundTag(this.#id, value); + return this; } /** * @param {{"id":number,"type":number,"value":string|IntPos|FloatPos|number|NbtCompound}[]} value 数据 + * @returns {GMLIB_BinaryStream} */ writeDataItem(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); @@ -2235,10 +2274,12 @@ class GMLIB_BinaryStream { default: throw new Error("Unknown data type"); } } + return this; } /** * @param {{"mA":number,"mB":number,"mType":number,"mImmediate":boolean,"mPassengerInitiated":boolean}} value 数据 + * @returns {GMLIB_BinaryStream} */ writeActorLink(value) { this.writeVarInt64(value.mA); @@ -2246,179 +2287,220 @@ class GMLIB_BinaryStream { this.writeUnsignedChar(value.mType); this.writeBool(value.mImmediate); this.writeBool(value.mPassengerInitiated); + return this; } /** * 写入字符串 * @param {string} value 字符串 + * @returns {GMLIB_BinaryStream} */ - writeString(value){ + writeString(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeString(this.#id, value); + return this; } /** * 写入布尔值 * @param {boolean} value 布尔值 + * @returns {GMLIB_BinaryStream} */ writeBool(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeBool(this.#id, value); + return this; } /** * 写入字节 * @param {number} value 字节 + * @returns {GMLIB_BinaryStream} */ writeByte(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeByte(this.#id, value); + return this; } /** * 写入(双精度)浮点数 * @param {number} value 浮点数 + * @returns {GMLIB_BinaryStream} */ writeDouble(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeDouble(this.#id, value); + return this; } /** * 写入(单精度)浮点数 * @param {number} value 浮点数 + * @returns {GMLIB_BinaryStream} */ writeFloat(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeFloat(this.#id, value); + return this; } /** * 写入浮点数坐标对象 * @param {FloatPos} value 浮点数坐标对象 + * @returns {GMLIB_BinaryStream} */ writeVec3({ x, y, z }) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); this.writeFloat(x); this.writeFloat(y); this.writeFloat(z); + return this; } /** * 写入整数坐标对象 * @param {IntPos} value 整数坐标对象 + * @returns {GMLIB_BinaryStream} */ writeBlockPos({ x, y, z }) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); this.writeVarInt(x); this.writeUnsignedVarInt(y); this.writeVarInt(z); + return this; } /** * 写入方向角对象 * @param {DirectionAngle} value 方向角对象 + * @returns {GMLIB_BinaryStream} */ writeVec2({ pitch, yaw }) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); this.writeFloat(pitch); this.writeFloat(yaw); + return this; } /** * @param {number} value 数值 + * @returns {GMLIB_BinaryStream} */ writeSignedBigEndianInt(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeSignedBigEndianInt(this.#id, value); + return this; } /** * @param {number} value 数值 + * @returns {GMLIB_BinaryStream} */ writeSignedInt(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeSignedInt(this.#id, value); + return this; } /** * @param {number} value 数值 + * @returns {GMLIB_BinaryStream} */ writeSignedInt64(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeSignedInt64(this.#id, value); + return this; } /** * @param {number} value 数值 + * @returns {GMLIB_BinaryStream} */ writeSignedShort(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeSignedShort(this.#id, value); + return this; } /** * @param {number} value 数值 + * @returns {GMLIB_BinaryStream} */ writeUnsignedChar(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeUnsignedChar(this.#id, value); + return this; } /** * @param {number} value 数值 + * @returns {GMLIB_BinaryStream} */ writeUnsignedInt(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeUnsignedInt(this.#id, value); + return this; } /** * @param {number} value 数值 + * @returns {GMLIB_BinaryStream} */ writeUnsignedInt64(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeUnsignedInt64(this.#id, value); + return this; } /** * @param {number} value 数值 + * @returns {GMLIB_BinaryStream} */ writeUnsignedShort(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeUnsignedShort(this.#id, value); + return this; } /** * @param {number} value 数值 + * @returns {GMLIB_BinaryStream} */ writeUnsignedVarInt(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeUnsignedVarInt(this.#id, value); + return this; } /** * @param {number} value 数值 + * @returns {GMLIB_BinaryStream} */ writeUnsignedVarInt64(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeUnsignedVarInt64(this.#id, value); + return this; } /** * @param {number} value 数值 + * @returns {GMLIB_BinaryStream} */ writeVarInt(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeVarInt(this.#id, value); + return this; } /** * @param {number} value 数值 + * @returns {GMLIB_BinaryStream} */ writeVarInt64(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); GMLIB_BinaryStream_API.writeVarInt64(this.#id, value); + return this; } } diff --git a/src/BinaryStreamApi.cpp b/src/BinaryStreamApi.cpp index fb8a20f..e588841 100644 --- a/src/BinaryStreamApi.cpp +++ b/src/BinaryStreamApi.cpp @@ -63,7 +63,11 @@ void Export_BinaryStream_API() { }); EXPORTAPI("writePacketHeader", int, binaryStream->writePacketHeader((MinecraftPacketIds)value)); EXPORTAPI("writeUuid", std::string const&, binaryStream->writeUuid(mce::UUID::fromString(value))); - EXPORTAPI("writeItem", ItemStack*, NetworkItemStackDescriptor(*value).write(*binaryStream)); + EXPORTAPI( + "writeItem", + ItemStack*, + binaryStream->writeType(NetworkItemStackDescriptor(*value)) + ); EXPORTAPI("writeString", std::string const&, binaryStream->writeString(value)); EXPORTAPI("writeCompoundTag", CompoundTag*, binaryStream->writeCompoundTag(*value)); EXPORTAPI2(writeBool);