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
33 #include "mainmenumanager.h"
34 #include "irrlichttypes_extrabloated.h"
40 #include "guiMainMenu.h"
42 #include "defaultsettings.h"
46 #include "quicktune.h"
47 #include "httpfetch.h"
48 #include "guiEngine.h"
49 #include "mapsector.h"
50 #include "fontengine.h"
51 #include "gameparams.h"
53 #include "client/clientlauncher.h"
56 #include "database-sqlite3.h"
58 #include "database-leveldb.h"
62 #include "database-redis.h"
65 #ifdef HAVE_TOUCHSCREENGUI
66 #include "touchscreengui.h"
71 These are loaded from the config file.
73 static Settings main_settings;
74 Settings *g_settings = &main_settings;
75 std::string g_settings_path;
78 Profiler main_profiler;
79 Profiler *g_profiler = &main_profiler;
81 // Menu clouds are created later
82 Clouds *g_menuclouds = 0;
83 irr::scene::ISceneManager *g_menucloudsmgr = 0;
90 std::ostream *dout_con_ptr = &dummyout;
91 std::ostream *derr_con_ptr = &verbosestream;
94 std::ostream *dout_server_ptr = &infostream;
95 std::ostream *derr_server_ptr = &errorstream;
98 std::ostream *dout_client_ptr = &infostream;
99 std::ostream *derr_client_ptr = &errorstream;
101 #define DEBUGFILE "debug.txt"
102 #define DEFAULT_SERVER_PORT 30000
104 typedef std::map<std::string, ValueSpec> OptionList;
106 /**********************************************************************
108 **********************************************************************/
110 static bool get_cmdline_opts(int argc, char *argv[], Settings *cmd_args);
111 static void set_allowed_options(OptionList *allowed_options);
113 static void print_help(const OptionList &allowed_options);
114 static void print_allowed_options(const OptionList &allowed_options);
115 static void print_version();
116 static void print_worldspecs(const std::vector<WorldSpec> &worldspecs,
118 static void print_modified_quicktune_values();
120 static void list_game_ids();
121 static void list_worlds();
122 static void setup_log_params(const Settings &cmd_args);
123 static bool create_userdata_path();
124 static bool init_common(int *log_level, const Settings &cmd_args, int argc, char *argv[]);
125 static void startup_message();
126 static bool read_config_file(const Settings &cmd_args);
127 static void init_debug_streams(int *log_level, const Settings &cmd_args);
129 static bool game_configure(GameParams *game_params, const Settings &cmd_args);
130 static void game_configure_port(GameParams *game_params, const Settings &cmd_args);
132 static bool game_configure_world(GameParams *game_params, const Settings &cmd_args);
133 static bool get_world_from_cmdline(GameParams *game_params, const Settings &cmd_args);
134 static bool get_world_from_config(GameParams *game_params, const Settings &cmd_args);
135 static bool auto_select_world(GameParams *game_params);
136 static std::string get_clean_world_path(const std::string &path);
138 static bool game_configure_subgame(GameParams *game_params, const Settings &cmd_args);
139 static bool get_game_from_cmdline(GameParams *game_params, const Settings &cmd_args);
140 static bool determine_subgame(GameParams *game_params);
142 static bool run_dedicated_server(const GameParams &game_params, const Settings &cmd_args);
143 static bool migrate_database(const GameParams &game_params, const Settings &cmd_args,
146 /**********************************************************************/
153 /* mainmenumanager.h */
155 gui::IGUIEnvironment* guienv = NULL;
156 gui::IGUIStaticText *guiroot = NULL;
157 MainMenuManager g_menumgr;
161 return (g_menumgr.menuCount() == 0);
164 // Passed to menus to allow disconnecting and exiting
165 MainGameCallback *g_gamecallback = NULL;
169 gettime.h implementation
176 /* Use imprecise system calls directly (from porting.h) */
177 return porting::getTime(PRECISION_MILLI);
180 u32 getTime(TimePrecision prec)
182 return porting::getTime(prec);
187 class StderrLogOutput: public ILogOutput
190 /* line: Full line with timestamp, level and thread */
191 void printLog(const std::string &line)
193 std::cerr << line << std::endl;
195 } main_stderr_log_out;
197 class DstreamNoStderrLogOutput: public ILogOutput
200 /* line: Full line with timestamp, level and thread */
201 void printLog(const std::string &line)
203 dstream_no_stderr << line << std::endl;
205 } main_dstream_no_stderr_log_out;
207 static OptionList allowed_options;
209 int main(int argc, char *argv[])
213 debug_set_exception_handler();
215 log_add_output_maxlev(&main_stderr_log_out, LMT_ACTION);
216 log_add_output_all_levs(&main_dstream_no_stderr_log_out);
218 log_register_thread("main");
221 bool cmd_args_ok = get_cmdline_opts(argc, argv, &cmd_args);
223 || cmd_args.getFlag("help")
224 || cmd_args.exists("nonopt1")) {
225 print_help(allowed_options);
226 return cmd_args_ok ? 0 : 1;
229 if (cmd_args.getFlag("version")) {
234 setup_log_params(cmd_args);
236 porting::signal_handler_init();
237 porting::initializePaths();
239 if (!create_userdata_path()) {
240 errorstream << "Cannot create user data directory" << std::endl;
244 // Initialize debug stacks
246 DSTACK(__FUNCTION_NAME);
249 BEGIN_DEBUG_EXCEPTION_HANDLER
251 // List gameids if requested
252 if (cmd_args.exists("gameid") && cmd_args.get("gameid") == "list") {
257 // List worlds if requested
258 if (cmd_args.exists("world") && cmd_args.get("world") == "list") {
263 GameParams game_params;
264 if (!init_common(&game_params.log_level, cmd_args, argc, argv))
269 if (cmd_args.getFlag("run-unittests")) {
276 game_params.is_dedicated_server = true;
278 game_params.is_dedicated_server = cmd_args.getFlag("server");
281 if (!game_configure(&game_params, cmd_args))
284 assert(game_params.world_path != "");
286 infostream << "Using commanded world path ["
287 << game_params.world_path << "]" << std::endl;
289 //Run dedicated server if asked to or no other option
290 g_settings->set("server_dedicated",
291 game_params.is_dedicated_server ? "true" : "false");
293 if (game_params.is_dedicated_server)
294 return run_dedicated_server(game_params, cmd_args) ? 0 : 1;
297 ClientLauncher launcher;
298 retval = launcher.run(game_params, cmd_args) ? 0 : 1;
303 // Update configuration file
304 if (g_settings_path != "")
305 g_settings->updateConfigFile(g_settings_path.c_str());
307 print_modified_quicktune_values();
309 // Stop httpfetch thread (if started)
312 END_DEBUG_EXCEPTION_HANDLER(errorstream)
318 /*****************************************************************************
320 *****************************************************************************/
323 static bool get_cmdline_opts(int argc, char *argv[], Settings *cmd_args)
325 set_allowed_options(&allowed_options);
327 return cmd_args->parseCommandLine(argc, argv, allowed_options);
330 static void set_allowed_options(OptionList *allowed_options)
332 allowed_options->clear();
334 allowed_options->insert(std::make_pair("help", ValueSpec(VALUETYPE_FLAG,
335 _("Show allowed options"))));
336 allowed_options->insert(std::make_pair("version", ValueSpec(VALUETYPE_FLAG,
337 _("Show version information"))));
338 allowed_options->insert(std::make_pair("config", ValueSpec(VALUETYPE_STRING,
339 _("Load configuration from specified file"))));
340 allowed_options->insert(std::make_pair("port", ValueSpec(VALUETYPE_STRING,
341 _("Set network port (UDP)"))));
342 allowed_options->insert(std::make_pair("run-unittests", ValueSpec(VALUETYPE_FLAG,
343 _("Run the unit tests and exit"))));
344 allowed_options->insert(std::make_pair("map-dir", ValueSpec(VALUETYPE_STRING,
345 _("Same as --world (deprecated)"))));
346 allowed_options->insert(std::make_pair("world", ValueSpec(VALUETYPE_STRING,
347 _("Set world path (implies local game) ('list' lists all)"))));
348 allowed_options->insert(std::make_pair("worldname", ValueSpec(VALUETYPE_STRING,
349 _("Set world by name (implies local game)"))));
350 allowed_options->insert(std::make_pair("quiet", ValueSpec(VALUETYPE_FLAG,
351 _("Print to console errors only"))));
352 allowed_options->insert(std::make_pair("info", ValueSpec(VALUETYPE_FLAG,
353 _("Print more information to console"))));
354 allowed_options->insert(std::make_pair("verbose", ValueSpec(VALUETYPE_FLAG,
355 _("Print even more information to console"))));
356 allowed_options->insert(std::make_pair("trace", ValueSpec(VALUETYPE_FLAG,
357 _("Print enormous amounts of information to log and console"))));
358 allowed_options->insert(std::make_pair("logfile", ValueSpec(VALUETYPE_STRING,
359 _("Set logfile path ('' = no logging)"))));
360 allowed_options->insert(std::make_pair("gameid", ValueSpec(VALUETYPE_STRING,
361 _("Set gameid (\"--gameid list\" prints available ones)"))));
362 allowed_options->insert(std::make_pair("migrate", ValueSpec(VALUETYPE_STRING,
363 _("Migrate from current map backend to another (Only works when using minetestserver or with --server)"))));
365 allowed_options->insert(std::make_pair("videomodes", ValueSpec(VALUETYPE_FLAG,
366 _("Show available video modes"))));
367 allowed_options->insert(std::make_pair("speedtests", ValueSpec(VALUETYPE_FLAG,
368 _("Run speed tests"))));
369 allowed_options->insert(std::make_pair("address", ValueSpec(VALUETYPE_STRING,
370 _("Address to connect to. ('' = local game)"))));
371 allowed_options->insert(std::make_pair("random-input", ValueSpec(VALUETYPE_FLAG,
372 _("Enable random user input, for testing"))));
373 allowed_options->insert(std::make_pair("server", ValueSpec(VALUETYPE_FLAG,
374 _("Run dedicated server"))));
375 allowed_options->insert(std::make_pair("name", ValueSpec(VALUETYPE_STRING,
376 _("Set player name"))));
377 allowed_options->insert(std::make_pair("password", ValueSpec(VALUETYPE_STRING,
378 _("Set password"))));
379 allowed_options->insert(std::make_pair("go", ValueSpec(VALUETYPE_FLAG,
380 _("Disable main menu"))));
385 static void print_help(const OptionList &allowed_options)
387 dstream << _("Allowed options:") << std::endl;
388 print_allowed_options(allowed_options);
391 static void print_allowed_options(const OptionList &allowed_options)
393 for (OptionList::const_iterator i = allowed_options.begin();
394 i != allowed_options.end(); ++i) {
395 std::ostringstream os1(std::ios::binary);
396 os1 << " --" << i->first;
397 if (i->second.type != VALUETYPE_FLAG)
398 os1 << _(" <value>");
400 dstream << padStringRight(os1.str(), 24);
402 if (i->second.help != NULL)
403 dstream << i->second.help;
405 dstream << std::endl;
409 static void print_version()
412 dstream << "minetestserver " << minetest_version_hash << std::endl;
414 dstream << "Minetest " << minetest_version_hash << std::endl;
415 dstream << "Using Irrlicht " << IRRLICHT_SDK_VERSION << std::endl;
417 dstream << "Build info: " << minetest_build_info << std::endl;
420 static void list_game_ids()
422 std::set<std::string> gameids = getAvailableGameIds();
423 for (std::set<std::string>::const_iterator i = gameids.begin();
424 i != gameids.end(); i++)
425 dstream << (*i) <<std::endl;
428 static void list_worlds()
430 dstream << _("Available worlds:") << std::endl;
431 std::vector<WorldSpec> worldspecs = getAvailableWorlds();
432 print_worldspecs(worldspecs, dstream);
435 static void print_worldspecs(const std::vector<WorldSpec> &worldspecs,
438 for (size_t i = 0; i < worldspecs.size(); i++) {
439 std::string name = worldspecs[i].name;
440 std::string path = worldspecs[i].path;
441 if (name.find(" ") != std::string::npos)
442 name = std::string("'") + name + "'";
443 path = std::string("'") + path + "'";
444 name = padStringRight(name, 14);
445 os << " " << name << " " << path << std::endl;
449 static void print_modified_quicktune_values()
451 bool header_printed = false;
452 std::vector<std::string> names = getQuicktuneNames();
454 for (u32 i = 0; i < names.size(); i++) {
455 QuicktuneValue val = getQuicktuneValue(names[i]);
458 if (!header_printed) {
459 dstream << "Modified quicktune values:" << std::endl;
460 header_printed = true;
462 dstream << names[i] << " = " << val.getString() << std::endl;
466 static void setup_log_params(const Settings &cmd_args)
468 // Quiet mode, print errors only
469 if (cmd_args.getFlag("quiet")) {
470 log_remove_output(&main_stderr_log_out);
471 log_add_output_maxlev(&main_stderr_log_out, LMT_ERROR);
474 // If trace is enabled, enable logging of certain things
475 if (cmd_args.getFlag("trace")) {
476 dstream << _("Enabling trace level debug output") << std::endl;
477 log_trace_level_enabled = true;
478 dout_con_ptr = &verbosestream; // this is somewhat old crap
479 socket_enable_debug_output = true; // socket doesn't use log.h
482 // In certain cases, output info level on stderr
483 if (cmd_args.getFlag("info") || cmd_args.getFlag("verbose") ||
484 cmd_args.getFlag("trace") || cmd_args.getFlag("speedtests"))
485 log_add_output(&main_stderr_log_out, LMT_INFO);
487 // In certain cases, output verbose level on stderr
488 if (cmd_args.getFlag("verbose") || cmd_args.getFlag("trace"))
489 log_add_output(&main_stderr_log_out, LMT_VERBOSE);
492 static bool create_userdata_path()
497 porting::initAndroid();
499 porting::setExternalStorageDir(porting::jnienv);
500 if (!fs::PathExists(porting::path_user)) {
501 success = fs::CreateDir(porting::path_user);
505 porting::copyAssets();
507 // Create user data directory
508 success = fs::CreateDir(porting::path_user);
511 infostream << "path_share = " << porting::path_share << std::endl;
512 infostream << "path_user = " << porting::path_user << std::endl;
517 static bool init_common(int *log_level, const Settings &cmd_args, int argc, char *argv[])
520 set_default_settings(g_settings);
522 // Initialize sockets
524 atexit(sockets_cleanup);
526 if (!read_config_file(cmd_args))
529 init_debug_streams(log_level, cmd_args);
531 // Initialize random seed
535 // Initialize HTTP fetcher
536 httpfetch_init(g_settings->getS32("curl_parallel_limit"));
539 init_gettext((porting::path_share + DIR_DELIM + "locale").c_str(),
540 g_settings->get("language"), argc, argv);
542 init_gettext((porting::path_share + DIR_DELIM + "locale").c_str(),
543 g_settings->get("language"));
549 static void startup_message()
551 infostream << PROJECT_NAME << " " << _("with")
552 << " SER_FMT_VER_HIGHEST_READ="
553 << (int)SER_FMT_VER_HIGHEST_READ << ", "
554 << minetest_build_info << std::endl;
557 static bool read_config_file(const Settings &cmd_args)
559 // Path of configuration file in use
560 assert(g_settings_path == ""); // Sanity check
562 if (cmd_args.exists("config")) {
563 bool r = g_settings->readConfigFile(cmd_args.get("config").c_str());
565 errorstream << "Could not read configuration from \""
566 << cmd_args.get("config") << "\"" << std::endl;
569 g_settings_path = cmd_args.get("config");
571 std::vector<std::string> filenames;
572 filenames.push_back(porting::path_user + DIR_DELIM + "minetest.conf");
573 // Legacy configuration file location
574 filenames.push_back(porting::path_user +
575 DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
578 // Try also from a lower level (to aid having the same configuration
579 // for many RUN_IN_PLACE installs)
580 filenames.push_back(porting::path_user +
581 DIR_DELIM + ".." + DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
584 for (size_t i = 0; i < filenames.size(); i++) {
585 bool r = g_settings->readConfigFile(filenames[i].c_str());
587 g_settings_path = filenames[i];
592 // If no path found, use the first one (menu creates the file)
593 if (g_settings_path == "")
594 g_settings_path = filenames[0];
600 static void init_debug_streams(int *log_level, const Settings &cmd_args)
603 std::string logfile = DEBUGFILE;
605 std::string logfile = porting::path_user + DIR_DELIM + DEBUGFILE;
607 if (cmd_args.exists("logfile"))
608 logfile = cmd_args.get("logfile");
610 log_remove_output(&main_dstream_no_stderr_log_out);
611 *log_level = g_settings->getS32("debug_log_level");
613 if (*log_level == 0) //no logging
615 if (*log_level < 0) {
616 dstream << "WARNING: Supplied debug_log_level < 0; Using 0" << std::endl;
618 } else if (*log_level > LMT_NUM_VALUES) {
619 dstream << "WARNING: Supplied debug_log_level > " << LMT_NUM_VALUES
620 << "; Using " << LMT_NUM_VALUES << std::endl;
621 *log_level = LMT_NUM_VALUES;
624 log_add_output_maxlev(&main_dstream_no_stderr_log_out,
625 (LogMessageLevel)(*log_level - 1));
627 debugstreams_init(false, logfile == "" ? NULL : logfile.c_str());
629 infostream << "logfile = " << logfile << std::endl;
631 atexit(debugstreams_deinit);
634 static bool game_configure(GameParams *game_params, const Settings &cmd_args)
636 game_configure_port(game_params, cmd_args);
638 if (!game_configure_world(game_params, cmd_args)) {
639 errorstream << "No world path specified or found." << std::endl;
643 game_configure_subgame(game_params, cmd_args);
648 static void game_configure_port(GameParams *game_params, const Settings &cmd_args)
650 if (cmd_args.exists("port"))
651 game_params->socket_port = cmd_args.getU16("port");
653 game_params->socket_port = g_settings->getU16("port");
655 if (game_params->socket_port == 0)
656 game_params->socket_port = DEFAULT_SERVER_PORT;
659 static bool game_configure_world(GameParams *game_params, const Settings &cmd_args)
661 if (get_world_from_cmdline(game_params, cmd_args))
663 if (get_world_from_config(game_params, cmd_args))
666 return auto_select_world(game_params);
669 static bool get_world_from_cmdline(GameParams *game_params, const Settings &cmd_args)
671 std::string commanded_world = "";
674 std::string commanded_worldname = "";
675 if (cmd_args.exists("worldname"))
676 commanded_worldname = cmd_args.get("worldname");
678 // If a world name was specified, convert it to a path
679 if (commanded_worldname != "") {
680 // Get information about available worlds
681 std::vector<WorldSpec> worldspecs = getAvailableWorlds();
683 for (u32 i = 0; i < worldspecs.size(); i++) {
684 std::string name = worldspecs[i].name;
685 if (name == commanded_worldname) {
686 dstream << _("Using world specified by --worldname on the "
687 "command line") << std::endl;
688 commanded_world = worldspecs[i].path;
694 dstream << _("World") << " '" << commanded_worldname
695 << _("' not available. Available worlds:") << std::endl;
696 print_worldspecs(worldspecs, dstream);
700 game_params->world_path = get_clean_world_path(commanded_world);
701 return commanded_world != "";
704 if (cmd_args.exists("world"))
705 commanded_world = cmd_args.get("world");
706 else if (cmd_args.exists("map-dir"))
707 commanded_world = cmd_args.get("map-dir");
708 else if (cmd_args.exists("nonopt0")) // First nameless argument
709 commanded_world = cmd_args.get("nonopt0");
711 game_params->world_path = get_clean_world_path(commanded_world);
712 return commanded_world != "";
715 static bool get_world_from_config(GameParams *game_params, const Settings &cmd_args)
718 std::string commanded_world = "";
720 if (g_settings->exists("map-dir"))
721 commanded_world = g_settings->get("map-dir");
723 game_params->world_path = get_clean_world_path(commanded_world);
725 return commanded_world != "";
728 static bool auto_select_world(GameParams *game_params)
730 // No world was specified; try to select it automatically
731 // Get information about available worlds
733 verbosestream << _("Determining world path") << std::endl;
735 std::vector<WorldSpec> worldspecs = getAvailableWorlds();
736 std::string world_path;
738 // If there is only a single world, use it
739 if (worldspecs.size() == 1) {
740 world_path = worldspecs[0].path;
741 dstream <<_("Automatically selecting world at") << " ["
742 << world_path << "]" << std::endl;
743 // If there are multiple worlds, list them
744 } else if (worldspecs.size() > 1 && game_params->is_dedicated_server) {
745 dstream << _("Multiple worlds are available.") << std::endl;
746 dstream << _("Please select one using --worldname <name>"
747 " or --world <path>") << std::endl;
748 print_worldspecs(worldspecs, dstream);
750 // If there are no worlds, automatically create a new one
752 // This is the ultimate default world path
753 world_path = porting::path_user + DIR_DELIM + "worlds" +
755 infostream << "Creating default world at ["
756 << world_path << "]" << std::endl;
759 assert(world_path != "");
760 game_params->world_path = world_path;
764 static std::string get_clean_world_path(const std::string &path)
766 const std::string worldmt = "world.mt";
767 std::string clean_path;
769 if (path.size() > worldmt.size()
770 && path.substr(path.size() - worldmt.size()) == worldmt) {
771 dstream << _("Supplied world.mt file - stripping it off.") << std::endl;
772 clean_path = path.substr(0, path.size() - worldmt.size());
780 static bool game_configure_subgame(GameParams *game_params, const Settings &cmd_args)
784 success = get_game_from_cmdline(game_params, cmd_args);
786 success = determine_subgame(game_params);
791 static bool get_game_from_cmdline(GameParams *game_params, const Settings &cmd_args)
793 SubgameSpec commanded_gamespec;
795 if (cmd_args.exists("gameid")) {
796 std::string gameid = cmd_args.get("gameid");
797 commanded_gamespec = findSubgame(gameid);
798 if (!commanded_gamespec.isValid()) {
799 errorstream << "Game \"" << gameid << "\" not found" << std::endl;
802 dstream << _("Using game specified by --gameid on the command line")
804 game_params->game_spec = commanded_gamespec;
811 static bool determine_subgame(GameParams *game_params)
813 SubgameSpec gamespec;
815 assert(game_params->world_path != ""); // pre-condition
817 verbosestream << _("Determining gameid/gamespec") << std::endl;
818 // If world doesn't exist
819 if (game_params->world_path != ""
820 && !getWorldExists(game_params->world_path)) {
821 // Try to take gamespec from command line
822 if (game_params->game_spec.isValid()) {
823 gamespec = game_params->game_spec;
824 infostream << "Using commanded gameid [" << gamespec.id << "]" << std::endl;
825 } else { // Otherwise we will be using "minetest"
826 gamespec = findSubgame(g_settings->get("default_game"));
827 infostream << "Using default gameid [" << gamespec.id << "]" << std::endl;
828 if (!gamespec.isValid()) {
829 errorstream << "Subgame specified in default_game ["
830 << g_settings->get("default_game")
831 << "] is invalid." << std::endl;
835 } else { // World exists
836 std::string world_gameid = getWorldGameId(game_params->world_path, false);
837 // If commanded to use a gameid, do so
838 if (game_params->game_spec.isValid()) {
839 gamespec = game_params->game_spec;
840 if (game_params->game_spec.id != world_gameid) {
841 errorstream << "WARNING: Using commanded gameid ["
842 << gamespec.id << "]" << " instead of world gameid ["
843 << world_gameid << "]" << std::endl;
846 // If world contains an embedded game, use it;
847 // Otherwise find world from local system.
848 gamespec = findWorldSubgame(game_params->world_path);
849 infostream << "Using world gameid [" << gamespec.id << "]" << std::endl;
853 if (!gamespec.isValid()) {
854 errorstream << "Subgame [" << gamespec.id << "] could not be found."
859 game_params->game_spec = gamespec;
864 /*****************************************************************************
866 *****************************************************************************/
867 static bool run_dedicated_server(const GameParams &game_params, const Settings &cmd_args)
869 DSTACK("Dedicated server branch");
871 verbosestream << _("Using world path") << " ["
872 << game_params.world_path << "]" << std::endl;
873 verbosestream << _("Using gameid") << " ["
874 << game_params.game_spec.id << "]" << std::endl;
877 std::string bind_str = g_settings->get("bind_address");
878 Address bind_addr(0, 0, 0, 0, game_params.socket_port);
880 if (g_settings->getBool("ipv6_server")) {
881 bind_addr.setAddress((IPv6AddressBytes*) NULL);
884 bind_addr.Resolve(bind_str.c_str());
885 } catch (ResolveError &e) {
886 infostream << "Resolving bind address \"" << bind_str
887 << "\" failed: " << e.what()
888 << " -- Listening on all addresses." << std::endl;
890 if (bind_addr.isIPv6() && !g_settings->getBool("enable_ipv6")) {
891 errorstream << "Unable to listen on "
892 << bind_addr.serializeString()
893 << L" because IPv6 is disabled" << std::endl;
898 Server server(game_params.world_path,
899 game_params.game_spec, false, bind_addr.isIPv6());
901 // Database migration
902 if (cmd_args.exists("migrate"))
903 return migrate_database(game_params, cmd_args, &server);
905 server.start(bind_addr);
908 bool &kill = *porting::signal_handler_killstatus();
909 dedicated_server_loop(server, kill);
914 static bool migrate_database(const GameParams &game_params, const Settings &cmd_args,
918 bool success = world_mt.readConfigFile((game_params.world_path
919 + DIR_DELIM + "world.mt").c_str());
921 errorstream << "Cannot read world.mt" << std::endl;
925 if (!world_mt.exists("backend")) {
926 errorstream << "Please specify your current backend in world.mt file:"
927 << std::endl << " backend = {sqlite3|leveldb|redis|dummy}"
932 std::string backend = world_mt.get("backend");
934 std::string migrate_to = cmd_args.get("migrate");
936 if (backend == migrate_to) {
937 errorstream << "Cannot migrate: new backend is same as the old one"
942 if (migrate_to == "sqlite3")
943 new_db = new Database_SQLite3(&(ServerMap&)server->getMap(),
944 game_params.world_path);
946 else if (migrate_to == "leveldb")
947 new_db = new Database_LevelDB(&(ServerMap&)server->getMap(),
948 game_params.world_path);
951 else if (migrate_to == "redis")
952 new_db = new Database_Redis(&(ServerMap&)server->getMap(),
953 game_params.world_path);
956 errorstream << "Migration to " << migrate_to << " is not supported"
961 std::vector<v3s16> blocks;
962 ServerMap &old_map = ((ServerMap&)server->getMap());
963 old_map.listAllLoadableBlocks(blocks);
966 for (std::vector<v3s16>::iterator i = blocks.begin(); i != blocks.end(); i++) {
967 MapBlock *block = old_map.loadBlock(*i);
969 errorstream << "Failed to load block " << PP(*i) << ", skipping it.";
972 old_map.saveBlock(block, new_db);
973 MapSector *sector = old_map.getSectorNoGenerate(v2s16(i->X, i->Z));
974 sector->deleteBlock(block);
977 if (count % 500 == 0)
978 actionstream << "Migrated " << count << " blocks "
979 << (100.0 * count / blocks.size()) << "% completed" << std::endl;
984 actionstream << "Successfully migrated " << count << " blocks" << std::endl;
985 world_mt.set("backend", migrate_to);
986 if (!world_mt.updateConfigFile(
987 (game_params.world_path+ DIR_DELIM + "world.mt").c_str()))
988 errorstream << "Failed to update world.mt!" << std::endl;
990 actionstream << "world.mt updated" << std::endl;