]> git.lizzy.rs Git - minetest.git/blobdiff - src/main.cpp
Fix crash on attaching player to entity
[minetest.git] / src / main.cpp
index 33cb34f68fbbb900c8b3dd14bc407254813e2dfb..f63b6a986e5de3c1f3b11e9634d9dbc4949884f0 100644 (file)
@@ -18,22 +18,21 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 */
 
 #ifdef _MSC_VER
-#ifndef SERVER // Dedicated server isn't linked with Irrlicht
-       #pragma comment(lib, "Irrlicht.lib")
-       // This would get rid of the console window
-       //#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
-#endif
+       #ifndef SERVER // Dedicated server isn't linked with Irrlicht
+               #pragma comment(lib, "Irrlicht.lib")
+               // This would get rid of the console window
+               //#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
+       #endif
        #pragma comment(lib, "zlibwapi.lib")
        #pragma comment(lib, "Shell32.lib")
 #endif
 
 #include "irrlicht.h" // createDevice
 
-#include "main.h"
 #include "mainmenumanager.h"
 #include "irrlichttypes_extrabloated.h"
 #include "debug.h"
-#include "test.h"
+#include "unittest/test.h"
 #include "server.h"
 #include "filesys.h"
 #include "version.h"
@@ -41,62 +40,34 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "game.h"
 #include "defaultsettings.h"
 #include "gettext.h"
-#include "profiler.h"
 #include "log.h"
 #include "quicktune.h"
 #include "httpfetch.h"
 #include "guiEngine.h"
+#include "map.h"
+#include "player.h"
 #include "mapsector.h"
 #include "fontengine.h"
 #include "gameparams.h"
+#include "database.h"
+#include "config.h"
+#if USE_CURSES
+       #include "terminal_chat_console.h"
+#endif
 #ifndef SERVER
 #include "client/clientlauncher.h"
 #endif
 
-#include "database-sqlite3.h"
-#ifdef USE_LEVELDB
-#include "database-leveldb.h"
-#endif
-
-#if USE_REDIS
-#include "database-redis.h"
-#endif
-
 #ifdef HAVE_TOUCHSCREENGUI
-#include "touchscreengui.h"
+       #include "touchscreengui.h"
 #endif
 
-/*
-       Settings.
-       These are loaded from the config file.
-*/
-static Settings main_settings;
-Settings *g_settings = &main_settings;
-std::string g_settings_path;
-
-// Global profiler
-Profiler main_profiler;
-Profiler *g_profiler = &main_profiler;
-
-// Menu clouds are created later
-Clouds *g_menuclouds = 0;
-irr::scene::ISceneManager *g_menucloudsmgr = 0;
-
-/*
-       Debug streams
-*/
-
-// Connection
-std::ostream *dout_con_ptr = &dummyout;
-std::ostream *derr_con_ptr = &verbosestream;
-
-// Server
-std::ostream *dout_server_ptr = &infostream;
-std::ostream *derr_server_ptr = &errorstream;
-
-// Client
-std::ostream *dout_client_ptr = &infostream;
-std::ostream *derr_client_ptr = &errorstream;
+#if !defined(SERVER) && \
+       (IRRLICHT_VERSION_MAJOR == 1) && \
+       (IRRLICHT_VERSION_MINOR == 8) && \
+       (IRRLICHT_VERSION_REVISION == 2)
+       #error "Irrlicht 1.8.2 is known to be broken - please update Irrlicht to version >= 1.8.3"
+#endif
 
 #define DEBUGFILE "debug.txt"
 #define DEFAULT_SERVER_PORT 30000
@@ -121,10 +92,10 @@ static void list_game_ids();
 static void list_worlds();
 static void setup_log_params(const Settings &cmd_args);
 static bool create_userdata_path();
-static bool init_common(int *log_level, const Settings &cmd_args, int argc, char *argv[]);
+static bool init_common(const Settings &cmd_args, int argc, char *argv[]);
 static void startup_message();
 static bool read_config_file(const Settings &cmd_args);
-static void init_debug_streams(int *log_level, const Settings &cmd_args);
+static void init_log_streams(const Settings &cmd_args);
 
 static bool game_configure(GameParams *game_params, const Settings &cmd_args);
 static void game_configure_port(GameParams *game_params, const Settings &cmd_args);
