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 ==============================
28 #pragma error ("For a server build, SERVER must be defined globally")
30 #error "For a server build, SERVER must be defined globally"
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")
55 #include <jmutexautolock.h>
57 #include "common_irrlicht.h"
63 #include "environment.h"
65 #include "serialization.h"
66 #include "constants.h"
71 #include "defaultsettings.h"
75 #include "nodedef.h" // For init_contentfeatures
76 #include "content_mapnode.h" // For content_mapnode_init
81 These are loaded from the config file.
83 Settings main_settings;
84 Settings *g_settings = &main_settings;
87 Profiler main_profiler;
88 Profiler *g_profiler = &main_profiler;
95 std::ostream *dout_con_ptr = &dummyout;
96 std::ostream *derr_con_ptr = &verbosestream;
99 std::ostream *dout_server_ptr = &infostream;
100 std::ostream *derr_server_ptr = &errorstream;
103 std::ostream *dout_client_ptr = &infostream;
104 std::ostream *derr_client_ptr = &errorstream;
107 gettime.h implementation
113 Use imprecise system calls directly (from porting.h)
115 return porting::getTimeMs();
118 class StderrLogOutput: public ILogOutput
121 /* line: Full line with timestamp, level and thread */
122 void printLog(const std::string &line)
124 std::cerr<<line<<std::endl;
126 } main_stderr_log_out;
128 class DstreamNoStderrLogOutput: public ILogOutput
131 /* line: Full line with timestamp, level and thread */
132 void printLog(const std::string &line)
134 dstream_no_stderr<<line<<std::endl;
136 } main_dstream_no_stderr_log_out;
138 int main(int argc, char *argv[])
144 log_add_output_maxlev(&main_stderr_log_out, LMT_ACTION);
145 log_add_output_all_levs(&main_dstream_no_stderr_log_out);
147 log_register_thread("main");
149 // Set locale. This is for forcing '.' as the decimal point.
150 std::locale::global(std::locale("C"));
151 // This enables printing all characters in bitmap font
152 setlocale(LC_CTYPE, "en_US");
155 Low-level initialization
158 bool disable_stderr = false;
160 disable_stderr = true;
163 porting::signal_handler_init();
164 bool &kill = *porting::signal_handler_killstatus();
166 porting::initializePaths();
168 // Create user data directory
169 fs::CreateDir(porting::path_user);
171 // Initialize debug streams
173 std::string debugfile = DEBUGFILE;
175 std::string debugfile = porting::path_user+DIR_DELIM+DEBUGFILE;
177 debugstreams_init(disable_stderr, debugfile.c_str());
178 // Initialize debug stacks
181 DSTACK(__FUNCTION_NAME);
183 // Init material properties table
184 //initializeMaterialProperties();
187 BEGIN_DEBUG_EXCEPTION_HANDLER
189 // Print startup message
190 actionstream<<PROJECT_NAME<<
191 " with SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
202 // List all allowed options
203 core::map<std::string, ValueSpec> allowed_options;
204 allowed_options.insert("help", ValueSpec(VALUETYPE_FLAG,
205 "Show allowed options"));
206 allowed_options.insert("config", ValueSpec(VALUETYPE_STRING,
207 "Load configuration from specified file"));
208 allowed_options.insert("port", ValueSpec(VALUETYPE_STRING,
209 "Set network port (UDP) to use"));
210 allowed_options.insert("disable-unittests", ValueSpec(VALUETYPE_FLAG,
211 "Disable unit tests"));
212 allowed_options.insert("enable-unittests", ValueSpec(VALUETYPE_FLAG,
213 "Enable unit tests"));
214 allowed_options.insert("map-dir", ValueSpec(VALUETYPE_STRING,
215 "Map directory (where everything in the world is stored)"));
216 allowed_options.insert("info-on-stderr", ValueSpec(VALUETYPE_FLAG,
217 "Print debug information to console"));
221 bool ret = cmd_args.parseCommandLine(argc, argv, allowed_options);
223 if(ret == false || cmd_args.getFlag("help"))
225 dstream<<"Allowed options:"<<std::endl;
226 for(core::map<std::string, ValueSpec>::Iterator
227 i = allowed_options.getIterator();
228 i.atEnd() == false; i++)
230 dstream<<" --"<<i.getNode()->getKey();
231 if(i.getNode()->getValue().type == VALUETYPE_FLAG)
240 if(i.getNode()->getValue().help != NULL)
242 dstream<<" "<<i.getNode()->getValue().help
247 return cmd_args.getFlag("help") ? 0 : 1;
250 if(cmd_args.getFlag("info-on-stderr"))
251 log_add_output(&main_stderr_log_out, LMT_INFO);
257 // Initialize default settings
258 set_default_settings(g_settings);
260 // Initialize sockets
262 atexit(sockets_cleanup);
268 // Path of configuration file in use
269 std::string configpath = "";
271 if(cmd_args.exists("config"))
273 bool r = g_settings->readConfigFile(cmd_args.get("config").c_str());
276 errorstream<<"Could not read configuration from \""
277 <<cmd_args.get("config")<<"\""<<std::endl;
280 configpath = cmd_args.get("config");
284 core::array<std::string> filenames;
285 filenames.push_back(porting::path_user +
286 DIR_DELIM + "minetest.conf");
288 // Try also from a lower level (to aid having the same configuration
289 // for many RUN_IN_PLACE installs)
290 filenames.push_back(porting::path_user +
291 DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
294 for(u32 i=0; i<filenames.size(); i++)
296 bool r = g_settings->readConfigFile(filenames[i].c_str());
299 configpath = filenames[i];
305 // Initialize random seed
312 if((ENABLE_TESTS && cmd_args.getFlag("disable-unittests") == false)
313 || cmd_args.getFlag("enable-unittests") == true)
322 std::cout<<std::endl<<std::endl;
325 <<" .__ __ __ "<<std::endl
326 <<" _____ |__| ____ _____/ |_ ____ _______/ |_ "<<std::endl
327 <<" / \\| |/ \\_/ __ \\ __\\/ __ \\ / ___/\\ __\\"<<std::endl
328 <<"| Y Y \\ | | \\ ___/| | \\ ___/ \\___ \\ | | "<<std::endl
329 <<"|__|_| /__|___| /\\___ >__| \\___ >____ > |__| "<<std::endl
330 <<" \\/ \\/ \\/ \\/ \\/ "<<std::endl
333 std::cout<<std::endl;
337 if(cmd_args.exists("port") && cmd_args.getU16("port") != 0)
338 port = cmd_args.getU16("port");
339 else if(g_settings->exists("port") && g_settings->getU16("port") != 0)
340 port = g_settings->getU16("port");
343 std::string map_dir = porting::path_user + DIR_DELIM + "server" + DIR_DELIM + "worlds" + DIR_DELIM + "world";
344 if(cmd_args.exists("map-dir"))
345 map_dir = cmd_args.get("map-dir");
346 else if(g_settings->exists("map-dir"))
347 map_dir = g_settings->get("map-dir");
349 // No map-dir option was specified.
350 // Check if the world is found from the default directory, and if
351 // not, see if the legacy world directory exists.
352 std::string legacy_map_dir = porting::path_user+DIR_DELIM+".."+DIR_DELIM+"world";
353 if(!fs::PathExists(map_dir) && fs::PathExists(legacy_map_dir)){
354 errorstream<<"Warning: Using legacy world directory \""
355 <<legacy_map_dir<<"\""<<std::endl;
356 map_dir = legacy_map_dir;
362 Server server(map_dir, configpath, "mesetint");
366 dedicated_server_loop(server, kill);
369 catch(con::PeerNotFoundException &e)
371 errorstream<<"Connection timed out."<<std::endl;
375 errorstream<<e.what()<<std::endl;
378 END_DEBUG_EXCEPTION_HANDLER(errorstream)
380 debugstreams_deinit();