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