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"
69 #include "materials.h"
73 #include "defaultsettings.h"
77 #include "mapnode_contentfeatures.h" // For init_contentfeatures
78 #include "content_mapnode.h" // For content_mapnode_init
82 These are loaded from the config file.
84 Settings main_settings;
85 Settings *g_settings = &main_settings;
88 Profiler main_profiler;
89 Profiler *g_profiler = &main_profiler;
96 std::ostream *dout_con_ptr = &dummyout;
97 std::ostream *derr_con_ptr = &verbosestream;
100 std::ostream *dout_server_ptr = &infostream;
101 std::ostream *derr_server_ptr = &errorstream;
104 std::ostream *dout_client_ptr = &infostream;
105 std::ostream *derr_client_ptr = &errorstream;
108 gettime.h implementation
114 Use imprecise system calls directly (from porting.h)
116 return porting::getTimeMs();
119 class StderrLogOutput: public ILogOutput
122 /* line: Full line with timestamp, level and thread */
123 void printLog(const std::string &line)
125 std::cerr<<line<<std::endl;
127 } main_stderr_log_out;
129 class DstreamNoStderrLogOutput: public ILogOutput
132 /* line: Full line with timestamp, level and thread */
133 void printLog(const std::string &line)
135 dstream_no_stderr<<line<<std::endl;
137 } main_dstream_no_stderr_log_out;
139 int main(int argc, char *argv[])
145 log_add_output_maxlev(&main_stderr_log_out, LMT_ACTION);
146 log_add_output_all_levs(&main_dstream_no_stderr_log_out);
148 log_register_thread("main");
150 // Set locale. This is for forcing '.' as the decimal point.
151 std::locale::global(std::locale("C"));
152 // This enables printing all characters in bitmap font
153 setlocale(LC_CTYPE, "en_US");
156 Low-level initialization
159 bool disable_stderr = false;
161 disable_stderr = true;
164 porting::signal_handler_init();
165 bool &kill = *porting::signal_handler_killstatus();
167 // Initialize porting::path_data and porting::path_userdata
168 porting::initializePaths();
170 // Create user data directory
171 fs::CreateDir(porting::path_userdata);
173 // Initialize debug streams
175 std::string debugfile = DEBUGFILE;
177 std::string debugfile = porting::path_userdata+DIR_DELIM+DEBUGFILE;
179 debugstreams_init(disable_stderr, debugfile.c_str());
180 // Initialize debug stacks
183 DSTACK(__FUNCTION_NAME);
185 // Init material properties table
186 //initializeMaterialProperties();
189 BEGIN_DEBUG_EXCEPTION_HANDLER
191 // Print startup message
192 actionstream<<PROJECT_NAME<<
193 " with SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
204 // List all allowed options
205 core::map<std::string, ValueSpec> allowed_options;
206 allowed_options.insert("help", ValueSpec(VALUETYPE_FLAG));
207 allowed_options.insert("config", ValueSpec(VALUETYPE_STRING,
208 "Load configuration from specified file"));
209 allowed_options.insert("port", ValueSpec(VALUETYPE_STRING));
210 allowed_options.insert("disable-unittests", ValueSpec(VALUETYPE_FLAG));
211 allowed_options.insert("enable-unittests", ValueSpec(VALUETYPE_FLAG));
212 allowed_options.insert("map-dir", ValueSpec(VALUETYPE_STRING));
213 allowed_options.insert("info-on-stderr", ValueSpec(VALUETYPE_FLAG));
217 bool ret = cmd_args.parseCommandLine(argc, argv, allowed_options);
219 if(ret == false || cmd_args.getFlag("help"))
221 dstream<<"Allowed options:"<<std::endl;
222 for(core::map<std::string, ValueSpec>::Iterator
223 i = allowed_options.getIterator();
224 i.atEnd() == false; i++)
226 dstream<<" --"<<i.getNode()->getKey();
227 if(i.getNode()->getValue().type == VALUETYPE_FLAG)
236 if(i.getNode()->getValue().help != NULL)
238 dstream<<" "<<i.getNode()->getValue().help
243 return cmd_args.getFlag("help") ? 0 : 1;
246 if(cmd_args.getFlag("info-on-stderr"))
247 log_add_output(&main_stderr_log_out, LMT_INFO);
253 // Initialize default settings
254 set_default_settings(g_settings);
256 // Initialize sockets
258 atexit(sockets_cleanup);
264 // Path of configuration file in use
265 std::string configpath = "";
267 if(cmd_args.exists("config"))
269 bool r = g_settings->readConfigFile(cmd_args.get("config").c_str());
272 errorstream<<"Could not read configuration from \""
273 <<cmd_args.get("config")<<"\""<<std::endl;
276 configpath = cmd_args.get("config");
280 core::array<std::string> filenames;
281 filenames.push_back(porting::path_userdata +
282 DIR_DELIM + "minetest.conf");
284 filenames.push_back(porting::path_userdata +
285 DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
288 for(u32 i=0; i<filenames.size(); i++)
290 bool r = g_settings->readConfigFile(filenames[i].c_str());
293 configpath = filenames[i];
299 // Initialize random seed
305 // Initialize content feature table without textures
306 init_contentfeatures(NULL);
307 // Initialize mapnode content without textures
308 content_mapnode_init(NULL);
315 if((ENABLE_TESTS && cmd_args.getFlag("disable-unittests") == false)
316 || cmd_args.getFlag("enable-unittests") == true)
325 std::cout<<std::endl<<std::endl;
328 <<" .__ __ __ "<<std::endl
329 <<" _____ |__| ____ _____/ |_ ____ _______/ |_ "<<std::endl
330 <<" / \\| |/ \\_/ __ \\ __\\/ __ \\ / ___/\\ __\\"<<std::endl
331 <<"| Y Y \\ | | \\ ___/| | \\ ___/ \\___ \\ | | "<<std::endl
332 <<"|__|_| /__|___| /\\___ >__| \\___ >____ > |__| "<<std::endl
333 <<" \\/ \\/ \\/ \\/ \\/ "<<std::endl
336 std::cout<<std::endl;
340 if(cmd_args.exists("port") && cmd_args.getU16("port") != 0)
342 port = cmd_args.getU16("port");
344 else if(g_settings->exists("port") && g_settings->getU16("port") != 0)
346 port = g_settings->getU16("port");
350 dstream<<"Please specify port (in config or on command line)"
354 // Figure out path to map
355 std::string map_dir = porting::path_userdata+DIR_DELIM+"world";
356 if(cmd_args.exists("map-dir"))
357 map_dir = cmd_args.get("map-dir");
358 else if(g_settings->exists("map-dir"))
359 map_dir = g_settings->get("map-dir");
362 Server server(map_dir.c_str(), configpath);
366 dedicated_server_loop(server, kill);
369 catch(con::PeerNotFoundException &e)
371 errorstream<<"Connection timed out."<<std::endl;
374 END_DEBUG_EXCEPTION_HANDLER(errorstream)
376 debugstreams_deinit();