aboutsummaryrefslogtreecommitdiff
path: root/textures/base/pack/drop_btn.png
blob: 5d777108a6dde8cbc6658b9cbe6224d996264060 (plain)
ofshex dumpascii
0000 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 40 00 00 00 40 08 06 00 00 00 aa 69 71 .PNG........IHDR...@...@......iq
0020 de 00 00 03 d2 49 44 41 54 78 da ed 99 4f 68 1d 55 14 c6 7f 27 4d 43 6b d3 d8 4a 57 15 31 e2 a2 .....IDATx...Oh.U...'MCk..JW.1..
0040 71 63 69 93 16 5c 14 8a 22 ba 93 74 e3 46 10 41 50 dc b9 51 74 93 b8 ac 42 0b 2e 6c a5 42 17 5d qci..\.."..t.F.AP..Qt...B..l.B.]
0060 89 4d 03 a5 e8 42 90 a0 b4 68 13 37 45 24 25 50 ba d0 d4 42 aa c5 28 2d c6 f8 75 d1 f3 60 fa bc .M...B...h.7E$%P...B..(-..u..`..
0080 33 79 93 79 6f fe e6 83 cb 9b 77 e7 ce 9f ef f7 ce 3d 33 f7 3c d8 50 67 92 34 a1 fb 9a 68 aa 71 3y.yo.....w......=3.<.Pg.4...h.q
00a0 49 fa 25 b2 3d d1 34 e3 97 23 ad 36 20 2c 64 1c 98 f4 af bf 02 8b 09 c7 ef 06 1e f5 ed 49 33 fb I.%.=.4..#.6.,d..............I3.
00c0 a0 b2 00 52 1a af 0d 08 73 f3 5a a7 f1 58 10 66 66 55 00 d0 17 d9 9e f3 9b 3f e0 46 d2 1a 3f e0 ...R....s.Z..X.ffU.......?.F..?.
00e0 c7 9f ab dc 14 f0 08 38 e9 7d 63 de d2 e6 80 73 c0 b4 6f 9f a9 4a 04 84 00 d0 01 88 38 e3 00 bb .......8.}c....s..o..J......8...
0100 80 17 fd dc bb 81 01 ef ff 17 f8 03 b8 0c 9c 07 e6 cc ec af 32 03 88 03 11 67 bc a5 71 e0 59 60 ....................2....g..q.Y`
0120 c7 1a d7 5e 01 6e 01 f3 c0 05 60 ca cc ae 97 11 40 3b 88 38 e3 2d 1d f7 28 48 ab 7f 80 1f fc f8 ...^.n....`.....@;.8.-..(H......
0140 af f3 8a 8e be 14 63 e7 fc 73 ba 47 f7 32 00 1c 02 ce 02 57 fc 45 6c b8 4c 00 3a d5 b7 c0 ed 8c ......c..s.G.2.....W.El.L.:.....
0160 f7 f4 04 f0 3e 70 bc d7 10 fa 7b 70 ce ef 80 6d 81 24 b8 09 18 02 76 76 78 dd 01 e0 25 e0 31 49 ....>p....{p...m.$....vvx...%.1I
0180 6f 98 d9 8f 55 01 b0 04 bc 10 f7 18 94 b4 1d 18 75 73 87 80 3d c0 60 42 8e 1a 05 3e 97 f4 72 2f o...U...........us..=.`B...>..r/
01a0 20 f4 91 b3 cc 6c d9 cc 66 cc ec 6d 33 1b f3 28 39 02 cc fa a3 32 a4 27 81 29 49 a3 95 07 10 03 .....l..f..m3..(9....2.'.)I.....
01c0 64 1a 78 0e 78 1e 98 01 56 03 43 1f 07 3e ed 76 4e e8 6b 7b cc 75 43 e3 59 22 03 78 0d 38 15 13 d.x.x...V.C..>.vN.k{.uC.Y".x.8..
01e0 0d fb 81 77 25 0d f6 0a c0 58 17 cc 1f c9 18 11 d7 81 77 80 8f 02 10 0c 78 d5 23 a5 7b 00 3c 61 ...w%....X........w.....x.#.{.<a
0200 4d 3a 80 37 d7 01 62 1c 38 e3 e6 27 b3 ae 03 cc 6c 19 f8 18 f8 02 50 db ee ad c0 7b dd 8c 82 a4 M:.7..b.8..'....l.....P....{....
0220 4a d0 ac a4 13 91 26 49 af 44 da 54 2f 2b 43 92 86 25 fd ac ff 6b 45 d2 e1 3c 4b 62 b3 de e4 a6 J.....&I.D.T/+C..%...kE..<Kb....
0240 a7 f2 2a 89 49 1a 97 b4 1a 80 70 3a 97 2c dd 06 42 79 d7 02 25 0d 4a ba 16 b8 fe ad 9e 4d 83 35 ..*.I.....p:.,..By..%.J......M.5
0260 40 e4 5e 04 4d f8 11 0e d3 04 79 2e 58 09 00 38 46 53 24 e9 66 00 c0 f7 95 7f 13 4c a1 50 b1 64 @.^.M.....y.X..8FS$.f......L.P.d
0280 b8 49 00 2e 06 fa 1e a9 3d 00 49 9b 7d f3 7c 60 f7 e6 ac 4f 82 2a 44 c0 7f 6d 15 a9 d0 4a b1 fe .I......=.I.}.|`...O.*D..m...J..
02a0 00 bc 46 18 5a 25 1e ac 35 00 33 8b ae 05 56 02 43 9e 69 4a 12 04 f8 33 d0 37 d2 24 00 cb 81 be ..F.Z%..5.3...V.C.iJ...3.7.$....
02c0 87 9b 04 20 94 03 fa 23 4f 0c ab 3b 80 34 f9 a2 23 f5 97 e5 e6 25 6d 02 5e e7 7e 85 f8 13 33 bb .......#O..;.4..#....%m.^.~...3.
02e0 93 f1 7c 5b 80 b7 80 bf 81 cf cc 6c b5 d4 2f 3b 6d 35 86 4b 92 86 02 e3 ae 06 d6 03 57 03 e3 86 ..|[.......l../;m5.K........W...
0300 fc 1c 2d 5d 70 20 a5 05 70 3a 60 6c 46 d2 b6 b4 00 24 3d 24 e9 9b c0 b8 b3 65 35 bf 47 f1 7a 00 ..-]p...p:`lF....$=$.....e5.G.z.
0320 c2 5a 00 12 cc b7 f4 54 19 01 6c 97 f4 5b 27 10 24 2d 04 f6 2f 44 ce 95 64 fe 66 68 5a 95 05 c2 .Z.....T..l..['.$-../D..d.fhZ...
0340 88 a4 c5 84 9b bf e4 e3 42 a5 b1 6b be ef 62 c2 f1 37 24 8d 50 66 49 da 27 69 29 c1 c4 82 a4 f9 ........B..k..b..7$.PfI.'i).....
0360 40 ff 7c cc d4 68 e9 77 49 fb ab 52 f5 59 2b 12 d2 aa fc bf 7c 0f 21 dc 28 65 d2 cb 09 42 75 cd @.|..h.wI..R.Y+.....|.!.(e...Bu.
0380 77 01 42 f5 cd 67 80 50 1f f3 eb 80 50 3f f3 29 20 d4 d7 7c db 2b f3 62 23 cd 27 40 68 8e f9 08 w.B..g.P....P?.)...|.+.b#.'@h...
03a0 84 a7 25 5d 91 f4 93 a4 7d 6c 68 43 d9 ca 68 05 85 f1 2e 2f 7f ed 28 c8 f7 6d 2f 93 2d 15 35 8f ..%]....}lhC..h..../..(..m/.-.5.
03c0 bf 52 f1 fa 12 8a ab 0a ef 2d 41 f4 ef 2d 12 80 95 65 fa 17 05 e0 6e 09 00 dc 29 12 c0 87 84 ff .R.......-A..-...e....n...).....
03e0 e5 c9 4b ab c0 d1 42 43 51 d2 56 a0 a8 5a fd dd ac 7f bc d4 46 f7 00 a1 6c b6 f6 5a fa 83 91 00 ..K...BCQ.V..Z......F...l..Z....
0400 00 00 00 49 45 4e 44 ae 42 60 82 ...IEND.B`.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
/*
Minetest
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "irrlicht.h" // createDevice
#include "mainmenumanager.h"
#include "irrlichttypes_extrabloated.h"
#include "chat_interface.h"
#include "debug.h"
#include "unittest/test.h"
#include "server.h"
#include "filesys.h"
#include "version.h"
#include "guiMainMenu.h"
#include "game.h"
#include "defaultsettings.h"
#include "gettext.h"
#include "log.h"
#include "quicktune.h"
#include "httpfetch.h"
#include "guiEngine.h"
#include "gameparams.h"
#include "database.h"
#include "config.h"
#include "player.h"
#include "porting.h"
#include "network/socket.h"
#if USE_CURSES
	#include "terminal_chat_console.h"
#endif
#ifndef SERVER
#include "client/clientlauncher.h"
#endif

#ifdef HAVE_TOUCHSCREENGUI
	#include "touchscreengui.h"
#endif

#if !defined(SERVER) && \
	(IRRLICHT_VERSION_MAJOR == 1) && \
	(IRRLICHT_VERSION_MINOR == 8) && \
	(IRRLICHT_VERSION_REVISION == 2)
	#error "Irrlicht 1.8.2 is known to be broken - please update Irrlicht to version >= 1.8.3"
#endif

#define DEBUGFILE "debug.txt"
#define DEFAULT_SERVER_PORT 30000

typedef std::map<std::string, ValueSpec> OptionList;

/**********************************************************************
 * Private functions
 **********************************************************************/

static bool get_cmdline_opts(int argc, char *argv[], Settings *cmd_args);
static void set_allowed_options(OptionList *allowed_options);

static void print_help(const OptionList &allowed_options);
static void print_allowed_options(const OptionList &allowed_options);
static void print_version();
static void print_worldspecs(const std::vector<WorldSpec> &worldspecs,
							 std::ostream &os);
static void print_modified_quicktune_values();

static void list_game_ids();
static void list_worlds();
static void setup_log_params(const Settings &cmd_args);
static bool create_userdata_path();
static bool init_common(const Settings &cmd_args, int argc, char *argv[]);
static void startup_message();
static bool read_config_file(const Settings &cmd_args);
static void init_log_streams(const Settings &cmd_args);

static bool game_configure(GameParams *game_params, const Settings &cmd_args);
static void game_configure_port(GameParams *game_params, const Settings &cmd_args);

static bool game_configure_world(GameParams *game_params, const Settings &cmd_args);
static bool get_world_from_cmdline(GameParams *game_params, const Settings &cmd_args);
static bool get_world_from_config(GameParams *game_params, const Settings &cmd_args);
static bool auto_select_world(GameParams *game_params);
static std::string get_clean_world_path(const std::string &path);

static bool game_configure_subgame(GameParams *game_params, const Settings &cmd_args);
static bool get_game_from_cmdline(GameParams *game_params, const Settings &cmd_args);
static bool determine_subgame(GameParams *game_params);

static bool run_dedicated_server(const GameParams &game_params, const Settings &cmd_args);
static bool migrate_map_database(const GameParams &game_params, const Settings &cmd_args);

/**********************************************************************/


FileLogOutput file_log_output;

static OptionList allowed_options;

int main(int argc, char *argv[])
{
	int retval;
	debug_set_exception_handler();

	g_logger.registerThread("Main");
	g_logger.addOutputMaxLevel(&stderr_output, LL_ACTION);

	Settings cmd_args;
	bool cmd_args_ok = get_cmdline_opts(argc, argv, &cmd_args);
	if (!cmd_args_ok
			|| cmd_args.getFlag("help")
			|| cmd_args.exists("nonopt1")) {
		porting::attachOrCreateConsole();
		print_help(allowed_options);
		return cmd_args_ok ? 0 : 1;
	}
	if (cmd_args.getFlag("console"))
		porting::attachOrCreateConsole();

	if (cmd_args.getFlag("version")) {
		porting::attachOrCreateConsole();
		print_version();
		return 0;
	}

	setup_log_params(cmd_args);

	porting::signal_handler_init();

#ifdef __ANDROID__
	porting::initAndroid();
	porting::initializePathsAndroid();
#else
	porting::initializePaths();
#endif

	if (!create_userdata_path()) {
		errorstream << "Cannot create user data directory" << std::endl;
		return 1;
	}

	// Initialize debug stacks
	DSTACK(FUNCTION_NAME);

	// Debug handler
	BEGIN_DEBUG_EXCEPTION_HANDLER

	// List gameids if requested
	if (cmd_args.exists("gameid") && cmd_args.get("gameid") == "list") {
		list_game_ids();
		return 0;
	}

	// List worlds if requested
	if (cmd_args.exists("world") && cmd_args.get("world") == "list") {
		list_worlds();
		return 0;
	}

	if (!init_common(cmd_args, argc, argv))
		return 1;

	if (g_settings->getBool("enable_console"))
		porting::attachOrCreateConsole();

#ifndef __ANDROID__
	// Run unit tests
	if (cmd_args.getFlag("run-unittests")) {
		return run_tests();
	}
#endif

	GameParams game_params;
#ifdef SERVER
	porting::attachOrCreateConsole();
	game_params.is_dedicated_server = true;
#else
	const bool isServer = cmd_args.getFlag("server");
	if (isServer)
		porting::attachOrCreateConsole();
	game_params.is_dedicated_server = isServer;
#endif

	if (!game_configure(&game_params, cmd_args))
		return 1;

	sanity_check(!game_params.world_path.empty());

	infostream << "Using commanded world path ["
	           << game_params.world_path << "]" << std::endl;

	if (game_params.is_dedicated_server)
		return run_dedicated_server(game_params, cmd_args) ? 0 : 1;

#ifndef SERVER
	ClientLauncher launcher;
	retval = launcher.run(game_params, cmd_args) ? 0 : 1;
#else
	retval = 0;
#endif

	// Update configuration file
	if (!g_settings_path.empty())
		g_settings->updateConfigFile(g_settings_path.c_str());

	print_modified_quicktune_values();

	// Stop httpfetch thread (if started)
	httpfetch_cleanup();

	END_DEBUG_EXCEPTION_HANDLER

	return retval;
}


/*****************************************************************************
 * Startup / Init
 *****************************************************************************/


static bool get_cmdline_opts(int argc, char *argv[], Settings *cmd_args)
{
	set_allowed_options(&allowed_options);

	return cmd_args->parseCommandLine(argc, argv, allowed_options);
}

static void set_allowed_options(OptionList *allowed_options)
{
	allowed_options->clear();

	allowed_options->insert(std::make_pair("help", ValueSpec(VALUETYPE_FLAG,
			_("Show allowed options"))));
	allowed_options->insert(std::make_pair("version", ValueSpec(VALUETYPE_FLAG,
			_("Show version information"))));
	allowed_options->insert(std::make_pair("config", ValueSpec(VALUETYPE_STRING,
			_("Load configuration from specified file"))));
	allowed_options->insert(std::make_pair("port", ValueSpec(VALUETYPE_STRING,
			_("Set network port (UDP)"))));
	allowed_options->insert(std::make_pair("run-unittests", ValueSpec(VALUETYPE_FLAG,
			_("Run the unit tests and exit"))));
	allowed_options->insert(std::make_pair("map-dir", ValueSpec(VALUETYPE_STRING,
			_("Same as --world (deprecated)"))));
	allowed_options->insert(std::make_pair("world", ValueSpec(VALUETYPE_STRING,
			_("Set world path (implies local game) ('list' lists all)"))));
	allowed_options->insert(std::make_pair("worldname", ValueSpec(VALUETYPE_STRING,
			_("Set world by name (implies local game)"))));
	allowed_options->insert(std::make_pair("quiet", ValueSpec(VALUETYPE_FLAG,
			_("Print to console errors only"))));
	allowed_options->insert(std::make_pair("info", ValueSpec(VALUETYPE_FLAG,
			_("Print more information to console"))));
	allowed_options->insert(std::make_pair("verbose",  ValueSpec(VALUETYPE_FLAG,
			_("Print even more information to console"))));
	allowed_options->insert(std::make_pair("trace", ValueSpec(VALUETYPE_FLAG,
			_("Print enormous amounts of information to log and console"))));
	allowed_options->insert(std::make_pair("logfile", ValueSpec(VALUETYPE_STRING,
			_("Set logfile path ('' = no logging)"))));
	allowed_options->insert(std::make_pair("gameid", ValueSpec(VALUETYPE_STRING,
			_("Set gameid (\"--gameid list\" prints available ones)"))));
	allowed_options->insert(std::make_pair("migrate", ValueSpec(VALUETYPE_STRING,
			_("Migrate from current map backend to another (Only works when using minetestserver or with --server)"))));
	allowed_options->insert(std::make_pair("migrate-players", ValueSpec(VALUETYPE_STRING,
		_("Migrate from current players backend to another (Only works when using minetestserver or with --server)"))));
	allowed_options->insert(std::make_pair("terminal", ValueSpec(VALUETYPE_FLAG,
			_("Feature an interactive terminal (Only works when using minetestserver or with --server)"))));
#ifndef SERVER
	allowed_options->insert(std::make_pair("videomodes", ValueSpec(VALUETYPE_FLAG,
			_("Show available video modes"))));
	allowed_options->insert(std::make_pair("speedtests", ValueSpec(VALUETYPE_FLAG,
			_("Run speed tests"))));
	allowed_options->insert(std::make_pair("address", ValueSpec(VALUETYPE_STRING,
			_("Address to connect to. ('' = local game)"))));
	allowed_options->insert(std::make_pair("random-input", ValueSpec(VALUETYPE_FLAG,
			_("Enable random user input, for testing"))));
	allowed_options->insert(std::make_pair("server", ValueSpec(VALUETYPE_FLAG,
			_("Run dedicated server"))));
	allowed_options->insert(std::make_pair("name", ValueSpec(VALUETYPE_STRING,
			_("Set player name"))));
	allowed_options->insert(std::make_pair("password", ValueSpec(VALUETYPE_STRING,
			_("Set password"))));
	allowed_options->insert(std::make_pair("go", ValueSpec(VALUETYPE_FLAG,
			_("Disable main menu"))));
	allowed_options->insert(std::make_pair("console", ValueSpec(VALUETYPE_FLAG,
		_("Starts with the console (Windows only)"))));
#endif

}

static void print_help(const OptionList &allowed_options)
{
	std::cout << _("Allowed options:") << std::endl;
	print_allowed_options(allowed_options);
}

static void print_allowed_options(const OptionList &allowed_options)
{
	for (const auto &allowed_option : allowed_options) {
		std::ostringstream os1(std::ios::binary);
		os1 << "  --" << allowed_option.first;
		if (allowed_option.second.type != VALUETYPE_FLAG)
			os1 << _(" <value>");

		std::cout << padStringRight(os1.str(), 30);

		if (allowed_option.second.help)
			std::cout << allowed_option.second.help;

		std::cout << std::endl;
	}
}

static void print_version()
{
	std::cout << PROJECT_NAME_C " " << g_version_hash
	          << " (" << porting::getPlatformName() << ")" << std::endl;
#ifndef SERVER
	std::cout << "Using Irrlicht " << IRRLICHT_SDK_VERSION << std::endl;
#endif
	std::cout << "Build info: " << g_build_info << std::endl;
}

static void list_game_ids()
{
	std::set<std::string> gameids = getAvailableGameIds();
	for (const std::string &gameid : gameids)
		std::cout << gameid <<std::endl;
}

static void list_worlds()
{
	std::cout << _("Available worlds:") << std::endl;
	std::vector<WorldSpec> worldspecs = getAvailableWorlds();
	print_worldspecs(worldspecs, std::cout);
}

static void print_worldspecs(const std::vector<WorldSpec> &worldspecs,
							 std::ostream &os)
{
	for (const WorldSpec &worldspec : worldspecs) {
		std::string name = worldspec.name;
		std::string path = worldspec.path;
		if (name.find(' ') != std::string::npos)
			name = std::string("'").append(name).append("'");
		path = std::string("'").append(path).append("'");
		name = padStringRight(name, 14);
		os << "  " << name << " " << path << std::endl;
	}
}

static void print_modified_quicktune_values()
{
	bool header_printed = false;
	std::vector<std::string> names = getQuicktuneNames();

	for (const std::string &name : names) {
		QuicktuneValue val = getQuicktuneValue(name);
		if (!val.modified)
			continue;
		if (!header_printed) {
			dstream << "Modified quicktune values:" << std::endl;
			header_printed = true;
		}
		dstream << name << " = " << val.getString() << std::endl;
	}
}

static void setup_log_params(const Settings &cmd_args)
{
	// Quiet mode, print errors only
	if (cmd_args.getFlag("quiet")) {
		g_logger.removeOutput(&stderr_output);
		g_logger.addOutputMaxLevel(&stderr_output, LL_ERROR);
	}

	// If trace is enabled, enable logging of certain things
	if (cmd_args.getFlag("trace")) {
		dstream << _("Enabling trace level debug output") << std::endl;
		g_logger.setTraceEnabled(true);
		dout_con_ptr = &verbosestream; // This is somewhat old
		socket_enable_debug_output = true; // Sockets doesn't use log.h
	}

	// In certain cases, output info level on stderr
	if (cmd_args.getFlag("info") || cmd_args.getFlag("verbose") ||
			cmd_args.getFlag("trace") || cmd_args.getFlag("speedtests"))
		g_logger.addOutput(&stderr_output, LL_INFO);

	// In certain cases, output verbose level on stderr
	if (cmd_args.getFlag("verbose") || cmd_args.getFlag("trace"))
		g_logger.addOutput(&stderr_output, LL_VERBOSE);
}

static bool create_userdata_path()
{
	bool success;

#ifdef __ANDROID__
	if (!fs::PathExists(porting::path_user)) {
		success = fs::CreateDir(porting::path_user);
	} else {
		success = true;
	}
	porting::copyAssets();
#else
	// Create user data directory
	success = fs::CreateDir(porting::path_user);
#endif

	return success;
}

static bool init_common(const Settings &cmd_args, int argc, char *argv[])
{
	startup_message();
	set_default_settings(g_settings);

	// Initialize sockets
	sockets_init();
	atexit(sockets_cleanup);

	if (!read_config_file(cmd_args))
		return false;

	init_log_streams(cmd_args);

	// Initialize random seed
	srand(time(0));
	mysrand(time(0));

	// Initialize HTTP fetcher
	httpfetch_init(g_settings->getS32("curl_parallel_limit"));

	init_gettext(porting::path_locale.c_str(),
		g_settings->get("language"), argc, argv);

	return true;
}

static void startup_message()
{
	infostream << PROJECT_NAME << " " << _("with")
	           << " SER_FMT_VER_HIGHEST_READ="
               << (int)SER_FMT_VER_HIGHEST_READ << ", "
               << g_build_info << std::endl;
}

static bool read_config_file(const Settings &cmd_args)
{
	// Path of configuration file in use
	sanity_check(g_settings_path == "");	// Sanity check

	if (cmd_args.exists("config")) {
		bool r = g_settings->readConfigFile(cmd_args.get("config").c_str());
		if (!r) {
			errorstream << "Could not read configuration from \""
			            << cmd_args.get("config") << "\"" << std::endl;
			return false;
		}
		g_settings_path = cmd_args.get("config");
	} else {
		std::vector<std::string> filenames;
		filenames.push_back(porting::path_user + DIR_DELIM + "minetest.conf");
		// Legacy configuration file location
		filenames.push_back(porting::path_user +
				DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");

#if RUN_IN_PLACE
		// Try also from a lower level (to aid having the same configuration
		// for many RUN_IN_PLACE installs)
		filenames.push_back(porting::path_user +
				DIR_DELIM + ".." + DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
#endif

		for (const std::string &filename : filenames) {
			bool r = g_settings->readConfigFile(filename.c_str());
			if (r) {
				g_settings_path = filename;
				break;
			}
		}

		// If no path found, use the first one (menu creates the file)
		if (g_settings_path.empty())
			g_settings_path = filenames[0];
	}

	return true;
}

static void init_log_streams(const Settings &cmd_args)
{
#if RUN_IN_PLACE
	std::string log_filename = DEBUGFILE;
#else
	std::string log_filename = porting::path_user + DIR_DELIM + DEBUGFILE;
#endif
	if (cmd_args.exists("logfile"))
		log_filename = cmd_args.get("logfile");

	g_logger.removeOutput(&file_log_output);
	std::string conf_loglev = g_settings->get("debug_log_level");

	// Old integer format
	if (std::isdigit(conf_loglev[0])) {
		warningstream << "Deprecated use of debug_log_level with an "
			"integer value; please update your configuration." << std::endl;
		static const char *lev_name[] =
			{"", "error", "action", "info", "verbose"};
		int lev_i = atoi(conf_loglev.c_str());
		if (lev_i < 0 || lev_i >= (int)ARRLEN(lev_name)) {
			warningstream << "Supplied invalid debug_log_level!"
				"  Assuming action level." << std::endl;
			lev_i = 2;
		}
		conf_loglev = lev_name[lev_i];
	}

	if (log_filename.empty() || conf_loglev.empty())  // No logging
		return;

	LogLevel log_level = Logger::stringToLevel(conf_loglev);
	if (log_level == LL_MAX) {
		warningstream << "Supplied unrecognized debug_log_level; "
			"using maximum." << std::endl;
	}

	verbosestream << "log_filename = " << log_filename << std::endl;

	file_log_output.open(log_filename);
	g_logger.addOutputMaxLevel(&file_log_output, log_level);
}

static bool game_configure(GameParams *game_params, const Settings &cmd_args)
{
	game_configure_port(game_params, cmd_args);

	if (!game_configure_world(game_params, cmd_args)) {
		errorstream << "No world path specified or found." << std::endl;
		return false;
	}

	game_configure_subgame(game_params, cmd_args);

	return true;
}

static void game_configure_port(GameParams *game_params, const Settings &cmd_args)
{
	if (cmd_args.exists("port"))
		game_params->socket_port = cmd_args.getU16("port");
	else
		game_params->socket_port = g_settings->getU16("port");

	if (game_params->socket_port == 0)
		game_params->socket_port = DEFAULT_SERVER_PORT;
}

static bool game_configure_world(GameParams *game_params, const Settings &cmd_args)
{
	if (get_world_from_cmdline(game_params, cmd_args))
		return true;

	if (get_world_from_config(game_params, cmd_args))
		return true;

	return auto_select_world(game_params);
}

static bool get_world_from_cmdline(GameParams *game_params, const Settings &cmd_args)
{
	std::string commanded_world;

	// World name
	std::string commanded_worldname;
	if (cmd_args.exists("worldname"))
		commanded_worldname = cmd_args.get("worldname");

	// If a world name was specified, convert it to a path
	if (!commanded_worldname.empty()) {
		// Get information about available worlds
		std::vector<WorldSpec> worldspecs = getAvailableWorlds();
		bool found = false;
		for (const WorldSpec &worldspec : worldspecs) {
			std::string name = worldspec.name;
			if (name == commanded_worldname) {
				dstream << _("Using world specified by --worldname on the "
					"command line") << std::endl;
				commanded_world = worldspec.path;
				found = true;
				break;
			}
		}
		if (!found) {
			dstream << _("World") << " '" << commanded_worldname
			        << _("' not available. Available worlds:") << std::endl;
			print_worldspecs(worldspecs, dstream);
			return false;
		}

		game_params->world_path = get_clean_world_path(commanded_world);
		return !commanded_world.empty();
	}

	if (cmd_args.exists("world"))
		commanded_world = cmd_args.get("world");
	else if (cmd_args.exists("map-dir"))
		commanded_world = cmd_args.get("map-dir");
	else if (cmd_args.exists("nonopt0")) // First nameless argument
		commanded_world = cmd_args.get("nonopt0");

	game_params->world_path = get_clean_world_path(commanded_world);
	return !commanded_world.empty();
}

static bool get_world_from_config(GameParams *game_params, const Settings &cmd_args)
{
	// World directory
	std::string commanded_world;

	if (g_settings->exists("map-dir"))
		commanded_world = g_settings->get("map-dir");

	game_params->world_path = get_clean_world_path(commanded_world);

	return !commanded_world.empty();
}

static bool auto_select_world(GameParams *game_params)
{
	// No world was specified; try to select it automatically
	// Get information about available worlds

	verbosestream << _("Determining world path") << std::endl;

	std::vector<WorldSpec> worldspecs = getAvailableWorlds();
	std::string world_path;

	// If there is only a single world, use it
	if (worldspecs.size() == 1) {
		world_path = worldspecs[0].path;
		dstream <<_("Automatically selecting world at") << " ["
		        << world_path << "]" << std::endl;
	// If there are multiple worlds, list them
	} else if (worldspecs.size() > 1 && game_params->is_dedicated_server) {
		std::cerr << _("Multiple worlds are available.") << std::endl;
		std::cerr << _("Please select one using --worldname <name>"
				" or --world <path>") << std::endl;
		print_worldspecs(worldspecs, std::cerr);
		return false;
	// If there are no worlds, automatically create a new one
	} else {
		// This is the ultimate default world path
		world_path = porting::path_user + DIR_DELIM + "worlds" +
				DIR_DELIM + "world";
		infostream << "Creating default world at ["
		           << world_path << "]" << std::endl;
	}

	assert(world_path != "");	// Post-condition
	game_params->world_path = world_path;
	return true;
}

static std::string get_clean_world_path(const std::string &path)
{
	const std::string worldmt = "world.mt";
	std::string clean_path;

	if (path.size() > worldmt.size()
			&& path.substr(path.size() - worldmt.size()) == worldmt) {
		dstream << _("Supplied world.mt file - stripping it off.") << std::endl;
		clean_path = path.substr(0, path.size() - worldmt.size());
	} else {
		clean_path = path;
	}
	return path;
}


static bool game_configure_subgame(GameParams *game_params, const Settings &cmd_args)
{
	bool success;

	success = get_game_from_cmdline(game_params, cmd_args);
	if (!success)
		success = determine_subgame(game_params);

	return success;
}

static bool get_game_from_cmdline(GameParams *game_params, const Settings &cmd_args)
{
	SubgameSpec commanded_gamespec;

	if (cmd_args.exists("gameid")) {
		std::string gameid = cmd_args.get("gameid");
		commanded_gamespec = findSubgame(gameid);
		if (!commanded_gamespec.isValid()) {
			errorstream << "Game \"" << gameid << "\" not found" << std::endl;
			return false;
		}
		dstream << _("Using game specified by --gameid on the command line")
		        << std::endl;
		game_params->game_spec = commanded_gamespec;
		return true;
	}

	return false;
}

static bool determine_subgame(GameParams *game_params)
{
	SubgameSpec gamespec;

	assert(game_params->world_path != "");	// Pre-condition

	verbosestream << _("Determining gameid/gamespec") << std::endl;
	// If world doesn't exist
	if (!game_params->world_path.empty()
		&& !getWorldExists(game_params->world_path)) {
		// Try to take gamespec from command line
		if (game_params->game_spec.isValid()) {
			gamespec = game_params->game_spec;
			infostream << "Using commanded gameid [" << gamespec.id << "]" << std::endl;
		} else { // Otherwise we will be using "minetest"
			gamespec = findSubgame(g_settings->get("default_game"));
			infostream << "Using default gameid [" << gamespec.id << "]" << std::endl;
			if (!gamespec.isValid()) {
				errorstream << "Subgame specified in default_game ["
				            << g_settings->get("default_game")