From ca69237d59aff013e1da17157627b2b4e7eb5aed Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Wed, 14 Dec 2022 10:16:46 +0000 Subject: [PATCH] Big-endian support for multiplayer Not sure if this works but it's a start --- Source/msg.cpp | 487 +++++++++++++++++++++++----------------- Source/multi.cpp | 64 +++--- Source/sync.cpp | 34 +-- Source/utils/endian.hpp | 8 + 4 files changed, 339 insertions(+), 254 deletions(-) diff --git a/Source/msg.cpp b/Source/msg.cpp index d708d7d16..8f5409a13 100644 --- a/Source/msg.cpp +++ b/Source/msg.cpp @@ -311,7 +311,8 @@ bool WasPlayerCmdAlreadyRequested(_cmd_id bCmd, Point position = {}, uint16_t wP return false; } - TCmdLocParam4 newSendParam = { bCmd, static_cast(position.x), static_cast(position.y), wParam1, wParam2, wParam3, wParam4 }; + TCmdLocParam4 newSendParam = { bCmd, static_cast(position.x), static_cast(position.y), + SDL_SwapLE16(wParam1), SDL_SwapLE16(wParam2), SDL_SwapLE16(wParam3), SDL_SwapLE16(wParam4) }; if (lastSentPlayerCmd.bCmd == newSendParam.bCmd && lastSentPlayerCmd.x == newSendParam.x && lastSentPlayerCmd.y == newSendParam.y && lastSentPlayerCmd.wParam1 == newSendParam.wParam1 && lastSentPlayerCmd.wParam2 == newSendParam.wParam2 && lastSentPlayerCmd.wParam3 == newSendParam.wParam3 && lastSentPlayerCmd.wParam4 == newSendParam.wParam4) { @@ -355,7 +356,7 @@ void PrePacket() auto *cmd = (TFakeDropPlr *)data; data += sizeof(*cmd); spaceLeft -= sizeof(*cmd); - multi_player_left(cmd->bPlr, cmd->dwReason); + multi_player_left(cmd->bPlr, SDL_SwapLE32(cmd->dwReason)); continue; } @@ -599,10 +600,12 @@ void DeltaImportData(_cmd_id cmd, uint32_t recvOffset) size_t OnLevelData(int pnum, const TCmd *pCmd) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t wBytes = SDL_SwapLE16(message.wBytes); + const uint16_t wOffset = SDL_SwapLE16(message.wOffset); if (gbDeltaSender != pnum) { - if (message.bCmd != CMD_DLEVEL_END && (message.bCmd != CMD_DLEVEL || message.wOffset != 0)) { - return message.wBytes + sizeof(message); + if (message.bCmd != CMD_DLEVEL_END && (message.bCmd != CMD_DLEVEL || wOffset != 0)) { + return wBytes + sizeof(message); } gbDeltaSender = pnum; @@ -612,29 +615,29 @@ size_t OnLevelData(int pnum, const TCmd *pCmd) if (sgbRecvCmd == CMD_DLEVEL_END) { if (message.bCmd == CMD_DLEVEL_END) { sgbDeltaChunks = MAX_CHUNKS - 1; - return message.wBytes + sizeof(message); + return wBytes + sizeof(message); } - if (message.bCmd != CMD_DLEVEL || message.wOffset != 0) { - return message.wBytes + sizeof(message); + if (message.bCmd != CMD_DLEVEL || wOffset != 0) { + return wBytes + sizeof(message); } sgdwRecvOffset = 0; sgbRecvCmd = message.bCmd; - } else if (sgbRecvCmd != message.bCmd || message.wOffset == 0) { + } else if (sgbRecvCmd != message.bCmd || wOffset == 0) { DeltaImportData(sgbRecvCmd, sgdwRecvOffset); if (message.bCmd == CMD_DLEVEL_END) { sgbDeltaChunks = MAX_CHUNKS - 1; sgbRecvCmd = CMD_DLEVEL_END; - return message.wBytes + sizeof(message); + return wBytes + sizeof(message); } sgdwRecvOffset = 0; sgbRecvCmd = message.bCmd; } - assert(message.wOffset == sgdwRecvOffset); - memcpy(&sgRecvBuf[message.wOffset], &message + 1, message.wBytes); - sgdwRecvOffset += message.wBytes; - return message.wBytes + sizeof(message); + assert(wOffset == sgdwRecvOffset); + memcpy(&sgRecvBuf[wOffset], &message + 1, wBytes); + sgdwRecvOffset += wBytes; + return wBytes + sizeof(message); } void DeltaSyncGolem(const TCmdGolem &message, int pnum, uint8_t level) @@ -649,7 +652,7 @@ void DeltaSyncGolem(const TCmdGolem &message, int pnum, uint8_t level) monster._mactive = UINT8_MAX; monster._menemy = message._menemy; monster._mdir = message._mdir; - monster.hitPoints = message._mhitpoints; + monster.hitPoints = SDL_SwapLE32(message._mhitpoints); } void DeltaLeaveSync(uint8_t bLevel) @@ -701,8 +704,10 @@ bool DeltaGetItem(const TCmdGItem &message, uint8_t bLevel) DLevel &deltaLevel = GetDeltaLevel(bLevel); for (TCmdPItem &item : deltaLevel.item) { - if (item.bCmd == CMD_INVALID || item.def.wIndx != message.def.wIndx || item.def.wCI != message.def.wCI || item.def.dwSeed != message.def.dwSeed) + if (item.bCmd == CMD_INVALID || item.def.wIndx != message.def.wIndx + || item.def.wCI != message.def.wCI || item.def.dwSeed != message.def.dwSeed) { continue; + } if (item.bCmd == TCmdPItem::PickedUpItem) { return true; @@ -836,8 +841,8 @@ void NetSendCmdGItem2(bool usonly, _cmd_id bCmd, uint8_t mast, uint8_t pnum, con int ticks = SDL_GetTicks(); if (cmd.dwTime == 0) { - cmd.dwTime = ticks; - } else if (ticks - cmd.dwTime > 5000) { + cmd.dwTime = SDL_SwapLE32(ticks); + } else if (ticks - SDL_SwapLE32(cmd.dwTime) > 5000) { return; } @@ -855,8 +860,8 @@ bool NetSendCmdReq2(_cmd_id bCmd, uint8_t mast, uint8_t pnum, const TCmdGItem &i int ticks = SDL_GetTicks(); if (cmd.dwTime == 0) - cmd.dwTime = ticks; - else if (ticks - cmd.dwTime > 5000) + cmd.dwTime = SDL_SwapLE32(ticks); + else if (ticks - SDL_SwapLE32(cmd.dwTime) > 5000) return false; tmsg_add((byte *)&cmd, sizeof(cmd)); @@ -895,7 +900,7 @@ size_t OnAddStrength(const TCmd *pCmd, size_t pnum) if (gbBufferMsgs == 1) SendPacket(pnum, &message, sizeof(message)); else if (message.wParam1 <= 256) - ModifyPlrStr(Players[pnum], message.wParam1); + ModifyPlrStr(Players[pnum], SDL_SwapLE16(message.wParam1)); return sizeof(message); } @@ -907,7 +912,7 @@ size_t OnAddMagic(const TCmd *pCmd, size_t pnum) if (gbBufferMsgs == 1) SendPacket(pnum, &message, sizeof(message)); else if (message.wParam1 <= 256) - ModifyPlrMag(Players[pnum], message.wParam1); + ModifyPlrMag(Players[pnum], SDL_SwapLE16(message.wParam1)); return sizeof(message); } @@ -919,7 +924,7 @@ size_t OnAddDexterity(const TCmd *pCmd, int pnum) if (gbBufferMsgs == 1) SendPacket(pnum, &message, sizeof(message)); else if (message.wParam1 <= 256) - ModifyPlrDex(Players[pnum], message.wParam1); + ModifyPlrDex(Players[pnum], SDL_SwapLE16(message.wParam1)); return sizeof(message); } @@ -931,7 +936,7 @@ size_t OnAddVitality(const TCmd *pCmd, size_t pnum) if (gbBufferMsgs == 1) SendPacket(pnum, &message, sizeof(message)); else if (message.wParam1 <= 256) - ModifyPlrVit(Players[pnum], message.wParam1); + ModifyPlrVit(Players[pnum], SDL_SwapLE16(message.wParam1)); return sizeof(message); } @@ -941,10 +946,10 @@ size_t OnGotoGetItem(const TCmd *pCmd, Player &player) const auto &message = *reinterpret_cast(pCmd); const Point position { message.x, message.y }; - if (gbBufferMsgs != 1 && player.isOnActiveLevel() && InDungeonBounds(position) && message.wParam1 < MAXITEMS + 1) { + if (gbBufferMsgs != 1 && player.isOnActiveLevel() && InDungeonBounds(position) && SDL_SwapLE16(message.wParam1) < MAXITEMS + 1) { MakePlrPath(player, position, false); player.destAction = ACTION_PICKUPITEM; - player.destParam1 = message.wParam1; + player.destParam1 = SDL_SwapLE16(message.wParam1); } return sizeof(message); @@ -964,7 +969,7 @@ bool IsGItemValid(const TCmdGItem &message) if (!InDungeonBounds({ message.x, message.y })) return false; - return IsItemAvailable(message.def.wIndx); + return IsItemAvailable(static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx))); } bool IsPItemValid(const TCmdPItem &message) @@ -974,7 +979,7 @@ bool IsPItemValid(const TCmdPItem &message) if (!InDungeonBounds(position)) return false; - return IsItemAvailable(message.def.wIndx); + return IsItemAvailable(static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx))); } void PrepareItemForNetwork(const Item &item, TItem &messageItem) @@ -984,14 +989,14 @@ void PrepareItemForNetwork(const Item &item, TItem &messageItem) messageItem.bMDur = item._iMaxDur; messageItem.bCh = item._iCharges; messageItem.bMCh = item._iMaxCharges; - messageItem.wValue = item._ivalue; - messageItem.wToHit = item._iPLToHit; - messageItem.wMaxDam = item._iMaxDam; + messageItem.wValue = SDL_SwapLE16(item._ivalue); + messageItem.wToHit = SDL_SwapLE16(item._iPLToHit); + messageItem.wMaxDam = SDL_SwapLE16(item._iMaxDam); messageItem.bMinStr = item._iMinStr; messageItem.bMinMag = item._iMinMag; messageItem.bMinDex = item._iMinDex; - messageItem.bAC = item._iAC; - messageItem.dwBuff = item.dwBuff; + messageItem.bAC = SDL_SwapLE16(item._iAC); + messageItem.dwBuff = SDL_SwapLE32(item.dwBuff); } void PrepareEarForNetwork(const Item &item, TEar &ear) @@ -1002,9 +1007,9 @@ void PrepareEarForNetwork(const Item &item, TEar &ear) void PrepareItemForNetwork(const Item &item, TCmdGItem &message) { - message.def.wIndx = item.IDidx; - message.def.wCI = item._iCreateInfo; - message.def.dwSeed = item._iSeed; + message.def.wIndx = static_cast<_item_indexes>(SDL_SwapLE16(item.IDidx)); + message.def.wCI = SDL_SwapLE16(item._iCreateInfo); + message.def.dwSeed = SDL_SwapLE32(item._iSeed); if (item.IDidx == IDI_EAR) PrepareEarForNetwork(item, message.ear); @@ -1014,9 +1019,9 @@ void PrepareItemForNetwork(const Item &item, TCmdGItem &message) void PrepareItemForNetwork(const Item &item, TCmdPItem &message) { - message.def.wIndx = item.IDidx; - message.def.wCI = item._iCreateInfo; - message.def.dwSeed = item._iSeed; + message.def.wIndx = static_cast<_item_indexes>(SDL_SwapLE16(item.IDidx)); + message.def.wCI = SDL_SwapLE16(item._iCreateInfo); + message.def.dwSeed = SDL_SwapLE32(item._iSeed); if (item.IDidx == IDI_EAR) PrepareEarForNetwork(item, message.ear); @@ -1026,9 +1031,9 @@ void PrepareItemForNetwork(const Item &item, TCmdPItem &message) void PrepareItemForNetwork(const Item &item, TCmdChItem &message) { - message.def.wIndx = item.IDidx; - message.def.wCI = item._iCreateInfo; - message.def.dwSeed = item._iSeed; + message.def.wIndx = static_cast<_item_indexes>(SDL_SwapLE16(item.IDidx)); + message.def.wCI = SDL_SwapLE16(item._iCreateInfo); + message.def.dwSeed = SDL_SwapLE32(item._iSeed); if (item.IDidx == IDI_EAR) PrepareEarForNetwork(item, message.ear); @@ -1038,34 +1043,37 @@ void PrepareItemForNetwork(const Item &item, TCmdChItem &message) void RecreateItem(const Player &player, const TItem &messageItem, Item &item) { - RecreateItem(player, item, messageItem.wIndx, messageItem.wCI, messageItem.dwSeed, messageItem.wValue, (messageItem.dwBuff & CF_HELLFIRE) != 0); + const uint32_t dwBuff = SDL_SwapLE32(messageItem.dwBuff); + RecreateItem(player, item, + static_cast<_item_indexes>(SDL_SwapLE16(messageItem.wIndx)), SDL_SwapLE16(messageItem.wCI), + SDL_SwapLE32(messageItem.dwSeed), SDL_SwapLE16(messageItem.wValue), (dwBuff & CF_HELLFIRE) != 0); if (messageItem.bId != 0) item._iIdentified = true; item._iDurability = messageItem.bDur; item._iMaxDur = messageItem.bMDur; item._iCharges = messageItem.bCh; item._iMaxCharges = messageItem.bMCh; - item._iPLToHit = messageItem.wToHit; - item._iMaxDam = messageItem.wMaxDam; + item._iPLToHit = SDL_SwapLE16(messageItem.wToHit); + item._iMaxDam = SDL_SwapLE16(messageItem.wMaxDam); item._iMinStr = messageItem.bMinStr; item._iMinMag = messageItem.bMinMag; item._iMinDex = messageItem.bMinDex; - item._iAC = messageItem.bAC; - item.dwBuff = messageItem.dwBuff; + item._iAC = SDL_SwapLE16(messageItem.bAC); + item.dwBuff = dwBuff; } void RecreateItem(const Player &player, const TCmdPItem &message, Item &item) { - if (message.def.wIndx == IDI_EAR) - RecreateEar(item, message.ear.wCI, message.ear.dwSeed, message.ear.bCursval, message.ear.heroname); + if (message.def.wIndx == SDL_SwapLE16(IDI_EAR)) + RecreateEar(item, SDL_SwapLE16(message.ear.wCI), SDL_SwapLE32(message.ear.dwSeed), message.ear.bCursval, message.ear.heroname); else RecreateItem(player, message.item, item); } void RecreateItem(const Player &player, const TCmdChItem &message, Item &item) { - if (message.def.wIndx == IDI_EAR) - RecreateEar(item, message.ear.wCI, message.ear.dwSeed, message.ear.bCursval, message.ear.heroname); + if (message.def.wIndx == SDL_SwapLE16(IDI_EAR)) + RecreateEar(item, SDL_SwapLE16(message.ear.wCI), SDL_SwapLE32(message.ear.dwSeed), message.ear.bCursval, message.ear.heroname); else RecreateItem(player, message.item, item); } @@ -1074,30 +1082,30 @@ int SyncDropItem(Point position, const TItem &item) { return SyncDropItem( position, - item.wIndx, - item.wCI, - item.dwSeed, + static_cast<_item_indexes>(SDL_SwapLE16(item.wIndx)), + SDL_SwapLE16(item.wCI), + SDL_SwapLE32(item.dwSeed), item.bId, item.bDur, item.bMDur, item.bCh, item.bMCh, - item.wValue, - item.dwBuff, - item.wToHit, - item.wMaxDam, + SDL_SwapLE16(item.wValue), + SDL_SwapLE32(item.dwBuff), + SDL_SwapLE16(item.wToHit), + SDL_SwapLE16(item.wMaxDam), item.bMinStr, item.bMinMag, item.bMinDex, - item.bAC); + SDL_SwapLE16(item.bAC)); } int SyncDropEar(Point position, const TEar &ear) { return SyncDropEar( position, - ear.wCI, - ear.dwSeed, + SDL_SwapLE16(ear.wCI), + SDL_SwapLE32(ear.dwSeed), ear.bCursval, ear.heroname); } @@ -1136,18 +1144,21 @@ size_t OnRequestGetItem(const TCmd *pCmd, Player &player) if (gbBufferMsgs != 1 && IOwnLevel(player) && IsGItemValid(message)) { const Point position { message.x, message.y }; - if (GetItemRecord(message.def.dwSeed, message.def.wCI, message.def.wIndx)) { + const int32_t dwSeed = SDL_SwapLE32(message.def.dwSeed); + const uint16_t wCI = SDL_SwapLE16(message.def.wCI); + const _item_indexes wIndx = static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx)); + if (GetItemRecord(dwSeed, wCI, wIndx)) { int ii = -1; if (InDungeonBounds(position)) { ii = abs(dItem[position.x][position.y]) - 1; - if (ii >= 0 && !Items[ii].keyAttributesMatch(message.def.dwSeed, static_cast<_item_indexes>(message.def.wIndx), message.def.wCI)) { + if (ii >= 0 && !Items[ii].keyAttributesMatch(dwSeed, wIndx, wCI)) { ii = -1; } } if (ii == -1) { // No item at the target position or the key attributes don't match, so try find a matching item. - int activeItemIndex = FindGetItem(message.def.dwSeed, message.def.wIndx, message.def.wCI); + int activeItemIndex = FindGetItem(dwSeed, wIndx, wCI); if (activeItemIndex != -1) { ii = ActiveItems[activeItemIndex]; } @@ -1156,10 +1167,10 @@ size_t OnRequestGetItem(const TCmd *pCmd, Player &player) if (ii != -1) { NetSendCmdGItem2(false, CMD_GETITEM, MyPlayerId, message.bPnum, message); if (message.bPnum != MyPlayerId) - SyncGetItem(position, message.def.dwSeed, message.def.wIndx, message.def.wCI); + SyncGetItem(position, dwSeed, wIndx, wCI); else InvGetItem(*MyPlayer, ii); - SetItemRecord(message.def.dwSeed, message.def.wCI, message.def.wIndx); + SetItemRecord(dwSeed, wCI, wIndx); } else if (!NetSendCmdReq2(CMD_REQUESTGITEM, MyPlayerId, message.bPnum, message)) { NetSendCmdExtra(message); } @@ -1177,6 +1188,9 @@ size_t OnGetItem(const TCmd *pCmd, size_t pnum) SendPacket(pnum, &message, sizeof(message)); } else if (IsGItemValid(message)) { const Point position { message.x, message.y }; + const int32_t dwSeed = SDL_SwapLE32(message.def.dwSeed); + const uint16_t wCI = SDL_SwapLE16(message.def.wCI); + const _item_indexes wIndx = static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx)); if (DeltaGetItem(message, message.bLevel)) { bool isOnActiveLevel = GetLevelForMultiplayer(*MyPlayer) == message.bLevel; if ((isOnActiveLevel || message.bPnum == MyPlayerId) && message.bMaster != MyPlayerId) { @@ -1186,11 +1200,11 @@ size_t OnGetItem(const TCmd *pCmd, size_t pnum) if (ii != -1) InvGetItem(*MyPlayer, ii); } else { - int activeItemIndex = FindGetItem(message.def.dwSeed, message.def.wIndx, message.def.wCI); + int activeItemIndex = FindGetItem(dwSeed, wIndx, wCI); InvGetItem(*MyPlayer, ActiveItems[activeItemIndex]); } } else { - SyncGetItem(position, message.def.dwSeed, message.def.wIndx, message.def.wCI); + SyncGetItem(position, dwSeed, wIndx, wCI); } } } else { @@ -1206,10 +1220,11 @@ size_t OnGotoAutoGetItem(const TCmd *pCmd, Player &player) const auto &message = *reinterpret_cast(pCmd); const Point position { message.x, message.y }; - if (gbBufferMsgs != 1 && player.isOnActiveLevel() && InDungeonBounds(position) && message.wParam1 < MAXITEMS + 1) { + const uint16_t itemIdx = SDL_SwapLE16(message.wParam1); + if (gbBufferMsgs != 1 && player.isOnActiveLevel() && InDungeonBounds(position) && itemIdx < MAXITEMS + 1) { MakePlrPath(player, position, false); player.destAction = ACTION_PICKUPAITEM; - player.destParam1 = message.wParam1; + player.destParam1 = itemIdx; } return sizeof(message); @@ -1221,14 +1236,17 @@ size_t OnRequestAutoGetItem(const TCmd *pCmd, Player &player) if (gbBufferMsgs != 1 && IOwnLevel(player) && IsGItemValid(message)) { const Point position { message.x, message.y }; - if (GetItemRecord(message.def.dwSeed, message.def.wCI, message.def.wIndx)) { - if (FindGetItem(message.def.dwSeed, message.def.wIndx, message.def.wCI) != -1) { + const int32_t dwSeed = SDL_SwapLE32(message.def.dwSeed); + const uint16_t wCI = SDL_SwapLE16(message.def.wCI); + const _item_indexes wIndx = static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx)); + if (GetItemRecord(dwSeed, wCI, wIndx)) { + if (FindGetItem(dwSeed, wIndx, wCI) != -1) { NetSendCmdGItem2(false, CMD_AGETITEM, MyPlayerId, message.bPnum, message); if (message.bPnum != MyPlayerId) - SyncGetItem(position, message.def.dwSeed, message.def.wIndx, message.def.wCI); + SyncGetItem(position, dwSeed, wIndx, wCI); else AutoGetItem(*MyPlayer, &Items[message.bCursitem], message.bCursitem); - SetItemRecord(message.def.dwSeed, message.def.wCI, message.def.wIndx); + SetItemRecord(dwSeed, wCI, wIndx); } else if (!NetSendCmdReq2(CMD_REQUESTAGITEM, MyPlayerId, message.bPnum, message)) { NetSendCmdExtra(message); } @@ -1258,7 +1276,7 @@ size_t OnAutoGetItem(const TCmd *pCmd, size_t pnum) AutoGetItem(*MyPlayer, &Items[message.bCursitem], message.bCursitem); } } else { - SyncGetItem(position, message.def.dwSeed, message.def.wIndx, message.def.wCI); + SyncGetItem(position, SDL_SwapLE32(message.def.dwSeed), static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx)), SDL_SwapLE16(message.def.wCI)); } } } else { @@ -1279,7 +1297,7 @@ size_t OnItemExtra(const TCmd *pCmd, size_t pnum) DeltaGetItem(message, message.bLevel); if (Players[pnum].isOnActiveLevel()) { const Point position { message.x, message.y }; - SyncGetItem(position, message.def.dwSeed, message.def.wIndx, message.def.wCI); + SyncGetItem(position, SDL_SwapLE32(message.def.dwSeed), static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx)), SDL_SwapLE16(message.def.wCI)); } } @@ -1296,6 +1314,9 @@ size_t OnPutItem(const TCmd *pCmd, size_t pnum) const Point position { message.x, message.y }; Player &player = Players[pnum]; bool isSelf = &player == MyPlayer; + const int32_t dwSeed = SDL_SwapLE32(message.def.dwSeed); + const uint16_t wCI = SDL_SwapLE16(message.def.wCI); + const _item_indexes wIndx = static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx)); if (player.isOnActiveLevel()) { int ii; if (isSelf) @@ -1303,14 +1324,14 @@ size_t OnPutItem(const TCmd *pCmd, size_t pnum) else ii = SyncDropItem(message); if (ii != -1) { - PutItemRecord(message.def.dwSeed, message.def.wCI, message.def.wIndx); + PutItemRecord(dwSeed, wCI, wIndx); DeltaPutItem(message, Items[ii].position, player); if (isSelf) pfile_update(true); } return sizeof(message); } else { - PutItemRecord(message.def.dwSeed, message.def.wCI, message.def.wIndx); + PutItemRecord(dwSeed, wCI, wIndx); DeltaPutItem(message, position, player); if (isSelf) pfile_update(true); @@ -1328,17 +1349,20 @@ size_t OnSyncPutItem(const TCmd *pCmd, size_t pnum) SendPacket(pnum, &message, sizeof(message)); else if (IsPItemValid(message)) { Player &player = Players[pnum]; + const int32_t dwSeed = SDL_SwapLE32(message.def.dwSeed); + const uint16_t wCI = SDL_SwapLE16(message.def.wCI); + const _item_indexes wIndx = static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx)); if (player.isOnActiveLevel()) { int ii = SyncDropItem(message); if (ii != -1) { - PutItemRecord(message.def.dwSeed, message.def.wCI, message.def.wIndx); + PutItemRecord(dwSeed, wCI, wIndx); DeltaPutItem(message, Items[ii].position, player); if (&player == MyPlayer) pfile_update(true); } return sizeof(message); } else { - PutItemRecord(message.def.dwSeed, message.def.wCI, message.def.wIndx); + PutItemRecord(dwSeed, wCI, wIndx); DeltaPutItem(message, { message.x, message.y }, player); if (&player == MyPlayer) pfile_update(true); @@ -1359,7 +1383,10 @@ size_t OnRespawnItem(const TCmd *pCmd, size_t pnum) if (player.isOnActiveLevel() && &player != MyPlayer) { SyncDropItem(message); } - PutItemRecord(message.def.dwSeed, message.def.wCI, message.def.wIndx); + const int32_t dwSeed = SDL_SwapLE32(message.def.dwSeed); + const uint16_t wCI = SDL_SwapLE16(message.def.wCI); + const _item_indexes wIndx = static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx)); + PutItemRecord(dwSeed, wCI, wIndx); DeltaPutItem(message, { message.x, message.y }, player); } @@ -1422,12 +1449,15 @@ size_t OnSpellWall(const TCmd *pCmd, Player &player) return sizeof(message); if (!InDungeonBounds(position)) return sizeof(message); - if (message.wParam1 > SPL_LAST) + + const uint16_t wParam1 = SDL_SwapLE16(message.wParam1); + if (wParam1 > SPL_LAST) return sizeof(message); - if (message.wParam2 > RSPLTYPE_INVALID) + const uint16_t wParam2 = SDL_SwapLE16(message.wParam2); + if (wParam2 > RSPLTYPE_INVALID) return sizeof(message); - auto spell = static_cast(message.wParam1); + auto spell = static_cast(wParam1); if (!IsValidSpell(spell)) { LogError(_("{:s} has cast an invalid spell."), player._pName); return sizeof(message); @@ -1441,10 +1471,10 @@ size_t OnSpellWall(const TCmd *pCmd, Player &player) player.destAction = ACTION_SPELLWALL; player.destParam1 = position.x; player.destParam2 = position.y; - player.destParam3 = message.wParam3; - player.destParam4 = message.wParam4; + player.destParam3 = SDL_SwapLE16(message.wParam3); + player.destParam4 = SDL_SwapLE16(message.wParam4); player.queuedSpell.spellId = spell; - player.queuedSpell.spellType = static_cast(message.wParam2); + player.queuedSpell.spellType = static_cast(wParam2); player.queuedSpell.spellFrom = 0; return sizeof(message); @@ -1461,12 +1491,14 @@ size_t OnSpellTile(const TCmd *pCmd, Player &player) return sizeof(message); if (!InDungeonBounds(position)) return sizeof(message); - if (message.wParam1 > SPL_LAST) + const uint16_t wParam1 = SDL_SwapLE16(message.wParam1); + if (wParam1 > SPL_LAST) return sizeof(message); - if (message.wParam2 > RSPLTYPE_INVALID) + const uint16_t wParam2 = SDL_SwapLE16(message.wParam2); + if (wParam2 > RSPLTYPE_INVALID) return sizeof(message); - auto spell = static_cast(message.wParam1); + auto spell = static_cast(wParam1); if (!IsValidSpell(spell)) { LogError(_("{:s} has cast an invalid spell."), player._pName); return sizeof(message); @@ -1480,9 +1512,9 @@ size_t OnSpellTile(const TCmd *pCmd, Player &player) player.destAction = ACTION_SPELL; player.destParam1 = position.x; player.destParam2 = position.y; - player.destParam3 = message.wParam3; + player.destParam3 = SDL_SwapLE16(message.wParam3); player.queuedSpell.spellId = spell; - player.queuedSpell.spellType = static_cast(message.wParam2); + player.queuedSpell.spellType = static_cast(wParam2); return sizeof(message); } @@ -1498,12 +1530,14 @@ size_t OnTargetSpellTile(const TCmd *pCmd, Player &player) return sizeof(message); if (!InDungeonBounds(position)) return sizeof(message); - if (message.wParam1 > SPL_LAST) + const uint16_t wParam1 = SDL_SwapLE16(message.wParam1); + if (wParam1 > SPL_LAST) return sizeof(message); - if (message.wParam3 > INVITEM_BELT_LAST) + const uint16_t wParam3 = SDL_SwapLE16(message.wParam3); + if (wParam3 > INVITEM_BELT_LAST) return sizeof(message); - auto spell = static_cast(message.wParam1); + auto spell = static_cast(wParam1); if (leveltype == DTYPE_TOWN && !spelldata[spell].sTownSpell) { LogError(_("{:s} has cast an illegal spell."), player._pName); return sizeof(message); @@ -1513,10 +1547,10 @@ size_t OnTargetSpellTile(const TCmd *pCmd, Player &player) player.destAction = ACTION_SPELL; player.destParam1 = position.x; player.destParam2 = position.y; - player.destParam3 = message.wParam2; + player.destParam3 = SDL_SwapLE16(message.wParam2); player.queuedSpell.spellId = spell; player.queuedSpell.spellType = RSPLTYPE_SCROLL; - player.queuedSpell.spellFrom = static_cast(message.wParam3); + player.queuedSpell.spellFrom = static_cast(wParam3); return sizeof(message); } @@ -1541,13 +1575,14 @@ size_t OnObjectTileAction(const TCmd &cmd, Player &player, action_id action, boo size_t OnAttackMonster(const TCmd *pCmd, Player &player) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t monsterIdx = SDL_SwapLE16(message.wParam1); - if (gbBufferMsgs != 1 && player.isOnActiveLevel() && message.wParam1 < MaxMonsters) { - Point position = Monsters[message.wParam1].position.future; + if (gbBufferMsgs != 1 && player.isOnActiveLevel() && monsterIdx < MaxMonsters) { + Point position = Monsters[monsterIdx].position.future; if (player.position.tile.WalkingDistance(position) > 1) MakePlrPath(player, position, false); player.destAction = ACTION_ATTACKMON; - player.destParam1 = message.wParam1; + player.destParam1 = monsterIdx; } return sizeof(message); @@ -1556,11 +1591,12 @@ size_t OnAttackMonster(const TCmd *pCmd, Player &player) size_t OnAttackPlayer(const TCmd *pCmd, Player &player) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t playerIdx = SDL_SwapLE16(message.wParam1); - if (gbBufferMsgs != 1 && player.isOnActiveLevel() && message.wParam1 < Players.size()) { - MakePlrPath(player, Players[message.wParam1].position.future, false); + if (gbBufferMsgs != 1 && player.isOnActiveLevel() && playerIdx < Players.size()) { + MakePlrPath(player, Players[playerIdx].position.future, false); player.destAction = ACTION_ATTACKPLR; - player.destParam1 = message.wParam1; + player.destParam1 = playerIdx; } return sizeof(message); @@ -1569,11 +1605,12 @@ size_t OnAttackPlayer(const TCmd *pCmd, Player &player) size_t OnRangedAttackMonster(const TCmd *pCmd, Player &player) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t monsterIdx = SDL_SwapLE16(message.wParam1); - if (gbBufferMsgs != 1 && player.isOnActiveLevel() && message.wParam1 < MaxMonsters) { + if (gbBufferMsgs != 1 && player.isOnActiveLevel() && monsterIdx < MaxMonsters) { ClrPlrPath(player); player.destAction = ACTION_RATTACKMON; - player.destParam1 = message.wParam1; + player.destParam1 = monsterIdx; } return sizeof(message); @@ -1582,11 +1619,12 @@ size_t OnRangedAttackMonster(const TCmd *pCmd, Player &player) size_t OnRangedAttackPlayer(const TCmd *pCmd, Player &player) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t playerIdx = SDL_SwapLE16(message.wParam1); - if (gbBufferMsgs != 1 && player.isOnActiveLevel() && message.wParam1 < Players.size()) { + if (gbBufferMsgs != 1 && player.isOnActiveLevel() && playerIdx < Players.size()) { ClrPlrPath(player); player.destAction = ACTION_RATTACKPLR; - player.destParam1 = message.wParam1; + player.destParam1 = playerIdx; } return sizeof(message); @@ -1600,14 +1638,17 @@ size_t OnSpellMonster(const TCmd *pCmd, Player &player) return sizeof(message); if (!player.isOnActiveLevel()) return sizeof(message); - if (message.wParam1 >= MaxMonsters) + const uint16_t monsterIdx = SDL_SwapLE16(message.wParam1); + if (monsterIdx >= MaxMonsters) return sizeof(message); - if (message.wParam2 > SPL_LAST) + const uint16_t wParam2 = SDL_SwapLE16(message.wParam2); + if (wParam2 > SPL_LAST) return sizeof(message); - if (message.wParam3 > RSPLTYPE_INVALID) + const uint16_t wParam3 = SDL_SwapLE16(message.wParam3); + if (wParam3 > RSPLTYPE_INVALID) return sizeof(message); - auto spell = static_cast(message.wParam2); + auto spell = static_cast(wParam2); if (!IsValidSpell(spell)) { LogError(_("{:s} has cast an invalid spell."), player._pName); return sizeof(message); @@ -1619,10 +1660,10 @@ size_t OnSpellMonster(const TCmd *pCmd, Player &player) ClrPlrPath(player); player.destAction = ACTION_SPELLMON; - player.destParam1 = message.wParam1; - player.destParam2 = message.wParam4; + player.destParam1 = monsterIdx; + player.destParam2 = SDL_SwapLE16(message.wParam4); player.queuedSpell.spellId = spell; - player.queuedSpell.spellType = static_cast(message.wParam3); + player.queuedSpell.spellType = static_cast(wParam3); player.queuedSpell.spellFrom = 0; return sizeof(message); @@ -1636,14 +1677,17 @@ size_t OnSpellPlayer(const TCmd *pCmd, Player &player) return sizeof(message); if (!player.isOnActiveLevel()) return sizeof(message); - if (message.wParam1 >= Players.size()) + const uint16_t playerIdx = SDL_SwapLE16(message.wParam1); + if (playerIdx >= Players.size()) return sizeof(message); - if (message.wParam2 > SPL_LAST) + const uint16_t wParam2 = SDL_SwapLE16(message.wParam2); + if (wParam2 > SPL_LAST) return sizeof(message); - if (message.wParam3 > RSPLTYPE_INVALID) + const uint16_t wParam3 = SDL_SwapLE16(message.wParam3); + if (wParam3 > RSPLTYPE_INVALID) return sizeof(message); - auto spell = static_cast(message.wParam2); + auto spell = static_cast(wParam2); if (!IsValidSpell(spell)) { LogError(_("{:s} has cast an invalid spell."), player._pName); return sizeof(message); @@ -1655,10 +1699,10 @@ size_t OnSpellPlayer(const TCmd *pCmd, Player &player) ClrPlrPath(player); player.destAction = ACTION_SPELLPLR; - player.destParam1 = message.wParam1; - player.destParam2 = message.wParam4; + player.destParam1 = playerIdx; + player.destParam2 = SDL_SwapLE16(message.wParam4); player.queuedSpell.spellId = spell; - player.queuedSpell.spellType = static_cast(message.wParam3); + player.queuedSpell.spellType = static_cast(wParam3); player.queuedSpell.spellFrom = 0; return sizeof(message); @@ -1672,14 +1716,17 @@ size_t OnTargetSpellMonster(const TCmd *pCmd, Player &player) return sizeof(message); if (!player.isOnActiveLevel()) return sizeof(message); - if (message.wParam1 >= MaxMonsters) + const uint16_t monsterIdx = SDL_SwapLE16(message.wParam1); + if (monsterIdx >= MaxMonsters) return sizeof(message); - if (message.wParam2 > SPL_LAST) + const uint16_t wParam2 = SDL_SwapLE16(message.wParam2); + if (wParam2 > SPL_LAST) return sizeof(message); - if (message.wParam4 > INVITEM_BELT_LAST) + const uint16_t wParam4 = SDL_SwapLE16(message.wParam4); + if (wParam4 > INVITEM_BELT_LAST) return sizeof(message); - auto spell = static_cast(message.wParam2); + auto spell = static_cast(wParam2); if (leveltype == DTYPE_TOWN && !spelldata[spell].sTownSpell) { LogError(_("{:s} has cast an illegal spell."), player._pName); return sizeof(message); @@ -1687,11 +1734,11 @@ size_t OnTargetSpellMonster(const TCmd *pCmd, Player &player) ClrPlrPath(player); player.destAction = ACTION_SPELLMON; - player.destParam1 = message.wParam1; - player.destParam2 = message.wParam3; + player.destParam1 = monsterIdx; + player.destParam2 = SDL_SwapLE16(message.wParam3); player.queuedSpell.spellId = spell; player.queuedSpell.spellType = RSPLTYPE_SCROLL; - player.queuedSpell.spellFrom = static_cast(message.wParam4); + player.queuedSpell.spellFrom = static_cast(wParam4); return sizeof(message); } @@ -1704,14 +1751,17 @@ size_t OnTargetSpellPlayer(const TCmd *pCmd, Player &player) return sizeof(message); if (!player.isOnActiveLevel()) return sizeof(message); - if (message.wParam1 >= Players.size()) + const uint16_t playerIdx = SDL_SwapLE16(message.wParam1); + if (playerIdx >= Players.size()) return sizeof(message); - if (message.wParam2 > SPL_LAST) + const uint16_t wParam2 = SDL_SwapLE16(message.wParam2); + if (wParam2 > SPL_LAST) return sizeof(message); - if (message.wParam4 > INVITEM_BELT_LAST) + const uint16_t wParam4 = SDL_SwapLE16(message.wParam4); + if (wParam4 > INVITEM_BELT_LAST) return sizeof(message); - auto spell = static_cast(message.wParam2); + auto spell = static_cast(wParam2); if (leveltype == DTYPE_TOWN && !spelldata[spell].sTownSpell) { LogError(_("{:s} has cast an illegal spell."), player._pName); return sizeof(message); @@ -1719,11 +1769,11 @@ size_t OnTargetSpellPlayer(const TCmd *pCmd, Player &player) ClrPlrPath(player); player.destAction = ACTION_SPELLPLR; - player.destParam1 = message.wParam1; - player.destParam2 = message.wParam3; + player.destParam1 = playerIdx; + player.destParam2 = SDL_SwapLE16(message.wParam3); player.queuedSpell.spellId = spell; player.queuedSpell.spellType = RSPLTYPE_SCROLL; - player.queuedSpell.spellFrom = static_cast(message.wParam4); + player.queuedSpell.spellFrom = static_cast(wParam4); return sizeof(message); } @@ -1731,11 +1781,12 @@ size_t OnTargetSpellPlayer(const TCmd *pCmd, Player &player) size_t OnKnockback(const TCmd *pCmd, size_t pnum) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t monsterIdx = SDL_SwapLE16(message.wParam1); Player &player = Players[pnum]; - if (gbBufferMsgs != 1 && player.isOnActiveLevel() && message.wParam1 < MaxMonsters) { - Monster &monster = Monsters[message.wParam1]; + if (gbBufferMsgs != 1 && player.isOnActiveLevel() && monsterIdx < MaxMonsters) { + Monster &monster = Monsters[monsterIdx]; M_GetKnockback(monster); M_StartHit(monster, player, 0); } @@ -1746,11 +1797,12 @@ size_t OnKnockback(const TCmd *pCmd, size_t pnum) size_t OnResurrect(const TCmd *pCmd, size_t pnum) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t playerIdx = SDL_SwapLE16(message.wParam1); if (gbBufferMsgs == 1) { SendPacket(pnum, &message, sizeof(message)); - } else if (message.wParam1 < Players.size()) { - DoResurrect(pnum, Players[message.wParam1]); + } else if (playerIdx < Players.size()) { + DoResurrect(pnum, Players[playerIdx]); if (pnum == MyPlayerId) pfile_update(true); } @@ -1761,10 +1813,11 @@ size_t OnResurrect(const TCmd *pCmd, size_t pnum) size_t OnHealOther(const TCmd *pCmd, const Player &caster) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t playerIdx = SDL_SwapLE16(message.wParam1); if (gbBufferMsgs != 1) { - if (caster.isOnActiveLevel() && message.wParam1 < Players.size()) { - DoHealOther(caster, Players[message.wParam1]); + if (caster.isOnActiveLevel() && playerIdx < Players.size()) { + DoHealOther(caster, Players[playerIdx]); } } @@ -1775,11 +1828,12 @@ size_t OnTalkXY(const TCmd *pCmd, Player &player) { const auto &message = *reinterpret_cast(pCmd); const Point position { message.x, message.y }; + const uint16_t townerIdx = SDL_SwapLE16(message.wParam1); - if (gbBufferMsgs != 1 && player.isOnActiveLevel() && InDungeonBounds(position) && message.wParam1 < NUM_TOWNERS) { + if (gbBufferMsgs != 1 && player.isOnActiveLevel() && InDungeonBounds(position) && townerIdx < NUM_TOWNERS) { MakePlrPath(player, position, false); player.destAction = ACTION_TALK; - player.destParam1 = message.wParam1; + player.destParam1 = townerIdx; } return sizeof(message); @@ -1788,16 +1842,17 @@ size_t OnTalkXY(const TCmd *pCmd, Player &player) size_t OnNewLevel(const TCmd *pCmd, size_t pnum) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t eventIdx = SDL_SwapLE16(message.wParam1); if (gbBufferMsgs == 1) { SendPacket(pnum, &message, sizeof(message)); } else if (pnum != MyPlayerId) { - if (message.wParam1 < WM_FIRST || message.wParam1 > WM_LAST) + if (eventIdx < WM_FIRST || eventIdx > WM_LAST) return sizeof(message); - auto mode = static_cast(message.wParam1); + auto mode = static_cast(eventIdx); - int levelId = message.wParam2; + const auto levelId = static_cast(SDL_SwapLE16(message.wParam2)); if (!IsValidLevel(levelId, mode == WM_DIABSETLVL)) { return sizeof(message); } @@ -1811,11 +1866,12 @@ size_t OnNewLevel(const TCmd *pCmd, size_t pnum) size_t OnWarp(const TCmd *pCmd, size_t pnum) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t portalIdx = SDL_SwapLE16(message.wParam1); if (gbBufferMsgs == 1) { SendPacket(pnum, &message, sizeof(message)); - } else if (message.wParam1 < MAXPORTAL) { - StartWarpLvl(Players[pnum], message.wParam1); + } else if (portalIdx < MAXPORTAL) { + StartWarpLvl(Players[pnum], portalIdx); } return sizeof(message); @@ -1825,11 +1881,12 @@ size_t OnMonstDeath(const TCmd *pCmd, size_t pnum) { const auto &message = *reinterpret_cast(pCmd); const Point position { message.x, message.y }; + const uint16_t monsterIdx = SDL_SwapLE16(message.wParam1); if (gbBufferMsgs != 1) { Player &player = Players[pnum]; - if (&player != MyPlayer && InDungeonBounds(position) && message.wParam1 < MaxMonsters) { - Monster &monster = Monsters[message.wParam1]; + if (&player != MyPlayer && InDungeonBounds(position) && monsterIdx < MaxMonsters) { + Monster &monster = Monsters[monsterIdx]; if (player.isOnActiveLevel()) M_SyncStartKill(monster, position, player); delta_kill_monster(monster, position, player); @@ -1890,15 +1947,16 @@ size_t OnAwakeGolem(const TCmd *pCmd, size_t pnum) size_t OnMonstDamage(const TCmd *pCmd, size_t pnum) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t monsterIdx = SDL_SwapLE16(message.wMon); if (gbBufferMsgs != 1) { Player &player = Players[pnum]; if (&player != MyPlayer) { - if (player.isOnActiveLevel() && message.wMon < MaxMonsters) { - auto &monster = Monsters[message.wMon]; + if (player.isOnActiveLevel() && monsterIdx < MaxMonsters) { + auto &monster = Monsters[monsterIdx]; monster.tag(player); if (monster.hitPoints > 0) { - monster.hitPoints -= message.dwDam; + monster.hitPoints -= SDL_SwapLE32(message.dwDam); if ((monster.hitPoints >> 6) < 1) monster.hitPoints = 1 << 6; delta_monster_hp(monster, player); @@ -1915,11 +1973,12 @@ size_t OnMonstDamage(const TCmd *pCmd, size_t pnum) size_t OnPlayerDeath(const TCmd *pCmd, size_t pnum) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t earFlag = SDL_SwapLE16(message.wParam1); if (gbBufferMsgs != 1) { Player &player = Players[pnum]; if (&player != MyPlayer) - StartPlayerKill(player, message.wParam1); + StartPlayerKill(player, earFlag); else pfile_update(true); } else { @@ -1932,11 +1991,12 @@ size_t OnPlayerDeath(const TCmd *pCmd, size_t pnum) size_t OnPlayerDamage(const TCmd *pCmd, Player &player) { const auto &message = *reinterpret_cast(pCmd); + const uint32_t damage = SDL_SwapLE32(message.dwDam); Player &target = Players[message.bPlr]; if (&target == MyPlayer && leveltype != DTYPE_TOWN && gbBufferMsgs != 1) { - if (player.isOnActiveLevel() && message.dwDam <= 192000 && target._pHitPoints >> 6 > 0) { - ApplyPlrDamage(target, 0, 0, message.dwDam, 1); + if (player.isOnActiveLevel() && damage <= 192000 && target._pHitPoints >> 6 > 0) { + ApplyPlrDamage(target, 0, 0, damage, 1); } } @@ -1997,7 +2057,7 @@ size_t OnChangePlayerItems(const TCmd *pCmd, size_t pnum) if (gbBufferMsgs == 1) { SendPacket(pnum, &message, sizeof(message)); - } else if (&player != MyPlayer && IsItemAvailable(message.def.wIndx)) { + } else if (&player != MyPlayer && IsItemAvailable(static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx)))) { Item &item = player.InvBody[message.bLoc]; item = {}; RecreateItem(player, message, item); @@ -2034,7 +2094,7 @@ size_t OnChangeInventoryItems(const TCmd *pCmd, int pnum) if (gbBufferMsgs == 1) { SendPacket(pnum, &message, sizeof(message)); - } else if (&player != MyPlayer && IsItemAvailable(message.def.wIndx)) { + } else if (&player != MyPlayer && IsItemAvailable(static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx)))) { Item item {}; RecreateItem(player, message, item); CheckInvSwap(player, item, message.bLoc); @@ -2046,12 +2106,13 @@ size_t OnChangeInventoryItems(const TCmd *pCmd, int pnum) size_t OnDeleteInventoryItems(const TCmd *pCmd, int pnum) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t invGridIndex = SDL_SwapLE16(message.wParam1); Player &player = Players[pnum]; if (gbBufferMsgs == 1) { SendPacket(pnum, &message, sizeof(message)); - } else if (&player != MyPlayer && message.wParam1 < InventoryGridCells) { - CheckInvRemove(player, message.wParam1); + } else if (&player != MyPlayer && invGridIndex < InventoryGridCells) { + CheckInvRemove(player, invGridIndex); } return sizeof(message); @@ -2067,7 +2128,7 @@ size_t OnChangeBeltItems(const TCmd *pCmd, int pnum) if (gbBufferMsgs == 1) { SendPacket(pnum, &message, sizeof(message)); - } else if (&player != MyPlayer && IsItemAvailable(message.def.wIndx)) { + } else if (&player != MyPlayer && IsItemAvailable(static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx)))) { Item &item = player.SpdList[message.bLoc]; item = {}; RecreateItem(player, message, item); @@ -2079,12 +2140,13 @@ size_t OnChangeBeltItems(const TCmd *pCmd, int pnum) size_t OnDeleteBeltItems(const TCmd *pCmd, int pnum) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t spdBarIndex = SDL_SwapLE16(message.wParam1); Player &player = Players[pnum]; if (gbBufferMsgs == 1) { SendPacket(pnum, &message, sizeof(message)); - } else if (&player != MyPlayer && message.wParam1 < MaxBeltItems) { - player.RemoveSpdBarItem(message.wParam1); + } else if (&player != MyPlayer && spdBarIndex < MaxBeltItems) { + player.RemoveSpdBarItem(spdBarIndex); } return sizeof(message); @@ -2093,11 +2155,12 @@ size_t OnDeleteBeltItems(const TCmd *pCmd, int pnum) size_t OnPlayerLevel(const TCmd *pCmd, size_t pnum) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t playerLevel = SDL_SwapLE16(message.wParam1); if (gbBufferMsgs != 1) { Player &player = Players[pnum]; - if (message.wParam1 <= MaxCharacterLevel && &player != MyPlayer) - player._pLevel = static_cast(message.wParam1); + if (playerLevel <= MaxCharacterLevel && &player != MyPlayer) + player._pLevel = static_cast(playerLevel); } else { SendPacket(pnum, &message, sizeof(message)); } @@ -2129,7 +2192,7 @@ size_t OnSpawnItem(const TCmd *pCmd, size_t pnum) if (player.isOnActiveLevel() && &player != MyPlayer) { SyncDropItem(message); } - PutItemRecord(message.def.dwSeed, message.def.wCI, message.def.wIndx); + PutItemRecord(SDL_SwapLE32(message.def.dwSeed), SDL_SwapLE16(message.def.wCI), static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx))); DeltaPutItem(message, { message.x, message.y }, player); } @@ -2139,13 +2202,14 @@ size_t OnSpawnItem(const TCmd *pCmd, size_t pnum) size_t OnSendPlayerInfo(const TCmd *pCmd, size_t pnum) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t wBytes = SDL_SwapLE16(message.wBytes); if (gbBufferMsgs == 1) - SendPacket(pnum, &message, message.wBytes + sizeof(message)); + SendPacket(pnum, &message, wBytes + sizeof(message)); else recv_plrinfo(pnum, message, message.bCmd == CMD_ACK_PLRINFO); - return message.wBytes + sizeof(message); + return wBytes + sizeof(message); } size_t OnPlayerJoinLevel(const TCmd *pCmd, size_t pnum) @@ -2158,7 +2222,7 @@ size_t OnPlayerJoinLevel(const TCmd *pCmd, size_t pnum) return sizeof(message); } - int playerLevel = message.wParam1; + const uint16_t playerLevel = SDL_SwapLE16(message.wParam1); bool isSetLevel = message.wParam2 != 0; if (!IsValidLevel(playerLevel, isSetLevel) || !InDungeonBounds(position)) { return sizeof(message); @@ -2205,13 +2269,14 @@ size_t OnActivatePortal(const TCmd *pCmd, size_t pnum) { const auto &message = *reinterpret_cast(pCmd); const Point position { message.x, message.y }; - bool isSetLevel = message.wParam3 != 0; + const uint16_t level = SDL_SwapLE16(message.wParam1); + const uint16_t dungeonTypeIdx = SDL_SwapLE16(message.wParam1); + const bool isSetLevel = message.wParam3 != 0; if (gbBufferMsgs == 1) { SendPacket(pnum, &message, sizeof(message)); - } else if (InDungeonBounds(position) && IsValidLevel(message.wParam1, isSetLevel) && message.wParam2 <= DTYPE_LAST) { - int level = message.wParam1; - auto dungeonType = static_cast(message.wParam2); + } else if (InDungeonBounds(position) && IsValidLevel(level, isSetLevel) && dungeonTypeIdx <= DTYPE_LAST) { + auto dungeonType = static_cast(dungeonTypeIdx); ActivatePortal(pnum, position, level, dungeonType, isSetLevel); Player &player = Players[pnum]; @@ -2271,11 +2336,12 @@ size_t OnRestartTown(const TCmd *pCmd, size_t pnum) size_t OnSetStrength(const TCmd *pCmd, size_t pnum) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t value = SDL_SwapLE16(message.wParam1); if (gbBufferMsgs != 1) { Player &player = Players[pnum]; - if (message.wParam1 <= 750 && &player != MyPlayer) - SetPlrStr(player, message.wParam1); + if (value <= 750 && &player != MyPlayer) + SetPlrStr(player, value); } else { SendPacket(pnum, &message, sizeof(message)); } @@ -2286,11 +2352,12 @@ size_t OnSetStrength(const TCmd *pCmd, size_t pnum) size_t OnSetDexterity(const TCmd *pCmd, size_t pnum) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t value = SDL_SwapLE16(message.wParam1); if (gbBufferMsgs != 1) { Player &player = Players[pnum]; - if (message.wParam1 <= 750 && &player != MyPlayer) - SetPlrDex(player, message.wParam1); + if (value <= 750 && &player != MyPlayer) + SetPlrDex(player, value); } else { SendPacket(pnum, &message, sizeof(message)); } @@ -2301,11 +2368,12 @@ size_t OnSetDexterity(const TCmd *pCmd, size_t pnum) size_t OnSetMagic(const TCmd *pCmd, size_t pnum) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t value = SDL_SwapLE16(message.wParam1); if (gbBufferMsgs != 1) { Player &player = Players[pnum]; - if (message.wParam1 <= 750 && &player != MyPlayer) - SetPlrMag(player, message.wParam1); + if (value <= 750 && &player != MyPlayer) + SetPlrMag(player, value); } else { SendPacket(pnum, &message, sizeof(message)); } @@ -2316,11 +2384,12 @@ size_t OnSetMagic(const TCmd *pCmd, size_t pnum) size_t OnSetVitality(const TCmd *pCmd, size_t pnum) { const auto &message = *reinterpret_cast(pCmd); + const uint16_t value = SDL_SwapLE16(message.wParam1); if (gbBufferMsgs != 1) { Player &player = Players[pnum]; - if (message.wParam1 <= 750 && &player != MyPlayer) - SetPlrVit(player, message.wParam1); + if (value <= 750 && &player != MyPlayer) + SetPlrVit(player, value); } else { SendPacket(pnum, &message, sizeof(message)); } @@ -2436,7 +2505,7 @@ size_t OnSetReflect(const TCmd *pCmd, Player &player) const auto &message = *reinterpret_cast(pCmd); if (gbBufferMsgs != 1) - player.wReflections = message.wParam1; + player.wReflections = SDL_SwapLE16(message.wParam1); return sizeof(message); } @@ -2488,7 +2557,7 @@ void msg_send_drop_pkt(int pnum, int reason) { TFakeDropPlr cmd; - cmd.dwReason = reason; + cmd.dwReason = SDL_SwapLE32(reason); cmd.bCmd = FAKE_CMD_DROPID; cmd.bPlr = pnum; SendPacket(pnum, &cmd, sizeof(cmd)); @@ -2620,7 +2689,7 @@ void delta_sync_monster(const TSyncMonster &monsterSync, uint8_t level) monster.position.y = monsterSync._my; monster._mactive = UINT8_MAX; monster._menemy = monsterSync._menemy; - monster.hitPoints = monsterSync._mhitpoints; + monster.hitPoints = SDL_SwapLE32(monsterSync._mhitpoints); monster.mWhoHit = monsterSync.mWhoHit; } @@ -2666,9 +2735,9 @@ void DeltaAddItem(int ii) for (const TCmdPItem &item : deltaLevel.item) { if (item.bCmd != CMD_INVALID - && item.def.wIndx == Items[ii].IDidx - && item.def.wCI == Items[ii]._iCreateInfo - && item.def.dwSeed == Items[ii]._iSeed + && static_cast<_item_indexes>(SDL_SwapLE16(item.def.wIndx)) == Items[ii].IDidx + && SDL_SwapLE16(item.def.wCI) == Items[ii]._iCreateInfo + && static_cast(SDL_SwapLE32(item.def.dwSeed)) == Items[ii]._iSeed && IsAnyOf(item.bCmd, TCmdPItem::PickedUpItem, TCmdPItem::FloorItem)) { return; } @@ -2817,9 +2886,9 @@ void DeltaLoadLevel() if (deltaLevel.item[i].bCmd == TCmdPItem::PickedUpItem) { int activeItemIndex = FindGetItem( - deltaLevel.item[i].def.dwSeed, - deltaLevel.item[i].def.wIndx, - deltaLevel.item[i].def.wCI); + SDL_SwapLE32(deltaLevel.item[i].def.dwSeed), + static_cast<_item_indexes>(SDL_SwapLE16(deltaLevel.item[i].def.wIndx)), + SDL_SwapLE16(deltaLevel.item[i].def.wCI)); if (activeItemIndex != -1) { const auto &position = Items[ActiveItems[activeItemIndex]].position; if (dItem[position.x][position.y] == ActiveItems[activeItemIndex] + 1) @@ -2894,7 +2963,7 @@ void NetSendCmdLocParam1(bool bHiPri, _cmd_id bCmd, Point position, uint16_t wPa cmd.bCmd = bCmd; cmd.x = position.x; cmd.y = position.y; - cmd.wParam1 = wParam1; + cmd.wParam1 = SDL_SwapLE16(wParam1); if (bHiPri) NetSendHiPri(MyPlayerId, (byte *)&cmd, sizeof(cmd)); else @@ -2913,8 +2982,8 @@ void NetSendCmdLocParam2(bool bHiPri, _cmd_id bCmd, Point position, uint16_t wPa cmd.bCmd = bCmd; cmd.x = position.x; cmd.y = position.y; - cmd.wParam1 = wParam1; - cmd.wParam2 = wParam2; + cmd.wParam1 = SDL_SwapLE16(wParam1); + cmd.wParam2 = SDL_SwapLE16(wParam2); if (bHiPri) NetSendHiPri(MyPlayerId, (byte *)&cmd, sizeof(cmd)); else @@ -2933,9 +3002,9 @@ void NetSendCmdLocParam3(bool bHiPri, _cmd_id bCmd, Point position, uint16_t wPa cmd.bCmd = bCmd; cmd.x = position.x; cmd.y = position.y; - cmd.wParam1 = wParam1; - cmd.wParam2 = wParam2; - cmd.wParam3 = wParam3; + cmd.wParam1 = SDL_SwapLE16(wParam1); + cmd.wParam2 = SDL_SwapLE16(wParam2); + cmd.wParam3 = SDL_SwapLE16(wParam3); if (bHiPri) NetSendHiPri(MyPlayerId, (byte *)&cmd, sizeof(cmd)); else @@ -2954,10 +3023,10 @@ void NetSendCmdLocParam4(bool bHiPri, _cmd_id bCmd, Point position, uint16_t wPa cmd.bCmd = bCmd; cmd.x = position.x; cmd.y = position.y; - cmd.wParam1 = wParam1; - cmd.wParam2 = wParam2; - cmd.wParam3 = wParam3; - cmd.wParam4 = wParam4; + cmd.wParam1 = SDL_SwapLE16(wParam1); + cmd.wParam2 = SDL_SwapLE16(wParam2); + cmd.wParam3 = SDL_SwapLE16(wParam3); + cmd.wParam4 = SDL_SwapLE16(wParam4); if (bHiPri) NetSendHiPri(MyPlayerId, (byte *)&cmd, sizeof(cmd)); else @@ -2974,7 +3043,7 @@ void NetSendCmdParam1(bool bHiPri, _cmd_id bCmd, uint16_t wParam1) TCmdParam1 cmd; cmd.bCmd = bCmd; - cmd.wParam1 = wParam1; + cmd.wParam1 = SDL_SwapLE16(wParam1); if (bHiPri) NetSendHiPri(MyPlayerId, (byte *)&cmd, sizeof(cmd)); else @@ -2988,8 +3057,8 @@ void NetSendCmdParam2(bool bHiPri, _cmd_id bCmd, uint16_t wParam1, uint16_t wPar TCmdParam2 cmd; cmd.bCmd = bCmd; - cmd.wParam1 = wParam1; - cmd.wParam2 = wParam2; + cmd.wParam1 = SDL_SwapLE16(wParam1); + cmd.wParam2 = SDL_SwapLE16(wParam2); if (bHiPri) NetSendHiPri(MyPlayerId, (byte *)&cmd, sizeof(cmd)); else @@ -3004,9 +3073,9 @@ void NetSendCmdParam3(bool bHiPri, _cmd_id bCmd, uint16_t wParam1, uint16_t wPar TCmdParam3 cmd; cmd.bCmd = bCmd; - cmd.wParam1 = wParam1; - cmd.wParam2 = wParam2; - cmd.wParam3 = wParam3; + cmd.wParam1 = SDL_SwapLE16(wParam1); + cmd.wParam2 = SDL_SwapLE16(wParam2); + cmd.wParam3 = SDL_SwapLE16(wParam3); if (bHiPri) NetSendHiPri(MyPlayerId, (byte *)&cmd, sizeof(cmd)); else @@ -3023,10 +3092,10 @@ void NetSendCmdParam4(bool bHiPri, _cmd_id bCmd, uint16_t wParam1, uint16_t wPar TCmdParam4 cmd; cmd.bCmd = bCmd; - cmd.wParam1 = wParam1; - cmd.wParam2 = wParam2; - cmd.wParam3 = wParam3; - cmd.wParam4 = wParam4; + cmd.wParam1 = SDL_SwapLE16(wParam1); + cmd.wParam2 = SDL_SwapLE16(wParam2); + cmd.wParam3 = SDL_SwapLE16(wParam3); + cmd.wParam4 = SDL_SwapLE16(wParam4); if (bHiPri) NetSendHiPri(MyPlayerId, (byte *)&cmd, sizeof(cmd)); else diff --git a/Source/multi.cpp b/Source/multi.cpp index a0c96ee01..a6d82068f 100644 --- a/Source/multi.cpp +++ b/Source/multi.cpp @@ -74,6 +74,13 @@ const event_type EventTypes[3] = { namespace { +constexpr uint16_t HeaderCheckVal = +#if SDL_BYTEORDER == SDL_LIL_ENDIAN + LoadBE16("ip"); +#else + LoadLE16("ip"); +#endif + uint32_t sgbSentThisCycle; void BufferInit(TBuffer *pBuf) @@ -124,15 +131,15 @@ void NetReceivePlayerData(TPkt *pkt) const Player &myPlayer = *MyPlayer; const Point target = myPlayer.GetTargetPosition(); - pkt->hdr.wCheck = LoadBE32("\0\0ip"); + pkt->hdr.wCheck = HeaderCheckVal; pkt->hdr.px = myPlayer.position.tile.x; pkt->hdr.py = myPlayer.position.tile.y; pkt->hdr.targx = target.x; pkt->hdr.targy = target.y; - pkt->hdr.php = myPlayer._pHitPoints; - pkt->hdr.pmhp = myPlayer._pMaxHP; - pkt->hdr.mana = myPlayer._pMana; - pkt->hdr.maxmana = myPlayer._pMaxMana; + pkt->hdr.php = SDL_SwapLE32(myPlayer._pHitPoints); + pkt->hdr.pmhp = SDL_SwapLE32(myPlayer._pMaxHP); + pkt->hdr.mana = SDL_SwapLE32(myPlayer._pMana); + pkt->hdr.maxmana = SDL_SwapLE32(myPlayer._pMaxMana); pkt->hdr.bstr = myPlayer._pBaseStr; pkt->hdr.bmag = myPlayer._pBaseMag; pkt->hdr.bdex = myPlayer._pBaseDex; @@ -185,9 +192,10 @@ void SendPacket(int playerId, const byte *packet, size_t size) TPkt pkt; NetReceivePlayerData(&pkt); - pkt.hdr.wLen = static_cast(size + sizeof(pkt.hdr)); + const size_t sizeWithheader = size + sizeof(pkt.hdr); + pkt.hdr.wLen = SDL_SwapLE16(static_cast(sizeWithheader)); memcpy(pkt.body, packet, size); - if (!SNetSendMessage(playerId, &pkt.hdr, pkt.hdr.wLen)) + if (!SNetSendMessage(playerId, &pkt.hdr, sizeWithheader)) nthread_terminate_game("SNetSendMessage0"); } @@ -501,8 +509,8 @@ void NetSendHiPri(int playerId, const byte *data, size_t size) byte *hipriBody = ReceivePacket(&sgHiPriBuf, pkt.body, &msgSize); byte *lowpriBody = ReceivePacket(&sgLoPriBuf, hipriBody, &msgSize); msgSize = sync_all_monsters(lowpriBody, msgSize); - size_t len = gdwNormalMsgSize - msgSize; - pkt.hdr.wLen = static_cast(len); + const size_t len = gdwNormalMsgSize - msgSize; + pkt.hdr.wLen = SDL_SwapLE16(static_cast(len)); if (!SNetSendMessage(SNPLAYER_OTHERS, &pkt.hdr, static_cast(len))) nthread_terminate_game("SNetSendMessage"); } @@ -512,8 +520,8 @@ void multi_send_msg_packet(uint32_t pmask, const byte *data, size_t size) { TPkt pkt; NetReceivePlayerData(&pkt); - size_t len = size + sizeof(pkt.hdr); - pkt.hdr.wLen = static_cast(len); + const size_t len = size + sizeof(pkt.hdr); + pkt.hdr.wLen = SDL_SwapLE16(static_cast(len)); memcpy(pkt.body, data, size); size_t playerID = 0; for (size_t v = 1; playerID < Players.size(); playerID++, v <<= 1) { @@ -601,9 +609,9 @@ void multi_process_network_packets() continue; if (playerId >= Players.size()) continue; - if (pkt->wCheck != LoadBE32("\0\0ip")) + if (pkt->wCheck != HeaderCheckVal) continue; - if (pkt->wLen != dwMsgSize) + if (SDL_SwapLE16(pkt->wLen) != dwMsgSize) continue; Player &player = Players[playerId]; if (!IsNetPlayerValid(player)) { @@ -618,10 +626,10 @@ void multi_process_network_packets() player.position.last = syncPosition; if (&player != MyPlayer) { assert(gbBufferMsgs != 2); - player._pHitPoints = pkt->php; - player._pMaxHP = pkt->pmhp; - player._pMana = pkt->mana; - player._pMaxMana = pkt->maxmana; + player._pHitPoints = SDL_SwapLE32(pkt->php); + player._pMaxHP = SDL_SwapLE32(pkt->pmhp); + player._pMana = SDL_SwapLE32(pkt->mana); + player._pMaxMana = SDL_SwapLE32(pkt->maxmana); bool cond = gbBufferMsgs == 1; player._pBaseStr = pkt->bstr; player._pBaseMag = pkt->bmag; @@ -666,29 +674,27 @@ void multi_send_zero_packet(size_t pnum, _cmd_id bCmd, const byte *data, size_t for (size_t offset = 0; offset < size;) { TPkt pkt {}; - pkt.hdr.wCheck = LoadBE32("\0\0ip"); + pkt.hdr.wCheck = HeaderCheckVal; auto &message = *reinterpret_cast(pkt.body); message.bCmd = bCmd; - message.wOffset = offset; + message.wOffset = SDL_SwapLE16(offset); size_t dwBody = gdwLargestMsgSize - sizeof(pkt.hdr) - sizeof(message); dwBody = std::min(dwBody, size - offset); assert(dwBody <= 0x0ffff); - message.wBytes = dwBody; + message.wBytes = SDL_SwapLE16(dwBody); - memcpy(&pkt.body[sizeof(message)], &data[offset], message.wBytes); + memcpy(&pkt.body[sizeof(message)], &data[offset], dwBody); - size_t dwMsg = sizeof(pkt.hdr); - dwMsg += sizeof(message); - dwMsg += message.wBytes; - pkt.hdr.wLen = dwMsg; + const size_t dwMsg = sizeof(pkt.hdr) + sizeof(message) + dwBody; + pkt.hdr.wLen = SDL_SwapLE16(dwMsg); if (!SNetSendMessage(pnum, &pkt, dwMsg)) { nthread_terminate_game("SNetSendMessage2"); return; } - offset += message.wBytes; + offset += dwBody; } } @@ -787,7 +793,7 @@ void recv_plrinfo(int pnum, const TCmdPlrInfoHdr &header, bool recv) } auto &packedPlayer = PackedPlayerBuffer[pnum]; - if (sgwPackPlrOffsetTbl[pnum] != header.wOffset) { + if (sgwPackPlrOffsetTbl[pnum] != SDL_SwapLE16(header.wOffset)) { sgwPackPlrOffsetTbl[pnum] = 0; if (header.wOffset != 0) { return; @@ -797,9 +803,9 @@ void recv_plrinfo(int pnum, const TCmdPlrInfoHdr &header, bool recv) SendPlayerInfo(pnum, CMD_ACK_PLRINFO); } - memcpy(reinterpret_cast(&packedPlayer) + header.wOffset, reinterpret_cast(&header) + sizeof(header), header.wBytes); + memcpy(reinterpret_cast(&packedPlayer) + SDL_SwapLE16(header.wOffset), reinterpret_cast(&header) + sizeof(header), SDL_SwapLE16(header.wBytes)); - sgwPackPlrOffsetTbl[pnum] += header.wBytes; + sgwPackPlrOffsetTbl[pnum] += SDL_SwapLE16(header.wBytes); if (sgwPackPlrOffsetTbl[pnum] != sizeof(packedPlayer)) { return; } diff --git a/Source/sync.cpp b/Source/sync.cpp index d814d92b0..e22815f4b 100644 --- a/Source/sync.cpp +++ b/Source/sync.cpp @@ -42,7 +42,7 @@ void SyncMonsterPos(TSyncMonster &monsterSync, int ndx) monsterSync._menemy = encode_enemy(monster); monsterSync._mdelta = sgnMonsterPriority[ndx] > 255 ? 255 : sgnMonsterPriority[ndx]; monsterSync.mWhoHit = monster.whoHit; - monsterSync._mhitpoints = monster.hitPoints; + monsterSync._mhitpoints = SDL_SwapLE32(monster.hitPoints); sgnMonsterPriority[ndx] = 0xFFFF; sgwLRU[ndx] = monster.activeForTicks == 0 ? 0xFFFF : 0xFFFE; @@ -106,27 +106,27 @@ void SyncPlrInv(TSyncHeader *pHdr) auto &item = Items[pHdr->bItemI]; pHdr->bItemX = item.position.x; pHdr->bItemY = item.position.y; - pHdr->wItemIndx = item.IDidx; + pHdr->wItemIndx = SDL_SwapLE16(item.IDidx); if (item.IDidx == IDI_EAR) { - pHdr->wItemCI = (item._iIName[0] << 8) | item._iIName[1]; - pHdr->dwItemSeed = (item._iIName[2] << 24) | (item._iIName[3] << 16) | (item._iIName[4] << 8) | item._iIName[5]; + pHdr->wItemCI = SDL_SwapLE16((item._iIName[0] << 8) | item._iIName[1]); + pHdr->dwItemSeed = SDL_SwapLE32((item._iIName[2] << 24) | (item._iIName[3] << 16) | (item._iIName[4] << 8) | item._iIName[5]); pHdr->bItemId = item._iIName[6]; pHdr->bItemDur = item._iIName[7]; pHdr->bItemMDur = item._iIName[8]; pHdr->bItemCh = item._iIName[9]; pHdr->bItemMCh = item._iIName[10]; - pHdr->wItemVal = (item._iIName[11] << 8) | ((item._iCurs - ICURS_EAR_SORCERER) << 6) | item._ivalue; - pHdr->dwItemBuff = (item._iIName[12] << 24) | (item._iIName[13] << 16) | (item._iIName[14] << 8) | item._iIName[15]; + pHdr->wItemVal = SDL_SwapLE16((item._iIName[11] << 8) | ((item._iCurs - ICURS_EAR_SORCERER) << 6) | item._ivalue); + pHdr->dwItemBuff = SDL_SwapLE32((item._iIName[12] << 24) | (item._iIName[13] << 16) | (item._iIName[14] << 8) | item._iIName[15]); } else { - pHdr->wItemCI = item._iCreateInfo; - pHdr->dwItemSeed = item._iSeed; + pHdr->wItemCI = SDL_SwapLE16(item._iCreateInfo); + pHdr->dwItemSeed = SDL_SwapLE32(item._iSeed); pHdr->bItemId = item._iIdentified ? 1 : 0; pHdr->bItemDur = item._iDurability; pHdr->bItemMDur = item._iMaxDur; pHdr->bItemCh = item._iCharges; pHdr->bItemMCh = item._iMaxCharges; if (item.IDidx == IDI_GOLD) { - pHdr->wItemVal = item._ivalue; + pHdr->wItemVal = SDL_SwapLE16(item._ivalue); } } } @@ -136,9 +136,9 @@ void SyncPlrInv(TSyncHeader *pHdr) const auto &item = MyPlayer->InvBody[sgnSyncPInv]; if (!item.isEmpty()) { pHdr->bPInvLoc = sgnSyncPInv; - pHdr->wPInvIndx = item.IDidx; - pHdr->wPInvCI = item._iCreateInfo; - pHdr->dwPInvSeed = item._iSeed; + pHdr->wPInvIndx = SDL_SwapLE16(item.IDidx); + pHdr->wPInvCI = SDL_SwapLE16(item._iCreateInfo); + pHdr->dwPInvSeed = SDL_SwapLE32(item._iSeed); pHdr->bPInvId = item._iIdentified ? 1 : 0; } @@ -282,6 +282,7 @@ uint32_t sync_all_monsters(byte *pbBuf, uint32_t dwMaxLen) pHdr->wLen += sizeof(TSyncMonster); dwMaxLen -= sizeof(TSyncMonster); } + pHdr->wLen = SDL_SwapLE16(pHdr->wLen); return dwMaxLen; } @@ -289,18 +290,19 @@ uint32_t sync_all_monsters(byte *pbBuf, uint32_t dwMaxLen) uint32_t OnSyncData(const TCmd *pCmd, size_t pnum) { const auto &header = *reinterpret_cast(pCmd); + const uint16_t wLen = SDL_SwapLE16(header.wLen); assert(gbBufferMsgs != 2); if (gbBufferMsgs == 1) { - return header.wLen + sizeof(header); + return wLen + sizeof(header); } if (pnum == MyPlayerId) { - return header.wLen + sizeof(header); + return wLen + sizeof(header); } assert(header.wLen % sizeof(TSyncMonster) == 0); - int monsterCount = header.wLen / sizeof(TSyncMonster); + int monsterCount = wLen / sizeof(TSyncMonster); uint8_t level = header.bLevel; bool syncLocalLevel = !MyPlayer->_pLvlChanging && GetLevelForMultiplayer(*MyPlayer) == level; @@ -320,7 +322,7 @@ uint32_t OnSyncData(const TCmd *pCmd, size_t pnum) } } - return header.wLen + sizeof(header); + return wLen + sizeof(header); } void sync_init() diff --git a/Source/utils/endian.hpp b/Source/utils/endian.hpp index 05a167c45..14c344b5c 100644 --- a/Source/utils/endian.hpp +++ b/Source/utils/endian.hpp @@ -15,6 +15,14 @@ constexpr uint16_t LoadLE16(const T *b) return (static_cast(b[1]) << 8) | static_cast(b[0]); } +template +constexpr uint16_t LoadBE16(const T *b) +{ + static_assert(sizeof(T) == 1, "invalid argument"); + // NOLINTNEXTLINE(readability-magic-numbers) + return (static_cast(b[0]) << 8) | static_cast(b[1]); +} + template constexpr uint32_t LoadLE32(const T *b) {