]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/main.cpp
Add a callback: minetest.register_on_craft(itemstack, player,
[dragonfireclient.git] / src / main.cpp
index 716da57c393e9b9cebd85bc9e46c46b5d5c08412..455138d5b690ac136f2f28900722ebb701f329d5 100644 (file)
@@ -58,6 +58,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "guiMessageMenu.h"
 #include "filesys.h"
 #include "config.h"
+#include "version.h"
 #include "guiMainMenu.h"
 #include "game.h"
 #include "keycode.h"
@@ -77,6 +78,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "quicktune.h"
 #include "serverlist.h"
 #include "guiEngine.h"
+#include "mapsector.h"
+
+#include "database-sqlite3.h"
+#ifdef USE_LEVELDB
+#include "database-leveldb.h"
+#endif
 
 /*
        Settings.
@@ -84,6 +91,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 */
 Settings main_settings;
 Settings *g_settings = &main_settings;
+std::string g_settings_path;
 
 // Global profiler
 Profiler main_profiler;
@@ -243,7 +251,7 @@ class MyEventReceiver : public IEventReceiver
                */
                if(noMenuActive() == false)
                {
-                       return false;
+                       return g_menumgr.preprocessEvent(event);
                }
 
                // Remember whether each key is down or up
@@ -764,6 +772,8 @@ int main(int argc, char *argv[])
        std::map<std::string, ValueSpec> allowed_options;
        allowed_options.insert(std::make_pair("help", ValueSpec(VALUETYPE_FLAG,
                        _("Show allowed options"))));
+       allowed_options.insert(std::make_pair("version", ValueSpec(VALUETYPE_FLAG,
+                       _("Show version information"))));
        allowed_options.insert(std::make_pair("config", ValueSpec(VALUETYPE_STRING,
                        _("Load configuration from specified file"))));
        allowed_options.insert(std::make_pair("port", ValueSpec(VALUETYPE_STRING,
@@ -788,6 +798,8 @@ int main(int argc, char *argv[])
                        _("Set logfile path ('' = no logging)"))));
        allowed_options.insert(std::make_pair("gameid", ValueSpec(VALUETYPE_STRING,
                        _("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)"))));
 #ifndef SERVER
        allowed_options.insert(std::make_pair("videomodes", ValueSpec(VALUETYPE_FLAG,
                        _("Show available video modes"))));
@@ -833,6 +845,18 @@ int main(int argc, char *argv[])
 
                return cmd_args.getFlag("help") ? 0 : 1;
        }
+
+       if(cmd_args.getFlag("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;
+#endif
+               dstream<<"Build info: "<<minetest_build_info<<std::endl;
+               return 0;
+       }
        
        /*
                Low-level initialization
@@ -893,8 +917,8 @@ int main(int argc, char *argv[])
 
        // Print startup message
        infostream<<PROJECT_NAME<<
-                       " "<<_("with")<<" SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
-                       <<", "<<BUILD_INFO
+                       " "<<_("with")<<" SER_FMT_VER_HIGHEST_READ="<<(int)SER_FMT_VER_HIGHEST_READ
+                       <<", "<<minetest_build_info
                        <<std::endl;
        
        /*
@@ -913,7 +937,7 @@ int main(int argc, char *argv[])
        */
        
        // Path of configuration file in use
-       std::string configpath = "";
+       g_settings_path = "";
        
        if(cmd_args.exists("config"))
        {
@@ -924,7 +948,7 @@ int main(int argc, char *argv[])
                                        <<cmd_args.get("config")<<"\""<<std::endl;
                        return 1;
                }
-               configpath = cmd_args.get("config");
+               g_settings_path = cmd_args.get("config");
        }
        else
        {
@@ -946,14 +970,14 @@ int main(int argc, char *argv[])
                        bool r = g_settings->readConfigFile(filenames[i].c_str());
                        if(r)
                        {
-                               configpath = filenames[i];
+                               g_settings_path = filenames[i];
                                break;
                        }
                }
                
                // If no path found, use the first one (menu creates the file)
-               if(configpath == "")
-                       configpath = filenames[0];
+               if(g_settings_path == "")
+                       g_settings_path = filenames[0];
        }
        
        // Initialize debug streams
@@ -994,7 +1018,19 @@ int main(int argc, char *argv[])
        {
                run_tests();
        }
