From 18597cb4a371ac2e3cc1dcb6bfc944bd6d7a15ee Mon Sep 17 00:00:00 2001 From: scatterflower Date: Thu, 8 Apr 2021 14:06:27 -0500 Subject: [PATCH] fix incorrectly setting char_id when argument is invalid --- include/aoclient.h | 2 +- src/aoclient.cpp | 7 ++++--- src/packets.cpp | 5 +++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/include/aoclient.h b/include/aoclient.h index 95a1b2b..e99d417 100644 --- a/include/aoclient.h +++ b/include/aoclient.h @@ -321,7 +321,7 @@ class AOClient : public QObject { * * @param char_id The character ID of the client's new character. */ - void changeCharacter(int char_id); + bool changeCharacter(int char_id); /** * @brief Changes the client's in-character position. diff --git a/src/aoclient.cpp b/src/aoclient.cpp index 6dbf76d..d9599b5 100644 --- a/src/aoclient.cpp +++ b/src/aoclient.cpp @@ -134,18 +134,18 @@ void AOClient::changeArea(int new_area) sendServerMessage("Area " + server->area_names[current_area] + " is spectate-only; to chat IC you will need to be invited by the CM."); } -void AOClient::changeCharacter(int char_id) +bool AOClient::changeCharacter(int char_id) { AreaData* area = server->areas[current_area]; if(char_id >= server->characters.length()) - return; + return false; if (char_id >= 0) { QString char_selected = server->characters[char_id]; bool taken = area->characters_taken.contains(char_id); if (taken || char_selected == "") - return; + return false; if (current_char != "") { area->characters_taken.removeAll(server->getCharID(current_char)); @@ -162,6 +162,7 @@ void AOClient::changeCharacter(int char_id) server->updateCharsTaken(area); sendPacket("PV", {QString::number(id), "CID", QString::number(char_id)}); + return true; } void AOClient::changePosition(QString new_pos) diff --git a/src/packets.cpp b/src/packets.cpp index 92e0906..9301e55 100644 --- a/src/packets.cpp +++ b/src/packets.cpp @@ -143,13 +143,14 @@ void AOClient::pktCharPassword(AreaData* area, int argc, QStringList argv, AOPac void AOClient::pktSelectChar(AreaData* area, int argc, QStringList argv, AOPacket packet) { bool argument_ok; - char_id = argv[1].toInt(&argument_ok); + int selected_char_id = argv[1].toInt(&argument_ok); if (!argument_ok) { char_id = -1; return; } - changeCharacter(char_id); + if (changeCharacter(selected_char_id)) + char_id = selected_char_id; } void AOClient::pktIcChat(AreaData* area, int argc, QStringList argv, AOPacket packet)