Skip to content

Commit

Permalink
feat: add copy constructor and getId method for GMLIB_BinaryStream
Browse files Browse the repository at this point in the history
  • Loading branch information
zimuya4153 committed Dec 17, 2024
1 parent 4c57b04 commit cf8076a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
14 changes: 12 additions & 2 deletions lib/GMLIB_API-JS.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1128,23 +1128,33 @@ 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;

/** 发送数据包到玩家数组 */
sendToPlayers(
/** 要发送数据包的玩家对象数组 */
players: Player[],
players: Player[] | Entity[],
/** 数据包发送后是否销毁 */
free: boolean = true
): GMLIB_BinaryStream;
Expand Down
25 changes: 18 additions & 7 deletions lib/GMLIB_API-JS.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} */
Expand Down Expand Up @@ -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}
*/
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
12 changes: 12 additions & 0 deletions src/BinaryStreamApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ class LegacyScriptBinaryStreamManager {

void cretateBinaryStream(uint id) { mBinaryStream[id] = std::make_shared<GMLIB_BinaryStream>(); }

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<GMLIB_BinaryStream> getBinaryStream(uint64 id) {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit cf8076a

Please sign in to comment.