diff --git a/lib/GMLIB_API-JS.d.ts b/lib/GMLIB_API-JS.d.ts index a105fc5..496cc60 100644 --- a/lib/GMLIB_API-JS.d.ts +++ b/lib/GMLIB_API-JS.d.ts @@ -1128,15 +1128,25 @@ export class GMLIB_BinaryStream { /** 数据包是否被销毁 */ #destroy: boolean; + /** 构造二进流数据包 */ constructor( /** 数据包数据 */ data: PacketData[]? ); + /** 拷贝二进制流数据包 */ + constructor( + /** 要拷贝的二进制流数据包 */ + stream: GMLIB_BinaryStream + ); + + /** 获取数据包的ID */ + getId(): number; + /** 发送数据包 */ sendTo( /** 要发送数据包的玩家对象 */ - player: Player, + player: Player | Entity, /** 数据包发送后是否销毁 */ free: boolean = true ): GMLIB_BinaryStream; @@ -1144,7 +1154,7 @@ export class GMLIB_BinaryStream { /** 发送数据包到玩家数组 */ sendToPlayers( /** 要发送数据包的玩家对象数组 */ - players: Player[], + players: Player[] | Entity[], /** 数据包发送后是否销毁 */ free: boolean = true ): GMLIB_BinaryStream; diff --git a/lib/GMLIB_API-JS.js b/lib/GMLIB_API-JS.js index 1a34e4c..e999d67 100644 --- a/lib/GMLIB_API-JS.js +++ b/lib/GMLIB_API-JS.js @@ -380,6 +380,8 @@ const GMLIB_API = { const GMLIB_BinaryStream_API = { /** 创建二进制流数据包 @type {function():number} */ create: ll.import("GMLIB_BinaryStream_API", "create"), + /** 拷贝二进制流数据包 @type {function(number):number} */ + copy: ll.import("GMLIB_BinaryStream_API", "copy"), /** 发送二进制流数据包 @type {function(number,player):void} */ sendTo: ll.import("GMLIB_BinaryStream_API", "sendTo"), /** 销毁二进制流数据包 @type {function(number):void} */ @@ -2116,30 +2118,39 @@ class GMLIB_BinaryStream { /** * 创建二进制流数据包 - * @param {{"type":string,"value":any}[]?} value 写入数据 + * @param {({"type":string,"value":any}[]|GMLIB_BinaryStream)?} value 写入数据 * @returns {GMLIB_BinaryStream} */ constructor(value) { - this.#id = GMLIB_BinaryStream_API.create(); + this.#id = value instanceof GMLIB_BinaryStream ? GMLIB_BinaryStream_API.copy(value.getId()) : GMLIB_BinaryStream_API.create(); if (Array.isArray(value)) this.write(value); } + /** + * 获取数据包的ID + * @returns {number} + */ + getId() { + return this.#id; + } + /** * 发送数据包 - * @param {Player} player 玩家 + * @param {Player|Entity} 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); + if (player instanceof LLSE_Entity && player.isPlayer()) player = player.toPlayer(); + if (player instanceof LLSE_Player && !player.isSimulatedPlayer()) GMLIB_BinaryStream_API.sendTo(this.#id, player); if (free) this.destroy(); return this; } /** * 发送数据包到玩家 - * @param {Player[]} players 玩家对象数组 + * @param {Player[]|Entity[]} players 玩家对象数组 * @param {boolean} free 发送后销毁数据包 * @returns {GMLIB_BinaryStream} */ @@ -2324,7 +2335,7 @@ class GMLIB_BinaryStream { */ writeBool(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); - GMLIB_BinaryStream_API.writeBool(this.#id, value); + GMLIB_BinaryStream_API.writeBool(this.#id, Boolean(value)); return this; } @@ -2335,7 +2346,7 @@ class GMLIB_BinaryStream { */ writeByte(value) { if (this.#destroy) throw new Error("The BinaryStream has been destroyed"); - GMLIB_BinaryStream_API.writeByte(this.#id, value); + GMLIB_BinaryStream_API.writeByte(this.#id, Number(value)); return this; } diff --git a/src/BinaryStreamApi.cpp b/src/BinaryStreamApi.cpp index 7eb6e95..465d21c 100644 --- a/src/BinaryStreamApi.cpp +++ b/src/BinaryStreamApi.cpp @@ -11,6 +11,15 @@ class LegacyScriptBinaryStreamManager { void cretateBinaryStream(uint id) { mBinaryStream[id] = std::make_shared(); } + uint64 copyBinaryStream(uint id) { + auto nextId = getNextId(); + cretateBinaryStream(nextId); + if(auto bs = getBinaryStream(nextId); bs !=nullptr){ + *getBinaryStream(nextId)->mBuffer = *getBinaryStream(id)->mBuffer; + } + return nextId; + } + void removeBinaryStream(uint64 id) { mBinaryStream.erase(id); } std::shared_ptr getBinaryStream(uint64 id) { @@ -46,6 +55,9 @@ void Export_BinaryStream_API() { BinaryStreamManager.cretateBinaryStream(id); return id; }); + RemoteCall::exportAs("GMLIB_BinaryStream_API", "copy", [](uint64 id) -> uint64 { + return BinaryStreamManager.copyBinaryStream(id); + }); RemoteCall::exportAs("GMLIB_BinaryStream_API", "reset", [](uint64 id) -> void { if (BinaryStreamManager.getBinaryStream(id) == nullptr) BinaryStreamManager.cretateBinaryStream(id); else BinaryStreamManager.getBinaryStream(id)->reset();