]> git.lizzy.rs Git - dragonfireclient.git/blob - src/servermain.cpp
some tinkering with gui. removed updating of configuration file at end
[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 TODO: Move the default settings into some separate file
24
25 */
26
27 #ifndef SERVER
28         #ifdef _WIN32
29         #else
30                 #error "For a server build, SERVER must be defined globally"
31         #endif
32 #endif
33
34 #ifdef UNITTEST_DISABLE
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 #ifdef _WIN32
53         #define WIN32_LEAN_AND_MEAN
54         #include <windows.h>
55         #define sleep_ms(x) Sleep(x)
56 #else
57         #include <unistd.h>
58         #define sleep_ms(x) usleep(x*1000)
59 #endif
60
61 #include <iostream>
62 #include <fstream>
63 #include <time.h>
64 #include <jmutexautolock.h>
65 #include <locale.h>
66 #include "common_irrlicht.h"
67 #include "debug.h"
68 #include "map.h"
69 #include "player.h"
70 #include "main.h"
71 #include "test.h"
72 #include "environment.h"
73 #include "server.h"
74 #include "serialization.h"
75 #include "constants.h"
76 #include "strfnd.h"
77 #include "porting.h"
78
79 // Dummy variable
80 IrrlichtDevice *g_device = NULL;
81
82 /*
83         Settings.
84         These are loaded from the config file.
85 */
86
87 Settings g_settings;
88
89 extern void set_default_settings();
90
91 /*
92         Debug streams
93 */
94
95 // Connection
96 std::ostream *dout_con_ptr = &dummyout;
97 std::ostream *derr_con_ptr = &dstream_no_stderr;
98
99 // Server
100 std::ostream *dout_server_ptr = &dstream;
101 std::ostream *derr_server_ptr = &dstream;
102
103 // Client
104 std::ostream *dout_client_ptr = &dstream;
105 std::ostream *derr_client_ptr = &dstream;
106
107
108 /*
109         Timestamp stuff
110 */
111
112 JMutex g_timestamp_mutex;
113
114 std::string getTimestamp()
115 {
116         if(g_timestamp_mutex.IsInitialized()==false)
117                 return "";
118         JMutexAutoLock lock(g_timestamp_mutex);
119         time_t t = time(NULL);
120         struct tm *tm = localtime(&t);
121         char cs[20];
122         strftime(cs, 20, "%H:%M:%S", tm);
123         return cs;
124 }
125
126 int main(int argc, char *argv[])
127 {
128         /*
129                 Low-level initialization
130         */
131
132         bool disable_stderr = false;
133 #ifdef _WIN32
134         disable_stderr = true;
135 #endif
136
137         // Initialize debug streams
138         debugstreams_init(disable_stderr, DEBUGFILE);
139         // Initialize debug stacks
140         debug_stacks_init();
141
142         DSTACK(__FUNCTION_NAME);
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
160         Settings cmd_args;
161         
162         bool ret = cmd_args.parseCommandLine(argc, argv, allowed_options);
163
164         if(ret == false || cmd_args.getFlag("help"))
165         {
166                 dstream<<"Allowed options:"<<std::endl;
167                 for(core::map<std::string, ValueSpec>::Iterator
168                                 i = allowed_options.getIterator();
169                                 i.atEnd() == false; i++)
170                 {
171                         dstream<<"  --"<<i.getNode()->getKey();
172                         if(i.getNode()->getValue().type == VALUETYPE_FLAG)
173                         {
174                         }
175                         else
176                         {
177                                 dstream<<" <value>";
178                         }
179                         dstream<<std::endl;
180
181                         if(i.getNode()->getValue().help != NULL)
182                         {
183                                 dstream<<"      "<<i.getNode()->getValue().help
184                                                 <<std::endl;
185                         }
186                 }
187
188                 return cmd_args.getFlag("help") ? 0 : 1;
189         }
190
191
192         /*
193                 Basic initialization
194         */
195
196         // Initialize default settings
197         set_default_settings();
198         
199         // Print startup message
200         dstream<<DTIME<<"minetest-c55 server"
201                         " with SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
202                         <<", ENABLE_TESTS="<<ENABLE_TESTS
203                         <<std::endl;
204         
205         // Set locale. This is for forcing '.' as the decimal point.
206         std::locale::global(std::locale("C"));
207         // This enables printing all characters in bitmap font
208         setlocale(LC_CTYPE, "en_US");
209
210         // Initialize sockets
211         sockets_init();
212         atexit(sockets_cleanup);
213         
214         // Initialize timestamp mutex
215         g_timestamp_mutex.Init();
216
217         /*
218                 Initialization
219         */
220
221         /*
222                 Read config file
223         */
224         
225         // Path of configuration file in use
226         std::string configpath = "";
227         
228         if(cmd_args.exists("config"))
229         {
230                 bool r = g_settings.readConfigFile(cmd_args.get("config").c_str());
231                 if(r == false)
232                 {
233                         dstream<<"Could not read configuration from \""
234                                         <<cmd_args.get("config")<<"\""<<std::endl;
235                         return 1;
236                 }
237                 configpath = cmd_args.get("config");
238         }
239         else
240         {
241                 const char *filenames[2] =
242                 {
243                         "../minetest.conf",
244                         "../../minetest.conf"
245                 };
246
247                 for(u32 i=0; i<2; i++)
248                 {
249                         bool r = g_settings.readConfigFile(filenames[i]);
250                         if(r)
251                         {
252                                 configpath = filenames[i];
253                                 break;
254                         }
255                 }
256         }
257
258         // Initialize random seed
259         srand(time(0));
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         // Read map parameters from settings
271
272         HMParams hm_params;
273         hm_params.blocksize = g_settings.getU16("heightmap_blocksize");
274         hm_params.randmax = g_settings.get("height_randmax");
275         hm_params.randfactor = g_settings.get("height_randfactor");
276         hm_params.base = g_settings.get("height_base");
277
278         MapParams map_params;
279         map_params.plants_amount = g_settings.getFloat("plants_amount");
280         map_params.ravines_amount = g_settings.getFloat("ravines_amount");
281
282         /*
283                 Check parameters
284         */
285
286         std::cout<<std::endl<<std::endl;
287         
288         std::cout
289         <<"        .__               __                   __   "<<std::endl
290         <<"  _____ |__| ____   _____/  |_  ____   _______/  |_ "<<std::endl
291         <<" /     \\|  |/    \\_/ __ \\   __\\/ __ \\ /  ___/\\   __\\"<<std::endl
292         <<"|  Y Y  \\  |   |  \\  ___/|  | \\  ___/ \\___ \\  |  |  "<<std::endl
293         <<"|__|_|  /__|___|  /\\___  >__|  \\___  >____  > |__|  "<<std::endl
294         <<"      \\/        \\/     \\/          \\/     \\/        "<<std::endl
295         <<std::endl
296         <<"Now with more waterish water!"
297         <<std::endl;
298
299         std::cout<<std::endl;
300         
301         // Port?
302         u16 port = 30000;
303         if(cmd_args.exists("port"))
304         {
305                 port = cmd_args.getU16("port");
306         }
307         else if(g_settings.exists("port"))
308         {
309                 port = g_settings.getU16("port");
310         }
311         else
312         {
313                 dstream<<"Please specify port (in config or on command line)"
314                                 <<std::endl;
315         }
316         
317         DSTACK("Dedicated server branch");
318         
319         std::cout<<std::endl;
320         std::cout<<"========================"<<std::endl;
321         std::cout<<"Running dedicated server"<<std::endl;
322         std::cout<<"========================"<<std::endl;
323         std::cout<<std::endl;
324
325         Server server("../map", hm_params, map_params);
326         server.start(port);
327
328         for(;;)
329         {
330                 // This is kind of a hack but can be done like this
331                 // because server.step() is very light
332                 sleep_ms(30);
333                 server.step(0.030);
334
335                 static int counter = 0;
336                 counter--;
337                 if(counter <= 0)
338                 {
339                         counter = 10;
340
341                         core::list<PlayerInfo> list = server.getPlayerInfo();
342                         core::list<PlayerInfo>::Iterator i;
343                         static u32 sum_old = 0;
344                         u32 sum = PIChecksum(list);
345                         if(sum != sum_old)
346                         {
347                                 std::cout<<DTIME<<"Player info:"<<std::endl;
348                                 for(i=list.begin(); i!=list.end(); i++)
349                                 {
350                                         i->PrintLine(&std::cout);
351                                 }
352                         }
353                         sum_old = sum;
354                 }
355         }
356
357         } //try
358         catch(con::PeerNotFoundException &e)
359         {
360                 dstream<<DTIME<<"Connection timed out."<<std::endl;
361         }
362 #if CATCH_UNHANDLED_EXCEPTIONS
363         /*
364                 This is what has to be done in every thread to get suitable debug info
365         */
366         catch(std::exception &e)
367         {
368                 dstream<<std::endl<<DTIME<<"An unhandled exception occurred: "
369                                 <<e.what()<<std::endl;
370                 assert(0);
371         }
372 #endif
373
374         debugstreams_deinit();
375         
376         return 0;
377 }
378
379 //END