-       
+
+       std::string language = g_settings->get("language");
+       if (language.length()) {
+#ifndef _WIN32
+               setenv("LANGUAGE", language.c_str(), 1);
+#else
+               char *lang_str = (char*)calloc(10 + language.length(), sizeof(char));
+               strcat(lang_str, "LANGUAGE=");
+               strcat(lang_str, language.c_str());
+               putenv(lang_str);
+#endif
+       }
+
        /*
                Game parameters
        */
@@ -1072,6 +1108,7 @@ int main(int argc, char *argv[])
                }
        }
 
+
        /*
                Run dedicated server if asked to or no other option
        */
@@ -1193,7 +1230,66 @@ int main(int argc, char *argv[])
                verbosestream<<_("Using gameid")<<" ["<<gamespec.id<<"]"<<std::endl;
 
                // Create server
-               Server server(world_path, configpath, gamespec, false);
+               Server server(world_path, gamespec, false);
+
+               // Database migration
+               if (cmd_args.exists("migrate")) {
+                       std::string migrate_to = cmd_args.get("migrate");
+                       Settings world_mt;
+                       bool success = world_mt.readConfigFile((world_path + DIR_DELIM + "world.mt").c_str());
+                       if (!success) {
+                               errorstream << "Cannot read world.mt" << std::endl;
+                               return 1;
+                       }
+                       if (!world_mt.exists("backend")) {
+                               errorstream << "Please specify your current backend in world.mt file:"
+                                       << std::endl << "       backend = {sqlite3|leveldb|dummy}" << std::endl;
+                               return 1;
+                       }
+                       std::string backend = world_mt.get("backend");
+                       Database *new_db;
+                       if (backend == migrate_to) {
+                               errorstream << "Cannot migrate: new backend is same as the old one" << std::endl;
+                               return 1;
+                       }
+                       if (migrate_to == "sqlite3")
+                               new_db = new Database_SQLite3(&(ServerMap&)server.getMap(), world_path);
+                       #if USE_LEVELDB
+                       else if (migrate_to == "leveldb")
+                               new_db = new Database_LevelDB(&(ServerMap&)server.getMap(), world_path);
+                       #endif
+                       else {
+                               errorstream << "Migration to " << migrate_to << " is not supported" << std::endl;
+                               return 1;
+                       }
+
+                       std::list<v3s16> blocks;
+                       ServerMap &old_map = ((ServerMap&)server.getMap());
+                       old_map.listAllLoadableBlocks(blocks);
+                       int count = 0;
+                       new_db->beginSave();
+                       for (std::list<v3s16>::iterator i = blocks.begin(); i != blocks.end(); ++i) {
+                               MapBlock *block = old_map.loadBlock(*i);
+                               new_db->saveBlock(block);
+                               MapSector *sector = old_map.getSectorNoGenerate(v2s16(i->X, i->Z));
+                               sector->deleteBlock(block);
+                               ++count;
+                               if (count % 500 == 0)
+                                       actionstream << "Migrated " << count << " blocks "
+                                               << (100.0 * count / blocks.size()) << "\% completed" << std::endl;
+                       }
+                       new_db->endSave();
+
+                       actionstream << "Successfully migrated " << count << " blocks" << std::endl;
+                       world_mt.set("backend", migrate_to);
+                       if(!world_mt.updateConfigFile((world_path + DIR_DELIM + "world.mt").c_str()))
+                               errorstream<<"Failed to update world.mt!"<<std::endl;
+                       else
+                               actionstream<<"world.mt updated"<<std::endl;
+
+                       return 0;
+               }
+
                server.start(port);
                
                // Run server
@@ -1287,6 +1383,7 @@ int main(int argc, char *argv[])
                params.Stencilbuffer = false;
                params.Vsync         = vsync;
                params.EventReceiver = &receiver;
+               params.HighPrecisionFPU = g_settings->getBool("high_precision_fpu");
 
                nulldevice = createDeviceEx(params);
 
@@ -1339,6 +1436,7 @@ int main(int argc, char *argv[])
        params.Stencilbuffer = false;
        params.Vsync         = vsync;
        params.EventReceiver = &receiver;
+       params.HighPrecisionFPU = g_settings->getBool("high_precision_fpu");
 
        device = createDeviceEx(params);
 
