]> git.lizzy.rs Git - dragonfireclient.git/blob - src/servermain.cpp
Use the logger; also, default to not showing much crap in console. Use --info-on...
[dragonfireclient.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 DstreamLogOutput: public ILogOutput
121 {
122 public:
123         /* line: Full line with timestamp, level and thread */
124         void printLog(const std::string &line)
125         {
126                 dstream<<line<<std::endl;
127         }
128 } main_dstream_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_dstream_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+"/"+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_dstream_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 + "/minetest.conf");
283 #ifdef RUN_IN_PLACE
284                 filenames.push_back(porting::path_userdata + "/../minetest.conf");
285 #endif
286
287                 for(u32 i=0; i<filenames.size(); i++)
288                 {
289                         bool r = g_settings->readConfigFile(filenames[i].c_str());
290                         if(r)
291                         {
292                                 configpath = filenames[i];
293                                 break;
294                         }
295                 }
296         }
297
298         // Initialize random seed
299         srand(time(0));
300         mysrand(time(0));
301
302         // Initialize stuff
303         
304         init_mapnode();
305         init_mineral();
306
307         /*
308                 Run unit tests
309         */
310         if((ENABLE_TESTS && cmd_args.getFlag("disable-unittests") == false)
311                         || cmd_args.getFlag("enable-unittests") == true)
312         {
313                 run_tests();
314         }
315
316         /*
317                 Check parameters
318         */
319
320         std::cout<<std::endl<<std::endl;
321         
322         std::cout
323         <<"        .__               __                   __   "<<std::endl
324         <<"  _____ |__| ____   _____/  |_  ____   _______/  |_ "<<std::endl
325         <<" /     \\|  |/    \\_/ __ \\   __\\/ __ \\ /  ___/\\   __\\"<<std::endl
326         <<"|  Y Y  \\  |   |  \\  ___/|  | \\  ___/ \\___ \\  |  |  "<<std::endl
327         <<"|__|_|  /__|___|  /\\___  >__|  \\___  >____  > |__|  "<<std::endl
328         <<"      \\/        \\/     \\/          \\/     \\/        "<<std::endl
329         <<std::endl;
330
331         std::cout<<std::endl;
332         
333         // Port?
334         u16 port = 30000;
335         if(cmd_args.exists("port") && cmd_args.getU16("port") != 0)
336         {
337                 port = cmd_args.getU16("port");
338         }
339         else if(g_settings->exists("port") && g_settings->getU16("port") != 0)
340         {
341                 port = g_settings->getU16("port");
342         }
343         else
344         {
345                 dstream<<"Please specify port (in config or on command line)"
346                                 <<std::endl;
347         }
348         
349         // Figure out path to map
350         std::string map_dir = porting::path_userdata+"/world";
351         if(cmd_args.exists("map-dir"))
352                 map_dir = cmd_args.get("map-dir");
353         else if(g_settings->exists("map-dir"))
354                 map_dir = g_settings->get("map-dir");
355         
356         // Create server
357         Server server(map_dir.c_str(), configpath);
358         server.start(port);
359
360         // Run server
361         dedicated_server_loop(server, kill);
362         
363         } //try
364         catch(con::PeerNotFoundException &e)
365         {
366                 errorstream<<"Connection timed out."<<std::endl;
367         }
368
369         END_DEBUG_EXCEPTION_HANDLER(errorstream)
370
371         debugstreams_deinit();
372         
373         return 0;
374 }
375
376 //END