diff --git a/include/game/World.h b/include/game/World.h index 2b3d7b2..2795b99 100644 --- a/include/game/World.h +++ b/include/game/World.h @@ -12,23 +12,30 @@ namespace td { namespace game { -typedef std::pair ChunkCoord; + +struct ChunkCoord { + std::int16_t x, y; + + friend bool operator==(const td::game::ChunkCoord& first, const td::game::ChunkCoord& other) { + return first.x == other.x && first.y == other.y; + } +}; + + } } + + namespace std { template <> struct hash { std::size_t operator()(const td::game::ChunkCoord& key) const { - // Compute individual hash values for first, - // second and third and combine them using XOR - // and bit shifting: - return ((std::hash()(key.first) ^ (std::hash()(key.second) << 1)) >> 1); + return std::hash()(key.x) ^ std::hash()(key.y); } }; } namespace td { - namespace protocol { class WorldBeginDataPacket; @@ -91,12 +98,13 @@ typedef std::vector ChunkPalette; typedef std::shared_ptr WalkableTilePtr; -typedef std::array ChunkData; typedef std::uint32_t TileIndex; //32 x 32 area struct Chunk { enum { ChunkWidth = 32, ChunkHeight = 32, ChunkSize = ChunkWidth * ChunkHeight }; + typedef std::array ChunkData; + // stores index of tile palette ChunkData tiles{ 0 }; ChunkPalette palette; diff --git a/include/misc/Compression.h b/include/misc/Compression.h index bcdf5d2..0173a63 100644 --- a/include/misc/Compression.h +++ b/include/misc/Compression.h @@ -6,6 +6,9 @@ namespace td { namespace utils { +std::uint64_t inflate(const std::string& source, std::string& dest); +std::uint64_t deflate(const std::string& source, std::string& dest); + DataBuffer Compress(const DataBuffer& buffer); DataBuffer Decompress(DataBuffer& buffer); DataBuffer Decompress(DataBuffer& buffer, std::uint64_t packetLength); diff --git a/include/misc/DataBuffer.h b/include/misc/DataBuffer.h index a40cfda..e13b8ea 100644 --- a/include/misc/DataBuffer.h +++ b/include/misc/DataBuffer.h @@ -1,5 +1,4 @@ -#ifndef MCLIB_COMMON_DATA_BUFFER_H_ -#define MCLIB_COMMON_DATA_BUFFER_H_ +#pragma once #include #include @@ -13,7 +12,7 @@ class DataBuffer { private: typedef std::vector Data; Data m_Buffer; - std::size_t m_ReadOffset = 0; + std::size_t m_ReadOffset; public: typedef Data::iterator iterator; @@ -23,7 +22,7 @@ public: DataBuffer(); DataBuffer(const DataBuffer& other); - DataBuffer(const DataBuffer& other, std::size_t offset); + DataBuffer(const DataBuffer& other, Data::difference_type offset); DataBuffer(DataBuffer&& other); DataBuffer(const std::string& str); @@ -64,7 +63,7 @@ public: template DataBuffer& operator>>(T& data) { assert(m_ReadOffset + sizeof(T) <= GetSize()); - data = *(T*)&m_Buffer[m_ReadOffset]; + data = *(reinterpret_cast(&m_Buffer[m_ReadOffset])); //std::reverse((std::uint8_t*)&data, (std::uint8_t*)&data + sizeof(T)); m_ReadOffset += sizeof(T); return *this; @@ -72,42 +71,43 @@ public: DataBuffer& operator>>(DataBuffer& data) { data.Resize(GetSize() - m_ReadOffset); - std::copy(m_Buffer.begin() + m_ReadOffset, m_Buffer.end(), data.begin()); + std::copy(m_Buffer.begin() + static_cast(m_ReadOffset), m_Buffer.end(), data.begin()); m_ReadOffset = m_Buffer.size(); return *this; } DataBuffer& operator>>(std::string& str) { - std::size_t stringSize = strlen((const char*)m_Buffer.data() + m_ReadOffset) + 1; // including null character + std::size_t stringSize = strlen(reinterpret_cast(m_Buffer.data()) + m_ReadOffset) + 1; // including null character str.resize(stringSize); - std::copy(m_Buffer.begin() + m_ReadOffset, m_Buffer.begin() + m_ReadOffset + stringSize, str.begin()); + std::copy(m_Buffer.begin() + static_cast(m_ReadOffset), + m_Buffer.begin() + static_cast(m_ReadOffset + stringSize), str.begin()); m_ReadOffset += stringSize; return *this; } void ReadSome(char* buffer, std::size_t amount) { assert(m_ReadOffset + amount <= GetSize()); - std::copy_n(m_Buffer.begin() + m_ReadOffset, amount, buffer); + std::copy_n(m_Buffer.begin() + static_cast(m_ReadOffset), amount, buffer); m_ReadOffset += amount; } void ReadSome(std::uint8_t* buffer, std::size_t amount) { assert(m_ReadOffset + amount <= GetSize()); - std::copy_n(m_Buffer.begin() + m_ReadOffset, amount, buffer); + std::copy_n(m_Buffer.begin() + static_cast(m_ReadOffset), amount, buffer); m_ReadOffset += amount; } void ReadSome(DataBuffer& buffer, std::size_t amount) { assert(m_ReadOffset + amount <= GetSize()); buffer.Resize(amount); - std::copy_n(m_Buffer.begin() + m_ReadOffset, amount, buffer.begin()); + std::copy_n(m_Buffer.begin() + static_cast(m_ReadOffset), amount, buffer.begin()); m_ReadOffset += amount; } void ReadSome(std::string& buffer, std::size_t amount) { assert(m_ReadOffset + amount <= GetSize()); buffer.resize(amount); - std::copy_n(m_Buffer.begin() + m_ReadOffset, amount, buffer.begin()); + std::copy_n(m_Buffer.begin() + static_cast(m_ReadOffset), amount, buffer.begin()); m_ReadOffset += amount; } @@ -163,5 +163,3 @@ public: std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer); } // ns td - -#endif diff --git a/include/network/TCPSocket.h b/include/network/TCPSocket.h index 2638d97..757bddf 100644 --- a/include/network/TCPSocket.h +++ b/include/network/TCPSocket.h @@ -15,7 +15,7 @@ class TCPSocket : public Socket { private: IPAddress m_RemoteIP; uint16_t m_Port; - sockaddr_in m_RemoteAddr; + sockaddr m_RemoteAddr; public: TCPSocket(); diff --git a/include/network/UDPSocket.h b/include/network/UDPSocket.h index 4124d4a..6cafb44 100644 --- a/include/network/UDPSocket.h +++ b/include/network/UDPSocket.h @@ -20,7 +20,9 @@ public: bool Connect(const IPAddress& address, std::uint16_t port); std::size_t Send(const std::uint8_t* data, std::size_t size); - DataBuffer Receive(std::size_t amount); + + virtual DataBuffer Receive(std::size_t amount); + virtual std::size_t Receive(DataBuffer& buffer, std::size_t amount); }; } // ns network diff --git a/src/misc/Compression.cpp b/src/misc/Compression.cpp index 2cb4c86..1904fd8 100644 --- a/src/misc/Compression.cpp +++ b/src/misc/Compression.cpp @@ -8,18 +8,17 @@ namespace td { namespace utils { -unsigned long inflate(const std::string& source, std::string& dest) { - unsigned long size = dest.size(); - uncompress((Bytef*)&dest[0], &size, (const Bytef*)source.c_str(), source.length()); +std::uint64_t inflate(const std::string& source, std::string& dest) { + std::size_t size; + uncompress(reinterpret_cast(dest.data()), &size, reinterpret_cast(source.c_str()), source.length()); return size; } -unsigned long deflate(const std::string& source, std::string& dest) { - unsigned long size = source.length(); - dest.resize(size); - - compress((Bytef*)&dest[0], &size, (const Bytef*)source.c_str(), source.length()); - dest.resize(size); +std::uint64_t deflate(const std::string& source, std::string& dest) { + std::size_t size; + dest.resize(source.size()); // Resize for the compressed data to fit into + compress(reinterpret_cast(dest.data()), &size, reinterpret_cast(source.c_str()), source.length()); + dest.resize(size); // Resize to cut useless data return size; } diff --git a/src/misc/DataBuffer.cpp b/src/misc/DataBuffer.cpp index 3782926..f0986c1 100644 --- a/src/misc/DataBuffer.cpp +++ b/src/misc/DataBuffer.cpp @@ -7,12 +7,13 @@ namespace td { -DataBuffer::DataBuffer() { } +DataBuffer::DataBuffer() : m_ReadOffset(0) { } DataBuffer::DataBuffer(const DataBuffer& other) : m_Buffer(other.m_Buffer), m_ReadOffset(other.m_ReadOffset) { } DataBuffer::DataBuffer(DataBuffer&& other) : m_Buffer(std::move(other.m_Buffer)), m_ReadOffset(std::move(other.m_ReadOffset)) { } -DataBuffer::DataBuffer(const std::string& str) : m_Buffer(str.begin(), str.end()) { } -DataBuffer::DataBuffer(const DataBuffer& other, std::size_t offset) { - m_Buffer.reserve(other.GetSize() - offset); +DataBuffer::DataBuffer(const std::string& str) : m_Buffer(str.begin(), str.end()), m_ReadOffset(0) { } + +DataBuffer::DataBuffer(const DataBuffer& other, Data::difference_type offset) : m_ReadOffset(0) { + m_Buffer.reserve(other.GetSize() - static_cast(offset)); std::copy(other.m_Buffer.begin() + offset, other.m_Buffer.end(), std::back_inserter(m_Buffer)); } @@ -58,7 +59,7 @@ DataBuffer::const_iterator DataBuffer::end() const { std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer) { for (unsigned char u : buffer) - os << std::hex << std::setfill('0') << std::setw(2) << (int)u << " "; + os << std::hex << std::setfill('0') << std::setw(2) << static_cast(u) << " "; os << std::dec; return os; } @@ -81,7 +82,7 @@ bool DataBuffer::ReadFile(const std::string& fileName) { bool DataBuffer::WriteFile(const std::string& fileName) { try { std::ofstream file(fileName, std::ostream::binary); - file.write((const char*)m_Buffer.data(), m_Buffer.size()); + file.write(reinterpret_cast(m_Buffer.data()), static_cast(m_Buffer.size())); file.flush(); } catch (std::exception& e) { std::cerr << "Failed to write file \"" << fileName << "\" reason : " << e.what() << std::endl; diff --git a/src/network/Network.cpp b/src/network/Network.cpp index f667678..b1007b6 100644 --- a/src/network/Network.cpp +++ b/src/network/Network.cpp @@ -33,7 +33,8 @@ NetworkInitializer initializer; IPAddresses Dns::Resolve(const std::string& host) { IPAddresses list; - addrinfo hints = { 0 }, * addresses; + addrinfo hints{}; + addrinfo* addresses = nullptr; //hints.ai_family = AF_UNSPEC; hints.ai_family = AF_INET; @@ -54,7 +55,7 @@ IPAddresses Dns::Resolve(const std::string& host) { #else char straddr[512]; - inet_ntop(p->ai_family, &((sockaddr_in*)p->ai_addr)->sin_addr, straddr, sizeof(straddr)); + inet_ntop(p->ai_family, &(reinterpret_cast(p->ai_addr))->sin_addr, straddr, sizeof(straddr)); #endif list.push_back(IPAddress(straddr)); diff --git a/src/network/Socket.cpp b/src/network/Socket.cpp index b047c1d..a595290 100644 --- a/src/network/Socket.cpp +++ b/src/network/Socket.cpp @@ -16,7 +16,7 @@ Socket::Socket(Type type) : m_Blocking(false), m_Type(type), m_Status(Disconnected), - m_Handle((SocketHandle)INVALID_SOCKET) + m_Handle(static_cast(INVALID_SOCKET)) { } diff --git a/src/network/TCPSocket.cpp b/src/network/TCPSocket.cpp index 1d74b7b..351808d 100644 --- a/src/network/TCPSocket.cpp +++ b/src/network/TCPSocket.cpp @@ -13,10 +13,8 @@ namespace td { namespace network { -TCPSocket::TCPSocket() - : Socket(Socket::TCP), m_Port(0) -{ - m_Handle = (SocketHandle)INVALID_SOCKET; +TCPSocket::TCPSocket() : Socket(Socket::TCP), m_Port(0) { + m_Handle = static_cast(INVALID_SOCKET); } bool TCPSocket::Connect(const IPAddress& address, unsigned short port) { @@ -32,7 +30,7 @@ bool TCPSocket::Connect(const IPAddress& address, unsigned short port) { hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; - if (::getaddrinfo(address.ToString().c_str(), std::to_string(port).c_str(), &hints, &result) != 0) { + if (::getaddrinfo(address.ToString().c_str(), std::to_string(static_cast(port)).c_str(), &hints, &result) != 0) { std::cerr << "Failed to get address info !\n"; return false; } @@ -44,8 +42,8 @@ bool TCPSocket::Connect(const IPAddress& address, unsigned short port) { struct addrinfo* ptr = nullptr; for (ptr = result; ptr != nullptr; ptr = ptr->ai_next) { - struct sockaddr_in* sockaddr = (struct sockaddr_in*)ptr->ai_addr; - if (::connect(m_Handle, (struct sockaddr*)sockaddr, sizeof(struct sockaddr_in)) != 0) { + struct sockaddr* sockaddr = ptr->ai_addr; + if (::connect(m_Handle, sockaddr, sizeof(sockaddr_in)) != 0) { std::cerr << "Failed to connect with this address !\n"; continue; } @@ -78,7 +76,7 @@ size_t TCPSocket::Send(const unsigned char* data, size_t size) { Disconnect(); return 0; } - sent += cur; + sent += static_cast(cur); } return sent; @@ -88,7 +86,7 @@ std::size_t TCPSocket::Receive(DataBuffer& buffer, std::size_t amount) { buffer.Resize(amount); buffer.SetReadOffset(0); - int recvAmount = ::recv(m_Handle, (char*)&buffer[0], amount, 0); + int recvAmount = ::recv(m_Handle, reinterpret_cast(buffer.data()), amount, 0); if (recvAmount <= 0) { #if defined(_WIN32) || defined(WIN32) int err = WSAGetLastError(); @@ -104,8 +102,8 @@ std::size_t TCPSocket::Receive(DataBuffer& buffer, std::size_t amount) { buffer.Clear(); return 0; } - buffer.Resize(recvAmount); - return recvAmount; + buffer.Resize(static_cast(recvAmount)); + return static_cast(recvAmount); } DataBuffer TCPSocket::Receive(std::size_t amount) { @@ -126,7 +124,7 @@ DataBuffer TCPSocket::Receive(std::size_t amount) { return DataBuffer(); } - return DataBuffer(std::string(buf.get(), received)); + return DataBuffer(std::string(buf.get(), static_cast(received))); } TCPSocket::TCPSocket(TCPSocket&& other) : Socket(TCP) { @@ -136,12 +134,12 @@ TCPSocket::TCPSocket(TCPSocket&& other) : Socket(TCP) { m_RemoteIP = other.m_RemoteIP; SetStatus(other.GetStatus()); SetBlocking(other.IsBlocking()); - other.m_Handle = (SocketHandle)INVALID_SOCKET; + other.m_Handle = static_cast(INVALID_SOCKET); } void SendPacket(const DataBuffer& data, network::TCPSocket& socket) { DataBuffer compressed = utils::Compress(data); - socket.Send((const std::uint8_t*)compressed.ToString().data(), compressed.GetSize()); + socket.Send(reinterpret_cast(compressed.ToString().data()), compressed.GetSize()); } diff --git a/src/network/UDPSocket.cpp b/src/network/UDPSocket.cpp index fdff46d..cdf0aa1 100644 --- a/src/network/UDPSocket.cpp +++ b/src/network/UDPSocket.cpp @@ -14,7 +14,7 @@ namespace network { UDPSocket::UDPSocket() : Socket(Socket::UDP), m_Port(0) { - m_Handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + m_Handle = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); } bool UDPSocket::Connect(const IPAddress& address, unsigned short port) { @@ -31,20 +31,20 @@ bool UDPSocket::Connect(const IPAddress& address, unsigned short port) { return true; } -size_t UDPSocket::Send(const unsigned char* data, size_t size) { - size_t sent = 0; +std::size_t UDPSocket::Send(const unsigned char* data, std::size_t size) { + std::size_t sent = 0; if (this->GetStatus() != Connected) return 0; while (sent < size) { - int cur = sendto(m_Handle, reinterpret_cast(data + sent), size - sent, 0, + int cur = ::sendto(m_Handle, reinterpret_cast(data + sent), size - sent, 0, reinterpret_cast(&m_RemoteAddr), sizeof(sockaddr_in)); if (cur <= 0) { Disconnect(); return 0; } - sent += cur; + sent += static_cast(cur); } return sent; @@ -54,7 +54,7 @@ DataBuffer UDPSocket::Receive(std::size_t amount) { std::unique_ptr buf(new char[amount]); socklen_t slen = sizeof(sockaddr_in); - int received = recvfrom(m_Handle, buf.get(), amount, 0, + int received = ::recvfrom(m_Handle, buf.get(), amount, 0, reinterpret_cast(&m_RemoteAddr), &slen); if (received <= 0) { @@ -64,13 +64,40 @@ DataBuffer UDPSocket::Receive(std::size_t amount) { int err = errno; #endif if (err == WOULDBLOCK) - return DataBuffer(std::string(buf.get(), received)); + return DataBuffer(std::string(buf.get(), static_cast(received))); Disconnect(); return DataBuffer(); } - return DataBuffer(std::string(buf.get(), received)); + return DataBuffer(std::string(buf.get(), static_cast(received))); +} + +std::size_t UDPSocket::Receive(DataBuffer& buffer, std::size_t amount) { + buffer.Resize(amount); + buffer.SetReadOffset(0); + + socklen_t slen = sizeof(sockaddr_in); + + int recvAmount = ::recvfrom(m_Handle, reinterpret_cast(buffer.data()), amount, 0, + reinterpret_cast(&m_RemoteAddr), &slen); + if (recvAmount <= 0) { +#if defined(_WIN32) || defined(WIN32) + int err = WSAGetLastError(); +#else + int err = errno; +#endif + if (err == WOULDBLOCK) { + buffer.Clear(); + return 0; + } + + Disconnect(); + buffer.Clear(); + return 0; + } + buffer.Resize(static_cast(recvAmount)); + return static_cast(recvAmount); } } // ns network diff --git a/src/protocol/Protocol.cpp b/src/protocol/Protocol.cpp index 62fce2e..a31c227 100644 --- a/src/protocol/Protocol.cpp +++ b/src/protocol/Protocol.cpp @@ -192,7 +192,7 @@ DataBuffer WorldDataPacket::SerializeCustom() const { game::ChunkCoord coords = pair.first; game::ChunkPtr chunk = pair.second; - data << coords.first << coords.second << (std::uint64_t)chunk->palette.size(); + data << coords.x << coords.y << (std::uint64_t)chunk->palette.size(); std::size_t bufferSize = data.GetSize(); data.Resize(data.GetSize() + chunk->palette.size() * sizeof(game::ChunkPalette::value_type)); @@ -200,7 +200,7 @@ DataBuffer WorldDataPacket::SerializeCustom() const { std::uint8_t bitsPerTile = countBits(chunk->palette.size()); - game::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1); + game::Chunk::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1); ChunkPackedData chunkData(game::Chunk::ChunkSize / (BITS_IN_BYTE * sizeof(ChunkPackedData::value_type) / bitsPerTile), 0); @@ -235,7 +235,7 @@ DataBuffer WorldDataPacket::Serialize() const { game::ChunkCoord coords = pair.first; game::ChunkPtr chunk = pair.second; - data << coords.first << coords.second << (std::uint64_t)chunk->palette.size(); + data << coords.x << coords.y << (std::uint64_t)chunk->palette.size(); std::size_t bufferSize = data.GetSize(); data.Resize(data.GetSize() + chunk->palette.size() * sizeof(game::ChunkPalette::value_type)); @@ -243,7 +243,7 @@ DataBuffer WorldDataPacket::Serialize() const { std::uint8_t bitsPerTile = countBits(chunk->palette.size()); - game::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1); + game::Chunk::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1); ChunkPackedData chunkData(game::Chunk::ChunkSize / (BITS_IN_BYTE * sizeof(ChunkPackedData::value_type) / bitsPerTile), 0); @@ -278,7 +278,7 @@ void WorldDataPacket::Deserialize(DataBuffer& data) { for (std::uint64_t chunkNumber = 0; chunkNumber < chunkCount; chunkNumber++) { game::ChunkPtr chunk = std::make_shared(); - game::ChunkCoord::first_type chunkX, chunkY; + decltype(game::ChunkCoord::x) chunkX, chunkY; data >> chunkX >> chunkY; std::uint64_t chunkPaletteSize; @@ -294,7 +294,7 @@ void WorldDataPacket::Deserialize(DataBuffer& data) { std::uint8_t bitsPerTile = countBits(chunkPaletteSize); // A bitmask that contains bitsPerTile set bits - game::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1); + game::Chunk::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1); ChunkPackedData chunkData(game::Chunk::ChunkSize / (BITS_IN_BYTE * sizeof(ChunkPackedData::value_type) / bitsPerTile), 0); @@ -306,7 +306,7 @@ void WorldDataPacket::Deserialize(DataBuffer& data) { int startOffset = (tileNumber * bitsPerTile) % BITS_IN_LONG; int endLong = ((tileNumber + 1) * bitsPerTile - 1) / BITS_IN_LONG; - game::ChunkData::value_type value; + game::Chunk::ChunkData::value_type value; if (startLong == endLong) { value = (chunkData[startLong] >> startOffset); } else { diff --git a/src/render/loader/WorldLoader.cpp b/src/render/loader/WorldLoader.cpp index b847c5d..7e17093 100644 --- a/src/render/loader/WorldLoader.cpp +++ b/src/render/loader/WorldLoader.cpp @@ -56,8 +56,8 @@ GL::VertexArray loadWorldModel(const td::game::World* world) { const td::game::ChunkCoord& coords = chunkInfo.first; td::game::ChunkPtr chunk = chunkInfo.second; - std::int32_t chunkX = coords.first * td::game::Chunk::ChunkWidth; - std::int32_t chunkY = coords.second * td::game::Chunk::ChunkHeight; + std::int32_t chunkX = coords.x * td::game::Chunk::ChunkWidth; + std::int32_t chunkY = coords.y * td::game::Chunk::ChunkHeight; for (int tileY = 0; tileY < td::game::Chunk::ChunkHeight; tileY++) { for (int tileX = 0; tileX < td::game::Chunk::ChunkWidth; tileX++) { @@ -97,8 +97,8 @@ GL::VertexArray loadWorldModel(const td::game::World* world) { for (int spawnColor = 0; spawnColor < 2; spawnColor++) { const game::Spawn& spawn = world->getTeam(game::TeamColor(spawnColor)).getSpawn(); - float fromX = spawn.getTopLeft().getX() , toX = spawn.getBottomRight().getX(); - float fromY = spawn.getTopLeft().getY() , toY = spawn.getBottomRight().getY(); + float fromX = spawn.getTopLeft().getX(), toX = spawn.getBottomRight().getX(); + float fromY = spawn.getTopLeft().getY(), toY = spawn.getBottomRight().getY(); positions.insert(positions.end(), { fromX, fromY,