e8a54512ec29ddce54d5d5c411abe3392db668c2
[oweals/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 #include "filesys.h"
73 #include "defaultsettings.h"
74 #include "settings.h"
75 #include "profiler.h"
76 #include "log.h"
77 #include "nodedef.h" // For init_contentfeatures
78 #include "content_mapnode.h" // For content_mapnode_init
79 #include "mods.h"
80
81 /*
82         Settings.
83         These are loaded from the config file.
84 */
85 Settings main_settings;
86 Settings *g_settings = &main_settings;
87
88 // Global profiler
89 Profiler main_profiler;
90 Profiler *g_profiler = &main_profiler;
91
92 /*
93         Debug streams
94 */
95
96 // Connection
97 std::ostream *dout_con_ptr = &dummyout;
98 std::ostream *derr_con_ptr = &verbosestream;
99
100 // Server
101 std::ostream *dout_server_ptr = &infostream;
102 std::ostream *derr_server_ptr = &errorstream;
103
104 // Client
105 std::ostream *dout_client_ptr = &infostream;
106 std::ostream *derr_client_ptr = &errorstream;
107
108 /*
109         gettime.h implementation
110 */
111
112 u32 getTimeMs()
113 {
114         /*
115                 Use imprecise system calls directly (from porting.h)
116         */
117         return porting::getTimeMs();
118 }
119
120 class StderrLogOutput: public ILogOutput
121 {
122 public:
123         /* line: Full line with timestamp, level and thread */
124         void printLog(const std::string &line)
125         {
126                 std::cerr<<line<<std::endl;
127         }
128 } main_stderr_log_out;
129
130 class DstreamNoStderrLogOutput: public ILogOutput
131 {
132 public:
133         /* line: Full line with timestamp, level and thread */
134         void printLog(const std::string &line)
135         {
136                 dstream_no_stderr<<line<<std::endl;
137         }
138 } main_dstream_no_stderr_log_out;
139
140 int main(int argc, char *argv[])
141 {
142         /*
143                 Initialization
144         */
145
146         log_add_output_maxlev(&main_stderr_log_out, LMT_ACTION);
147         log_add_output_all_levs(&main_dstream_no_stderr_log_out);
148
149         log_register_thread("main");
150
151         // Set locale. This is for forcing '.' as the decimal point.
152         std::locale::global(std::locale("C"));
153         // This enables printing all characters in bitmap font
154         setlocale(LC_CTYPE, "en_US");
155
156         /*
157                 Low-level initialization
158         */
159
160         bool disable_stderr = false;
161 #ifdef _WIN32
162         disable_stderr = true;
163 #endif
164
165         porting::signal_handler_init();
166         bool &kill = *porting::signal_handler_killstatus();
167         
168         // Initialize porting::path_data and porting::path_userdata
169         porting::initializePaths();
170
171         // Create user data directory
172         fs::CreateDir(porting::path_userdata);
173         
174         // Initialize debug streams
175 #ifdef RUN_IN_PLACE
176         std::string debugfile = DEBUGFILE;
177 #else
178         std::string debugfile = porting::path_userdata+DIR_DELIM+DEBUGFILE;
179 #endif
180         debugstreams_init(disable_stderr, debugfile.c_str());
181         // Initialize debug stacks
182         debug_stacks_init();
183
184         DSTACK(__FUNCTION_NAME);
185
186         // Init material properties table
187         //initializeMaterialProperties();
188
189         // Debug handler
190         BEGIN_DEBUG_EXCEPTION_HANDLER
191
192         // Print startup message
193         actionstream<<PROJECT_NAME<<
194                         " with SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
195                         <<", "<<BUILD_INFO
196                         <<std::endl;
197         
198         try
199         {
200         
201         /*
202                 Parse command line
203         */
204         
205         // List all allowed options
206         core::map<std::string, ValueSpec> allowed_options;
207         allowed_options.insert("help", ValueSpec(VALUETYPE_FLAG));
208         allowed_options.insert("config", ValueSpec(VALUETYPE_STRING,
209                         "Load configuration from specified file"));
210         allowed_options.insert("port", ValueSpec(VALUETYPE_STRING));
211         allowed_options.insert("disable-unittests", ValueSpec(VALUETYPE_FLAG));
212         allowed_options.insert("enable-unittests", ValueSpec(VALUETYPE_FLAG));
213         allowed_options.insert("map-dir", ValueSpec(VALUETYPE_STRING));
214         allowed_options.insert("info-on-stderr", ValueSpec(VALUETYPE_FLAG));
215
216         Settings cmd_args;
217         
218         bool ret = cmd_args.parseCommandLine(argc, argv, allowed_options);
219
220         if(ret == false || cmd_args.getFlag("help"))
221         {
222                 dstream<<"Allowed options:"<<std::endl;
223                 for(core::map<std::string, ValueSpec>::Iterator
224                                 i = allowed_options.getIterator();
225                                 i.atEnd() == false; i++)
226                 {
227                         dstream<<"  --"<<i.getNode()->getKey();
228                         if(i.getNode()->getValue().type == VALUETYPE_FLAG)
229                         {
230                         }
231                         else
232                         {
233                                 dstream<<" <value>";
234                         }
235                         dstream<<std::endl;
236
237                         if(i.getNode()->getValue().help != NULL)
238                         {
239                                 dstream<<"      "<<i.getNode()->getValue().help
240                                                 <<std::endl;
241                         }
242                 }
243
244                 return cmd_args.getFlag("help") ? 0 : 1;
245         }
246
247         if(cmd_args.getFlag("info-on-stderr"))
248                 log_add_output(&main_stderr_log_out, LMT_INFO);
249
250         /*
251                 Basic initialization
252         */
253
254         // Initialize default settings
255         set_default_settings(g_settings);
256         
257         // Initialize sockets
258         sockets_init();
259         atexit(sockets_cleanup);
260         
261         /*
262                 Read config file
263         */
264         
265         // Path of configuration file in use
266         std::string configpath = "";
267         
268         if(cmd_args.exists("config"))
269         {
270                 bool r = g_settings->readConfigFile(cmd_args.get("config").c_str());
271                 if(r == false)
272                 {
273                         errorstream<<"Could not read configuration from \""
274                                         <<cmd_args.get("config")<<"\""<<std::endl;
275                         return 1;
276                 }
277                 configpath = cmd_args.get("config");
278         }
279         else
280         {
281                 core::array<std::string> filenames;
282                 filenames.push_back(porting::path_userdata +
283                                 DIR_DELIM + "minetest.conf");
284 #ifdef RUN_IN_PLACE
285                 filenames.push_back(porting::path_userdata +
286                                 DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
287 #endif
288
289                 for(u32 i=0; i<filenames.size(); i++)
290                 {
291                         bool r = g_settings->readConfigFile(filenames[i].c_str());
292                         if(r)
293                         {
294                                 configpath = filenames[i];
295                                 break;
296                         }
297                 }
298         }
299
300         // Initialize random seed
301         srand(time(0));
302         mysrand(time(0));
303
304         // Initialize stuff
305         
306         init_mineral();
307
308         /*
309                 Run unit tests
310         */
311         if((ENABLE_TESTS && cmd_args.getFlag("disable-unittests") == false)
312                         || cmd_args.getFlag("enable-unittests") == true)
313         {
314                 run_tests();
315         }
316
317         /*
318                 Check parameters
319         */
320
321         std::cout<<std::endl<<std::endl;
322         
323         std::cout
324         <<"        .__               __                   __   "<<std::endl
325         <<"  _____ |__| ____   _____/  |_  ____   _______/  |_ "<<std::endl
326         <<" /     \\|  |/    \\_/ __ \\   __\\/ __ \\ /  ___/\\   __\\"<<std::endl
327         <<"|  Y Y  \\  |   |  \\  ___/|  | \\  ___/ \\___ \\  |  |  "<<std::endl
328         <<"|__|_|  /__|___|  /\\___  >__|  \\___  >____  > |__|  "<<std::endl
329         <<"      \\/        \\/     \\/          \\/     \\/        "<<std::endl
330         <<std::endl;
331
332         std::cout<<std::endl;
333         
334         // Port?
335         u16 port = 30000;
336         if(cmd_args.exists("port") && cmd_args.getU16("port") != 0)
337         {
338                 port = cmd_args.getU16("port");
339         }
340         else if(g_settings->exists("port") && g_settings->getU16("port") != 0)
341         {
342                 port = g_settings->getU16("port");
343         }
344         else
345         {
346                 dstream<<"Please specify port (in config or on command line)"
347                                 <<std::endl;
348         }
349         
350         // Figure out path to map
351         std::string map_dir = porting::path_userdata+DIR_DELIM+"world";
352         if(cmd_args.exists("map-dir"))
353                 map_dir = cmd_args.get("map-dir");
354         else if(g_settings->exists("map-dir"))
355                 map_dir = g_settings->get("map-dir");
356         
357         // Create server
358         Server server(map_dir.c_str(), configpath);
359         server.start(port);
360
361         // Run server
362         dedicated_server_loop(server, kill);
363         
364         } //try
365         catch(con::PeerNotFoundException &e)
366         {
367                 errorstream<<"Connection timed out."<<std::endl;
368         }
369         catch(ModError &e)
370         {
371                 errorstream<<e.what()<<std::endl;
372         }
373
374         END_DEBUG_EXCEPTION_HANDLER(errorstream)
375
376         debugstreams_deinit();
377         
378         return 0;
379 }
380
381 //END