Skip to content

Commit

Permalink
🥳 Fixed support for MSVC
Browse files Browse the repository at this point in the history
  • Loading branch information
Lygaen committed May 6, 2024
1 parent 6c0598c commit fdfd14c
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
/run/
/cmake-build-debug/
/cmake-build-release/
/out/
/.vs/
CmakeSettings.json
Folder.DotSettings.user
scrap.excalidraw
3 changes: 2 additions & 1 deletion libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libs/rapidjson
Submodule rapidjson updated 150 files
4 changes: 2 additions & 2 deletions src/net/packets/login/encryptionexchange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 27 additions & 9 deletions src/net/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ std::int32_t IMCStream::readInt()
{
// Kinda tedious but better than nothing !
std::uint32_t i = *reinterpret_cast<std::uint32_t *>(&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<std::int32_t *>(&i);
}
else
Expand All @@ -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<std::uint32_t *>(&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<std::byte *>(&n), 0, sizeof(std::int32_t));
}
else
Expand All @@ -141,7 +151,11 @@ std::int64_t IMCStream::readLong()
{
// Again, kinda tedious but better than nothing !
std::uint64_t i = *reinterpret_cast<std::uint64_t *>(&b);
#ifdef _WIN32
i = _byteswap_uint64(i);
#elif defined(__linux__)
i = __builtin_bswap64(i);
#endif
return *reinterpret_cast<std::int64_t *>(&i);
}
else
Expand All @@ -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<std::uint64_t *>(&l);
n = __builtin_bswap64(n);
#ifdef _WIN32
n = _byteswap_uint64(n);
#elif defined(__linux__)
i = __builtin_bswap64(i);
#endif
write(reinterpret_cast<std::byte *>(&n), 0, sizeof(std::int64_t));
}
else
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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));
Expand All @@ -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
Expand All @@ -554,18 +573,17 @@ 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");

// 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);
}
6 changes: 3 additions & 3 deletions src/plugins/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class EventsManager
* @return EventHandler<T>::subId the handler used for unsuscribing
*/
template <class T>
EventHandler<T>::subId subscribe(const EventHandler<T>::callbackType &callback)
typename EventHandler<T>::subId subscribe(const typename EventHandler<T>::callbackType &callback)
{
static_assert(std::is_base_of_v<IEvent<T>, T>, "Class doesn't derive from IEvent");
EventHandler<T> *handler = getOrCreateHandler<T>();
Expand All @@ -298,7 +298,7 @@ class EventsManager
* @return EventHandler<T>::subId the handler used for unsuscribing
*/
template <class T>
EventHandler<T>::subId subscribe(EventHandler<T>::callbackType &&callback)
typename EventHandler<T>::subId subscribe(typename EventHandler<T>::callbackType &&callback)
{
static_assert(std::is_base_of_v<IEvent<T>, T>, "Class doesn't derive from IEvent");
EventHandler<T> *handler = getOrCreateHandler<T>();
Expand All @@ -313,7 +313,7 @@ class EventsManager
* @param subId the handler of the function
*/
template <class T>
void unsubscribe(EventHandler<T>::subId subId)
void unsubscribe(typename EventHandler<T>::subId subId)
{
static_assert(std::is_base_of_v<IEvent<T>, T>, "Class doesn't derive from IEvent");
EventHandler<T> *handler = getOrCreateHandler<T>();
Expand Down
1 change: 1 addition & 0 deletions src/utils/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <openssl/x509.h>
#include <openssl/md5.h>
#include <cstring>
#include <stdexcept>

/**
* @brief The static OpenCrypto keypair variable
Expand Down
2 changes: 2 additions & 0 deletions src/utils/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#define MINESERVER_CRYPTO_H

#include <memory>
#include <string>
#include <cstddef>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <zlib.h>
Expand Down

0 comments on commit fdfd14c

Please sign in to comment.