@@ -140,31 +111,10 @@ static bool get_game_from_cmdline(GameParams *game_params, const Settings &cmd_a
 static bool determine_subgame(GameParams *game_params);
 
 static bool run_dedicated_server(const GameParams &game_params, const Settings &cmd_args);
-static bool migrate_database(const GameParams &game_params, const Settings &cmd_args,
-               Server *server);
+static bool migrate_database(const GameParams &game_params, const Settings &cmd_args);
 
 /**********************************************************************/
 
-#ifndef SERVER
-/*
-       Random stuff
-*/
-
-/* mainmenumanager.h */
-
-gui::IGUIEnvironment* guienv = NULL;
-gui::IGUIStaticText *guiroot = NULL;
-MainMenuManager g_menumgr;
-
-bool noMenuActive()
-{
-       return (g_menumgr.menuCount() == 0);
-}
-
-// Passed to menus to allow disconnecting and exiting
-MainGameCallback *g_gamecallback = NULL;
-#endif
-
 /*
        gettime.h implementation
 */
@@ -184,25 +134,7 @@ u32 getTime(TimePrecision prec)
 
 #endif
 
-class StderrLogOutput: public ILogOutput
-{
-public:
-       /* line: Full line with timestamp, level and thread */
-       void printLog(const std::string &line)
-       {
-               std::cerr << line << std::endl;
-       }
-} main_stderr_log_out;
-
-class DstreamNoStderrLogOutput: public ILogOutput
-{
-public:
-       /* line: Full line with timestamp, level and thread */
-       void printLog(const std::string &line)
-       {
-               dstream_no_stderr << line << std::endl;
-       }
-} main_dstream_no_stderr_log_out;
+FileLogOutput file_log_output;
 
 static OptionList allowed_options;
 
@@ -212,10 +144,8 @@ int main(int argc, char *argv[])
 
        debug_set_exception_handler();
 
-       log_add_output_maxlev(&main_stderr_log_out, LMT_ACTION);
-       log_add_output_all_levs(&main_dstream_no_stderr_log_out);
-
-       log_register_thread("main");
+       g_logger.registerThread("Main");
+       g_logger.addOutputMaxLevel(&stderr_output, LL_ACTION);
 
        Settings cmd_args;
        bool cmd_args_ok = get_cmdline_opts(argc, argv, &cmd_args);
@@ -234,7 +164,13 @@ int main(int argc, char *argv[])
        setup_log_params(cmd_args);
 
        porting::signal_handler_init();
+
+#ifdef __ANDROID__
+       porting::initAndroid();
+       porting::initializePathsAndroid();
+#else
        porting::initializePaths();
+#endif
 
        if (!create_userdata_path()) {
                errorstream << "Cannot create user data directory" << std::endl;
@@ -242,8 +178,7 @@ int main(int argc, char *argv[])
        }
 
        // Initialize debug stacks
-       debug_stacks_init();
-       DSTACK(__FUNCTION_NAME);
+       DSTACK(FUNCTION_NAME);
 
        // Debug handler
        BEGIN_DEBUG_EXCEPTION_HANDLER
@@ -260,18 +195,17 @@ int main(int argc, char *argv[])
                return 0;
        }
 
-       GameParams game_params;
-       if (!init_common(&game_params.log_level, cmd_args, argc, argv))
+       if (!init_common(cmd_args, argc, argv))
                return 1;
 
 #ifndef __ANDROID__
        // Run unit tests
-       if (cmd_args.getFlag("do-unittests")) {
-               run_tests();
-               return 0;
+       if (cmd_args.getFlag("run-unittests")) {
+               return run_tests();
        }
 #endif
 
+       GameParams game_params;
 #ifdef SERVER
        game_params.is_dedicated_server = true;
 #else
@@ -281,7 +215,7 @@ int main(int argc, char *argv[])
        if (!game_configure(&game_params, cmd_args))
                return 1;
 
-       assert(game_params.world_path != "");
+       sanity_check(!game_params.world_path.empty());
 
        infostream << "Using commanded world path ["
                   << game_params.world_path << "]" << std::endl;
@@ -309,7 +243,7 @@ int main(int argc, char *argv[])
        // Stop httpfetch thread (if started)
        httpfetch_cleanup();
 
-       END_DEBUG_EXCEPTION_HANDLER(errorstream)
+       END_DEBUG_EXCEPTION_HANDLER
 
        return retval;
 }
