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