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"
44 #include "quicktune.h"
45 #include "httpfetch.h"
46 #include "guiEngine.h"
48 #include "mapsector.h"
49 #include "fontengine.h"
50 #include "gameparams.h"
53 #include "client/clientlauncher.h"
56 #ifdef HAVE_TOUCHSCREENGUI
57 #include "touchscreengui.h"
60 #define DEBUGFILE "debug.txt"
61 #define DEFAULT_SERVER_PORT 30000
63 typedef std::map<std::string, ValueSpec> OptionList;
65 /**********************************************************************
67 **********************************************************************/
69 static bool get_cmdline_opts(int argc, char *argv[], Settings *cmd_args);
70 static void set_allowed_options(OptionList *allowed_options);
72 static void print_help(const OptionList &allowed_options);
73 static void print_allowed_options(const OptionList &allowed_options);
74 static void print_version();
75 static void print_worldspecs(const std::vector<WorldSpec> &worldspecs,
77 static void print_modified_quicktune_values();
79 static void list_game_ids();
80 static void list_worlds();
81 static void setup_log_params(const Settings &cmd_args);
82 static bool create_userdata_path();
83 static bool init_common(const Settings &cmd_args, int argc, char *argv[]);
84 static void startup_message();
85 static bool read_config_file(const Settings &cmd_args);
86 static void init_log_streams(const Settings &cmd_args);
88 static bool game_configure(GameParams *game_params, const Settings &cmd_args);
89 static void game_configure_port(GameParams *game_params, const Settings &cmd_args);
91 static bool game_configure_world(GameParams *game_params, const Settings &cmd_args);
92 static bool get_world_from_cmdline(GameParams *game_params, const Settings &cmd_args);
93 static bool get_world_from_config(GameParams *game_params, const Settings &cmd_args);
94 static bool auto_select_world(GameParams *game_params);
95 static std::string get_clean_world_path(const std::string &path);
97 static bool game_configure_subgame(GameParams *game_params, const Settings &cmd_args);
98 static bool get_game_from_cmdline(GameParams *game_params, const Settings &cmd_args);
99 static bool determine_subgame(GameParams *game_params);
101 static bool run_dedicated_server(const GameParams &game_params, const Settings &cmd_args);
102 static bool migrate_database(const GameParams &game_params, const Settings &cmd_args);
104 /**********************************************************************/
107 gettime.h implementation
114 /* Use imprecise system calls directly (from porting.h) */
115 return porting::getTime(PRECISION_MILLI);
118 u32 getTime(TimePrecision prec)
120 return porting::getTime(prec);
125 FileLogOutput file_log_output;
127 static OptionList allowed_options;
129 int main(int argc, char *argv[])
133 debug_set_exception_handler();
135 g_logger.registerThread("Main");
136 g_logger.addOutputMaxLevel(&stderr_output, LL_ACTION);
139 bool cmd_args_ok = get_cmdline_opts(argc, argv, &cmd_args);
141 || cmd_args.getFlag("help")
142 || cmd_args.exists("nonopt1")) {
143 print_help(allowed_options);
144 return cmd_args_ok ? 0 : 1;
147 if (cmd_args.getFlag("version")) {
152 setup_log_params(cmd_args);
154 porting::signal_handler_init();
155 porting::initializePaths();
157 if (!create_userdata_path()) {
158 errorstream << "Cannot create user data directory" << std::endl;
162 // Initialize debug stacks
163 DSTACK(FUNCTION_NAME);
166 BEGIN_DEBUG_EXCEPTION_HANDLER
168 // List gameids if requested
169 if (cmd_args.exists("gameid") && cmd_args.get("gameid") == "list") {
174 // List worlds if requested
175 if (cmd_args.exists("world") && cmd_args.get("world") == "list") {
180 if (!init_common(cmd_args, argc, argv))
185 if (cmd_args.getFlag("run-unittests")) {
191 GameParams game_params;
193 game_params.is_dedicated_server = true;
195 game_params.is_dedicated_server = cmd_args.getFlag("server");
198 if (!game_configure(&game_params, cmd_args))
201 sanity_check(!game_params.world_path.empty());
203 infostream << "Using commanded world path ["
204 << game_params.world_path << "]" << std::endl;
206 //Run dedicated server if asked to or no other option
207 g_settings->set("server_dedicated",
208 game_params.is_dedicated_server ? "true" : "false");
210 if (game_params.is_dedicated_server)
211 return run_dedicated_server(game_params, cmd_args) ? 0 : 1;
214 ClientLauncher launcher;
215 retval = launcher.run(game_params, cmd_args) ? 0 : 1;
220 // Update configuration file
221 if (g_settings_path != "")
222 g_settings->updateConfigFile(g_settings_path.c_str());
224 print_modified_quicktune_values();
226 // Stop httpfetch thread (if started)
229 END_DEBUG_EXCEPTION_HANDLER
235 /*****************************************************************************
237 *****************************************************************************/
240 static bool get_cmdline_opts(int argc, char *argv[], Settings *cmd_args)
242 set_allowed_options(&allowed_options);
244 return cmd_args->parseCommandLine(argc, argv, allowed_options);
247 static void set_allowed_options(OptionList *allowed_options)
249 allowed_options->clear();
251 allowed_options->insert(std::make_pair("help", ValueSpec(VALUETYPE_FLAG,
252 _("Show allowed options"))));
253 allowed_options->insert(std::make_pair("version", ValueSpec(VALUETYPE_FLAG,
254 _("Show version information"))));
255 allowed_options->insert(std::make_pair("config", ValueSpec(VALUETYPE_STRING,
256 _("Load configuration from specified file"))));
257 allowed_options->insert(std::make_pair("port", ValueSpec(VALUETYPE_STRING,
258 _("Set network port (UDP)"))));
259 allowed_options->insert(std::make_pair("run-unittests", ValueSpec(VALUETYPE_FLAG,
260 _("Run the unit tests and exit"))));
261 allowed_options->insert(std::make_pair("map-dir", ValueSpec(VALUETYPE_STRING,
262 _("Same as --world (deprecated)"))));
263 allowed_options->insert(std::make_pair("world", ValueSpec(VALUETYPE_STRING,
264 _("Set world path (implies local game) ('list' lists all)"))));
265 allowed_options->insert(std::make_pair("worldname", ValueSpec(VALUETYPE_STRING,
266 _("Set world by name (implies local game)"))));
267 allowed_options->insert(std::make_pair("quiet", ValueSpec(VALUETYPE_FLAG,
268 _("Print to console errors only"))));
269 allowed_options->insert(std::make_pair("info", ValueSpec(VALUETYPE_FLAG,
270 _("Print more information to console"))));
271 allowed_options->insert(std::make_pair("verbose", ValueSpec(VALUETYPE_FLAG,
272 _("Print even more information to console"))));
273 allowed_options->insert(std::make_pair("trace", ValueSpec(VALUETYPE_FLAG,
274 _("Print enormous amounts of information to log and console"))));
275 allowed_options->insert(std::make_pair("logfile", ValueSpec(VALUETYPE_STRING,
276 _("Set logfile path ('' = no logging)"))));
277 allowed_options->insert(std::make_pair("gameid", ValueSpec(VALUETYPE_STRING,
278 _("Set gameid (\"--gameid list\" prints available ones)"))));
279 allowed_options->insert(std::make_pair("migrate", ValueSpec(VALUETYPE_STRING,
280 _("Migrate from current map backend to another (Only works when using minetestserver or with --server)"))));
282 allowed_options->insert(std::make_pair("videomodes", ValueSpec(VALUETYPE_FLAG,
283 _("Show available video modes"))));
284 allowed_options->insert(std::make_pair("speedtests", ValueSpec(VALUETYPE_FLAG,
285 _("Run speed tests"))));
286 allowed_options->insert(std::make_pair("address", ValueSpec(VALUETYPE_STRING,
287 _("Address to connect to. ('' = local game)"))));
288 allowed_options->insert(std::make_pair("random-input", ValueSpec(VALUETYPE_FLAG,
289 _("Enable random user input, for testing"))));
290 allowed_options->insert(std::make_pair("server", ValueSpec(VALUETYPE_FLAG,
291 _("Run dedicated server"))));
292 allowed_options->insert(std::make_pair("name", ValueSpec(VALUETYPE_STRING,
293 _("Set player name"))));
294 allowed_options->insert(std::make_pair("password", ValueSpec(VALUETYPE_STRING,
295 _("Set password"))));
296 allowed_options->insert(std::make_pair("go", ValueSpec(VALUETYPE_FLAG,
297 _("Disable main menu"))));
302 static void print_help(const OptionList &allowed_options)
304 dstream << _("Allowed options:") << std::endl;
305 print_allowed_options(allowed_options);
308 static void print_allowed_options(const OptionList &allowed_options)
310 for (OptionList::const_iterator i = allowed_options.begin();
311 i != allowed_options.end(); ++i) {
312 std::ostringstream os1(std::ios::binary);
313 os1 << " --" << i->first;
314 if (i->second.type != VALUETYPE_FLAG)
315 os1 << _(" <value>");
317 dstream << padStringRight(os1.str(), 24);
319 if (i->second.help != NULL)
320 dstream << i->second.help;
322 dstream << std::endl;
326 static void print_version()
328 dstream << PROJECT_NAME_C " " << g_version_hash << std::endl;
330 dstream << "Using Irrlicht " << IRRLICHT_SDK_VERSION << std::endl;
332 dstream << "Build info: " << g_build_info << std::endl;
335 static void list_game_ids()
337 std::set<std::string> gameids = getAvailableGameIds();
338 for (std::set<std::string>::const_iterator i = gameids.begin();
339 i != gameids.end(); ++i)
340 dstream << (*i) <<std::endl;
343 static void list_worlds()
345 dstream << _("Available worlds:") << std::endl;
346 std::vector<WorldSpec> worldspecs = getAvailableWorlds();
347 print_worldspecs(worldspecs, dstream);
350 static void print_worldspecs(const std::vector<WorldSpec> &worldspecs,
353 for (size_t i = 0; i < worldspecs.size(); i++) {
354 std::string name = worldspecs[i].name;
355 std::string path = worldspecs[i].path;
356 if (name.find(" ") != std::string::npos)
357 name = std::string("'") + name + "'";
358 path = std::string("'") + path + "'";
359 name = padStringRight(name, 14);
360 os << " " << name << " " << path << std::endl;
364 static void print_modified_quicktune_values()
366 bool header_printed = false;
367 std::vector<std::string> names = getQuicktuneNames();
369 for (u32 i = 0; i < names.size(); i++) {
370 QuicktuneValue val = getQuicktuneValue(names[i]);
373 if (!header_printed) {
374 dstream << "Modified quicktune values:" << std::endl;
375 header_printed = true;
377 dstream << names[i] << " = " << val.getString() << std::endl;
381 static void setup_log_params(const Settings &cmd_args)
383 // Quiet mode, print errors only
384 if (cmd_args.getFlag("quiet")) {
385 g_logger.removeOutput(&stderr_output);
386 g_logger.addOutputMaxLevel(&stderr_output, LL_ERROR);
389 // If trace is enabled, enable logging of certain things
390 if (cmd_args.getFlag("trace")) {
391 dstream << _("Enabling trace level debug output") << std::endl;
392 g_logger.setTraceEnabled(true);
393 dout_con_ptr = &verbosestream; // This is somewhat old
394 socket_enable_debug_output = true; // Sockets doesn't use log.h
397 // In certain cases, output info level on stderr
398 if (cmd_args.getFlag("info") || cmd_args.getFlag("verbose") ||
399 cmd_args.getFlag("trace") || cmd_args.getFlag("speedtests"))
400 g_logger.addOutput(&stderr_output, LL_INFO);
402 // In certain cases, output verbose level on stderr
403 if (cmd_args.getFlag("verbose") || cmd_args.getFlag("trace"))
404 g_logger.addOutput(&stderr_output, LL_VERBOSE);
407 static bool create_userdata_path()
412 porting::initAndroid();
414 porting::setExternalStorageDir(porting::jnienv);
415 if (!fs::PathExists(porting::path_user)) {
416 success = fs::CreateDir(porting::path_user);
420 porting::copyAssets();
422 // Create user data directory
423 success = fs::CreateDir(porting::path_user);
426 infostream << "path_share = " << porting::path_share << std::endl;
427 infostream << "path_user = " << porting::path_user << std::endl;
432 static bool init_common(const Settings &cmd_args, int argc, char *argv[])
435 set_default_settings(g_settings);
437 // Initialize sockets
439 atexit(sockets_cleanup);
441 if (!read_config_file(cmd_args))
444 init_log_streams(cmd_args);
446 // Initialize random seed
450 // Initialize HTTP fetcher
451 httpfetch_init(g_settings->getS32("curl_parallel_limit"));
453 init_gettext(porting::path_locale.c_str(),
454 g_settings->get("language"), argc, argv);
459 static void startup_message()
461 infostream << PROJECT_NAME << " " << _("with")
462 << " SER_FMT_VER_HIGHEST_READ="
463 << (int)SER_FMT_VER_HIGHEST_READ << ", "
464 << g_build_info << std::endl;
467 static bool read_config_file(const Settings &cmd_args)
469 // Path of configuration file in use
470 sanity_check(g_settings_path == ""); // Sanity check
472 if (cmd_args.exists("config")) {
473 bool r = g_settings->readConfigFile(cmd_args.get("config").c_str());
475 errorstream << "Could not read configuration from \""
476 << cmd_args.get("config") << "\"" << std::endl;
479 g_settings_path = cmd_args.get("config");
481 std::vector<std::string> filenames;
482 filenames.push_back(porting::path_user + DIR_DELIM + "minetest.conf");
483 // Legacy configuration file location
484 filenames.push_back(porting::path_user +
485 DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
488 // Try also from a lower level (to aid having the same configuration
489 // for many RUN_IN_PLACE installs)
490 filenames.push_back(porting::path_user +
491 DIR_DELIM + ".." + DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
494 for (size_t i = 0; i < filenames.size(); i++) {
495 bool r = g_settings->readConfigFile(filenames[i].c_str());
497 g_settings_path = filenames[i];
502 // If no path found, use the first one (menu creates the file)
503 if (g_settings_path == "")
504 g_settings_path = filenames[0];
510 static void init_log_streams(const Settings &cmd_args)
513 std::string log_filename = DEBUGFILE;
515 std::string log_filename = porting::path_user + DIR_DELIM + DEBUGFILE;
517 if (cmd_args.exists("logfile"))
518 log_filename = cmd_args.get("logfile");
520 g_logger.removeOutput(&file_log_output);
521 std::string conf_loglev = g_settings->get("debug_log_level");
523 // Old integer format
524 if (std::isdigit(conf_loglev[0])) {
525 warningstream << "Deprecated use of debug_log_level with an "
526 "integer value; please update your configuration." << std::endl;
527 static const char *lev_name[] =
528 {"", "error", "action", "info", "verbose"};
529 int lev_i = atoi(conf_loglev.c_str());
530 if (lev_i < 0 || lev_i >= (int)ARRLEN(lev_name)) {
531 warningstream << "Supplied invalid debug_log_level!"
532 " Assuming action level." << std::endl;
535 conf_loglev = lev_name[lev_i];
538 if (log_filename.empty() || conf_loglev.empty()) // No logging
541 LogLevel log_level = Logger::stringToLevel(conf_loglev);
542 if (log_level == LL_MAX) {
543 warningstream << "Supplied unrecognized debug_log_level; "
544 "using maximum." << std::endl;
547 verbosestream << "log_filename = " << log_filename << std::endl;
549 file_log_output.open(log_filename.c_str());
550 g_logger.addOutputMaxLevel(&file_log_output, log_level);
553 static bool game_configure(GameParams *game_params, const Settings &cmd_args)
555 game_configure_port(game_params, cmd_args);
557 if (!game_configure_world(game_params, cmd_args)) {
558 errorstream << "No world path specified or found." << std::endl;
562 game_configure_subgame(game_params, cmd_args);
567 static void game_configure_port(GameParams *game_params, const Settings &cmd_args)
569 if (cmd_args.exists("port"))
570 game_params->socket_port = cmd_args.getU16("port");
572 game_params->socket_port = g_settings->getU16("port");
574 if (game_params->socket_port == 0)
575 game_params->socket_port = DEFAULT_SERVER_PORT;
578 static bool game_configure_world(GameParams *game_params, const Settings &cmd_args)
580 if (get_world_from_cmdline(game_params, cmd_args))
582 if (get_world_from_config(game_params, cmd_args))
585 return auto_select_world(game_params);
588 static bool get_world_from_cmdline(GameParams *game_params, const Settings &cmd_args)
590 std::string commanded_world = "";
593 std::string commanded_worldname = "";
594 if (cmd_args.exists("worldname"))
595 commanded_worldname = cmd_args.get("worldname");
597 // If a world name was specified, convert it to a path
598 if (commanded_worldname != "") {
599 // Get information about available worlds
600 std::vector<WorldSpec> worldspecs = getAvailableWorlds();
602 for (u32 i = 0; i < worldspecs.size(); i++) {
603 std::string name = worldspecs[i].name;
604 if (name == commanded_worldname) {
605 dstream << _("Using world specified by --worldname on the "
606 "command line") << std::endl;
607 commanded_world = worldspecs[i].path;
613 dstream << _("World") << " '" << commanded_worldname
614 << _("' not available. Available worlds:") << std::endl;
615 print_worldspecs(worldspecs, dstream);
619 game_params->world_path = get_clean_world_path(commanded_world);
620 return commanded_world != "";
623 if (cmd_args.exists("world"))
624 commanded_world = cmd_args.get("world");
625 else if (cmd_args.exists("map-dir"))
626 commanded_world = cmd_args.get("map-dir");
627 else if (cmd_args.exists("nonopt0")) // First nameless argument
628 commanded_world = cmd_args.get("nonopt0");
630 game_params->world_path = get_clean_world_path(commanded_world);
631 return commanded_world != "";
634 static bool get_world_from_config(GameParams *game_params, const Settings &cmd_args)
637 std::string commanded_world = "";
639 if (g_settings->exists("map-dir"))
640 commanded_world = g_settings->get("map-dir");
642 game_params->world_path = get_clean_world_path(commanded_world);
644 return commanded_world != "";
647 static bool auto_select_world(GameParams *game_params)
649 // No world was specified; try to select it automatically
650 // Get information about available worlds
652 verbosestream << _("Determining world path") << std::endl;
654 std::vector<WorldSpec> worldspecs = getAvailableWorlds();
655 std::string world_path;
657 // If there is only a single world, use it
658 if (worldspecs.size() == 1) {
659 world_path = worldspecs[0].path;
660 dstream <<_("Automatically selecting world at") << " ["
661 << world_path << "]" << std::endl;
662 // If there are multiple worlds, list them
663 } else if (worldspecs.size() > 1 && game_params->is_dedicated_server) {
664 std::cerr << _("Multiple worlds are available.") << std::endl;
665 std::cerr << _("Please select one using --worldname <name>"
666 " or --world <path>") << std::endl;
667 print_worldspecs(worldspecs, std::cerr);
669 // If there are no worlds, automatically create a new one
671 // This is the ultimate default world path
672 world_path = porting::path_user + DIR_DELIM + "worlds" +
674 infostream << "Creating default world at ["
675 << world_path << "]" << std::endl;
678 assert(world_path != ""); // Post-condition
679 game_params->world_path = world_path;
683 static std::string get_clean_world_path(const std::string &path)
685 const std::string worldmt = "world.mt";
686 std::string clean_path;
688 if (path.size() > worldmt.size()
689 && path.substr(path.size() - worldmt.size()) == worldmt) {
690 dstream << _("Supplied world.mt file - stripping it off.") << std::endl;
691 clean_path = path.substr(0, path.size() - worldmt.size());
699 static bool game_configure_subgame(GameParams *game_params, const Settings &cmd_args)
703 success = get_game_from_cmdline(game_params, cmd_args);
705 success = determine_subgame(game_params);
710 static bool get_game_from_cmdline(GameParams *game_params, const Settings &cmd_args)
712 SubgameSpec commanded_gamespec;
714 if (cmd_args.exists("gameid")) {
715 std::string gameid = cmd_args.get("gameid");
716 commanded_gamespec = findSubgame(gameid);
717 if (!commanded_gamespec.isValid()) {
718 errorstream << "Game \"" << gameid << "\" not found" << std::endl;
721 dstream << _("Using game specified by --gameid on the command line")
723 game_params->game_spec = commanded_gamespec;
730 static bool determine_subgame(GameParams *game_params)
732 SubgameSpec gamespec;
734 assert(game_params->world_path != ""); // Pre-condition
736 verbosestream << _("Determining gameid/gamespec") << std::endl;
737 // If world doesn't exist
738 if (game_params->world_path != ""
739 && !getWorldExists(game_params->world_path)) {
740 // Try to take gamespec from command line
741 if (game_params->game_spec.isValid()) {
742 gamespec = game_params->game_spec;
743 infostream << "Using commanded gameid [" << gamespec.id << "]" << std::endl;
744 } else { // Otherwise we will be using "minetest"
745 gamespec = findSubgame(g_settings->get("default_game"));
746 infostream << "Using default gameid [" << gamespec.id << "]" << std::endl;
747 if (!gamespec.isValid()) {
748 errorstream << "Subgame specified in default_game ["
749 << g_settings->get("default_game")
750 << "] is invalid." << std::endl;
754 } else { // World exists
755 std::string world_gameid = getWorldGameId(game_params->world_path, false);
756 // If commanded to use a gameid, do so
757 if (game_params->game_spec.isValid()) {
758 gamespec = game_params->game_spec;
759 if (game_params->game_spec.id != world_gameid) {
760 warningstream << "Using commanded gameid ["
761 << gamespec.id << "]" << " instead of world gameid ["
762 << world_gameid << "]" << std::endl;
765 // If world contains an embedded game, use it;
766 // Otherwise find world from local system.
767 gamespec = findWorldSubgame(game_params->world_path);
768 infostream << "Using world gameid [" << gamespec.id << "]" << std::endl;
772 if (!gamespec.isValid()) {
773 errorstream << "Subgame [" << gamespec.id << "] could not be found."
778 game_params->game_spec = gamespec;
783 /*****************************************************************************
785 *****************************************************************************/
786 static bool run_dedicated_server(const GameParams &game_params, const Settings &cmd_args)
788 DSTACK("Dedicated server branch");
790 verbosestream << _("Using world path") << " ["
791 << game_params.world_path << "]" << std::endl;
792 verbosestream << _("Using gameid") << " ["
793 << game_params.game_spec.id << "]" << std::endl;
796 std::string bind_str = g_settings->get("bind_address");
797 Address bind_addr(0, 0, 0, 0, game_params.socket_port);
799 if (g_settings->getBool("ipv6_server")) {
800 bind_addr.setAddress((IPv6AddressBytes*) NULL);
803 bind_addr.Resolve(bind_str.c_str());
804 } catch (ResolveError &e) {
805 infostream << "Resolving bind address \"" << bind_str
806 << "\" failed: " << e.what()
807 << " -- Listening on all addresses." << std::endl;
809 if (bind_addr.isIPv6() && !g_settings->getBool("enable_ipv6")) {
810 errorstream << "Unable to listen on "
811 << bind_addr.serializeString()
812 << L" because IPv6 is disabled" << std::endl;
816 // Database migration
817 if (cmd_args.exists("migrate"))
818 return migrate_database(game_params, cmd_args);
821 Server server(game_params.world_path, game_params.game_spec, false,
823 server.start(bind_addr);
826 bool &kill = *porting::signal_handler_killstatus();
827 dedicated_server_loop(server, kill);
832 static bool migrate_database(const GameParams &game_params, const Settings &cmd_args)
834 std::string migrate_to = cmd_args.get("migrate");
836 std::string world_mt_path = game_params.world_path + DIR_DELIM + "world.mt";
837 if (!world_mt.readConfigFile(world_mt_path.c_str())) {
838 errorstream << "Cannot read world.mt!" << std::endl;
841 if (!world_mt.exists("backend")) {
842 errorstream << "Please specify your current backend in world.mt:"
844 << " backend = {sqlite3|leveldb|redis|dummy}"
848 std::string backend = world_mt.get("backend");
849 if (backend == migrate_to) {
850 errorstream << "Cannot migrate: new backend is same"
851 << " as the old one" << std::endl;
854 Database *old_db = ServerMap::createDatabase(backend, game_params.world_path, world_mt),
855 *new_db = ServerMap::createDatabase(migrate_to, game_params.world_path, world_mt);
858 time_t last_update_time = 0;
859 bool &kill = *porting::signal_handler_killstatus();
861 std::vector<v3s16> blocks;
862 old_db->listAllLoadableBlocks(blocks);
864 for (std::vector<v3s16>::const_iterator it = blocks.begin(); it != blocks.end(); ++it) {
865 if (kill) return false;
867 const std::string &data = old_db->loadBlock(*it);
869 new_db->saveBlock(*it, data);
871 errorstream << "Failed to load block " << PP(*it) << ", skipping it." << std::endl;
873 if (++count % 0xFF == 0 && time(NULL) - last_update_time >= 1) {
874 std::cerr << " Migrated " << count << " blocks, "
875 << (100.0 * count / blocks.size()) << "% completed.\r";
878 last_update_time = time(NULL);
881 std::cerr << std::endl;
886 actionstream << "Successfully migrated " << count << " blocks" << std::endl;
887 world_mt.set("backend", migrate_to);
888 if (!world_mt.updateConfigFile(world_mt_path.c_str()))
889 errorstream << "Failed to update world.mt!" << std::endl;
891 actionstream << "world.mt updated" << std::endl;