3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
21 =============================== NOTES ==============================
23 TODO: Move the default settings into some separate file
30 #error "For a server build, SERVER must be defined globally"
34 #ifdef UNITTEST_DISABLE
36 #pragma message ("Disabling unit tests")
38 #warning "Disabling unit tests"
41 #define ENABLE_TESTS 0
44 #define ENABLE_TESTS 1
48 #pragma comment(lib, "jthread.lib")
49 #pragma comment(lib, "zlibwapi.lib")
53 #define WIN32_LEAN_AND_MEAN
55 #define sleep_ms(x) Sleep(x)
58 #define sleep_ms(x) usleep(x*1000)
64 #include <jmutexautolock.h>
66 #include "common_irrlicht.h"
72 #include "environment.h"
74 #include "serialization.h"
75 #include "constants.h"
80 IrrlichtDevice *g_device = NULL;
84 These are loaded from the config file.
89 extern void set_default_settings();
96 std::ostream *dout_con_ptr = &dummyout;
97 std::ostream *derr_con_ptr = &dstream_no_stderr;
100 std::ostream *dout_server_ptr = &dstream;
101 std::ostream *derr_server_ptr = &dstream;
104 std::ostream *dout_client_ptr = &dstream;
105 std::ostream *derr_client_ptr = &dstream;
112 JMutex g_timestamp_mutex;
114 std::string getTimestamp()
116 if(g_timestamp_mutex.IsInitialized()==false)
118 JMutexAutoLock lock(g_timestamp_mutex);
119 time_t t = time(NULL);
120 struct tm *tm = localtime(&t);
122 strftime(cs, 20, "%H:%M:%S", tm);
126 int main(int argc, char *argv[])
129 Low-level initialization
132 bool disable_stderr = false;
134 disable_stderr = true;
137 // Initialize debug streams
138 debugstreams_init(disable_stderr, DEBUGFILE);
139 // Initialize debug stacks
142 DSTACK(__FUNCTION_NAME);
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));
162 bool ret = cmd_args.parseCommandLine(argc, argv, allowed_options);
164 if(ret == false || cmd_args.getFlag("help"))
166 dstream<<"Allowed options:"<<std::endl;
167 for(core::map<std::string, ValueSpec>::Iterator
168 i = allowed_options.getIterator();
169 i.atEnd() == false; i++)
171 dstream<<" --"<<i.getNode()->getKey();
172 if(i.getNode()->getValue().type == VALUETYPE_FLAG)
181 if(i.getNode()->getValue().help != NULL)
183 dstream<<" "<<i.getNode()->getValue().help
188 return cmd_args.getFlag("help") ? 0 : 1;
196 // Initialize default settings
197 set_default_settings();
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
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");
210 // Initialize sockets
212 atexit(sockets_cleanup);
214 // Initialize timestamp mutex
215 g_timestamp_mutex.Init();
225 // Path of configuration file in use
226 std::string configpath = "";
228 if(cmd_args.exists("config"))
230 bool r = g_settings.readConfigFile(cmd_args.get("config").c_str());
233 dstream<<"Could not read configuration from \""
234 <<cmd_args.get("config")<<"\""<<std::endl;
237 configpath = cmd_args.get("config");
241 const char *filenames[2] =
244 "../../minetest.conf"
247 for(u32 i=0; i<2; i++)
249 bool r = g_settings.readConfigFile(filenames[i]);
252 configpath = filenames[i];
258 // Initialize random seed
264 if((ENABLE_TESTS && cmd_args.getFlag("disable-unittests") == false)
265 || cmd_args.getFlag("enable-unittests") == true)
270 // Read map parameters from settings
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");
278 MapParams map_params;
279 map_params.plants_amount = g_settings.getFloat("plants_amount");
280 map_params.ravines_amount = g_settings.getFloat("ravines_amount");
286 std::cout<<std::endl<<std::endl;
289 <<" .__ __ __ "<<std::endl
290 <<" _____ |__| ____ _____/ |_ ____ _______/ |_ "<<std::endl
291 <<" / \\| |/ \\_/ __ \\ __\\/ __ \\ / ___/\\ __\\"<<std::endl
292 <<"| Y Y \\ | | \\ ___/| | \\ ___/ \\___ \\ | | "<<std::endl
293 <<"|__|_| /__|___| /\\___ >__| \\___ >____ > |__| "<<std::endl
294 <<" \\/ \\/ \\/ \\/ \\/ "<<std::endl
296 <<"Now with more waterish water!"
299 std::cout<<std::endl;
303 if(cmd_args.exists("port"))
305 port = cmd_args.getU16("port");
307 else if(g_settings.exists("port"))
309 port = g_settings.getU16("port");
313 dstream<<"Please specify port (in config or on command line)"
317 DSTACK("Dedicated server branch");
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;
325 Server server("../map", hm_params, map_params);
330 // This is kind of a hack but can be done like this
331 // because server.step() is very light
335 static int counter = 0;
341 core::list<PlayerInfo> list = server.getPlayerInfo();
342 core::list<PlayerInfo>::Iterator i;
343 static u32 sum_old = 0;
344 u32 sum = PIChecksum(list);
347 std::cout<<DTIME<<"Player info:"<<std::endl;
348 for(i=list.begin(); i!=list.end(); i++)
350 i->PrintLine(&std::cout);
358 Update configuration file
362 g_settings.updateConfigFile(configpath.c_str());
366 catch(con::PeerNotFoundException &e)
368 dstream<<DTIME<<"Connection timed out."<<std::endl;
370 #if CATCH_UNHANDLED_EXCEPTIONS
372 This is what has to be done in every thread to get suitable debug info
374 catch(std::exception &e)
376 dstream<<std::endl<<DTIME<<"An unhandled exception occurred: "
377 <<e.what()<<std::endl;
382 debugstreams_deinit();