Re-add jungles, apple trees
[oweals/minetest.git] / src / subgame.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 #include "util/string.h"
26
27 std::string getGameName(const std::string &game_path)
28 {
29         std::string conf_path = game_path + DIR_DELIM + "game.conf";
30         Settings conf;
31         bool succeeded = conf.readConfigFile(conf_path.c_str());
32         if(!succeeded)
33                 return "";
34         if(!conf.exists("name"))
35                 return "";
36         return conf.get("name");
37 }
38
39 struct GameFindPath
40 {
41         std::string path;
42         bool user_specific;
43         GameFindPath(const std::string &path, bool user_specific):
44                 path(path),
45                 user_specific(user_specific)
46         {}
47 };
48
49 SubgameSpec findSubgame(const std::string &id)
50 {
51         if(id == "")
52                 return SubgameSpec();
53         std::string share = porting::path_share;
54         std::string user = porting::path_user;
55         std::vector<GameFindPath> find_paths;
56         find_paths.push_back(GameFindPath(
57                         user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true));
58         find_paths.push_back(GameFindPath(
59                         user + DIR_DELIM + "games" + DIR_DELIM + id, true));
60         find_paths.push_back(GameFindPath(
61                         share + DIR_DELIM + "games" + DIR_DELIM + id + "_game", false));
62         find_paths.push_back(GameFindPath(
63                         share + DIR_DELIM + "games" + DIR_DELIM + id, false));
64         // Find game directory
65         std::string game_path;
66         bool user_game = true; // Game is in user's directory
67         for(u32 i=0; i<find_paths.size(); i++){
68                 const std::string &try_path = find_paths[i].path;
69                 if(fs::PathExists(try_path)){
70                         game_path = try_path;
71                         user_game = find_paths[i].user_specific;
72                         break;
73                 }
74         }
75         if(game_path == "")
76                 return SubgameSpec();
77         std::string gamemod_path = game_path + DIR_DELIM + "mods";
78         // Find mod directories
79         std::set<std::string> mods_paths;
80         if(!user_game)
81                 mods_paths.insert(share + DIR_DELIM + "mods" + DIR_DELIM + id);
82         if(user != share || user_game)
83                 mods_paths.insert(user + DIR_DELIM + "mods" + DIR_DELIM + id);
84         std::string game_name = getGameName(game_path);
85         if(game_name == "")
86                 game_name = id;
87         return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name);
88 }
89
90 SubgameSpec findWorldSubgame(const std::string &world_path)
91 {
92         std::string world_gameid = getWorldGameId(world_path, true);
93         // See if world contains an embedded game; if so, use it.
94         std::string world_gamepath = world_path + DIR_DELIM + "game";
95         if(fs::PathExists(world_gamepath)){
96                 SubgameSpec gamespec;
97                 gamespec.id = world_gameid;
98                 gamespec.path = world_gamepath;
99                 gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
100                 gamespec.name = getGameName(world_gamepath);
101                 if(gamespec.name == "")
102                         gamespec.name = "unknown";
103                 return gamespec;
104         }
105         return findSubgame(world_gameid);
106 }
107
108 std::set<std::string> getAvailableGameIds()
109 {
110         std::set<std::string> gameids;
111         std::set<std::string> gamespaths;
112         gamespaths.insert(porting::path_share + DIR_DELIM + "games");
113         gamespaths.insert(porting::path_user + DIR_DELIM + "games");
114         for(std::set<std::string>::const_iterator i = gamespaths.begin();
115                         i != gamespaths.end(); i++){
116                 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
117                 for(u32 j=0; j<dirlist.size(); j++){
118                         if(!dirlist[j].dir)
119                                 continue;
120                         const char *ends[] = {"_game", NULL};
121                         std::string shorter = removeStringEnd(dirlist[j].name, ends);
122                         if(shorter != "")
123                                 gameids.insert(shorter);
124                         else
125                                 gameids.insert(dirlist[j].name);
126                 }
127         }
128         return gameids;
129 }
130
131 std::vector<SubgameSpec> getAvailableGames()
132 {
133         std::vector<SubgameSpec> specs;
134         std::set<std::string> gameids = getAvailableGameIds();
135         for(std::set<std::string>::const_iterator i = gameids.begin();
136                         i != gameids.end(); i++)
137                 specs.push_back(findSubgame(*i));
138         return specs;
139 }
140
141 #define LEGACY_GAMEID "minetest"
142
143 bool getWorldExists(const std::string &world_path)
144 {
145         return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
146                         fs::PathExists(world_path + DIR_DELIM + "world.mt"));
147 }
148
149 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
150 {
151         std::string conf_path = world_path + DIR_DELIM + "world.mt";
152         Settings conf;
153         bool succeeded = conf.readConfigFile(conf_path.c_str());
154         if(!succeeded){
155                 if(can_be_legacy){
156                         // If map_meta.txt exists, it is probably an old minetest world
157                         if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
158                                 return LEGACY_GAMEID;
159                 }
160                 return "";
161         }
162         if(!conf.exists("gameid"))
163                 return "";
164         // The "mesetint" gameid has been discarded
165         if(conf.get("gameid") == "mesetint")
166                 return "minetest";
167         return conf.get("gameid");
168 }
169
170 std::vector<WorldSpec> getAvailableWorlds()
171 {
172         std::vector<WorldSpec> worlds;
173         std::set<std::string> worldspaths;
174         worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
175         infostream<<"Searching worlds..."<<std::endl;
176         for(std::set<std::string>::const_iterator i = worldspaths.begin();
177                         i != worldspaths.end(); i++){
178                 infostream<<"  In "<<(*i)<<": "<<std::endl;
179                 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
180                 for(u32 j=0; j<dirvector.size(); j++){
181                         if(!dirvector[j].dir)
182                                 continue;
183                         std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
184                         std::string name = dirvector[j].name;
185                         // Just allow filling in the gameid always for now
186                         bool can_be_legacy = true;
187                         std::string gameid = getWorldGameId(fullpath, can_be_legacy);
188                         WorldSpec spec(fullpath, name, gameid);
189                         if(!spec.isValid()){
190                                 infostream<<"(invalid: "<<name<<") ";
191                         } else {
192                                 infostream<<name<<" ";
193                                 worlds.push_back(spec);
194                         }
195                 }
196                 infostream<<std::endl;
197         }
198         // Check old world location
199         do{
200                 std::string fullpath = porting::path_user + DIR_DELIM + "world";
201                 if(!fs::PathExists(fullpath))
202                         break;
203                 std::string name = "Old World";
204                 std::string gameid = getWorldGameId(fullpath, true);
205                 WorldSpec spec(fullpath, name, gameid);
206                 infostream<<"Old world found."<<std::endl;
207                 worlds.push_back(spec);
208         }while(0);
209         infostream<<worlds.size()<<" found."<<std::endl;
210         return worlds;
211 }
212
213 bool initializeWorld(const std::string &path, const std::string &gameid)
214 {
215         infostream<<"Initializing world at "<<path<<std::endl;
216         // Create world.mt if does not already exist
217         std::string worldmt_path = path + DIR_DELIM + "world.mt";
218         if(!fs::PathExists(worldmt_path)){
219                 infostream<<"Creating world.mt ("<<worldmt_path<<")"<<std::endl;
220                 fs::CreateAllDirs(path);
221                 std::ofstream of(worldmt_path.c_str(), std::ios::binary);
222                 of<<"gameid = "<<gameid<<"\n";
223         }
224         return true;
225 }
226
227