Skip to content

Commit 5564640

Browse files
committed
refactor(Packet): Use well defined packet structs when parsing client packets
1 parent abebf5b commit 5564640

108 files changed

Lines changed: 6948 additions & 3435 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/game/AccountMgr.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ AccountMgr::~AccountMgr()
4444

4545
AccountOpResult AccountMgr::CreateAccount(std::string username, std::string password)
4646
{
47-
if (utf8length(username) > MAX_ACCOUNT_STR)
47+
if (utf8length(username).value_or(MAX_ACCOUNT_STR + 1) > MAX_ACCOUNT_STR)
4848
return AOR_NAME_TOO_LONG; // username's too long
4949

5050
normalizeString(username);
@@ -119,10 +119,10 @@ AccountOpResult AccountMgr::ChangeUsername(uint32 accid, std::string new_uname,
119119
if (!result)
120120
return AOR_NAME_NOT_EXIST; // account doesn't exist
121121

122-
if (utf8length(new_uname) > MAX_ACCOUNT_STR)
122+
if (utf8length(new_uname).value_or(MAX_ACCOUNT_STR + 1) > MAX_ACCOUNT_STR)
123123
return AOR_NAME_TOO_LONG;
124124

125-
if (utf8length(new_passwd) > MAX_ACCOUNT_STR)
125+
if (utf8length(new_passwd).value_or(MAX_ACCOUNT_STR + 1) > MAX_ACCOUNT_STR)
126126
return AOR_PASS_TOO_LONG;
127127

128128
normalizeString(new_uname);
@@ -160,7 +160,7 @@ AccountOpResult AccountMgr::ChangePassword(uint32 accid, std::string new_passwd,
160160
else
161161
normalizeString(username); // account doesn't exist
162162

163-
if (utf8length(new_passwd) > MAX_ACCOUNT_STR)
163+
if (utf8length(new_passwd).value_or(MAX_ACCOUNT_STR + 1) > MAX_ACCOUNT_STR)
164164
return AOR_PASS_TOO_LONG;
165165

166166
normalizeString(new_passwd);
@@ -376,7 +376,7 @@ void AccountMgr::LoadAccountBanList(bool silent)
376376
sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, "Loading account_banned ...");
377377

378378
std::unique_ptr<QueryResult> banresult(LoginDatabase.PQuery("SELECT `id`, `unbandate`, `bandate` FROM `account_banned` WHERE `active` = 1 AND (`unbandate` > UNIX_TIMESTAMP() OR `bandate` = `unbandate`)"));
379-
379+
380380
if (!banresult)
381381
{
382382
if (!silent)
@@ -539,7 +539,7 @@ AccountPersistentData& AccountMgr::GetAccountPersistentData(uint32 accountId)
539539
if (itr != m_accountPersistentData.end())
540540
return itr->second;
541541
}
542-
542+
543543
{
544544
std::lock_guard<std::shared_timed_mutex> guard(m_accountPersistentDataMutex);
545545
return m_accountPersistentData[accountId];

src/game/Anticheat/WardenAnticheat/Warden.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ void Warden::ApplyPenalty(std::string message, WardenActions penalty, std::share
502502
});
503503
}
504504

505-
void Warden::HandlePacket(WorldPacket& recvData)
505+
void Warden::HandlePacket(ByteBuffer recvData)
506506
{
507507
// initialize decrypt packet
508508
DecryptData(const_cast<uint8*>(recvData.contents()), recvData.size());
@@ -658,18 +658,20 @@ void Warden::HandlePacket(WorldPacket& recvData)
658658
void Warden::Update()
659659
{
660660
{
661-
std::vector<WorldPacket> packetQueue;
661+
std::queue<std::vector<uint8>> packetQueue;
662662

663663
{
664-
std::lock_guard<std::mutex> lock(m_packetQueueMutex);
665-
std::swap(packetQueue, m_packetQueue);
664+
std::lock_guard<std::mutex> lock(m_packetDataQueueMutex);
665+
std::swap(packetQueue, m_packetDataQueue);
666666
}
667667

668-
for (auto& packet : packetQueue)
668+
while (packetQueue.size())
669669
{
670+
std::vector<uint8> packetData = std::move(packetQueue.front());
671+
packetQueue.pop();
670672
try
671673
{
672-
HandlePacket(packet);
674+
HandlePacket(ByteBuffer::from(std::move(packetData)));
673675
}
674676
catch (ByteBufferException &)
675677
{

src/game/Anticheat/WardenAnticheat/Warden.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class Warden
169169

170170
static void LoadScriptedScans();
171171

172-
void HandlePacket(WorldPacket& recvData);
172+
void HandlePacket(ByteBuffer recvData);
173173

174174
bool IsUsingMaiev() const { return m_maiev; }
175175
WardenModule const* GetModule() const { return m_module; }
@@ -183,8 +183,8 @@ class Warden
183183
virtual void GetPlayerInfo(std::string& clock, std::string& fingerprint, std::string& hypervisors,
184184
std::string& renderer, std::string& proxifier) const = 0;
185185

186-
std::vector<WorldPacket> m_packetQueue;
187-
std::mutex m_packetQueueMutex;
186+
std::queue<std::vector<uint8>> m_packetDataQueue;
187+
std::mutex m_packetDataQueueMutex;
188188

189189
// used by maiev string hash check
190190
mutable std::string m_hashString;

src/game/AuctionHouse/AuctionHouseMgr.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ struct AuctionHouseClientQuery
100100
uint8 levelmax;
101101
uint8 usable;
102102
uint32 listfrom, auctionSlotID, auctionMainCategory, auctionSubCategory, quality;
103-
uint32 outbiddedCount;
104103
std::vector<uint32> outbiddedAuctionIds;
105104
};
106105

src/game/Battlegrounds/BattleGround.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "Formulas.h"
3737
#include "GridNotifiersImpl.h"
3838
#include "Chat.h"
39+
#include "ScriptMgr.h"
3940

4041
namespace MaNGOS
4142
{
@@ -690,7 +691,7 @@ void BattleGround::EndBattleGround(Team winner)
690691
if (team == winner)
691692
RewardMark(pPlayer, true);
692693
// World of Warcraft Client Patch 1.8.4 (2005-12-06)
693-
// - Battles must now last at least ten minutes after the start of the
694+
// - Battles must now last at least ten minutes after the start of the
694695
// battle in order for the losing team to receive a Mark of honor.
695696
else if (GetStartTime() > 10 * MINUTE * IN_MILLISECONDS)
696697
RewardMark(pPlayer, false);
@@ -1199,7 +1200,7 @@ void BattleGround::DecreaseInvitedCount(Team team)
11991200
}
12001201
}
12011202
void BattleGround::IncreaseInvitedCount(Team team)
1202-
{
1203+
{
12031204
switch (team)
12041205
{
12051206
case ALLIANCE:
@@ -1283,7 +1284,7 @@ bool BattleGround::AddObject(uint32 type, uint32 entry, float x, float y, float
12831284
delete go;
12841285
return false;
12851286
}
1286-
1287+
12871288
// add to world, so it can be later looked up from HashMapHolder
12881289
go->AddToWorld();
12891290
m_bgObjects[type] = go->GetObjectGuid();

src/game/CMakeLists.txt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,36 @@ set (game_SRCS
196196
PlayerBots/PlayerBotAI.cpp
197197
PlayerBots/PlayerBotMgr.cpp
198198
Server/Protocol/Opcodes.cpp
199+
Server/Packets/Battleground.cpp
200+
Server/Packets/AuctionHouse.cpp
201+
Server/Packets/Channel.cpp
202+
Server/Packets/Character.cpp
203+
Server/Packets/Chat.cpp
204+
Server/Packets/Combat.cpp
205+
Server/Packets/Duel.cpp
206+
Server/Packets/GmTicket.cpp
207+
Server/Packets/Group.cpp
208+
Server/Packets/Guild.cpp
209+
Server/Packets/Item.cpp
210+
Server/Packets/Loot.cpp
211+
Server/Packets/Mail.cpp
212+
Server/Packets/Misc.cpp
213+
Server/Packets/Movement.cpp
214+
Server/Packets/Npc.cpp
215+
Server/Packets/Pet.cpp
216+
Server/Packets/Petition.cpp
217+
Server/Packets/Query.cpp
218+
Server/Packets/Quest.cpp
219+
Server/Packets/Skill.cpp
220+
Server/Packets/Spell.cpp
221+
Server/Packets/Taxi.cpp
222+
Server/Packets/Trade.cpp
199223
Server/WorldSession.cpp
200224
Server/WorldSocket.cpp
201225
Server/WorldSocketMgr.cpp
202226
Spells/Spell.cpp
203227
Spells/SpellAuras.cpp
228+
Spells/SpellCastTargetsInfo.cpp
204229
Spells/SpellEffects.cpp
205230
Spells/SpellEntry.cpp
206231
Spells/SpellMgr.cpp
@@ -419,13 +444,40 @@ set (game_SRCS
419444
PlayerBots/PlayerBotAI.h
420445
PlayerBots/PlayerBotMgr.h
421446
Server/Protocol/Opcodes.h
447+
Server/Protocol/Opcodes_active.h
448+
Server/Packet.h
449+
Server/Packets/Battleground.h
450+
Server/Packets/AuctionHouse.h
451+
Server/Packets/Channel.h
452+
Server/Packets/Character.h
453+
Server/Packets/Chat.h
454+
Server/Packets/Combat.h
455+
Server/Packets/Duel.h
456+
Server/Packets/GmTicket.h
457+
Server/Packets/Group.h
458+
Server/Packets/Guild.h
459+
Server/Packets/Item.h
460+
Server/Packets/Loot.h
461+
Server/Packets/Mail.h
462+
Server/Packets/Misc.h
463+
Server/Packets/Movement.h
464+
Server/Packets/Npc.h
465+
Server/Packets/Pet.h
466+
Server/Packets/Petition.h
467+
Server/Packets/Query.h
468+
Server/Packets/Quest.h
469+
Server/Packets/Skill.h
470+
Server/Packets/Spell.h
471+
Server/Packets/Taxi.h
472+
Server/Packets/Trade.h
422473
Server/WorldPacket.h
423474
Server/WorldSession.h
424475
Server/WorldSocket.h
425476
Server/WorldSocketMgr.h
426477
Spells/Spell.h
427478
Spells/SpellAuraDefines.h
428479
Spells/SpellAuras.h
480+
Spells/SpellCastTargetsInfo.h
429481
Spells/SpellClassMask.h
430482
Spells/SpellDefines.h
431483
Spells/SpellEntry.h

src/game/Chat/Chat.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ ChatCommand * ChatHandler::getCommandTable()
11401140
{ "", SEC_TICKETMASTER, true, &ChatHandler::HandleGMTicketGetByIdOrNameCommand, "", nullptr },
11411141
{ nullptr, 0, false, nullptr, "", nullptr }
11421142
};
1143-
1143+
11441144
static ChatCommand serviceCommandTable[] =
11451145
{
11461146
{ "del_characters", SEC_ADMINISTRATOR, true, &ChatHandler::HandleServiceDeleteCharacters, "", nullptr },
@@ -1411,7 +1411,7 @@ void ChatHandler::LoadRbacPermissions()
14111411
sLog.Out(LOG_DBERROR, LOG_LVL_ERROR, "Unknown RBAC permission id %u assigned to command '%s'!", permissionId, command.c_str());
14121412
continue;
14131413
}
1414-
1414+
14151415
SetPermissionMaskForCommandInTable(commandTable, command.c_str(), permissionId);
14161416

14171417
} while (result->NextRow());
@@ -2020,7 +2020,7 @@ bool ChatHandler::SetDataForCommandInTable(ChatCommand *commandTable, char const
20202020
return false;
20212021
}
20222022

2023-
bool ChatHandler::ParseCommands(char const* text)
2023+
ParseCommandResult ChatHandler::ParseCommands(char const* text)
20242024
{
20252025
MANGOS_ASSERT(text);
20262026
MANGOS_ASSERT(*text);
@@ -2029,19 +2029,19 @@ bool ChatHandler::ParseCommands(char const* text)
20292029
if (m_session)
20302030
{
20312031
if (text[0] != '!' && text[0] != '.')
2032-
return false;
2032+
return ParseCommandResult::WasNotHandled;
20332033

20342034
// ignore single . and ! in line
20352035
if (strlen(text) < 2)
2036-
return false;
2036+
return ParseCommandResult::WasNotHandled;
20372037

20382038
if (m_session->GetSecurity() == SEC_PLAYER && !sWorld.getConfig(CONFIG_BOOL_PLAYER_COMMANDS))
2039-
return false;
2039+
return ParseCommandResult::WasNotHandled;
20402040
}
20412041

20422042
// ignore messages staring from many dots.
20432043
if ((text[0] == '.' && text[1] == '.') || (text[0] == '!' && text[1] == '!'))
2044-
return false;
2044+
return ParseCommandResult::WasNotHandled;
20452045

20462046
// skip first . or ! (in console allowed use command with . and ! and without its)
20472047
if (text[0] == '!' || text[0] == '.')
@@ -2073,7 +2073,7 @@ bool ChatHandler::ParseCommands(char const* text)
20732073
else
20742074
ExecuteCommand(text);
20752075

2076-
return true;
2076+
return ParseCommandResult::CommandDetectedAndHandled;
20772077
}
20782078

20792079
bool ChatHandler::ShowHelpForSubCommands(ChatCommand *table, char const* cmd)
@@ -2160,7 +2160,7 @@ bool ChatHandler::ShowHelpForCommand(ChatCommand *table, char const* cmd)
21602160
return command || childCommands;
21612161
}
21622162

2163-
bool ChatHandler::isValidChatMessage(char const* message)
2163+
bool ChatHandler::isValidChatMessage(std::string const& msg)
21642164
{
21652165
/*
21662166
@@ -2174,9 +2174,11 @@ bool ChatHandler::isValidChatMessage(char const* message)
21742174
| will be escaped to ||
21752175
*/
21762176

2177-
if (strlen(message) > 255)
2177+
if (msg.length() > 255)
21782178
return false;
21792179

2180+
char const* message = msg.c_str();
2181+
21802182
char const validSequence[6] = "cHhhr";
21812183
char const* validSequenceIterator = validSequence;
21822184

src/game/Chat/Chat.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ enum PlayerChatTag
9191
CHAT_TAG_GM = 3,
9292
};
9393

94+
enum class ParseCommandResult
95+
{
96+
CommandDetectedAndHandled,
97+
WasNotHandled,
98+
};
99+
94100
class PartyBotAI;
95101
class BattleBotAI;
96102

@@ -119,10 +125,10 @@ class ChatHandler
119125
void PSendSysMessage(int32 entry, ... );
120126
std::string PGetParseString(int32 entry, ...);
121127

122-
bool ParseCommands(char const* text);
128+
ParseCommandResult ParseCommands(char const* text);
123129
ChatCommand const* FindCommand(char const* text);
124130

125-
bool isValidChatMessage(char const* msg);
131+
bool isValidChatMessage(std::string const& msg);
126132
bool HasSentErrorMessage() { return m_sentErrorMessage;}
127133

128134
std::string playerLink(std::string const& name) const { return m_session ? "|cffffffff|Hplayer:"+name+"|h["+name+"]|h|r" : name; }
@@ -189,7 +195,7 @@ class ChatHandler
189195
void CheckIntegrity(ChatCommand *table, ChatCommand *parentCommand);
190196
static void FillFullCommandsName(ChatCommand* table, std::string prefix);
191197
static ChatCommand* getCommandTable();
192-
198+
193199
bool HandleAnticheatCommand(char*);
194200
bool HandleReloadAnticheatCommand(char*);
195201
bool HandleViewLogCommand(char*);
@@ -390,8 +396,8 @@ class ChatHandler
390396
bool HandleDebugControlCommand(char *args);
391397
bool HandlePvPCommand(char *args);
392398
// Channel
393-
bool HandleChannelJoinCommand(char*);
394-
bool HandleChannelLeaveCommand(char*);
399+
bool HandleChannelJoinCommand(char* args);
400+
bool HandleChannelLeaveCommand(char* args);
395401

396402
bool HandleAccountCommand(char* args);
397403
bool HandleAccountCharactersCommand(char* args);
@@ -471,7 +477,7 @@ class ChatHandler
471477
bool HandleDebugPlaySoundCommand(char* args);
472478
bool HandleDebugPlayScriptText(char* args);
473479
bool HandleDebugPlayMusicCommand(char* args);
474-
480+
475481
bool HandleDebugSendBuyErrorCommand(char* args);
476482
bool HandleDebugSendChannelNotifyCommand(char* args);
477483
bool HandleDebugSendChatMsgCommand(char* args);

0 commit comments

Comments
 (0)