Node placement / mineral / serialization / iron freq / node_dig callback
[oweals/minetest.git] / src / servermain.cpp
index 9a35a4b0e1f92f2bcbfc6d07f579e22462e9e295..3ef1d9479a75788ac70c6a6753322b4eb59d1633 100644 (file)
@@ -25,12 +25,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #ifndef SERVER
        #ifdef _WIN32
+               #pragma error ("For a server build, SERVER must be defined globally")
        #else
                #error "For a server build, SERVER must be defined globally"
        #endif
 #endif
 
-#ifdef UNITTEST_DISABLE
+#ifdef NDEBUG
        #ifdef _WIN32
                #pragma message ("Disabling unit tests")
        #else
@@ -66,15 +67,26 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "strfnd.h"
 #include "porting.h"
 #include "materials.h"
+#include "config.h"
+#include "filesys.h"
+#include "defaultsettings.h"
+#include "settings.h"
+#include "profiler.h"
+#include "log.h"
+#include "nodedef.h" // For init_contentfeatures
+#include "content_mapnode.h" // For content_mapnode_init
+#include "mods.h"
 
 /*
        Settings.
        These are loaded from the config file.
 */
+Settings main_settings;
+Settings *g_settings = &main_settings;
 
-Settings g_settings;
-
-extern void set_default_settings();
+// Global profiler
+Profiler main_profiler;
+Profiler *g_profiler = &main_profiler;
 
 /*
        Debug streams
@@ -82,16 +94,15 @@ extern void set_default_settings();
 
 // Connection
 std::ostream *dout_con_ptr = &dummyout;
-std::ostream *derr_con_ptr = &dstream_no_stderr;
+std::ostream *derr_con_ptr = &verbosestream;
 
 // Server
-std::ostream *dout_server_ptr = &dstream;
-std::ostream *derr_server_ptr = &dstream;
+std::ostream *dout_server_ptr = &infostream;
+std::ostream *derr_server_ptr = &errorstream;
 
 // Client
-std::ostream *dout_client_ptr = &dstream;
-std::ostream *derr_client_ptr = &dstream;
-
+std::ostream *dout_client_ptr = &infostream;
+std::ostream *derr_client_ptr = &errorstream;
 
 /*
        gettime.h implementation
@@ -105,8 +116,42 @@ u32 getTimeMs()
        return porting::getTimeMs();
 }
 
+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;
+
 int main(int argc, char *argv[])
 {
+       /*
+               Initialization
+       */
+
+       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");
+
+       // Set locale. This is for forcing '.' as the decimal point.
+       std::locale::global(std::locale("C"));
+       // This enables printing all characters in bitmap font
+       setlocale(LC_CTYPE, "en_US");
+
        /*
                Low-level initialization
        */
@@ -116,15 +161,39 @@ int main(int argc, char *argv[])
        disable_stderr = true;
 #endif
 
+       porting::signal_handler_init();
+       bool &kill = *porting::signal_handler_killstatus();
+       
+       // Initialize porting::path_data and porting::path_userdata
+       porting::initializePaths();
+
+       // Create user data directory
+       fs::CreateDir(porting::path_userdata);
+       
        // Initialize debug streams
-       debugstreams_init(disable_stderr, DEBUGFILE);
+#ifdef RUN_IN_PLACE
+       std::string debugfile = DEBUGFILE;
+#else
+       std::string debugfile = porting::path_userdata+DIR_DELIM+DEBUGFILE;
+#endif
+       debugstreams_init(disable_stderr, debugfile.c_str());
        // Initialize debug stacks
        debug_stacks_init();
 
        DSTACK(__FUNCTION_NAME);
 
-       initializeMaterialProperties();
+       // Init material properties table
+       //initializeMaterialProperties();
+
+       // Debug handler
+       BEGIN_DEBUG_EXCEPTION_HANDLER
 
+       // Print startup message
+       actionstream<<PROJECT_NAME<<
+                       " with SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
+                       <<", "<<BUILD_INFO
+                       <<std::endl;
+       
        try
        {
        
@@ -141,6 +210,7 @@ int main(int argc, char *argv[])
        allowed_options.insert("disable-unittests", ValueSpec(VALUETYPE_FLAG));
        allowed_options.insert("enable-unittests", ValueSpec(VALUETYPE_FLAG));
        allowed_options.insert("map-dir", ValueSpec(VALUETYPE_STRING));
+       allowed_options.insert("info-on-stderr", ValueSpec(VALUETYPE_FLAG));
 
        Settings cmd_args;
        
@@ -173,33 +243,20 @@ int main(int argc, char *argv[])
                return cmd_args.getFlag("help") ? 0 : 1;
        }
 
+       if(cmd_args.getFlag("info-on-stderr"))
+               log_add_output(&main_stderr_log_out, LMT_INFO);
 
        /*
                Basic initialization
        */
 
        // Initialize default settings
-       set_default_settings();
-       
-       // Print startup message
-       dstream<<DTIME<<"minetest-c55 server"
-                       " with SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
-                       <<", ENABLE_TESTS="<<ENABLE_TESTS
-                       <<std::endl;
+       set_default_settings(g_settings);
        
-       // Set locale. This is for forcing '.' as the decimal point.
-       std::locale::global(std::locale("C"));
-       // This enables printing all characters in bitmap font
-       setlocale(LC_CTYPE, "en_US");
-
        // Initialize sockets
        sockets_init();
        atexit(sockets_cleanup);
        
