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"
80 These are loaded from the config file.
82 Settings main_settings;
83 Settings *g_settings = &main_settings;
86 Profiler main_profiler;
87 Profiler *g_profiler = &main_profiler;
90 ITextureSource *g_texturesource = NULL;
97 std::ostream *dout_con_ptr = &dummyout;
98 std::ostream *derr_con_ptr = &verbosestream;
101 std::ostream *dout_server_ptr = &infostream;
102 std::ostream *derr_server_ptr = &errorstream;
105 std::ostream *dout_client_ptr = &infostream;
106 std::ostream *derr_client_ptr = &errorstream;
109 gettime.h implementation
115 Use imprecise system calls directly (from porting.h)
117 return porting::getTimeMs();
120 class StderrLogOutput: public ILogOutput
123 /* line: Full line with timestamp, level and thread */
124 void printLog(const std::string &line)
126 std::cerr<<line<<std::endl;
128 } main_stderr_log_out;
130 class DstreamNoStderrLogOutput: public ILogOutput
133 /* line: Full line with timestamp, level and thread */
134 void printLog(const std::string &line)
136 dstream_no_stderr<<line<<std::endl;
138 } main_dstream_no_stderr_log_out;
140 int main(int argc, char *argv[])
146 log_add_output_maxlev(&main_stderr_log_out, LMT_ACTION);
147 log_add_output_all_levs(&main_dstream_no_stderr_log_out);
149 log_register_thread("main");
151 // Set locale. This is for forcing '.' as the decimal point.
152 std::locale::global(std::locale("C"));
153 // This enables printing all characters in bitmap font
154 setlocale(LC_CTYPE, "en_US");
157 Low-level initialization
160 bool disable_stderr = false;
162 disable_stderr = true;
165 porting::signal_handler_init();
166 bool &kill = *porting::signal_handler_killstatus();
168 // Initialize porting::path_data and porting::path_userdata
169 porting::initializePaths();
171 // Create user data directory
172 fs::CreateDir(porting::path_userdata);
174 // Initialize debug streams
176 std::string debugfile = DEBUGFILE;
178 std::string debugfile = porting::path_userdata+DIR_DELIM+DEBUGFILE;
180 debugstreams_init(disable_stderr, debugfile.c_str());
181 // Initialize debug stacks
184 DSTACK(__FUNCTION_NAME);
186 // Init material properties table
187 //initializeMaterialProperties();
190 BEGIN_DEBUG_EXCEPTION_HANDLER
192 // Print startup message
193 actionstream<<PROJECT_NAME<<
194 " with SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
205 // List all allowed options
206 core::map<std::string, ValueSpec> allowed_options;
207 allowed_options.insert("help", ValueSpec(VALUETYPE_FLAG));
208 allowed_options.insert("config", ValueSpec(VALUETYPE_STRING,
209 "Load configuration from specified file"));
210 allowed_options.insert("port", ValueSpec(VALUETYPE_STRING));
211 allowed_options.insert("disable-unittests", ValueSpec(VALUETYPE_FLAG));
212 allowed_options.insert("enable-unittests", ValueSpec(VALUETYPE_FLAG));
213 allowed_options.insert("map-dir", ValueSpec(VALUETYPE_STRING));
214 allowed_options.insert("info-on-stderr", ValueSpec(VALUETYPE_FLAG));
218 bool ret = cmd_args.parseCommandLine(argc, argv, allowed_options);
220 if(ret == false || cmd_args.getFlag("help"))
222 dstream<<"Allowed options:"<<std::endl;
223 for(core::map<std::string, ValueSpec>::Iterator
224 i = allowed_options.getIterator();
225 i.atEnd() == false; i++)
227 dstream<<" --"<<i.getNode()->getKey();
228 if(i.getNode()->getValue().type == VALUETYPE_FLAG)
237 if(i.getNode()->getValue().help != NULL)
239 dstream<<" "<<i.getNode()->getValue().help
244 return cmd_args.getFlag("help") ? 0 : 1;
247 if(cmd_args.getFlag("info-on-stderr"))
248 log_add_output(&main_stderr_log_out, LMT_INFO);
254 // Initialize default settings
255 set_default_settings(g_settings);
257 // Initialize sockets
259 atexit(sockets_cleanup);
265 // Path of configuration file in use
266 std::string configpath = "";
268 if(cmd_args.exists("config"))
270 bool r = g_settings->readConfigFile(cmd_args.get("config").c_str());
273 errorstream<<"Could not read configuration from \""
274 <<cmd_args.get("config")<<"\""<<std::endl;
277 configpath = cmd_args.get("config");
281 core::array<std::string> filenames;
282 filenames.push_back(porting::path_userdata +
283 DIR_DELIM + "minetest.conf");
285 filenames.push_back(porting::path_userdata +
286 DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
289 for(u32 i=0; i<filenames.size(); i++)
291 bool r = g_settings->readConfigFile(filenames[i].c_str());
294 configpath = filenames[i];
300 // 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)
339 port = cmd_args.getU16("port");
341 else if(g_settings->exists("port") && g_settings->getU16("port") != 0)
343 port = g_settings->getU16("port");
347 dstream<<"Please specify port (in config or on command line)"
351 // Figure out path to map
352 std::string map_dir = porting::path_userdata+DIR_DELIM+"world";
353 if(cmd_args.exists("map-dir"))
354 map_dir = cmd_args.get("map-dir");
355 else if(g_settings->exists("map-dir"))
356 map_dir = g_settings->get("map-dir");
359 Server server(map_dir.c_str(), configpath);
363 dedicated_server_loop(server, kill);
366 catch(con::PeerNotFoundException &e)
368 errorstream<<"Connection timed out."<<std::endl;
371 END_DEBUG_EXCEPTION_HANDLER(errorstream)
373 debugstreams_deinit();