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.
21 #ifndef SERVER // Dedicated server isn't linked with Irrlicht
22 #pragma comment(lib, "Irrlicht.lib")
23 // This would get rid of the console window
24 //#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
26 #pragma comment(lib, "zlibwapi.lib")
27 #pragma comment(lib, "Shell32.lib")
30 #include "irrlicht.h" // createDevice
32 #include "mainmenumanager.h"
33 #include "irrlichttypes_extrabloated.h"
35 #include "unittest/test.h"
39 #include "guiMainMenu.h"
41 #include "defaultsettings.h"
45 #include "quicktune.h"
46 #include "httpfetch.h"
47 #include "guiEngine.h"
49 #include "mapsector.h"
50 #include "fontengine.h"
51 #include "gameparams.h"
54 #include "client/clientlauncher.h"
57 #ifdef HAVE_TOUCHSCREENGUI
58 #include "touchscreengui.h"
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(int *log_level, 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_debug_streams(int *log_level, 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_database(const GameParams &game_params, const Settings &cmd_args);
105 /**********************************************************************/
108 gettime.h implementation
115 /* Use imprecise system calls directly (from porting.h) */
116 return porting::getTime(PRECISION_MILLI);
119 u32 getTime(TimePrecision prec)
121 return porting::getTime(prec);
126 class StderrLogOutput: public ILogOutput
129 /* line: Full line with timestamp, level and thread */
130 void printLog(const std::string &line)
132 std::cerr << line << std::endl;
134 } main_stderr_log_out;
136 class DstreamNoStderrLogOutput: public ILogOutput
139 /* line: Full line with timestamp, level and thread */
140 void printLog(const std::string &line)
142 dstream_no_stderr << line << std::endl;
144 } main_dstream_no_stderr_log_out;
146 static OptionList allowed_options;
148 int main(int argc, char *argv[])
152 debug_set_exception_handler();
154 log_add_output_maxlev(&main_stderr_log_out, LMT_ACTION);
155 log_add_output_all_levs(&main_dstream_no_stderr_log_out);
157 log_register_thread("main");
160 bool cmd_args_ok = get_cmdline_opts(argc, argv, &cmd_args);
162 || cmd_args.getFlag("help")
163 || cmd_args.exists("nonopt1")) {
164 print_help(allowed_options);
165 return cmd_args_ok ? 0 : 1;
168 if (cmd_args.getFlag("version")) {
173 setup_log_params(cmd_args);
175 porting::signal_handler_init();
176 porting::initializePaths();
178 if (!create_userdata_path()) {
179 errorstream << "Cannot create user data directory" << std::endl;
183 // Initialize debug stacks
185 DSTACK(__FUNCTION_NAME);
188 BEGIN_DEBUG_EXCEPTION_HANDLER
190 // List gameids if requested
191 if (cmd_args.exists("gameid") && cmd_args.get("gameid") == "list") {
196 // List worlds if requested
197 if (cmd_args.exists("world") && cmd_args.get("world") == "list") {
202 GameParams game_params;
203 if (!init_common(&game_params.log_level, cmd_args, argc, argv))
208 if (cmd_args.getFlag("run-unittests")) {
215 game_params.is_dedicated_server = true;
217 game_params.is_dedicated_server = cmd_args.getFlag("server");
220 if (!game_configure(&game_params, cmd_args))
223 sanity_check(game_params.world_path != "");
225 infostream << "Using commanded world path ["
226 << game_params.world_path << "]" << std::endl;
228 //Run dedicated server if asked to or no other option
229 g_settings->set("server_dedicated",
230 game_params.is_dedicated_server ? "true" : "false");
232 if (game_params.is_dedicated_server)
233 return run_dedicated_server(game_params, cmd_args) ? 0 : 1;
236 ClientLauncher launcher;
237 retval = launcher.run(game_params, cmd_args) ? 0 : 1;
242 // Update configuration file
243 if (g_settings_path != "")
244 g_settings->updateConfigFile(g_settings_path.c_str());
246 print_modified_quicktune_values();
248 // Stop httpfetch thread (if started)
251 END_DEBUG_EXCEPTION_HANDLER(errorstream)
257 /*****************************************************************************
259 *****************************************************************************/
262 static bool get_cmdline_opts(int argc, char *argv[], Settings *cmd_args)
264 set_allowed_options(&allowed_options);
266 return cmd_args->parseCommandLine(argc, argv, allowed_options);
269 static void set_allowed_options(OptionList *allowed_options)
271 allowed_options->clear();
273 allowed_options->insert(std::make_pair("help", ValueSpec(VALUETYPE_FLAG,
274 _("Show allowed options"))));
275 allowed_options->insert(std::make_pair("version", ValueSpec(VALUETYPE_FLAG,
276 _("Show version information"))));
277 allowed_options->insert(std::make_pair("config", ValueSpec(VALUETYPE_STRING,
278 _("Load configuration from specified file"))));
279 allowed_options->insert(std::make_pair("port", ValueSpec(VALUETYPE_STRING,
280 _("Set network port (UDP)"))));
281 allowed_options->insert(std::make_pair("run-unittests", ValueSpec(VALUETYPE_FLAG,
282 _("Run the unit tests and exit"))));
283 allowed_options->insert(std::make_pair("map-dir", ValueSpec(VALUETYPE_STRING,
284 _("Same as --world (deprecated)"))));
285 allowed_options->insert(std::make_pair("world", ValueSpec(VALUETYPE_STRING,
286 _("Set world path (implies local game) ('list' lists all)"))));
287 allowed_options->insert(std::make_pair("worldname", ValueSpec(VALUETYPE_STRING,
288 _("Set world by name (implies local game)"))));
289 allowed_options->insert(std::make_pair("quiet", ValueSpec(VALUETYPE_FLAG,
290 _("Print to console errors only"))));
291 allowed_options->insert(std::make_pair("info", ValueSpec(VALUETYPE_FLAG,
292 _("Print more information to console"))));
293 allowed_options->insert(std::make_pair("verbose", ValueSpec(VALUETYPE_FLAG,
294 _("Print even more information to console"))));
295 allowed_options->insert(std::make_pair("trace", ValueSpec(VALUETYPE_FLAG,
296 _("Print enormous amounts of information to log and console"))));
297 allowed_options->insert(std::make_pair("logfile", ValueSpec(VALUETYPE_STRING,
298 _("Set logfile path ('' = no logging)"))));
299 allowed_options->insert(std::make_pair("gameid", ValueSpec(VALUETYPE_STRING,
300 _("Set gameid (\"--gameid list\" prints available ones)"))));
301 allowed_options->insert(std::make_pair("migrate", ValueSpec(VALUETYPE_STRING,
302 _("Migrate from current map backend to another (Only works when using minetestserver or with --server)"))));
304 allowed_options->insert(std::make_pair("videomodes", ValueSpec(VALUETYPE_FLAG,
305 _("Show available video modes"))));
306 allowed_options->insert(std::make_pair("speedtests", ValueSpec(VALUETYPE_FLAG,
307 _("Run speed tests"))));
308 allowed_options->insert(std::make_pair("address", ValueSpec(VALUETYPE_STRING,
309 _("Address to connect to. ('' = local game)"))));
310 allowed_options->insert(std::make_pair("random-input", ValueSpec(VALUETYPE_FLAG,
311 _("Enable random user input, for testing"))));
312 allowed_options->insert(std::make_pair("server", ValueSpec(VALUETYPE_FLAG,
313 _("Run dedicated server"))));
314 allowed_options->insert(std::make_pair("name", ValueSpec(VALUETYPE_STRING,
315 _("Set player name"))));
316 allowed_options->insert(std::make_pair("password", ValueSpec(VALUETYPE_STRING,
317 _("Set password"))));
318 allowed_options->insert(std::make_pair("go", ValueSpec(VALUETYPE_FLAG,
319 _("Disable main menu"))));
324 static void print_help(const OptionList &allowed_options)
326 dstream << _("Allowed options:") << std::endl;
327 print_allowed_options(allowed_options);
330 static void print_allowed_options(const OptionList &allowed_options)
332 for (OptionList::const_iterator i = allowed_options.begin();
333 i != allowed_options.end(); ++i) {
334 std::ostringstream os1(std::ios::binary);
335 os1 << " --" << i->first;
336 if (i->second.type != VALUETYPE_FLAG)
337 os1 << _(" <value>");
339 dstream << padStringRight(os1.str(), 24);
341 if (i->second.help != NULL)
342 dstream << i->second.help;
344 dstream << std::endl;
348 static void print_version()
350 dstream << PROJECT_NAME " " << g_version_hash << std::endl;
352 dstream << "Using Irrlicht " << IRRLICHT_SDK_VERSION << std::endl;
354 dstream << "Build info: " << g_build_info << std::endl;
357 static void list_game_ids()
359 std::set<std::string> gameids = getAvailableGameIds();
360 for (std::set<std::string>::const_iterator i = gameids.begin();
361 i != gameids.end(); i++)
362 dstream << (*i) <<std::endl;
365 static void list_worlds()
367 dstream << _("Available worlds:") << std::endl;
368 std::vector<WorldSpec> worldspecs = getAvailableWorlds();
369 print_worldspecs(worldspecs, dstream);
372 static void print_worldspecs(const std::vector<WorldSpec> &worldspecs,
375 for (size_t i = 0; i < worldspecs.size(); i++) {
376 std::string name = worldspecs[i].name;
377 std::string path = worldspecs[i].path;
378 if (name.find(" ") != std::string::npos)
379 name = std::string("'") + name + "'";
380 path = std::string("'") + path + "'";
381 name = padStringRight(name, 14);
382 os << " " << name << " " << path << std::endl;
386 static void print_modified_quicktune_values()
388 bool header_printed = false;
389 std::vector<std::string> names = getQuicktuneNames();
391 for (u32 i = 0; i < names.size(); i++) {
392 QuicktuneValue val = getQuicktuneValue(names[i]);
395 if (!header_printed) {
396 dstream << "Modified quicktune values:" << std::endl;
397 header_printed = true;
399 dstream << names[i] << " = " << val.getString() << std::endl;
403 static void setup_log_params(const Settings &cmd_args)
405 // Quiet mode, print errors only
406 if (cmd_args.getFlag("quiet")) {
407 log_remove_output(&main_stderr_log_out);
408 log_add_output_maxlev(&main_stderr_log_out, LMT_ERROR);
411 // If trace is enabled, enable logging of certain things
412 if (cmd_args.getFlag("trace")) {
413 dstream << _("Enabling trace level debug output") << std::endl;
414 log_trace_level_enabled = true;
415 dout_con_ptr = &verbosestream; // this is somewhat old crap
416 socket_enable_debug_output = true; // socket doesn't use log.h
419 // In certain cases, output info level on stderr
420 if (cmd_args.getFlag("info") || cmd_args.getFlag("verbose") ||
421 cmd_args.getFlag("trace") || cmd_args.getFlag("speedtests"))
422 log_add_output(&main_stderr_log_out, LMT_INFO);
424 // In certain cases, output verbose level on stderr
425 if (cmd_args.getFlag("verbose") || cmd_args.getFlag("trace"))
426 log_add_output(&main_stderr_log_out, LMT_VERBOSE);
429 static bool create_userdata_path()
434 porting::initAndroid();
436 porting::setExternalStorageDir(porting::jnienv);
437 if (!fs::PathExists(porting::path_user)) {
438 success = fs::CreateDir(porting::path_user);
442 porting::copyAssets();
444 // Create user data directory
445 success = fs::CreateDir(porting::path_user);
448 infostream << "path_share = " << porting::path_share << std::endl;
449 infostream << "path_user = " << porting::path_user << std::endl;
454 static bool init_common(int *log_level, const Settings &cmd_args, int argc, char *argv[])
457 set_default_settings(g_settings);
459 // Initialize sockets
461 atexit(sockets_cleanup);
463 if (!read_config_file(cmd_args))
466 init_debug_streams(log_level, cmd_args);
468 // Initialize random seed
472 // Initialize HTTP fetcher
473 httpfetch_init(g_settings->getS32("curl_parallel_limit"));
476 init_gettext((porting::path_share + DIR_DELIM + "locale").c_str(),
477 g_settings->get("language"), argc, argv);
479 init_gettext((porting::path_share + DIR_DELIM + "locale").c_str(),
480 g_settings->get("language"));
486 static void startup_message()
488 infostream << PROJECT_NAME << " " << _("with")
489 << " SER_FMT_VER_HIGHEST_READ="
490 << (int)SER_FMT_VER_HIGHEST_READ << ", "
491 << g_build_info << std::endl;
494 static bool read_config_file(const Settings &cmd_args)
496 // Path of configuration file in use
497 sanity_check(g_settings_path == ""); // Sanity check
499 if (cmd_args.exists("config")) {
500 bool r = g_settings->readConfigFile(cmd_args.get("config").c_str());
502 errorstream << "Could not read configuration from \""
503 << cmd_args.get("config") << "\"" << std::endl;
506 g_settings_path = cmd_args.get("config");
508 std::vector<std::string> filenames;
509 filenames.push_back(porting::path_user + DIR_DELIM + "minetest.conf");
510 // Legacy configuration file location
511 filenames.push_back(porting::path_user +
512 DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
515 // Try also from a lower level (to aid having the same configuration
516 // for many RUN_IN_PLACE installs)
517 filenames.push_back(porting::path_user +
518 DIR_DELIM + ".." + DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
521 for (size_t i = 0; i < filenames.size(); i++) {
522 bool r = g_settings->readConfigFile(filenames[i].c_str());
524 g_settings_path = filenames[i];
529 // If no path found, use the first one (menu creates the file)
530 if (g_settings_path == "")
531 g_settings_path = filenames[0];
537 static void init_debug_streams(int *log_level, const Settings &cmd_args)
540 std::string logfile = DEBUGFILE;
542 std::string logfile = porting::path_user + DIR_DELIM + DEBUGFILE;
544 if (cmd_args.exists("logfile"))
545 logfile = cmd_args.get("logfile");
547 log_remove_output(&main_dstream_no_stderr_log_out);
548 *log_level = g_settings->getS32("debug_log_level");
550 if (*log_level == 0) //no logging
552 if (*log_level < 0) {
553 dstream << "WARNING: Supplied debug_log_level < 0; Using 0" << std::endl;
555 } else if (*log_level > LMT_NUM_VALUES) {
556 dstream << "WARNING: Supplied debug_log_level > " << LMT_NUM_VALUES
557 << "; Using " << LMT_NUM_VALUES << std::endl;
558 *log_level = LMT_NUM_VALUES;
561 log_add_output_maxlev(&main_dstream_no_stderr_log_out,
562 (LogMessageLevel)(*log_level - 1));
564 debugstreams_init(false, logfile == "" ? NULL : logfile.c_str());
566 infostream << "logfile = " << logfile << std::endl;
568 atexit(debugstreams_deinit);
571 static bool game_configure(GameParams *game_params, const Settings &cmd_args)
573 game_configure_port(game_params, cmd_args);
575 if (!game_configure_world(game_params, cmd_args)) {
576 errorstream << "No world path specified or found." << std::endl;
580 game_configure_subgame(game_params, cmd_args);
585 static void game_configure_port(GameParams *game_params, const Settings &cmd_args)
587 if (cmd_args.exists("port"))
588 game_params->socket_port = cmd_args.getU16("port");
590 game_params->socket_port = g_settings->getU16("port");
592 if (game_params->socket_port == 0)
593 game_params->socket_port = DEFAULT_SERVER_PORT;
596 static bool game_configure_world(GameParams *game_params, const Settings &cmd_args)
598 if (get_world_from_cmdline(game_params, cmd_args))
600 if (get_world_from_config(game_params, cmd_args))
603 return auto_select_world(game_params);
606 static bool get_world_from_cmdline(GameParams *game_params, const Settings &cmd_args)
608 std::string commanded_world = "";
611 std::string commanded_worldname = "";
612 if (cmd_args.exists("worldname"))
613 commanded_worldname = cmd_args.get("worldname");
615 // If a world name was specified, convert it to a path
616 if (commanded_worldname != "") {
617 // Get information about available worlds
618 std::vector<WorldSpec> worldspecs = getAvailableWorlds();
620 for (u32 i = 0; i < worldspecs.size(); i++) {
621 std::string name = worldspecs[i].name;
622 if (name == commanded_worldname) {
623 dstream << _("Using world specified by --worldname on the "
624 "command line") << std::endl;
625 commanded_world = worldspecs[i].path;
631 dstream << _("World") << " '" << commanded_worldname
632 << _("' not available. Available worlds:") << std::endl;
633 print_worldspecs(worldspecs, dstream);
637 game_params->world_path = get_clean_world_path(commanded_world);
638 return commanded_world != "";
641 if (cmd_args.exists("world"))
642 commanded_world = cmd_args.get("world");
643 else if (cmd_args.exists("map-dir"))
644 commanded_world = cmd_args.get("map-dir");
645 else if (cmd_args.exists("nonopt0")) // First nameless argument
646 commanded_world = cmd_args.get("nonopt0");
648 game_params->world_path = get_clean_world_path(commanded_world);
649 return commanded_world != "";
652 static bool get_world_from_config(GameParams *game_params, const Settings &cmd_args)
655 std::string commanded_world = "";
657 if (g_settings->exists("map-dir"))
658 commanded_world = g_settings->get("map-dir");
660 game_params->world_path = get_clean_world_path(commanded_world);
662 return commanded_world != "";
665 static bool auto_select_world(GameParams *game_params)
667 // No world was specified; try to select it automatically
668 // Get information about available worlds
670 verbosestream << _("Determining world path") << std::endl;
672 std::vector<WorldSpec> worldspecs = getAvailableWorlds();
673 std::string world_path;
675 // If there is only a single world, use it
676 if (worldspecs.size() == 1) {
677 world_path = worldspecs[0].path;
678 dstream <<_("Automatically selecting world at") << " ["
679 << world_path << "]" << std::endl;
680 // If there are multiple worlds, list them
681 } else if (worldspecs.size() > 1 && game_params->is_dedicated_server) {
682 dstream << _("Multiple worlds are available.") << std::endl;
683 dstream << _("Please select one using --worldname <name>"
684 " or --world <path>") << std::endl;
685 print_worldspecs(worldspecs, dstream);
687 // If there are no worlds, automatically create a new one
689 // This is the ultimate default world path
690 world_path = porting::path_user + DIR_DELIM + "worlds" +
692 infostream << "Creating default world at ["
693 << world_path << "]" << std::endl;
696 assert(world_path != ""); // Post-condition
697 game_params->world_path = world_path;
701 static std::string get_clean_world_path(const std::string &path)
703 const std::string worldmt = "world.mt";
704 std::string clean_path;
706 if (path.size() > worldmt.size()
707 && path.substr(path.size() - worldmt.size()) == worldmt) {
708 dstream << _("Supplied world.mt file - stripping it off.") << std::endl;
709 clean_path = path.substr(0, path.size() - worldmt.size());
717 static bool game_configure_subgame(GameParams *game_params, const Settings &cmd_args)
721 success = get_game_from_cmdline(game_params, cmd_args);
723 success = determine_subgame(game_params);
728 static bool get_game_from_cmdline(GameParams *game_params, const Settings &cmd_args)
730 SubgameSpec commanded_gamespec;
732 if (cmd_args.exists("gameid")) {
733 std::string gameid = cmd_args.get("gameid");
734 commanded_gamespec = findSubgame(gameid);
735 if (!commanded_gamespec.isValid()) {
736 errorstream << "Game \"" << gameid << "\" not found" << std::endl;
739 dstream << _("Using game specified by --gameid on the command line")
741 game_params->game_spec = commanded_gamespec;
748 static bool determine_subgame(GameParams *game_params)
750 SubgameSpec gamespec;
752 assert(game_params->world_path != ""); // Pre-condition
754 verbosestream << _("Determining gameid/gamespec") << std::endl;
755 // If world doesn't exist
756 if (game_params->world_path != ""
757 && !getWorldExists(game_params->world_path)) {
758 // Try to take gamespec from command line
759 if (game_params->game_spec.isValid()) {
760 gamespec = game_params->game_spec;
761 infostream << "Using commanded gameid [" << gamespec.id << "]" << std::endl;
762 } else { // Otherwise we will be using "minetest"
763 gamespec = findSubgame(g_settings->get("default_game"));
764 infostream << "Using default gameid [" << gamespec.id << "]" << std::endl;
765 if (!gamespec.isValid()) {
766 errorstream << "Subgame specified in default_game ["
767 << g_settings->get("default_game")
768 << "] is invalid." << std::endl;
772 } else { // World exists
773 std::string world_gameid = getWorldGameId(game_params->world_path, false);
774 // If commanded to use a gameid, do so
775 if (game_params->game_spec.isValid()) {
776 gamespec = game_params->game_spec;
777 if (game_params->game_spec.id != world_gameid) {
778 errorstream << "WARNING: Using commanded gameid ["
779 << gamespec.id << "]" << " instead of world gameid ["
780 << world_gameid << "]" << std::endl;
783 // If world contains an embedded game, use it;
784 // Otherwise find world from local system.
785 gamespec = findWorldSubgame(game_params->world_path);
786 infostream << "Using world gameid [" << gamespec.id << "]" << std::endl;
790 if (!gamespec.isValid()) {
791 errorstream << "Subgame [" << gamespec.id << "] could not be found."
796 game_params->game_spec = gamespec;
801 /*****************************************************************************
803 *****************************************************************************/
804 static bool run_dedicated_server(const GameParams &game_params, const Settings &cmd_args)
806 DSTACK("Dedicated server branch");
808 verbosestream << _("Using world path") << " ["
809 << game_params.world_path << "]" << std::endl;
810 verbosestream << _("Using gameid") << " ["
811 << game_params.game_spec.id << "]" << std::endl;
814 std::string bind_str = g_settings->get("bind_address");
815 Address bind_addr(0, 0, 0, 0, game_params.socket_port);
817 if (g_settings->getBool("ipv6_server")) {
818 bind_addr.setAddress((IPv6AddressBytes*) NULL);
821 bind_addr.Resolve(bind_str.c_str());
822 } catch (ResolveError &e) {
823 infostream << "Resolving bind address \"" << bind_str
824 << "\" failed: " << e.what()
825 << " -- Listening on all addresses." << std::endl;
827 if (bind_addr.isIPv6() && !g_settings->getBool("enable_ipv6")) {
828 errorstream << "Unable to listen on "
829 << bind_addr.serializeString()
830 << L" because IPv6 is disabled" << std::endl;
834 // Database migration
835 if (cmd_args.exists("migrate"))
836 return migrate_database(game_params, cmd_args);
839 Server server(game_params.world_path,
840 game_params.game_spec, false, bind_addr.isIPv6());
842 server.start(bind_addr);
845 bool &kill = *porting::signal_handler_killstatus();
846 dedicated_server_loop(server, kill);
851 static bool migrate_database(const GameParams &game_params, const Settings &cmd_args)
853 std::string migrate_to = cmd_args.get("migrate");
855 std::string world_mt_path = game_params.world_path + DIR_DELIM + "world.mt";
856 if (!world_mt.readConfigFile(world_mt_path.c_str())) {
857 errorstream << "Cannot read world.mt!" << std::endl;
860 if (!world_mt.exists("backend")) {
861 errorstream << "Please specify your current backend in world.mt:"
863 << " backend = {sqlite3|leveldb|redis|dummy}"
867 std::string backend = world_mt.get("backend");
868 if (backend == migrate_to) {
869 errorstream << "Cannot migrate: new backend is same"
870 << " as the old one" << std::endl;
873 Database *old_db = ServerMap::createDatabase(backend, game_params.world_path, world_mt),
874 *new_db = ServerMap::createDatabase(migrate_to, game_params.world_path, world_mt);
877 time_t last_update_time = 0;
878 bool &kill = *porting::signal_handler_killstatus();
880 std::vector<v3s16> blocks;
881 old_db->listAllLoadableBlocks(blocks);
883 for (std::vector<v3s16>::const_iterator it = blocks.begin(); it != blocks.end(); ++it) {
884 if (kill) return false;
886 const std::string &data = old_db->loadBlock(*it);
888 new_db->saveBlock(*it, data);
890 errorstream << "Failed to load block " << PP(*it) << ", skipping it." << std::endl;
892 if (++count % 0xFF == 0 && time(NULL) - last_update_time >= 1) {
893 std::cerr << " Migrated " << count << " blocks, "
894 << (100.0 * count / blocks.size()) << "% completed.\r";
897 last_update_time = time(NULL);
900 std::cerr << std::endl;
905 actionstream << "Successfully migrated " << count << " blocks" << std::endl;
906 world_mt.set("backend", migrate_to);
907 if (!world_mt.updateConfigFile(world_mt_path.c_str()))
908 errorstream << "Failed to update world.mt!" << std::endl;
910 actionstream << "world.mt updated" << std::endl;