-       /*
-               Initialization
-       */
-
        /*
                Read config file
        */
@@ -209,10 +266,10 @@ int main(int argc, char *argv[])
        
        if(cmd_args.exists("config"))
        {
-               bool r = g_settings.readConfigFile(cmd_args.get("config").c_str());
+               bool r = g_settings->readConfigFile(cmd_args.get("config").c_str());
                if(r == false)
                {
-                       dstream<<"Could not read configuration from \""
+                       errorstream<<"Could not read configuration from \""
                                        <<cmd_args.get("config")<<"\""<<std::endl;
                        return 1;
                }
@@ -220,15 +277,17 @@ int main(int argc, char *argv[])
        }
        else
        {
-               const char *filenames[2] =
-               {
-                       "../minetest.conf",
-                       "../../minetest.conf"
-               };
+               core::array<std::string> filenames;
+               filenames.push_back(porting::path_userdata +
+                               DIR_DELIM + "minetest.conf");
+#ifdef RUN_IN_PLACE
+               filenames.push_back(porting::path_userdata +
+                               DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
+#endif
 
-               for(u32 i=0; i<2; i++)
+               for(u32 i=0; i<filenames.size(); i++)
                {
-                       bool r = g_settings.readConfigFile(filenames[i]);
+                       bool r = g_settings->readConfigFile(filenames[i].c_str());
                        if(r)
                        {
                                configpath = filenames[i];
@@ -239,6 +298,7 @@ int main(int argc, char *argv[])
 
        // Initialize random seed
        srand(time(0));
+       mysrand(time(0));
 
        /*
                Run unit tests
@@ -248,18 +308,6 @@ int main(int argc, char *argv[])
        {
                run_tests();
        }
-       
-       // Read map parameters from settings
-
-       HMParams hm_params;
-       hm_params.blocksize = g_settings.getU16("heightmap_blocksize");
-       hm_params.randmax = g_settings.get("height_randmax");
-       hm_params.randfactor = g_settings.get("height_randfactor");
-       hm_params.base = g_settings.get("height_base");
-
-       MapParams map_params;
-       map_params.plants_amount = g_settings.getFloat("plants_amount");
-       map_params.ravines_amount = g_settings.getFloat("ravines_amount");
 
        /*
                Check parameters
@@ -284,9 +332,9 @@ int main(int argc, char *argv[])
        {
                port = cmd_args.getU16("port");
        }
-       else if(g_settings.exists("port") && g_settings.getU16("port") != 0)
+       else if(g_settings->exists("port") && g_settings->getU16("port") != 0)
        {
-               port = g_settings.getU16("port");
+               port = g_settings->getU16("port");
        }
        else
        {
@@ -294,67 +342,31 @@ int main(int argc, char *argv[])
                                <<std::endl;
        }
        
-       DSTACK("Dedicated server branch");
-       
-       std::cout<<std::endl;
-       std::cout<<"========================"<<std::endl;
-       std::cout<<"Running dedicated server"<<std::endl;
-       std::cout<<"========================"<<std::endl;
-       std::cout<<std::endl;
-       
        // Figure out path to map
-       std::string map_dir = "../map";
+       std::string map_dir = porting::path_userdata+DIR_DELIM+"world";
        if(cmd_args.exists("map-dir"))
                map_dir = cmd_args.get("map-dir");
+       else if(g_settings->exists("map-dir"))
+               map_dir = g_settings->get("map-dir");
        
-       Server server(map_dir.c_str(), hm_params, map_params);
+       // Create server
+       Server server(map_dir.c_str(), configpath);
        server.start(port);
 
-       for(;;)
-       {
-               // This is kind of a hack but can be done like this
-               // because server.step() is very light
-               sleep_ms(30);
-               server.step(0.030);
-
-               static int counter = 0;
-               counter--;
-               if(counter <= 0)
-               {
-                       counter = 10;
-
-                       core::list<PlayerInfo> list = server.getPlayerInfo();
-                       core::list<PlayerInfo>::Iterator i;
-                       static u32 sum_old = 0;
-                       u32 sum = PIChecksum(list);
-                       if(sum != sum_old)
-                       {
-                               std::cout<<DTIME<<"Player info:"<<std::endl;
-                               for(i=list.begin(); i!=list.end(); i++)
-                               {
-                                       i->PrintLine(&std::cout);
-                               }
-                       }
-                       sum_old = sum;
-               }
-       }
-
+       // Run server
+       dedicated_server_loop(server, kill);
+       
        } //try
        catch(con::PeerNotFoundException &e)
        {
-               dstream<<DTIME<<"Connection timed out."<<std::endl;
+               errorstream<<"Connection timed out."<<std::endl;
        }
-#if CATCH_UNHANDLED_EXCEPTIONS
-       /*
-               This is what has to be done in every thread to get suitable debug info
-       */
-       catch(std::exception &e)
+       catch(ModError &e)
        {
-               dstream<<std::endl<<DTIME<<"An unhandled exception occurred: "
-                               <<e.what()<<std::endl;
-               assert(0);
+               errorstream<<e.what()<<std::endl;
        }
-#endif
+
+       END_DEBUG_EXCEPTION_HANDLER(errorstream)
 
        debugstreams_deinit();