@@ -339,7 +273,7 @@ static void set_allowed_options(OptionList *allowed_options)
                        _("Load configuration from specified file"))));
        allowed_options->insert(std::make_pair("port", ValueSpec(VALUETYPE_STRING,
                        _("Set network port (UDP)"))));
-       allowed_options->insert(std::make_pair("do-unittests", ValueSpec(VALUETYPE_FLAG,
+       allowed_options->insert(std::make_pair("run-unittests", ValueSpec(VALUETYPE_FLAG,
                        _("Run the unit tests and exit"))));
        allowed_options->insert(std::make_pair("map-dir", ValueSpec(VALUETYPE_STRING,
                        _("Same as --world (deprecated)"))));
@@ -361,6 +295,8 @@ static void set_allowed_options(OptionList *allowed_options)
                        _("Set gameid (\"--gameid list\" prints available ones)"))));
        allowed_options->insert(std::make_pair("migrate", ValueSpec(VALUETYPE_STRING,
                        _("Migrate from current map backend to another (Only works when using minetestserver or with --server)"))));
+       allowed_options->insert(std::make_pair("terminal", ValueSpec(VALUETYPE_FLAG,
+                       _("Feature an interactive terminal (Only works when using minetestserver or with --server)"))));
 #ifndef SERVER
        allowed_options->insert(std::make_pair("videomodes", ValueSpec(VALUETYPE_FLAG,
                        _("Show available video modes"))));
@@ -384,7 +320,7 @@ static void set_allowed_options(OptionList *allowed_options)
 
 static void print_help(const OptionList &allowed_options)
 {
-       dstream << _("Allowed options:") << std::endl;
+       std::cout << _("Allowed options:") << std::endl;
        print_allowed_options(allowed_options);
 }
 
@@ -397,39 +333,38 @@ static void print_allowed_options(const OptionList &allowed_options)
                if (i->second.type != VALUETYPE_FLAG)
                        os1 << _(" <value>");
 
-               dstream << padStringRight(os1.str(), 24);
+               std::cout << padStringRight(os1.str(), 24);
 
                if (i->second.help != NULL)
-                       dstream << i->second.help;
+                       std::cout << i->second.help;
 
