World selection box in main menu (and random fixing)
[oweals/minetest.git] / src / subgame.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2012 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 #include "subgame.h"
21 #include "porting.h"
22 #include "filesys.h"
23 #include "settings.h"
24 #include "log.h"
25
26 SubgameSpec findSubgame(const std::string &id)
27 {
28         if(id == "")
29                 return SubgameSpec();
30         std::string share_server = porting::path_share + DIR_DELIM + "server";
31         std::string user_server = porting::path_user + DIR_DELIM + "server";
32         // Find game directory
33         std::string game_path =
34                         user_server + DIR_DELIM + "games" + DIR_DELIM + id;
35         bool user_game = true; // Game is in user's directory
36         if(!fs::PathExists(game_path)){
37                 game_path = share_server + DIR_DELIM + "games" + DIR_DELIM + id;
38                 user_game = false;
39         }
40         if(!fs::PathExists(game_path))
41                 return SubgameSpec();
42         // Find addon directories
43         std::set<std::string> addon_paths;
44         if(!user_game)
45                 addon_paths.insert(share_server + DIR_DELIM + "addons"
46                                 + DIR_DELIM + id);
47         addon_paths.insert(user_server + DIR_DELIM + "addons"
48                         + DIR_DELIM + id);
49         return SubgameSpec(id, game_path, addon_paths);
50 }
51
52 std::set<std::string> getAvailableGameIds()
53 {
54         std::set<std::string> gameids;
55         std::set<std::string> gamespaths;
56         gamespaths.insert(porting::path_share + DIR_DELIM + "server"
57                         + DIR_DELIM + "games");
58         gamespaths.insert(porting::path_user + DIR_DELIM + "server"
59                         + DIR_DELIM + "games");
60         for(std::set<std::string>::const_iterator i = gamespaths.begin();
61                         i != gamespaths.end(); i++){
62                 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
63                 for(u32 j=0; j<dirlist.size(); j++){
64                         if(!dirlist[j].dir)
65                                 continue;
66                         gameids.insert(dirlist[j].name);
67                 }
68         }
69         return gameids;
70 }
71
72 #define LEGACY_GAMEID "mesetint"
73
74 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
75 {
76         std::string conf_path = world_path + DIR_DELIM + "world.mt";
77         Settings conf;
78         bool succeeded = conf.readConfigFile(conf_path.c_str());
79         if(!succeeded){
80                 if(can_be_legacy){
81                         // If map_meta.txt exists, it is probably an old minetest world
82                         if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
83                                 return LEGACY_GAMEID;
84                 }
85                 return "";
86         }
87         if(!conf.exists("gameid"))
88                 return "";
89         return conf.get("gameid");
90 }
91
92 std::vector<WorldSpec> getAvailableWorlds()
93 {
94         std::vector<WorldSpec> worlds;
95         std::set<std::string> worldspaths;
96         worldspaths.insert(porting::path_user + DIR_DELIM + "server"
97                         + DIR_DELIM + "worlds");
98         infostream<<"Searching worlds..."<<std::endl;
99         for(std::set<std::string>::const_iterator i = worldspaths.begin();
100                         i != worldspaths.end(); i++){
101                 infostream<<"  In "<<(*i)<<": "<<std::endl;
102                 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
103                 for(u32 j=0; j<dirvector.size(); j++){
104                         if(!dirvector[j].dir)
105                                 continue;
106                         std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
107                         std::string name = dirvector[j].name;
108                         std::string gameid = getWorldGameId(fullpath);
109                         WorldSpec spec(fullpath, name, gameid);
110                         if(!spec.isValid()){
111                                 infostream<<"(invalid: "<<name<<") ";
112                         } else {
113                                 infostream<<name<<" ";
114                                 worlds.push_back(spec);
115                         }
116                 }
117                 infostream<<std::endl;
118         }
119         // Check old world location
120         do{
121                 std::string fullpath = porting::path_user + DIR_DELIM + ".."
122                                 + DIR_DELIM + "world";
123                 if(!fs::PathExists(fullpath))
124                         break;
125                 std::string name = "Old World";
126                 std::string gameid = getWorldGameId(fullpath, true);
127                 WorldSpec spec(fullpath, name, gameid);
128                 infostream<<"Old world found."<<std::endl;
129                 worlds.push_back(spec);
130         }while(0);
131         infostream<<worlds.size()<<" found."<<std::endl;
132         return worlds;
133 }
134
135