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