-               dstream << std::endl;
+               std::cout << std::endl;
        }
 }
 
 static void print_version()
 {
-#ifdef SERVER
-       dstream << "minetestserver " << minetest_version_hash << std::endl;
-#else
-       dstream << "Minetest " << minetest_version_hash << std::endl;
-       dstream << "Using Irrlicht " << IRRLICHT_SDK_VERSION << std::endl;
+       std::cout << PROJECT_NAME_C " " << g_version_hash
+                 << " (" << porting::getPlatformName() << ")" << std::endl;
+#ifndef SERVER
+       std::cout << "Using Irrlicht " << IRRLICHT_SDK_VERSION << std::endl;
 #endif
-       dstream << "Build info: " << minetest_build_info << std::endl;
+       std::cout << "Build info: " << g_build_info << std::endl;
 }
 
 static void list_game_ids()
 {
        std::set<std::string> gameids = getAvailableGameIds();
        for (std::set<std::string>::const_iterator i = gameids.begin();
-                       i != gameids.end(); i++)
-               dstream << (*i) <<std::endl;
+                       i != gameids.end(); ++i)
+               std::cout << (*i) <<std::endl;
 }
 
 static void list_worlds()
 {
-       dstream << _("Available worlds:") << std::endl;
+       std::cout << _("Available worlds:") << std::endl;
        std::vector<WorldSpec> worldspecs = getAvailableWorlds();
-       print_worldspecs(worldspecs, dstream);
+       print_worldspecs(worldspecs, std::cout);
 }
 
 static void print_worldspecs(const std::vector<WorldSpec> &worldspecs,
@@ -467,26 +402,26 @@ static void setup_log_params(const Settings &cmd_args)
 {
        // Quiet mode, print errors only
        if (cmd_args.getFlag("quiet")) {
-               log_remove_output(&main_stderr_log_out);
-               log_add_output_maxlev(&main_stderr_log_out, LMT_ERROR);
+               g_logger.removeOutput(&stderr_output);
+               g_logger.addOutputMaxLevel(&stderr_output, LL_ERROR);
        }
 
        // If trace is enabled, enable logging of certain things
        if (cmd_args.getFlag("trace")) {
                dstream << _("Enabling trace level debug output") << std::endl;
-               log_trace_level_enabled = true;
-               dout_con_ptr = &verbosestream; // this is somewhat old crap
-               socket_enable_debug_output = true; // socket doesn't use log.h
+               g_logger.setTraceEnabled(true);
+               dout_con_ptr = &verbosestream; // This is somewhat old
+               socket_enable_debug_output = true; // Sockets doesn't use log.h
        }
 
        // In certain cases, output info level on stderr
        if (cmd_args.getFlag("info") || cmd_args.getFlag("verbose") ||
                        cmd_args.getFlag("trace") || cmd_args.getFlag("speedtests"))
-               log_add_output(&main_stderr_log_out, LMT_INFO);
+               g_logger.addOutput(&stderr_output, LL_INFO);
 
        // In certain cases, output verbose level on stderr
        if (cmd_args.getFlag("verbose") || cmd_args.getFlag("trace"))
-               log_add_output(&main_stderr_log_out, LMT_VERBOSE);
+               g_logger.addOutput(&stderr_output, LL_VERBOSE);
 }
 
 static bool create_userdata_path()
@@ -494,9 +429,6 @@ static bool create_userdata_path()
        bool success;
 
 #ifdef __ANDROID__
-       porting::initAndroid();
-
-       porting::setExternalStorageDir(porting::jnienv);
        if (!fs::PathExists(porting::path_user)) {
                success = fs::CreateDir(porting::path_user);
        } else {
@@ -508,13 +440,10 @@ static bool create_userdata_path()
        success = fs::CreateDir(porting::path_user);
 #endif
 
-       infostream << "path_share = " << porting::path_share << std::endl;
-       infostream << "path_user  = " << porting::path_user << std::endl;
-
        return success;
 }
 
-static bool init_common(int *log_level, const Settings &cmd_args, int argc, char *argv[])
+static bool init_common(const Settings &cmd_args, int argc, char *argv[])
 {
        startup_message();
        set_default_settings(g_settings);
@@ -526,7 +455,7 @@ static bool init_common(int *log_level, const Settings &cmd_args, int argc, char
        if (!read_config_file(cmd_args))
                return false;
 
-       init_debug_streams(log_level, cmd_args);
+       init_log_streams(cmd_args);
 
        // Initialize random seed
        srand(time(0));
@@ -535,13 +464,8 @@ static bool init_common(int *log_level, const Settings &cmd_args, int argc, char
        // Initialize HTTP fetcher
        httpfetch_init(g_settings->getS32("curl_parallel_limit"));
 
-#ifdef _MSC_VER
-       init_gettext((porting::path_share + DIR_DELIM + "locale").c_str(),
+       init_gettext(porting::path_locale.c_str(),
                g_settings->get("language"), argc, argv);
-#else
-       init_gettext((porting::path_share + DIR_DELIM + "locale").c_str(),
-               g_settings->get("language"));
-#endif
 
        return true;
 }
@@ -551,13 +475,13 @@ static void startup_message()
        infostream << PROJECT_NAME << " " << _("with")
                   << " SER_FMT_VER_HIGHEST_READ="
                << (int)SER_FMT_VER_HIGHEST_READ << ", "
-               << minetest_build_info << std::endl;
+               << g_build_info << std::endl;
 }
 
 static bool read_config_file(const Settings &cmd_args)
 {
        // Path of configuration file in use
-       assert(g_settings_path == "");  // Sanity check
+       sanity_check(g_settings_path == "");    // Sanity check
 
        if (cmd_args.exists("config")) {
                bool r = g_settings->readConfigFile(cmd_args.get("config").c_str());
@@ -597,38 +521,47 @@ static bool read_config_file(const Settings &cmd_args)
        return true;
 }
 
-static void init_debug_streams(int *log_level, const Settings &cmd_args)
+static void init_log_streams(const Settings &cmd_args)
 {
 #if RUN_IN_PLACE
-       std::string logfile = DEBUGFILE;
+       std::string log_filename = DEBUGFILE;
 #else
-       std::string logfile = porting::path_user + DIR_DELIM + DEBUGFILE;
+       std::string log_filename = porting::path_user + DIR_DELIM + DEBUGFILE;
 #endif
        if (cmd_args.exists("logfile"))
-               logfile = cmd_args.get("logfile");
-
-       log_remove_output(&main_dstream_no_stderr_log_out);
-       *log_level = g_settings->getS32("debug_log_level");
-
-       if (*log_level == 0) //no logging
-               logfile = "";
-       if (*log_level < 0) {
-               dstream << "WARNING: Supplied debug_log_level < 0; Using 0" << std::endl;
-               *log_level = 0;
-       } else if (*log_level > LMT_NUM_VALUES) {
-               dstream << "WARNING: Supplied debug_log_level > " << LMT_NUM_VALUES
-                       << "; Using " << LMT_NUM_VALUES << std::endl;
-               *log_level = LMT_NUM_VALUES;
+               log_filename = cmd_args.get("logfile");
+
+       g_logger.removeOutput(&file_log_output);
+       std::string conf_loglev = g_settings->get("debug_log_level");
+
+       // Old integer format
+       if (std::isdigit(conf_loglev[0])) {
+               warningstream << "Deprecated use of debug_log_level with an "
+                       "integer value; please update your configuration." << std::endl;
+               static const char *lev_name[] =
+                       {"", "error", "action", "info", "verbose"};
+               int lev_i = atoi(conf_loglev.c_str());
+               if (lev_i < 0 || lev_i >= (int)ARRLEN(lev_name)) {
+                       warningstream << "Supplied invalid debug_log_level!"
+                               "  Assuming action level." << std::endl;
+                       lev_i = 2;
+               }
+               conf_loglev = lev_name[lev_i];
        }
 
-       log_add_output_maxlev(&main_dstream_no_stderr_log_out,
-                       (LogMessageLevel)(*log_level - 1));
+       if (log_filename.empty() || conf_loglev.empty())  // No logging
+               return;
 
-       debugstreams_init(false, logfile == "" ? NULL : logfile.c_str());
+       LogLevel log_level = Logger::stringToLevel(conf_loglev);
+       if (log_level == LL_MAX) {
+               warningstream << "Supplied unrecognized debug_log_level; "
+                       "using maximum." << std::endl;
+       }
 
-       infostream << "logfile = " << logfile << std::endl;
+       verbosestream << "log_filename = " << log_filename << std::endl;
 
-       atexit(debugstreams_deinit);
+       file_log_output.open(log_filename.c_str());
+       g_logger.addOutputMaxLevel(&file_log_output, log_level);
 }
 
 static bool game_configure(GameParams *game_params, const Settings &cmd_args)
@@ -742,10 +675,10 @@ static bool auto_select_world(GameParams *game_params)
                        << world_path << "]" << std::endl;
        // If there are multiple worlds, list them
        } else if (worldspecs.size() > 1 && game_params->is_dedicated_server) {
-               dstream << _("Multiple worlds are available.") << std::endl;
-               dstream << _("Please select one using --worldname <name>"
+               std::cerr << _("Multiple worlds are available.") << std::endl;
+               std::cerr << _("Please select one using --worldname <name>"
                                " or --world <path>") << std::endl;
-               print_worldspecs(worldspecs, dstream);
+               print_worldspecs(worldspecs, std::cerr);
                return false;
        // If there are no worlds, automatically create a new one
        } else {
@@ -756,7 +689,7 @@ static bool auto_select_world(GameParams *game_params)
                           << world_path << "]" << std::endl;
        }
 
-       assert(world_path != "");
+       assert(world_path != "");       // Post-condition
        game_params->world_path = world_path;
        return true;
 }
@@ -812,7 +745,7 @@ static bool determine_subgame(GameParams *game_params)
 {
        SubgameSpec gamespec;
 
-       assert(game_params->world_path != "");  // pre-condition
+       assert(game_params->world_path != "");  // Pre-condition
 
        verbosestream << _("Determining gameid/gamespec") << std::endl;
        // If world doesn't exist
@@ -838,7 +771,7 @@ static bool determine_subgame(GameParams *game_params)
                if (game_params->game_spec.isValid()) {
                        gamespec = game_params->game_spec;
                        if (game_params->game_spec.id != world_gameid) {
-                               errorstream << "WARNING: Using commanded gameid ["
+                               warningstream << "Using commanded gameid ["
                                            << gamespec.id << "]" << " instead of world gameid ["
                                            << world_gameid << "]" << std::endl;
                        }
@@ -894,100 +827,154 @@ static bool run_dedicated_server(const GameParams &game_params, const Settings &
                return false;
        }
 
-       // Create server
-       Server server(game_params.world_path,
-                       game_params.game_spec, false, bind_addr.isIPv6());
-
        // Database migration
        if (cmd_args.exists("migrate"))
-               return migrate_database(game_params, cmd_args, &server);
+               return migrate_database(game_params, cmd_args);
+
+       if (cmd_args.exists("terminal")) {
+#if USE_CURSES
+               bool name_ok = true;
+               std::string admin_nick = g_settings->get("name");
+
+               name_ok = name_ok && !admin_nick.empty();
+               name_ok = name_ok && string_allowed(admin_nick, PLAYERNAME_ALLOWED_CHARS);
+
+               if (!name_ok) {
+                       if (admin_nick.empty()) {
+                               errorstream << "No name given for admin. "
+                                       << "Please check your minetest.conf that it "
+                                       << "contains a 'name = ' to your main admin account."
+                                       << std::endl;
+                       } else {
+                               errorstream << "Name for admin '"
+                                       << admin_nick << "' is not valid. "
+                                       << "Please check that it only contains allowed characters. "
+                                       << "Valid characters are: " << PLAYERNAME_ALLOWED_CHARS_USER_EXPL
+                                       << std::endl;
+                       }
+                       return false;
+               }
+               ChatInterface iface;
+               bool &kill = *porting::signal_handler_killstatus();
 
-       server.start(bind_addr);
+               try {
+                       // Create server
+                       Server server(game_params.world_path,
+                               game_params.game_spec, false, bind_addr.isIPv6(), &iface);
 
-       // Run server
-       bool &kill = *porting::signal_handler_killstatus();
-       dedicated_server_loop(server, kill);
+                       g_term_console.setup(&iface, &kill, admin_nick);
+
+                       g_term_console.start();
+
+                       server.start(bind_addr);
+                       // Run server
+                       dedicated_server_loop(server, kill);
+               } catch (const ModError &e) {
+                       g_term_console.stopAndWaitforThread();
+                       errorstream << "ModError: " << e.what() << std::endl;
+                       return false;
+               } catch (const ServerError &e) {
+                       g_term_console.stopAndWaitforThread();
+                       errorstream << "ServerError: " << e.what() << std::endl;
+                       return false;
+               }
+
+               // Tell the console to stop, and wait for it to finish,
+               // only then leave context and free iface
+               g_term_console.stop();
+               g_term_console.wait();
+
+               g_term_console.clearKillStatus();
+       } else {
+#else
+               errorstream << "Cmd arg --terminal passed, but "
+                       << "compiled without ncurses. Ignoring." << std::endl;
+       } {
+#endif
+               try {
+                       // Create server
+                       Server server(game_params.world_path, game_params.game_spec, false,
+                               bind_addr.isIPv6());
+                       server.start(bind_addr);
+
+                       // Run server
+                       bool &kill = *porting::signal_handler_killstatus();
+                       dedicated_server_loop(server, kill);
+
+               } catch (const ModError &e) {
+                       errorstream << "ModError: " << e.what() << std::endl;
+                       return false;
+               } catch (const ServerError &e) {
+                       errorstream << "ServerError: " << e.what() << std::endl;
+                       return false;
+               }
+       }
 
        return true;
 }
 
-static bool migrate_database(const GameParams &game_params, const Settings &cmd_args,
-               Server *server)
+static bool migrate_database(const GameParams &game_params, const Settings &cmd_args)
 {
+       std::string migrate_to = cmd_args.get("migrate");
        Settings world_mt;
-       bool success = world_mt.readConfigFile((game_params.world_path
-                       + DIR_DELIM + "world.mt").c_str());
-       if (!success) {
-               errorstream << "Cannot read world.mt" << std::endl;
+       std::string world_mt_path = game_params.world_path + DIR_DELIM + "world.mt";
+       if (!world_mt.readConfigFile(world_mt_path.c_str())) {
+               errorstream << "Cannot read world.mt!" << std::endl;
                return false;
        }
-
        if (!world_mt.exists("backend")) {
-               errorstream << "Please specify your current backend in world.mt file:"
-                           << std::endl << "   backend = {sqlite3|leveldb|redis|dummy}"
-                           << std::endl;
+               errorstream << "Please specify your current backend in world.mt:"
+                       << std::endl
+                       << "    backend = {sqlite3|leveldb|redis|dummy}"
+                       << std::endl;
                return false;
        }
-
        std::string backend = world_mt.get("backend");
-       Database *new_db;
-       std::string migrate_to = cmd_args.get("migrate");
-
        if (backend == migrate_to) {
-               errorstream << "Cannot migrate: new backend is same as the old one"
-                           << std::endl;
+               errorstream << "Cannot migrate: new backend is same"
+                       << " as the old one" << std::endl;
                return false;
        }
+       Database *old_db = ServerMap::createDatabase(backend, game_params.world_path, world_mt),
+               *new_db = ServerMap::createDatabase(migrate_to, game_params.world_path, world_mt);
 
-       if (migrate_to == "sqlite3")
-               new_db = new Database_SQLite3(&(ServerMap&)server->getMap(),
-                               game_params.world_path);
-#if USE_LEVELDB
-       else if (migrate_to == "leveldb")
-               new_db = new Database_LevelDB(&(ServerMap&)server->getMap(),
-                               game_params.world_path);
-#endif
-#if USE_REDIS
-       else if (migrate_to == "redis")
-               new_db = new Database_Redis(&(ServerMap&)server->getMap(),
-                               game_params.world_path);
-#endif
-       else {
-               errorstream << "Migration to " << migrate_to << " is not supported"
-                           << std::endl;
-               return false;
-       }
+       u32 count = 0;
+       time_t last_update_time = 0;
+       bool &kill = *porting::signal_handler_killstatus();
 
        std::vector<v3s16> blocks;
-       ServerMap &old_map = ((ServerMap&)server->getMap());
-       old_map.listAllLoadableBlocks(blocks);
-       int count = 0;
+       old_db->listAllLoadableBlocks(blocks);
        new_db->beginSave();
-       for (std::vector<v3s16>::iterator i = blocks.begin(); i != blocks.end(); i++) {
-               MapBlock *block = old_map.loadBlock(*i);
-               if (!block) {
-                       errorstream << "Failed to load block " << PP(*i) << ", skipping it.";
+       for (std::vector<v3s16>::const_iterator it = blocks.begin(); it != blocks.end(); ++it) {
+               if (kill) return false;
+
+               std::string data;
+               old_db->loadBlock(*it, &data);
+               if (!data.empty()) {
+                       new_db->saveBlock(*it, data);
+               } else {
+                       errorstream << "Failed to load block " << PP(*it) << ", skipping it." << std::endl;
                }
-               else {
-                       old_map.saveBlock(block, new_db);
-                       MapSector *sector = old_map.getSectorNoGenerate(v2s16(i->X, i->Z));
-                       sector->deleteBlock(block);
+               if (++count % 0xFF == 0 && time(NULL) - last_update_time >= 1) {
+                       std::cerr << " Migrated " << count << " blocks, "
+                               << (100.0 * count / blocks.size()) << "% completed.\r";
+                       new_db->endSave();
+                       new_db->beginSave();
+                       last_update_time = time(NULL);
                }
-               ++count;
-               if (count % 500 == 0)
-                  actionstream << "Migrated " << count << " blocks "
-                          << (100.0 * count / blocks.size()) << "% completed" << std::endl;
        }
+       std::cerr << std::endl;
        new_db->endSave();
+       delete old_db;
        delete new_db;
 
        actionstream << "Successfully migrated " << count << " blocks" << std::endl;
        world_mt.set("backend", migrate_to);
-       if (!world_mt.updateConfigFile(
-                               (game_params.world_path+ DIR_DELIM + "world.mt").c_str()))
+       if (!world_mt.updateConfigFile(world_mt_path.c_str()))
                errorstream << "Failed to update world.mt!" << std::endl;
        else
                actionstream << "world.mt updated" << std::endl;
 
        return true;
 }
+