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