3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "irrlicht.h" // createDevice
21 #include "irrlichttypes_extrabloated.h"
22 #include "chat_interface.h"
24 #include "unittest/test.h"
29 #include "defaultsettings.h"
32 #include "quicktune.h"
33 #include "httpfetch.h"
34 #include "gameparams.h"
35 #include "database/database.h"
39 #include "network/socket.h"
41 #include "terminal_chat_console.h"
44 #include "gui/guiMainMenu.h"
45 #include "client/clientlauncher.h"
46 #include "gui/guiEngine.h"
47 #include "gui/mainmenumanager.h"
50 #ifdef HAVE_TOUCHSCREENGUI
51 #include "gui/touchscreengui.h"
54 #if !defined(SERVER) && \
55 (IRRLICHT_VERSION_MAJOR == 1) && \
56 (IRRLICHT_VERSION_MINOR == 8) && \
57 (IRRLICHT_VERSION_REVISION == 2)
58 #error "Irrlicht 1.8.2 is known to be broken - please update Irrlicht to version >= 1.8.3"
61 #define DEBUGFILE "debug.txt"
62 #define DEFAULT_SERVER_PORT 30000
64 typedef std::map<std::string, ValueSpec> OptionList;
66 /**********************************************************************
68 **********************************************************************/
70 static bool get_cmdline_opts(int argc, char *argv[], Settings *cmd_args);
71 static void set_allowed_options(OptionList *allowed_options);
73 static void print_help(const OptionList &allowed_options);
74 static void print_allowed_options(const OptionList &allowed_options);
75 static void print_version();
76 static void print_worldspecs(const std::vector<WorldSpec> &worldspecs,
78 static void print_modified_quicktune_values();
80 static void list_game_ids();
81 static void list_worlds();
82 static void setup_log_params(const Settings &cmd_args);
83 static bool create_userdata_path();
84 static bool init_common(const Settings &cmd_args, int argc, char *argv[]);
85 static void startup_message();
86 static bool read_config_file(const Settings &cmd_args);
87 static void init_log_streams(const Settings &cmd_args);
89 static bool game_configure(GameParams *game_params, const Settings &cmd_args);
90 static void game_configure_port(GameParams *game_params, const Settings &cmd_args);
92 static bool game_configure_world(GameParams *game_params, const Settings &cmd_args);
93 static bool get_world_from_cmdline(GameParams *game_params, const Settings &cmd_args);
94 static bool get_world_from_config(GameParams *game_params, const Settings &cmd_args);
95 static bool auto_select_world(GameParams *game_params);
96 static std::string get_clean_world_path(const std::string &path);
98 static bool game_configure_subgame(GameParams *game_params, const Settings &cmd_args);
99 static bool get_game_from_cmdline(GameParams *game_params, const Settings &cmd_args);
100 static bool determine_subgame(GameParams *game_params);
102 static bool run_dedicated_server(const GameParams &game_params, const Settings &cmd_args);
103 static bool migrate_map_database(const GameParams &game_params, const Settings &cmd_args);
105 /**********************************************************************/
108 FileLogOutput file_log_output;
110 static OptionList allowed_options;
112 int main(int argc, char *argv[])
115 debug_set_exception_handler();
117 g_logger.registerThread("Main");
118 g_logger.addOutputMaxLevel(&stderr_output, LL_ACTION);
121 bool cmd_args_ok = get_cmdline_opts(argc, argv, &cmd_args);
123 || cmd_args.getFlag("help")
124 || cmd_args.exists("nonopt1")) {
125 porting::attachOrCreateConsole();
126 print_help(allowed_options);
127 return cmd_args_ok ? 0 : 1;
129 if (cmd_args.getFlag("console"))
130 porting::attachOrCreateConsole();
132 if (cmd_args.getFlag("version")) {
133 porting::attachOrCreateConsole();
138 setup_log_params(cmd_args);
140 porting::signal_handler_init();
143 porting::initAndroid();
144 porting::initializePathsAndroid();
146 porting::initializePaths();
149 if (!create_userdata_path()) {
150 errorstream << "Cannot create user data directory" << std::endl;
155 BEGIN_DEBUG_EXCEPTION_HANDLER
157 // List gameids if requested
158 if (cmd_args.exists("gameid") && cmd_args.get("gameid") == "list") {
163 // List worlds if requested
164 if (cmd_args.exists("world") && cmd_args.get("world") == "list") {
169 if (!init_common(cmd_args, argc, argv))
172 if (g_settings->getBool("enable_console"))
173 porting::attachOrCreateConsole();
177 if (cmd_args.getFlag("run-unittests")) {
182 GameParams game_params;
184 porting::attachOrCreateConsole();
185 game_params.is_dedicated_server = true;
187 const bool isServer = cmd_args.getFlag("server");
189 porting::attachOrCreateConsole();
190 game_params.is_dedicated_server = isServer;
193 if (!game_configure(&game_params, cmd_args))
196 sanity_check(!game_params.world_path.empty());
198 infostream << "Using commanded world path ["
199 << game_params.world_path << "]" << std::endl;
201 if (game_params.is_dedicated_server)
202 return run_dedicated_server(game_params, cmd_args) ? 0 : 1;
205 ClientLauncher launcher;
206 retval = launcher.run(game_params, cmd_args) ? 0 : 1;
211 // Update configuration file
212 if (!g_settings_path.empty())
213 g_settings->updateConfigFile(g_settings_path.c_str());
215 print_modified_quicktune_values();
217 // Stop httpfetch thread (if started)
220 END_DEBUG_EXCEPTION_HANDLER
226 /*****************************************************************************
228 *****************************************************************************/
231 static bool get_cmdline_opts(int argc, char *argv[], Settings *cmd_args)
233 set_allowed_options(&allowed_options);
235 return cmd_args->parseCommandLine(argc, argv, allowed_options);
238 static void set_allowed_options(OptionList *allowed_options)
240 allowed_options->clear();
242 allowed_options->insert(std::make_pair("help", ValueSpec(VALUETYPE_FLAG,
243 _("Show allowed options"))));
244 allowed_options->insert(std::make_pair("version", ValueSpec(VALUETYPE_FLAG,
245 _("Show version information"))));
246 allowed_options->insert(std::make_pair("config", ValueSpec(VALUETYPE_STRING,
247 _("Load configuration from specified file"))));
248 allowed_options->insert(std::make_pair("port", ValueSpec(VALUETYPE_STRING,
249 _("Set network port (UDP)"))));
250 allowed_options->insert(std::make_pair("run-unittests", ValueSpec(VALUETYPE_FLAG,
251 _("Run the unit tests and exit"))));
252 allowed_options->insert(std::make_pair("map-dir", ValueSpec(VALUETYPE_STRING,
253 _("Same as --world (deprecated)"))));
254 allowed_options->insert(std::make_pair("world", ValueSpec(VALUETYPE_STRING,
255 _("Set world path (implies local game) ('list' lists all)"))));
256 allowed_options->insert(std::make_pair("worldname", ValueSpec(VALUETYPE_STRING,
257 _("Set world by name (implies local game)"))));
258 allowed_options->insert(std::make_pair("quiet", ValueSpec(VALUETYPE_FLAG,
259 _("Print to console errors only"))));
260 allowed_options->insert(std::make_pair("info", ValueSpec(VALUETYPE_FLAG,
261 _("Print more information to console"))));
262 allowed_options->insert(std::make_pair("verbose", ValueSpec(VALUETYPE_FLAG,
263 _("Print even more information to console"))));
264 allowed_options->insert(std::make_pair("trace", ValueSpec(VALUETYPE_FLAG,
265 _("Print enormous amounts of information to log and console"))));
266 allowed_options->insert(std::make_pair("logfile", ValueSpec(VALUETYPE_STRING,
267 _("Set logfile path ('' = no logging)"))));
268 allowed_options->insert(std::make_pair("gameid", ValueSpec(VALUETYPE_STRING,
269 _("Set gameid (\"--gameid list\" prints available ones)"))));
270 allowed_options->insert(std::make_pair("migrate", ValueSpec(VALUETYPE_STRING,
271 _("Migrate from current map backend to another (Only works when using minetestserver or with --server)"))));
272 allowed_options->insert(std::make_pair("migrate-players", ValueSpec(VALUETYPE_STRING,
273 _("Migrate from current players backend to another (Only works when using minetestserver or with --server)"))));
274 allowed_options->insert(std::make_pair("terminal", ValueSpec(VALUETYPE_FLAG,
275 _("Feature an interactive terminal (Only works when using minetestserver or with --server)"))));
277 allowed_options->insert(std::make_pair("videomodes", ValueSpec(VALUETYPE_FLAG,
278 _("Show available video modes"))));
279 allowed_options->insert(std::make_pair("speedtests", ValueSpec(VALUETYPE_FLAG,
280 _("Run speed tests"))));
281 allowed_options->insert(std::make_pair("address", ValueSpec(VALUETYPE_STRING,
282 _("Address to connect to. ('' = local game)"))));
283 allowed_options->insert(std::make_pair("random-input", ValueSpec(VALUETYPE_FLAG,
284 _("Enable random user input, for testing"))));
285 allowed_options->insert(std::make_pair("server", ValueSpec(VALUETYPE_FLAG,
286 _("Run dedicated server"))));
287 allowed_options->insert(std::make_pair("name", ValueSpec(VALUETYPE_STRING,
288 _("Set player name"))));
289 allowed_options->insert(std::make_pair("password", ValueSpec(VALUETYPE_STRING,
290 _("Set password"))));
291 allowed_options->insert(std::make_pair("go", ValueSpec(VALUETYPE_FLAG,
292 _("Disable main menu"))));
293 allowed_options->insert(std::make_pair("console", ValueSpec(VALUETYPE_FLAG,
294 _("Starts with the console (Windows only)"))));
299 static void print_help(const OptionList &allowed_options)
301 std::cout << _("Allowed options:") << std::endl;
302 print_allowed_options(allowed_options);
305 static void print_allowed_options(const OptionList &allowed_options)
307 for (const auto &allowed_option : allowed_options) {
308 std::ostringstream os1(std::ios::binary);
309 os1 << " --" << allowed_option.first;
310 if (allowed_option.second.type != VALUETYPE_FLAG)
311 os1 << _(" <value>");
313 std::cout << padStringRight(os1.str(), 30);
315 if (allowed_option.second.help)
316 std::cout << allowed_option.second.help;
318 std::cout << std::endl;
322 static void print_version()
324 std::cout << PROJECT_NAME_C " " << g_version_hash
325 << " (" << porting::getPlatformName() << ")" << std::endl;
327 std::cout << "Using Irrlicht " IRRLICHT_SDK_VERSION << std::endl;
329 std::cout << g_build_info << std::endl;
332 static void list_game_ids()
334 std::set<std::string> gameids = getAvailableGameIds();
335 for (const std::string &gameid : gameids)
336 std::cout << gameid <<std::endl;
339 static void list_worlds()
341 std::cout << _("Available worlds:") << std::endl;
342 std::vector<WorldSpec> worldspecs = getAvailableWorlds();
343 print_worldspecs(worldspecs, std::cout);
346 static void print_worldspecs(const std::vector<WorldSpec> &worldspecs,
349 for (const WorldSpec &worldspec : worldspecs) {
350 std::string name = worldspec.name;
351 std::string path = worldspec.path;
352 if (name.find(' ') != std::string::npos)
353 name = std::string("'").append(name).append("'");
354 path = std::string("'").append(path).append("'");
355 name = padStringRight(name, 14);
356 os << " " << name << " " << path << std::endl;
360 static void print_modified_quicktune_values()
362 bool header_printed = false;
363 std::vector<std::string> names = getQuicktuneNames();
365 for (const std::string &name : names) {
366 QuicktuneValue val = getQuicktuneValue(name);
369 if (!header_printed) {
370 dstream << "Modified quicktune values:" << std::endl;
371 header_printed = true;
373 dstream << name << " = " << val.getString() << std::endl;
377 static void setup_log_params(const Settings &cmd_args)
379 // Quiet mode, print errors only
380 if (cmd_args.getFlag("quiet")) {
381 g_logger.removeOutput(&stderr_output);
382 g_logger.addOutputMaxLevel(&stderr_output, LL_ERROR);
385 // If trace is enabled, enable logging of certain things
386 if (cmd_args.getFlag("trace")) {
387 dstream << _("Enabling trace level debug output") << std::endl;
388 g_logger.setTraceEnabled(true);
389 dout_con_ptr = &verbosestream; // This is somewhat old
390 socket_enable_debug_output = true; // Sockets doesn't use log.h
393 // In certain cases, output info level on stderr
394 if (cmd_args.getFlag("info") || cmd_args.getFlag("verbose") ||
395 cmd_args.getFlag("trace") || cmd_args.getFlag("speedtests"))
396 g_logger.addOutput(&stderr_output, LL_INFO);
398 // In certain cases, output verbose level on stderr
399 if (cmd_args.getFlag("verbose") || cmd_args.getFlag("trace"))
400 g_logger.addOutput(&stderr_output, LL_VERBOSE);
403 static bool create_userdata_path()
408 if (!fs::PathExists(porting::path_user)) {
409 success = fs::CreateDir(porting::path_user);
413 porting::copyAssets();
415 // Create user data directory
416 success = fs::CreateDir(porting::path_user);
422 static bool init_common(const Settings &cmd_args, int argc, char *argv[])
425 set_default_settings(g_settings);
427 // Initialize sockets
429 atexit(sockets_cleanup);
431 if (!read_config_file(cmd_args))
434 init_log_streams(cmd_args);
436 // Initialize random seed
440 // Initialize HTTP fetcher
441 httpfetch_init(g_settings->getS32("curl_parallel_limit"));
443 init_gettext(porting::path_locale.c_str(),
444 g_settings->get("language"), argc, argv);
449 static void startup_message()
451 infostream << PROJECT_NAME << " " << _("with")
452 << " SER_FMT_VER_HIGHEST_READ="
453 << (int)SER_FMT_VER_HIGHEST_READ << ", "
454 << g_build_info << std::endl;
457 static bool read_config_file(const Settings &cmd_args)
459 // Path of configuration file in use
460 sanity_check(g_settings_path == ""); // Sanity check
462 if (cmd_args.exists("config")) {
463 bool r = g_settings->readConfigFile(cmd_args.get("config").c_str());
465 errorstream << "Could not read configuration from \""
466 << cmd_args.get("config") << "\"" << std::endl;
469 g_settings_path = cmd_args.get("config");
471 std::vector<std::string> filenames;
472 filenames.push_back(porting::path_user + DIR_DELIM + "minetest.conf");
473 // Legacy configuration file location
474 filenames.push_back(porting::path_user +
475 DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
478 // Try also from a lower level (to aid having the same configuration
479 // for many RUN_IN_PLACE installs)
480 filenames.push_back(porting::path_user +
481 DIR_DELIM + ".." + DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
484 for (const std::string &filename : filenames) {
485 bool r = g_settings->readConfigFile(filename.c_str());
487 g_settings_path = filename;
492 // If no path found, use the first one (menu creates the file)
493 if (g_settings_path.empty())
494 g_settings_path = filenames[0];
500 static void init_log_streams(const Settings &cmd_args)
503 std::string log_filename = DEBUGFILE;
505 std::string log_filename = porting::path_user + DIR_DELIM + DEBUGFILE;
507 if (cmd_args.exists("logfile"))
508 log_filename = cmd_args.get("logfile");
510 g_logger.removeOutput(&file_log_output);
511 std::string conf_loglev = g_settings->get("debug_log_level");
513 // Old integer format
514 if (std::isdigit(conf_loglev[0])) {
515 warningstream << "Deprecated use of debug_log_level with an "
516 "integer value; please update your configuration." << std::endl;
517 static const char *lev_name[] =
518 {"", "error", "action", "info", "verbose"};
519 int lev_i = atoi(conf_loglev.c_str());
520 if (lev_i < 0 || lev_i >= (int)ARRLEN(lev_name)) {
521 warningstream << "Supplied invalid debug_log_level!"
522 " Assuming action level." << std::endl;
525 conf_loglev = lev_name[lev_i];
528 if (log_filename.empty() || conf_loglev.empty()) // No logging
531 LogLevel log_level = Logger::stringToLevel(conf_loglev);
532 if (log_level == LL_MAX) {
533 warningstream << "Supplied unrecognized debug_log_level; "
534 "using maximum." << std::endl;
537 verbosestream << "log_filename = " << log_filename << std::endl;
539 file_log_output.open(log_filename);
540 g_logger.addOutputMaxLevel(&file_log_output, log_level);
543 static bool game_configure(GameParams *game_params, const Settings &cmd_args)
545 game_configure_port(game_params, cmd_args);
547 if (!game_configure_world(game_params, cmd_args)) {
548 errorstream << "No world path specified or found." << std::endl;
552 game_configure_subgame(game_params, cmd_args);
557 static void game_configure_port(GameParams *game_params, const Settings &cmd_args)
559 if (cmd_args.exists("port"))
560 game_params->socket_port = cmd_args.getU16("port");
562 game_params->socket_port = g_settings->getU16("port");
564 if (game_params->socket_port == 0)
565 game_params->socket_port = DEFAULT_SERVER_PORT;
568 static bool game_configure_world(GameParams *game_params, const Settings &cmd_args)
570 if (get_world_from_cmdline(game_params, cmd_args))
573 if (get_world_from_config(game_params, cmd_args))
576 return auto_select_world(game_params);
579 static bool get_world_from_cmdline(GameParams *game_params, const Settings &cmd_args)
581 std::string commanded_world;
584 std::string commanded_worldname;
585 if (cmd_args.exists("worldname"))
586 commanded_worldname = cmd_args.get("worldname");
588 // If a world name was specified, convert it to a path
589 if (!commanded_worldname.empty()) {
590 // Get information about available worlds
591 std::vector<WorldSpec> worldspecs = getAvailableWorlds();
593 for (const WorldSpec &worldspec : worldspecs) {
594 std::string name = worldspec.name;
595 if (name == commanded_worldname) {
596 dstream << _("Using world specified by --worldname on the "
597 "command line") << std::endl;
598 commanded_world = worldspec.path;
604 dstream << _("World") << " '" << commanded_worldname
605 << _("' not available. Available worlds:") << std::endl;
606 print_worldspecs(worldspecs, dstream);
610 game_params->world_path = get_clean_world_path(commanded_world);
611 return !commanded_world.empty();
614 if (cmd_args.exists("world"))
615 commanded_world = cmd_args.get("world");
616 else if (cmd_args.exists("map-dir"))
617 commanded_world = cmd_args.get("map-dir");
618 else if (cmd_args.exists("nonopt0")) // First nameless argument
619 commanded_world = cmd_args.get("nonopt0");
621 game_params->world_path = get_clean_world_path(commanded_world);
622 return !commanded_world.empty();
625 static bool get_world_from_config(GameParams *game_params, const Settings &cmd_args)
628 std::string commanded_world;
630 if (g_settings->exists("map-dir"))
631 commanded_world = g_settings->get("map-dir");
633 game_params->world_path = get_clean_world_path(commanded_world);
635 return !commanded_world.empty();
638 static bool auto_select_world(GameParams *game_params)
640 // No world was specified; try to select it automatically
641 // Get information about available worlds
643 verbosestream << _("Determining world path") << std::endl;
645 std::vector<WorldSpec> worldspecs = getAvailableWorlds();
646 std::string world_path;
648 // If there is only a single world, use it
649 if (worldspecs.size() == 1) {
650 world_path = worldspecs[0].path;
651 dstream <<_("Automatically selecting world at") << " ["
652 << world_path << "]" << std::endl;
653 // If there are multiple worlds, list them
654 } else if (worldspecs.size() > 1 && game_params->is_dedicated_server) {
655 std::cerr << _("Multiple worlds are available.") << std::endl;
656 std::cerr << _("Please select one using --worldname <name>"
657 " or --world <path>") << std::endl;
658 print_worldspecs(worldspecs, std::cerr);
660 // If there are no worlds, automatically create a new one
662 // This is the ultimate default world path
663 world_path = porting::path_user + DIR_DELIM + "worlds" +
665 infostream << "Creating default world at ["
666 << world_path << "]" << std::endl;
669 assert(world_path != ""); // Post-condition
670 game_params->world_path = world_path;
674 static std::string get_clean_world_path(const std::string &path)
676 const std::string worldmt = "world.mt";
677 std::string clean_path;
679 if (path.size() > worldmt.size()
680 && path.substr(path.size() - worldmt.size()) == worldmt) {
681 dstream << _("Supplied world.mt file - stripping it off.") << std::endl;
682 clean_path = path.substr(0, path.size() - worldmt.size());
690 static bool game_configure_subgame(GameParams *game_params, const Settings &cmd_args)
694 success = get_game_from_cmdline(game_params, cmd_args);
696 success = determine_subgame(game_params);
701 static bool get_game_from_cmdline(GameParams *game_params, const Settings &cmd_args)
703 SubgameSpec commanded_gamespec;
705 if (cmd_args.exists("gameid")) {
706 std::string gameid = cmd_args.get("gameid");
707 commanded_gamespec = findSubgame(gameid);
708 if (!commanded_gamespec.isValid()) {
709 errorstream << "Game \"" << gameid << "\" not found" << std::endl;
712 dstream << _("Using game specified by --gameid on the command line")
714 game_params->game_spec = commanded_gamespec;
721 static bool determine_subgame(GameParams *game_params)
723 SubgameSpec gamespec;
725 assert(game_params->world_path != ""); // Pre-condition
727 verbosestream << _("Determining gameid/gamespec") << std::endl;
728 // If world doesn't exist
729 if (!game_params->world_path.empty()
730 && !getWorldExists(game_params->world_path)) {
731 // Try to take gamespec from command line
732 if (game_params->game_spec.isValid()) {
733 gamespec = game_params->game_spec;
734 infostream << "Using commanded gameid [" << gamespec.id << "]" << std::endl;
735 } else { // Otherwise we will be using "minetest"
736 gamespec = findSubgame(g_settings->get("default_game"));
737 infostream << "Using default gameid [" << gamespec.id << "]" << std::endl;
738 if (!gamespec.isValid()) {
739 errorstream << "Subgame specified in default_game ["
740 << g_settings->get("default_game")
741 << "] is invalid." << std::endl;
745 } else { // World exists
746 std::string world_gameid = getWorldGameId(game_params->world_path, false);
747 // If commanded to use a gameid, do so
748 if (game_params->game_spec.isValid()) {
749 gamespec = game_params->game_spec;
750 if (game_params->game_spec.id != world_gameid) {
751 warningstream << "Using commanded gameid ["
752 << gamespec.id << "]" << " instead of world gameid ["
753 << world_gameid << "]" << std::endl;
756 // If world contains an embedded game, use it;
757 // Otherwise find world from local system.
758 gamespec = findWorldSubgame(game_params->world_path);
759 infostream << "Using world gameid [" << gamespec.id << "]" << std::endl;
763 if (!gamespec.isValid()) {
764 errorstream << "Subgame [" << gamespec.id << "] could not be found."
769 game_params->game_spec = gamespec;
774 /*****************************************************************************
776 *****************************************************************************/
777 static bool run_dedicated_server(const GameParams &game_params, const Settings &cmd_args)
779 verbosestream << _("Using world path") << " ["
780 << game_params.world_path << "]" << std::endl;
781 verbosestream << _("Using gameid") << " ["
782 << game_params.game_spec.id << "]" << std::endl;
785 std::string bind_str = g_settings->get("bind_address");
786 Address bind_addr(0, 0, 0, 0, game_params.socket_port);
788 if (g_settings->getBool("ipv6_server")) {
789 bind_addr.setAddress((IPv6AddressBytes*) NULL);
792 bind_addr.Resolve(bind_str.c_str());
793 } catch (ResolveError &e) {
794 infostream << "Resolving bind address \"" << bind_str
795 << "\" failed: " << e.what()
796 << " -- Listening on all addresses." << std::endl;
798 if (bind_addr.isIPv6() && !g_settings->getBool("enable_ipv6")) {
799 errorstream << "Unable to listen on "
800 << bind_addr.serializeString()
801 << L" because IPv6 is disabled" << std::endl;
805 // Database migration
806 if (cmd_args.exists("migrate"))
807 return migrate_map_database(game_params, cmd_args);
809 if (cmd_args.exists("migrate-players"))
810 return ServerEnvironment::migratePlayersDatabase(game_params, cmd_args);
812 if (cmd_args.exists("terminal")) {
815 std::string admin_nick = g_settings->get("name");
817 name_ok = name_ok && !admin_nick.empty();
818 name_ok = name_ok && string_allowed(admin_nick, PLAYERNAME_ALLOWED_CHARS);
821 if (admin_nick.empty()) {
822 errorstream << "No name given for admin. "
823 << "Please check your minetest.conf that it "
824 << "contains a 'name = ' to your main admin account."
827 errorstream << "Name for admin '"
828 << admin_nick << "' is not valid. "
829 << "Please check that it only contains allowed characters. "
830 << "Valid characters are: " << PLAYERNAME_ALLOWED_CHARS_USER_EXPL
836 bool &kill = *porting::signal_handler_killstatus();
840 Server server(game_params.world_path, game_params.game_spec,
841 false, bind_addr, true, &iface);
843 g_term_console.setup(&iface, &kill, admin_nick);
845 g_term_console.start();
849 dedicated_server_loop(server, kill);
850 } catch (const ModError &e) {
851 g_term_console.stopAndWaitforThread();
852 errorstream << "ModError: " << e.what() << std::endl;
854 } catch (const ServerError &e) {
855 g_term_console.stopAndWaitforThread();
856 errorstream << "ServerError: " << e.what() << std::endl;
860 // Tell the console to stop, and wait for it to finish,
861 // only then leave context and free iface
862 g_term_console.stop();
863 g_term_console.wait();
865 g_term_console.clearKillStatus();
868 errorstream << "Cmd arg --terminal passed, but "
869 << "compiled without ncurses. Ignoring." << std::endl;
874 Server server(game_params.world_path, game_params.game_spec, false,
879 bool &kill = *porting::signal_handler_killstatus();
880 dedicated_server_loop(server, kill);
882 } catch (const ModError &e) {
883 errorstream << "ModError: " << e.what() << std::endl;
885 } catch (const ServerError &e) {
886 errorstream << "ServerError: " << e.what() << std::endl;
894 static bool migrate_map_database(const GameParams &game_params, const Settings &cmd_args)
896 std::string migrate_to = cmd_args.get("migrate");
898 std::string world_mt_path = game_params.world_path + DIR_DELIM + "world.mt";
899 if (!world_mt.readConfigFile(world_mt_path.c_str())) {
900 errorstream << "Cannot read world.mt!" << std::endl;
904 if (!world_mt.exists("backend")) {
905 errorstream << "Please specify your current backend in world.mt:"
907 << " backend = {sqlite3|leveldb|redis|dummy|postgresql}"
912 std::string backend = world_mt.get("backend");
913 if (backend == migrate_to) {
914 errorstream << "Cannot migrate: new backend is same"
915 << " as the old one" << std::endl;
919 MapDatabase *old_db = ServerMap::createDatabase(backend, game_params.world_path, world_mt),
920 *new_db = ServerMap::createDatabase(migrate_to, game_params.world_path, world_mt);
923 time_t last_update_time = 0;
924 bool &kill = *porting::signal_handler_killstatus();
926 std::vector<v3s16> blocks;
927 old_db->listAllLoadableBlocks(blocks);
929 for (std::vector<v3s16>::const_iterator it = blocks.begin(); it != blocks.end(); ++it) {
930 if (kill) return false;
933 old_db->loadBlock(*it, &data);
935 new_db->saveBlock(*it, data);
937 errorstream << "Failed to load block " << PP(*it) << ", skipping it." << std::endl;
939 if (++count % 0xFF == 0 && time(NULL) - last_update_time >= 1) {
940 std::cerr << " Migrated " << count << " blocks, "
941 << (100.0 * count / blocks.size()) << "% completed.\r";
944 last_update_time = time(NULL);
947 std::cerr << std::endl;
952 actionstream << "Successfully migrated " << count << " blocks" << std::endl;
953 world_mt.set("backend", migrate_to);
954 if (!world_mt.updateConfigFile(world_mt_path.c_str()))
955 errorstream << "Failed to update world.mt!" << std::endl;
957 actionstream << "world.mt updated" << std::endl;