From ce6ffaabce4047f07eb050dd50c41c7348cb8236 Mon Sep 17 00:00:00 2001 From: Foe Date: Mon, 29 Jul 2024 22:56:02 +0200 Subject: [PATCH 1/2] Install x11 library on runner --- .github/workflows/linux-build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/linux-build.yml b/.github/workflows/linux-build.yml index 04f4fbff..cc567733 100644 --- a/.github/workflows/linux-build.yml +++ b/.github/workflows/linux-build.yml @@ -23,7 +23,7 @@ jobs: - name: Install dependencies run: | - sudo apt-get update && sudo apt-get install -yq clang + sudo apt-get update && sudo apt-get install -yq clang libx11-dev sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-14 100 - name: Install Vulkan SDK From c8ac3a84b26b360398b41ae885030948a58ef730 Mon Sep 17 00:00:00 2001 From: NixAJ Date: Tue, 30 Jul 2024 17:42:34 +0200 Subject: [PATCH 2/2] Added AreaLight related Client DB Definitions Fixed an issue where 32 bit floats marked as bitpacked was not properly read Added Gameplay library Moved some Network code into Gameplay Fixed an issue where the window title would be incorrect --- .../FileFormat/Novus/ClientDB/ClientDB.h | 54 +++---- .../FileFormat/Novus/ClientDB/Definitions.h | 150 ++++++++++++++++++ .../FileFormat/Warcraft/Parsers/Wdc3Parser.h | 2 +- Source/Gameplay/Gameplay.lua | 18 +++ Source/Gameplay/Gameplay/Gameplay.h | 0 Source/Gameplay/Gameplay/Network/Define.h | 10 ++ .../Gameplay/Network/GameMessageRouter.cpp | 33 ++++ .../Gameplay/Network/GameMessageRouter.h | 41 +++++ Source/Gameplay/Gameplay/Network/Opcode.h | 34 ++++ Source/Modules.lua | 1 + Source/Network/Network/Client.cpp | 7 +- Source/Network/Network/Client.h | 1 + Source/Network/Network/Define.h | 93 +---------- Source/Network/Network/PacketHandler.cpp | 29 ---- Source/Network/Network/PacketHandler.h | 39 ----- Source/Network/Network/Server.cpp | 17 +- Source/Network/Network/Socket.cpp | 35 ++-- Source/Network/Network/Socket.h | 5 +- Source/Renderer/Renderer/Window.cpp | 6 +- 19 files changed, 351 insertions(+), 224 deletions(-) create mode 100644 Source/Gameplay/Gameplay.lua create mode 100644 Source/Gameplay/Gameplay/Gameplay.h create mode 100644 Source/Gameplay/Gameplay/Network/Define.h create mode 100644 Source/Gameplay/Gameplay/Network/GameMessageRouter.cpp create mode 100644 Source/Gameplay/Gameplay/Network/GameMessageRouter.h create mode 100644 Source/Gameplay/Gameplay/Network/Opcode.h delete mode 100644 Source/Network/Network/PacketHandler.cpp delete mode 100644 Source/Network/Network/PacketHandler.h diff --git a/Source/FileFormat/FileFormat/Novus/ClientDB/ClientDB.h b/Source/FileFormat/FileFormat/Novus/ClientDB/ClientDB.h index 068b3e0e..0e128529 100644 --- a/Source/FileFormat/FileFormat/Novus/ClientDB/ClientDB.h +++ b/Source/FileFormat/FileFormat/Novus/ClientDB/ClientDB.h @@ -40,11 +40,11 @@ namespace ClientDB for (void* row : _rows) { if (row) - delete row; - } - } + delete row; + } + } - void Each(std::function* storage, const T*)> func) + void Each(std::function* storage, const T*)> func) const { for (T* row : _rows) { @@ -95,10 +95,10 @@ namespace ClientDB { u32 maxID = _minID + Count(); if (id < _minID || id > maxID) - return nullptr; + return nullptr; - return _rows[id - _minID]; - } + return _rows[id - _minID]; + } bool HasRow(u32 id) { const T* row = GetRow(id); @@ -135,8 +135,8 @@ namespace ClientDB memset(_rows.data(), 0, sizeof(T*) * numNewRows); _minID = newMinID; - } - } + } + } bool hasRowAtID = _rows[row.id - _minID] != nullptr; _numRows += 1 * !hasRowAtID; @@ -182,7 +182,7 @@ namespace ClientDB u32 numRows = static_cast(_rows.size()); return numRows; } - u32 GetNumRows() + u32 GetNumRows() const { return _numRows; } @@ -221,16 +221,16 @@ namespace ClientDB if (header.version != CURRENT_VERSION) { NC_LOG_ERROR("ClientDB::Storage::Read('{0}') - Version mismatch. Got: {1}, Expected: {2}", _name, header.version, CURRENT_VERSION); - return false; - } + return false; + } u32 maxID = 0; - if (!buffer->GetU32(_minID)) - return false; + if (!buffer->GetU32(_minID)) + return false; - if (!buffer->GetU32(maxID)) - return false; + if (!buffer->GetU32(maxID)) + return false; u32 numRows = maxID - _minID + 1; _rows.resize(numRows); @@ -241,16 +241,16 @@ namespace ClientDB while (buffer->GetActiveSize() > 0) { - T* row = new T(); + T* row = new T(); if (!row->Read(buffer, stringTable)) { - delete row; - return false; - } + delete row; + return false; + } if (row->id < _minID || row->id > maxID) { - NC_LOG_ERROR("ClientDB::Storage::Read('{0}') - Row ID out of bounds. Got: {1} (Min: {2}, Max: {3})", _name, row->id, _minID, maxID); + NC_LOG_ERROR("ClientDB::Storage::Read('{0}') - Row ID out of bounds. Got: {1} (Min: {2}, Max: {3})", _name, row->id, _minID, maxID); delete row; continue; @@ -260,10 +260,10 @@ namespace ClientDB _numRows += 1 * !hasRowAtID; _rows[row->id - _minID] = row; - } + } - return true; - } + return true; + } bool Write(std::shared_ptr& buffer, const Novus::Container::StringTableUnsafe& stringTable) { @@ -301,9 +301,9 @@ namespace ClientDB if (!row) continue; - if (!row->Write(buffer, stringTable)) - return false; - } + if (!row->Write(buffer, stringTable)) + return false; + } return true; } diff --git a/Source/FileFormat/FileFormat/Novus/ClientDB/Definitions.h b/Source/FileFormat/FileFormat/Novus/ClientDB/Definitions.h index 9c05b17a..32e20852 100644 --- a/Source/FileFormat/FileFormat/Novus/ClientDB/Definitions.h +++ b/Source/FileFormat/FileFormat/Novus/ClientDB/Definitions.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -770,6 +771,155 @@ namespace ClientDB::Definitions } }; + struct Light : public Base + { + public: + u16 mapID; + vec3 position; + vec2 fallOff; + u16 lightParamsID[8]; + + public: + bool Read(std::shared_ptr& buffer, const Novus::Container::StringTableUnsafe& stringTable) override + { + if (!buffer->Get(*this)) + return false; + + return true; + } + bool Write(std::shared_ptr& buffer, const Novus::Container::StringTableUnsafe& stringTable) override + { + if (!buffer->Put(*this)) + return false; + + return true; + } + bool WriteStringTable(Novus::Container::StringTableUnsafe& stringTable) override + { + return true; + } + }; + + struct LightParam : public Base + { + public: + struct Flags + { + u8 highlightSky : 1 = 0; + u8 : 7; + }; + + public: + Flags flags; + u16 lightSkyboxID; + f32 glow; + f32 waterShallowAlpha; + f32 waterDeepAlpha; + f32 oceanShallowAlpha; + f32 oceanDeepAlpha; + + public: + bool Read(std::shared_ptr& buffer, const Novus::Container::StringTableUnsafe& stringTable) override + { + if (!buffer->Get(*this)) + return false; + + return true; + } + bool Write(std::shared_ptr& buffer, const Novus::Container::StringTableUnsafe& stringTable) override + { + if (!buffer->Put(*this)) + return false; + + return true; + } + bool WriteStringTable(Novus::Container::StringTableUnsafe& stringTable) override + { + return true; + } + }; + + struct LightData : public Base + { + public: + u16 lightParamID; + u32 timestamp; + u32 diffuseColor; + u32 ambientColor; + u32 skyTopColor; + u32 skyMiddleColor; + u32 skyBand1Color; + u32 skyBand2Color; + u32 skySmogColor; + u32 skyFogColor; + u32 sunColor; + u32 sunFogColor; + f32 sunFogStrength; + u32 cloudSunColor; + u32 cloudEmissiveColor; + u32 cloudLayer1AmbientColor; + u32 cloudLayer2AmbientColor; + u32 oceanShallowColor; + u32 oceanDeepColor; + u32 riverShallowColor; + u32 riverDeepColor; + u32 shadowColor; + f32 fogEnd; + f32 fogScaler; + f32 fogDensity; + f32 sunFogAngle; + f32 cloudDensity; + u32 fogHeightColor; + u32 endFogColor; + u32 endFogHeightColor; + + public: + bool Read(std::shared_ptr& buffer, const Novus::Container::StringTableUnsafe& stringTable) override + { + if (!buffer->Get(*this)) + return false; + + return true; + } + bool Write(std::shared_ptr& buffer, const Novus::Container::StringTableUnsafe& stringTable) override + { + if (!buffer->Put(*this)) + return false; + + return true; + } + bool WriteStringTable(Novus::Container::StringTableUnsafe& stringTable) override + { + return true; + } + }; + + struct LightSkybox : public Base + { + public: + u32 modelHash; + + public: + bool Read(std::shared_ptr& buffer, const Novus::Container::StringTableUnsafe& stringTable) override + { + if (!buffer->Get(*this)) + return false; + + return true; + } + bool Write(std::shared_ptr& buffer, const Novus::Container::StringTableUnsafe& stringTable) override + { + if (!buffer->Put(*this)) + return false; + + return true; + } + bool WriteStringTable(Novus::Container::StringTableUnsafe& stringTable) override + { + return true; + } + }; + // Custom struct CameraSave : public Base { diff --git a/Source/FileFormat/FileFormat/Warcraft/Parsers/Wdc3Parser.h b/Source/FileFormat/FileFormat/Warcraft/Parsers/Wdc3Parser.h index 9f9198b6..b9d22680 100644 --- a/Source/FileFormat/FileFormat/Warcraft/Parsers/Wdc3Parser.h +++ b/Source/FileFormat/FileFormat/Warcraft/Parsers/Wdc3Parser.h @@ -62,7 +62,7 @@ namespace DB2::WDC3 unpackedBits = *reinterpret_cast(&unpackedBitsSigned); } - T result = static_cast(unpackedBits); + T result = *reinterpret_cast(&unpackedBits); return result; } diff --git a/Source/Gameplay/Gameplay.lua b/Source/Gameplay/Gameplay.lua new file mode 100644 index 00000000..98882a0d --- /dev/null +++ b/Source/Gameplay/Gameplay.lua @@ -0,0 +1,18 @@ +local mod = Solution.Util.CreateModuleTable("Gameplay", { "base", "network" }) + +Solution.Util.CreateStaticLib(mod.Name, Solution.Projects.Current.BinDir, mod.Dependencies, function() + local defines = { "_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS", "_SILENCE_ALL_MS_EXT_DEPRECATION_WARNINGS" } + + Solution.Util.SetLanguage("C++") + Solution.Util.SetCppDialect(20) + + local files = Solution.Util.GetFilesForCpp(mod.Path) + Solution.Util.SetFiles(files) + Solution.Util.SetIncludes(mod.Path) + Solution.Util.SetDefines(defines) +end) + +Solution.Util.CreateDep(mod.NameLow, mod.Dependencies, function() + Solution.Util.SetIncludes(mod.Path) + Solution.Util.SetLinks(mod.Name) +end) \ No newline at end of file diff --git a/Source/Gameplay/Gameplay/Gameplay.h b/Source/Gameplay/Gameplay/Gameplay.h new file mode 100644 index 00000000..e69de29b diff --git a/Source/Gameplay/Gameplay/Network/Define.h b/Source/Gameplay/Gameplay/Network/Define.h new file mode 100644 index 00000000..e9a56829 --- /dev/null +++ b/Source/Gameplay/Gameplay/Network/Define.h @@ -0,0 +1,10 @@ +#include "Base/Types.h" + +namespace Network +{ + enum class ConnectionStatus : u8 + { + None, + Connected + }; +} \ No newline at end of file diff --git a/Source/Gameplay/Gameplay/Network/GameMessageRouter.cpp b/Source/Gameplay/Gameplay/Network/GameMessageRouter.cpp new file mode 100644 index 00000000..75c61a8c --- /dev/null +++ b/Source/Gameplay/Gameplay/Network/GameMessageRouter.cpp @@ -0,0 +1,33 @@ +#include "GameMessageRouter.h" + +#include "Base/Memory/Bytebuffer.h" + +namespace Network +{ + GameMessageRouter::GameMessageRouter() { } + + void GameMessageRouter::SetMessageHandler(GameOpcode opcode, GameMessageHandler&& handler) + { + _handlers[static_cast(opcode)] = std::move(handler); + } + + bool GameMessageRouter::CallHandler(SocketID socketID, std::shared_ptr& packet) + { + Network::PacketHeader header; + if (!packet->Get(header)) + return false; + + GameOpcode opcode = static_cast(header.opcode); + if (opcode == GameOpcode::Invalid || opcode >= GameOpcode::Count) + return false; + + const GameMessageHandler& opcodeHandler = _handlers[static_cast(header.opcode)]; + bool isSmallerThanMinSize = header.size < opcodeHandler.minSize; + bool isLargerThanMaxSize = header.size > opcodeHandler.maxSize && opcodeHandler.maxSize != -1; + + if (!opcodeHandler.handler || isSmallerThanMinSize || isLargerThanMaxSize) + return false; + + return opcodeHandler.handler(socketID, packet); + } +} \ No newline at end of file diff --git a/Source/Gameplay/Gameplay/Network/GameMessageRouter.h b/Source/Gameplay/Gameplay/Network/GameMessageRouter.h new file mode 100644 index 00000000..52bd3026 --- /dev/null +++ b/Source/Gameplay/Gameplay/Network/GameMessageRouter.h @@ -0,0 +1,41 @@ +#pragma once +#include "Define.h" +#include "Opcode.h" + +#include "Base/Types.h" +#include "Network/Define.h" + +#include + +#include + +class Bytebuffer; + +namespace Network +{ + typedef bool (*MessageHandlerFn)(SocketID, std::shared_ptr&); + + struct GameMessageHandler + { + GameMessageHandler() { } + GameMessageHandler(ConnectionStatus inStatus, u16 inSize, MessageHandlerFn inHandler) : status(inStatus), minSize(inSize), maxSize(inSize), handler(inHandler) { } + GameMessageHandler(ConnectionStatus inStatus, u16 inMinSize, i16 inMaxSize, MessageHandlerFn inHandler) : status(inStatus), minSize(inMinSize), maxSize(inMaxSize), handler(inHandler) { } + + ConnectionStatus status = ConnectionStatus::None; + u16 minSize = 0; + i16 maxSize = 0; + MessageHandlerFn handler = nullptr; + }; + + class GameMessageRouter + { + public: + GameMessageRouter(); + + void SetMessageHandler(GameOpcode opcode, GameMessageHandler&& handler); + bool CallHandler(SocketID socketID, std::shared_ptr& packet); + + private: + GameMessageHandler _handlers[static_cast(GameOpcode::Count)]; + }; +} \ No newline at end of file diff --git a/Source/Gameplay/Gameplay/Network/Opcode.h b/Source/Gameplay/Gameplay/Network/Opcode.h new file mode 100644 index 00000000..70cb7066 --- /dev/null +++ b/Source/Gameplay/Gameplay/Network/Opcode.h @@ -0,0 +1,34 @@ +#pragma once +#include "Network/Define.h" + +namespace Network +{ + enum class GameOpcode : OpcodeType + { + Invalid, + + Client_Connect, + Server_Connected, + + Client_SendCheatCommand, + Server_SendCheatCommandResult, + + Server_PlayerCreate, + Server_EntityCreate, + Server_EntityDestroy, + Server_EntityUpdate, + Shared_EntityMove, + Shared_EntityMoveStop, + Server_EntityResourcesUpdate, + Server_EntityDisplayInfoUpdate, + Shared_EntityTargetUpdate, + Server_EntityCastSpell, + + Client_LocalRequestSpellCast, + + Server_SendSpellCastResult, + Server_SendCombatEvent, + + Count + }; +} \ No newline at end of file diff --git a/Source/Modules.lua b/Source/Modules.lua index ae8aea5a..f6d65748 100644 --- a/Source/Modules.lua +++ b/Source/Modules.lua @@ -14,6 +14,7 @@ local modules = "Network/Network.lua", "FileFormat/FileFormat.lua", "Input/Input.lua", + "Gameplay/Gameplay.lua", "ShaderCooker/ShaderCooker.lua", "Renderer/Renderer.lua", } diff --git a/Source/Network/Network/Client.cpp b/Source/Network/Network/Client.cpp index aeffd7d8..73d5c58a 100644 --- a/Source/Network/Network/Client.cpp +++ b/Source/Network/Network/Client.cpp @@ -81,7 +81,7 @@ namespace Network return result; } - Socket::Result Client::Connect(std::string& hostname, u16 port) + Socket::Result Client::Connect(const char* hostname, u16 port) { if (_isInitialized == false) { @@ -103,6 +103,11 @@ namespace Network return result; } + Socket::Result Client::Connect(std::string& hostname, u16 port) + { + return Connect(hostname.c_str(), port); + } + Socket::Result Client::Close() { if (_isInitialized == false) diff --git a/Source/Network/Network/Client.h b/Source/Network/Network/Client.h index b37e3dfa..11746d5a 100644 --- a/Source/Network/Network/Client.h +++ b/Source/Network/Network/Client.h @@ -17,6 +17,7 @@ namespace Network Socket::Result Reinit(); Socket::Result Connect(u32 host, u16 port); + Socket::Result Connect(const char* hostname, u16 port); Socket::Result Connect(std::string& hostname, u16 port); Socket::Result Close(); Socket::Result Send(void* data, size_t size); diff --git a/Source/Network/Network/Define.h b/Source/Network/Network/Define.h index 5cf2350c..be475344 100644 --- a/Source/Network/Network/Define.h +++ b/Source/Network/Network/Define.h @@ -5,96 +5,7 @@ class Bytebuffer; namespace Network { - - enum class Opcode : u16 - { - INVALID, - CMSG_LOGON_CHALLENGE, - SMSG_LOGON_CHALLENGE, - CMSG_LOGON_HANDSHAKE, - SMSG_LOGON_HANDSHAKE, - CMSG_CONNECTED, - SMSG_CONNECTED, - MSG_REQUEST_ADDRESS, - SMSG_SEND_ADDRESS, - MSG_REQUEST_INTERNAL_SERVER_INFO, - SMSG_SEND_FULL_INTERNAL_SERVER_INFO, - SMSG_SEND_ADD_INTERNAL_SERVER_INFO, - SMSG_SEND_REMOVE_INTERNAL_SERVER_INFO, - - CMSG_REQUEST_REALMLIST, - SMSG_SEND_REALMLIST, - CMSG_SELECT_REALM, - SMSG_SEND_REALM_CONNECTION_INFO, - SMSG_REQUEST_REALM_CONNECTION_INFO, - - SMSG_PLAYER_CREATE, - SMSG_ENTITY_CREATE, - SMSG_ENTITY_DESTROY, - SMSG_ENTITY_UPDATE, - MSG_ENTITY_MOVE, - MSG_ENTITY_MOVE_HEARTBEAT, - MSG_ENTITY_MOVE_STOP, - CMSG_STORELOC, - SMSG_STORELOC, - CMSG_GOTO, - - SMSG_ENTITY_RESOURCES_UPDATE, - SMSG_ENTITY_ATTRIBUTES_UPDATE, - SMSG_ENTITY_RATINGS_UPDATE, - SMSG_ENTITY_DISPLAYINFO_UPDATE, - - MSG_ENTITY_TARGET_UPDATE, - - CMSG_REQUEST_SPELLCAST, - SMSG_SEND_SPELLCAST_RESULT, - SMSG_ENTITY_CAST_SPELL, - - SMSG_COMBAT_EVENT, - - CMSG_CHEAT_DAMAGE, - CMSG_CHEAT_KILL, - CMSG_CHEAT_RESURRECT, - CMSG_CHEAT_MORPH, - CMSG_CHEAT_CREATE_CHARACTER, - SMSG_CHEAT_CREATE_CHARACTER_RESULT, - CMSG_CHEAT_DELETE_CHARACTER, - SMSG_CHEAT_DELETE_CHARACTER_RESULT, - - MAX_COUNT - }; - - enum class ConnectionStatus : u8 - { - AUTH_NONE, - AUTH_CHALLENGE, - AUTH_HANDSHAKE, - AUTH_FAILED, - AUTH_SUCCESS, - CONNECTED, - }; - - enum class AddressType : u8 - { - AUTH, - REALM, - WORLD, - INSTANCE, - CHAT, - LOADBALANCE, - REGION, - COUNT, - INVALID - }; - - enum class RegionType : u8 - { - NA, - SA, - EU, - OCEANIC, - COUNT - }; + typedef u16 OpcodeType; struct ConnectionInfo { @@ -108,7 +19,7 @@ namespace Network struct PacketHeader { public: - Opcode opcode = Opcode::INVALID; + OpcodeType opcode = 0; u16 size = 0; }; diff --git a/Source/Network/Network/PacketHandler.cpp b/Source/Network/Network/PacketHandler.cpp deleted file mode 100644 index 5c8c9f1c..00000000 --- a/Source/Network/Network/PacketHandler.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "PacketHandler.h" -#include "Client.h" - -namespace Network -{ - PacketHandler::PacketHandler() { } - - void PacketHandler::SetMessageHandler(Opcode opcode, OpcodeHandler handler) - { - handlers[static_cast(opcode)] = handler; - } - - bool PacketHandler::CallHandler(SocketID socketID, std::shared_ptr& packet) - { - Network::PacketHeader header; - if (!packet->Get(header)) - return false; - - if (header.opcode <= Opcode::INVALID || header.opcode > Opcode::MAX_COUNT) - return false; - - const OpcodeHandler& opcodeHandler = handlers[static_cast(header.opcode)]; - - if (!opcodeHandler.handler || header.size < opcodeHandler.minSize || (header.size > opcodeHandler.maxSize && opcodeHandler.maxSize != -1)) - return false; - - return opcodeHandler.handler(socketID, packet); - } -} \ No newline at end of file diff --git a/Source/Network/Network/PacketHandler.h b/Source/Network/Network/PacketHandler.h deleted file mode 100644 index a5f59d7d..00000000 --- a/Source/Network/Network/PacketHandler.h +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once -#include "Define.h" - -#include "Base/Types.h" - -#include - -#include - -class Bytebuffer; - -namespace Network -{ - typedef bool (*MessageHandlerFn)(SocketID, std::shared_ptr&); - - struct OpcodeHandler - { - OpcodeHandler() { } - OpcodeHandler(ConnectionStatus inStatus, u16 inSize, MessageHandlerFn inHandler) : status(inStatus), minSize(inSize), maxSize(inSize), handler(inHandler) { } - OpcodeHandler(ConnectionStatus inStatus, u16 inMinSize, i16 inMaxSize, MessageHandlerFn inHandler) : status(inStatus), minSize(inMinSize), maxSize(inMaxSize), handler(inHandler) { } - - ConnectionStatus status = ConnectionStatus::AUTH_NONE; - u16 minSize = 0; - i16 maxSize = 0; - MessageHandlerFn handler = nullptr; - }; - - class PacketHandler - { - public: - PacketHandler(); - - void SetMessageHandler(Opcode opcode, OpcodeHandler handler); - bool CallHandler(SocketID socketID, std::shared_ptr& packet); - - private: - OpcodeHandler handlers[static_cast(Opcode::MAX_COUNT)]; - }; -} \ No newline at end of file diff --git a/Source/Network/Network/Server.cpp b/Source/Network/Network/Server.cpp index eb85d2a5..5a54dc33 100644 --- a/Source/Network/Network/Server.cpp +++ b/Source/Network/Network/Server.cpp @@ -237,8 +237,10 @@ namespace Network std::shared_ptr& buffer = connection.client->GetReadBuffer(); while (size_t activeSize = buffer->GetActiveSize()) { + static constexpr u8 PacketHeaderSize = sizeof(PacketHeader); + // We have received a partial header and need to read more - if (activeSize < sizeof(PacketHeader)) + if (activeSize < PacketHeaderSize) { buffer->Normalize(); break; @@ -246,19 +248,10 @@ namespace Network PacketHeader* header = reinterpret_cast(buffer->GetReadPointer()); - if (header->opcode == Opcode::INVALID || header->opcode > Opcode::MAX_COUNT) - { -#ifdef NC_DEBUG - NC_LOG_ERROR("Network : Received Invalid Opcode ({0}) from (SocketId : {1}, \"{2}:{3}\")", static_cast::type>(header->opcode), static_cast(socketID), connectionInfo.ipAddrStr, connectionInfo.port); -#endif // NC_DEBUG - CloseSocketID(connection.id); - break; - } - - if (header->size > DEFAULT_BUFFER_SIZE) + if (header->size > DEFAULT_BUFFER_SIZE - PacketHeaderSize) { #ifdef NC_DEBUG - NC_LOG_ERROR("Network : Received Invalid Opcode Size ({0} : {1}) from (SocketId : {2}, \"{3}:{4}\")", static_cast::type>(header->opcode), header->size, static_cast(socketID), connectionInfo.ipAddrStr, connectionInfo.port); + NC_LOG_ERROR("Network : Received Invalid Opcode Size ({0} : {1}) from (SocketId : {2}, \"{3}:{4}\")", header->opcode, header->size, static_cast(socketID), connectionInfo.ipAddrStr, connectionInfo.port); #endif // NC_DEBUG CloseSocketID(connection.id); break; diff --git a/Source/Network/Network/Socket.cpp b/Source/Network/Network/Socket.cpp index d6a0f25c..c7885eae 100644 --- a/Source/Network/Network/Socket.cpp +++ b/Source/Network/Network/Socket.cpp @@ -126,36 +126,31 @@ namespace Network return result; } - Socket::Result Socket::Connect(std::string& hostName, u16 port) + Socket::Result Socket::Connect(const char* hostName, u16 port) { if (_type != Type::CLIENT) return Result::ERROR_CONNECT_MODE_SERVER; Result result = Result::SUCCESS; -#ifdef _WIN32 - hostent* localHost = gethostbyname(hostName.c_str()); - char* localIP = inet_ntoa(*(struct in_addr*)*localHost->h_addr_list); - - sockaddr_in serverAddr { }; - serverAddr.sin_family = AF_INET; - serverAddr.sin_addr.s_addr = inet_addr(localIP); - serverAddr.sin_port = htons(port); + u32 host = 0; - i32 code = connect(_socket, (SOCKADDR*)&serverAddr, sizeof(serverAddr)); - if (code == SOCKET_ERROR) - { - result = GetResultFromError(WSAGetLastError()); - } +#ifdef _WIN32 + hostent* localHost = gethostbyname(hostName); + if (!localHost) + return Result::ERROR_CLIENT_FAILED_TO_RESOLVE_HOSTNAME; - _connectInfo.ipAddr = serverAddr.sin_addr.S_un.S_addr; - _connectInfo.ipAddrStr = hostName; - _connectInfo.port = port; + char* localIP = inet_ntoa(*(struct in_addr*)*localHost->h_addr_list); + if (!localIP) + return Result::ERROR_CLIENT_FAILED_TO_CONVERT_HOSTNAME_TO_ADDRESS; -#elif __linux__ - return Result::ERROR_NOT_IMPLEMENTED; + host = inet_addr(localIP); #endif + return Connect(host, port); + } - return result; + Socket::Result Socket::Connect(std::string& hostName, u16 port) + { + return Connect(hostName.c_str(), port); } Socket::Result Socket::Bind(std::string& hostName, u16 port) diff --git a/Source/Network/Network/Socket.h b/Source/Network/Network/Socket.h index 28a1700e..f853fa1b 100644 --- a/Source/Network/Network/Socket.h +++ b/Source/Network/Network/Socket.h @@ -69,7 +69,9 @@ namespace Network ERROR_CLIENT_ALREADY_INITIALIZED, ERROR_CLIENT_UNCONNECTED, ERROR_CLIENT_ALREADY_CONNECTED, - ERROR_CLIENT_FAILED_TO_CLOSE + ERROR_CLIENT_FAILED_TO_CLOSE, + ERROR_CLIENT_FAILED_TO_RESOLVE_HOSTNAME, + ERROR_CLIENT_FAILED_TO_CONVERT_HOSTNAME_TO_ADDRESS, }; Socket(); @@ -79,6 +81,7 @@ namespace Network static Socket::Result Create(Type type, Mode mode, std::shared_ptr& out); Result Connect(u32 host, u16 port); + Result Connect(const char* hostName, u16 port); Result Connect(std::string& hostName, u16 port); Result Bind(std::string& hostName, u16 port); Result Accept(std::shared_ptr& connection); diff --git a/Source/Renderer/Renderer/Window.cpp b/Source/Renderer/Renderer/Window.cpp index 2bd0431e..910c4385 100644 --- a/Source/Renderer/Renderer/Window.cpp +++ b/Source/Renderer/Renderer/Window.cpp @@ -41,10 +41,10 @@ namespace Novus glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); -#if NC_Debug - _window = glfwCreateWindow(width, height, "CNovusCore (DEBUG)", NULL, NULL); +#if NC_DEBUG + _window = glfwCreateWindow(width, height, "NovusEngine (DEBUG)", NULL, NULL); #else - _window = glfwCreateWindow(width, height, "CNovusCore", NULL, NULL); + _window = glfwCreateWindow(width, height, "NovusEngine", NULL, NULL); #endif if (!_window)