diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2023-05-15 16:09:09 +0200 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2023-05-15 16:09:15 +0200 |
commit | 1992ce920eb5268be9487b3bba6d28353d871111 (patch) | |
tree | 0674c82a0726135c0a5d136a359f841d503c510f /src/account-server/accounthandler.cpp | |
parent | f395960adeea1f51f01ec8045d1e175926a6ea4a (diff) | |
download | manaserv-1992ce920eb5268be9487b3bba6d28353d871111.tar.gz manaserv-1992ce920eb5268be9487b3bba6d28353d871111.tar.bz2 manaserv-1992ce920eb5268be9487b3bba6d28353d871111.tar.xz manaserv-1992ce920eb5268be9487b3bba6d28353d871111.zip |
Manage CharacterData using std::unique_ptr
Fixes many memory leaks, but also made it clear that we're very often
loading all the character data only to immediately throw it away again,
even when most of the time all we really need is the database ID or the
name.
Diffstat (limited to 'src/account-server/accounthandler.cpp')
-rw-r--r-- | src/account-server/accounthandler.cpp | 43 |
1 files changed, 22 insertions, 21 deletions
diff --git a/src/account-server/accounthandler.cpp b/src/account-server/accounthandler.cpp index 9cb32af1..6c1e10c8 100644 --- a/src/account-server/accounthandler.cpp +++ b/src/account-server/accounthandler.cpp @@ -281,17 +281,17 @@ void AccountHandler::computerDisconnected(NetComputer *comp) delete client; // ~AccountClient unsets the account } -static void sendCharacterData(MessageOut &charInfo, const CharacterData *ch) +static void sendCharacterData(MessageOut &charInfo, const CharacterData &ch) { - charInfo.writeInt8(ch->getCharacterSlot()); - charInfo.writeString(ch->getName()); - charInfo.writeInt8(ch->getGender()); - charInfo.writeInt8(ch->getHairStyle()); - charInfo.writeInt8(ch->getHairColor()); - charInfo.writeInt16(ch->getAttributePoints()); - charInfo.writeInt16(ch->getCorrectionPoints()); - - auto &possessions = ch->getPossessions(); + charInfo.writeInt8(ch.getCharacterSlot()); + charInfo.writeString(ch.getName()); + charInfo.writeInt8(ch.getGender()); + charInfo.writeInt8(ch.getHairStyle()); + charInfo.writeInt8(ch.getHairColor()); + charInfo.writeInt16(ch.getAttributePoints()); + charInfo.writeInt16(ch.getCorrectionPoints()); + + auto &possessions = ch.getPossessions(); auto &equipData = possessions.getEquipment(); auto &inventoryData = possessions.getInventory(); charInfo.writeInt8(equipData.size()); @@ -303,8 +303,8 @@ static void sendCharacterData(MessageOut &charInfo, const CharacterData *ch) charInfo.writeInt16(it->second.itemId); } - charInfo.writeInt8(ch->getAttributes().size()); - for (auto &it : ch->getAttributes()) + charInfo.writeInt8(ch.getAttributes().size()); + for (auto &it : ch.getAttributes()) { // {id, base value in 256ths, modified value in 256ths }* charInfo.writeInt32(it.first); @@ -318,7 +318,7 @@ static void sendFullCharacterData(AccountClient *client, { MessageOut msg(APMSG_CHAR_INFO); for (auto &charIt : chars) - sendCharacterData(msg, charIt.second); + sendCharacterData(msg, *charIt.second); client->send(msg); } @@ -501,7 +501,7 @@ void AccountHandler::handleLoginMessage(AccountClient &client, MessageIn &msg) sendFullCharacterData(&client, chars); } else { for (auto &charIt : chars) - sendCharacterData(reply, charIt.second); + sendCharacterData(reply, *charIt.second); client.send(reply); } @@ -853,7 +853,7 @@ void AccountHandler::handleCharacterCreateMessage(AccountClient &client, } else { - auto newCharacter = new CharacterData(name); + auto newCharacter = std::make_unique<CharacterData>(name); // Set the initial attributes provided by the client for (unsigned i = 0; i < mModifiableAttributes.size(); ++i) @@ -873,7 +873,11 @@ void AccountHandler::handleCharacterCreateMessage(AccountClient &client, Point startingPos(Configuration::getValue("char_startX", 1024), Configuration::getValue("char_startY", 1024)); newCharacter->setPosition(startingPos); - acc->addCharacter(newCharacter); + + reply.writeInt8(ERRMSG_OK); + sendCharacterData(reply, *newCharacter); + + acc->addCharacter(std::move(newCharacter)); LOG_INFO("Character " << name << " was created for " << acc->getName() << "'s account."); @@ -882,15 +886,12 @@ void AccountHandler::handleCharacterCreateMessage(AccountClient &client, // log transaction Transaction trans; - trans.mCharacterId = newCharacter->getDatabaseID(); + trans.mCharacterId = acc->getCharacter(slot)->getDatabaseID(); trans.mAction = TRANS_CHAR_CREATE; trans.mMessage = acc->getName() + " created character "; trans.mMessage.append("called " + name); storage->addTransaction(trans); - reply.writeInt8(ERRMSG_OK); - - sendCharacterData(reply, newCharacter); client.send(reply); return; } @@ -1116,7 +1117,7 @@ void AccountHandler::handleStellarLogin(const std::string &token, const std::str addServerInfo(reply); for (auto &charIt : acc->getCharacters()) - sendCharacterData(reply, charIt.second); + sendCharacterData(reply, *charIt.second); client->send(reply); |