]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/main.cpp
Rename packethandler/{client,server}.cpp to {client,server}packethandler.cpp
[dragonfireclient.git] / src / main.cpp
index de9d953084fcc878c5da584b617f715e453a189c..d84a09f7616e3dd2edc18d6c14ef540c8a19b7a2 100644 (file)
@@ -17,19 +17,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#ifdef NDEBUG
-       /*#ifdef _WIN32
-               #pragma message ("Disabling unit tests")
-       #else
-               #warning "Disabling unit tests"
-       #endif*/
-       // Disable unit tests
-       #define ENABLE_TESTS 0
-#else
-       // Enable unit tests
-       #define ENABLE_TESTS 1
-#endif
-
 #ifdef _MSC_VER
 #ifndef SERVER // Dedicated server isn't linked with Irrlicht
        #pragma comment(lib, "Irrlicht.lib")
@@ -59,22 +46,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #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
@@ -153,8 +133,7 @@ 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);
 
 /**********************************************************************/
 
@@ -279,9 +258,9 @@ int main(int argc, char *argv[])
 
 #ifndef __ANDROID__
        // Run unit tests
-       if ((ENABLE_TESTS && cmd_args.getFlag("disable-unittests") == false)
-                       || cmd_args.getFlag("enable-unittests") == true) {
+       if (cmd_args.getFlag("run-unittests")) {
                run_tests();
+               return 0;
        }
 #endif
 
@@ -294,7 +273,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;
@@ -352,10 +331,8 @@ 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("disable-unittests", ValueSpec(VALUETYPE_FLAG,
-                       _("Disable unit tests"))));
-       allowed_options->insert(std::make_pair("enable-unittests", ValueSpec(VALUETYPE_FLAG,
-                       _("Enable unit tests"))));
+       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)"))));
        allowed_options->insert(std::make_pair("world", ValueSpec(VALUETYPE_STRING,
@@ -572,7 +549,7 @@ static void startup_message()
 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());
@@ -771,7 +748,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;
 }
@@ -827,7 +804,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
@@ -909,14 +886,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
@@ -926,82 +903,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::list<v3s16> blocks;
-       ServerMap &old_map = ((ServerMap&)server->getMap());
-       old_map.listAllLoadableBlocks(blocks);
-       int count = 0;
+       std::vector<v3s16> blocks;
+       old_db->listAllLoadableBlocks(blocks);
        new_db->beginSave();
-       for (std::list<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 {
-                       old_map.saveBlock(block, new_db);
-                       MapSector *sector = old_map.getSectorNoGenerate(v2s16(i->X, i->Z));
-                       sector->deleteBlock(block);
+                       errorstream << "Failed to load block " << PP(*it) << ", skipping it." << std::endl;
+               }
+               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;
 }
+