utility.h: Change Buffer's interface to be more compatible with SharedBuffer's interf...
[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
78 /*
79         Settings.
80         These are loaded from the config file.
81 */
82 Settings main_settings;
83 Settings *g_settings = &main_settings;
84
85 // Global profiler
86 Profiler main_profiler;
87 Profiler *g_profiler = &main_profiler;
88
89 // A dummy thing
90 ITextureSource *g_texturesource = NULL;
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_mapnode();
307         init_mineral();
308
309         /*
310                 Run unit tests
311         */
312         if((ENABLE_TESTS && cmd_args.getFlag("disable-unittests") == false)
313                         || cmd_args.getFlag("enable-unittests") == true)
314         {
315                 run_tests();
316         }
317
318         /*
319                 Check parameters
320         */
321
322         std::cout<<std::endl<<std::endl;
323         
324         std::cout
325         <<"        .__               __                   __   "<<std::endl
326         <<"  _____ |__| ____   _____/  |_  ____   _______/  |_ "<<std::endl
327         <<" /     \\|  |/    \\_/ __ \\   __\\/ __ \\ /  ___/\\   __\\"<<std::endl
328         <<"|  Y Y  \\  |   |  \\  ___/|  | \\  ___/ \\___ \\  |  |  "<<std::endl
329         <<"|__|_|  /__|___|  /\\___  >__|  \\___  >____  > |__|  "<<std::endl
330         <<"      \\/        \\/     \\/          \\/     \\/        "<<std::endl
331         <<std::endl;
332
333         std::cout<<std::endl;
334         
335         // Port?
336         u16 port = 30000;
337         if(cmd_args.exists("port") && cmd_args.getU16("port") != 0)
338         {
339                 port = cmd_args.getU16("port");
340         }
341         else if(g_settings->exists("port") && g_settings->getU16("port") != 0)
342         {
343                 port = g_settings->getU16("port");
344         }
345         else
346         {
347                 dstream<<"Please specify port (in config or on command line)"
348                                 <<std::endl;
349         }
350         
351         // Figure out path to map
352         std::string map_dir = porting::path_userdata+DIR_DELIM+"world";
353         if(cmd_args.exists("map-dir"))
354                 map_dir = cmd_args.get("map-dir");
355         else if(g_settings->exists("map-dir"))
356                 map_dir = g_settings->get("map-dir");
357         
358         // Create server
359         Server server(map_dir.c_str(), configpath);
360         server.start(port);
361
362         // Run server
363         dedicated_server_loop(server, kill);
364         
365         } //try
366         catch(con::PeerNotFoundException &e)
367         {
368                 errorstream<<"Connection timed out."<<std::endl;
369         }
370
371         END_DEBUG_EXCEPTION_HANDLER(errorstream)
372
373         debugstreams_deinit();
374         
375         return 0;
376 }
377
378 //END