From 44de4681527cd1c12a3125885251d15c443dbc35 Mon Sep 17 00:00:00 2001 From: vittorioromeo Date: Tue, 6 Aug 2024 16:55:05 +0200 Subject: [PATCH] adapt to newest sfml --- CMakeLists.txt | 4 +- build/r_gdb.sh | 2 +- include/SSVOpenHexagon/Components/CWall.hpp | 4 +- include/SSVOpenHexagon/Core/HexagonGame.hpp | 2 +- include/SSVOpenHexagon/Core/HexagonServer.hpp | 18 +-- include/SSVOpenHexagon/Core/MenuGame.hpp | 2 +- include/SSVOpenHexagon/Global/PCH.hpp | 43 ++++++- src/OHServerControl/main.cpp | 2 +- src/SSVOpenHexagon/Core/HGUpdate.cpp | 9 +- src/SSVOpenHexagon/Core/HexagonClient.cpp | 8 +- src/SSVOpenHexagon/Core/HexagonServer.cpp | 106 +++++++++++++++--- src/SSVOpenHexagon/Core/Joystick.cpp | 2 +- src/SSVOpenHexagon/Core/MenuGame.cpp | 3 +- src/SSVOpenHexagon/Core/main.cpp | 9 +- src/SSVOpenHexagon/Global/AssetStorage.cpp | 2 +- src/SSVOpenHexagon/Global/Config.cpp | 6 +- src/SSVOpenHexagon/Global/Imgui.cpp | 2 +- test/ReplayExecution.t.cpp | 21 ++-- test/ReplayExecutionBenchmark.t.cpp | 39 ++++--- 19 files changed, 202 insertions(+), 82 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f88bf177..533a99d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -97,7 +97,7 @@ set(SFML_ENABLE_PCH true) CPMAddPackage( NAME SFML GITHUB_REPOSITORY vittorioromeo/SFML - GIT_TAG 0608821ff4e8de462d5aa44343df42d989f8102a + GIT_TAG dd0cc60de5f07f4180821db9b3f89fbc15cdf03f ) set_target_properties(sfml-system PROPERTIES UNITY_BUILD ON) @@ -201,7 +201,7 @@ if(NOT SSVOH_ANDROID) CPMAddPackage( NAME imgui-sfml GITHUB_REPOSITORY vittorioromeo/imgui-sfml - GIT_TAG 32b2c1588b78b2946f8c7c61420fbd8a45ae4e9c + GIT_TAG d65ebaba593dbda0353aab99ace9b895788f727d ) set_target_properties(ImGui-SFML PROPERTIES UNITY_BUILD ON) diff --git a/build/r_gdb.sh b/build/r_gdb.sh index 2724535f..d72a27d7 100644 --- a/build/r_gdb.sh +++ b/build/r_gdb.sh @@ -1,2 +1,2 @@ #!/bin/bash -(cp ./SSVOpenHexagon-Console.exe ../_RELEASE ; cd ../_RELEASE && gdb -- ./SSVOpenHexagon-Console.exe "$@") +(cp ./SSVOpenHexagon.exe ../_RELEASE ; cd ../_RELEASE && gdb -- ./SSVOpenHexagon.exe "$@") diff --git a/include/SSVOpenHexagon/Components/CWall.hpp b/include/SSVOpenHexagon/Components/CWall.hpp index cba6ef7a..09729b92 100644 --- a/include/SSVOpenHexagon/Components/CWall.hpp +++ b/include/SSVOpenHexagon/Components/CWall.hpp @@ -9,6 +9,8 @@ #include "SSVOpenHexagon/Utils/FastVertexVector.hpp" #include +#include +#include #include #include @@ -63,7 +65,7 @@ class CWall const float rad = getCurveRadians(ft); moveVertexAlongCurveImpl( - vertex, centerPos, std::sin(rad), std::cos(rad)); + vertex, centerPos, sf::base::sin(rad), sf::base::cos(rad)); } void draw(sf::Color color, Utils::FastVertexVectorTris& wallQuads); diff --git a/include/SSVOpenHexagon/Core/HexagonGame.hpp b/include/SSVOpenHexagon/Core/HexagonGame.hpp index 60260392..1faa1a7e 100644 --- a/include/SSVOpenHexagon/Core/HexagonGame.hpp +++ b/include/SSVOpenHexagon/Core/HexagonGame.hpp @@ -33,7 +33,7 @@ #include #include -#include +#include #include #include diff --git a/include/SSVOpenHexagon/Core/HexagonServer.hpp b/include/SSVOpenHexagon/Core/HexagonServer.hpp index 52577996..62089a11 100644 --- a/include/SSVOpenHexagon/Core/HexagonServer.hpp +++ b/include/SSVOpenHexagon/Core/HexagonServer.hpp @@ -93,20 +93,8 @@ class HexagonServer std::optional _gameStatus; - explicit ConnectedClient(const Utils::SCTimePoint lastActivity) - : _socket{}, - _lastActivity{lastActivity}, - _consecutiveFailures{0}, - _mustDisconnect{false}, - _clientPublicKey{}, - _loginData{}, - _state{State::Disconnected} - {} - - ~ConnectedClient() - { - _socket.disconnect(); - } + explicit ConnectedClient(const Utils::SCTimePoint lastActivity); + ~ConnectedClient(); }; std::list _connectedClients; @@ -156,7 +144,7 @@ class HexagonServer const ProtocolVersion& protocolVersion, const GameVersion& gameVersion, const std::vector& supportedLevelValidators); - void kickAndRemoveClient(ConnectedClient& c); + [[nodiscard]] bool kickAndRemoveClient(ConnectedClient& c); void run(); void runIteration(); diff --git a/include/SSVOpenHexagon/Core/MenuGame.hpp b/include/SSVOpenHexagon/Core/MenuGame.hpp index 17489247..17356b32 100644 --- a/include/SSVOpenHexagon/Core/MenuGame.hpp +++ b/include/SSVOpenHexagon/Core/MenuGame.hpp @@ -26,7 +26,7 @@ #include #include -#include +#include #include diff --git a/include/SSVOpenHexagon/Global/PCH.hpp b/include/SSVOpenHexagon/Global/PCH.hpp index d719f10a..f952f8c3 100644 --- a/include/SSVOpenHexagon/Global/PCH.hpp +++ b/include/SSVOpenHexagon/Global/PCH.hpp @@ -129,11 +129,44 @@ // SFML // ---------------------------------------------------------------------------- -#include -#include -#include -#include -#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include // // diff --git a/src/OHServerControl/main.cpp b/src/OHServerControl/main.cpp index 7efae7d3..bb6ef1cc 100644 --- a/src/OHServerControl/main.cpp +++ b/src/OHServerControl/main.cpp @@ -31,7 +31,7 @@ int main(int argc, char* argv[]) std::string stringBuf; sf::Packet packet; - sf::UdpSocket controlSocket; + sf::UdpSocket controlSocket(true /* isBlocking */); const auto sendToServer = [&] { diff --git a/src/SSVOpenHexagon/Core/HGUpdate.cpp b/src/SSVOpenHexagon/Core/HGUpdate.cpp index 70d68f9b..32d63e93 100644 --- a/src/SSVOpenHexagon/Core/HGUpdate.cpp +++ b/src/SSVOpenHexagon/Core/HGUpdate.cpp @@ -1029,8 +1029,8 @@ void HexagonGame::updateTrailParticles(float mFT) p.sprite.setScale(p.sprite.getScale() * 0.98f); - p.sprite.setPosition( - sf::Vector2f::fromAngle(status.radius + 2.4f, sf::radians(p.angle))); + p.sprite.setPosition(sf::Vector2f::fromAngle( + status.radius + 2.4f, sf::radians(p.angle))); } if (player.hasChangedAngle()) @@ -1064,8 +1064,9 @@ void HexagonGame::updateSwapParticles(float mFT) c.a = alpha; p.sprite.setColor(c); - p.velocity = sf::Vector2f::fromAngle(ssvu::getRndR(0.1f, 10.f) * speedMult, - sf::radians(si.angle + ssvu::getRndR(-expand, expand))); + p.velocity = + sf::Vector2f::fromAngle(ssvu::getRndR(0.1f, 10.f) * speedMult, + sf::radians(si.angle + ssvu::getRndR(-expand, expand))); return p; }; diff --git a/src/SSVOpenHexagon/Core/HexagonClient.cpp b/src/SSVOpenHexagon/Core/HexagonClient.cpp index 4bd0bb85..aa5d34f0 100644 --- a/src/SSVOpenHexagon/Core/HexagonClient.cpp +++ b/src/SSVOpenHexagon/Core/HexagonClient.cpp @@ -440,7 +440,7 @@ HexagonClient::HexagonClient(Steam::steam_manager& steamManager, _ticketSteamID{}, _serverIp{serverIp}, _serverPort{serverPort}, - _socket{}, + _socket{true /* isBlocking */}, _socketConnected{false}, _packetBuffer{}, _errorOss{}, @@ -497,7 +497,11 @@ void HexagonClient::disconnect() (void)sendDisconnect(); } - _socket.disconnect(); + if (!_socket.disconnect()) + { + SSVOH_CLOG << "Failure disconnecting client socket\n"; + } + _socketConnected = false; SSVOH_CLOG << "Client disconnected\n"; diff --git a/src/SSVOpenHexagon/Core/HexagonServer.cpp b/src/SSVOpenHexagon/Core/HexagonServer.cpp index 54409944..74a5f5dc 100644 --- a/src/SSVOpenHexagon/Core/HexagonServer.cpp +++ b/src/SSVOpenHexagon/Core/HexagonServer.cpp @@ -28,7 +28,11 @@ #include -#include +#include +#include +#include +#include +#include #include @@ -62,6 +66,25 @@ static auto& slog(const char* funcName) namespace hg { +HexagonServer::ConnectedClient::ConnectedClient( + const Utils::SCTimePoint lastActivity) + : _socket{true /* isBlocking */}, // TODO (P0): should this be blocking???? + _lastActivity{lastActivity}, + _consecutiveFailures{0}, + _mustDisconnect{false}, + _clientPublicKey{}, + _loginData{}, + _state{State::Disconnected} +{} + +HexagonServer::ConnectedClient::~ConnectedClient() +{ + if (!_socket.disconnect()) + { + SSVOH_SLOG_ERROR << "Failed to disconnect connected client socket\n"; + } +} + template [[nodiscard]] bool HexagonServer::fail(const Ts&... xs) { @@ -93,7 +116,11 @@ template return fail("Failure binding UDP control socket"); } - _socketSelector.add(_controlSocket); + if (!_socketSelector.add(_controlSocket)) + { + return fail("Failed to add UDP control to socket selector"); + } + return true; } @@ -115,7 +142,11 @@ template { SSVOH_SLOG << "Initializing socket selector...\n"; - _socketSelector.add(_listener); + if (!_socketSelector.add(_listener)) + { + return fail("Failed to add listener to socket selector"); + } + return true; } @@ -265,7 +296,7 @@ template ); } -void HexagonServer::kickAndRemoveClient(ConnectedClient& c) +bool HexagonServer::kickAndRemoveClient(ConnectedClient& c) { (void)sendKick(c); @@ -274,7 +305,12 @@ void HexagonServer::kickAndRemoveClient(ConnectedClient& c) Database::removeAllLoginTokensForUser(c._loginData->_userId); } - _socketSelector.remove(c._socket); + if (!_socketSelector.remove(c._socket)) + { + return fail("Failed to remove client socket from socket selector"); + } + + return true; } void HexagonServer::run() @@ -460,7 +496,11 @@ bool HexagonServer::runIteration_TryAcceptingNewClient() // Add the new client to the selector so that we will be notified when he // sends something - _socketSelector.add(potentialSocket); + if (!_socketSelector.add(potentialSocket)) + { + return fail("Failed to add potential client socket to socket selector"); + } + return true; } @@ -484,6 +524,7 @@ void HexagonServer::runIteration_LoopOverSockets() _packetBuffer.clear(); // TODO (P1): potential hanging spot? + // TODO (P0): SHOULD WE SET THE SOCKET TO NONBLOCKING HERE??? if (clientSocket.receive(_packetBuffer) == sf::Socket::Status::Done) { SSVOH_SLOG_VERBOSE << "Successfully received data from client '" @@ -511,7 +552,12 @@ void HexagonServer::runIteration_LoopOverSockets() SSVOH_SLOG << "Too many consecutive failures for client '" << clientAddr << "', removing from list\n"; - kickAndRemoveClient(connectedClient); + if (!kickAndRemoveClient(connectedClient)) + { + SSVOH_SLOG + << "Failed kicking client after max consecutive failures\n"; + } + it = _connectedClients.erase(it); } } @@ -534,7 +580,11 @@ void HexagonServer::runIteration_PurgeClients() SSVOH_SLOG << "Client '" << clientAddr << "' disconnected, removing from list\n"; - kickAndRemoveClient(connectedClient); + if (!kickAndRemoveClient(connectedClient)) + { + SSVOH_SLOG << "Failed kicking client after must disconnect\n"; + } + it = _connectedClients.erase(it); continue; } @@ -544,7 +594,11 @@ void HexagonServer::runIteration_PurgeClients() SSVOH_SLOG << "Client '" << clientAddr << "' timed out, removing from list\n"; - kickAndRemoveClient(connectedClient); + if (!kickAndRemoveClient(connectedClient)) + { + SSVOH_SLOG << "Failed kicking client after inactivity\n"; + } + it = _connectedClients.erase(it); continue; } @@ -594,7 +648,11 @@ void HexagonServer::runIteration_PurgeTokens() SSVOH_SLOG << "Kicking stale token client '" << clientAddr << "'\n"; - kickAndRemoveClient(c); + if (!kickAndRemoveClient(c)) + { + SSVOH_SLOG << "Failed kicking client with stale token\n"; + } + it = _connectedClients.erase(it); } } @@ -1337,7 +1395,8 @@ HexagonServer::HexagonServer(HGAssets& assets, HexagonGame& hexagonGame, _serverIp{serverIp}, _serverPort{serverPort}, _serverControlPort{serverControlPort}, - _listener{}, + _controlSocket{true /* isBlocking */}, + _listener{true /* isBlocking */}, _socketSelector{}, _running{true}, _verbose{false}, @@ -1390,7 +1449,12 @@ HexagonServer::HexagonServer(HGAssets& assets, HexagonGame& hexagonGame, [](int s) { std::printf("Caught signal %d\n", s); - globalListener.close(); + + if (!globalListener.close()) + { + std::printf("Failed closing global listener\n"); + } + globalRunning = false; }); } @@ -1421,12 +1485,24 @@ HexagonServer::~HexagonServer() connectedClient._socket.setBlocking(true); (void)sendKick(connectedClient); - connectedClient._socket.disconnect(); + + if (!connectedClient._socket.disconnect()) + { + SSVOH_SLOG << "Failed to disconnect connected client socket during " + "shutdown\n"; + } } _socketSelector.clear(); - _listener.close(); - _controlSocket.unbind(); + if (!_listener.close()) + { + SSVOH_SLOG << "Failed to close listener during shutdown\n"; + } + + if (!_controlSocket.unbind()) + { + SSVOH_SLOG << "Failed to unbing control socket during shutdown\n"; + } } } // namespace hg diff --git a/src/SSVOpenHexagon/Core/Joystick.cpp b/src/SSVOpenHexagon/Core/Joystick.cpp index 06a06683..d7d2bce2 100644 --- a/src/SSVOpenHexagon/Core/Joystick.cpp +++ b/src/SSVOpenHexagon/Core/Joystick.cpp @@ -6,7 +6,7 @@ #include "SSVOpenHexagon/Utils/Casts.hpp" -#include +#include #include #include diff --git a/src/SSVOpenHexagon/Core/MenuGame.cpp b/src/SSVOpenHexagon/Core/MenuGame.cpp index 91b80838..a922916a 100644 --- a/src/SSVOpenHexagon/Core/MenuGame.cpp +++ b/src/SSVOpenHexagon/Core/MenuGame.cpp @@ -54,6 +54,7 @@ #include #include +#include #include #include @@ -1325,7 +1326,7 @@ void MenuGame::initMenus() // Organize available resolutions based on their aspect ratio. int ratio; - for (const auto& vm : sf::VideoMode::getFullscreenModes()) + for (const auto& vm : sf::VideoModeUtils::getFullscreenModes()) { if (vm.bitsPerPixel == 32) { diff --git a/src/SSVOpenHexagon/Core/main.cpp b/src/SSVOpenHexagon/Core/main.cpp index b9e1c6d3..c10f5179 100644 --- a/src/SSVOpenHexagon/Core/main.cpp +++ b/src/SSVOpenHexagon/Core/main.cpp @@ -31,10 +31,13 @@ #include +#include +#include + #include #include -#include +#include #include @@ -233,7 +236,7 @@ getFirstCompressedReplayFilenameFromArgs(const std::vector& args) hg::HexagonServer hs{ assets, // hg, // - sf::IpAddress::resolve(hg::Config::getServerIp()).value(), // + sf::IpAddressUtils::resolve(hg::Config::getServerIp()).value(), // hg::Config::getServerPort(), // hg::Config::getServerControlPort(), // hg::Utils::toUnorderedSet(hg::Config::getServerLevelWhitelist()) // @@ -421,7 +424,7 @@ getFirstCompressedReplayFilenameFromArgs(const std::vector& args) // Initialize hexagon client // TODO (P0): handle `resolve` errors hg::HexagonClient hc{steamManager, - sf::IpAddress::resolve(hg::Config::getServerIp()).value(), + sf::IpAddressUtils::resolve(hg::Config::getServerIp()).value(), hg::Config::getServerPort()}; // diff --git a/src/SSVOpenHexagon/Global/AssetStorage.cpp b/src/SSVOpenHexagon/Global/AssetStorage.cpp index c254e828..e54a6787 100644 --- a/src/SSVOpenHexagon/Global/AssetStorage.cpp +++ b/src/SSVOpenHexagon/Global/AssetStorage.cpp @@ -16,7 +16,7 @@ #include -#include +#include #include diff --git a/src/SSVOpenHexagon/Global/Config.cpp b/src/SSVOpenHexagon/Global/Config.cpp index fcc13e50..b2690a9a 100644 --- a/src/SSVOpenHexagon/Global/Config.cpp +++ b/src/SSVOpenHexagon/Global/Config.cpp @@ -17,7 +17,9 @@ #include #include +#include #include +#include #include @@ -477,14 +479,14 @@ std::string uneligibilityReason; static void applyAutoWindowedResolution() { - auto d = sf::VideoMode::getDesktopMode(); + auto d = sf::VideoModeUtils::getDesktopMode(); windowedWidth() = d.size.x; windowedHeight() = d.size.y; } static void applyAutoFullscreenResolution() { - auto d = sf::VideoMode::getDesktopMode(); + auto d = sf::VideoModeUtils::getDesktopMode(); fullscreenWidth() = d.size.x; fullscreenHeight() = d.size.y; } diff --git a/src/SSVOpenHexagon/Global/Imgui.cpp b/src/SSVOpenHexagon/Global/Imgui.cpp index 8b0053de..8ebf3b3b 100644 --- a/src/SSVOpenHexagon/Global/Imgui.cpp +++ b/src/SSVOpenHexagon/Global/Imgui.cpp @@ -13,7 +13,7 @@ #include #include -#include +#include #include namespace hg::Imgui { diff --git a/test/ReplayExecution.t.cpp b/test/ReplayExecution.t.cpp index cc437699..39a4c1bb 100644 --- a/test/ReplayExecution.t.cpp +++ b/test/ReplayExecution.t.cpp @@ -10,6 +10,8 @@ #include "SSVOpenHexagon/Core/HexagonGame.hpp" +#include + #include "TestUtils.hpp" #include @@ -35,15 +37,18 @@ try hg::Config::loadConfig({}); - hg::HGAssets assets{nullptr /* steamManager */, true /* headless */}; + hg::HGAssets assets{nullptr /* graphicsContext */, + nullptr /* steamManager */, true /* headless */}; hg::ProfileData fakeProfile{hg::GAME_VERSION, "testProfile", {}, {}}; assets.addLocalProfile(std::move(fakeProfile)); assets.pSetCurrent("testProfile"); - const auto doTest = [&](int i, bool differentHG, ssvs::GameWindow* gw) + const auto doTest = [&](int i, bool differentHG, sf::GraphicsContext* gc, + ssvs::GameWindow* gw) { hg::HexagonGame hg{ + gc, // nullptr /* steamManager */, // nullptr /* discordManager */, // assets, // @@ -83,6 +88,7 @@ try if (differentHG) { hg::HexagonGame hg2{ + gc, // nullptr /* steamManager */, // nullptr /* discordManager */, // assets, // @@ -110,16 +116,17 @@ try for (int i = 0; i < 25; ++i) { - doTest(i, false, nullptr); - doTest(i, true, nullptr); + doTest(i, false, nullptr, nullptr); + doTest(i, true, nullptr, nullptr); } #ifndef SSVOH_HEADLESS_TESTS - ssvs::GameWindow gw; + sf::GraphicsContext gc; + ssvs::GameWindow gw(gc); for (int i = 0; i < 25; ++i) { - doTest(i, false, &gw); - doTest(i, true, &gw); + doTest(i, false, &gc, &gw); + doTest(i, true, &gc, &gw); } #endif diff --git a/test/ReplayExecutionBenchmark.t.cpp b/test/ReplayExecutionBenchmark.t.cpp index 9f7f29bf..53baafdf 100644 --- a/test/ReplayExecutionBenchmark.t.cpp +++ b/test/ReplayExecutionBenchmark.t.cpp @@ -36,7 +36,8 @@ try hg::Config::loadConfig({}); - hg::HGAssets assets{nullptr /* steamManager */, true /* headless */}; + hg::HGAssets assets{nullptr /* graphicsContext */, + nullptr /* steamManager */, true /* headless */}; hg::ProfileData fakeProfile{hg::GAME_VERSION, "testProfile", {}, {}}; assets.addLocalProfile(std::move(fakeProfile)); @@ -45,15 +46,16 @@ try const auto doTest = [&](int i, bool differentHG, ssvs::GameWindow* gw) { hg::HexagonGame hg{ - nullptr /* steamManager */, // - nullptr /* discordManager */, // - assets, // - nullptr /* audio */, // - gw, // - nullptr /* client */ // + nullptr /* graphicsContext */, // + nullptr /* steamManager */, // + nullptr /* discordManager */, // + assets, // + nullptr /* audio */, // + gw, // + nullptr /* client */ // }; - if(getRndBool()) + if (getRndBool()) { hg.executeRandomInputs = true; } @@ -81,15 +83,16 @@ try TEST_ASSERT(rf.has_value()); std::optional score2; - if(differentHG) + if (differentHG) { hg::HexagonGame hg2{ - nullptr /* steamManager */, // - nullptr /* discordManager */, // - assets, // - nullptr /* audio */, // - nullptr /* window */, // - nullptr /* client */ // + nullptr /* graphicsContext */, // + nullptr /* steamManager */, // + nullptr /* discordManager */, // + assets, // + nullptr /* audio */, // + nullptr /* window */, // + nullptr /* client */ // }; score2 = hg2.runReplayUntilDeathAndGetScore( @@ -109,7 +112,7 @@ try TEST_ASSERT_EQ(score, replayPlayedTimeSeconds); }; - for(int i = 0; i < 100; ++i) + for (int i = 0; i < 100; ++i) { doTest(i, false, nullptr); doTest(i, true, nullptr); @@ -117,11 +120,11 @@ try return 0; } -catch(const std::runtime_error& e) +catch (const std::runtime_error& e) { std::cerr << "EXCEPTION: " << e.what() << std::endl; } -catch(...) +catch (...) { std::cerr << "EXCEPTION: unknown" << std::endl; }