Browse Source

SDL3: Use endianness swap helpers throughout

pull/8214/head
Gleb Mazovetskiy 5 months ago
parent
commit
842b23803f
  1. 3
      Source/items/validation.cpp
  2. 19
      Source/levels/reencode_dun_cels.cpp
  3. 19
      Source/levels/town.cpp
  4. 13
      Source/loadsave.cpp
  5. 8
      Source/monster.cpp
  6. 295
      Source/msg.cpp
  7. 58
      Source/multi.cpp
  8. 205
      Source/pack.cpp
  9. 5
      Source/pfile.cpp
  10. 29
      Source/sync.cpp
  11. 40
      Source/utils/endian_swap.hpp
  12. 17
      Source/utils/language.cpp
  13. 68
      Source/utils/surface_to_pcx.cpp
  14. 15
      Source/utils/surface_to_pcx.hpp
  15. 5
      test/drlg_test.hpp
  16. 41
      test/pack_test.cpp
  17. 37
      test/writehero_test.cpp

3
Source/items/validation.cpp

@ -13,6 +13,7 @@
#include "msg.h"
#include "player.h"
#include "spells.h"
#include "utils/endian_swap.hpp"
#include "utils/is_of.hpp"
namespace devilution {
@ -184,7 +185,7 @@ bool IsItemDeltaValid(const TCmdPItem &itemDelta)
return false;
if (!InDungeonBounds({ itemDelta.x, itemDelta.y }))
return false;
const _item_indexes idx = static_cast<_item_indexes>(SDL_SwapLE16(itemDelta.def.wIndx));
const _item_indexes idx = static_cast<_item_indexes>(Swap16LE(itemDelta.def.wIndx));
if (idx == IDI_EAR)
return true;
if (!IsItemAvailable(idx))

19
Source/levels/reencode_dun_cels.cpp

@ -9,11 +9,10 @@
#include <utility>
#include <vector>
#include <SDL_endian.h>
#include "levels/dun_tile.hpp"
#include "utils/attributes.h"
#include "utils/endian_read.hpp"
#include "utils/endian_swap.hpp"
#include "utils/endian_write.hpp"
#include "utils/format_int.hpp"
#include "utils/log.hpp"
@ -208,7 +207,7 @@ size_t GetReencodedSize(const uint8_t *dungeonCels, std::span<std::pair<uint16_t
size_t frameSize;
switch (info.type) {
case TileType::TransparentSquare: {
const uint32_t srcFrameBegin = SDL_SwapLE32(srcOffsets[frame]);
const uint32_t srcFrameBegin = Swap32LE(srcOffsets[frame]);
if (info.isFloor()) {
uint8_t out[1024];
uint8_t *outIt = out;
@ -217,7 +216,7 @@ size_t GetReencodedSize(const uint8_t *dungeonCels, std::span<std::pair<uint16_t
ReencodeFloorWithFoliage(outIt, src, newType);
frameSize = outIt - out;
} else {
const uint32_t srcFrameEnd = SDL_SwapLE32(srcOffsets[frame + 1]);
const uint32_t srcFrameEnd = Swap32LE(srcOffsets[frame + 1]);
frameSize = srcFrameEnd - srcFrameBegin;
}
} break;
@ -247,8 +246,8 @@ void ReencodeDungeonCels(std::unique_ptr<std::byte[]> &dungeonCels, std::span<st
int numFoliage = 0;
LogVerbose("Re-encoding dungeon CELs: {} frames, {} bytes",
FormatInteger(SDL_SwapLE32(srcOffsets[0])),
FormatInteger(SDL_SwapLE32(srcOffsets[SDL_SwapLE32(srcOffsets[0]) + 1])));
FormatInteger(Swap32LE(srcOffsets[0])),
FormatInteger(Swap32LE(srcOffsets[Swap32LE(srcOffsets[0]) + 1])));
const size_t outSize = GetReencodedSize(srcData, frames);
std::unique_ptr<std::byte[]> result { new std::byte[outSize] };
@ -259,7 +258,7 @@ void ReencodeDungeonCels(std::unique_ptr<std::byte[]> &dungeonCels, std::span<st
for (const auto &[frame, info] : frames) {
WriteLE32(lookup, static_cast<uint32_t>(out - resultPtr));
lookup += 4;
const uint32_t srcFrameBegin = SDL_SwapLE32(srcOffsets[frame]);
const uint32_t srcFrameBegin = Swap32LE(srcOffsets[frame]);
const uint8_t *src = &srcData[srcFrameBegin];
switch (info.type) {
case TileType::TransparentSquare: {
@ -268,7 +267,7 @@ void ReencodeDungeonCels(std::unique_ptr<std::byte[]> &dungeonCels, std::span<st
ReencodeFloorWithFoliage(out, src, newType);
++numFoliage;
} else {
const uint32_t srcFrameEnd = SDL_SwapLE32(srcOffsets[frame + 1]);
const uint32_t srcFrameEnd = Swap32LE(srcOffsets[frame + 1]);
const uint32_t size = srcFrameEnd - srcFrameBegin;
std::memcpy(out, src, size);
out += size;
@ -296,8 +295,8 @@ void ReencodeDungeonCels(std::unique_ptr<std::byte[]> &dungeonCels, std::span<st
const auto *dstOffsets = reinterpret_cast<const uint32_t *>(resultPtr);
LogVerbose(" Re-encoded dungeon CELs: {} frames, {} bytes. Extracted {} foliage tiles.",
FormatInteger(SDL_SwapLE32(dstOffsets[0])),
FormatInteger(SDL_SwapLE32(dstOffsets[SDL_SwapLE32(dstOffsets[0]) + 1])),
FormatInteger(Swap32LE(dstOffsets[0])),
FormatInteger(Swap32LE(dstOffsets[Swap32LE(dstOffsets[0]) + 1])),
FormatInteger(numFoliage));
dungeonCels = std::move(result);

19
Source/levels/town.cpp

@ -11,6 +11,7 @@
#include "multi.h"
#include "player.h"
#include "quests.h"
#include "utils/endian_swap.hpp"
namespace devilution {
@ -37,13 +38,13 @@ void FillSector(const char *path, int xi, int yy)
int v3 = 218;
int v4 = 218;
const int tileId = SDL_SwapLE16(tileLayer[j * size.width + i]) - 1;
const int tileId = Swap16LE(tileLayer[j * size.width + i]) - 1;
if (tileId >= 0) {
const MegaTile mega = pMegaTiles[tileId];
v1 = SDL_SwapLE16(mega.micro1);
v2 = SDL_SwapLE16(mega.micro2);
v3 = SDL_SwapLE16(mega.micro3);
v4 = SDL_SwapLE16(mega.micro4);
v1 = Swap16LE(mega.micro1);
v2 = Swap16LE(mega.micro2);
v3 = Swap16LE(mega.micro3);
v4 = Swap16LE(mega.micro4);
}
dPiece[xx + 0][yy + 0] = v1;
@ -66,10 +67,10 @@ void FillTile(int xx, int yy, int t)
{
const MegaTile mega = pMegaTiles[t - 1];
dPiece[xx + 0][yy + 0] = SDL_SwapLE16(mega.micro1);
dPiece[xx + 1][yy + 0] = SDL_SwapLE16(mega.micro2);
dPiece[xx + 0][yy + 1] = SDL_SwapLE16(mega.micro3);
dPiece[xx + 1][yy + 1] = SDL_SwapLE16(mega.micro4);
dPiece[xx + 0][yy + 0] = Swap16LE(mega.micro1);
dPiece[xx + 1][yy + 0] = Swap16LE(mega.micro2);
dPiece[xx + 0][yy + 1] = Swap16LE(mega.micro3);
dPiece[xx + 1][yy + 1] = Swap16LE(mega.micro4);
}
/**

13
Source/loadsave.cpp

@ -40,6 +40,7 @@
#include "stores.h"
#include "utils/algorithm/container.hpp"
#include "utils/endian_read.hpp"
#include "utils/endian_swap.hpp"
#include "utils/is_of.hpp"
#include "utils/language.h"
#include "utils/status_macros.hpp"
@ -62,11 +63,11 @@ T SwapLE(T in)
{
switch (sizeof(T)) {
case 2:
return SDL_SwapLE16(in);
return Swap16LE(in);
case 4:
return SDL_SwapLE32(in);
return Swap32LE(in);
case 8:
return SDL_SwapLE64(in);
return Swap64LE(in);
default:
return in;
}
@ -77,11 +78,11 @@ T SwapBE(T in)
{
switch (sizeof(T)) {
case 2:
return SDL_SwapBE16(in);
return Swap16BE(in);
case 4:
return SDL_SwapBE32(in);
return Swap32BE(in);
case 8:
return static_cast<T>(SDL_SwapBE64(in));
return static_cast<T>(Swap64BE(in));
default:
return in;
}

8
Source/monster.cpp

@ -24,7 +24,12 @@
#include <utility>
#include <vector>
#ifdef USE_SDL3
#include <SDL3/SDL_timer.h>
#else
#include <SDL.h>
#endif
#include <expected.hpp>
#include <fmt/core.h>
@ -85,6 +90,7 @@
#include "utils/algorithm/container.hpp"
#include "utils/attributes.h"
#include "utils/cl2_to_clx.hpp"
#include "utils/endian_swap.hpp"
#include "utils/enum_traits.h"
#include "utils/file_name_generator.hpp"
#include "utils/is_of.hpp"
@ -3668,7 +3674,7 @@ tl::expected<void, std::string> SetMapMonsters(const uint16_t *dunData, Point st
for (WorldTileCoord j = 0; j < size.height; j++) {
for (WorldTileCoord i = 0; i < size.width; i++) {
auto monsterId = static_cast<uint8_t>(SDL_SwapLE16(monsterLayer[j * size.width + i]));
auto monsterId = static_cast<uint8_t>(Swap16LE(monsterLayer[j * size.width + i]));
if (monsterId != 0) {
ASSIGN_OR_RETURN(const size_t typeIndex, AddMonsterType(MonstConvTbl[monsterId - 1], PLACE_SPECIAL));
PlaceMonster(ActiveMonsterCount++, typeIndex, startPosition + Displacement { i, j });

295
Source/msg.cpp

@ -50,6 +50,7 @@
#include "sync.h"
#include "tmsg.h"
#include "towners.h"
#include "utils/endian_swap.hpp"
#include "utils/is_of.hpp"
#include "utils/language.h"
#include "utils/str_cat.hpp"
@ -391,7 +392,7 @@ bool WasPlayerCmdAlreadyRequested(_cmd_id bCmd, Point position = {}, uint16_t wP
}
const TCmdLocParam5 newSendParam = { bCmd, static_cast<uint8_t>(position.x), static_cast<uint8_t>(position.y),
SDL_SwapLE16(wParam1), SDL_SwapLE16(wParam2), SDL_SwapLE16(wParam3), SDL_SwapLE16(wParam4), SDL_SwapLE16(wParam5) };
Swap16LE(wParam1), Swap16LE(wParam2), Swap16LE(wParam3), Swap16LE(wParam4), Swap16LE(wParam5) };
if (lastSentPlayerCmd.bCmd == newSendParam.bCmd && lastSentPlayerCmd.x == newSendParam.x && lastSentPlayerCmd.y == newSendParam.y
&& lastSentPlayerCmd.wParam1 == newSendParam.wParam1 && lastSentPlayerCmd.wParam2 == newSendParam.wParam2
@ -437,7 +438,7 @@ void PrePacket()
auto *cmd = reinterpret_cast<TFakeDropPlr *>(data);
data += sizeof(*cmd);
remainingBytes -= sizeof(*cmd);
multi_player_left(cmd->bPlr, SDL_SwapLE32(cmd->dwReason));
multi_player_left(cmd->bPlr, Swap32LE(cmd->dwReason));
continue;
}
@ -636,12 +637,12 @@ const std::byte *DeltaImportMonster(const std::byte *src, const std::byte *end,
std::byte *DeltaExportSpawnedMonsters(std::byte *dst, const ankerl::unordered_dense::map<size_t, DSpawnedMonster> &spawnedMonsters)
{
uint16_t size = SDL_SwapLE16(static_cast<uint16_t>(spawnedMonsters.size()));
uint16_t size = Swap16LE(static_cast<uint16_t>(spawnedMonsters.size()));
memcpy(dst, &size, sizeof(uint16_t));
dst += sizeof(uint16_t);
for (const auto &deltaSpawnedMonster : spawnedMonsters) {
uint16_t monsterId = SDL_SwapLE16(static_cast<uint16_t>(deltaSpawnedMonster.first));
uint16_t monsterId = Swap16LE(static_cast<uint16_t>(deltaSpawnedMonster.first));
memcpy(dst, &monsterId, sizeof(uint16_t));
dst += sizeof(uint16_t);
@ -659,7 +660,7 @@ const std::byte *DeltaImportSpawnedMonsters(const std::byte *src, const std::byt
uint16_t size;
memcpy(&size, src, sizeof(uint16_t));
size = SDL_SwapLE16(size);
size = Swap16LE(size);
src += sizeof(uint16_t);
if (size > MaxMonsters)
return nullptr;
@ -671,7 +672,7 @@ const std::byte *DeltaImportSpawnedMonsters(const std::byte *src, const std::byt
for (size_t i = 0; i < size; i++) {
uint16_t monsterId;
memcpy(&monsterId, src, sizeof(uint16_t));
monsterId = SDL_SwapLE16(monsterId);
monsterId = Swap16LE(monsterId);
src += sizeof(uint16_t);
DSpawnedMonster spawnedMonster;
@ -703,7 +704,7 @@ std::byte *DeltaExportJunk(std::byte *dst)
sgJunk.quests[q].qstate = quest._qactive;
sgJunk.quests[q].qvar1 = quest._qvar1;
sgJunk.quests[q].qvar2 = quest._qvar2;
sgJunk.quests[q].qmsg = SDL_SwapLE16(static_cast<int16_t>(quest._qmsg));
sgJunk.quests[q].qmsg = Swap16LE(static_cast<int16_t>(quest._qmsg));
memcpy(dst, &sgJunk.quests[q], sizeof(MultiQuests));
dst += sizeof(MultiQuests);
q++;
@ -856,7 +857,7 @@ void DeltaLoadMonsters(const DLevel &deltaLevel)
ChangeLightXY(monster.lightId, position);
}
monster.hitPoints = SDL_SwapLE32(deltaMonster.hitPoints);
monster.hitPoints = Swap32LE(deltaMonster.hitPoints);
monster.whoHit = deltaMonster.mWhoHit;
if (deltaMonster.hitPoints != 0)
continue;
@ -923,9 +924,9 @@ void DeltaLoadItems(const DLevel &deltaLevel)
if (deltaItem.bCmd == TCmdPItem::PickedUpItem) {
const int activeItemIndex = FindGetItem(
SDL_SwapLE32(deltaItem.def.dwSeed),
static_cast<_item_indexes>(SDL_SwapLE16(deltaItem.def.wIndx)),
SDL_SwapLE16(deltaItem.def.wCI));
Swap32LE(deltaItem.def.dwSeed),
static_cast<_item_indexes>(Swap16LE(deltaItem.def.wIndx)),
Swap16LE(deltaItem.def.wCI));
if (activeItemIndex != -1) {
const auto &position = Items[ActiveItems[activeItemIndex]].position;
if (dItem[position.x][position.y] == ActiveItems[activeItemIndex] + 1)
@ -949,8 +950,8 @@ void DeltaLoadItems(const DLevel &deltaLevel)
size_t OnLevelData(const TCmdPlrInfoHdr &message, size_t maxCmdSize, const Player &player)
{
const uint16_t wBytes = SDL_SwapLE16(message.wBytes);
const uint16_t wOffset = SDL_SwapLE16(message.wOffset);
const uint16_t wBytes = Swap16LE(message.wBytes);
const uint16_t wOffset = Swap16LE(message.wOffset);
if (!ValidateCmdSize(wBytes + sizeof(message), maxCmdSize, player.getId()))
return maxCmdSize;
@ -1151,8 +1152,8 @@ void NetSendCmdGItem2(bool usonly, _cmd_id bCmd, uint8_t mast, uint8_t pnum, con
auto ticks = static_cast<int32_t>(SDL_GetTicks());
if (cmd.dwTime == 0) {
cmd.dwTime = SDL_SwapLE32(ticks);
} else if (ticks - SDL_SwapLE32(cmd.dwTime) > 5000) {
cmd.dwTime = Swap32LE(ticks);
} else if (ticks - Swap32LE(cmd.dwTime) > 5000) {
return;
}
@ -1170,8 +1171,8 @@ bool NetSendCmdReq2(_cmd_id bCmd, const Player &player, uint8_t pnum, const TCmd
auto ticks = static_cast<int32_t>(SDL_GetTicks());
if (cmd.dwTime == 0)
cmd.dwTime = SDL_SwapLE32(ticks);
else if (ticks - SDL_SwapLE32(cmd.dwTime) > 5000)
cmd.dwTime = Swap32LE(ticks);
else if (ticks - Swap32LE(cmd.dwTime) > 5000)
return false;
tmsg_add(reinterpret_cast<std::byte *>(&cmd), sizeof(cmd));
@ -1207,7 +1208,7 @@ size_t OnAddStrength(const TCmdParam1 &message, Player &player)
if (gbBufferMsgs == 1)
BufferMessage(player, &message, sizeof(message));
else if (message.wParam1 <= 256)
ModifyPlrStr(player, SDL_SwapLE16(message.wParam1));
ModifyPlrStr(player, Swap16LE(message.wParam1));
return sizeof(message);
}
@ -1217,7 +1218,7 @@ size_t OnAddMagic(const TCmdParam1 &message, Player &player)
if (gbBufferMsgs == 1)
BufferMessage(player, &message, sizeof(message));
else if (message.wParam1 <= 256)
ModifyPlrMag(player, SDL_SwapLE16(message.wParam1));
ModifyPlrMag(player, Swap16LE(message.wParam1));
return sizeof(message);
}
@ -1227,7 +1228,7 @@ size_t OnAddDexterity(const TCmdParam1 &message, Player &player)
if (gbBufferMsgs == 1)
BufferMessage(player, &message, sizeof(message));
else if (message.wParam1 <= 256)
ModifyPlrDex(player, SDL_SwapLE16(message.wParam1));
ModifyPlrDex(player, Swap16LE(message.wParam1));
return sizeof(message);
}
@ -1237,7 +1238,7 @@ size_t OnAddVitality(const TCmdParam1 &message, Player &player)
if (gbBufferMsgs == 1)
BufferMessage(player, &message, sizeof(message));
else if (message.wParam1 <= 256)
ModifyPlrVit(player, SDL_SwapLE16(message.wParam1));
ModifyPlrVit(player, Swap16LE(message.wParam1));
return sizeof(message);
}
@ -1246,10 +1247,10 @@ size_t OnGotoGetItem(const TCmdLocParam1 &message, Player &player)
{
const Point position { message.x, message.y };
if (gbBufferMsgs != 1 && player.isOnActiveLevel() && InDungeonBounds(position) && SDL_SwapLE16(message.wParam1) < MAXITEMS + 1) {
if (gbBufferMsgs != 1 && player.isOnActiveLevel() && InDungeonBounds(position) && Swap16LE(message.wParam1) < MAXITEMS + 1) {
MakePlrPath(player, position, false);
player.destAction = ACTION_PICKUPITEM;
player.destParam1 = SDL_SwapLE16(message.wParam1);
player.destParam1 = Swap16LE(message.wParam1);
}
return sizeof(message);
@ -1269,7 +1270,7 @@ bool IsGItemValid(const TCmdGItem &message)
if (!InDungeonBounds({ message.x, message.y }))
return false;
return IsItemAvailable(static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx)));
return IsItemAvailable(static_cast<_item_indexes>(Swap16LE(message.def.wIndx)));
}
bool IsPItemValid(const TCmdPItem &message, const Player &player)
@ -1282,11 +1283,11 @@ bool IsPItemValid(const TCmdPItem &message, const Player &player)
if (!InDungeonBounds(position))
return false;
auto idx = static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx));
auto idx = static_cast<_item_indexes>(Swap16LE(message.def.wIndx));
if (idx != IDI_EAR) {
const uint16_t creationFlags = SDL_SwapLE16(message.item.wCI);
const uint32_t dwBuff = SDL_SwapLE16(message.item.dwBuff);
const uint16_t creationFlags = Swap16LE(message.item.wCI);
const uint32_t dwBuff = Swap16LE(message.item.dwBuff);
if (idx != IDI_GOLD)
ValidateField(creationFlags, IsCreationFlagComboValid(creationFlags));
@ -1305,9 +1306,9 @@ bool IsPItemValid(const TCmdPItem &message, const Player &player)
void PrepareItemForNetwork(const Item &item, TCmdGItem &message)
{
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);
message.def.wIndx = static_cast<_item_indexes>(Swap16LE(item.IDidx));
message.def.wCI = Swap16LE(item._iCreateInfo);
message.def.dwSeed = Swap32LE(item._iSeed);
if (item.IDidx == IDI_EAR)
PrepareEarForNetwork(item, message.ear);
@ -1317,9 +1318,9 @@ void PrepareItemForNetwork(const Item &item, TCmdGItem &message)
void PrepareItemForNetwork(const Item &item, TCmdPItem &message)
{
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);
message.def.wIndx = static_cast<_item_indexes>(Swap16LE(item.IDidx));
message.def.wCI = Swap16LE(item._iCreateInfo);
message.def.dwSeed = Swap32LE(item._iSeed);
if (item.IDidx == IDI_EAR)
PrepareEarForNetwork(item, message.ear);
@ -1329,9 +1330,9 @@ void PrepareItemForNetwork(const Item &item, TCmdPItem &message)
void PrepareItemForNetwork(const Item &item, TCmdChItem &message)
{
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);
message.def.wIndx = static_cast<_item_indexes>(Swap16LE(item.IDidx));
message.def.wCI = Swap16LE(item._iCreateInfo);
message.def.dwSeed = Swap32LE(item._iSeed);
if (item.IDidx == IDI_EAR)
PrepareEarForNetwork(item, message.ear);
@ -1341,16 +1342,16 @@ void PrepareItemForNetwork(const Item &item, TCmdChItem &message)
void RecreateItem(const Player &player, const TCmdPItem &message, Item &item)
{
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);
if (message.def.wIndx == Swap16LE(IDI_EAR))
RecreateEar(item, Swap16LE(message.ear.wCI), Swap32LE(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 == SDL_SwapLE16(IDI_EAR))
RecreateEar(item, SDL_SwapLE16(message.ear.wCI), SDL_SwapLE32(message.ear.dwSeed), message.ear.bCursval, message.ear.heroname);
if (message.def.wIndx == Swap16LE(IDI_EAR))
RecreateEar(item, Swap16LE(message.ear.wCI), Swap32LE(message.ear.dwSeed), message.ear.bCursval, message.ear.heroname);
else
RecreateItem(player, message.item, item);
}
@ -1359,26 +1360,26 @@ int SyncDropItem(Point position, const TItem &item)
{
return SyncDropItem(
position,
static_cast<_item_indexes>(SDL_SwapLE16(item.wIndx)),
SDL_SwapLE16(item.wCI),
SDL_SwapLE32(item.dwSeed),
static_cast<_item_indexes>(Swap16LE(item.wIndx)),
Swap16LE(item.wCI),
Swap32LE(item.dwSeed),
item.bId,
item.bDur,
item.bMDur,
item.bCh,
item.bMCh,
SDL_SwapLE16(item.wValue),
SDL_SwapLE32(item.dwBuff),
SDL_SwapLE16(item.wToHit),
SDL_SwapLE16(item.wMaxDam));
Swap16LE(item.wValue),
Swap32LE(item.dwBuff),
Swap16LE(item.wToHit),
Swap16LE(item.wMaxDam));
}
int SyncDropEar(Point position, const TEar &ear)
{
return SyncDropEar(
position,
SDL_SwapLE16(ear.wCI),
SDL_SwapLE32(ear.dwSeed),
Swap16LE(ear.wCI),
Swap32LE(ear.dwSeed),
ear.bCursval,
ear.heroname);
}
@ -1417,9 +1418,9 @@ size_t OnRequestGetItem(const TCmdGItem &message, Player &player)
return sizeof(message);
const Point position { message.x, message.y };
const uint32_t dwSeed = SDL_SwapLE32(message.def.dwSeed);
const uint16_t wCI = SDL_SwapLE16(message.def.wCI);
const auto wIndx = static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx));
const uint32_t dwSeed = Swap32LE(message.def.dwSeed);
const uint16_t wCI = Swap16LE(message.def.wCI);
const auto wIndx = static_cast<_item_indexes>(Swap16LE(message.def.wIndx));
if (!GetItemRecord(dwSeed, wCI, wIndx))
return sizeof(message);
@ -1464,9 +1465,9 @@ size_t OnGetItem(const TCmdGItem &message, Player &player)
return sizeof(message);
const Point position { message.x, message.y };
const uint32_t dwSeed = SDL_SwapLE32(message.def.dwSeed);
const uint16_t wCI = SDL_SwapLE16(message.def.wCI);
const auto wIndx = static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx));
const uint32_t dwSeed = Swap32LE(message.def.dwSeed);
const uint16_t wCI = Swap16LE(message.def.wCI);
const auto wIndx = static_cast<_item_indexes>(Swap16LE(message.def.wIndx));
if (!DeltaGetItem(message, message.bLevel)) {
NetSendCmdGItem2(true, CMD_GETITEM, message.bMaster, message.bPnum, message);
return sizeof(message);
@ -1497,7 +1498,7 @@ size_t OnGotoAutoGetItem(const TCmdLocParam1 &message, Player &player)
{
const Point position { message.x, message.y };
const uint16_t itemIdx = SDL_SwapLE16(message.wParam1);
const uint16_t itemIdx = Swap16LE(message.wParam1);
if (gbBufferMsgs != 1 && player.isOnActiveLevel() && InDungeonBounds(position) && itemIdx < MAXITEMS + 1) {
MakePlrPath(player, position, false);
player.destAction = ACTION_PICKUPAITEM;
@ -1511,9 +1512,9 @@ size_t OnRequestAutoGetItem(const TCmdGItem &message, Player &player)
{
if (gbBufferMsgs != 1 && player.isLevelOwnedByLocalClient() && IsGItemValid(message)) {
const Point position { message.x, message.y };
const uint32_t dwSeed = SDL_SwapLE32(message.def.dwSeed);
const uint16_t wCI = SDL_SwapLE16(message.def.wCI);
const auto wIndx = static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx));
const uint32_t dwSeed = Swap32LE(message.def.dwSeed);
const uint16_t wCI = Swap16LE(message.def.wCI);
const auto wIndx = static_cast<_item_indexes>(Swap16LE(message.def.wIndx));
if (GetItemRecord(dwSeed, wCI, wIndx)) {
if (FindGetItem(dwSeed, wIndx, wCI) != -1) {
NetSendCmdGItem2(false, CMD_AGETITEM, MyPlayerId, message.bPnum, message);
@ -1552,7 +1553,7 @@ size_t OnAutoGetItem(const TCmdGItem &message, Player &player)
return sizeof(message);
if (message.bPnum != MyPlayerId) {
SyncGetItem(position, SDL_SwapLE32(message.def.dwSeed), static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx)), SDL_SwapLE16(message.def.wCI));
SyncGetItem(position, Swap32LE(message.def.dwSeed), static_cast<_item_indexes>(Swap16LE(message.def.wIndx)), Swap16LE(message.def.wCI));
return sizeof(message);
}
@ -1575,7 +1576,7 @@ size_t OnItemExtra(const TCmdGItem &message, Player &player)
DeltaGetItem(message, message.bLevel);
if (player.isOnActiveLevel()) {
const Point position { message.x, message.y };
SyncGetItem(position, SDL_SwapLE32(message.def.dwSeed), static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx)), SDL_SwapLE16(message.def.wCI));
SyncGetItem(position, Swap32LE(message.def.dwSeed), static_cast<_item_indexes>(Swap16LE(message.def.wIndx)), Swap16LE(message.def.wCI));
}
}
@ -1589,9 +1590,9 @@ size_t OnPutItem(const TCmdPItem &message, Player &player)
} else if (IsPItemValid(message, player)) {
const Point position { message.x, message.y };
const bool isSelf = &player == MyPlayer;
const int32_t dwSeed = SDL_SwapLE32(message.def.dwSeed);
const uint16_t wCI = SDL_SwapLE16(message.def.wCI);
const auto wIndx = static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx));
const int32_t dwSeed = Swap32LE(message.def.dwSeed);
const uint16_t wCI = Swap16LE(message.def.wCI);
const auto wIndx = static_cast<_item_indexes>(Swap16LE(message.def.wIndx));
if (player.isOnActiveLevel()) {
int ii;
if (isSelf) {
@ -1624,9 +1625,9 @@ size_t OnSyncPutItem(const TCmdPItem &message, Player &player)
if (gbBufferMsgs == 1)
BufferMessage(player, &message, sizeof(message));
else if (IsPItemValid(message, player)) {
const int32_t dwSeed = SDL_SwapLE32(message.def.dwSeed);
const uint16_t wCI = SDL_SwapLE16(message.def.wCI);
const auto wIndx = static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx));
const int32_t dwSeed = Swap32LE(message.def.dwSeed);
const uint16_t wCI = Swap16LE(message.def.wCI);
const auto wIndx = static_cast<_item_indexes>(Swap16LE(message.def.wIndx));
if (player.isOnActiveLevel()) {
const int ii = SyncDropItem(message);
if (ii != -1) {
@ -1690,9 +1691,9 @@ size_t OnRangedAttackTile(const TCmdLoc &message, Player &player)
bool InitNewSpell(Player &player, uint16_t wParamSpellID, uint16_t wParamSpellType, uint16_t wParamSpellFrom)
{
wParamSpellID = SDL_SwapLE16(wParamSpellID);
wParamSpellType = SDL_SwapLE16(wParamSpellType);
wParamSpellFrom = SDL_SwapLE16(wParamSpellFrom);
wParamSpellID = Swap16LE(wParamSpellID);
wParamSpellType = Swap16LE(wParamSpellType);
wParamSpellFrom = Swap16LE(wParamSpellFrom);
if (wParamSpellID > static_cast<int8_t>(SpellID::LAST))
return false;
@ -1732,7 +1733,7 @@ size_t OnSpellWall(const TCmdLocParam4 &message, Player &player)
return sizeof(message);
if (!InDungeonBounds(position))
return sizeof(message);
const int16_t wParamDirection = SDL_SwapLE16(message.wParam3);
const int16_t wParamDirection = Swap16LE(message.wParam3);
if (wParamDirection > static_cast<uint16_t>(Direction::SouthEast))
return sizeof(message);
@ -1804,7 +1805,7 @@ size_t OnObjectTileAction(const TCmdLoc &message, Player &player)
size_t OnAttackMonster(const TCmdParam1 &message, Player &player)
{
const uint16_t monsterIdx = SDL_SwapLE16(message.wParam1);
const uint16_t monsterIdx = Swap16LE(message.wParam1);
if (gbBufferMsgs != 1 && player.isOnActiveLevel() && leveltype != DTYPE_TOWN && monsterIdx < MaxMonsters) {
const Point position = Monsters[monsterIdx].position.future;
@ -1819,7 +1820,7 @@ size_t OnAttackMonster(const TCmdParam1 &message, Player &player)
size_t OnAttackPlayer(const TCmdParam1 &message, Player &player)
{
const uint16_t playerIdx = SDL_SwapLE16(message.wParam1);
const uint16_t playerIdx = Swap16LE(message.wParam1);
if (gbBufferMsgs != 1 && player.isOnActiveLevel() && leveltype != DTYPE_TOWN && playerIdx < Players.size()) {
MakePlrPath(player, Players[playerIdx].position.future, false);
@ -1832,7 +1833,7 @@ size_t OnAttackPlayer(const TCmdParam1 &message, Player &player)
size_t OnRangedAttackMonster(const TCmdParam1 &message, Player &player)
{
const uint16_t monsterIdx = SDL_SwapLE16(message.wParam1);
const uint16_t monsterIdx = Swap16LE(message.wParam1);
if (gbBufferMsgs != 1 && player.isOnActiveLevel() && leveltype != DTYPE_TOWN && monsterIdx < MaxMonsters) {
ClrPlrPath(player);
@ -1845,7 +1846,7 @@ size_t OnRangedAttackMonster(const TCmdParam1 &message, Player &player)
size_t OnRangedAttackPlayer(const TCmdParam1 &message, Player &player)
{
const uint16_t playerIdx = SDL_SwapLE16(message.wParam1);
const uint16_t playerIdx = Swap16LE(message.wParam1);
if (gbBufferMsgs != 1 && player.isOnActiveLevel() && leveltype != DTYPE_TOWN && playerIdx < Players.size()) {
ClrPlrPath(player);
@ -1864,7 +1865,7 @@ size_t OnSpellMonster(const TCmdParam4 &message, Player &player)
return sizeof(message);
if (leveltype == DTYPE_TOWN)
return sizeof(message);
const uint16_t monsterIdx = SDL_SwapLE16(message.wParam1);
const uint16_t monsterIdx = Swap16LE(message.wParam1);
if (monsterIdx >= MaxMonsters)
return sizeof(message);
@ -1885,7 +1886,7 @@ size_t OnSpellPlayer(const TCmdParam4 &message, Player &player)
return sizeof(message);
if (!player.isOnActiveLevel())
return sizeof(message);
const uint16_t playerIdx = SDL_SwapLE16(message.wParam1);
const uint16_t playerIdx = Swap16LE(message.wParam1);
if (playerIdx >= Players.size())
return sizeof(message);
@ -1902,7 +1903,7 @@ size_t OnSpellPlayer(const TCmdParam4 &message, Player &player)
size_t OnKnockback(const TCmdParam1 &message, Player &player)
{
const uint16_t monsterIdx = SDL_SwapLE16(message.wParam1);
const uint16_t monsterIdx = Swap16LE(message.wParam1);
if (gbBufferMsgs != 1 && player.isOnActiveLevel() && leveltype != DTYPE_TOWN && monsterIdx < MaxMonsters) {
Monster &monster = Monsters[monsterIdx];
@ -1915,7 +1916,7 @@ size_t OnKnockback(const TCmdParam1 &message, Player &player)
size_t OnResurrect(const TCmdParam1 &message, Player &player)
{
const uint16_t playerIdx = SDL_SwapLE16(message.wParam1);
const uint16_t playerIdx = Swap16LE(message.wParam1);
if (gbBufferMsgs == 1) {
BufferMessage(player, &message, sizeof(message));
@ -1930,7 +1931,7 @@ size_t OnResurrect(const TCmdParam1 &message, Player &player)
size_t OnHealOther(const TCmdParam1 &message, const Player &caster)
{
const uint16_t playerIdx = SDL_SwapLE16(message.wParam1);
const uint16_t playerIdx = Swap16LE(message.wParam1);
if (gbBufferMsgs != 1) {
if (caster.isOnActiveLevel() && playerIdx < Players.size()) {
@ -1944,7 +1945,7 @@ size_t OnHealOther(const TCmdParam1 &message, const Player &caster)
size_t OnTalkXY(const TCmdLocParam1 &message, Player &player)
{
const Point position { message.x, message.y };
const uint16_t townerIdx = SDL_SwapLE16(message.wParam1);
const uint16_t townerIdx = Swap16LE(message.wParam1);
if (gbBufferMsgs != 1 && player.isOnActiveLevel() && InDungeonBounds(position) && townerIdx < NUM_TOWNERS) {
MakePlrPath(player, position, false);
@ -1957,7 +1958,7 @@ size_t OnTalkXY(const TCmdLocParam1 &message, Player &player)
size_t OnNewLevel(const TCmdParam2 &message, Player &player)
{
const uint16_t eventIdx = SDL_SwapLE16(message.wParam1);
const uint16_t eventIdx = Swap16LE(message.wParam1);
if (gbBufferMsgs == 1) {
BufferMessage(player, &message, sizeof(message));
@ -1967,7 +1968,7 @@ size_t OnNewLevel(const TCmdParam2 &message, Player &player)
auto mode = static_cast<interface_mode>(eventIdx);
const auto levelId = static_cast<uint8_t>(SDL_SwapLE16(message.wParam2));
const auto levelId = static_cast<uint8_t>(Swap16LE(message.wParam2));
if (!IsValidLevel(levelId, mode == WM_DIABSETLVL)) {
return sizeof(message);
}
@ -1980,7 +1981,7 @@ size_t OnNewLevel(const TCmdParam2 &message, Player &player)
size_t OnWarp(const TCmdParam1 &message, Player &player)
{
const uint16_t portalIdx = SDL_SwapLE16(message.wParam1);
const uint16_t portalIdx = Swap16LE(message.wParam1);
if (gbBufferMsgs == 1) {
BufferMessage(player, &message, sizeof(message));
@ -1994,7 +1995,7 @@ size_t OnWarp(const TCmdParam1 &message, Player &player)
size_t OnMonstDeath(const TCmdLocParam1 &message, Player &player)
{
const Point position { message.x, message.y };
const uint16_t monsterIdx = SDL_SwapLE16(message.wParam1);
const uint16_t monsterIdx = Swap16LE(message.wParam1);
if (gbBufferMsgs != 1) {
if (&player != MyPlayer && player.plrlevel > 0 && InDungeonBounds(position) && monsterIdx < MaxMonsters) {
@ -2025,7 +2026,7 @@ size_t OnRequestSpawnGolem(const TCmdLocParam1 &message, const Player &player)
size_t OnMonstDamage(const TCmdMonDamage &message, Player &player)
{
const uint16_t monsterIdx = SDL_SwapLE16(message.wMon);
const uint16_t monsterIdx = Swap16LE(message.wMon);
if (gbBufferMsgs != 1) {
if (&player != MyPlayer) {
@ -2033,7 +2034,7 @@ size_t OnMonstDamage(const TCmdMonDamage &message, Player &player)
Monster &monster = Monsters[monsterIdx];
monster.tag(player);
if (monster.hitPoints > 0) {
monster.hitPoints -= SDL_SwapLE32(message.dwDam);
monster.hitPoints -= Swap32LE(message.dwDam);
if ((monster.hitPoints >> 6) < 1)
monster.hitPoints = 1 << 6;
delta_monster_hp(monster, player);
@ -2049,7 +2050,7 @@ size_t OnMonstDamage(const TCmdMonDamage &message, Player &player)
size_t OnPlayerDeath(const TCmdParam1 &message, Player &player)
{
const auto deathReason = static_cast<DeathReason>(SDL_SwapLE16(message.wParam1));
const auto deathReason = static_cast<DeathReason>(Swap16LE(message.wParam1));
if (gbBufferMsgs != 1) {
if (&player != MyPlayer)
@ -2065,7 +2066,7 @@ size_t OnPlayerDeath(const TCmdParam1 &message, Player &player)
size_t OnPlayerDamage(const TCmdDamage &message, Player &player)
{
const uint32_t damage = SDL_SwapLE32(message.dwDam);
const uint32_t damage = Swap32LE(message.dwDam);
Player &target = Players[message.bPlr];
if (&target == MyPlayer && leveltype != DTYPE_TOWN && gbBufferMsgs != 1) {
@ -2126,7 +2127,7 @@ size_t OnChangePlayerItems(const TCmdChItem &message, Player &player)
if (gbBufferMsgs == 1) {
BufferMessage(player, &message, sizeof(message));
} else if (&player != MyPlayer && IsItemAvailable(static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx)))) {
} else if (&player != MyPlayer && IsItemAvailable(static_cast<_item_indexes>(Swap16LE(message.def.wIndx)))) {
Item &item = player.InvBody[message.bLoc];
item = {};
RecreateItem(player, message, item);
@ -2157,7 +2158,7 @@ size_t OnChangeInventoryItems(const TCmdChItem &message, Player &player)
if (gbBufferMsgs == 1) {
BufferMessage(player, &message, sizeof(message));
} else if (&player != MyPlayer && IsItemAvailable(static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx)))) {
} else if (&player != MyPlayer && IsItemAvailable(static_cast<_item_indexes>(Swap16LE(message.def.wIndx)))) {
Item item {};
RecreateItem(player, message, item);
CheckInvSwap(player, item, message.bLoc);
@ -2168,7 +2169,7 @@ size_t OnChangeInventoryItems(const TCmdChItem &message, Player &player)
size_t OnDeleteInventoryItems(const TCmdParam1 &message, Player &player)
{
const uint16_t invGridIndex = SDL_SwapLE16(message.wParam1);
const uint16_t invGridIndex = Swap16LE(message.wParam1);
if (gbBufferMsgs == 1) {
BufferMessage(player, &message, sizeof(message));
@ -2186,7 +2187,7 @@ size_t OnChangeBeltItems(const TCmdChItem &message, Player &player)
if (gbBufferMsgs == 1) {
BufferMessage(player, &message, sizeof(message));
} else if (&player != MyPlayer && IsItemAvailable(static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx)))) {
} else if (&player != MyPlayer && IsItemAvailable(static_cast<_item_indexes>(Swap16LE(message.def.wIndx)))) {
Item &item = player.SpdList[message.bLoc];
item = {};
RecreateItem(player, message, item);
@ -2197,7 +2198,7 @@ size_t OnChangeBeltItems(const TCmdChItem &message, Player &player)
size_t OnDeleteBeltItems(const TCmdParam1 &message, Player &player)
{
const uint16_t spdBarIndex = SDL_SwapLE16(message.wParam1);
const uint16_t spdBarIndex = Swap16LE(message.wParam1);
if (gbBufferMsgs == 1) {
BufferMessage(player, &message, sizeof(message));
@ -2210,7 +2211,7 @@ size_t OnDeleteBeltItems(const TCmdParam1 &message, Player &player)
size_t OnPlayerLevel(const TCmdParam1 &message, Player &player)
{
const uint16_t playerLevel = SDL_SwapLE16(message.wParam1);
const uint16_t playerLevel = Swap16LE(message.wParam1);
if (gbBufferMsgs != 1) {
if (playerLevel <= player.getMaxCharacterLevel() && &player != MyPlayer)
@ -2241,7 +2242,7 @@ size_t OnSpawnItem(const TCmdPItem &message, Player &player)
if (player.isOnActiveLevel() && &player != MyPlayer) {
SyncDropItem(message);
}
PutItemRecord(SDL_SwapLE32(message.def.dwSeed), SDL_SwapLE16(message.def.wCI), static_cast<_item_indexes>(SDL_SwapLE16(message.def.wIndx)));
PutItemRecord(Swap32LE(message.def.dwSeed), Swap16LE(message.def.wCI), static_cast<_item_indexes>(Swap16LE(message.def.wIndx)));
DeltaPutItem(message, { message.x, message.y }, player);
}
@ -2250,7 +2251,7 @@ size_t OnSpawnItem(const TCmdPItem &message, Player &player)
size_t OnSendPlayerInfo(const TCmdPlrInfoHdr &header, size_t maxCmdSize, Player &player)
{
const uint16_t wBytes = SDL_SwapLE16(header.wBytes);
const uint16_t wBytes = Swap16LE(header.wBytes);
if (!ValidateCmdSize(wBytes + sizeof(header), maxCmdSize, player.getId()))
return maxCmdSize;
@ -2272,7 +2273,7 @@ size_t OnPlayerJoinLevel(const TCmdLocParam2 &message, Player &player)
return sizeof(message);
}
const auto playerLevel = static_cast<uint8_t>(SDL_SwapLE16(message.wParam1));
const auto playerLevel = static_cast<uint8_t>(Swap16LE(message.wParam1));
const bool isSetLevel = message.wParam2 != 0;
if (!IsValidLevel(playerLevel, isSetLevel) || !InDungeonBounds(position)) {
return sizeof(message);
@ -2321,8 +2322,8 @@ size_t OnPlayerJoinLevel(const TCmdLocParam2 &message, Player &player)
size_t OnActivatePortal(const TCmdLocParam3 &message, Player &player)
{
const Point position { message.x, message.y };
const auto level = static_cast<uint8_t>(SDL_SwapLE16(message.wParam1));
const uint16_t dungeonTypeIdx = SDL_SwapLE16(message.wParam2);
const auto level = static_cast<uint8_t>(Swap16LE(message.wParam1));
const uint16_t dungeonTypeIdx = Swap16LE(message.wParam2);
const bool isSetLevel = message.wParam3 != 0;
if (gbBufferMsgs == 1) {
@ -2386,7 +2387,7 @@ size_t OnRestartTown(const TCmd &cmd, Player &player)
size_t OnSetStrength(const TCmdParam1 &message, Player &player)
{
const uint16_t value = SDL_SwapLE16(message.wParam1);
const uint16_t value = Swap16LE(message.wParam1);
if (gbBufferMsgs != 1) {
if (value <= 750 && &player != MyPlayer)
@ -2400,7 +2401,7 @@ size_t OnSetStrength(const TCmdParam1 &message, Player &player)
size_t OnSetDexterity(const TCmdParam1 &message, Player &player)
{
const uint16_t value = SDL_SwapLE16(message.wParam1);
const uint16_t value = Swap16LE(message.wParam1);
if (gbBufferMsgs != 1) {
if (value <= 750 && &player != MyPlayer)
@ -2414,7 +2415,7 @@ size_t OnSetDexterity(const TCmdParam1 &message, Player &player)
size_t OnSetMagic(const TCmdParam1 &message, Player &player)
{
const uint16_t value = SDL_SwapLE16(message.wParam1);
const uint16_t value = Swap16LE(message.wParam1);
if (gbBufferMsgs != 1) {
if (value <= 750 && &player != MyPlayer)
@ -2428,7 +2429,7 @@ size_t OnSetMagic(const TCmdParam1 &message, Player &player)
size_t OnSetVitality(const TCmdParam1 &message, Player &player)
{
const uint16_t value = SDL_SwapLE16(message.wParam1);
const uint16_t value = Swap16LE(message.wParam1);
if (gbBufferMsgs != 1) {
if (value <= 750 && &player != MyPlayer)
@ -2469,7 +2470,7 @@ size_t OnSyncQuest(const TCmdQuest &message, Player &player)
BufferMessage(player, &message, sizeof(message));
} else {
if (&player != MyPlayer && message.q < MAXQUESTS && message.qstate <= QUEST_HIVE_DONE)
SetMultiQuest(message.q, message.qstate, message.qlog != 0, message.qvar1, message.qvar2, SDL_SwapLE16(message.qmsg));
SetMultiQuest(message.q, message.qstate, message.qlog != 0, message.qvar1, message.qvar2, Swap16LE(message.qmsg));
}
return sizeof(message);
@ -2493,8 +2494,8 @@ size_t OnCheatExperience(const TCmd &cmd, Player &player) // NOLINT(misc-unused-
size_t OnChangeSpellLevel(const TCmdParam2 &message, Player &player) // NOLINT(misc-unused-parameters)
{
const auto spellID = static_cast<SpellID>(SDL_SwapLE16(message.wParam1));
const uint8_t spellLevel = std::min(static_cast<uint8_t>(SDL_SwapLE16(message.wParam2)), MaxSpellLevel);
const auto spellID = static_cast<SpellID>(Swap16LE(message.wParam1));
const uint8_t spellLevel = std::min(static_cast<uint8_t>(Swap16LE(message.wParam2)), MaxSpellLevel);
if (gbBufferMsgs == 1) {
BufferMessage(player, &message, sizeof(message));
@ -2530,7 +2531,7 @@ size_t OnRemoveShield(const TCmd &cmd, Player &player)
size_t OnSetReflect(const TCmdParam1 &message, Player &player)
{
if (gbBufferMsgs != 1)
player.wReflections = SDL_SwapLE16(message.wParam1);
player.wReflections = Swap16LE(message.wParam1);
return sizeof(message);
}
@ -2580,8 +2581,8 @@ size_t OnSpawnMonster(const TCmdSpawnMonster &message, const Player &player)
const WorldTilePosition position { message.x, message.y };
auto typeIndex = static_cast<size_t>(SDL_SwapLE16(message.typeIndex));
auto monsterId = static_cast<size_t>(SDL_SwapLE16(message.monsterId));
auto typeIndex = static_cast<size_t>(Swap16LE(message.typeIndex));
auto monsterId = static_cast<size_t>(Swap16LE(message.monsterId));
const uint8_t golemOwnerPlayerId = message.golemOwnerPlayerId;
if (golemOwnerPlayerId >= Players.size()) {
return sizeof(message);
@ -2652,10 +2653,10 @@ void PrepareItemForNetwork(const Item &item, TItem &messageItem)
messageItem.bMDur = item._iMaxDur;
messageItem.bCh = item._iCharges;
messageItem.bMCh = item._iMaxCharges;
messageItem.wValue = SDL_SwapLE16(item._ivalue);
messageItem.wToHit = SDL_SwapLE16(item._iPLToHit);
messageItem.wMaxDam = SDL_SwapLE16(item._iMaxDam);
messageItem.dwBuff = SDL_SwapLE32(item.dwBuff);
messageItem.wValue = Swap16LE(item._ivalue);
messageItem.wToHit = Swap16LE(item._iPLToHit);
messageItem.wMaxDam = Swap16LE(item._iMaxDam);
messageItem.dwBuff = Swap32LE(item.dwBuff);
}
void PrepareEarForNetwork(const Item &item, TEar &ear)
@ -2666,10 +2667,10 @@ void PrepareEarForNetwork(const Item &item, TEar &ear)
void RecreateItem(const Player &player, const TItem &messageItem, Item &item)
{
const uint32_t dwBuff = SDL_SwapLE32(messageItem.dwBuff);
const uint32_t dwBuff = Swap32LE(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);
static_cast<_item_indexes>(Swap16LE(messageItem.wIndx)), Swap16LE(messageItem.wCI),
Swap32LE(messageItem.dwSeed), Swap16LE(messageItem.wValue), dwBuff);
if (messageItem.bId != 0)
item._iIdentified = true;
item._iMaxDur = messageItem.bMDur;
@ -2677,8 +2678,8 @@ void RecreateItem(const Player &player, const TItem &messageItem, Item &item)
item._iMaxCharges = std::clamp<int>(messageItem.bMCh, 0, item._iMaxCharges);
item._iCharges = std::clamp<int>(messageItem.bCh, 0, item._iMaxCharges);
if (gbIsHellfire) {
item._iPLToHit = ClampToHit(item, static_cast<uint8_t>(SDL_SwapLE16(messageItem.wToHit)));
item._iMaxDam = ClampMaxDam(item, static_cast<uint8_t>(SDL_SwapLE16(messageItem.wMaxDam)));
item._iPLToHit = ClampToHit(item, static_cast<uint8_t>(Swap16LE(messageItem.wToHit)));
item._iMaxDam = ClampMaxDam(item, static_cast<uint8_t>(Swap16LE(messageItem.wMaxDam)));
}
}
@ -2691,7 +2692,7 @@ void msg_send_drop_pkt(uint8_t pnum, int reason)
{
TFakeDropPlr cmd;
cmd.dwReason = SDL_SwapLE32(reason);
cmd.dwReason = Swap32LE(reason);
cmd.bCmd = FAKE_CMD_DROPID;
cmd.bPlr = pnum;
BufferMessage(pnum, &cmd, sizeof(cmd));
@ -2803,8 +2804,8 @@ void delta_monster_hp(const Monster &monster, const Player &player)
return;
DMonsterStr *pD = &GetDeltaLevel(player).monster[monster.getId()];
if (SDL_SwapLE32(pD->hitPoints) > monster.hitPoints)
pD->hitPoints = SDL_SwapLE32(monster.hitPoints);
if (Swap32LE(pD->hitPoints) > monster.hitPoints)
pD->hitPoints = Swap32LE(monster.hitPoints);
}
void delta_sync_monster(const TSyncMonster &monsterSync, uint8_t level)
@ -2852,7 +2853,7 @@ void DeltaSyncJunk()
quest._qactive = sgJunk.quests[q].qstate;
quest._qvar1 = sgJunk.quests[q].qvar1;
quest._qvar2 = sgJunk.quests[q].qvar2;
quest._qmsg = static_cast<_speech_id>(SDL_SwapLE16(sgJunk.quests[q].qmsg));
quest._qmsg = static_cast<_speech_id>(Swap16LE(sgJunk.quests[q].qmsg));
}
q++;
}
@ -2868,9 +2869,9 @@ void DeltaAddItem(int ii)
for (const TCmdPItem &item : deltaLevel.item) {
if (item.bCmd != CMD_INVALID
&& static_cast<_item_indexes>(SDL_SwapLE16(item.def.wIndx)) == Items[ii].IDidx
&& SDL_SwapLE16(item.def.wCI) == Items[ii]._iCreateInfo
&& static_cast<uint32_t>(SDL_SwapLE32(item.def.dwSeed)) == Items[ii]._iSeed
&& static_cast<_item_indexes>(Swap16LE(item.def.wIndx)) == Items[ii].IDidx
&& Swap16LE(item.def.wCI) == Items[ii]._iCreateInfo
&& static_cast<uint32_t>(Swap32LE(item.def.dwSeed)) == Items[ii]._iSeed
&& IsAnyOf(item.bCmd, TCmdPItem::PickedUpItem, TCmdPItem::FloorItem)) {
return;
}
@ -2967,9 +2968,9 @@ void NetSendCmdSpawnMonster(Point position, Direction dir, uint16_t typeIndex, u
cmd.x = position.x;
cmd.y = position.y;
cmd.dir = dir;
cmd.typeIndex = SDL_SwapLE16(typeIndex);
cmd.monsterId = SDL_SwapLE16(monsterId);
cmd.seed = SDL_SwapLE32(seed);
cmd.typeIndex = Swap16LE(typeIndex);
cmd.monsterId = Swap16LE(monsterId);
cmd.seed = Swap32LE(seed);
cmd.golemOwnerPlayerId = golemOwnerPlayerId;
cmd.golemSpellLevel = golemSpellLevel;
NetSendHiPri(MyPlayerId, reinterpret_cast<std::byte *>(&cmd), sizeof(cmd));
@ -3003,7 +3004,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 = SDL_SwapLE16(wParam1);
cmd.wParam1 = Swap16LE(wParam1);
if (bHiPri)
NetSendHiPri(MyPlayerId, reinterpret_cast<std::byte *>(&cmd), sizeof(cmd));
else
@ -3022,8 +3023,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 = SDL_SwapLE16(wParam1);
cmd.wParam2 = SDL_SwapLE16(wParam2);
cmd.wParam1 = Swap16LE(wParam1);
cmd.wParam2 = Swap16LE(wParam2);
if (bHiPri)
NetSendHiPri(MyPlayerId, reinterpret_cast<std::byte *>(&cmd), sizeof(cmd));
else
@ -3042,9 +3043,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 = SDL_SwapLE16(wParam1);
cmd.wParam2 = SDL_SwapLE16(wParam2);
cmd.wParam3 = SDL_SwapLE16(wParam3);
cmd.wParam1 = Swap16LE(wParam1);
cmd.wParam2 = Swap16LE(wParam2);
cmd.wParam3 = Swap16LE(wParam3);
if (bHiPri)
NetSendHiPri(MyPlayerId, reinterpret_cast<std::byte *>(&cmd), sizeof(cmd));
else
@ -3063,10 +3064,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 = SDL_SwapLE16(wParam1);
cmd.wParam2 = SDL_SwapLE16(wParam2);
cmd.wParam3 = SDL_SwapLE16(wParam3);
cmd.wParam4 = SDL_SwapLE16(wParam4);
cmd.wParam1 = Swap16LE(wParam1);
cmd.wParam2 = Swap16LE(wParam2);
cmd.wParam3 = Swap16LE(wParam3);
cmd.wParam4 = Swap16LE(wParam4);
if (bHiPri)
NetSendHiPri(MyPlayerId, reinterpret_cast<std::byte *>(&cmd), sizeof(cmd));
else
@ -3083,7 +3084,7 @@ void NetSendCmdParam1(bool bHiPri, _cmd_id bCmd, uint16_t wParam1)
TCmdParam1 cmd;
cmd.bCmd = bCmd;
cmd.wParam1 = SDL_SwapLE16(wParam1);
cmd.wParam1 = Swap16LE(wParam1);
if (bHiPri)
NetSendHiPri(MyPlayerId, reinterpret_cast<std::byte *>(&cmd), sizeof(cmd));
else
@ -3097,8 +3098,8 @@ void NetSendCmdParam2(bool bHiPri, _cmd_id bCmd, uint16_t wParam1, uint16_t wPar
TCmdParam2 cmd;
cmd.bCmd = bCmd;
cmd.wParam1 = SDL_SwapLE16(wParam1);
cmd.wParam2 = SDL_SwapLE16(wParam2);
cmd.wParam1 = Swap16LE(wParam1);
cmd.wParam2 = Swap16LE(wParam2);
if (bHiPri)
NetSendHiPri(MyPlayerId, reinterpret_cast<std::byte *>(&cmd), sizeof(cmd));
else
@ -3113,10 +3114,10 @@ void NetSendCmdParam4(bool bHiPri, _cmd_id bCmd, uint16_t wParam1, uint16_t wPar
TCmdParam4 cmd;
cmd.bCmd = bCmd;
cmd.wParam1 = SDL_SwapLE16(wParam1);
cmd.wParam2 = SDL_SwapLE16(wParam2);
cmd.wParam3 = SDL_SwapLE16(wParam3);
cmd.wParam4 = SDL_SwapLE16(wParam4);
cmd.wParam1 = Swap16LE(wParam1);
cmd.wParam2 = Swap16LE(wParam2);
cmd.wParam3 = Swap16LE(wParam3);
cmd.wParam4 = Swap16LE(wParam4);
if (bHiPri)
NetSendHiPri(MyPlayerId, reinterpret_cast<std::byte *>(&cmd), sizeof(cmd));
else
@ -3134,7 +3135,7 @@ void NetSendCmdQuest(bool bHiPri, const Quest &quest)
cmd.qlog = quest._qlog ? 1 : 0;
cmd.qvar1 = quest._qvar1;
cmd.qvar2 = quest._qvar2;
cmd.qmsg = SDL_SwapLE16(quest._qmsg);
cmd.qmsg = Swap16LE(quest._qmsg);
if (bHiPri)
NetSendHiPri(MyPlayerId, reinterpret_cast<std::byte *>(&cmd), sizeof(cmd));

58
Source/multi.cpp

@ -10,7 +10,14 @@
#include <cstring>
#include <string_view>
#ifdef USE_SDL3
#include <SDL3/SDL_endian.h>
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_timer.h>
#else
#include <SDL.h>
#endif
#include <config.h>
#include <fmt/format.h>
@ -34,6 +41,7 @@
#include "sync.h"
#include "tmsg.h"
#include "utils/endian_read.hpp"
#include "utils/endian_swap.hpp"
#include "utils/is_of.hpp"
#include "utils/language.h"
#include "utils/str_cat.hpp"
@ -80,12 +88,12 @@ const event_type EventTypes[3] = {
void GameData::swapLE()
{
size = SDL_SwapLE32(size);
programid = SDL_SwapLE32(programid);
gameSeed[0] = SDL_SwapLE32(gameSeed[0]);
gameSeed[1] = SDL_SwapLE32(gameSeed[1]);
gameSeed[2] = SDL_SwapLE32(gameSeed[2]);
gameSeed[3] = SDL_SwapLE32(gameSeed[3]);
size = Swap32LE(size);
programid = Swap32LE(programid);
gameSeed[0] = Swap32LE(gameSeed[0]);
gameSeed[1] = Swap32LE(gameSeed[1]);
gameSeed[2] = Swap32LE(gameSeed[2]);
gameSeed[3] = Swap32LE(gameSeed[3]);
}
namespace {
@ -164,10 +172,10 @@ void NetReceivePlayerData(TPkt *pkt)
pkt->hdr.py = myPlayer.position.tile.y;
pkt->hdr.targx = target.x;
pkt->hdr.targy = target.y;
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.php = Swap32LE(myPlayer._pHitPoints);
pkt->hdr.pmhp = Swap32LE(myPlayer._pMaxHP);
pkt->hdr.mana = Swap32LE(myPlayer._pMana);
pkt->hdr.maxmana = Swap32LE(myPlayer._pMaxMana);
pkt->hdr.bstr = myPlayer._pBaseStr;
pkt->hdr.bmag = myPlayer._pBaseMag;
pkt->hdr.bdex = myPlayer._pBaseDex;
@ -220,7 +228,7 @@ void SendPacket(uint8_t playerId, const std::byte *packet, size_t size)
NetReceivePlayerData(&pkt);
const size_t sizeWithheader = size + sizeof(pkt.hdr);
pkt.hdr.wLen = SDL_SwapLE16(static_cast<uint16_t>(sizeWithheader));
pkt.hdr.wLen = Swap16LE(static_cast<uint16_t>(sizeWithheader));
memcpy(pkt.body, packet, size);
if (!SNetSendMessage(playerId, &pkt.hdr, sizeWithheader))
nthread_terminate_game("SNetSendMessage0");
@ -417,7 +425,7 @@ void HandleEvents(_SNETEVENT *pEvt)
int32_t leftReason = 0;
if (pEvt->data != nullptr && pEvt->databytes >= sizeof(leftReason)) {
std::memcpy(&leftReason, pEvt->data, sizeof(leftReason));
leftReason = SDL_SwapLE32(leftReason);
leftReason = Swap32LE(leftReason);
}
sgdwPlayerLeftReasonTbl[playerId] = leftReason;
if (leftReason == LEAVE_ENDING)
@ -555,7 +563,7 @@ void NetSendHiPri(uint8_t playerId, const std::byte *data, size_t size)
destination = CopyBufferedPackets(destination, &lowPriorityBuffer, &remainingSpace);
remainingSpace = sync_all_monsters(destination, remainingSpace);
const size_t len = gdwNormalMsgSize - remainingSpace;
pkt.hdr.wLen = SDL_SwapLE16(static_cast<uint16_t>(len));
pkt.hdr.wLen = Swap16LE(static_cast<uint16_t>(len));
if (!SNetSendMessage(SNPLAYER_OTHERS, &pkt.hdr, len))
nthread_terminate_game("SNetSendMessage");
}
@ -566,7 +574,7 @@ void multi_send_msg_packet(uint32_t pmask, const std::byte *data, size_t size)
TPkt pkt;
NetReceivePlayerData(&pkt);
const size_t len = size + sizeof(pkt.hdr);
pkt.hdr.wLen = SDL_SwapLE16(static_cast<uint16_t>(len));
pkt.hdr.wLen = Swap16LE(static_cast<uint16_t>(len));
memcpy(pkt.body, data, size);
uint8_t playerID = 0;
for (uint32_t v = 1; playerID < Players.size(); playerID++, v <<= 1) {
@ -660,7 +668,7 @@ void multi_process_network_packets()
continue;
if (pkt->wCheck != HeaderCheckVal)
continue;
if (SDL_SwapLE16(pkt->wLen) != dwMsgSize)
if (Swap16LE(pkt->wLen) != dwMsgSize)
continue;
Player &player = Players[playerId];
if (!IsNetPlayerValid(player)) {
@ -675,10 +683,10 @@ void multi_process_network_packets()
player.position.last = syncPosition;
if (&player != MyPlayer) {
assert(gbBufferMsgs != 2);
player._pHitPoints = SDL_SwapLE32(pkt->php);
player._pMaxHP = SDL_SwapLE32(pkt->pmhp);
player._pMana = SDL_SwapLE32(pkt->mana);
player._pMaxMana = SDL_SwapLE32(pkt->maxmana);
player._pHitPoints = Swap32LE(pkt->php);
player._pMaxHP = Swap32LE(pkt->pmhp);
player._pMana = Swap32LE(pkt->mana);
player._pMaxMana = Swap32LE(pkt->maxmana);
const bool cond = gbBufferMsgs == 1;
player._pBaseStr = pkt->bstr;
player._pBaseMag = pkt->bmag;
@ -729,18 +737,18 @@ void multi_send_zero_packet(uint8_t pnum, _cmd_id bCmd, const std::byte *data, s
auto &message = *reinterpret_cast<TCmdPlrInfoHdr *>(pkt.body);
message.bCmd = bCmd;
assert(offset <= 0x0ffff);
message.wOffset = SDL_SwapLE16(static_cast<uint16_t>(offset));
message.wOffset = Swap16LE(static_cast<uint16_t>(offset));
size_t dwBody = gdwLargestMsgSize - sizeof(pkt.hdr) - sizeof(message);
dwBody = std::min(dwBody, size - offset);
assert(dwBody <= 0x0ffff);
message.wBytes = SDL_SwapLE16(static_cast<uint16_t>(dwBody));
message.wBytes = Swap16LE(static_cast<uint16_t>(dwBody));
memcpy(&pkt.body[sizeof(message)], &data[offset], dwBody);
const size_t dwMsg = sizeof(pkt.hdr) + sizeof(message) + dwBody;
assert(dwMsg <= 0x0ffff);
pkt.hdr.wLen = SDL_SwapLE16(static_cast<uint16_t>(dwMsg));
pkt.hdr.wLen = Swap16LE(static_cast<uint16_t>(dwMsg));
if (!SNetSendMessage(pnum, &pkt, dwMsg)) {
nthread_terminate_game("SNetSendMessage2");
@ -848,7 +856,7 @@ void recv_plrinfo(Player &player, const TCmdPlrInfoHdr &header, bool recv)
const uint8_t pnum = player.getId();
auto &packedPlayer = PackedPlayerBuffer[pnum];
if (sgwPackPlrOffsetTbl[pnum] != SDL_SwapLE16(header.wOffset)) {
if (sgwPackPlrOffsetTbl[pnum] != Swap16LE(header.wOffset)) {
sgwPackPlrOffsetTbl[pnum] = 0;
if (header.wOffset != 0) {
return;
@ -858,9 +866,9 @@ void recv_plrinfo(Player &player, const TCmdPlrInfoHdr &header, bool recv)
SendPlayerInfo(pnum, CMD_ACK_PLRINFO);
}
memcpy(reinterpret_cast<uint8_t *>(&packedPlayer) + SDL_SwapLE16(header.wOffset), reinterpret_cast<const uint8_t *>(&header) + sizeof(header), SDL_SwapLE16(header.wBytes));
memcpy(reinterpret_cast<uint8_t *>(&packedPlayer) + Swap16LE(header.wOffset), reinterpret_cast<const uint8_t *>(&header) + sizeof(header), Swap16LE(header.wBytes));
sgwPackPlrOffsetTbl[pnum] += SDL_SwapLE16(header.wBytes);
sgwPackPlrOffsetTbl[pnum] += Swap16LE(header.wBytes);
if (sgwPackPlrOffsetTbl[pnum] != sizeof(packedPlayer)) {
return;
}

205
Source/pack.cpp

@ -15,6 +15,7 @@
#include "plrmsg.h"
#include "stores.h"
#include "utils/endian_read.hpp"
#include "utils/endian_swap.hpp"
#include "utils/is_of.hpp"
#include "utils/log.hpp"
#include "utils/utf8.hpp"
@ -117,20 +118,20 @@ void PackItem(ItemPack &packedItem, const Item &item, bool isHellfire)
if (gbIsSpawn) {
idx = RemapItemIdxToSpawn(idx);
}
packedItem.idx = SDL_SwapLE16(idx);
packedItem.idx = Swap16LE(idx);
if (item.IDidx == IDI_EAR) {
packedItem.iCreateInfo = SDL_SwapLE16(item._iIName[1] | (item._iIName[0] << 8));
packedItem.iSeed = SDL_SwapLE32(LoadBE32(&item._iIName[2]));
packedItem.iCreateInfo = Swap16LE(item._iIName[1] | (item._iIName[0] << 8));
packedItem.iSeed = Swap32LE(LoadBE32(&item._iIName[2]));
packedItem.bId = item._iIName[6];
packedItem.bDur = item._iIName[7];
packedItem.bMDur = item._iIName[8];
packedItem.bCh = item._iIName[9];
packedItem.bMCh = item._iIName[10];
packedItem.wValue = SDL_SwapLE16(item._ivalue | (item._iIName[11] << 8) | ((item._iCurs - ICURS_EAR_SORCERER) << 6));
packedItem.dwBuff = SDL_SwapLE32(LoadBE32(&item._iIName[12]));
packedItem.wValue = Swap16LE(item._ivalue | (item._iIName[11] << 8) | ((item._iCurs - ICURS_EAR_SORCERER) << 6));
packedItem.dwBuff = Swap32LE(LoadBE32(&item._iIName[12]));
} else {
packedItem.iSeed = SDL_SwapLE32(item._iSeed);
packedItem.iCreateInfo = SDL_SwapLE16(item._iCreateInfo);
packedItem.iSeed = Swap32LE(item._iSeed);
packedItem.iCreateInfo = Swap16LE(item._iCreateInfo);
packedItem.bId = (item._iMagical << 1) | (item._iIdentified ? 1 : 0);
if (item._iMaxDur > 255)
packedItem.bMDur = 254;
@ -141,7 +142,7 @@ void PackItem(ItemPack &packedItem, const Item &item, bool isHellfire)
packedItem.bCh = item._iCharges;
packedItem.bMCh = item._iMaxCharges;
if (item.IDidx == IDI_GOLD)
packedItem.wValue = SDL_SwapLE16(item._ivalue);
packedItem.wValue = Swap16LE(item._ivalue);
packedItem.dwBuff = item.dwBuff;
}
}
@ -168,13 +169,13 @@ void PackPlayer(PlayerPack &packed, const Player &player)
packed.pBaseVit = player._pBaseVit;
packed.pLevel = player.getCharacterLevel();
packed.pStatPts = player._pStatPts;
packed.pExperience = SDL_SwapLE32(player._pExperience);
packed.pGold = SDL_SwapLE32(player._pGold);
packed.pHPBase = SDL_SwapLE32(player._pHPBase);
packed.pMaxHPBase = SDL_SwapLE32(player._pMaxHPBase);
packed.pManaBase = SDL_SwapLE32(player._pManaBase);
packed.pMaxManaBase = SDL_SwapLE32(player._pMaxManaBase);
packed.pMemSpells = SDL_SwapLE64(player._pMemSpells);
packed.pExperience = Swap32LE(player._pExperience);
packed.pGold = Swap32LE(player._pGold);
packed.pHPBase = Swap32LE(player._pHPBase);
packed.pMaxHPBase = Swap32LE(player._pMaxHPBase);
packed.pManaBase = Swap32LE(player._pManaBase);
packed.pMaxManaBase = Swap32LE(player._pMaxManaBase);
packed.pMemSpells = Swap64LE(player._pMemSpells);
for (int i = 0; i < 37; i++) // Should be MAX_SPELLS but set to 37 to make save games compatible
packed.pSplLvl[i] = player._pSplLvl[i];
@ -194,9 +195,9 @@ void PackPlayer(PlayerPack &packed, const Player &player)
for (int i = 0; i < MaxBeltItems; i++)
PackItem(packed.SpdList[i], player.SpdList[i], gbIsHellfire);
packed.wReflections = SDL_SwapLE16(player.wReflections);
packed.pDamAcFlags = SDL_SwapLE32(static_cast<uint32_t>(player.pDamAcFlags));
packed.pDiabloKillLevel = SDL_SwapLE32(player.pDiabloKillLevel);
packed.wReflections = Swap16LE(player.wReflections);
packed.pDamAcFlags = Swap32LE(static_cast<uint32_t>(player.pDamAcFlags));
packed.pDiabloKillLevel = Swap32LE(player.pDiabloKillLevel);
packed.bIsHellfire = gbIsHellfire ? 1 : 0;
}
@ -206,9 +207,9 @@ void PackNetItem(const Item &item, ItemNetPack &packedItem)
packedItem.def.wIndx = static_cast<_item_indexes>(0xFFFF);
return;
}
packedItem.def.wIndx = static_cast<_item_indexes>(SDL_SwapLE16(item.IDidx));
packedItem.def.wCI = SDL_SwapLE16(item._iCreateInfo);
packedItem.def.dwSeed = SDL_SwapLE32(item._iSeed);
packedItem.def.wIndx = static_cast<_item_indexes>(Swap16LE(item.IDidx));
packedItem.def.wCI = Swap16LE(item._iCreateInfo);
packedItem.def.dwSeed = Swap32LE(item._iSeed);
if (item.IDidx != IDI_EAR)
PrepareItemForNetwork(item, packedItem.item);
else
@ -228,12 +229,12 @@ void PackNetPlayer(PlayerNetPack &packed, const Player &player)
packed.pBaseVit = player._pBaseVit;
packed.pLevel = player.getCharacterLevel();
packed.pStatPts = player._pStatPts;
packed.pExperience = SDL_SwapLE32(player._pExperience);
packed.pHPBase = SDL_SwapLE32(player._pHPBase);
packed.pMaxHPBase = SDL_SwapLE32(player._pMaxHPBase);
packed.pManaBase = SDL_SwapLE32(player._pManaBase);
packed.pMaxManaBase = SDL_SwapLE32(player._pMaxManaBase);
packed.pMemSpells = SDL_SwapLE64(player._pMemSpells);
packed.pExperience = Swap32LE(player._pExperience);
packed.pHPBase = Swap32LE(player._pHPBase);
packed.pMaxHPBase = Swap32LE(player._pMaxHPBase);
packed.pManaBase = Swap32LE(player._pManaBase);
packed.pMaxManaBase = Swap32LE(player._pMaxManaBase);
packed.pMemSpells = Swap64LE(player._pMemSpells);
for (int i = 0; i < MAX_SPELLS; i++)
packed.pSplLvl[i] = player._pSplLvl[i];
@ -251,36 +252,36 @@ void PackNetPlayer(PlayerNetPack &packed, const Player &player)
for (int i = 0; i < MaxBeltItems; i++)
PackNetItem(player.SpdList[i], packed.SpdList[i]);
packed.wReflections = SDL_SwapLE16(player.wReflections);
packed.wReflections = Swap16LE(player.wReflections);
packed.pDiabloKillLevel = player.pDiabloKillLevel;
packed.pManaShield = player.pManaShield;
packed.friendlyMode = player.friendlyMode ? 1 : 0;
packed.isOnSetLevel = player.plrIsOnSetLevel;
packed.pStrength = SDL_SwapLE32(player._pStrength);
packed.pMagic = SDL_SwapLE32(player._pMagic);
packed.pDexterity = SDL_SwapLE32(player._pDexterity);
packed.pVitality = SDL_SwapLE32(player._pVitality);
packed.pHitPoints = SDL_SwapLE32(player._pHitPoints);
packed.pMaxHP = SDL_SwapLE32(player._pMaxHP);
packed.pMana = SDL_SwapLE32(player._pMana);
packed.pMaxMana = SDL_SwapLE32(player._pMaxMana);
packed.pDamageMod = SDL_SwapLE32(player._pDamageMod);
packed.pStrength = Swap32LE(player._pStrength);
packed.pMagic = Swap32LE(player._pMagic);
packed.pDexterity = Swap32LE(player._pDexterity);
packed.pVitality = Swap32LE(player._pVitality);
packed.pHitPoints = Swap32LE(player._pHitPoints);
packed.pMaxHP = Swap32LE(player._pMaxHP);
packed.pMana = Swap32LE(player._pMana);
packed.pMaxMana = Swap32LE(player._pMaxMana);
packed.pDamageMod = Swap32LE(player._pDamageMod);
// we pack base to block as a basic check that remote players are using the same playerdat values as we are
packed.pBaseToBlk = SDL_SwapLE32(player.getBaseToBlock());
packed.pIMinDam = SDL_SwapLE32(player._pIMinDam);
packed.pIMaxDam = SDL_SwapLE32(player._pIMaxDam);
packed.pIAC = SDL_SwapLE32(player._pIAC);
packed.pIBonusDam = SDL_SwapLE32(player._pIBonusDam);
packed.pIBonusToHit = SDL_SwapLE32(player._pIBonusToHit);
packed.pIBonusAC = SDL_SwapLE32(player._pIBonusAC);
packed.pIBonusDamMod = SDL_SwapLE32(player._pIBonusDamMod);
packed.pIGetHit = SDL_SwapLE32(player._pIGetHit);
packed.pIEnAc = SDL_SwapLE32(player._pIEnAc);
packed.pIFMinDam = SDL_SwapLE32(player._pIFMinDam);
packed.pIFMaxDam = SDL_SwapLE32(player._pIFMaxDam);
packed.pILMinDam = SDL_SwapLE32(player._pILMinDam);
packed.pILMaxDam = SDL_SwapLE32(player._pILMaxDam);
packed.pBaseToBlk = Swap32LE(player.getBaseToBlock());
packed.pIMinDam = Swap32LE(player._pIMinDam);
packed.pIMaxDam = Swap32LE(player._pIMaxDam);
packed.pIAC = Swap32LE(player._pIAC);
packed.pIBonusDam = Swap32LE(player._pIBonusDam);
packed.pIBonusToHit = Swap32LE(player._pIBonusToHit);
packed.pIBonusAC = Swap32LE(player._pIBonusAC);
packed.pIBonusDamMod = Swap32LE(player._pIBonusDamMod);
packed.pIGetHit = Swap32LE(player._pIGetHit);
packed.pIEnAc = Swap32LE(player._pIEnAc);
packed.pIFMinDam = Swap32LE(player._pIFMinDam);
packed.pIFMaxDam = Swap32LE(player._pIFMaxDam);
packed.pILMinDam = Swap32LE(player._pILMinDam);
packed.pILMaxDam = Swap32LE(player._pILMaxDam);
}
void UnPackItem(const ItemPack &packedItem, const Player &player, Item &item, bool isHellfire)
@ -290,7 +291,7 @@ void UnPackItem(const ItemPack &packedItem, const Player &player, Item &item, bo
return;
}
auto idx = static_cast<_item_indexes>(SDL_SwapLE16(packedItem.idx));
auto idx = static_cast<_item_indexes>(Swap16LE(packedItem.idx));
if (gbIsSpawn) {
idx = RemapItemIdxFromSpawn(idx);
@ -305,10 +306,10 @@ void UnPackItem(const ItemPack &packedItem, const Player &player, Item &item, bo
}
if (idx == IDI_EAR) {
const uint16_t ic = SDL_SwapLE16(packedItem.iCreateInfo);
const uint32_t iseed = SDL_SwapLE32(packedItem.iSeed);
const uint16_t ivalue = SDL_SwapLE16(packedItem.wValue);
const int32_t ibuff = SDL_SwapLE32(packedItem.dwBuff);
const uint16_t ic = Swap16LE(packedItem.iCreateInfo);
const uint32_t iseed = Swap32LE(packedItem.iSeed);
const uint16_t ivalue = Swap16LE(packedItem.wValue);
const int32_t ibuff = Swap32LE(packedItem.dwBuff);
char heroName[17];
heroName[0] = static_cast<char>((ic >> 8) & 0x7F);
@ -334,8 +335,8 @@ void UnPackItem(const ItemPack &packedItem, const Player &player, Item &item, bo
item = {};
// Item generation logic will assign CF_HELLFIRE based on isHellfire
// so if we carry it over from packedItem, it may be incorrect
const uint32_t dwBuff = SDL_SwapLE32(packedItem.dwBuff) | (isHellfire ? CF_HELLFIRE : 0);
RecreateItem(player, item, idx, SDL_SwapLE16(packedItem.iCreateInfo), SDL_SwapLE32(packedItem.iSeed), SDL_SwapLE16(packedItem.wValue), dwBuff);
const uint32_t dwBuff = Swap32LE(packedItem.dwBuff) | (isHellfire ? CF_HELLFIRE : 0);
RecreateItem(player, item, idx, Swap16LE(packedItem.iCreateInfo), Swap32LE(packedItem.iSeed), Swap16LE(packedItem.wValue), dwBuff);
item._iIdentified = (packedItem.bId & 1) != 0;
item._iMaxDur = packedItem.bMDur;
item._iDurability = ClampDurability(item, packedItem.bDur);
@ -350,8 +351,8 @@ void UnPackPlayer(const PlayerPack &packed, Player &player)
player = {};
player.setCharacterLevel(packed.pLevel);
player._pMaxHPBase = SDL_SwapLE32(packed.pMaxHPBase);
player._pHPBase = SDL_SwapLE32(packed.pHPBase);
player._pMaxHPBase = Swap32LE(packed.pMaxHPBase);
player._pHPBase = Swap32LE(packed.pHPBase);
player._pHPBase = std::clamp<int32_t>(player._pHPBase, 0, player._pMaxHPBase);
player._pMaxHP = player._pMaxHPBase;
player._pHitPoints = player._pHPBase;
@ -378,15 +379,15 @@ void UnPackPlayer(const PlayerPack &packed, Player &player)
player._pVitality = player._pBaseVit;
player._pStatPts = packed.pStatPts;
player._pExperience = SDL_SwapLE32(packed.pExperience);
player._pGold = SDL_SwapLE32(packed.pGold);
player._pExperience = Swap32LE(packed.pExperience);
player._pGold = Swap32LE(packed.pGold);
if ((int)(player._pHPBase & 0xFFFFFFC0) < 64)
player._pHPBase = 64;
player._pMaxManaBase = SDL_SwapLE32(packed.pMaxManaBase);
player._pManaBase = SDL_SwapLE32(packed.pManaBase);
player._pMaxManaBase = Swap32LE(packed.pMaxManaBase);
player._pManaBase = Swap32LE(packed.pManaBase);
player._pManaBase = std::min<int32_t>(player._pManaBase, player._pMaxManaBase);
player._pMemSpells = SDL_SwapLE64(packed.pMemSpells);
player._pMemSpells = Swap64LE(packed.pMemSpells);
// Only read spell levels for learnable spells (Diablo)
for (int i = 0; i < 37; i++) { // Should be MAX_SPELLS but set to 36 to make save games compatible
@ -428,23 +429,23 @@ void UnPackPlayer(const PlayerPack &packed, Player &player)
UnPackItem(packed.SpdList[i], player, player.SpdList[i], isHellfire);
CalcPlrInv(player, false);
player.wReflections = SDL_SwapLE16(packed.wReflections);
player.pDiabloKillLevel = SDL_SwapLE32(packed.pDiabloKillLevel);
player.wReflections = Swap16LE(packed.wReflections);
player.pDiabloKillLevel = Swap32LE(packed.pDiabloKillLevel);
}
bool UnPackNetItem(const Player &player, const ItemNetPack &packedItem, Item &item)
{
item = {};
const _item_indexes idx = static_cast<_item_indexes>(SDL_SwapLE16(packedItem.def.wIndx));
const _item_indexes idx = static_cast<_item_indexes>(Swap16LE(packedItem.def.wIndx));
if (idx < 0 || idx >= static_cast<_item_indexes>(AllItemsList.size()))
return true;
if (idx == IDI_EAR) {
RecreateEar(item, SDL_SwapLE16(packedItem.ear.wCI), SDL_SwapLE32(packedItem.ear.dwSeed), packedItem.ear.bCursval, packedItem.ear.heroname);
RecreateEar(item, Swap16LE(packedItem.ear.wCI), Swap32LE(packedItem.ear.dwSeed), packedItem.ear.bCursval, packedItem.ear.heroname);
return true;
}
const uint16_t creationFlags = SDL_SwapLE16(packedItem.item.wCI);
const uint32_t dwBuff = SDL_SwapLE16(packedItem.item.dwBuff);
const uint16_t creationFlags = Swap16LE(packedItem.item.wCI);
const uint32_t dwBuff = Swap16LE(packedItem.item.dwBuff);
if (idx != IDI_GOLD)
ValidateField(creationFlags, IsCreationFlagComboValid(creationFlags));
if ((creationFlags & CF_TOWN) != 0)
@ -472,13 +473,13 @@ bool UnPackNetPlayer(const PlayerNetPack &packed, Player &player)
ValidateField(packed.plrlevel, packed.plrlevel < NUMLEVELS);
ValidateField(packed.pLevel, packed.pLevel >= 1 && packed.pLevel <= player.getMaxCharacterLevel());
const int32_t baseHpMax = SDL_SwapLE32(packed.pMaxHPBase);
const int32_t baseHp = SDL_SwapLE32(packed.pHPBase);
const int32_t hpMax = SDL_SwapLE32(packed.pMaxHP);
const int32_t baseHpMax = Swap32LE(packed.pMaxHPBase);
const int32_t baseHp = Swap32LE(packed.pHPBase);
const int32_t hpMax = Swap32LE(packed.pMaxHP);
ValidateFields(baseHp, baseHpMax, baseHp >= (baseHpMax - hpMax) && baseHp <= baseHpMax);
const int32_t baseManaMax = SDL_SwapLE32(packed.pMaxManaBase);
const int32_t baseMana = SDL_SwapLE32(packed.pManaBase);
const int32_t baseManaMax = Swap32LE(packed.pMaxManaBase);
const int32_t baseMana = Swap32LE(packed.pManaBase);
ValidateFields(baseMana, baseManaMax, baseMana <= baseManaMax);
ValidateFields(packed.pClass, packed.pBaseStr, packed.pBaseStr <= player.GetMaximumAttributeValue(CharacterAttribute::Strength));
@ -513,11 +514,11 @@ bool UnPackNetPlayer(const PlayerNetPack &packed, Player &player)
player._pVitality = player._pBaseVit;
player._pStatPts = packed.pStatPts;
player._pExperience = SDL_SwapLE32(packed.pExperience);
player._pExperience = Swap32LE(packed.pExperience);
player._pMaxManaBase = baseManaMax;
player._pManaBase = baseMana;
player._pMemSpells = SDL_SwapLE64(packed.pMemSpells);
player.wReflections = SDL_SwapLE16(packed.wReflections);
player._pMemSpells = Swap64LE(packed.pMemSpells);
player.wReflections = Swap16LE(packed.wReflections);
player.pDiabloKillLevel = packed.pDiabloKillLevel;
player.pManaShield = packed.pManaShield != 0;
player.friendlyMode = packed.friendlyMode != 0;
@ -578,29 +579,29 @@ bool UnPackNetPlayer(const PlayerNetPack &packed, Player &player)
CalcPlrInv(player, false);
player._pGold = CalculateGold(player);
ValidateFields(player._pStrength, SDL_SwapLE32(packed.pStrength), player._pStrength == SDL_SwapLE32(packed.pStrength));
ValidateFields(player._pMagic, SDL_SwapLE32(packed.pMagic), player._pMagic == SDL_SwapLE32(packed.pMagic));
ValidateFields(player._pDexterity, SDL_SwapLE32(packed.pDexterity), player._pDexterity == SDL_SwapLE32(packed.pDexterity));
ValidateFields(player._pVitality, SDL_SwapLE32(packed.pVitality), player._pVitality == SDL_SwapLE32(packed.pVitality));
ValidateFields(player._pHitPoints, SDL_SwapLE32(packed.pHitPoints), player._pHitPoints == SDL_SwapLE32(packed.pHitPoints));
ValidateFields(player._pMaxHP, SDL_SwapLE32(packed.pMaxHP), player._pMaxHP == SDL_SwapLE32(packed.pMaxHP));
ValidateFields(player._pMana, SDL_SwapLE32(packed.pMana), player._pMana == SDL_SwapLE32(packed.pMana));
ValidateFields(player._pMaxMana, SDL_SwapLE32(packed.pMaxMana), player._pMaxMana == SDL_SwapLE32(packed.pMaxMana));
ValidateFields(player._pDamageMod, SDL_SwapLE32(packed.pDamageMod), player._pDamageMod == SDL_SwapLE32(packed.pDamageMod));
ValidateFields(player.getBaseToBlock(), SDL_SwapLE32(packed.pBaseToBlk), player.getBaseToBlock() == SDL_SwapLE32(packed.pBaseToBlk));
ValidateFields(player._pIMinDam, SDL_SwapLE32(packed.pIMinDam), player._pIMinDam == SDL_SwapLE32(packed.pIMinDam));
ValidateFields(player._pIMaxDam, SDL_SwapLE32(packed.pIMaxDam), player._pIMaxDam == SDL_SwapLE32(packed.pIMaxDam));
ValidateFields(player._pIAC, SDL_SwapLE32(packed.pIAC), player._pIAC == SDL_SwapLE32(packed.pIAC));
ValidateFields(player._pIBonusDam, SDL_SwapLE32(packed.pIBonusDam), player._pIBonusDam == SDL_SwapLE32(packed.pIBonusDam));
ValidateFields(player._pIBonusToHit, SDL_SwapLE32(packed.pIBonusToHit), player._pIBonusToHit == SDL_SwapLE32(packed.pIBonusToHit));
ValidateFields(player._pIBonusAC, SDL_SwapLE32(packed.pIBonusAC), player._pIBonusAC == SDL_SwapLE32(packed.pIBonusAC));
ValidateFields(player._pIBonusDamMod, SDL_SwapLE32(packed.pIBonusDamMod), player._pIBonusDamMod == SDL_SwapLE32(packed.pIBonusDamMod));
ValidateFields(player._pIGetHit, SDL_SwapLE32(packed.pIGetHit), player._pIGetHit == SDL_SwapLE32(packed.pIGetHit));
ValidateFields(player._pIEnAc, SDL_SwapLE32(packed.pIEnAc), player._pIEnAc == SDL_SwapLE32(packed.pIEnAc));
ValidateFields(player._pIFMinDam, SDL_SwapLE32(packed.pIFMinDam), player._pIFMinDam == SDL_SwapLE32(packed.pIFMinDam));
ValidateFields(player._pIFMaxDam, SDL_SwapLE32(packed.pIFMaxDam), player._pIFMaxDam == SDL_SwapLE32(packed.pIFMaxDam));
ValidateFields(player._pILMinDam, SDL_SwapLE32(packed.pILMinDam), player._pILMinDam == SDL_SwapLE32(packed.pILMinDam));
ValidateFields(player._pILMaxDam, SDL_SwapLE32(packed.pILMaxDam), player._pILMaxDam == SDL_SwapLE32(packed.pILMaxDam));
ValidateFields(player._pStrength, Swap32LE(packed.pStrength), player._pStrength == Swap32LE(packed.pStrength));
ValidateFields(player._pMagic, Swap32LE(packed.pMagic), player._pMagic == Swap32LE(packed.pMagic));
ValidateFields(player._pDexterity, Swap32LE(packed.pDexterity), player._pDexterity == Swap32LE(packed.pDexterity));
ValidateFields(player._pVitality, Swap32LE(packed.pVitality), player._pVitality == Swap32LE(packed.pVitality));
ValidateFields(player._pHitPoints, Swap32LE(packed.pHitPoints), player._pHitPoints == Swap32LE(packed.pHitPoints));
ValidateFields(player._pMaxHP, Swap32LE(packed.pMaxHP), player._pMaxHP == Swap32LE(packed.pMaxHP));
ValidateFields(player._pMana, Swap32LE(packed.pMana), player._pMana == Swap32LE(packed.pMana));
ValidateFields(player._pMaxMana, Swap32LE(packed.pMaxMana), player._pMaxMana == Swap32LE(packed.pMaxMana));
ValidateFields(player._pDamageMod, Swap32LE(packed.pDamageMod), player._pDamageMod == Swap32LE(packed.pDamageMod));
ValidateFields(player.getBaseToBlock(), Swap32LE(packed.pBaseToBlk), player.getBaseToBlock() == Swap32LE(packed.pBaseToBlk));
ValidateFields(player._pIMinDam, Swap32LE(packed.pIMinDam), player._pIMinDam == Swap32LE(packed.pIMinDam));
ValidateFields(player._pIMaxDam, Swap32LE(packed.pIMaxDam), player._pIMaxDam == Swap32LE(packed.pIMaxDam));
ValidateFields(player._pIAC, Swap32LE(packed.pIAC), player._pIAC == Swap32LE(packed.pIAC));
ValidateFields(player._pIBonusDam, Swap32LE(packed.pIBonusDam), player._pIBonusDam == Swap32LE(packed.pIBonusDam));
ValidateFields(player._pIBonusToHit, Swap32LE(packed.pIBonusToHit), player._pIBonusToHit == Swap32LE(packed.pIBonusToHit));
ValidateFields(player._pIBonusAC, Swap32LE(packed.pIBonusAC), player._pIBonusAC == Swap32LE(packed.pIBonusAC));
ValidateFields(player._pIBonusDamMod, Swap32LE(packed.pIBonusDamMod), player._pIBonusDamMod == Swap32LE(packed.pIBonusDamMod));
ValidateFields(player._pIGetHit, Swap32LE(packed.pIGetHit), player._pIGetHit == Swap32LE(packed.pIGetHit));
ValidateFields(player._pIEnAc, Swap32LE(packed.pIEnAc), player._pIEnAc == Swap32LE(packed.pIEnAc));
ValidateFields(player._pIFMinDam, Swap32LE(packed.pIFMinDam), player._pIFMinDam == Swap32LE(packed.pIFMinDam));
ValidateFields(player._pIFMaxDam, Swap32LE(packed.pIFMaxDam), player._pIFMaxDam == Swap32LE(packed.pIFMaxDam));
ValidateFields(player._pILMinDam, Swap32LE(packed.pILMinDam), player._pILMinDam == Swap32LE(packed.pILMinDam));
ValidateFields(player._pILMaxDam, Swap32LE(packed.pILMaxDam), player._pILMaxDam == Swap32LE(packed.pILMaxDam));
ValidateFields(player._pMaxHPBase, player.calculateBaseLife(), player._pMaxHPBase <= player.calculateBaseLife());
ValidateFields(player._pMaxManaBase, player.calculateBaseMana(), player._pMaxManaBase <= player.calculateBaseMana());

5
Source/pfile.cpp

@ -23,6 +23,7 @@
#include "playerdat.hpp"
#include "qol/stash.h"
#include "utils/endian_read.hpp"
#include "utils/endian_swap.hpp"
#include "utils/file_util.h"
#include "utils/language.h"
#include "utils/parse_int.hpp"
@ -340,9 +341,9 @@ void CreateDetailDiffs(std::string_view prefix, std::string_view memoryMapFile,
app_fatal("read32BitInt failed. Too less bytes to read.");
memcpy(&value, compareInfo.data.get() + compareInfo.currentPosition, sizeof(value));
if (useLE)
value = SDL_SwapLE32(value);
value = Swap32LE(value);
else
value = SDL_SwapBE32(value);
value = Swap32BE(value);
return value;
};

29
Source/sync.cpp

@ -12,6 +12,7 @@
#include "monster.h"
#include "monsters/validation.hpp"
#include "player.h"
#include "utils/endian_swap.hpp"
#include "utils/is_of.hpp"
namespace devilution {
@ -47,7 +48,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 = SDL_SwapLE32(monster.hitPoints);
monsterSync._mhitpoints = Swap32LE(monster.hitPoints);
sgnMonsterPriority[ndx] = 0xFFFF;
sgwLRU[ndx] = monster.activeForTicks == 0 ? 0xFFFF : 0xFFFE;
@ -111,27 +112,27 @@ void SyncPlrInv(TSyncHeader *pHdr)
auto &item = Items[pHdr->bItemI];
pHdr->bItemX = item.position.x;
pHdr->bItemY = item.position.y;
pHdr->wItemIndx = SDL_SwapLE16(item.IDidx);
pHdr->wItemIndx = Swap16LE(item.IDidx);
if (item.IDidx == IDI_EAR) {
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->wItemCI = Swap16LE((item._iIName[0] << 8) | item._iIName[1]);
pHdr->dwItemSeed = Swap32LE((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 = 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]);
pHdr->wItemVal = Swap16LE((item._iIName[11] << 8) | ((item._iCurs - ICURS_EAR_SORCERER) << 6) | item._ivalue);
pHdr->dwItemBuff = Swap32LE((item._iIName[12] << 24) | (item._iIName[13] << 16) | (item._iIName[14] << 8) | item._iIName[15]);
} else {
pHdr->wItemCI = SDL_SwapLE16(item._iCreateInfo);
pHdr->dwItemSeed = SDL_SwapLE32(item._iSeed);
pHdr->wItemCI = Swap16LE(item._iCreateInfo);
pHdr->dwItemSeed = Swap32LE(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 = SDL_SwapLE16(item._ivalue);
pHdr->wItemVal = Swap16LE(item._ivalue);
}
}
}
@ -141,9 +142,9 @@ void SyncPlrInv(TSyncHeader *pHdr)
const auto &item = MyPlayer->InvBody[sgnSyncPInv];
if (!item.isEmpty()) {
pHdr->bPInvLoc = sgnSyncPInv;
pHdr->wPInvIndx = SDL_SwapLE16(item.IDidx);
pHdr->wPInvCI = SDL_SwapLE16(item._iCreateInfo);
pHdr->dwPInvSeed = SDL_SwapLE32(item._iSeed);
pHdr->wPInvIndx = Swap16LE(item.IDidx);
pHdr->wPInvCI = Swap16LE(item._iCreateInfo);
pHdr->dwPInvSeed = Swap32LE(item._iSeed);
pHdr->bPInvId = item._iIdentified ? 1 : 0;
}
@ -268,14 +269,14 @@ size_t sync_all_monsters(std::byte *pbBuf, size_t dwMaxLen)
pHdr->wLen += sizeof(TSyncMonster);
dwMaxLen -= sizeof(TSyncMonster);
}
pHdr->wLen = SDL_SwapLE16(pHdr->wLen);
pHdr->wLen = Swap16LE(pHdr->wLen);
return dwMaxLen;
}
size_t OnSyncData(const TSyncHeader &header, size_t maxCmdSize, const Player &player)
{
const uint16_t wLen = SDL_SwapLE16(header.wLen);
const uint16_t wLen = Swap16LE(header.wLen);
if (!ValidateCmdSize(wLen + sizeof(header), maxCmdSize, player.getId()))
return maxCmdSize;

40
Source/utils/endian_swap.hpp

@ -10,7 +10,7 @@
namespace devilution {
constexpr uint16_t Swap16LE(uint16_t val)
inline uint16_t Swap16LE(uint16_t val)
{
#ifdef USE_SDL3
return SDL_Swap16LE(val);
@ -19,7 +19,16 @@ constexpr uint16_t Swap16LE(uint16_t val)
#endif
}
constexpr uint32_t Swap32LE(uint32_t val)
inline uint16_t Swap16BE(uint16_t val)
{
#ifdef USE_SDL3
return SDL_Swap16BE(val);
#else
return SDL_SwapBE16(val);
#endif
}
inline uint32_t Swap32LE(uint32_t val)
{
#ifdef USE_SDL3
return SDL_Swap32LE(val);
@ -28,4 +37,31 @@ constexpr uint32_t Swap32LE(uint32_t val)
#endif
}
inline uint32_t Swap32BE(uint32_t val)
{
#ifdef USE_SDL3
return SDL_Swap32BE(val);
#else
return SDL_SwapBE32(val);
#endif
}
inline uint64_t Swap64LE(uint64_t val)
{
#ifdef USE_SDL3
return SDL_Swap64LE(val);
#else
return SDL_SwapLE64(val);
#endif
}
inline uint64_t Swap64BE(uint64_t val)
{
#ifdef USE_SDL3
return SDL_Swap64BE(val);
#else
return SDL_SwapBE64(val);
#endif
}
} // namespace devilution

17
Source/utils/language.cpp

@ -11,6 +11,7 @@
#include "engine/assets.hpp"
#include "options.h"
#include "utils/algorithm/container.hpp"
#include "utils/endian_swap.hpp"
#include "utils/file_util.h"
#include "utils/log.hpp"
#include "utils/paths.h"
@ -72,12 +73,12 @@ struct MoHead {
void SwapLE(MoHead &head)
{
head.magic = SDL_SwapLE32(head.magic);
head.revision.major = SDL_SwapLE16(head.revision.major);
head.revision.minor = SDL_SwapLE16(head.revision.minor);
head.nbMappings = SDL_SwapLE32(head.nbMappings);
head.srcOffset = SDL_SwapLE32(head.srcOffset);
head.dstOffset = SDL_SwapLE32(head.dstOffset);
head.magic = Swap32LE(head.magic);
head.revision.major = Swap16LE(head.revision.major);
head.revision.minor = Swap16LE(head.revision.minor);
head.nbMappings = Swap32LE(head.nbMappings);
head.srcOffset = Swap32LE(head.srcOffset);
head.dstOffset = Swap32LE(head.dstOffset);
}
struct MoEntry {
@ -87,8 +88,8 @@ struct MoEntry {
void SwapLE(MoEntry &entry)
{
entry.length = SDL_SwapLE32(entry.length);
entry.offset = SDL_SwapLE32(entry.offset);
entry.length = Swap32LE(entry.length);
entry.offset = Swap32LE(entry.offset);
}
std::string_view TrimLeft(std::string_view str)

68
Source/utils/surface_to_pcx.cpp

@ -6,18 +6,36 @@
#include <cstring>
#include <string>
#ifdef USE_SDL3
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_iostream.h>
#else
#include <SDL.h>
#endif
#include <expected.hpp>
#include "engine/surface.hpp"
#include "utils/endian_swap.hpp"
#include "utils/pcx.hpp"
namespace devilution {
namespace {
tl::expected<void, std::string> CheckedFWrite(const void *ptr, size_t size, SDL_RWops *out)
tl::expected<void, std::string> CheckedFWrite(const void *ptr, size_t size,
#ifdef USE_SDL3
SDL_IOStream *
#else
SDL_RWops *
#endif
out)
{
if (SDL_RWwrite(out, ptr, size, 1) != 1) {
#ifdef USE_SDL3
if (SDL_WriteIO(out, ptr, size) != size)
#else
if (SDL_RWwrite(out, ptr, size, 1) != 1)
#endif
{
const char *errorMessage = SDL_GetError();
if (errorMessage == nullptr)
errorMessage = "";
@ -35,7 +53,13 @@ tl::expected<void, std::string> CheckedFWrite(const void *ptr, size_t size, SDL_
* @param out File stream to write to
* @return True on success
*/
tl::expected<void, std::string> WritePcxHeader(int16_t width, int16_t height, SDL_RWops *out)
tl::expected<void, std::string> WritePcxHeader(int16_t width, int16_t height,
#ifdef USE_SDL3
SDL_IOStream *
#else
SDL_RWops *
#endif
out)
{
PCXHeader buffer;
@ -44,12 +68,12 @@ tl::expected<void, std::string> WritePcxHeader(int16_t width, int16_t height, SD
buffer.Version = 5;
buffer.Encoding = 1;
buffer.BitsPerPixel = 8;
buffer.Xmax = SDL_SwapLE16(width - 1);
buffer.Ymax = SDL_SwapLE16(height - 1);
buffer.HDpi = SDL_SwapLE16(width);
buffer.VDpi = SDL_SwapLE16(height);
buffer.Xmax = Swap16LE(width - 1);
buffer.Ymax = Swap16LE(height - 1);
buffer.HDpi = Swap16LE(width);
buffer.VDpi = Swap16LE(height);
buffer.NPlanes = 1;
buffer.BytesPerLine = SDL_SwapLE16(width);
buffer.BytesPerLine = Swap16LE(width);
return CheckedFWrite(&buffer, sizeof(buffer), out);
}
@ -60,7 +84,13 @@ tl::expected<void, std::string> WritePcxHeader(int16_t width, int16_t height, SD
* @param out File stream for the PCX file.
* @return True if successful, else false
*/
tl::expected<void, std::string> WritePcxPalette(SDL_Color *palette, SDL_RWops *out)
tl::expected<void, std::string> WritePcxPalette(SDL_Color *palette,
#ifdef USE_SDL3
SDL_IOStream *
#else
SDL_RWops *
#endif
out)
{
uint8_t pcxPalette[1 + 256 * 3];
@ -123,7 +153,13 @@ uint8_t *WritePcxLine(uint8_t *src, uint8_t *dst, int width)
* @param out File stream for the PCX file.
* @return True if successful, else false
*/
tl::expected<void, std::string> WritePcxPixels(const Surface &buf, SDL_RWops *out)
tl::expected<void, std::string> WritePcxPixels(const Surface &buf,
#ifdef USE_SDL3
SDL_IOStream *
#else
SDL_RWops *
#endif
out)
{
const int width = buf.w();
const std::unique_ptr<uint8_t[]> pBuffer { new uint8_t[static_cast<size_t>(2 * width)] };
@ -140,7 +176,13 @@ tl::expected<void, std::string> WritePcxPixels(const Surface &buf, SDL_RWops *ou
} // namespace
tl::expected<void, std::string>
WriteSurfaceToFilePcx(const Surface &buf, SDL_RWops *outStream)
WriteSurfaceToFilePcx(const Surface &buf,
#ifdef USE_SDL3
SDL_IOStream *
#else
SDL_RWops *
#endif
outStream)
{
tl::expected<void, std::string> result = WritePcxHeader(buf.w(), buf.h(), outStream);
if (!result.has_value()) return result;
@ -148,7 +190,11 @@ WriteSurfaceToFilePcx(const Surface &buf, SDL_RWops *outStream)
if (!result.has_value()) return result;
result = WritePcxPalette(buf.surface->format->palette->colors, outStream);
if (!result.has_value()) return result;
#ifdef USE_SDL3
SDL_CloseIO(outStream);
#else
SDL_RWclose(outStream);
#endif
return {};
}

15
Source/utils/surface_to_pcx.hpp

@ -1,7 +1,12 @@
#include <cstdio>
#include <string>
#ifdef USE_SDL3
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_iostream.h>
#else
#include <SDL.h>
#endif
#include <expected.hpp>
#include "engine/surface.hpp"
@ -9,6 +14,12 @@
namespace devilution {
tl::expected<void, std::string>
WriteSurfaceToFilePcx(const Surface &buf, SDL_RWops *outStream);
WriteSurfaceToFilePcx(const Surface &buf,
#ifdef USE_SDL3
SDL_IOStream *
#else
SDL_RWops *
#endif
outStream);
} // namespace devilution

5
test/drlg_test.hpp

@ -13,6 +13,7 @@
#include "multi.h"
#include "player.h"
#include "quests.h"
#include "utils/endian_swap.hpp"
#include "utils/paths.h"
using namespace devilution;
@ -88,7 +89,7 @@ void TestCreateDungeon(int level, uint32_t seed, lvl_entry entry)
for (int y = 0; y < DMAXY; y++) {
for (int x = 0; x < DMAXX; x++) {
auto tileId = static_cast<uint8_t>(SDL_SwapLE16(*tileLayer));
auto tileId = static_cast<uint8_t>(Swap16LE(*tileLayer));
tileLayer++;
ASSERT_EQ(dungeon[x][y], tileId) << "Tiles don't match at " << x << "x" << y;
}
@ -98,7 +99,7 @@ void TestCreateDungeon(int level, uint32_t seed, lvl_entry entry)
for (int y = 16; y < 16 + DMAXY * 2; y++) {
for (int x = 16; x < 16 + DMAXX * 2; x++) {
auto sectorId = static_cast<uint8_t>(SDL_SwapLE16(*transparentLayer));
auto sectorId = static_cast<uint8_t>(Swap16LE(*transparentLayer));
transparentLayer++;
ASSERT_EQ(dTransVal[x][y], sectorId) << "Room/region indexes don't match at " << x << "x" << y;
}

41
test/pack_test.cpp

@ -8,6 +8,7 @@
#include "monstdat.h"
#include "pack.h"
#include "playerdat.hpp"
#include "utils/endian_swap.hpp"
#include "utils/is_of.hpp"
#include "utils/paths.h"
@ -16,34 +17,34 @@ namespace {
void SwapLE(ItemPack &pack)
{
pack.iSeed = SDL_SwapLE32(pack.iSeed);
pack.iCreateInfo = SDL_SwapLE16(pack.iCreateInfo);
pack.idx = SDL_SwapLE16(pack.idx);
pack.wValue = SDL_SwapLE16(pack.wValue);
pack.dwBuff = SDL_SwapLE32(pack.dwBuff);
pack.iSeed = Swap32LE(pack.iSeed);
pack.iCreateInfo = Swap16LE(pack.iCreateInfo);
pack.idx = Swap16LE(pack.idx);
pack.wValue = Swap16LE(pack.wValue);
pack.dwBuff = Swap32LE(pack.dwBuff);
}
void SwapLE(PlayerPack &pack)
{
pack.dwLowDateTime = SDL_SwapLE32(pack.dwLowDateTime);
pack.dwHighDateTime = SDL_SwapLE32(pack.dwHighDateTime);
pack.pExperience = SDL_SwapLE32(pack.pExperience);
pack.pGold = SDL_SwapLE32(pack.pGold);
pack.pHPBase = SDL_SwapLE32(pack.pHPBase);
pack.pMaxHPBase = SDL_SwapLE32(pack.pMaxHPBase);
pack.pManaBase = SDL_SwapLE32(pack.pManaBase);
pack.pMaxManaBase = SDL_SwapLE32(pack.pMaxManaBase);
pack.pMemSpells = SDL_SwapLE64(pack.pMemSpells);
pack.dwLowDateTime = Swap32LE(pack.dwLowDateTime);
pack.dwHighDateTime = Swap32LE(pack.dwHighDateTime);
pack.pExperience = Swap32LE(pack.pExperience);
pack.pGold = Swap32LE(pack.pGold);
pack.pHPBase = Swap32LE(pack.pHPBase);
pack.pMaxHPBase = Swap32LE(pack.pMaxHPBase);
pack.pManaBase = Swap32LE(pack.pManaBase);
pack.pMaxManaBase = Swap32LE(pack.pMaxManaBase);
pack.pMemSpells = Swap64LE(pack.pMemSpells);
for (ItemPack &item : pack.InvBody)
SwapLE(item);
for (ItemPack &item : pack.InvList)
SwapLE(item);
for (ItemPack &item : pack.SpdList)
SwapLE(item);
pack.wReflections = SDL_SwapLE16(pack.wReflections);
pack.pDiabloKillLevel = SDL_SwapLE32(pack.pDiabloKillLevel);
pack.pDifficulty = SDL_SwapLE32(pack.pDifficulty);
pack.pDamAcFlags = SDL_SwapLE32(pack.pDamAcFlags);
pack.wReflections = Swap16LE(pack.wReflections);
pack.pDiabloKillLevel = Swap32LE(pack.pDiabloKillLevel);
pack.pDifficulty = Swap32LE(pack.pDifficulty);
pack.pDamAcFlags = Swap32LE(pack.pDamAcFlags);
}
ItemPack SwappedLE(const ItemPack &pack)
@ -834,7 +835,7 @@ TEST_F(PackTest, PackItem_empty)
// Copy the value out before comparing to avoid loading a misaligned address.
const auto idx = is.idx;
ASSERT_EQ(SDL_SwapLE16(idx), 0xFFFF);
ASSERT_EQ(Swap16LE(idx), 0xFFFF);
TestItemNameGeneration(id);
}
@ -845,7 +846,7 @@ static void compareGold(const ItemPack &is, int iCurs)
ASSERT_EQ(id._iCurs, iCurs);
ASSERT_EQ(id.IDidx, IDI_GOLD);
// Copy the value out before comparing to avoid loading a misaligned address.
const auto wvalue = SDL_SwapLE16(is.wValue);
const auto wvalue = Swap16LE(is.wValue);
ASSERT_EQ(id._ivalue, wvalue);
ASSERT_EQ(id._itype, ItemType::Gold);
ASSERT_EQ(id._iClass, ICLASS_GOLD);

37
test/writehero_test.cpp

@ -16,6 +16,7 @@
#include "pack.h"
#include "pfile.h"
#include "playerdat.hpp"
#include "utils/endian_swap.hpp"
#include "utils/file_util.h"
#include "utils/paths.h"
@ -30,24 +31,24 @@ constexpr int SpellDatVanilla[] = {
void SwapLE(ItemPack &pack)
{
pack.iSeed = SDL_SwapLE32(pack.iSeed);
pack.iCreateInfo = SDL_SwapLE16(pack.iCreateInfo);
pack.idx = SDL_SwapLE16(pack.idx);
pack.wValue = SDL_SwapLE16(pack.wValue);
pack.dwBuff = SDL_SwapLE32(pack.dwBuff);
pack.iSeed = Swap32LE(pack.iSeed);
pack.iCreateInfo = Swap16LE(pack.iCreateInfo);
pack.idx = Swap16LE(pack.idx);
pack.wValue = Swap16LE(pack.wValue);
pack.dwBuff = Swap32LE(pack.dwBuff);
}
void SwapLE(PlayerPack &player)
{
player.dwLowDateTime = SDL_SwapLE32(player.dwLowDateTime);
player.dwHighDateTime = SDL_SwapLE32(player.dwHighDateTime);
player.pExperience = SDL_SwapLE32(player.pExperience);
player.pGold = SDL_SwapLE32(player.pGold);
player.pHPBase = SDL_SwapLE32(player.pHPBase);
player.pMaxHPBase = SDL_SwapLE32(player.pMaxHPBase);
player.pManaBase = SDL_SwapLE32(player.pManaBase);
player.pMaxManaBase = SDL_SwapLE32(player.pMaxManaBase);
player.pMemSpells = SDL_SwapLE64(player.pMemSpells);
player.dwLowDateTime = Swap32LE(player.dwLowDateTime);
player.dwHighDateTime = Swap32LE(player.dwHighDateTime);
player.pExperience = Swap32LE(player.pExperience);
player.pGold = Swap32LE(player.pGold);
player.pHPBase = Swap32LE(player.pHPBase);
player.pMaxHPBase = Swap32LE(player.pMaxHPBase);
player.pManaBase = Swap32LE(player.pManaBase);
player.pMaxManaBase = Swap32LE(player.pMaxManaBase);
player.pMemSpells = Swap64LE(player.pMemSpells);
for (ItemPack &item : player.InvBody) {
SwapLE(item);
}
@ -57,10 +58,10 @@ void SwapLE(PlayerPack &player)
for (ItemPack &item : player.SpdList) {
SwapLE(item);
}
player.wReflections = SDL_SwapLE16(player.wReflections);
player.pDiabloKillLevel = SDL_SwapLE32(player.pDiabloKillLevel);
player.pDifficulty = SDL_SwapLE32(player.pDifficulty);
player.pDamAcFlags = SDL_SwapLE32(player.pDamAcFlags);
player.wReflections = Swap16LE(player.wReflections);
player.pDiabloKillLevel = Swap32LE(player.pDiabloKillLevel);
player.pDifficulty = Swap32LE(player.pDifficulty);
player.pDamAcFlags = Swap32LE(player.pDamAcFlags);
}
void PackItemUnique(ItemPack *id, int idx)

Loading…
Cancel
Save