]> git.lizzy.rs Git - minetest.git/blobdiff - src/main.cpp
Handle certain errors properly when using --go
[minetest.git] / src / main.cpp
index 0c936ab63396936e7fc9531d8d630facb915ea98..0d1961fff1de7fc5d3daf4b2de217558729d724b 100644 (file)
@@ -68,6 +68,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "profiler.h"
 #include "log.h"
 #include "mods.h"
+#include "utility_string.h"
+#include "subgame.h"
 
 /*
        Settings.
@@ -740,6 +742,8 @@ void SpeedTests()
 
 int main(int argc, char *argv[])
 {
+       int retval = 0;
+
        /*
                Initialization
        */
@@ -765,26 +769,36 @@ int main(int argc, char *argv[])
        allowed_options.insert("config", ValueSpec(VALUETYPE_STRING,
                        "Load configuration from specified file"));
        allowed_options.insert("port", ValueSpec(VALUETYPE_STRING,
-                       "Set network port (UDP) to use"));
+                       "Set network port (UDP)"));
        allowed_options.insert("disable-unittests", ValueSpec(VALUETYPE_FLAG,
                        "Disable unit tests"));
        allowed_options.insert("enable-unittests", ValueSpec(VALUETYPE_FLAG,
                        "Enable unit tests"));
        allowed_options.insert("map-dir", ValueSpec(VALUETYPE_STRING,
-                       "Map directory (where everything in the world is stored)"));
-       allowed_options.insert("info-on-stderr", ValueSpec(VALUETYPE_FLAG,
-                       "Print more information to console (deprecated; use --verbose)"));
+                       "Same as --world (deprecated)"));
+       allowed_options.insert("world", ValueSpec(VALUETYPE_STRING,
+                       "Set world path (implies local game)"));
        allowed_options.insert("verbose", ValueSpec(VALUETYPE_FLAG,
                        "Print more information to console"));
+       allowed_options.insert("logfile", ValueSpec(VALUETYPE_STRING,
+                       "Set logfile path ('' = no logging)"));
+       allowed_options.insert("gameid", ValueSpec(VALUETYPE_STRING,
+                       "Set gameid (\"--gameid list\" prints available ones)"));
 #ifndef SERVER
        allowed_options.insert("speedtests", ValueSpec(VALUETYPE_FLAG,
                        "Run speed tests"));
        allowed_options.insert("address", ValueSpec(VALUETYPE_STRING,
-                       "Address to connect to"));
+                       "Address to connect to. ('' = local game)"));
        allowed_options.insert("random-input", ValueSpec(VALUETYPE_FLAG,
                        "Enable random user input, for testing"));
        allowed_options.insert("server", ValueSpec(VALUETYPE_FLAG,
-                       "Run server directly"));
+                       "Run dedicated server"));
+       allowed_options.insert("name", ValueSpec(VALUETYPE_STRING,
+                       "Set player name"));
+       allowed_options.insert("password", ValueSpec(VALUETYPE_STRING,
+                       "Set password"));
+       allowed_options.insert("go", ValueSpec(VALUETYPE_FLAG,
+                       "Disable main menu"));
 #endif
 
        Settings cmd_args;
@@ -798,21 +812,17 @@ int main(int argc, char *argv[])
                                i = allowed_options.getIterator();
                                i.atEnd() == false; i++)
                {
-                       dstream<<"  --"<<i.getNode()->getKey();
+                       std::ostringstream os1(std::ios::binary);
+                       os1<<"  --"<<i.getNode()->getKey();
                        if(i.getNode()->getValue().type == VALUETYPE_FLAG)
-                       {
-                       }
+                               {}
                        else
-                       {
-                               dstream<<" <value>";
-                       }
-                       dstream<<std::endl;
+                               os1<<" <value>";
+                       dstream<<padStringRight(os1.str(), 24);
 
                        if(i.getNode()->getValue().help != NULL)
-                       {
-                               dstream<<"      "<<i.getNode()->getValue().help
-                                               <<std::endl;
-                       }
+                               dstream<<i.getNode()->getValue().help;
+                       dstream<<std::endl;
                }
 
                return cmd_args.getFlag("help") ? 0 : 1;
