From fdfd14c4795610b1c57b586869e4fad6eba6f85f Mon Sep 17 00:00:00 2001 From: CAYEUX Mathieu <53528937+Lygaen@users.noreply.github.com> Date: Mon, 6 May 2024 09:46:10 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=B3=20Fixed=20support=20for=20MSVC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 +++ libs/CMakeLists.txt | 3 +- libs/rapidjson | 2 +- src/net/packets/login/encryptionexchange.cpp | 4 +-- src/net/stream.cpp | 36 +++++++++++++++----- src/plugins/event.h | 6 ++-- src/utils/crypto.cpp | 1 + src/utils/crypto.h | 2 ++ 8 files changed, 42 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 41ba3d48..87708859 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,8 @@ /run/ /cmake-build-debug/ /cmake-build-release/ +/out/ +/.vs/ +CmakeSettings.json +Folder.DotSettings.user scrap.excalidraw \ No newline at end of file diff --git a/libs/CMakeLists.txt b/libs/CMakeLists.txt index a71885bc..d307dfd8 100644 --- a/libs/CMakeLists.txt +++ b/libs/CMakeLists.txt @@ -8,9 +8,10 @@ set(CMAKE_WARN_DEPRECATED OFF CACHE BOOL "" FORCE) set(RAPIDJSON_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) set(RAPIDJSON_BUILD_DOC OFF CACHE BOOL "" FORCE) set(RAPIDJSON_BUILD_TESTS OFF CACHE BOOL "" FORCE) -set(RAPIDJSON_BUILD_CXX17 ON CACHE BOOL "" FORCE) +set(RAPIDJSON_BUILD_CXX20 ON CACHE BOOL "" FORCE) add_subdirectory(rapidjson/) +set(ZLIB_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) add_subdirectory(zlib/) # Fixing ZLib shit because we are doing a submodule install, and it is annoying to diff --git a/libs/rapidjson b/libs/rapidjson index f54b0e47..ab1842a2 160000 --- a/libs/rapidjson +++ b/libs/rapidjson @@ -1 +1 @@ -Subproject commit f54b0e47a08782a6131cc3d60f94d038fa6e0a51 +Subproject commit ab1842a2dae061284c0a62dca1cc6d5e7e37e346 diff --git a/src/net/packets/login/encryptionexchange.cpp b/src/net/packets/login/encryptionexchange.cpp index 6c3b0a1d..3cbcfbec 100644 --- a/src/net/packets/login/encryptionexchange.cpp +++ b/src/net/packets/login/encryptionexchange.cpp @@ -43,14 +43,14 @@ void EncryptionResponse::read(IMCStream *stream) { { int len = stream->readVarInt(); - std::byte buff[len]; + std::byte *buff = new std::byte[len]; stream->read(buff, 0, len); sharedSecret = crypto::rsaDecrypt(buff, len, &sharedSecretLength); } { int len = stream->readVarInt(); - std::byte buff[len]; + std::byte *buff = new std::byte[len]; stream->read(buff, 0, len); verifyToken = crypto::rsaDecrypt(buff, len, &verifyTokenLength); diff --git a/src/net/stream.cpp b/src/net/stream.cpp index d1a92023..c23dd737 100644 --- a/src/net/stream.cpp +++ b/src/net/stream.cpp @@ -109,7 +109,12 @@ std::int32_t IMCStream::readInt() { // Kinda tedious but better than nothing ! std::uint32_t i = *reinterpret_cast(&b); - i = __builtin_bswap32(i); +#ifdef _WIN32 + // no builtin bswap for MSVC + i = (i << 24) | ((i & 0xff00) << 8) | ((i & 0xff0000) >> 8) | (i >> 24); +#elif defined(__linux__) + n = __builtin_bswap32(n); +#endif return *reinterpret_cast(&i); } else @@ -123,7 +128,12 @@ void IMCStream::writeInt(std::int32_t i) if constexpr (std::endian::native == std::endian::little) { std::uint32_t n = *reinterpret_cast(&i); +#ifdef _WIN32 + // no builtin bswap for MSVC + n = (n << 24) | ((n & 0xff00) << 8) | ((n & 0xff0000) >> 8) | (n >> 24); +#elif defined(__linux__) n = __builtin_bswap32(n); +#endif write(reinterpret_cast(&n), 0, sizeof(std::int32_t)); } else @@ -141,7 +151,11 @@ std::int64_t IMCStream::readLong() { // Again, kinda tedious but better than nothing ! std::uint64_t i = *reinterpret_cast(&b); +#ifdef _WIN32 + i = _byteswap_uint64(i); +#elif defined(__linux__) i = __builtin_bswap64(i); +#endif return *reinterpret_cast(&i); } else @@ -155,7 +169,11 @@ void IMCStream::writeLong(std::int64_t l) if constexpr (std::endian::native == std::endian::little) { std::uint64_t n = *reinterpret_cast(&l); - n = __builtin_bswap64(n); +#ifdef _WIN32 + n = _byteswap_uint64(n); +#elif defined(__linux__) + i = __builtin_bswap64(i); +#endif write(reinterpret_cast(&n), 0, sizeof(std::int64_t)); } else @@ -344,6 +362,7 @@ MinecraftUUID IMCStream::readUUID() return uuid; } +#undef min void MemoryStream::read(std::byte *buffer, std::size_t offset, std::size_t len) { std::memcpy(buffer + offset, data.data() + readIndex, std::min(len, data.size())); @@ -518,7 +537,7 @@ void ZLibStream::finishPacketWrite(const std::byte *packetData, size_t len) return; } - std::byte compBytes[2 * len]; + std::byte *compBytes = new std::byte[2 * len]; int packetLength = comp.compress(packetData, len, compBytes, 2 * len); baseStream->writeVarInt(packetLength + calculateVarIntSize(len)); @@ -542,7 +561,7 @@ void ZLibStream::flush() if (len >= threshold) throw std::runtime_error("Invalid received compression size"); - std::byte bytes[len]; + std::byte *bytes = new std::byte[len]; baseStream->read(bytes, 0, len); // We write back the length to the stream @@ -554,10 +573,10 @@ void ZLibStream::flush() return; } int len = packetLength - calculateVarIntSize(dataLength); - std::byte compressed[len]; + std::byte *compressed = new std::byte[len]; baseStream->read(compressed, 0, len); - std::byte final[dataLength]; + std::byte *final = new std::byte[dataLength]; int written = comp.uncompress(compressed, len, final, dataLength); if (written != dataLength) throw std::runtime_error("Invalid uncompressed length"); @@ -565,7 +584,6 @@ void ZLibStream::flush() // We write back the length to the stream MemoryStream m; m.writeVarInt(dataLength); - std::copy(m.getData().begin(), m.getData().end(), std::back_inserter(inBuffer)); - - std::copy(final, final + dataLength, std::back_inserter(inBuffer)); + std::ranges::copy(m.getData(), std::back_inserter(inBuffer)); + inBuffer.insert(inBuffer.end(), final, final + dataLength); } diff --git a/src/plugins/event.h b/src/plugins/event.h index a8d99d51..c5cb890a 100644 --- a/src/plugins/event.h +++ b/src/plugins/event.h @@ -282,7 +282,7 @@ class EventsManager * @return EventHandler::subId the handler used for unsuscribing */ template - EventHandler::subId subscribe(const EventHandler::callbackType &callback) + typename EventHandler::subId subscribe(const typename EventHandler::callbackType &callback) { static_assert(std::is_base_of_v, T>, "Class doesn't derive from IEvent"); EventHandler *handler = getOrCreateHandler(); @@ -298,7 +298,7 @@ class EventsManager * @return EventHandler::subId the handler used for unsuscribing */ template - EventHandler::subId subscribe(EventHandler::callbackType &&callback) + typename EventHandler::subId subscribe(typename EventHandler::callbackType &&callback) { static_assert(std::is_base_of_v, T>, "Class doesn't derive from IEvent"); EventHandler *handler = getOrCreateHandler(); @@ -313,7 +313,7 @@ class EventsManager * @param subId the handler of the function */ template - void unsubscribe(EventHandler::subId subId) + void unsubscribe(typename EventHandler::subId subId) { static_assert(std::is_base_of_v, T>, "Class doesn't derive from IEvent"); EventHandler *handler = getOrCreateHandler(); diff --git a/src/utils/crypto.cpp b/src/utils/crypto.cpp index 5d50a217..5c4adf7f 100644 --- a/src/utils/crypto.cpp +++ b/src/utils/crypto.cpp @@ -16,6 +16,7 @@ #include #include #include +#include /** * @brief The static OpenCrypto keypair variable diff --git a/src/utils/crypto.h b/src/utils/crypto.h index 2a036744..d7b0d7ab 100644 --- a/src/utils/crypto.h +++ b/src/utils/crypto.h @@ -13,6 +13,8 @@ #define MINESERVER_CRYPTO_H #include +#include +#include #include #include #include