]> git.lizzy.rs Git - minetest.git/blob - src/servermain.cpp
path_userdata is now created before potentially creating debug.txt in there
[minetest.git] / src / servermain.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 /*
21 =============================== NOTES ==============================
22
23
24 */
25
26 #ifndef SERVER
27         #ifdef _WIN32
28                 #pragma error ("For a server build, SERVER must be defined globally")
29         #else
30                 #error "For a server build, SERVER must be defined globally"
31         #endif
32 #endif
33
34 #ifdef NDEBUG
35         #ifdef _WIN32
36                 #pragma message ("Disabling unit tests")
37         #else
38                 #warning "Disabling unit tests"
39         #endif
40         // Disable unit tests
41         #define ENABLE_TESTS 0
42 #else
43         // Enable unit tests
44         #define ENABLE_TESTS 1
45 #endif
46
47 #ifdef _MSC_VER
48 #pragma comment(lib, "jthread.lib")
49 #pragma comment(lib, "zlibwapi.lib")
50 #endif
51
52 #include <iostream>
53 #include <fstream>
54 #include <time.h>
55 #include <jmutexautolock.h>
56 #include <locale.h>
57 #include "common_irrlicht.h"
58 #include "debug.h"
59 #include "map.h"
60 #include "player.h"
61 #include "main.h"
62 #include "test.h"
63 #include "environment.h"
64 #include "server.h"
65 #include "serialization.h"
66 #include "constants.h"
67 #include "strfnd.h"
68 #include "porting.h"
69 #include "materials.h"
70 #include "config.h"
71 #include "mineral.h"
72
73 /*
74         Settings.
75         These are loaded from the config file.
76 */
77
78 Settings g_settings;
79
80 extern void set_default_settings();
81
82 // Global profiler
83 Profiler g_profiler;
84
85 // A dummy thing
86 ITextureSource *g_texturesource = NULL;
87
88 /*
89         Debug streams
90 */
91
92 // Connection
93 std::ostream *dout_con_ptr = &dummyout;
94 std::ostream *derr_con_ptr = &dstream_no_stderr;
95
96 // Server
97 std::ostream *dout_server_ptr = &dstream;
98 std::ostream *derr_server_ptr = &dstream;
99
100 // Client
101 std::ostream *dout_client_ptr = &dstream;
102 std::ostream *derr_client_ptr = &dstream;
103
104 /*
105         gettime.h implementation
106 */
107
108 u32 getTimeMs()
109 {
110         /*
111                 Use imprecise system calls directly (from porting.h)
112         */
113         return porting::getTimeMs();
114 }
115
116 int main(int argc, char *argv[])
117 {
118         /*
119                 Initialization
120         */
121
122         // Set locale. This is for forcing '.' as the decimal point.
123         std::locale::global(std::locale("C"));
124         // This enables printing all characters in bitmap font
125         setlocale(LC_CTYPE, "en_US");
126
127         /*
128                 Low-level initialization
129         */
130
131         bool disable_stderr = false;
132 #ifdef _WIN32
133         disable_stderr = true;
134 #endif
135
136         porting::signal_handler_init();
137         bool &kill = *porting::signal_handler_killstatus();
138         
139         // Initialize porting::path_data and porting::path_userdata
140         porting::initializePaths();
141
142         // Create user data directory
143         fs::CreateDir(porting::path_userdata);
144         
145         // Initialize debug streams
146 #ifdef RUN_IN_PLACE
147         std::string debugfile = DEBUGFILE;
148 #else
149         std::string debugfile = porting::path_userdata+"/"+DEBUGFILE;
150 #endif
151         debugstreams_init(disable_stderr, debugfile.c_str());
152         // Initialize debug stacks
153         debug_stacks_init();
154
155         DSTACK(__FUNCTION_NAME);
156
157         // Init material properties table
158         //initializeMaterialProperties();
159
160         // Debug handler
161         BEGIN_DEBUG_EXCEPTION_HANDLER
162
163         // Print startup message
164         dstream<<DTIME<<"minetest-c55"
165                         " with SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
166                         <<", "<<BUILD_INFO
167                         <<std::endl;
168         
169         try
170         {
171         
172         /*
173                 Parse command line
174         */
175         
176         // List all allowed options
177         core::map<std::string, ValueSpec> allowed_options;
178         allowed_options.insert("help", ValueSpec(VALUETYPE_FLAG));
179         allowed_options.insert("config", ValueSpec(VALUETYPE_STRING,
180                         "Load configuration from specified file"));
181         allowed_options.insert("port", ValueSpec(VALUETYPE_STRING));
182         allowed_options.insert("disable-unittests", ValueSpec(VALUETYPE_FLAG));
183         allowed_options.insert("enable-unittests", ValueSpec(VALUETYPE_FLAG));
184         allowed_options.insert("map-dir", ValueSpec(VALUETYPE_STRING));
185
186         Settings cmd_args;
187         
188         bool ret = cmd_args.parseCommandLine(argc, argv, allowed_options);
189
190         if(ret == false || cmd_args.getFlag("help"))
191         {
192                 dstream<<"Allowed options:"<<std::endl;
193                 for(core::map<std::string, ValueSpec>::Iterator
194                                 i = allowed_options.getIterator();
195                                 i.atEnd() == false; i++)
196                 {
197                         dstream<<"  --"<<i.getNode()->getKey();
198                         if(i.getNode()->getValue().type == VALUETYPE_FLAG)
199                         {
200                         }
201                         else
202                         {
203                                 dstream<<" <value>";
204                         }
205                         dstream<<std::endl;
206
207                         if(i.getNode()->getValue().help != NULL)
208                         {
209                                 dstream<<"      "<<i.getNode()->getValue().help
210                                                 <<std::endl;
211                         }
212                 }
213
214                 return cmd_args.getFlag("help") ? 0 : 1;
215         }
216
217
218         /*
219                 Basic initialization
220         */
221
222         // Initialize default settings
223         set_default_settings();
224         
225         // Initialize sockets
226         sockets_init();
227         atexit(sockets_cleanup);
228         
229         /*
230                 Read config file
231         */
232         
233         // Path of configuration file in use
234         std::string configpath = "";
235         
236         if(cmd_args.exists("config"))
237         {
238                 bool r = g_settings.readConfigFile(cmd_args.get("config").c_str());
239                 if(r == false)
240                 {
241                         dstream<<"Could not read configuration from \""
242                                         <<cmd_args.get("config")<<"\""<<std::endl;
243                         return 1;
244                 }
245                 configpath = cmd_args.get("config");
246         }
247         else
248         {
249                 core::array<std::string> filenames;
250                 filenames.push_back(porting::path_userdata + "/minetest.conf");
251 #ifdef RUN_IN_PLACE
252                 filenames.push_back(porting::path_userdata + "/../minetest.conf");
253 #endif
254
255                 for(u32 i=0; i<filenames.size(); i++)
256                 {
257                         bool r = g_settings.readConfigFile(filenames[i].c_str());
258                         if(r)
259                         {
260                                 configpath = filenames[i];
261                                 break;
262                         }
263                 }
264         }
265
266         // Initialize random seed
267         srand(time(0));
268         mysrand(time(0));
269
270         // Initialize stuff
271         
272         init_mapnode();
273         init_mineral();
274
275         /*
276                 Run unit tests
277         */
278         if((ENABLE_TESTS && cmd_args.getFlag("disable-unittests") == false)
279                         || cmd_args.getFlag("enable-unittests") == true)
280         {
281                 run_tests();
282         }
283
284         /*
285                 Check parameters
286         */
287
288         std::cout<<std::endl<<std::endl;
289         
290         std::cout
291         <<"        .__               __                   __   "<<std::endl
292         <<"  _____ |__| ____   _____/  |_  ____   _______/  |_ "<<std::endl
293         <<" /     \\|  |/    \\_/ __ \\   __\\/ __ \\ /  ___/\\   __\\"<<std::endl
294         <<"|  Y Y  \\  |   |  \\  ___/|  | \\  ___/ \\___ \\  |  |  "<<std::endl
295         <<"|__|_|  /__|___|  /\\___  >__|  \\___  >____  > |__|  "<<std::endl
296         <<"      \\/        \\/     \\/          \\/     \\/        "<<std::endl
297         <<std::endl;
298
299         std::cout<<std::endl;
300         
301         // Port?
302         u16 port = 30000;
303         if(cmd_args.exists("port") && cmd_args.getU16("port") != 0)
304         {
305                 port = cmd_args.getU16("port");
306         }
307         else if(g_settings.exists("port") && g_settings.getU16("port") != 0)
308         {
309                 port = g_settings.getU16("port");
310         }
311         else
312         {
313                 dstream<<"Please specify port (in config or on command line)"
314                                 <<std::endl;
315         }
316         
317         // Figure out path to map
318         std::string map_dir = porting::path_userdata+"/world";
319         if(cmd_args.exists("map-dir"))
320                 map_dir = cmd_args.get("map-dir");
321         else if(g_settings.exists("map-dir"))
322                 map_dir = g_settings.get("map-dir");
323         
324         // Create server
325         Server server(map_dir.c_str());
326         server.start(port);
327
328         // Run server
329         dedicated_server_loop(server, kill);
330         
331         } //try
332         catch(con::PeerNotFoundException &e)
333         {
334                 dstream<<DTIME<<"Connection timed out."<<std::endl;
335         }
336
337         END_DEBUG_EXCEPTION_HANDLER
338
339         debugstreams_deinit();
340         
341         return 0;
342 }
343
344 //END