continued.
[oweals/minetest.git] / src / servermain.cpp
index 5edc8ac7c0794c16c1efea2161936572649448bc..8fcefaae7cfc7982fcdbefd943ffba36a502c09f 100644 (file)
@@ -20,18 +20,18 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 /*
 =============================== NOTES ==============================
 
-TODO: Move the default settings into some separate file
 
 */
 
 #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
@@ -49,15 +49,6 @@ TODO: Move the default settings into some separate file
 #pragma comment(lib, "zlibwapi.lib")
 #endif
 
-#ifdef _WIN32
-       #define WIN32_LEAN_AND_MEAN
-       #include <windows.h>
-       #define sleep_ms(x) Sleep(x)
-#else
-       #include <unistd.h>
-       #define sleep_ms(x) usleep(x*1000)
-#endif
-
 #include <iostream>
 #include <fstream>
 #include <time.h>
@@ -75,9 +66,8 @@ TODO: Move the default settings into some separate file
 #include "constants.h"
 #include "strfnd.h"
 #include "porting.h"
-
-// Dummy variable
-IrrlichtDevice *g_device = NULL;
+#include "materials.h"
+#include "config.h"
 
 /*
        Settings.
@@ -106,21 +96,15 @@ std::ostream *derr_client_ptr = &dstream;
 
 
 /*
-       Timestamp stuff
+       gettime.h implementation
 */
 
-JMutex g_timestamp_mutex;
-
-std::string getTimestamp()
+u32 getTimeMs()
 {
-       if(g_timestamp_mutex.IsInitialized()==false)
-               return "";
-       JMutexAutoLock lock(g_timestamp_mutex);
-       time_t t = time(NULL);
-       struct tm *tm = localtime(&t);
-       char cs[20];
-       strftime(cs, 20, "%H:%M:%S", tm);
-       return cs;
+       /*
+               Use imprecise system calls directly (from porting.h)
+       */
+       return porting::getTimeMs();
 }
 
 int main(int argc, char *argv[])
@@ -141,6 +125,18 @@ int main(int argc, char *argv[])
 
        DSTACK(__FUNCTION_NAME);
 
+       porting::initializePaths();
+
+       initializeMaterialProperties();
+
+       BEGIN_DEBUG_EXCEPTION_HANDLER
+
+       // Print startup message
+       dstream<<DTIME<<"minetest-c55"
+                       " with SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
+                       <<", "<<BUILD_INFO
+                       <<std::endl;
+       
        try
        {
        
@@ -156,6 +152,7 @@ int main(int argc, char *argv[])
        allowed_options.insert("port", ValueSpec(VALUETYPE_STRING));
        allowed_options.insert("disable-unittests", ValueSpec(VALUETYPE_FLAG));
        allowed_options.insert("enable-unittests", ValueSpec(VALUETYPE_FLAG));
+       allowed_options.insert("map-dir", ValueSpec(VALUETYPE_STRING));
 
        Settings cmd_args;
        
@@ -196,12 +193,6 @@ int main(int argc, char *argv[])
        // 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 locale. This is for forcing '.' as the decimal point.
        std::locale::global(std::locale("C"));
        // This enables printing all characters in bitmap font
@@ -211,9 +202,6 @@ int main(int argc, char *argv[])
        sockets_init();
        atexit(sockets_cleanup);
        
-       // Initialize timestamp mutex
-       g_timestamp_mutex.Init();
-
        /*
                Initialization
        */
@@ -238,15 +226,15 @@ 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 + "/minetest.conf");
+#ifdef RUN_IN_PLACE
+               filenames.push_back(porting::path_userdata + "/../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];
@@ -257,6 +245,7 @@ int main(int argc, char *argv[])
 
        // Initialize random seed
        srand(time(0));
+       mysrand(time(0));
 
        /*
                Run unit tests
@@ -292,19 +281,17 @@ int main(int argc, char *argv[])
        <<"|  Y Y  \\  |   |  \\  ___/|  | \\  ___/ \\___ \\  |  |  "<<std::endl
        <<"|__|_|  /__|___|  /\\___  >__|  \\___  >____  > |__|  "<<std::endl
        <<"      \\/        \\/     \\/          \\/     \\/        "<<std::endl
-       <<std::endl
-       <<"Now with more waterish water!"
        <<std::endl;
 
        std::cout<<std::endl;
        
        // Port?
        u16 port = 30000;
-       if(cmd_args.exists("port"))
+       if(cmd_args.exists("port") && cmd_args.getU16("port") != 0)
        {
                port = cmd_args.getU16("port");
        }
-       else if(g_settings.exists("port"))
+       else if(g_settings.exists("port") && g_settings.getU16("port") != 0)
        {
                port = g_settings.getU16("port");
        }
@@ -321,8 +308,15 @@ int main(int argc, char *argv[])
        std::cout<<"Running dedicated server"<<std::endl;
        std::cout<<"========================"<<std::endl;
        std::cout<<std::endl;
-
-       Server server("../map", hm_params, map_params);
+       
+       // Figure out path to map
+       std::string map_dir = porting::path_userdata+"/map";
+       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);
        server.start(port);
 
        for(;;)
@@ -354,30 +348,13 @@ int main(int argc, char *argv[])
                }
        }
 
-       /*
-               Update configuration file
-       */
-       if(configpath != "")
-       {
-               g_settings.updateConfigFile(configpath.c_str());
-       }
-
        } //try
        catch(con::PeerNotFoundException &e)
        {
                dstream<<DTIME<<"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)
-       {
-               dstream<<std::endl<<DTIME<<"An unhandled exception occurred: "
-                               <<e.what()<<std::endl;
-               assert(0);
-       }
-#endif
+
+       END_DEBUG_EXCEPTION_HANDLER
 
        debugstreams_deinit();