From db587f13c68bba1f06d47e922029a812b4e2ece7 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Wed, 18 Mar 2020 02:02:47 +0100 Subject: [PATCH] Singleplayer server will now use a random available port instead of 4242. --- source/client/graphics/TextureAtlas.cpp | 2 +- source/client/states/TitleScreenState.cpp | 3 ++- source/server/core/ServerApplication.cpp | 4 ++-- source/server/core/ServerApplication.hpp | 1 + source/server/network/Server.cpp | 4 +++- source/server/network/Server.hpp | 4 ++++ 6 files changed, 13 insertions(+), 5 deletions(-) diff --git a/source/client/graphics/TextureAtlas.cpp b/source/client/graphics/TextureAtlas.cpp index ca78464f6..ac951ffd8 100644 --- a/source/client/graphics/TextureAtlas.cpp +++ b/source/client/graphics/TextureAtlas.cpp @@ -107,7 +107,7 @@ void TextureAtlas::packTextures() { } void TextureAtlas::loadFromRegistry(const std::string &texturePack) { - if (!gk::Filesystem::fileExists("texturepacks/" + texturePack)) + if (!texturePack.empty() && !gk::Filesystem::fileExists("texturepacks/" + texturePack)) throw EXCEPTION("Texture pack '" + texturePack +"' doesn't exist"); if (texturePack.empty()) diff --git a/source/client/states/TitleScreenState.cpp b/source/client/states/TitleScreenState.cpp index 021a69332..15fa3cb01 100644 --- a/source/client/states/TitleScreenState.cpp +++ b/source/client/states/TitleScreenState.cpp @@ -106,7 +106,7 @@ void TitleScreenState::startSingleplayer(bool showLoadingState) { m_thread = std::thread([this] () { ServerApplication app{*m_eventHandler}; app.setSingleplayer(true); - app.setPort(m_port); + app.setPort(0); app.run(); }); @@ -129,6 +129,7 @@ void TitleScreenState::onGuiScaleChanged(const GuiScaleChangedEvent &event) { void TitleScreenState::onServerOnlineEvent(const ServerOnlineEvent &event) { m_isServerOnline = event.isOnline; + m_port = event.port; m_isServerLaunched = false; } diff --git a/source/server/core/ServerApplication.cpp b/source/server/core/ServerApplication.cpp index 078329ec0..b257dcba3 100644 --- a/source/server/core/ServerApplication.cpp +++ b/source/server/core/ServerApplication.cpp @@ -76,10 +76,10 @@ void ServerApplication::init() { m_scriptEngine.luaCore().setRegistry(&m_registry); - std::cout << "Server is running on localhost:" << m_port << std::endl; + std::cout << "Server is running on localhost:" << m_server.port() << std::endl; if (m_eventHandler) - m_eventHandler->emplaceEvent(true); + m_eventHandler->emplaceEvent(true, m_server.port()); } int ServerApplication::run(bool isProtected) { diff --git a/source/server/core/ServerApplication.hpp b/source/server/core/ServerApplication.hpp index 47eb9bd4b..9122a1977 100644 --- a/source/server/core/ServerApplication.hpp +++ b/source/server/core/ServerApplication.hpp @@ -41,6 +41,7 @@ struct ServerOnlineEvent { bool isOnline; + u16 port; }; class ServerApplication { diff --git a/source/server/network/Server.cpp b/source/server/network/Server.cpp index 12543596d..fe3a277e0 100644 --- a/source/server/network/Server.cpp +++ b/source/server/network/Server.cpp @@ -32,7 +32,9 @@ void Server::init(u16 port) { m_udpSocket.setBlocking(false); - if (m_tcpListener.listen(port) != sf::Socket::Done) + m_port = m_udpSocket.getLocalPort(); + + if (m_tcpListener.listen(m_port) != sf::Socket::Done) throw EXCEPTION("Network error: Listen failed"); m_tcpListener.setBlocking(false); diff --git a/source/server/network/Server.hpp b/source/server/network/Server.hpp index 25eae5a3d..dffe32afa 100644 --- a/source/server/network/Server.hpp +++ b/source/server/network/Server.hpp @@ -52,6 +52,8 @@ class Server { bool isRunning() const { return m_isRunning; } + u16 port() const { return m_port; } + const ServerInfo &info() const { return m_info; } sf::UdpSocket &udpSocket() { return m_udpSocket; } @@ -71,6 +73,8 @@ class Server { bool m_isRunning = false; bool m_isSingleplayer = false; + u16 m_port = 0; + ServerInfo m_info; sf::UdpSocket m_udpSocket;