From 3cb07d5fb6bec4556ee11009ac4590b486e1eab0 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Fri, 17 Jan 2025 20:14:56 +0100 Subject: [PATCH] Add limited game auto-selection to dedicated server --- src/main.cpp | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 2d0c2aa79b69c..de407a932b71f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -830,9 +830,7 @@ static bool game_configure(GameParams *game_params, const Settings &cmd_args) return false; } - game_configure_subgame(game_params, cmd_args); - - return true; + return game_configure_subgame(game_params, cmd_args); } static void game_configure_port(GameParams *game_params, const Settings &cmd_args) @@ -1014,17 +1012,25 @@ static bool determine_subgame(GameParams *game_params) if (game_params->game_spec.isValid()) { gamespec = game_params->game_spec; infostream << "Using commanded gameid [" << gamespec.id << "]" << std::endl; - } else { - if (game_params->is_dedicated_server) { - std::string contentdb_url = g_settings->get("contentdb_url"); - - // If this is a dedicated server and no gamespec has been specified, - // print a friendly error pointing to ContentDB. - errorstream << "To run a " PROJECT_NAME_C " server, you need to select a game using the '--gameid' argument." << std::endl - << "Check out " << contentdb_url << " for a selection of games to pick from and download." << std::endl; + } else if (game_params->is_dedicated_server) { + auto games = getAvailableGameIds(); + // If there's exactly one obvious choice then do the right thing + if (games.size() > 1) + games.erase("devtest"); + if (games.size() == 1) { + gamespec = findSubgame(*games.begin()); + infostream << "Automatically selecting gameid [" << gamespec.id << "]" << std::endl; + } else { + // Else, force the user to choose + auto &url = g_settings->get("contentdb_url"); + + errorstream << "To run a " PROJECT_NAME_C " server, you need to select a game using the '--gameid' argument." << std::endl; + if (games.empty()) + errorstream << "Check out " << url << " for a selection of games to pick from and download." << std::endl; + else + errorstream << "Use '--gameid list' to print a list of all installed games." << std::endl; + return false; } - - return false; } } else { // World exists std::string world_gameid = getWorldGameId(game_params->world_path, false); @@ -1045,6 +1051,8 @@ static bool determine_subgame(GameParams *game_params) } if (!gamespec.isValid()) { + if (!game_params->is_dedicated_server) + return true; // not an error, this would prevent the main menu from running errorstream << "Game [" << gamespec.id << "] could not be found." << std::endl; return false;