Node definition manager refactor (#7016)
[oweals/minetest.git] / src / main.cpp
index b9530c55ec586940823a962bc3270666212b37a1..8fce83dcbb52c54aa9f5e471d5d9d4bc825c0bd7 100644 (file)
@@ -18,40 +18,37 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 */
 
 #include "irrlicht.h" // createDevice
-
-#include "mainmenumanager.h"
 #include "irrlichttypes_extrabloated.h"
+#include "chat_interface.h"
 #include "debug.h"
 #include "unittest/test.h"
 #include "server.h"
 #include "filesys.h"
 #include "version.h"
-#include "guiMainMenu.h"
 #include "game.h"
 #include "defaultsettings.h"
 #include "gettext.h"
 #include "log.h"
 #include "quicktune.h"
 #include "httpfetch.h"
-#include "guiEngine.h"
-#include "map.h"
-#include "player.h"
-#include "mapsector.h"
-#include "fontengine.h"
 #include "gameparams.h"
-#include "database.h"
+#include "database/database.h"
 #include "config.h"
+#include "player.h"
 #include "porting.h"
+#include "network/socket.h"
 #if USE_CURSES
        #include "terminal_chat_console.h"
 #endif
 #ifndef SERVER
+#include "gui/guiMainMenu.h"
 #include "client/clientlauncher.h"
-
+#include "gui/guiEngine.h"
+#include "gui/mainmenumanager.h"
 #endif
 
 #ifdef HAVE_TOUCHSCREENGUI
-       #include "touchscreengui.h"
+       #include "gui/touchscreengui.h"
 #endif
 
 #if !defined(SERVER) && \
@@ -154,9 +151,6 @@ int main(int argc, char *argv[])
                return 1;
        }
 
-       // Initialize debug stacks
-       DSTACK(FUNCTION_NAME);
-
        // Debug handler
        BEGIN_DEBUG_EXCEPTION_HANDLER
 
@@ -215,7 +209,7 @@ int main(int argc, char *argv[])
 #endif
 
        // Update configuration file
-       if (g_settings_path != "")
+       if (!g_settings_path.empty())
                g_settings->updateConfigFile(g_settings_path.c_str());
 
        print_modified_quicktune_values();
