7ec397ace526f955c6189bba03dd8e8c2913a041
[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
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 /*
83         Debug streams
84 */
85
86 // Connection
87 std::ostream *dout_con_ptr = &dummyout;
88 std::ostream *derr_con_ptr = &dstream_no_stderr;
89
90 // Server
91 std::ostream *dout_server_ptr = &dstream;
92 std::ostream *derr_server_ptr = &dstream;
93
94 // Client
95 std::ostream *dout_client_ptr = &dstream;
96 std::ostream *derr_client_ptr = &dstream;
97
98
99 /*
100         gettime.h implementation
101 */
102
103 u32 getTimeMs()
104 {
105         /*
106                 Use imprecise system calls directly (from porting.h)
107         */
108         return porting::getTimeMs();
109 }
110
111 int main(int argc, char *argv[])
112 {
113         /*
114                 Low-level initialization
115         */
116
117         bool disable_stderr = false;
118 #ifdef _WIN32
119         disable_stderr = true;
120 #endif
121
122         // Initialize debug streams
123         debugstreams_init(disable_stderr, DEBUGFILE);
124         // Initialize debug stacks
125         debug_stacks_init();
126
127         DSTACK(__FUNCTION_NAME);
128
129         porting::initializePaths();
130
131         initializeMaterialProperties();
132
133         BEGIN_DEBUG_EXCEPTION_HANDLER
134
135         // Print startup message
136         dstream<<DTIME<<"minetest-c55"
137                         " with SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
138                         <<", "<<BUILD_INFO
139                         <<std::endl;
140         
141         try
142         {
143         
144         /*
145                 Parse command line
146         */
147         
148         // List all allowed options
149         core::map<std::string, ValueSpec> allowed_options;
150         allowed_options.insert("help", ValueSpec(VALUETYPE_FLAG));
151         allowed_options.insert("config", ValueSpec(VALUETYPE_STRING,
152                         "Load configuration from specified file"));
153         allowed_options.insert("port", ValueSpec(VALUETYPE_STRING));
154         allowed_options.insert("disable-unittests", ValueSpec(VALUETYPE_FLAG));
155         allowed_options.insert("enable-unittests", ValueSpec(VALUETYPE_FLAG));
156         allowed_options.insert("map-dir", ValueSpec(VALUETYPE_STRING));
157
158         Settings cmd_args;
159         
160         bool ret = cmd_args.parseCommandLine(argc, argv, allowed_options);
161
162         if(ret == false || cmd_args.getFlag("help"))
163         {
164                 dstream<<"Allowed options:"<<std::endl;
165                 for(core::map<std::string, ValueSpec>::Iterator
166                                 i = allowed_options.getIterator();
167                                 i.atEnd() == false; i++)
168                 {
169                         dstream<<"  --"<<i.getNode()->getKey();
170                         if(i.getNode()->getValue().type == VALUETYPE_FLAG)
171                         {
172                         }
173                         else
174                         {
175                                 dstream<<" <value>";
176                         }
177                         dstream<<std::endl;
178
179                         if(i.getNode()->getValue().help != NULL)
180                         {
181                                 dstream<<"      "<<i.getNode()->getValue().help
182                                                 <<std::endl;
183                         }
184                 }
185
186                 return cmd_args.getFlag("help") ? 0 : 1;
187         }
188
189
190         /*
191                 Basic initialization
192         */
193
194         // Initialize default settings
195         set_default_settings();
196         
197         // Set locale. This is for forcing '.' as the decimal point.
198         std::locale::global(std::locale("C"));
199         // This enables printing all characters in bitmap font
200         setlocale(LC_CTYPE, "en_US");
201
202         // Initialize sockets
203         sockets_init();
204         atexit(sockets_cleanup);
205         
206         /*
207                 Initialization
208         */
209
210         /*
211                 Read config file
212         */
213         
214         // Path of configuration file in use
215         std::string configpath = "";
216         
217         if(cmd_args.exists("config"))
218         {
219                 bool r = g_settings.readConfigFile(cmd_args.get("config").c_str());
220                 if(r == false)
221                 {
222                         dstream<<"Could not read configuration from \""
223                                         <<cmd_args.get("config")<<"\""<<std::endl;
224                         return 1;
225                 }
226                 configpath = cmd_args.get("config");
227         }
228         else
229         {
230                 core::array<std::string> filenames;
231                 filenames.push_back(porting::path_userdata + "/minetest.conf");
232 #ifdef RUN_IN_PLACE
233                 filenames.push_back(porting::path_userdata + "/../minetest.conf");
234 #endif
235
236                 for(u32 i=0; i<filenames.size(); i++)
237                 {
238                         bool r = g_settings.readConfigFile(filenames[i].c_str());
239                         if(r)
240                         {
241                                 configpath = filenames[i];
242                                 break;
243                         }
244                 }
245         }
246
247         // Initialize random seed
248         srand(time(0));
249         mysrand(time(0));
250
251         /*
252                 Run unit tests
253         */
254         if((ENABLE_TESTS && cmd_args.getFlag("disable-unittests") == false)
255                         || cmd_args.getFlag("enable-unittests") == true)
256         {
257                 run_tests();
258         }
259
260         // Initialize stuff
261         
262         IIrrlichtWrapper irrlicht; // Dummy
263         init_mapnode(&irrlicht);
264         init_mineral(&irrlicht);
265
266         /*
267                 Check parameters
268         */
269
270         std::cout<<std::endl<<std::endl;
271         
272         std::cout
273         <<"        .__               __                   __   "<<std::endl
274         <<"  _____ |__| ____   _____/  |_  ____   _______/  |_ "<<std::endl
275         <<" /     \\|  |/    \\_/ __ \\   __\\/ __ \\ /  ___/\\   __\\"<<std::endl
276         <<"|  Y Y  \\  |   |  \\  ___/|  | \\  ___/ \\___ \\  |  |  "<<std::endl
277         <<"|__|_|  /__|___|  /\\___  >__|  \\___  >____  > |__|  "<<std::endl
278         <<"      \\/        \\/     \\/          \\/     \\/        "<<std::endl
279         <<std::endl;
280
281         std::cout<<std::endl;
282         
283         // Port?
284         u16 port = 30000;
285         if(cmd_args.exists("port") && cmd_args.getU16("port") != 0)
286         {
287                 port = cmd_args.getU16("port");
288         }
289         else if(g_settings.exists("port") && g_settings.getU16("port") != 0)
290         {
291                 port = g_settings.getU16("port");
292         }
293         else
294         {
295                 dstream<<"Please specify port (in config or on command line)"
296                                 <<std::endl;
297         }
298         
299         // Figure out path to map
300         std::string map_dir = porting::path_userdata+"/map";
301         if(cmd_args.exists("map-dir"))
302                 map_dir = cmd_args.get("map-dir");
303         else if(g_settings.exists("map-dir"))
304                 map_dir = g_settings.get("map-dir");
305         
306         // Create server
307         Server server(map_dir.c_str());
308         server.start(port);
309         
310         // Run server
311         dedicated_server_loop(server);
312         
313         } //try
314         catch(con::PeerNotFoundException &e)
315         {
316                 dstream<<DTIME<<"Connection timed out."<<std::endl;
317         }
318
319         END_DEBUG_EXCEPTION_HANDLER
320
321         debugstreams_deinit();
322         
323         return 0;
324 }
325
326 //END