@@ -821,10 +831,9 @@ int main(int argc, char *argv[])
        /*
                Low-level initialization
        */
-
-       if(cmd_args.getFlag("verbose") ||
-                       cmd_args.getFlag("info-on-stderr") ||
-                       cmd_args.getFlag("speedtests"))
+       
+       // In certain cases, output info level on stderr
+       if(cmd_args.getFlag("verbose") || cmd_args.getFlag("speedtests"))
                log_add_output(&main_stderr_log_out, LMT_INFO);
 
        porting::signal_handler_init();
@@ -839,23 +848,38 @@ int main(int argc, char *argv[])
        
        // Initialize debug streams
 #ifdef RUN_IN_PLACE
-       std::string debugfile = DEBUGFILE;
+       std::string logfile = DEBUGFILE;
 #else
-       std::string debugfile = porting::path_user+DIR_DELIM+DEBUGFILE;
+       std::string logfile = porting::path_user+DIR_DELIM+DEBUGFILE;
 #endif
-       bool disable_stderr = false;
-       debugstreams_init(disable_stderr, debugfile.c_str());
+       if(cmd_args.exists("logfile"))
+               logfile = cmd_args.get("logfile");
+       if(logfile != "")
+               debugstreams_init(false, logfile.c_str());
+       else
+               debugstreams_init(false, NULL);
+
+       infostream<<"logfile    = "<<logfile<<std::endl;
+       infostream<<"path_share = "<<porting::path_share<<std::endl;
+       infostream<<"path_user  = "<<porting::path_user<<std::endl;
+
        // Initialize debug stacks
        debug_stacks_init();
-
        DSTACK(__FUNCTION_NAME);
 
-       dstream<<"path_share = "<<porting::path_share<<std::endl;
-       dstream<<"path_user  = "<<porting::path_user<<std::endl;
-
        // Debug handler
        BEGIN_DEBUG_EXCEPTION_HANDLER
-
+       
+       // List gameids if requested
+       if(cmd_args.exists("gameid") && cmd_args.get("gameid") == "list")
+       {
+               std::set<std::string> gameids = getAvailableGameIds();
+               for(std::set<std::string>::const_iterator i = gameids.begin();
+                               i != gameids.end(); i++)
+                       dstream<<(*i)<<std::endl;
+               return 0;
+       }
+       
        // Print startup message
        actionstream<<PROJECT_NAME<<
                        " with SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
@@ -948,25 +972,29 @@ int main(int argc, char *argv[])
        if(port == 0)
                port = 30000;
        
-       // Map directory
-       std::string map_dir = porting::path_user + DIR_DELIM + "server" + DIR_DELIM + "worlds" + DIR_DELIM + "world";
-       if(cmd_args.exists("map-dir"))
-               map_dir = cmd_args.get("map-dir");
+       // World directory
+       std::string commanded_world = "";
+       if(cmd_args.exists("world"))
+               commanded_world = cmd_args.get("world");
+       else if(cmd_args.exists("map-dir"))
+               commanded_world = cmd_args.get("map-dir");
        else if(g_settings->exists("map-dir"))
