aboutsummaryrefslogtreecommitdiff
path: root/textures/base/pack/search.png
blob: aace8044ac235bcf5935ed8a72c4cb136746a916 (plain)
ofshex dumpascii
0000 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 80 00 00 00 80 08 06 00 00 00 c3 3e 61 .PNG........IHDR..............>a
0020 cb 00 00 07 3b 49 44 41 54 78 da ed 9d 4b 6c 15 65 18 86 9f bf 02 da 5a 6e 21 85 53 42 44 89 f4 ....;IDATx...Kl.e......Zn!.SBD..
0040 a2 10 21 20 21 71 61 d2 44 5c a1 24 ac dc b2 20 d1 45 37 2e 48 74 61 58 b8 80 c4 40 a2 86 c4 05 ..!.!qa.D\.$.....E7.HtaX...@....
0060 2b a3 18 83 01 1b 52 08 49 77 24 0d 86 80 41 a5 05 44 6e a1 2d d0 d4 42 69 53 90 7e 2e e6 43 40 +.....R.Iw$...A..Dn.-..BiS.~..C@
0080 98 99 43 2f a7 33 e7 bc cf a6 8b 73 7a e6 fd bf ef 9d 7f fe fb 80 10 42 08 21 84 10 42 08 21 84 ..C/.3.....sz..........B.!..B.!.
00a0 10 42 08 21 84 10 42 08 21 84 10 42 08 21 84 10 42 08 21 f2 4a c8 b3 78 33 ab 06 1a 80 26 e0 15 .B.!..B.!..B.!..B.!.J..x3....&..
00c0 60 11 30 17 98 05 cc f0 af fd 03 dc 05 06 81 3e e0 2f a0 0b 38 1b 42 18 91 01 f2 97 f4 85 40 0b `.0............>./..8.B.......@.
00e0 b0 1e 58 00 cc 07 66 7b e2 e7 02 73 80 6a 37 01 9e fc 11 e0 96 9b 60 10 b8 0d 0c 00 fd 40 27 d0 ..X...f{...s.j7.......`......@'.
0100 11 42 b8 2e 03 64 3b f1 6f 01 ef 03 4b 80 97 fc 6f 1d 50 33 ce 9f 1c 06 6e 00 57 81 cb fe f7 60 .B...d;.o...K...o.P3....n.W....`
0120 08 e1 98 0c 90 ad c4 b7 00 9b 81 65 c0 ab 40 01 a8 9d e4 cb 0c 01 bd c0 79 e0 02 b0 3f 84 d0 21 ...........e..@.........y...?..!
0140 03 4c 6f e2 1b 80 0f 81 d7 80 46 4f fc f3 53 7c d9 51 37 42 37 f0 07 b0 27 84 70 56 06 28 7d f2 .Lo.......FO..S|.Q7B7...'.pV.(}.
0160 b7 02 ef 00 ab 80 c5 13 a8 e6 c7 cb 30 70 0d 38 05 1c 0d 21 7c 23 03 94 26 f1 75 c0 a7 c0 9b 7e ............0p.8...!|#..&.u....~
0180 d7 2f 98 66 49 fd 5e 1b fc 02 7c 1e 42 b8 21 03 4c 5d f2 57 02 db 80 35 de c8 ab c9 88 b4 61 6f ./.fI.^...|.B.!.L].W...5......ao
01a0 24 9e 00 76 84 10 4e 97 93 01 66 64 a8 85 ff b1 df f9 8b 81 aa 22 ff 75 cc bb 77 7d fe ec be ed $..v..N...fd.........".u..w}....
01c0 dd be 31 c0 1e 31 79 95 77 0b 67 7b 5b 62 91 77 17 8b b9 4e 8d 8f 35 d4 02 35 66 f6 45 39 f5 14 ..1..1y.w.g{[b.w...N..5..5f.E9..
01e0 42 46 92 bf cd 93 5f 28 f2 df 06 81 1e bf 33 7b 80 4b c0 49 e0 ac 1b 61 08 b8 e7 df 9d e9 c9 2b BF...._(......3{.K.I...a.......+
0200 78 22 57 03 4b 81 7a af 69 ea 7d fc a0 18 7a fd 71 b0 a3 d2 ba 8b 53 56 ed 9b d9 4f 66 d6 63 c5 x"W.K.z.i.}...z.q.....SV...Of.c.
0220 31 60 66 a7 cd 6c 9f 99 6d 37 b3 b5 5d 5d 5d cf 6c e2 ae ae ae 60 66 6b fd 37 f6 f9 6f 0e 14 a9 1`f..l..m7..]]].l....`fk.7..o...
0240 a1 c7 35 af 54 06 27 d8 e0 33 b3 6f cd ec 4a 11 41 1f 36 b3 f3 66 76 c0 cc 3e 31 b3 c2 24 ea 28 ..5.T.'..3.o..J.A.6..fv..>1..$.(
0260 f8 6f 1e f0 6b 0c 17 a1 e7 8a 6b af 53 26 c7 1f f8 dd 66 76 c6 cc ee a7 04 bb df cc 3a cd 6c 8f .o..k.....k.S&....fv........:.l.
0280 99 35 4f a1 9e 66 bf 46 a7 5f 33 89 fb ae 7d b7 32 39 ce 7e be 99 1d 33 b3 3b 29 81 ee 33 b3 a3 .5O..f.F._3...}.29.~...3.;)..3..
02a0 66 d6 da de de 3e e5 ed 95 f6 f6 f6 60 66 ad 7e cd be 14 6d 77 bc 0c 5b 95 d1 67 4b 7e 83 99 fd f....>......`f.~...mw..[..gK~...
02c0 68 66 37 53 02 dc 6b 66 87 cc 6c d3 34 68 dc e4 d7 ee 4d d1 78 d3 cb d2 a0 cc 16 1f dc 5d 66 76 hf7S..kf..l.4h....M.x........]fv
02e0 ae 88 3b ff 90 99 6d 98 46 9d 1b 5c 43 5a 4d 70 ce cc 76 29 b3 c5 05 b5 c5 cc 8e a4 54 fd fd 5e ..;...m.F..\CZMp..v)........T..^
0300 05 6f ca 80 de 4d ae a5 3f e5 51 70 c4 27 ad 72 47 55 89 af b7 99 68 88 37 6e 94 6f 04 38 07 b4 .o...M..?.Qp.'.rGU....h.7n.o.8..
0320 85 10 0e 4c fb 20 49 a4 a1 cd 35 8d 24 0c 14 35 7a d9 64 80 94 01 9f 65 29 83 3d d7 80 93 87 0f ...L..I...5.$..5z.d....e).=.....
0340 1f fe 32 2b 01 72 2d 27 5d 5b 1c 05 60 99 97 51 c4 18 60 67 ca b3 7f c0 fb e2 cd 19 d4 de ec da ..2+.r-'][..`..Q..`g............
0360 06 52 da 02 3b 55 03 3c 3d 80 0b 89 56 f0 24 dd fd 57 81 e3 21 84 33 59 0b 92 6b 3a ee 1a 93 6a .R..;U.<=...V.$..W..!.3Y..k:...j
0380 81 25 5e 56 19 e0 7f b4 10 8d bb c7 ad e4 19 04 7e 07 f6 66 38 56 7b 5d e3 60 cc e7 b5 5e c6 16 .%^V............~..f8V{].`...^..
03a0 19 e0 49 d6 7b 0d 10 47 0f d0 1d 42 e8 cd 6a a0 5c 5b b7 6b 8d 63 89 97 55 06 78 a4 fa af 26 5a ..I.{..G...B..j.\[.k.c..U.x...&Z
03c0 d8 11 37 6e 3e 46 34 ab d7 96 83 78 b5 b9 d6 b1 98 cf eb 80 05 5e 66 19 c0 69 26 5a ba 1d d7 f5 ..7n>F4....x.........^f..i&Z....
03e0 bb 05 f4 74 77 77 9f c8 7a b0 5c 63 8f 6b 8e eb 12 ce f7 32 cb 00 ce 2a a2 85 18 71 f4 01 97 9a ...tww..z.\c.k.....2...*...q....
0400 9a 9a 2c eb c1 72 8d 97 5c 73 1c b3 bd cc 32 80 f3 06 c9 0b 2e 7a bd 9f 9d 17 4e ba e6 38 e6 7a ..,..r..\s....2......z....N..8.z
0420 99 65 80 07 37 4e 8a 01 6e 13 ad e4 c9 0b 67 5d 73 92 01 9a 64 80 87 bc 4c b4 fe 2e 8e bb 29 77 .e..7N..n.....g]s...d...L.....)w
0440 54 d6 e8 75 cd 71 cc f1 32 e7 82 52 2c 0a ad 03 5e 48 f8 7c 8c 68 0d 5f 5e 18 4a e8 05 40 b4 2f T..u.q..2..R,...^H.|.h._^.J..@./
0460 b1 4e 06 78 bc 65 9c 74 1d e3 e1 02 ce 3c 70 8f 87 2b 8e 9f c6 2c 4a 3f c9 96 e9 47 80 a8 70 03 .N.x.e.t.....<p..+...,J?...G..p.
0480 0c a7 3c 33 03 d1 d2 ed bc 30 93 e4 e5 f4 77 bd cc 32 80 73 83 f8 b9 f4 07 1a 6a 73 64 80 da 94 ..<3.....0....w..2.s......jsd...
04a0 b8 8d 78 99 65 00 e7 22 f1 23 67 0f 9e 99 85 1c 19 a0 c0 c3 c3 27 9e c6 2d 2f b3 0c e0 74 11 3f ..x.e..".#g..........'..-/...t.?
04c0 83 06 d1 c8 59 9e 16 55 36 90 3c b2 39 e8 65 96 01 9c 5f 53 0c 50 20 da ae 95 17 56 a7 d4 58 83 ....Y..U6.<.9.e..._S.P.....V..X.
04e0 5e 66 19 c0 39 45 f2 c8 d9 22 60 e9 78 b6 78 95 1a d7 b8 d4 35 c7 71 db cb 2c 03 38 67 88 0e 64 ^f..9E..."`.x.x.....5.q..,.8g..d
0500 8a 6b 19 cf 01 ea 1b 1b 1b d7 64 3d 58 ae b1 9e f8 91 cd 61 2f eb 19 19 e0 41 1f 2f 3a 8a ad 3f .k........d=X......a/....A./:..?
0520 a1 65 5c 45 b4 92 66 63 0e e2 b5 d1 b5 56 25 f4 78 fa f3 74 fc 5c a9 06 82 3a 49 5e 4f 57 0f 34 .e\E..fc.....V%.x..t.\...:I^OW.4
0540 4e e6 a6 cf c9 c6 b5 35 ba d6 38 ae 7a 59 91 01 1e a7 83 68 25 4d dc 98 ff 5c e0 75 60 4b 86 63 N......5..8.zY.....h%M...\.u`K.c
0560 b5 c5 35 c6 cd 6c 0e 79 19 3b 64 80 27 1f 03 d7 fd ee 48 9a f5 5b 02 ac cb ea b2 70 60 1d c9 eb ..5..l.y.;d.'.....H..[.....p`...
0580 1a 7b 81 ab 79 3b 70 b2 94 73 01 07 89 ce e1 1b 8d f9 7c 1e b0 02 28 c9 4e e0 62 71 2d ad ae 6d .{..y;p..s........|...(.N.bq-..m
05a0 5e cc d7 ee 02 7f 02 3f 23 12 ef a4 af cd ec 62 ca 41 10 9d 66 d6 9a 21 cd ad ae 29 e9 e0 88 41 ^......?#......b.A..f..!...)...A
05c0 33 fb 21 8f 39 29 f5 6c e0 7e a2 a5 d5 71 5d c2 6a 60 39 b0 31 2b 9b 43 bd e5 bf dc b5 25 f1 a2 3.!.9).l.~...q].j`9.1+.C.....%..
05e0 99 35 ea 36 4f 0f 6a b9 6d 0f 37 33 1b f5 bf df c9 04 e9 81 2d a7 03 22 9e 86 4c 50 44 80 cb e1 .5.6O.j.m.73........-.."..LPD...
0600 88 18 99 60 82 26 c8 f3 21 51 65 63 82 30 8d 06 a8 03 76 01 6f a7 f4 af 21 5a 64 71 0d f8 8d 68 ...`.&..!Qec.0....v.o...!Zdq...h
0620 97 ee de c9 da 47 e8 23 7c 5b bc 9f bf 82 e8 a4 d2 c9 da da d5 16 42 78 4f b7 7b 7c f0 f3 78 50 .....G.#|[............BxO.{|..xP
0640 64 59 d5 04 3a 2a f6 d9 8e 8a 1d 2f df 03 db 43 08 dd 32 40 bc 09 b2 7a 58 74 59 9b 40 c7 c5 97 dY..:*...../...C..2@...zXtY.@...
0660 96 cc 99 40 2f 8c 28 3d 99 6a 18 ea 95 31 15 5e 13 e8 a5 51 8f 33 0a dc 24 9a ab 98 4d b4 c7 ef ...@/.(=.j...1.^...Q.3..$...M...
0680 b9 72 36 81 5e 1b 17 f1 e8 6b e3 2e 13 bd 14 a2 05 f8 a0 dc 6b 02 bd 38 32 e6 c5 91 de 77 ff ac .r6.^....k..........k..82....w..
06a0 dc 4d a0 57 c7 26 bc 3a b6 84 26 98 b6 86 a1 5e 1e 9d 7e 8d 8a a8 09 44 8a 09 7c 48 77 aa d1 2c .M.W.&.:..&....^..~....D..|Hw..,
06c0 a2 4c 20 13 c8 04 32 81 4c 60 66 5a 61 2c 13 a8 26 90 09 64 02 99 40 26 90 09 64 02 99 40 0d 43 .L....2.L`fZa,..&..d..@&..d..@.C
06e0 99 40 35 81 4c 20 13 c8 04 32 81 4c f0 1f 5f 99 d9 bc 89 68 d5 59 c1 53 80 cf e8 6d 27 9a e1 9b .@5.L....2.L.._....h.Y.S...m'...
0700 4a 2e 84 10 fe 56 c4 2b af 26 b8 e2 7b 18 6b 14 e5 ca 33 81 92 5f c1 26 50 f2 2b d8 04 4a 7e 85 J....V.+.&..{.k...3.._.&P.+..J~.
0720 9b e0 23 45 b0 32 4d a0 3b bf 82 4d a0 e4 57 b0 09 94 fc 0a 36 81 92 5f c1 26 50 f2 2b d8 04 4a ..#E.2M.;..M..W.....6.._.&P.+..J
0740 7e 05 9b 60 99 99 bd ab e4 0b 21 84 10 42 08 21 84 10 42 08 21 84 10 42 08 21 84 10 62 a2 fc 0b ~..`......!..B.!..B.!..B.!..b...
0760 f7 5f e3 a4 00 70 65 1e 00 00 00 00 49 45 4e 44 ae 42 60 82 ._...pe.....IEND.B`.
a> 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 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
/*
Minetest
Copyright (C) 2013 sapier

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 "lua_api/l_mainmenu.h"
#include "lua_api/l_internal.h"
#include "common/c_content.h"
#include "cpp_api/s_async.h"
#include "gui/guiEngine.h"
#include "gui/guiMainMenu.h"
#include "gui/guiKeyChangeMenu.h"
#include "gui/guiPathSelectMenu.h"
#include "version.h"
#include "porting.h"
#include "filesys.h"
#include "convert_json.h"
#include "content/packages.h"
#include "content/content.h"
#include "content/subgames.h"
#include "serverlist.h"
#include "mapgen/mapgen.h"
#include "settings.h"

#include <IFileArchive.h>
#include <IFileSystem.h>
#include "client/renderingengine.h"
#include "network/networkprotocol.h"


/******************************************************************************/
std::string ModApiMainMenu::getTextData(lua_State *L, std::string name)
{
	lua_getglobal(L, "gamedata");

	lua_getfield(L, -1, name.c_str());

	if(lua_isnil(L, -1))
		return "";

	return luaL_checkstring(L, -1);
}

/******************************************************************************/
int ModApiMainMenu::getIntegerData(lua_State *L, std::string name,bool& valid)
{
	lua_getglobal(L, "gamedata");

	lua_getfield(L, -1, name.c_str());

	if(lua_isnil(L, -1)) {
		valid = false;
		return -1;
		}

	valid = true;
	return luaL_checkinteger(L, -1);
}

/******************************************************************************/
int ModApiMainMenu::getBoolData(lua_State *L, std::string name,bool& valid)
{
	lua_getglobal(L, "gamedata");

	lua_getfield(L, -1, name.c_str());

	if(lua_isnil(L, -1)) {
		valid = false;
		return false;
		}

	valid = true;
	return readParam<bool>(L, -1);
}

/******************************************************************************/
int ModApiMainMenu::l_update_formspec(lua_State *L)
{
	GUIEngine* engine = getGuiEngine(L);
	sanity_check(engine != NULL);

	if (engine->m_startgame)
		return 0;

	//read formspec
	std::string formspec(luaL_checkstring(L, 1));

	if (engine->m_formspecgui != 0) {
		engine->m_formspecgui->setForm(formspec);
	}

	return 0;
}

/******************************************************************************/
int ModApiMainMenu::l_start(lua_State *L)
{
	GUIEngine* engine = getGuiEngine(L);
	sanity_check(engine != NULL);

	//update c++ gamedata from lua table

	bool valid = false;

	MainMenuData *data = engine->m_data;

	data->selected_world = getIntegerData(L, "selected_world",valid) -1;
	data->simple_singleplayer_mode = getBoolData(L,"singleplayer",valid);
	data->do_reconnect = getBoolData(L, "do_reconnect", valid);
	if (!data->do_reconnect) {
		data->name     = getTextData(L,"playername");
		data->password = getTextData(L,"password");
		data->address  = getTextData(L,"address");
		data->port     = getTextData(L,"port");
	}
	data->serverdescription = getTextData(L,"serverdescription");
	data->servername        = getTextData(L,"servername");

	//close menu next time
	engine->m_startgame = true;
	return 0;
}

/******************************************************************************/
int ModApiMainMenu::l_close(lua_State *L)
{
	GUIEngine* engine = getGuiEngine(L);
	sanity_check(engine != NULL);

	engine->m_kill = true;
	return 0;
}

/******************************************************************************/
int ModApiMainMenu::l_set_background(lua_State *L)
{
	GUIEngine* engine = getGuiEngine(L);
	sanity_check(engine != NULL);

	std::string backgroundlevel(luaL_checkstring(L, 1));
	std::string texturename(luaL_checkstring(L, 2));

	bool tile_image = false;
	bool retval     = false;
	unsigned int minsize = 16;

	if (!lua_isnone(L, 3)) {
		tile_image = readParam<bool>(L, 3);
	}

	if (!lua_isnone(L, 4)) {
		minsize = lua_tonumber(L, 4);
	}

	if (backgroundlevel == "background") {
		retval |= engine->setTexture(TEX_LAYER_BACKGROUND, texturename,
				tile_image, minsize);
	}

	if (backgroundlevel == "overlay") {
		retval |= engine->setTexture(TEX_LAYER_OVERLAY, texturename,
				tile_image, minsize);
	}

	if (backgroundlevel == "header") {
		retval |= engine->setTexture(TEX_LAYER_HEADER,  texturename,
				tile_image, minsize);
	}

	if (backgroundlevel == "footer") {
		retval |= engine->setTexture(TEX_LAYER_FOOTER, texturename,
				tile_image, minsize);
	}

	lua_pushboolean(L,retval);
	return 1;
}

/******************************************************************************/
int ModApiMainMenu::l_set_clouds(lua_State *L)
{
	GUIEngine* engine = getGuiEngine(L);
	sanity_check(engine != NULL);

	bool value = readParam<bool>(L,1);

	engine->m_clouds_enabled = value;

	return 0;
}

/******************************************************************************/
int ModApiMainMenu::l_get_textlist_index(lua_State *L)
{
	// get_table_index accepts both tables and textlists
	return l_get_table_index(L);
}

/******************************************************************************/
int ModApiMainMenu::l_get_table_index(lua_State *L)
{
	GUIEngine* engine = getGuiEngine(L);
	sanity_check(engine != NULL);

	std::string tablename(luaL_checkstring(L, 1));
	GUITable *table = engine->m_menu->getTable(tablename);
	s32 selection = table ? table->getSelected() : 0;

	if (selection >= 1)
		lua_pushinteger(L, selection);
	else
		lua_pushnil(L);
	return 1;
}

/******************************************************************************/
int ModApiMainMenu::l_get_worlds(lua_State *L)
{
	std::vector<WorldSpec> worlds = getAvailableWorlds();

	lua_newtable(L);
	int top = lua_gettop(L);
	unsigned int index = 1;

	for (const WorldSpec &world : worlds) {
		lua_pushnumber(L,index);

		lua_newtable(L);
		int top_lvl2 = lua_gettop(L);

		lua_pushstring(L,"path");
		lua_pushstring(L, world.path.c_str());
		lua_settable(L, top_lvl2);

		lua_pushstring(L,"name");
		lua_pushstring(L, world.name.c_str());
		lua_settable(L, top_lvl2);

		lua_pushstring(L,"gameid");
		lua_pushstring(L, world.gameid.c_str());
		lua_settable(L, top_lvl2);

		lua_settable(L, top);
		index++;
	}
	return 1;
}

/******************************************************************************/
int ModApiMainMenu::l_get_favorites(lua_State *L)
{
	std::string listtype = "local";

	if (!lua_isnone(L,1)) {
		listtype = luaL_checkstring(L,1);
	}

	std::vector<ServerListSpec> servers;

	if(listtype == "online") {
		servers = ServerList::getOnline();
	} else {
		servers = ServerList::getLocal();
	}

	lua_newtable(L);
	int top = lua_gettop(L);
	unsigned int index = 1;

	for (const Json::Value &server : servers) {

		lua_pushnumber(L,index);

		lua_newtable(L);
		int top_lvl2 = lua_gettop(L);

		if (!server["clients"].asString().empty()) {
			std::string clients_raw = server["clients"].asString();
			char* endptr = 0;
			int numbervalue = strtol(clients_raw.c_str(),&endptr,10);

			if ((!clients_raw.empty()) && (*endptr == 0)) {
				lua_pushstring(L,"clients");
				lua_pushnumber(L,numbervalue);
				lua_settable(L, top_lvl2);
			}
		}

		if (!server["clients_max"].asString().empty()) {

			std::string clients_max_raw = server["clients_max"].asString();
			char* endptr = 0;
			int numbervalue = strtol(clients_max_raw.c_str(),&endptr,10);

			if ((!clients_max_raw.empty()) && (*endptr == 0)) {
				lua_pushstring(L,"clients_max");
				lua_pushnumber(L,numbervalue);
				lua_settable(L, top_lvl2);
			}
		}

		if (!server["version"].asString().empty()) {
			lua_pushstring(L,"version");
			std::string topush = server["version"].asString();
			lua_pushstring(L,topush.c_str());
			lua_settable(L, top_lvl2);
		}

		if (!server["proto_min"].asString().empty()) {
			lua_pushstring(L,"proto_min");
			lua_pushinteger(L, server["proto_min"].asInt());
			lua_settable(L, top_lvl2);
		}

		if (!server["proto_max"].asString().empty()) {
			lua_pushstring(L,"proto_max");
			lua_pushinteger(L, server["proto_max"].asInt());
			lua_settable(L, top_lvl2);
		}

		if (!server["password"].asString().empty()) {
			lua_pushstring(L,"password");
			lua_pushboolean(L, server["password"].asBool());
			lua_settable(L, top_lvl2);
		}

		if (!server["creative"].asString().empty()) {
			lua_pushstring(L,"creative");
			lua_pushboolean(L, server["creative"].asBool());
			lua_settable(L, top_lvl2);
		}

		if (!server["damage"].asString().empty()) {
			lua_pushstring(L,"damage");
			lua_pushboolean(L, server["damage"].asBool());
			lua_settable(L, top_lvl2);
		}

		if (!server["pvp"].asString().empty()) {
			lua_pushstring(L,"pvp");
			lua_pushboolean(L, server["pvp"].asBool());
			lua_settable(L, top_lvl2);
		}

		if (!server["description"].asString().empty()) {
			lua_pushstring(L,"description");
			std::string topush = server["description"].asString();
			lua_pushstring(L,topush.c_str());
			lua_settable(L, top_lvl2);
		}

		if (!server["name"].asString().empty()) {
			lua_pushstring(L,"name");
			std::string topush = server["name"].asString();
			lua_pushstring(L,topush.c_str());
			lua_settable(L, top_lvl2);
		}

		if (!server["address"].asString().empty()) {
			lua_pushstring(L,"address");
			std::string topush = server["address"].asString();
			lua_pushstring(L,topush.c_str());
			lua_settable(L, top_lvl2);
		}

		if (!server["port"].asString().empty()) {
			lua_pushstring(L,"port");
			std::string topush = server["port"].asString();
			lua_pushstring(L,topush.c_str());
			lua_settable(L, top_lvl2);
		}

		if (server.isMember("ping")) {
			float ping = server["ping"].asFloat();
			lua_pushstring(L, "ping");
			lua_pushnumber(L, ping);
			lua_settable(L, top_lvl2);
		}

		lua_settable(L, top);
		index++;
	}
	return 1;
}

/******************************************************************************/
int ModApiMainMenu::l_delete_favorite(lua_State *L)
{
	std::vector<ServerListSpec> servers;

	std::string listtype = "local";

	if (!lua_isnone(L,2)) {
		listtype = luaL_checkstring(L,2);
	}

	if ((listtype != "local") &&
		(listtype != "online"))
		return 0;


	if(listtype == "online") {
		servers = ServerList::getOnline();
	} else {
		servers = ServerList::getLocal();
	}

	int fav_idx	= luaL_checkinteger(L,1) -1;

	if ((fav_idx >= 0) &&
			(fav_idx < (int) servers.size())) {

		ServerList::deleteEntry(servers[fav_idx]);
	}

	return 0;
}

/******************************************************************************/
int ModApiMainMenu::l_get_games(lua_State *L)
{
	std::vector<SubgameSpec> games = getAvailableGames();

	lua_newtable(L);
	int top = lua_gettop(L);
	unsigned int index = 1;

	for (const SubgameSpec &game : games) {
		lua_pushnumber(L, index);
		lua_newtable(L);
		int top_lvl2 = lua_gettop(L);

		lua_pushstring(L,  "id");
		lua_pushstring(L,  game.id.c_str());
		lua_settable(L,    top_lvl2);

		lua_pushstring(L,  "path");
		lua_pushstring(L,  game.path.c_str());
		lua_settable(L,    top_lvl2);

		lua_pushstring(L,  "type");
		lua_pushstring(L,  "game");
		lua_settable(L,    top_lvl2);

		lua_pushstring(L,  "gamemods_path");
		lua_pushstring(L,  game.gamemods_path.c_str());
		lua_settable(L,    top_lvl2);

		lua_pushstring(L,  "name");
		lua_pushstring(L,  game.name.c_str());
		lua_settable(L,    top_lvl2);

		lua_pushstring(L,  "author");
		lua_pushstring(L,  game.author.c_str());
		lua_settable(L,    top_lvl2);

		lua_pushstring(L,  "release");
		lua_pushinteger(L, game.release);
		lua_settable(L,    top_lvl2);

		lua_pushstring(L,  "menuicon_path");
		lua_pushstring(L,  game.menuicon_path.c_str());
		lua_settable(L,    top_lvl2);

		lua_pushstring(L, "addon_mods_paths");
		lua_newtable(L);
		int table2 = lua_gettop(L);
		int internal_index = 1;
		for (const std::string &addon_mods_path : game.addon_mods_paths) {
			lua_pushnumber(L, internal_index);
			lua_pushstring(L, addon_mods_path.c_str());
			lua_settable(L,   table2);
			internal_index++;
		}
		lua_settable(L, top_lvl2);
		lua_settable(L, top);
		index++;
	}
	return 1;
}

/******************************************************************************/
int ModApiMainMenu::l_get_content_info(lua_State *L)
{
	std::string path = luaL_checkstring(L, 1);

	ContentSpec spec;
	spec.path = path;
	parseContentInfo(spec);

	lua_newtable(L);

	lua_pushstring(L, spec.name.c_str());
	lua_setfield(L, -2, "name");

	lua_pushstring(L, spec.type.c_str());
	lua_setfield(L, -2, "type");

	lua_pushstring(L, spec.author.c_str());
	lua_setfield(L, -2, "author");

	lua_pushinteger(L, spec.release);
	lua_setfield(L, -2, "release");

	lua_pushstring(L, spec.desc.c_str());
	lua_setfield(L, -2, "description");

	lua_pushstring(L, spec.path.c_str());
	lua_setfield(L, -2, "path");

	if (spec.type == "mod") {
		ModSpec spec;
		spec.path = path;
		parseModContents(spec);

		// Dependencies
		lua_newtable(L);
		int i = 1;
		for (const auto &dep : spec.depends) {
			lua_pushstring(L, dep.c_str());
			lua_rawseti(L, -2, i++);
		}
		lua_setfield(L, -2, "depends");

		// Optional Dependencies
		lua_newtable(L);
		i = 1;
		for (const auto &dep : spec.optdepends) {
			lua_pushstring(L, dep.c_str());
			lua_rawseti(L, -2, i++);
		}
		lua_setfield(L, -2, "optional_depends");
	}

	return 1;
}

/******************************************************************************/
int ModApiMainMenu::l_show_keys_menu(lua_State *L)
{
	GUIEngine* engine = getGuiEngine(L);
	sanity_check(engine != NULL);

	GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(RenderingEngine::get_gui_env(),
								engine->m_parent,
								-1,
								engine->m_menumanager);
	kmenu->drop();
	return 0;
}

/******************************************************************************/
int ModApiMainMenu::l_create_world(lua_State *L)
{
	const char *name	= luaL_checkstring(L, 1);
	int gameidx			= luaL_checkinteger(L,2) -1;

	std::string path = porting::path_user + DIR_DELIM
			"worlds" + DIR_DELIM
			+ name;

	std::vector<SubgameSpec> games = getAvailableGames();

	if ((gameidx >= 0) &&
			(gameidx < (int) games.size())) {

		// Create world if it doesn't exist
		if (!loadGameConfAndInitWorld(path, games[gameidx])) {
			lua_pushstring(L, "Failed to initialize world");
		} else {
			lua_pushnil(L);
		}
	} else {
		lua_pushstring(L, "Invalid game index");
	}
	return 1;
}

/******************************************************************************/
int ModApiMainMenu::l_delete_world(lua_State *L)
{
	int world_id = luaL_checkinteger(L, 1) - 1;
	std::vector<WorldSpec> worlds = getAvailableWorlds();
	if (world_id < 0 || world_id >= (int) worlds.size()) {
		lua_pushstring(L, "Invalid world index");
		return 1;
	}
	const WorldSpec &spec = worlds[world_id];
	if (!fs::RecursiveDelete(spec.path)) {
		lua_pushstring(L, "Failed to delete world");
		return 1;
	}
	return 0;
}

/******************************************************************************/
int ModApiMainMenu::l_set_topleft_text(lua_State *L)
{
	GUIEngine* engine = getGuiEngine(L);
	sanity_check(engine != NULL);

	std::string text;

	if (!lua_isnone(L,1) &&	!lua_isnil(L,1))
		text = luaL_checkstring(L, 1);

	engine->setTopleftText(text);
	return 0;
}