Cleanup ClientLauncher structure (#10160)

Remove duplicated variables and unify the startup data into a new (inherited) struct.
master
SmallJoker 2020-07-14 19:10:37 +02:00 committed by GitHub
parent 2bec83eec0
commit 4fa1e03f68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 197 additions and 200 deletions

View File

@ -56,6 +56,20 @@ bool isMenuActive()
// Passed to menus to allow disconnecting and exiting
MainGameCallback *g_gamecallback = nullptr;
#if 0
// This can be helpful for the next code cleanup
static void dump_start_data(const GameStartData &data)
{
std::cout <<
"\ndedicated " << (int)data.is_dedicated_server <<
"\nport " << data.socket_port <<
"\nworld_path " << data.world_spec.path <<
"\nworld game " << data.world_spec.gameid <<
"\ngame path " << data.game_spec.path <<
"\nplayer name " << data.name <<
"\naddress " << data.address << std::endl;
}
#endif
ClientLauncher::~ClientLauncher()
{
@ -74,9 +88,16 @@ ClientLauncher::~ClientLauncher()
}
bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args)
{
init_args(game_params, cmd_args);
/* This function is called when a client must be started.
* Covered cases:
* - Singleplayer (address but map provided)
* - Join server (no map but address provided)
* - Local server (for main menu only)
*/
init_args(start_data, cmd_args);
// List video modes if requested
if (list_video_modes)
@ -216,7 +237,7 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
core::rect<s32>(0, 0, 10000, 10000));
bool game_has_run = launch_game(error_message, reconnect_requested,
game_params, cmd_args);
start_data, cmd_args);
// Reset the reconnect_requested flag
reconnect_requested = false;
@ -241,13 +262,6 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
break;
}
if (current_playername.length() > PLAYERNAME_SIZE-1) {
error_message = gettext("Player name too long.");
playername = current_playername.substr(0, PLAYERNAME_SIZE-1);
g_settings->set("name", playername);
continue;
}
RenderingEngine::get_video_driver()->setTextureCreationFlag(
video::ETCF_CREATE_MIP_MAPS, g_settings->getBool("mip_map"));
@ -258,18 +272,11 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
the_game(
kill,
random_input,
input,
worldspec.path,
current_playername,
current_password,
current_address,
current_port,
start_data,
error_message,
chat_backend,
&reconnect_requested,
gamespec,
simple_singleplayer_mode
&reconnect_requested
);
RenderingEngine::get_scene_manager()->clear();
@ -311,31 +318,27 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
return retval;
}
void ClientLauncher::init_args(GameParams &game_params, const Settings &cmd_args)
void ClientLauncher::init_args(GameStartData &start_data, const Settings &cmd_args)
{
skip_main_menu = cmd_args.getFlag("go");
// FIXME: This is confusing (but correct)
start_data.address = g_settings->get("address");
if (cmd_args.exists("address")) {
// Join a remote server
start_data.address = cmd_args.get("address");
start_data.world_path.clear();
}
if (!start_data.world_path.empty()) {
// Start a singleplayer instance
start_data.address = "";
}
/* If world_path is set then override it unless skipping the main menu using
* the --go command line param. Else, give preference to the address
* supplied on the command line
*/
address = g_settings->get("address");
if (!game_params.world_path.empty() && !skip_main_menu)
address = "";
else if (cmd_args.exists("address"))
address = cmd_args.get("address");
playername = g_settings->get("name");
start_data.name = g_settings->get("name");
if (cmd_args.exists("name"))
playername = cmd_args.get("name");
start_data.name = cmd_args.get("name");
list_video_modes = cmd_args.getFlag("videomodes");
use_freetype = g_settings->getBool("freetype");
random_input = g_settings->getBool("random_input")
|| cmd_args.getFlag("random-input");
}
@ -375,28 +378,20 @@ void ClientLauncher::init_input()
}
bool ClientLauncher::launch_game(std::string &error_message,
bool reconnect_requested, GameParams &game_params,
bool reconnect_requested, GameStartData &start_data,
const Settings &cmd_args)
{
// Initialize menu data
MainMenuData menudata;
menudata.address = address;
menudata.name = playername;
menudata.password = password;
menudata.port = itos(game_params.socket_port);
menudata.script_data.errormessage = error_message;
menudata.script_data.reconnect_requested = reconnect_requested;
// Prepare and check the start data to launch a game
std::string error_message_lua = error_message;
error_message.clear();
if (cmd_args.exists("password"))
menudata.password = cmd_args.get("password");
start_data.password = cmd_args.get("password");
if (cmd_args.exists("password-file")) {
std::ifstream passfile(cmd_args.get("password-file"));
if (passfile.good()) {
getline(passfile, menudata.password);
getline(passfile, start_data.password);
} else {
error_message = gettext("Provided password file "
"failed to open: ")
@ -407,88 +402,112 @@ bool ClientLauncher::launch_game(std::string &error_message,
}
// If a world was commanded, append and select it
if (!game_params.world_path.empty()) {
worldspec.gameid = getWorldGameId(game_params.world_path, true);
worldspec.name = _("[--world parameter]");
// This is provieded by "get_world_from_cmdline()", main.cpp
if (!start_data.world_path.empty()) {
auto &spec = start_data.world_spec;
if (worldspec.gameid.empty()) { // Create new
worldspec.gameid = g_settings->get("default_game");
worldspec.name += " [new]";
spec.path = start_data.world_path;
spec.gameid = getWorldGameId(spec.path, true);
spec.name = _("[--world parameter]");
if (spec.gameid.empty()) { // Create new
spec.gameid = g_settings->get("default_game");
spec.name += " [new]";
}
worldspec.path = game_params.world_path;
}
/* Show the GUI menu
*/
std::string server_name, server_description;
start_data.local_server = false;
if (!skip_main_menu) {
// Initialize menu data
// TODO: Re-use existing structs (GameStartData)
MainMenuData menudata;
menudata.address = start_data.address;
menudata.name = start_data.name;
menudata.password = start_data.password;
menudata.port = itos(start_data.socket_port);
menudata.script_data.errormessage = error_message_lua;
menudata.script_data.reconnect_requested = reconnect_requested;
main_menu(&menudata);
// Skip further loading if there was an exit signal.
if (*porting::signal_handler_killstatus())
return false;
address = menudata.address;
if (!menudata.script_data.errormessage.empty()) {
/* The calling function will pass this back into this function upon the
* next iteration (if any) causing it to be displayed by the GUI
*/
error_message = menudata.script_data.errormessage;
return false;
}
int newport = stoi(menudata.port);
if (newport != 0)
game_params.socket_port = newport;
simple_singleplayer_mode = menudata.simple_singleplayer_mode;
start_data.socket_port = newport;
// Update world information using main menu data
std::vector<WorldSpec> worldspecs = getAvailableWorlds();
if (menudata.selected_world >= 0
&& menudata.selected_world < (int)worldspecs.size()) {
int world_index = menudata.selected_world;
if (world_index >= 0 && world_index < (int)worldspecs.size()) {
g_settings->set("selected_world_path",
worldspecs[menudata.selected_world].path);
worldspec = worldspecs[menudata.selected_world];
worldspecs[world_index].path);
start_data.world_spec = worldspecs[world_index];
}
start_data.name = menudata.name;
start_data.password = menudata.password;
start_data.address = std::move(menudata.address);
server_name = menudata.servername;
server_description = menudata.serverdescription;
start_data.local_server = !menudata.simple_singleplayer_mode &&
start_data.address.empty();
}
if (!menudata.script_data.errormessage.empty()) {
/* The calling function will pass this back into this function upon the
* next iteration (if any) causing it to be displayed by the GUI
*/
error_message = menudata.script_data.errormessage;
if (!RenderingEngine::run())
return false;
}
if (menudata.name.empty() && !simple_singleplayer_mode) {
if (!start_data.isSinglePlayer() && start_data.name.empty()) {
error_message = gettext("Please choose a name!");
errorstream << error_message << std::endl;
return false;
}
playername = menudata.name;
password = menudata.password;
current_playername = playername;
current_password = password;
current_address = address;
current_port = game_params.socket_port;
// If using simple singleplayer mode, override
if (simple_singleplayer_mode) {
assert(!skip_main_menu);
current_playername = "singleplayer";
current_password = "";
current_address = "";
current_port = myrand_range(49152, 65535);
if (start_data.isSinglePlayer()) {
start_data.name = "singleplayer";
start_data.password = "";
start_data.socket_port = myrand_range(49152, 65535);
} else {
g_settings->set("name", playername);
if (!address.empty()) {
g_settings->set("name", start_data.name);
if (!start_data.address.empty()) {
ServerListSpec server;
server["name"] = menudata.servername;
server["address"] = menudata.address;
server["port"] = menudata.port;
server["description"] = menudata.serverdescription;
server["name"] = server_name;
server["address"] = start_data.address;
server["port"] = itos(start_data.socket_port);
server["description"] = server_description;
ServerList::insert(server);
}
}
if (start_data.name.length() > PLAYERNAME_SIZE - 1) {
error_message = gettext("Player name too long.");
start_data.name.resize(PLAYERNAME_SIZE);
g_settings->set("name", start_data.name);
return false;
}
auto &worldspec = start_data.world_spec;
infostream << "Selected world: " << worldspec.name
<< " [" << worldspec.path << "]" << std::endl;
if (current_address.empty()) { // If local game
if (start_data.address.empty()) {
// For singleplayer and local server
if (worldspec.path.empty()) {
error_message = gettext("No world selected and no address "
"provided. Nothing to do.");
@ -504,8 +523,8 @@ bool ClientLauncher::launch_game(std::string &error_message,
}
// Load gamespec for required game
gamespec = findWorldSubgame(worldspec.path);
if (!gamespec.isValid() && !game_params.game_spec.isValid()) {
start_data.game_spec = findWorldSubgame(worldspec.path);
if (!start_data.game_spec.isValid()) {
error_message = gettext("Could not find or load game \"")
+ worldspec.gameid + "\"";
errorstream << error_message << std::endl;
@ -515,15 +534,7 @@ bool ClientLauncher::launch_game(std::string &error_message,
if (porting::signal_handler_killstatus())
return true;
if (game_params.game_spec.isValid() &&
game_params.game_spec.id != worldspec.gameid) {
warningstream << "Overriding gamespec from \""
<< worldspec.gameid << "\" to \""
<< game_params.game_spec.id << "\"" << std::endl;
gamespec = game_params.game_spec;
}
if (!gamespec.isValid()) {
if (!start_data.game_spec.isValid()) {
error_message = gettext("Invalid gamespec.");
error_message += " (world.gameid=" + worldspec.gameid + ")";
errorstream << error_message << std::endl;
@ -531,6 +542,7 @@ bool ClientLauncher::launch_game(std::string &error_message,
}
}
start_data.world_path = start_data.world_spec.path;
return true;
}

View File

@ -32,15 +32,15 @@ public:
~ClientLauncher();
bool run(GameParams &game_params, const Settings &cmd_args);
bool run(GameStartData &start_data, const Settings &cmd_args);
protected:
void init_args(GameParams &game_params, const Settings &cmd_args);
private:
void init_args(GameStartData &start_data, const Settings &cmd_args);
bool init_engine();
void init_input();
bool launch_game(std::string &error_message, bool reconnect_requested,
GameParams &game_params, const Settings &cmd_args);
GameStartData &start_data, const Settings &cmd_args);
void main_menu(MainMenuData *menudata);
@ -48,23 +48,8 @@ protected:
bool list_video_modes = false;
bool skip_main_menu = false;
bool use_freetype = false;
bool random_input = false;
std::string address = "";
std::string playername = "";
std::string password = "";
InputHandler *input = nullptr;
MyEventReceiver *receiver = nullptr;
gui::IGUISkin *skin = nullptr;
gui::IGUIFont *font = nullptr;
SubgameSpec gamespec;
WorldSpec worldspec;
bool simple_singleplayer_mode = false;
// These are set up based on the menu and other things
// TODO: Are these required since there's already playername, password, etc
std::string current_playername = "inv£lid";
std::string current_password = "";
std::string current_address = "does-not-exist";
int current_port = 0;
};

View File

@ -34,11 +34,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "clouds.h"
#include "config.h"
#include "content_cao.h"
#include "content/subgames.h"
#include "client/event_manager.h"
#include "fontengine.h"
#include "itemdef.h"
#include "log.h"
#include "filesys.h"
#include "gameparams.h"
#include "gettext.h"
#include "gui/guiChatConsole.h"
#include "gui/guiConfirmRegistration.h"
@ -669,19 +671,11 @@ public:
~Game();
bool startup(bool *kill,
bool random_input,
InputHandler *input,
const std::string &map_dir,
const std::string &playername,
const std::string &password,
// If address is "", local server is used and address is updated
std::string *address,
u16 port,
const GameStartData &game_params,
std::string &error_message,
bool *reconnect,
ChatBackend *chat_backend,
const SubgameSpec &gamespec, // Used for local game
bool simple_singleplayer_mode);
ChatBackend *chat_backend);
void run();
void shutdown();
@ -691,21 +685,18 @@ protected:
void extendedResourceCleanup();
// Basic initialisation
bool init(const std::string &map_dir, std::string *address,
u16 port,
const SubgameSpec &gamespec);
bool init(const std::string &map_dir, const std::string &address,
u16 port, const SubgameSpec &gamespec);
bool initSound();
bool createSingleplayerServer(const std::string &map_dir,
const SubgameSpec &gamespec, u16 port, std::string *address);
const SubgameSpec &gamespec, u16 port);
// Client creation
bool createClient(const std::string &playername,
const std::string &password, std::string *address, u16 port);
bool createClient(const GameStartData &start_data);
bool initGui();
// Client connection
bool connectToServer(const std::string &playername,
const std::string &password, std::string *address, u16 port,
bool connectToServer(const GameStartData &start_data,
bool *connect_ok, bool *aborted);
bool getServerContent(bool *aborted);
@ -885,7 +876,6 @@ private:
bool *reconnect_requested;
scene::ISceneNode *skybox;
bool random_input;
bool simple_singleplayer_mode;
/* End 'cache' */
@ -1017,28 +1007,21 @@ Game::~Game()
}
bool Game::startup(bool *kill,
bool random_input,
InputHandler *input,
const std::string &map_dir,
const std::string &playername,
const std::string &password,
std::string *address, // can change if simple_singleplayer_mode
u16 port,
const GameStartData &start_data,
std::string &error_message,
bool *reconnect,
ChatBackend *chat_backend,
const SubgameSpec &gamespec,
bool simple_singleplayer_mode)
ChatBackend *chat_backend)
{
// "cache"
this->device = RenderingEngine::get_raw_device();
this->kill = kill;
this->error_message = &error_message;
this->reconnect_requested = reconnect;
this->random_input = random_input;
this->input = input;
this->chat_backend = chat_backend;
this->simple_singleplayer_mode = simple_singleplayer_mode;
this->simple_singleplayer_mode = start_data.isSinglePlayer();
input->keycache.populate();
@ -1059,10 +1042,12 @@ bool Game::startup(bool *kill,
g_client_translations->clear();
if (!init(map_dir, address, port, gamespec))
// address can change if simple_singleplayer_mode
if (!init(start_data.world_spec.path, start_data.address,
start_data.socket_port, start_data.game_spec))
return false;
if (!createClient(playername, password, address, port))
if (!createClient(start_data))
return false;
RenderingEngine::initialize(client, hud);
@ -1221,7 +1206,7 @@ void Game::shutdown()
bool Game::init(
const std::string &map_dir,
std::string *address,
const std::string &address,
u16 port,
const SubgameSpec &gamespec)
{
@ -1245,8 +1230,8 @@ bool Game::init(
return false;
// Create a server if not connecting to an existing one
if (address->empty()) {
if (!createSingleplayerServer(map_dir, gamespec, port, address))
if (address.empty()) {
if (!createSingleplayerServer(map_dir, gamespec, port))
return false;
}
@ -1281,7 +1266,7 @@ bool Game::initSound()
}
bool Game::createSingleplayerServer(const std::string &map_dir,
const SubgameSpec &gamespec, u16 port, std::string *address)
const SubgameSpec &gamespec, u16 port)
{
showOverlayMessage(N_("Creating server..."), 0, 5);
@ -1314,8 +1299,7 @@ bool Game::createSingleplayerServer(const std::string &map_dir,
return true;
}
bool Game::createClient(const std::string &playername,
const std::string &password, std::string *address, u16 port)
bool Game::createClient(const GameStartData &start_data)
{
showOverlayMessage(N_("Creating client..."), 0, 10);
@ -1330,8 +1314,7 @@ bool Game::createClient(const std::string &playername,
g_touchscreengui->hide();
}
#endif
if (!connectToServer(playername, password, address, port,
&could_connect, &connect_aborted))
if (!connectToServer(start_data, &could_connect, &connect_aborted))
return false;
if (!could_connect) {
@ -1463,8 +1446,7 @@ bool Game::initGui()
return true;
}
bool Game::connectToServer(const std::string &playername,
const std::string &password, std::string *address, u16 port,
bool Game::connectToServer(const GameStartData &start_data,
bool *connect_ok, bool *connection_aborted)
{
*connect_ok = false; // Let's not be overly optimistic
@ -1473,10 +1455,10 @@ bool Game::connectToServer(const std::string &playername,
showOverlayMessage(N_("Resolving address..."), 0, 15);
Address connect_address(0, 0, 0, 0, port);
Address connect_address(0, 0, 0, 0, start_data.socket_port);
try {
connect_address.Resolve(address->c_str());
connect_address.Resolve(start_data.address.c_str());
if (connect_address.isZero()) { // i.e. INADDR_ANY, IN6ADDR_ANY
//connect_address.Resolve("localhost");
@ -1503,7 +1485,8 @@ bool Game::connectToServer(const std::string &playername,
return false;
}
client = new Client(playername.c_str(), password, *address,
client = new Client(start_data.name.c_str(),
start_data.password, start_data.address,
*draw_control, texture_src, shader_src,
itemdef_manager, nodedef_manager, sound, eventmgr,
connect_address.isIPv6(), m_game_ui.get());
@ -1574,13 +1557,13 @@ bool Game::connectToServer(const std::string &playername,
} else {
registration_confirmation_shown = true;
(new GUIConfirmRegistration(guienv, guienv->getRootGUIElement(), -1,
&g_menumgr, client, playername, password,
&g_menumgr, client, start_data.name, start_data.password,
connection_aborted, texture_src))->drop();
}
} else {
wait_time += dtime;
// Only time out if we aren't waiting for the server we started
if (!address->empty() && wait_time > 10) {
if (!start_data.isSinglePlayer() && wait_time > 10) {
*error_message = "Connection timed out.";
errorstream << *error_message << std::endl;
break;
@ -2399,10 +2382,10 @@ void Game::checkZoomEnabled()
void Game::updateCameraDirection(CameraOrientation *cam, float dtime)
{
if ((device->isWindowActive() && device->isWindowFocused()
&& !isMenuActive()) || random_input) {
&& !isMenuActive()) || input->isRandom()) {
#ifndef __ANDROID__
if (!random_input) {
if (!input->isRandom()) {
// Mac OSX gets upset if this is set every frame
if (device->getCursorControl()->isVisible())
device->getCursorControl()->setVisible(false);
@ -3349,7 +3332,7 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
}
// formspec in meta
if (meta && !meta->getString("formspec").empty() && !random_input
if (meta && !meta->getString("formspec").empty() && !input->isRandom()
&& !isKeyDown(KeyType::SNEAK)) {
// on_rightclick callbacks are called anyway
if (nodedef_manager->get(map.getNode(nodepos)).rightclickable)
@ -4260,19 +4243,11 @@ void Game::showPauseMenu()
/****************************************************************************/
void the_game(bool *kill,
bool random_input,
InputHandler *input,
const std::string &map_dir,
const std::string &playername,
const std::string &password,
const std::string &address, // If empty local server is created
u16 port,
const GameStartData &start_data,
std::string &error_message,
ChatBackend &chat_backend,
bool *reconnect_requested,
const SubgameSpec &gamespec, // Used for local game
bool simple_singleplayer_mode)
bool *reconnect_requested) // Used for local game
{
Game game;
@ -4280,14 +4255,11 @@ void the_game(bool *kill,
* is created then this is updated and we don't want to change the value
* passed to us by the calling function
*/
std::string server_address = address;
try {
if (game.startup(kill, random_input, input, map_dir,
playername, password, &server_address, port, error_message,
reconnect_requested, &chat_backend, gamespec,
simple_singleplayer_mode)) {
if (game.startup(kill, input, start_data, error_message,
reconnect_requested, &chat_backend)) {
game.run();
}

View File

@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
class InputHandler;
class ChatBackend; /* to avoid having to include chat.h */
struct SubgameSpec;
struct GameStartData;
struct Jitter {
f32 max, min, avg, counter, max_sample, min_sample, max_fraction;
@ -41,16 +42,10 @@ struct CameraOrientation {
f32 camera_pitch; // "up/down"
};
void the_game(bool *kill,
bool random_input,
InputHandler *input,
const std::string &map_dir,
const std::string &playername,
const std::string &password,
const std::string &address, // If "", local server is used
u16 port,
const GameStartData &start_data,
std::string &error_message,
ChatBackend &chat_backend,
bool *reconnect_requested,
const SubgameSpec &gamespec, // Used for local game
bool simple_singleplayer_mode);
bool *reconnect_requested);

View File

@ -219,6 +219,11 @@ public:
virtual ~InputHandler() = default;
virtual bool isRandom() const
{
return false;
}
virtual bool isKeyDown(GameKeyType k) = 0;
virtual bool wasKeyDown(GameKeyType k) = 0;
virtual bool cancelPressed() = 0;
@ -372,6 +377,11 @@ class RandomInputHandler : public InputHandler
public:
RandomInputHandler() = default;
bool isRandom() const
{
return true;
}
virtual bool isKeyDown(GameKeyType k) { return keydown[keycache.key[k]]; }
virtual bool wasKeyDown(GameKeyType k) { return false; }
virtual bool cancelPressed() { return false; }

View File

@ -23,10 +23,29 @@ with this program; if not, write to the Free Software Foundation, Inc.,
struct SubgameSpec;
// Information provided from "main"
struct GameParams
{
GameParams() = default;
u16 socket_port;
std::string world_path;
SubgameSpec game_spec;
bool is_dedicated_server;
};
// Information processed by main menu
struct GameStartData : GameParams
{
GameStartData() = default;
bool isSinglePlayer() const { return address.empty() && !local_server; }
std::string name;
std::string password;
std::string address;
bool local_server;
// "world_path" must be kept in sync!
WorldSpec world_spec;
};

View File

@ -197,7 +197,7 @@ int main(int argc, char *argv[])
}
#endif
GameParams game_params;
GameStartData game_params;
#ifdef SERVER
porting::attachOrCreateConsole();
game_params.is_dedicated_server = true;
@ -604,10 +604,14 @@ static bool game_configure(GameParams *game_params, const Settings &cmd_args)
static void game_configure_port(GameParams *game_params, const Settings &cmd_args)
{
if (cmd_args.exists("port"))
if (cmd_args.exists("port")) {
game_params->socket_port = cmd_args.getU16("port");
else
game_params->socket_port = g_settings->getU16("port");
} else {
if (game_params->is_dedicated_server)
game_params->socket_port = g_settings->getU16("port");
else
game_params->socket_port = g_settings->getU16("remote_port");
}
if (game_params->socket_port == 0)
game_params->socket_port = DEFAULT_SERVER_PORT;