From 4ab84f3de2606d0e3b8685f231d13079b8eddd88 Mon Sep 17 00:00:00 2001 From: wtver <51377408+maattch@users.noreply.github.com> Date: Thu, 23 Jan 2025 22:57:11 -0300 Subject: [PATCH] fix --- src/events.cpp | 4 ++-- src/events.h | 4 ++-- src/game.cpp | 4 ++-- src/game.h | 2 +- src/networkmessage.h | 3 +++ src/protocolgame.cpp | 4 +++- 6 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/events.cpp b/src/events.cpp index 3937e72169..36e36bea5f 100644 --- a/src/events.cpp +++ b/src/events.cpp @@ -1380,7 +1380,7 @@ void onInventoryUpdate(Player* player, Item* item, slots_t slot, bool equip) scriptInterface.callVoidFunction(4); } -void onNetworkMessage(Player* player, uint8_t recvByte, NetworkMessage* msg) +void onNetworkMessage(Player* player, uint8_t recvByte, NetworkMessage_ptr msg) { // Player:onNetworkMessage(recvByte, msg) if (playerHandlers.onNetworkMessage == -1) { @@ -1403,7 +1403,7 @@ void onNetworkMessage(Player* player, uint8_t recvByte, NetworkMessage* msg) lua_pushnumber(L, recvByte); - tfs::lua::pushUserdata(L, msg); + tfs::lua::pushUserdata(L, msg.release()); tfs::lua::setMetatable(L, -1, "NetworkMessage"); scriptInterface.callVoidFunction(3); diff --git a/src/events.h b/src/events.h index d7e1e41ef8..687094b316 100644 --- a/src/events.h +++ b/src/events.h @@ -7,9 +7,9 @@ #include "const.h" #include "creature.h" #include "luascript.h" +#include "networkmessage.h" class ItemType; -class NetworkMessage; class Party; class Spell; class Tile; @@ -83,7 +83,7 @@ void onLoseExperience(Player* player, uint64_t& exp); void onGainSkillTries(Player* player, skills_t skill, uint64_t& tries); void onWrapItem(Player* player, Item* item); void onInventoryUpdate(Player* player, Item* item, slots_t slot, bool equip); -void onNetworkMessage(Player* player, uint8_t recvByte, NetworkMessage* msg); +void onNetworkMessage(Player* player, uint8_t recvByte, NetworkMessage_ptr msg); bool onSpellCheck(Player* player, const Spell* spell); } // namespace tfs::events::player diff --git a/src/game.cpp b/src/game.cpp index 9a871adb52..9a1b273a0f 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -5548,14 +5548,14 @@ void Game::parsePlayerExtendedOpcode(uint32_t playerId, uint8_t opcode, const st } } -void Game::parsePlayerNetworkMessage(uint32_t playerId, uint8_t recvByte, NetworkMessage* msg) +void Game::parsePlayerNetworkMessage(uint32_t playerId, uint8_t recvByte, NetworkMessage_ptr msg) { Player* player = getPlayerByID(playerId); if (!player) { return; } - tfs::events::player::onNetworkMessage(player, recvByte, msg); + tfs::events::player::onNetworkMessage(player, recvByte, std::move(msg)); } std::vector Game::getMarketItemList(uint16_t wareId, uint16_t sufficientCount, Player& player) diff --git a/src/game.h b/src/game.h index 1ad03f1454..47f1c40c93 100644 --- a/src/game.h +++ b/src/game.h @@ -392,7 +392,7 @@ class Game void playerAcceptMarketOffer(uint32_t playerId, uint32_t timestamp, uint16_t counter, uint16_t amount); void parsePlayerExtendedOpcode(uint32_t playerId, uint8_t opcode, const std::string& buffer); - void parsePlayerNetworkMessage(uint32_t playerId, uint8_t recvByte, NetworkMessage* msg); + void parsePlayerNetworkMessage(uint32_t playerId, uint8_t recvByte, NetworkMessage_ptr msg); std::vector getMarketItemList(uint16_t wareId, uint16_t sufficientCount, Player& player); diff --git a/src/networkmessage.h b/src/networkmessage.h index 84898e19c7..9710a7fd55 100644 --- a/src/networkmessage.h +++ b/src/networkmessage.h @@ -9,6 +9,9 @@ class Item; struct Position; +class NetworkMessage; +using NetworkMessage_ptr = std::unique_ptr; + class NetworkMessage { public: diff --git a/src/protocolgame.cpp b/src/protocolgame.cpp index 8bfec0886f..fec7d4143c 100644 --- a/src/protocolgame.cpp +++ b/src/protocolgame.cpp @@ -793,8 +793,10 @@ void ProtocolGame::parsePacket(NetworkMessage& msg) // case 0xFE: break; // store window history 2 default: + // we cannot pass an unique_ptr as capture here because + // std::function requires the callable object to be *copyable* g_dispatcher.addTask([=, playerID = player->getID(), msg = new NetworkMessage(msg)]() { - g_game.parsePlayerNetworkMessage(playerID, recvbyte, msg); + g_game.parsePlayerNetworkMessage(playerID, recvbyte, NetworkMessage_ptr(msg)); }); break; }