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