Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding network msg to network msg #4878

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2527,6 +2527,7 @@ void LuaScriptInterface::registerFunctions()
registerMethod(L, "NetworkMessage", "addDouble", LuaScriptInterface::luaNetworkMessageAddDouble);
registerMethod(L, "NetworkMessage", "addItem", LuaScriptInterface::luaNetworkMessageAddItem);
registerMethod(L, "NetworkMessage", "addItemId", LuaScriptInterface::luaNetworkMessageAddItemId);
registerMethod(L, "NetworkMessage", "addNetworkMessage", LuaScriptInterface::luaNetworkMessageAddNetworkMessage);

registerMethod(L, "NetworkMessage", "reset", LuaScriptInterface::luaNetworkMessageReset);
registerMethod(L, "NetworkMessage", "seek", LuaScriptInterface::luaNetworkMessageSeek);
Expand Down Expand Up @@ -6293,6 +6294,26 @@ int LuaScriptInterface::luaNetworkMessageAddItemId(lua_State* L)
return 1;
}

int LuaScriptInterface::luaNetworkMessageAddNetworkMessage(lua_State* L)
{
// networkMessage:addNetworkMessage(msg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

networkMessage:compose(otherMsg)?

NetworkMessage* message = tfs::lua::getUserdata<NetworkMessage>(L, 1);
if (!message) {
lua_pushnil(L);
return 1;
}

NetworkMessage* msgToAdd = tfs::lua::getUserdata<NetworkMessage>(L, 2);
if (!msgToAdd) {
lua_pushnil(L);
return 1;
}

message->addNetworkMessage(msgToAdd);
tfs::lua::pushBoolean(L, true);
return 1;
}

int LuaScriptInterface::luaNetworkMessageReset(lua_State* L)
{
// networkMessage:reset()
Expand Down
1 change: 1 addition & 0 deletions src/luascript.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ class LuaScriptInterface
static int luaNetworkMessageAddDouble(lua_State* L);
static int luaNetworkMessageAddItem(lua_State* L);
static int luaNetworkMessageAddItemId(lua_State* L);
static int luaNetworkMessageAddNetworkMessage(lua_State* L);

static int luaNetworkMessageReset(lua_State* L);
static int luaNetworkMessageSeek(lua_State* L);
Expand Down
16 changes: 16 additions & 0 deletions src/networkmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,19 @@ void NetworkMessage::addItem(const Item* item)
}

void NetworkMessage::addItemId(uint16_t itemId) { add<uint16_t>(Item::items[itemId].clientId); }

void NetworkMessage::addNetworkMessage(NetworkMessage* networkMsg)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probaly can be changed from:
void NetworkMessage::addNetworkMessage(NetworkMessage* networkMsg)
to:
void NetworkMessage::addNetworkMessage(NetworkMessage networkMsg)

{
if (!networkMsg) {
return;
}

if (!canAdd(networkMsg->getLength())) {
return;
}

std::memcpy(buffer.data() + info.position, networkMsg->getBuffer() + INITIAL_BUFFER_POSITION,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldnt addBytes from networkmessage class suffice here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, i will check.

networkMsg->getLength());
info.position += networkMsg->getLength();
info.length += networkMsg->getLength();
}
1 change: 1 addition & 0 deletions src/networkmessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class NetworkMessage
void addItem(uint16_t id, uint8_t count);
void addItem(const Item* item);
void addItemId(uint16_t itemId);
void addNetworkMessage(NetworkMessage* networkMsg);

MsgSize_t getLength() const { return info.length; }

Expand Down
Loading