Skip to content

Commit

Permalink
Singleplayer server will now use a random available port instead of 4…
Browse files Browse the repository at this point in the history
…242.
  • Loading branch information
Unarelith committed Mar 18, 2020
1 parent e1e1c46 commit db587f1
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion source/client/graphics/TextureAtlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion source/client/states/TitleScreenState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions source/server/core/ServerApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServerOnlineEvent>(true);
m_eventHandler->emplaceEvent<ServerOnlineEvent>(true, m_server.port());
}

int ServerApplication::run(bool isProtected) {
Expand Down
1 change: 1 addition & 0 deletions source/server/core/ServerApplication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

struct ServerOnlineEvent {
bool isOnline;
u16 port;
};

class ServerApplication {
Expand Down
4 changes: 3 additions & 1 deletion source/server/network/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions source/server/network/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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;
Expand Down

0 comments on commit db587f1

Please sign in to comment.