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