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

Use shared_ptr for Inbox #4880

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 12 additions & 8 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5321,7 +5321,8 @@ void Game::playerCancelMarketOffer(uint32_t playerId, uint32_t timestamp, uint16
while (tmpAmount > 0) {
int32_t stackCount = std::min<int32_t>(ITEM_STACK_SIZE, tmpAmount);
Item* item = Item::CreateItem(it.id, stackCount);
if (internalAddItem(player->getInbox(), item, INDEX_WHEREEVER, FLAG_NOLIMIT) != RETURNVALUE_NOERROR) {
if (internalAddItem(player->getInbox().get(), item, INDEX_WHEREEVER, FLAG_NOLIMIT) !=
RETURNVALUE_NOERROR) {
delete item;
break;
}
Expand All @@ -5338,7 +5339,8 @@ void Game::playerCancelMarketOffer(uint32_t playerId, uint32_t timestamp, uint16

for (uint16_t i = 0; i < offer.amount; ++i) {
Item* item = Item::CreateItem(it.id, subType);
if (internalAddItem(player->getInbox(), item, INDEX_WHEREEVER, FLAG_NOLIMIT) != RETURNVALUE_NOERROR) {
if (internalAddItem(player->getInbox().get(), item, INDEX_WHEREEVER, FLAG_NOLIMIT) !=
RETURNVALUE_NOERROR) {
delete item;
break;
}
Expand Down Expand Up @@ -5429,7 +5431,7 @@ void Game::playerAcceptMarketOffer(uint32_t playerId, uint32_t timestamp, uint16
while (tmpAmount > 0) {
uint16_t stackCount = std::min<uint16_t>(ITEM_STACK_SIZE, tmpAmount);
Item* item = Item::CreateItem(it.id, stackCount);
if (internalAddItem(buyerPlayer->getInbox(), item, INDEX_WHEREEVER, FLAG_NOLIMIT) !=
if (internalAddItem(buyerPlayer->getInbox().get(), item, INDEX_WHEREEVER, FLAG_NOLIMIT) !=
RETURNVALUE_NOERROR) {
delete item;
break;
Expand All @@ -5447,7 +5449,7 @@ void Game::playerAcceptMarketOffer(uint32_t playerId, uint32_t timestamp, uint16

for (uint16_t i = 0; i < amount; ++i) {
Item* item = Item::CreateItem(it.id, subType);
if (internalAddItem(buyerPlayer->getInbox(), item, INDEX_WHEREEVER, FLAG_NOLIMIT) !=
if (internalAddItem(buyerPlayer->getInbox().get(), item, INDEX_WHEREEVER, FLAG_NOLIMIT) !=
RETURNVALUE_NOERROR) {
delete item;
break;
Expand Down Expand Up @@ -5476,7 +5478,8 @@ void Game::playerAcceptMarketOffer(uint32_t playerId, uint32_t timestamp, uint16
while (tmpAmount > 0) {
uint16_t stackCount = std::min<uint16_t>(ITEM_STACK_SIZE, tmpAmount);
Item* item = Item::CreateItem(it.id, stackCount);
if (internalAddItem(player->getInbox(), item, INDEX_WHEREEVER, FLAG_NOLIMIT) != RETURNVALUE_NOERROR) {
if (internalAddItem(player->getInbox().get(), item, INDEX_WHEREEVER, FLAG_NOLIMIT) !=
RETURNVALUE_NOERROR) {
delete item;
break;
}
Expand All @@ -5493,7 +5496,8 @@ void Game::playerAcceptMarketOffer(uint32_t playerId, uint32_t timestamp, uint16

for (uint16_t i = 0; i < amount; ++i) {
Item* item = Item::CreateItem(it.id, subType);
if (internalAddItem(player->getInbox(), item, INDEX_WHEREEVER, FLAG_NOLIMIT) != RETURNVALUE_NOERROR) {
if (internalAddItem(player->getInbox().get(), item, INDEX_WHEREEVER, FLAG_NOLIMIT) !=
RETURNVALUE_NOERROR) {
delete item;
break;
}
Expand Down Expand Up @@ -5554,10 +5558,10 @@ void Game::parsePlayerNetworkMessage(uint32_t playerId, uint8_t recvByte, Networ
tfs::events::player::onNetworkMessage(player, recvByte, msg);
}

std::vector<Item*> Game::getMarketItemList(uint16_t wareId, uint16_t sufficientCount, const Player& player)
std::vector<Item*> Game::getMarketItemList(uint16_t wareId, uint16_t sufficientCount, Player& player)
{
uint16_t count = 0;
std::list<Container*> containers{player.getInbox()};
std::list<Container*> containers{player.getInbox().get()};

for (const auto& chest : player.depotChests) {
if (!chest.second->empty()) {
Expand Down
2 changes: 1 addition & 1 deletion src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ class Game
void parsePlayerExtendedOpcode(uint32_t playerId, uint8_t opcode, const std::string& buffer);
void parsePlayerNetworkMessage(uint32_t playerId, uint8_t recvByte, NetworkMessage* msg);

std::vector<Item*> getMarketItemList(uint16_t wareId, uint16_t sufficientCount, const Player& player);
std::vector<Item*> getMarketItemList(uint16_t wareId, uint16_t sufficientCount, Player& player);

void cleanup();
void shutdown();
Expand Down
6 changes: 3 additions & 3 deletions src/house.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ bool House::transferToDepot(Player* player) const
}

for (Item* item : moveItemList) {
g_game.internalMoveItem(item->getParent(), player->getInbox(), INDEX_WHEREEVER, item, item->getItemCount(),
nullptr, FLAG_NOLIMIT);
g_game.internalMoveItem(item->getParent(), player->getInbox().get(), INDEX_WHEREEVER, item,
item->getItemCount(), nullptr, FLAG_NOLIMIT);
}
return true;
}
Expand Down Expand Up @@ -686,7 +686,7 @@ void Houses::payHouses(RentPeriod_t rentPeriod) const
letter->setText(fmt::format(
"Warning! \nThe {:s} rent of {:d} gold for your house \"{:s}\" is payable. Have it within {:d} days or you will lose this house.",
period, house->getRent(), house->getName(), daysLeft));
g_game.internalAddItem(player.getInbox(), letter, INDEX_WHEREEVER, FLAG_NOLIMIT);
g_game.internalAddItem(player.getInbox().get(), letter, INDEX_WHEREEVER, FLAG_NOLIMIT);
house->setPayRentWarnings(house->getPayRentWarnings() + 1);
} else {
house->setOwner(0, true, &player);
Expand Down
3 changes: 3 additions & 0 deletions src/inbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#include "container.h"

class Inbox;
using Inbox_ptr = std::shared_ptr<Inbox>;

class Inbox final : public Container
{
public:
Expand Down
4 changes: 2 additions & 2 deletions src/iomarket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void IOMarket::processExpiredOffers(DBResult_ptr result, bool)
while (tmpAmount > 0) {
uint16_t stackCount = std::min<uint16_t>(ITEM_STACK_SIZE, tmpAmount);
Item* item = Item::CreateItem(itemType.id, stackCount);
if (g_game.internalAddItem(player->getInbox(), item, INDEX_WHEREEVER, FLAG_NOLIMIT) !=
if (g_game.internalAddItem(player->getInbox().get(), item, INDEX_WHEREEVER, FLAG_NOLIMIT) !=
RETURNVALUE_NOERROR) {
delete item;
break;
Expand All @@ -150,7 +150,7 @@ void IOMarket::processExpiredOffers(DBResult_ptr result, bool)

for (uint16_t i = 0; i < amount; ++i) {
Item* item = Item::CreateItem(itemType.id, subType);
if (g_game.internalAddItem(player->getInbox(), item, INDEX_WHEREEVER, FLAG_NOLIMIT) !=
if (g_game.internalAddItem(player->getInbox().get(), item, INDEX_WHEREEVER, FLAG_NOLIMIT) !=
RETURNVALUE_NOERROR) {
delete item;
break;
Expand Down
6 changes: 3 additions & 3 deletions src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9106,10 +9106,10 @@ int LuaScriptInterface::luaPlayerGetInbox(lua_State* L)
return 1;
}

Inbox* inbox = player->getInbox();
const auto& inbox = player->getInbox();
if (inbox) {
tfs::lua::pushUserdata<Item>(L, inbox);
tfs::lua::setItemMetatable(L, -1, inbox);
pushSharedPtr(L, inbox);
tfs::lua::setItemMetatable(L, -1, inbox.get());
} else {
tfs::lua::pushBoolean(L, false);
}
Expand Down
6 changes: 3 additions & 3 deletions src/mailbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ bool Mailbox::sendItem(Item* item) const

Player* player = g_game.getPlayerByName(receiver);
if (player) {
if (g_game.internalMoveItem(item->getParent(), player->getInbox(), INDEX_WHEREEVER, item, item->getItemCount(),
nullptr, FLAG_NOLIMIT) == RETURNVALUE_NOERROR) {
if (g_game.internalMoveItem(item->getParent(), player->getInbox().get(), INDEX_WHEREEVER, item,
item->getItemCount(), nullptr, FLAG_NOLIMIT) == RETURNVALUE_NOERROR) {
g_game.transformItem(item, item->getID() + 1);
player->onReceiveMail();
return true;
Expand All @@ -94,7 +94,7 @@ bool Mailbox::sendItem(Item* item) const
return false;
}

if (g_game.internalMoveItem(item->getParent(), tmpPlayer.getInbox(), INDEX_WHEREEVER, item,
if (g_game.internalMoveItem(item->getParent(), tmpPlayer.getInbox().get(), INDEX_WHEREEVER, item,
item->getItemCount(), nullptr, FLAG_NOLIMIT) == RETURNVALUE_NOERROR) {
g_game.transformItem(item, item->getID() + 1);
IOLoginData::savePlayer(&tmpPlayer);
Expand Down
12 changes: 3 additions & 9 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "depotchest.h"
#include "events.h"
#include "game.h"
#include "inbox.h"
#include "iologindata.h"
#include "monster.h"
#include "movement.h"
Expand Down Expand Up @@ -43,11 +42,8 @@ Player::Player(ProtocolGame_ptr p) :
lastPing(OTSYS_TIME()),
lastPong(lastPing),
client(std::move(p)),
inbox(new Inbox(ITEM_INBOX)),
storeInbox(new StoreInbox(ITEM_STORE_INBOX))
{
inbox->incrementReferenceCounter();

storeInbox->setParent(this);
storeInbox->incrementReferenceCounter();
}
Expand All @@ -62,11 +58,9 @@ Player::~Player()
}

if (depotLocker) {
depotLocker->removeInbox(inbox);
depotLocker->removeInbox(inbox.get());
}

inbox->decrementReferenceCounter();

storeInbox->setParent(nullptr);
storeInbox->decrementReferenceCounter();

Expand Down Expand Up @@ -840,7 +834,7 @@ DepotLocker& Player::getDepotLocker()
if (!depotLocker) {
depotLocker = std::make_shared<DepotLocker>(ITEM_LOCKER);
depotLocker->internalAddThing(Item::CreateItem(ITEM_MARKET));
depotLocker->internalAddThing(inbox);
depotLocker->internalAddThing(getInbox().get());

DepotChest* depotChest = new DepotChest(ITEM_DEPOT, false);
// adding in reverse to align them from first to last
Expand Down Expand Up @@ -3215,7 +3209,7 @@ void Player::postRemoveNotification(Thing* thing, const Cylinder* newParent, int
autoCloseContainers(container);
}
} else if (const Inbox* inboxContainer = dynamic_cast<const Inbox*>(topContainer)) {
if (inboxContainer == inbox) {
if (inboxContainer == inbox.get()) {
onSendContainer(container);
} else {
autoCloseContainers(container);
Expand Down
11 changes: 9 additions & 2 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "enums.h"
#include "groups.h"
#include "guild.h"
#include "inbox.h"
#include "protocolgame.h"
#include "town.h"
#include "vocation.h"
Expand Down Expand Up @@ -182,7 +183,13 @@ class Player final : public Creature, public Cylinder
void setLastWalkthroughAttempt(int64_t walkthroughAttempt) { lastWalkthroughAttempt = walkthroughAttempt; }
void setLastWalkthroughPosition(Position walkthroughPosition) { lastWalkthroughPosition = walkthroughPosition; }

Inbox* getInbox() const { return inbox; }
Inbox_ptr getInbox()
{
if (!inbox) {
inbox = std::make_shared<Inbox>(ITEM_INBOX);
}
return inbox;
}

StoreInbox* getStoreInbox() const { return storeInbox; }

Expand Down Expand Up @@ -1211,7 +1218,7 @@ class Player final : public Creature, public Cylinder
Guild_ptr guild = nullptr;
GuildRank_ptr guildRank = nullptr;
Group* group = nullptr;
Inbox* inbox;
Inbox_ptr inbox = nullptr;
Item* tradeItem = nullptr;
Item* inventory[CONST_SLOT_LAST + 1] = {};
Item* writeItem = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2100,7 +2100,7 @@ void ProtocolGame::sendMarketEnter()
player->setInMarket(true);

std::map<uint16_t, uint32_t> depotItems;
std::forward_list<Container*> containerList{player->getInbox()};
std::forward_list<Container*> containerList{player->getInbox().get()};

for (const auto& chest : player->depotChests) {
if (!chest.second->empty()) {
Expand Down
Loading