Server configuration is now written when "/#setting whatever = whatever" is issued.
[oweals/minetest.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
24 */
25
26 #ifndef SERVER
27         #ifdef _WIN32
28                 #pragma error ("For a server build, SERVER must be defined globally")
29         #else
30                 #error "For a server build, SERVER must be defined globally"
31         #endif
32 #endif
33
34 #ifdef NDEBUG
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 #include <iostream>
53 #include <fstream>
54 #include <time.h>
55 #include <jmutexautolock.h>
56 #include <locale.h>
57 #include "common_irrlicht.h"
58 #include "debug.h"
59 #include "map.h"
60 #include "player.h"
61 #include "main.h"
62 #include "test.h"
63 #include "environment.h"
64 #include "server.h"
65 #include "serialization.h"
66 #include "constants.h"
67 #include "strfnd.h"
68 #include "porting.h"
69 #include "materials.h"
70 #include "config.h"
71 #include "mineral.h"
72 #include "filesys.h"
73
74 /*
75         Settings.
76         These are loaded from the config file.
77 */
78
79 Settings g_settings;
80
81 extern void set_default_settings();
82
83 // Global profiler
84 Profiler g_profiler;
85
86 // A dummy thing
87 ITextureSource *g_texturesource = NULL;
88
89 /*
90         Debug streams
91 */
92
93 // Connection
94 std::ostream *dout_con_ptr = &dummyout;
95 std::ostream *derr_con_ptr = &dstream_no_stderr;
96
97 // Server
98 std::ostream *dout_server_ptr = &dstream;
99 std::ostream *derr_server_ptr = &dstream;
100
101 // Client
102 std::ostream *dout_client_ptr = &dstream;
103 std::ostream *derr_client_ptr = &dstream;
104
105 /*
106         gettime.h implementation
107 */
108
109 u32 getTimeMs()
110 {
111         /*
112                 Use imprecise system calls directly (from porting.h)
113         */
114         return porting::getTimeMs();
115 }
116
117 int main(int argc, char *argv[])
118 {
119         /*
120                 Initialization
121         */
122
123         // Set locale. This is for forcing '.' as the decimal point.
124         std::locale::global(std::locale("C"));
125         // This enables printing all characters in bitmap font
126         setlocale(LC_CTYPE, "en_US");
127
128         /*
129                 Low-level initialization
130         */
131
132         bool disable_stderr = false;
133 #ifdef _WIN32
134         disable_stderr = true;
135 #endif
136
137         porting::signal_handler_init();
138         bool &kill = *porting::signal_handler_killstatus();
139         
140         // Initialize porting::path_data and porting::path_userdata
141         porting::initializePaths();
142
143         // Create user data directory
144         fs::CreateDir(porting::path_userdata);
145         
146         // Initialize debug streams
147 #ifdef RUN_IN_PLACE
148         std::string debugfile = DEBUGFILE;
149 #else
150         std::string debugfile = porting::path_userdata+"/"+DEBUGFILE;
151 #endif
152         debugstreams_init(disable_stderr, debugfile.c_str());
153         // Initialize debug stacks
154         debug_stacks_init();
155
156         DSTACK(__FUNCTION_NAME);
157
158         // Init material properties table
159         //initializeMaterialProperties();
160
161         // Debug handler
162         BEGIN_DEBUG_EXCEPTION_HANDLER
163
164         // Print startup message
165         dstream<<DTIME<<"minetest-c55"
166                         " with SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
167                         <<", "<<BUILD_INFO
168                         <<std::endl;
169         
170         try
171         {
172         
173         /*
174                 Parse command line
175         */
176         
177         // List all allowed options
178         core::map<std::string, ValueSpec> allowed_options;
179         allowed_options.insert("help", ValueSpec(VALUETYPE_FLAG));
180         allowed_options.insert("config", ValueSpec(VALUETYPE_STRING,
181                         "Load configuration from specified file"));
182         allowed_options.insert("port", ValueSpec(VALUETYPE_STRING));
183         allowed_options.insert("disable-unittests", ValueSpec(VALUETYPE_FLAG));
184         allowed_options.insert("enable-unittests", ValueSpec(VALUETYPE_FLAG));
185         allowed_options.insert("map-dir", ValueSpec(VALUETYPE_STRING));
186
187         Settings cmd_args;
188         
189         bool ret = cmd_args.parseCommandLine(argc, argv, allowed_options);
190
191         if(ret == false || cmd_args.getFlag("help"))
192         {
193                 dstream<<"Allowed options:"<<std::endl;
194                 for(core::map<std::string, ValueSpec>::Iterator
195                                 i = allowed_options.getIterator();
196                                 i.atEnd() == false; i++)
197                 {
198                         dstream<<"  --"<<i.getNode()->getKey();
199                         if(i.getNode()->getValue().type == VALUETYPE_FLAG)
200                         {
201                         }
202                         else
203                         {
204                                 dstream<<" <value>";
205                         }
206                         dstream<<std::endl;
207
208                         if(i.getNode()->getValue().help != NULL)
209                         {
210                                 dstream<<"      "<<i.getNode()->getValue().help
211                                                 <<std::endl;
212                         }
213                 }
214
215                 return cmd_args.getFlag("help") ? 0 : 1;
216         }
217
218
219         /*
220                 Basic initialization
221         */
222
223         // Initialize default settings
224         set_default_settings();
225         
226         // Initialize sockets
227         sockets_init();
228         atexit(sockets_cleanup);
229         
230         /*
231                 Read config file
232         */
233         
234         // Path of configuration file in use
235         std::string configpath = "";
236         
237         if(cmd_args.exists("config"))
238         {
239                 bool r = g_settings.readConfigFile(cmd_args.get("config").c_str());
240                 if(r == false)
241                 {
242                         dstream<<"Could not read configuration from \""
243                                         <<cmd_args.get("config")<<"\""<<std::endl;
244                         return 1;
245                 }
246                 configpath = cmd_args.get("config");
247         }
248         else
249         {
250                 core::array<std::string> filenames;
251                 filenames.push_back(porting::path_userdata + "/minetest.conf");
252 #ifdef RUN_IN_PLACE
253                 filenames.push_back(porting::path_userdata + "/../minetest.conf");
254 #endif
255
256                 for(u32 i=0; i<filenames.size(); i++)
257                 {
258                         bool r = g_settings.readConfigFile(filenames[i].c_str());
259                         if(r)
260                         {
261                                 configpath = filenames[i];
262                                 break;
263                         }
264                 }
265         }
266
267         // Initialize random seed
268         srand(time(0));
269         mysrand(time(0));
270
271         // Initialize stuff
272         
273         init_mapnode();
274         init_mineral();
275
276         /*
277                 Run unit tests
278         */
279         if((ENABLE_TESTS && cmd_args.getFlag("disable-unittests") == false)
280                         || cmd_args.getFlag("enable-unittests") == true)
281         {
282                 run_tests();
283         }
284
285         /*
286                 Check parameters
287         */
288
289         std::cout<<std::endl<<std::endl;
290         
291         std::cout
292         <<"        .__               __                   __   "<<std::endl
293         <<"  _____ |__| ____   _____/  |_  ____   _______/  |_ "<<std::endl
294         <<" /     \\|  |/    \\_/ __ \\   __\\/ __ \\ /  ___/\\   __\\"<<std::endl
295         <<"|  Y Y  \\  |   |  \\  ___/|  | \\  ___/ \\___ \\  |  |  "<<std::endl
296         <<"|__|_|  /__|___|  /\\___  >__|  \\___  >____  > |__|  "<<std::endl
297         <<"      \\/        \\/     \\/          \\/     \\/        "<<std::endl
298         <<std::endl;
299
300         std::cout<<std::endl;
301         
302         // Port?
303         u16 port = 30000;
304         if(cmd_args.exists("port") && cmd_args.getU16("port") != 0)
305         {
306                 port = cmd_args.getU16("port");
307         }
308         else if(g_settings.exists("port") && g_settings.getU16("port") != 0)
309         {
310                 port = g_settings.getU16("port");
311         }
312         else
313         {
314                 dstream<<"Please specify port (in config or on command line)"
315                                 <<std::endl;
316         }
317         
318         // Figure out path to map
319         std::string map_dir = porting::path_userdata+"/world";
320         if(cmd_args.exists("map-dir"))
321                 map_dir = cmd_args.get("map-dir");
322         else if(g_settings.exists("map-dir"))
323                 map_dir = g_settings.get("map-dir");
324         
325         // Create server
326         Server server(map_dir.c_str(), configpath);
327         server.start(port);
328
329         // Run server
330         dedicated_server_loop(server, kill);
331         
332         } //try
333         catch(con::PeerNotFoundException &e)
334         {
335                 dstream<<DTIME<<"Connection timed out."<<std::endl;
336         }
337
338         END_DEBUG_EXCEPTION_HANDLER
339
340         debugstreams_deinit();
341         
342         return 0;
343 }
344
345 //END