@@ -310,17 +304,16 @@ static void print_help(const OptionList &allowed_options)
 
 static void print_allowed_options(const OptionList &allowed_options)
 {
-       for (OptionList::const_iterator i = allowed_options.begin();
-                       i != allowed_options.end(); ++i) {
+       for (const auto &allowed_option : allowed_options) {
                std::ostringstream os1(std::ios::binary);
-               os1 << "  --" << i->first;
-               if (i->second.type != VALUETYPE_FLAG)
+               os1 << "  --" << allowed_option.first;
+               if (allowed_option.second.type != VALUETYPE_FLAG)
                        os1 << _(" <value>");
 
                std::cout << padStringRight(os1.str(), 30);
 
-               if (i->second.help)
-                       std::cout << i->second.help;
+               if (allowed_option.second.help)
+                       std::cout << allowed_option.second.help;
 
                std::cout << std::endl;
        }
@@ -329,19 +322,18 @@ static void print_allowed_options(const OptionList &allowed_options)
 static void print_version()
 {
        std::cout << PROJECT_NAME_C " " << g_version_hash
-                 << " (" << porting::getPlatformName() << ")" << std::endl;
+               << " (" << porting::getPlatformName() << ")" << std::endl;
 #ifndef SERVER
-       std::cout << "Using Irrlicht " << IRRLICHT_SDK_VERSION << std::endl;
+       std::cout << "Using Irrlicht " IRRLICHT_SDK_VERSION << std::endl;
 #endif
-       std::cout << "Build info: " << g_build_info << std::endl;
+       std::cout << g_build_info << std::endl;
 }
 
 static void list_game_ids()
 {
        std::set<std::string> gameids = getAvailableGameIds();
-       for (std::set<std::string>::const_iterator i = gameids.begin();
-                       i != gameids.end(); ++i)
-               std::cout << (*i) <<std::endl;
+       for (const std::string &gameid : gameids)
+               std::cout << gameid <<std::endl;
 }
 
 static void list_worlds()
@@ -354,12 +346,12 @@ static void list_worlds()
 static void print_worldspecs(const std::vector<WorldSpec> &worldspecs,
                                                         std::ostream &os)
 {
-       for (size_t i = 0; i < worldspecs.size(); i++) {
-               std::string name = worldspecs[i].name;
-               std::string path = worldspecs[i].path;
-               if (name.find(" ") != std::string::npos)
-                       name = std::string("'") + name + "'";
-               path = std::string("'") + path + "'";
+       for (const WorldSpec &worldspec : worldspecs) {
+               std::string name = worldspec.name;
+               std::string path = worldspec.path;
+               if (name.find(' ') != std::string::npos)
+                       name = std::string("'").append(name).append("'");
+               path = std::string("'").append(path).append("'");
                name = padStringRight(name, 14);
                os << "  " << name << " " << path << std::endl;
        }
@@ -370,15 +362,15 @@ static void print_modified_quicktune_values()
        bool header_printed = false;
        std::vector<std::string> names = getQuicktuneNames();
 
-       for (u32 i = 0; i < names.size(); i++) {
-               QuicktuneValue val = getQuicktuneValue(names[i]);
+       for (const std::string &name : names) {
+               QuicktuneValue val = getQuicktuneValue(name);
                if (!val.modified)
                        continue;
                if (!header_printed) {
                        dstream << "Modified quicktune values:" << std::endl;
                        header_printed = true;
                }
-               dstream << names[i] << " = " << val.getString() << std::endl;
+               dstream << name << " = " << val.getString() << std::endl;
        }
 }
 
@@ -489,16 +481,16 @@ static bool read_config_file(const Settings &cmd_args)
                                DIR_DELIM + ".." + DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
 #endif
 
-               for (size_t i = 0; i < filenames.size(); i++) {
-                       bool r = g_settings->readConfigFile(filenames[i].c_str());
+               for (const std::string &filename : filenames) {
+                       bool r = g_settings->readConfigFile(filename.c_str());
                        if (r) {
-                               g_settings_path = filenames[i];
+                               g_settings_path = filename;
                                break;
                        }
                }
 
                // If no path found, use the first one (menu creates the file)
-               if (g_settings_path == "")
+               if (g_settings_path.empty())
                        g_settings_path = filenames[0];
        }
 
@@ -544,7 +536,7 @@ static void init_log_streams(const Settings &cmd_args)
 
        verbosestream << "log_filename = " << log_filename << std::endl;
 
-       file_log_output.open(log_filename.c_str());
+       file_log_output.open(log_filename);
        g_logger.addOutputMaxLevel(&file_log_output, log_level);
 }
 
@@ -577,6 +569,7 @@ static bool game_configure_world(GameParams *game_params, const Settings &cmd_ar
 {
        if (get_world_from_cmdline(game_params, cmd_args))
                return true;
+
        if (get_world_from_config(game_params, cmd_args))
                return true;
 
@@ -585,24 +578,24 @@ static bool game_configure_world(GameParams *game_params, const Settings &cmd_ar
 
 static bool get_world_from_cmdline(GameParams *game_params, const Settings &cmd_args)
 {
-       std::string commanded_world = "";
+       std::string commanded_world;
 
        // World name
-       std::string commanded_worldname = "";
+       std::string commanded_worldname;
        if (cmd_args.exists("worldname"))
                commanded_worldname = cmd_args.get("worldname");
 
        // If a world name was specified, convert it to a path
-       if (commanded_worldname != "") {
+       if (!commanded_worldname.empty()) {
                // Get information about available worlds
                std::vector<WorldSpec> worldspecs = getAvailableWorlds();
                bool found = false;
-               for (u32 i = 0; i < worldspecs.size(); i++) {
-                       std::string name = worldspecs[i].name;
+               for (const WorldSpec &worldspec : worldspecs) {
+                       std::string name = worldspec.name;
                        if (name == commanded_worldname) {
                                dstream << _("Using world specified by --worldname on the "
                                        "command line") << std::endl;
-                               commanded_world = worldspecs[i].path;
+                               commanded_world = worldspec.path;
                                found = true;
                                break;
                        }
@@ -615,7 +608,7 @@ static bool get_world_from_cmdline(GameParams *game_params, const Settings &cmd_
                }
 
                game_params->world_path = get_clean_world_path(commanded_world);
-               return commanded_world != "";
+               return !commanded_world.empty();
        }
 
        if (cmd_args.exists("world"))
@@ -626,20 +619,20 @@ static bool get_world_from_cmdline(GameParams *game_params, const Settings &cmd_
                commanded_world = cmd_args.get("nonopt0");
 
        game_params->world_path = get_clean_world_path(commanded_world);
-       return commanded_world != "";
+       return !commanded_world.empty();
 }
 
 static bool get_world_from_config(GameParams *game_params, const Settings &cmd_args)
 {
        // World directory
-       std::string commanded_world = "";
+       std::string commanded_world;
 
        if (g_settings->exists("map-dir"))
                commanded_world = g_settings->get("map-dir");
 
        game_params->world_path = get_clean_world_path(commanded_world);
 
-       return commanded_world != "";
+       return !commanded_world.empty();
 }
 
 static bool auto_select_world(GameParams *game_params)
@@ -733,8 +726,8 @@ static bool determine_subgame(GameParams *game_params)
 
        verbosestream << _("Determining gameid/gamespec") << std::endl;
        // If world doesn't exist
-       if (game_params->world_path != ""
-                       && !getWorldExists(game_params->world_path)) {
+       if (!game_params->world_path.empty()
+               && !getWorldExists(game_params->world_path)) {
                // Try to take gamespec from command line
                if (game_params->game_spec.isValid()) {
                        gamespec = game_params->game_spec;
@@ -783,8 +776,6 @@ static bool determine_subgame(GameParams *game_params)
  *****************************************************************************/
 static bool run_dedicated_server(const GameParams &game_params, const Settings &cmd_args)
 {
-       DSTACK("Dedicated server branch");
-
        verbosestream << _("Using world path") << " ["
                      << game_params.world_path << "]" << std::endl;
        verbosestream << _("Using gameid") << " ["
@@ -814,7 +805,8 @@ static bool run_dedicated_server(const GameParams &game_params, const Settings &
        // Database migration
        if (cmd_args.exists("migrate"))
                return migrate_map_database(game_params, cmd_args);
-       else if (cmd_args.exists("migrate-players"))
+
+       if (cmd_args.exists("migrate-players"))
                return ServerEnvironment::migratePlayersDatabase(game_params, cmd_args);
 
        if (cmd_args.exists("terminal")) {
@@ -846,13 +838,13 @@ static bool run_dedicated_server(const GameParams &game_params, const Settings &
                try {
                        // Create server
                        Server server(game_params.world_path, game_params.game_spec,
-                                       false, bind_addr.isIPv6(), true, &iface);
+                                       false, bind_addr, true, &iface);
 
                        g_term_console.setup(&iface, &kill, admin_nick);
 
                        g_term_console.start();
 
-                       server.start(bind_addr);
+                       server.start();
                        // Run server
                        dedicated_server_loop(server, kill);
                } catch (const ModError &e) {
@@ -880,8 +872,8 @@ static bool run_dedicated_server(const GameParams &game_params, const Settings &
                try {
                        // Create server
                        Server server(game_params.world_path, game_params.game_spec, false,
-                               bind_addr.isIPv6(), true);
-                       server.start(bind_addr);
+                               bind_addr, true);
+                       server.start();
 
                        // Run server
                        bool &kill = *porting::signal_handler_killstatus();