From 5dcdce8b5f2d71445461df19aae510f42a388d38 Mon Sep 17 00:00:00 2001 From: BurntRanch <69512353+BurntRanch@users.noreply.github.com> Date: Mon, 6 Jan 2025 16:23:00 +0300 Subject: [PATCH] client data! --- include/engine.hpp | 10 ++-------- include/util.hpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/engine.cpp | 44 ++++---------------------------------------- 3 files changed, 46 insertions(+), 48 deletions(-) diff --git a/include/engine.hpp b/include/engine.hpp index fb66567..e625cbb 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -536,6 +536,8 @@ class Engine { void ProcessNetworkEvents(std::vector *networkingEvents); + bool IsConnectedToGameServer(); + /* Sends a packet to the server that gets handled at the program level, this could include inputs n such. */ void SendRequestToServer(std::vector &data); @@ -628,14 +630,6 @@ class Engine { void DeserializeClientRequest(std::vector &serializedClientRequest, Networking_ClientRequest &dest); - /* Deserialize to dest */ - template - void Deserialize(std::vector &object, T &dest); - - /* Serialize and append to dest. */ - template - void Serialize(T object, std::vector &dest); - void CheckButtonClicks(SDL_Event *event); void InitNetworkingThread(NetworkingThreadStatus status); diff --git a/include/util.hpp b/include/util.hpp index e7e42a0..18449cf 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -39,4 +39,44 @@ glm::vec2 getScale(rapidxml::xml_node *propertiesNode); float getZDepth(rapidxml::xml_node *propertiesNode, float depthDefault = 1.0f); bool getVisible(rapidxml::xml_node *propertiesNode); +template +void Deserialize(std::vector &object, T &dest) { + if constexpr (std::is_same::value) { + UTILASSERT(object.size() >= sizeof(size_t)); /* minimum size */ + + size_t stringSize = *reinterpret_cast(object.data()); + object.erase(object.begin(), object.begin() + sizeof(size_t)); + + UTILASSERT(object.size() >= stringSize); /* Each char is 1 byte, this is valid. */ + + char *string = reinterpret_cast(object.data()); + + dest = std::string(string, stringSize); + + object.erase(object.begin(), object.begin() + stringSize); + } else { + UTILASSERT(object.size() >= sizeof(T)); + + dest = *reinterpret_cast(object.data()); + + object.erase(object.begin(), object.begin() + sizeof(T)); + } +} + +template +void Serialize(T object, std::vector &dest) { + if constexpr (std::is_same::value) { + Serialize(object.size(), dest); + + for (char &c : object) { + Serialize(c, dest); + } + } else { + for (size_t i = 0; i < sizeof(T); i++) { + std::byte *byte = reinterpret_cast(&object) + i; + dest.push_back(*byte); + } + } +} + #endif diff --git a/src/engine.cpp b/src/engine.cpp index 7fb3868..9a9becb 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -3552,6 +3552,10 @@ void Engine::ProcessNetworkEvents(std::vector *networkingEvent } } +bool Engine::IsConnectedToGameServer() { + return m_NetworkingThreadStates[0].netConnections.size() == 1; +} + void Engine::SendRequestToServer(std::vector &data) { NetworkingThreadState &state = m_NetworkingThreadStates[0]; @@ -3912,44 +3916,4 @@ void Engine::DeserializeClientRequest(std::vector &serializedClientRe Deserialize(serializedClientRequest, dest.requestType); } -template -void Engine::Deserialize(std::vector &object, T &dest) { - if constexpr (std::is_same::value) { - UTILASSERT(object.size() >= sizeof(size_t)); /* minimum size */ - - size_t stringSize = *reinterpret_cast(object.data()); - object.erase(object.begin(), object.begin() + sizeof(size_t)); - - UTILASSERT(object.size() >= stringSize); /* Each char is 1 byte, this is valid. */ - - char *string = reinterpret_cast(object.data()); - - dest = std::string(string, stringSize); - - object.erase(object.begin(), object.begin() + stringSize); - } else { - UTILASSERT(object.size() >= sizeof(T)); - - dest = *reinterpret_cast(object.data()); - - object.erase(object.begin(), object.begin() + sizeof(T)); - } -} - -template -void Engine::Serialize(T object, std::vector &dest) { - if constexpr (std::is_same::value) { - Serialize(object.size(), dest); - - for (char &c : object) { - Serialize(c, dest); - } - } else { - for (size_t i = 0; i < sizeof(T); i++) { - std::byte *byte = reinterpret_cast(&object) + i; - dest.push_back(*byte); - } - } -} - Engine *Engine::m_CallbackInstance = nullptr;