-               map_dir = g_settings->get("map-dir");
-       else{
-               // No map-dir option was specified.
-               // Check if the world is found from the default directory, and if
-               // not, see if the legacy world directory exists.
-               std::string legacy_map_dir = porting::path_user+DIR_DELIM+".."+DIR_DELIM+"world";
-               if(!fs::PathExists(map_dir) && fs::PathExists(legacy_map_dir)){
-                       errorstream<<"Warning: Using legacy world directory \""
-                                       <<legacy_map_dir<<"\""<<std::endl;
-                       map_dir = legacy_map_dir;
+               commanded_world = g_settings->get("map-dir");
+       
+       // Gamespec
+       SubgameSpec commanded_gamespec;
+       if(cmd_args.exists("gameid")){
+               std::string gameid = cmd_args.get("gameid");
+               commanded_gamespec = findSubgame(gameid);
+               if(!commanded_gamespec.isValid()){
+                       errorstream<<"Game \""<<gameid<<"\" not found"<<std::endl;
+                       return 1;
                }
        }
 
-       // Run dedicated server if asked to or no other option
+       /*
+               Run dedicated server if asked to or no other option
+       */
 #ifdef SERVER
        bool run_dedicated_server = true;
 #else
@@ -975,14 +1003,52 @@ int main(int argc, char *argv[])
        if(run_dedicated_server)
        {
                DSTACK("Dedicated server branch");
-
                // Create time getter if built with Irrlicht
 #ifndef SERVER
                g_timegetter = new SimpleTimeGetter();
 #endif
+
+               // World directory
+               std::string world_path;
+               bool is_legacy_world = false;
+               if(commanded_world != ""){
+                       world_path = commanded_world;
+               }
+               else{
+                       // No specific world was commanded
+                       // Check if the world is found from the default directory, and if
+                       // not, see if the legacy world directory exists.
+                       world_path = porting::path_user + DIR_DELIM + "server" + DIR_DELIM + "worlds" + DIR_DELIM + "world";
+                       std::string legacy_world_path = porting::path_user+DIR_DELIM+".."+DIR_DELIM+"world";
+                       if(!fs::PathExists(world_path) && fs::PathExists(legacy_world_path)){
+                               errorstream<<"Warning: Using legacy world directory \""
+                                               <<legacy_world_path<<"\""<<std::endl;
+                               world_path = legacy_world_path;
+                               is_legacy_world = true;
+                       }
+               }
+
+               // Gamespec
+               std::string world_gameid = getWorldGameId(world_path, is_legacy_world);
+               SubgameSpec gamespec = findSubgame(world_gameid);
+               if(commanded_gamespec.isValid() &&
+                               commanded_gamespec.id != world_gameid){
+                       errorstream<<"WARNING: Overriding gameid from \""
+                                       <<world_gameid<<"\" to \""
+                                       <<commanded_gamespec.id<<"\""<<std::endl;
+                       gamespec = commanded_gamespec;
+               }
+
+               if(!gamespec.isValid()){
+                       errorstream<<"Invalid gamespec. (world_gameid="
+                                       <<world_gameid<<")"<<std::endl;
+                       return 1;
+               }
                
+               infostream<<"Using gamespec \""<<gamespec.id<<"\""<<std::endl;
+
                // Create server
-               Server server(map_dir, configpath, "mesetint");
+               Server server(world_path, configpath, gamespec);
                server.start(port);
                
                // Run server
@@ -997,19 +1063,17 @@ int main(int argc, char *argv[])
                More parameters
        */
        
-       // Address to connect to
-       std::string address = "";
-       
+       std::string address = g_settings->get("address");
        if(cmd_args.exists("address"))
-       {
                address = cmd_args.get("address");
-       }
-       else
-       {
-               address = g_settings->get("address");
-       }
+       else if(cmd_args.exists("world"))
+               address = "";
        
        std::string playername = g_settings->get("name");
+       if(cmd_args.exists("name"))
+               playername = cmd_args.get("name");
+       
+       bool skip_main_menu = cmd_args.getFlag("go");
 
        /*
                Device initialization
@@ -1075,9 +1139,6 @@ int main(int argc, char *argv[])
        */
        //driver->setMinHardwareBufferVertexCount(50);
 
-       // Set the window caption
-       device->setWindowCaption(L"Minetest [Main Menu]");
-       
        // Create time getter
        g_timegetter = new IrrlichtTimeGetter(device);
        
@@ -1127,6 +1188,8 @@ int main(int argc, char *argv[])
        //skin->setColor(gui::EGDC_3D_SHADOW, video::SColor(0,0,0,0));
        skin->setColor(gui::EGDC_3D_HIGH_LIGHT, video::SColor(255,0,0,0));
        skin->setColor(gui::EGDC_3D_SHADOW, video::SColor(255,0,0,0));
+       skin->setColor(gui::EGDC_HIGH_LIGHT, video::SColor(255,70,100,50));
+       skin->setColor(gui::EGDC_HIGH_LIGHT_TEXT, video::SColor(255,255,255,255));
        
        /*
                GUI stuff
@@ -1144,11 +1207,15 @@ int main(int argc, char *argv[])
        // The password entered during the menu screen,
        std::string password;
 
+       bool first_loop = true;
+
        /*
                Menu-game loop
        */
        while(device->run() && kill == false)
        {
+               // Set the window caption
+               device->setWindowCaption(L"Minetest [Main Menu]");
 
                // This is used for catching disconnects
                try
@@ -1167,6 +1234,9 @@ int main(int argc, char *argv[])
                        guiroot = guienv->addStaticText(L"",
                                        core::rect<s32>(0, 0, 10000, 10000));
                        
+                       SubgameSpec gamespec;
+                       WorldSpec worldspec;
+
                        /*
                                Out-of-game menu loop.
 
@@ -1174,127 +1244,188 @@ int main(int argc, char *argv[])
                        */
                        while(kill == false)
                        {
+                               // If skip_main_menu, only go through here once
+                               if(skip_main_menu && !first_loop){
+                                       kill = true;
+                                       break;
+                               }
+                               first_loop = false;
+                               
                                // Cursor can be non-visible when coming from the game
                                device->getCursorControl()->setVisible(true);
                                // Some stuff are left to scene manager when coming from the game
                                // (map at least?)
                                smgr->clear();
-                               // Reset or hide the debug gui texts
-                               /*guitext->setText(L"Minetest-c55");
-                               guitext2->setVisible(false);
-                               guitext_info->setVisible(false);
-                               guitext_chat->setVisible(false);*/
                                
                                // Initialize menu data
                                MainMenuData menudata;
                                menudata.address = narrow_to_wide(address);
                                menudata.name = narrow_to_wide(playername);
                                menudata.port = narrow_to_wide(itos(port));
+                               if(cmd_args.exists("password"))
+                                       menudata.password = narrow_to_wide(cmd_args.get("password"));
                                menudata.fancy_trees = g_settings->getBool("new_style_leaves");
                                menudata.smooth_lighting = g_settings->getBool("smooth_lighting");
                                menudata.clouds_3d = g_settings->getBool("enable_3d_clouds");
                                menudata.opaque_water = g_settings->getBool("opaque_water");
                                menudata.creative_mode = g_settings->getBool("creative_mode");
                                menudata.enable_damage = g_settings->getBool("enable_damage");
-
-                               GUIMainMenu *menu =
-                                               new GUIMainMenu(guienv, guiroot, -1, 
-                                                       &g_menumgr, &menudata, g_gamecallback);
-                               menu->allowFocusRemoval(true);
-
-                               if(error_message != L"")
-                               {
-                                       errorstream<<"error_message = "
-                                                       <<wide_to_narrow(error_message)<<std::endl;
-
-                                       GUIMessageMenu *menu2 =
-                                                       new GUIMessageMenu(guienv, guiroot, -1, 
-                                                               &g_menumgr, error_message.c_str());
-                                       menu2->drop();
-                                       error_message = L"";
+                               // Get world listing for the menu
+                               std::vector<WorldSpec> worldspecs = getAvailableWorlds();
+                               for(std::vector<WorldSpec>::const_iterator i = worldspecs.begin();
+                                               i != worldspecs.end(); i++)
+                                       menudata.worlds.push_back(narrow_to_wide(
+                                                       i->name + " [" + i->gameid + "]"));
+                               // Select if there is only one
+                               if(worldspecs.size() == 1)
+                                       menudata.selected_world = 0;
+                               else
+                                       menudata.selected_world = -1;
+                               // If a world was commanded, append and select it
+                               if(commanded_world != ""){
+                                       std::string gameid = getWorldGameId(commanded_world, true);
+                                       if(gameid == "")
+                                               gameid = g_settings->get("default_game");
+                                       WorldSpec spec(commanded_world, "[commanded world]", gameid);
+                                       worldspecs.push_back(spec);
+                                       menudata.worlds.push_back(narrow_to_wide(spec.name)
+                                                       +L" ["+narrow_to_wide(spec.gameid)+L"]");
+                                       menudata.selected_world = menudata.worlds.size()-1;
                                }
 
-                               video::IVideoDriver* driver = device->getVideoDriver();
-                               
-                               infostream<<"Created main menu"<<std::endl;
-
-                               while(device->run() && kill == false)
+                               if(skip_main_menu == false)
                                {
-                                       if(menu->getStatus() == true)
-                                               break;
-
-                                       //driver->beginScene(true, true, video::SColor(255,0,0,0));
-                                       driver->beginScene(true, true, video::SColor(255,128,128,128));
-
-                                       drawMenuBackground(driver);
-
-                                       guienv->drawAll();
+                                       GUIMainMenu *menu =
+                                                       new GUIMainMenu(guienv, guiroot, -1, 
+                                                               &g_menumgr, &menudata, g_gamecallback);
+                                       menu->allowFocusRemoval(true);
+
+                                       if(error_message != L"")
+                                       {
+                                               verbosestream<<"error_message = "
+                                                               <<wide_to_narrow(error_message)<<std::endl;
+
+                                               GUIMessageMenu *menu2 =
+                                                               new GUIMessageMenu(guienv, guiroot, -1, 
+                                                                       &g_menumgr, error_message.c_str());
+                                               menu2->drop();
+                                               error_message = L"";
+                                       }
+
+                                       video::IVideoDriver* driver = device->getVideoDriver();
                                        
-                                       driver->endScene();
+                                       infostream<<"Created main menu"<<std::endl;
+
+                                       while(device->run() && kill == false)
+                                       {
+                                               if(menu->getStatus() == true)
+                                                       break;
+
+                                               //driver->beginScene(true, true, video::SColor(255,0,0,0));
+                                               driver->beginScene(true, true, video::SColor(255,128,128,128));
+
+                                               drawMenuBackground(driver);
+
+                                               guienv->drawAll();
+                                               
+                                               driver->endScene();
+                                               
+                                               // On some computers framerate doesn't seem to be
+                                               // automatically limited
+                                               sleep_ms(25);
+                                       }
                                        
-                                       // On some computers framerate doesn't seem to be
-                                       // automatically limited
-                                       sleep_ms(25);
+                                       // Break out of menu-game loop to shut down cleanly
+                                       if(device->run() == false || kill == true)
+                                               break;
+                                       
+                                       infostream<<"Dropping main menu"<<std::endl;
+
+                                       menu->drop();
                                }
-                               
-                               // Break out of menu-game loop to shut down cleanly
-                               if(device->run() == false || kill == true)
-                                       break;
-                               
-                               infostream<<"Dropping main menu"<<std::endl;
 
-                               menu->drop();
+                               // Set world path to selected one
+                               if(menudata.selected_world != -1){
+                                       worldspec = worldspecs[menudata.selected_world];
+                                       infostream<<"Selected world: "<<worldspec.name
+                                                       <<" ["<<worldspec.path<<"]"<<std::endl;
+                               }
                                
                                // Delete map if requested
-                               if(menudata.delete_map)
+                               if(menudata.delete_world)
                                {
-                                       bool r = fs::RecursiveDeleteContent(map_dir);
-                                       if(r == false)
-                                               error_message = L"Delete failed";
+                                       if(menudata.selected_world == -1){
+                                               error_message = L"Cannot delete world: "
+                                                               L"no world selected";
+                                               errorstream<<wide_to_narrow(error_message)<<std::endl;
+                                               continue;
+                                       }
+                                       /*bool r = fs::RecursiveDeleteContent(worldspec.path);
+                                       if(r == false){
+                                               error_message = L"World delete failed";
+                                               errorstream<<wide_to_narrow(error_message)<<std::endl;
+                                       }*/
+                                       // TODO: Some kind of a yes/no dialog is needed.
+                                       error_message = L"This doesn't do anything currently.";
+                                       errorstream<<wide_to_narrow(error_message)<<std::endl;
                                        continue;
                                }
 
                                playername = wide_to_narrow(menudata.name);
-
                                password = translatePassword(playername, menudata.password);
-
                                //infostream<<"Main: password hash: '"<<password<<"'"<<std::endl;
 
                                address = wide_to_narrow(menudata.address);
                                int newport = stoi(wide_to_narrow(menudata.port));
                                if(newport != 0)
                                        port = newport;
+                               // Save settings
                                g_settings->set("new_style_leaves", itos(menudata.fancy_trees));
                                g_settings->set("smooth_lighting", itos(menudata.smooth_lighting));
                                g_settings->set("enable_3d_clouds", itos(menudata.clouds_3d));
                                g_settings->set("opaque_water", itos(menudata.opaque_water));
                                g_settings->set("creative_mode", itos(menudata.creative_mode));
                                g_settings->set("enable_damage", itos(menudata.enable_damage));
-                               
-                               // NOTE: These are now checked server side; no need to do it
-                               //       here, so let's not do it here.
-                               /*// Check for valid parameters, restart menu if invalid.
-                               if(playername == "")
-                               {
-                                       error_message = L"Name required.";
-                                       continue;
-                               }
-                               // Check that name has only valid chars
-                               if(string_allowed(playername, PLAYERNAME_ALLOWED_CHARS)==false)
-                               {
-                                       error_message = L"Characters allowed: "
-                                                       +narrow_to_wide(PLAYERNAME_ALLOWED_CHARS);
-                                       continue;
-                               }*/
-
-                               // Save settings
                                g_settings->set("name", playername);
                                g_settings->set("address", address);
                                g_settings->set("port", itos(port));
                                // Update configuration file
                                if(configpath != "")
                                        g_settings->updateConfigFile(configpath.c_str());
-                       
+                               
+                               // If local game
+                               if(address == "")
+                               {
+                                       if(menudata.selected_world == -1){
+                                               error_message = L"No world selected and no address "
+                                                               L"provided. Nothing to do.";
+                                               errorstream<<wide_to_narrow(error_message)<<std::endl;
+                                               continue;
+                                       }
+                                       // Load gamespec for required game
+                                       gamespec = findSubgame(worldspec.gameid);
+                                       if(!gamespec.isValid() && !commanded_gamespec.isValid()){
+                                               error_message = L"Could not find or load game \""
+                                                               + narrow_to_wide(worldspec.gameid) + L"\"";
+                                               errorstream<<wide_to_narrow(error_message)<<std::endl;
+                                               continue;
+                                       }
+                                       if(commanded_gamespec.isValid() &&
+                                                       commanded_gamespec.id != worldspec.gameid){
+                                               errorstream<<"WARNING: Overriding gamespec from \""
+                                                               <<worldspec.gameid<<"\" to \""
+                                                               <<commanded_gamespec.id<<"\""<<std::endl;
+                                               gamespec = commanded_gamespec;
+                                       }
+
+                                       if(!gamespec.isValid()){
+                                               error_message = L"Invalid gamespec. (world_gameid="
+                                                               +narrow_to_wide(worldspec.gameid)+L")";
+                                               errorstream<<wide_to_narrow(error_message)<<std::endl;
+                                               continue;
+                                       }
+                               }
+
                                // Continue to game
                                break;
                        }
@@ -1312,26 +1443,27 @@ int main(int argc, char *argv[])
                                input,
                                device,
                                font,
-                               map_dir,
+                               worldspec.path,
                                playername,
                                password,
                                address,
                                port,
                                error_message,
                                configpath,
-                               chat_backend
+                               chat_backend,
+                               gamespec
                        );
 
                } //try
                catch(con::PeerNotFoundException &e)
                {
-                       errorstream<<"Connection error (timed out?)"<<std::endl;
                        error_message = L"Connection error (timed out?)";
+                       errorstream<<wide_to_narrow(error_message)<<std::endl;
                }
-               catch(SocketException &e)
+               catch(ServerError &e)
                {
-                       errorstream<<"Socket error (port already in use?)"<<std::endl;
-                       error_message = L"Socket error (port already in use?)";
+                       error_message = narrow_to_wide(e.what());
+                       errorstream<<wide_to_narrow(error_message)<<std::endl;
                }
                catch(ModError &e)
                {
@@ -1349,6 +1481,16 @@ int main(int argc, char *argv[])
                }
 #endif
 
+               // If no main menu, show error and exit
+               if(skip_main_menu)
+               {
+                       if(error_message != L""){
+                               verbosestream<<"error_message = "
+                                               <<wide_to_narrow(error_message)<<std::endl;
+                               retval = 1;
+                       }
+                       break;
+               }
        } // Menu-game loop
        
        delete input;
@@ -1368,7 +1510,7 @@ int main(int argc, char *argv[])
        
        debugstreams_deinit();
        
-       return 0;
+       return retval;
 }
 
 //END