@@ -1388,12 +1486,22 @@ int main(int argc, char *argv[])
 
        guienv = device->getGUIEnvironment();
        gui::IGUISkin* skin = guienv->getSkin();
-       #if USE_FREETYPE
        std::string font_path = g_settings->get("font_path");
-       u16 font_size = g_settings->getU16("font_size");
-       gui::IGUIFont *font = gui::CGUITTFont::createTTFont(guienv, font_path.c_str(), font_size);
+       gui::IGUIFont *font;
+       #if USE_FREETYPE
+       bool use_freetype = g_settings->getBool("freetype");
+       if (use_freetype) {
+               std::string fallback;
+               if (is_yes(gettext("needs_fallback_font")))
+                       fallback = "fallback_";
+               u16 font_size = g_settings->getU16(fallback + "font_size");
+               font_path = g_settings->get(fallback + "font_path");
+               font = gui::CGUITTFont::createTTFont(guienv, font_path.c_str(), font_size);
+       } else {
+               font = guienv->getFont(font_path.c_str());
+       }
        #else
-       gui::IGUIFont* font = guienv->getFont(getTexturePath("fontlucida.png").c_str());
+       font = guienv->getFont(font_path.c_str());
        #endif
        if(font)
                skin->setFont(font);
@@ -1512,7 +1620,6 @@ int main(int argc, char *argv[])
                                
                                // Initialize menu data
                                MainMenuData menudata;
-                               menudata.kill = kill;
                                menudata.address = address;
                                menudata.name = playername;
                                menudata.port = itos(port);
@@ -1558,13 +1665,16 @@ int main(int argc, char *argv[])
                                        }
                                        infostream<<"Waited for other menus"<<std::endl;
 
-                                       GUIEngine* temp = new GUIEngine(device, guiroot, &g_menumgr,smgr,&menudata);
+                                       GUIEngine* temp = new GUIEngine(device, guiroot, &g_menumgr,smgr,&menudata,kill);
                                        
                                        delete temp;
                                        //once finished you'll never end up here
                                        smgr->clear();
-                                       kill = menudata.kill;
+                               }
 
+                               if(menudata.errormessage != ""){
+                                       error_message = narrow_to_wide(menudata.errormessage);
+                                       continue;
                                }
 
                                //update worldspecs (necessary as new world may have been created)
@@ -1591,7 +1701,7 @@ int main(int argc, char *argv[])
                                g_settings->set("port", itos(port));
 
                                if((menudata.selected_world >= 0) &&
-                                               (menudata.selected_world < worldspecs.size()))
+                                               (menudata.selected_world < (int)worldspecs.size()))
                                        g_settings->set("selected_world_path",
                                                        worldspecs[menudata.selected_world].path);
 
@@ -1623,7 +1733,7 @@ int main(int argc, char *argv[])
                                
                                // Set world path to selected one
                                if ((menudata.selected_world >= 0) &&
-                                       (menudata.selected_world < worldspecs.size())) {
+                                       (menudata.selected_world < (int)worldspecs.size())) {
                                        worldspec = worldspecs[menudata.selected_world];
                                        infostream<<"Selected world: "<<worldspec.name
                                                        <<" ["<<worldspec.path<<"]"<<std::endl;
@@ -1669,7 +1779,10 @@ int main(int argc, char *argv[])
 
                        // Break out of menu-game loop to shut down cleanly
                        if(device->run() == false || kill == true) {
-                               g_settings->updateConfigFile(configpath.c_str());
+                               if(g_settings_path != "") {
+                                       g_settings->updateConfigFile(
+                                               g_settings_path.c_str());
+                               }
                                break;
                        }
 
@@ -1688,7 +1801,6 @@ int main(int argc, char *argv[])
                                current_address,
                                current_port,
                                error_message,
-                               configpath,
                                chat_backend,
                                gamespec,
                                simple_singleplayer_mode
@@ -1736,14 +1848,15 @@ int main(int argc, char *argv[])
        device->drop();
 
 #if USE_FREETYPE
-       font->drop();
+       if (use_freetype)
+               font->drop();
 #endif
 
 #endif // !SERVER
        
        // Update configuration file
-       if(configpath != "")
-               g_settings->updateConfigFile(configpath.c_str());
+       if(g_settings_path != "")
+               g_settings->updateConfigFile(g_settings_path.c_str());
        
        // Print modified quicktune values
        {