diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-06-19 10:10:15 +0200 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-06-19 10:10:15 +0200 |
commit | 61d02a395b0ca594640f0fb917d7162a5aba2c37 (patch) | |
tree | 7dd6f7f540dda3d89d37bc7e19f3f4d27dd53c85 /src/net/tmwa/messageout.cpp | |
parent | 5da59b807d9840fb23feb42df729bf5e88ee9678 (diff) | |
parent | ac0b338b3e026d0d05a447894de9a6a994da82dc (diff) | |
download | mana-online-player-list.tar.gz mana-online-player-list.tar.bz2 mana-online-player-list.tar.xz mana-online-player-list.zip |
Merge branch 'master' into online-player-listonline-player-list
Conflicts:
src/net/tmwa/chathandler.cpp
Diffstat (limited to 'src/net/tmwa/messageout.cpp')
-rw-r--r-- | src/net/tmwa/messageout.cpp | 48 |
1 files changed, 16 insertions, 32 deletions
diff --git a/src/net/tmwa/messageout.cpp b/src/net/tmwa/messageout.cpp index 19f5ee49..12c9419a 100644 --- a/src/net/tmwa/messageout.cpp +++ b/src/net/tmwa/messageout.cpp @@ -23,54 +23,40 @@ #include "net/tmwa/network.h" -#include <SDL.h> #include <SDL_endian.h> #include <cstring> namespace TmwAthena { -MessageOut::MessageOut(uint16_t id): - mNetwork(TmwAthena::Network::instance()), - mData(mNetwork->mOutBuffer + mNetwork->mOutSize) +MessageOut::MessageOut(uint16_t id) { writeInt16(id); } -void MessageOut::expand(size_t bytes) +char *MessageOut::expand(size_t bytes) { - mNetwork->mOutSize += bytes; + Network &net = *Network::mInstance; + char *data = net.mOutBuffer + net.mOutSize; + net.mOutSize += bytes; + return data; } void MessageOut::writeInt8(uint8_t value) { - expand(1); - mData[mPos] = value; - mPos += 1; + *expand(1) = value; } void MessageOut::writeInt16(uint16_t value) { - expand(2); -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - uint16_t swap = SDL_Swap16(value); - memcpy(mData + mPos, &swap, sizeof(uint16_t)); -#else - memcpy(mData + mPos, &value, sizeof(uint16_t)); -#endif - mPos += 2; + value = SDL_SwapLE16(value); + memcpy(expand(sizeof(uint16_t)), &value, sizeof(uint16_t)); } void MessageOut::writeInt32(uint32_t value) { - expand(4); -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - uint32_t swap = SDL_Swap32(value); - memcpy(mData + mPos, &swap, sizeof(uint32_t)); -#else - memcpy(mData + mPos, &value, sizeof(uint32_t)); -#endif - mPos += 4; + value = SDL_SwapLE32(value); + memcpy(expand(sizeof(uint32_t)), &value, sizeof(uint32_t)); } void MessageOut::writeString(const std::string &string, int length) @@ -87,24 +73,22 @@ void MessageOut::writeString(const std::string &string, int length) // Make sure the length of the string is no longer than specified stringLength = length; } - expand(length); + + char *data = expand(length); // Write the actual string - memcpy(mData + mPos, string.data(), stringLength); + memcpy(data, string.data(), stringLength); // Pad remaining space with zeros if (length > stringLength) { - memset(mData + mPos + stringLength, '\0', length - stringLength); + memset(data + stringLength, '\0', length - stringLength); } - mPos += length; } void MessageOut::writeCoordinates(uint16_t x, uint16_t y, uint8_t direction) { - char *data = mData + mPos; - expand(3); - mPos += 3; + char *data = expand(3); uint16_t temp = x; temp <<= 6; |