diff --git a/Source/engine.h b/Source/engine.h index 842af3269..a84438002 100644 --- a/Source/engine.h +++ b/Source/engine.h @@ -26,6 +26,40 @@ DEVILUTION_BEGIN_NAMESPACE +template +T SwapLE(T in) +{ + static_assert((sizeof(T) == 1) || (sizeof(T) == 2) || (sizeof(T) == 4) || (sizeof(T) == 8), + "SwapLE called for unsupported size"); + switch (sizeof(T)) { + case 1: + return in; + case 2: + return static_cast(SDL_SwapLE16(static_cast(in))); + case 4: + return static_cast(SDL_SwapLE32(static_cast(in))); + case 8: + return static_cast(SDL_SwapLE64(static_cast(in))); + } +} + +template +T SwapBE(T in) +{ + static_assert((sizeof(T) == 1) || (sizeof(T) == 2) || (sizeof(T) == 4) || (sizeof(T) == 8), + "SwapBE called for unsupported size"); + switch (sizeof(T)) { + case 1: + return in; + case 2: + return static_cast(SDL_SwapBE16(static_cast(in))); + case 4: + return static_cast(SDL_SwapBE32(static_cast(in))); + case 8: + return static_cast(SDL_SwapBE64(static_cast(in))); + } +} + inline BYTE *CelGetFrameStart(BYTE *pCelBuff, int nCel) { DWORD *pFrameTable; @@ -105,27 +139,43 @@ struct CelOutputBuffer { * * Only use this if the buffer owns its data. */ - void Free() { + void Free() + { SDL_FreeSurface(this->surface); this->surface = NULL; } - int w() const { return region.w; } - int h() const { return region.h; } + int w() const + { + return region.w; + } + int h() const + { + return region.h; + } BYTE *at(int x, int y) const { return static_cast(surface->pixels) + region.x + x + surface->pitch * (region.y + y); } - BYTE *begin() const { return at(0, 0); } - BYTE *end() const { return at(0, region.h); } + BYTE *begin() const + { + return at(0, 0); + } + BYTE *end() const + { + return at(0, region.h); + } /** * @brief Line width of the raw underlying byte buffer. * May be wider than its logical width (for power-of-2 alignment). */ - int pitch() { return surface->pitch; } + int pitch() + { + return surface->pitch; + } bool in_bounds(Sint16 x, Sint16 y) const { diff --git a/Source/loadsave.cpp b/Source/loadsave.cpp index 474dba378..b21825676 100644 --- a/Source/loadsave.cpp +++ b/Source/loadsave.cpp @@ -14,36 +14,6 @@ int giNumberOfSmithPremiumItems; namespace { -template -T SwapLE(T in) -{ - switch (sizeof(T)) { - case 2: - return SDL_SwapLE16(in); - case 4: - return SDL_SwapLE32(in); - case 8: - return SDL_SwapLE64(in); - default: - return in; - } -} - -template -T SwapBE(T in) -{ - switch (sizeof(T)) { - case 2: - return SDL_SwapBE16(in); - case 4: - return SDL_SwapBE32(in); - case 8: - return SDL_SwapBE64(in); - default: - return in; - } -} - class LoadHelper { Uint8 *m_buffer; Uint32 m_bufferPtr = 0; diff --git a/SourceX/dvlnet/packet.cpp b/SourceX/dvlnet/packet.cpp index 1521f3087..973361c1b 100644 --- a/SourceX/dvlnet/packet.cpp +++ b/SourceX/dvlnet/packet.cpp @@ -1,4 +1,5 @@ #include "dvlnet/packet.h" +#include "engine.h" namespace dvl { namespace net { diff --git a/SourceX/dvlnet/packet.h b/SourceX/dvlnet/packet.h index 0d8d62c69..46f0c8546 100644 --- a/SourceX/dvlnet/packet.h +++ b/SourceX/dvlnet/packet.h @@ -172,6 +172,7 @@ void packet_in::process_element(T &x) if (decrypted_buffer.size() < sizeof(T)) throw packet_exception(); std::memcpy(&x, decrypted_buffer.data(), sizeof(T)); + x = SwapBE(x); decrypted_buffer.erase(decrypted_buffer.begin(), decrypted_buffer.begin() + sizeof(T)); } @@ -263,6 +264,7 @@ inline void packet_out::process_element(buffer_t &x) template void packet_out::process_element(T &x) { + x = SwapBE(x); encrypted_buffer.insert(encrypted_buffer.end(), begin(x), end(x)); }