]> git.lizzy.rs Git - minetest.git/blobdiff - src/main.cpp
Biome API: Make fallback biome stone and water, disable filler
[minetest.git] / src / main.cpp
index 33cb34f68fbbb900c8b3dd14bc407254813e2dfb..1d73b40253003a6a37140e33daae2375cd13a61e 100644 (file)
@@ -29,11 +29,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #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,63 +40,23 @@ 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 "mapsector.h"
 #include "fontengine.h"
 #include "gameparams.h"
+#include "database.h"
 #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"
 #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;
-
 #define DEBUGFILE "debug.txt"
 #define DEFAULT_SERVER_PORT 30000
 
@@ -140,31 +99,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
 */
@@ -266,7 +204,7 @@ int main(int argc, char *argv[])
 
 #ifndef __ANDROID__
        // Run unit tests
-       if (cmd_args.getFlag("do-unittests")) {
+       if (cmd_args.getFlag("run-unittests")) {
                run_tests();
                return 0;
        }
@@ -281,7 +219,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 != "");
 
        infostream << "Using commanded world path ["
                   << game_params.world_path << "]" << std::endl;
@@ -339,7 +277,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)"))));
@@ -408,13 +346,11 @@ static void print_allowed_options(const OptionList &allowed_options)
 
 static void print_version()
 {
-#ifdef SERVER
-       dstream << "minetestserver " << minetest_version_hash << std::endl;
-#else
-       dstream << "Minetest " << minetest_version_hash << std::endl;
+       dstream << PROJECT_NAME_C " " << g_version_hash << std::endl;
+#ifndef SERVER
        dstream << "Using Irrlicht " << IRRLICHT_SDK_VERSION << std::endl;
 #endif
-       dstream << "Build info: " << minetest_build_info << std::endl;
+       dstream << "Build info: " << g_build_info << std::endl;
 }
 
 static void list_game_ids()
@@ -551,13 +487,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());
@@ -756,7 +692,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 +748,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
@@ -894,14 +830,14 @@ static bool run_dedicated_server(const GameParams &game_params, const Settings &
                return false;
        }
 
+       // Database migration
+       if (cmd_args.exists("migrate"))
+               return migrate_database(game_params, cmd_args);
+
        // 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);
-
        server.start(bind_addr);
 
        // Run server
@@ -911,83 +847,67 @@ static bool run_dedicated_server(const GameParams &game_params, const Settings &
        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;
+
+               const std::string &data = old_db->loadBlock(*it);
+               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;
 }
+