Search for subgames using $MINETEST_SUBGAME_PATH.
[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 "strfnd.h"
26 #ifndef SERVER
27 #include "tile.h" // getImagePath
28 #endif
29 #include "util/string.h"
30
31 bool getGameMinetestConfig(const std::string &game_path, Settings &conf)
32 {
33         std::string conf_path = game_path + DIR_DELIM + "minetest.conf";
34         return conf.readConfigFile(conf_path.c_str());
35 }
36
37 bool getGameConfig(const std::string &game_path, Settings &conf)
38 {
39         std::string conf_path = game_path + DIR_DELIM + "game.conf";
40         return conf.readConfigFile(conf_path.c_str());
41 }
42
43 std::string getGameName(const std::string &game_path)
44 {
45         Settings conf;
46         if(!getGameConfig(game_path, conf))
47                 return "";
48         if(!conf.exists("name"))
49                 return "";
50         return conf.get("name");
51 }
52
53 struct GameFindPath
54 {
55         std::string path;
56         bool user_specific;
57         GameFindPath(const std::string &path, bool user_specific):
58                 path(path),
59                 user_specific(user_specific)
60         {}
61 };
62
63 Strfnd getSubgamePathEnv() {
64   std::string sp;
65   char *subgame_path = getenv("MINETEST_SUBGAME_PATH");
66
67   if(subgame_path) {
68     sp = std::string(subgame_path);
69   }
70
71   return Strfnd(sp);
72 }
73
74 SubgameSpec findSubgame(const std::string &id)
75 {
76         if(id == "")
77                 return SubgameSpec();
78         std::string share = porting::path_share;
79         std::string user = porting::path_user;
80         std::vector<GameFindPath> find_paths;
81
82         Strfnd search_paths = getSubgamePathEnv();
83
84         while(!search_paths.atend()) {
85                 std::string path = search_paths.next(":");
86                 find_paths.push_back(GameFindPath(
87                                        path + DIR_DELIM + id, false));
88                 find_paths.push_back(GameFindPath(
89                                        path + DIR_DELIM + id + "_game", false));
90         }
91
92         find_paths.push_back(GameFindPath(
93                         user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true));
94         find_paths.push_back(GameFindPath(
95                         user + DIR_DELIM + "games" + DIR_DELIM + id, true));
96         find_paths.push_back(GameFindPath(
97                         share + DIR_DELIM + "games" + DIR_DELIM + id + "_game", false));
98         find_paths.push_back(GameFindPath(
99                         share + DIR_DELIM + "games" + DIR_DELIM + id, false));
100         // Find game directory
101         std::string game_path;
102         bool user_game = true; // Game is in user's directory
103         for(u32 i=0; i<find_paths.size(); i++){
104                 const std::string &try_path = find_paths[i].path;
105                 if(fs::PathExists(try_path)){
106                         game_path = try_path;
107                         user_game = find_paths[i].user_specific;
108                         break;
109                 }
110         }
111         if(game_path == "")
112                 return SubgameSpec();
113         std::string gamemod_path = game_path + DIR_DELIM + "mods";
114         // Find mod directories
115         std::set<std::string> mods_paths;
116         if(!user_game)
117                 mods_paths.insert(share + DIR_DELIM + "mods");
118         if(user != share || user_game)
119                 mods_paths.insert(user + DIR_DELIM + "mods");
120         std::string game_name = getGameName(game_path);
121         if(game_name == "")
122                 game_name = id;
123         std::string menuicon_path;
124 #ifndef SERVER
125         menuicon_path = getImagePath(game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
126 #endif
127         return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
128                         menuicon_path);
129 }
130
131 SubgameSpec findWorldSubgame(const std::string &world_path)
132 {
133         std::string world_gameid = getWorldGameId(world_path, true);
134         // See if world contains an embedded game; if so, use it.
135         std::string world_gamepath = world_path + DIR_DELIM + "game";
136         if(fs::PathExists(world_gamepath)){
137                 SubgameSpec gamespec;
138                 gamespec.id = world_gameid;
139                 gamespec.path = world_gamepath;
140                 gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
141                 gamespec.name = getGameName(world_gamepath);
142                 if(gamespec.name == "")
143                         gamespec.name = "unknown";
144                 return gamespec;
145         }
146         return findSubgame(world_gameid);
147 }
148
149 std::set<std::string> getAvailableGameIds()
150 {
151         std::set<std::string> gameids;
152         std::set<std::string> gamespaths;
153         gamespaths.insert(porting::path_share + DIR_DELIM + "games");
154         gamespaths.insert(porting::path_user + DIR_DELIM + "games");
155
156         Strfnd search_paths = getSubgamePathEnv();
157
158         while(!search_paths.atend()) {
159                 gamespaths.insert(search_paths.next(":"));
160         }
161
162         for(std::set<std::string>::const_iterator i = gamespaths.begin();
163                         i != gamespaths.end(); i++){
164                 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
165                 for(u32 j=0; j<dirlist.size(); j++){
166                         if(!dirlist[j].dir)
167                                 continue;
168                         // If configuration file is not found or broken, ignore game
169                         Settings conf;
170                         if(!getGameConfig(*i + DIR_DELIM + dirlist[j].name, conf))
171                                 continue;
172                         // Add it to result
173                         const char *ends[] = {"_game", NULL};
174                         std::string shorter = removeStringEnd(dirlist[j].name, ends);
175                         if(shorter != "")
176                                 gameids.insert(shorter);
177                         else
178                                 gameids.insert(dirlist[j].name);
179                 }
180         }
181         return gameids;
182 }
183
184 std::vector<SubgameSpec> getAvailableGames()
185 {
186         std::vector<SubgameSpec> specs;
187         std::set<std::string> gameids = getAvailableGameIds();
188         for(std::set<std::string>::const_iterator i = gameids.begin();
189                         i != gameids.end(); i++)
190                 specs.push_back(findSubgame(*i));
191         return specs;
192 }
193
194 #define LEGACY_GAMEID "minetest"
195
196 bool getWorldExists(const std::string &world_path)
197 {
198         return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
199                         fs::PathExists(world_path + DIR_DELIM + "world.mt"));
200 }
201
202 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
203 {
204         std::string conf_path = world_path + DIR_DELIM + "world.mt";
205         Settings conf;
206         bool succeeded = conf.readConfigFile(conf_path.c_str());
207         if(!succeeded){
208                 if(can_be_legacy){
209                         // If map_meta.txt exists, it is probably an old minetest world
210                         if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
211                                 return LEGACY_GAMEID;
212                 }
213                 return "";
214         }
215         if(!conf.exists("gameid"))
216                 return "";
217         // The "mesetint" gameid has been discarded
218         if(conf.get("gameid") == "mesetint")
219                 return "minetest";
220         return conf.get("gameid");
221 }
222
223 std::vector<WorldSpec> getAvailableWorlds()
224 {
225         std::vector<WorldSpec> worlds;
226         std::set<std::string> worldspaths;
227         worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
228         infostream<<"Searching worlds..."<<std::endl;
229         for(std::set<std::string>::const_iterator i = worldspaths.begin();
230                         i != worldspaths.end(); i++){
231                 infostream<<"  In "<<(*i)<<": "<<std::endl;
232                 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
233                 for(u32 j=0; j<dirvector.size(); j++){
234                         if(!dirvector[j].dir)
235                                 continue;
236                         std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
237                         std::string name = dirvector[j].name;
238                         // Just allow filling in the gameid always for now
239                         bool can_be_legacy = true;
240                         std::string gameid = getWorldGameId(fullpath, can_be_legacy);
241                         WorldSpec spec(fullpath, name, gameid);
242                         if(!spec.isValid()){
243                                 infostream<<"(invalid: "<<name<<") ";
244                         } else {
245                                 infostream<<name<<" ";
246                                 worlds.push_back(spec);
247                         }
248                 }
249                 infostream<<std::endl;
250         }
251         // Check old world location
252         do{
253                 std::string fullpath = porting::path_user + DIR_DELIM + "world";
254                 if(!fs::PathExists(fullpath))
255                         break;
256                 std::string name = "Old World";
257                 std::string gameid = getWorldGameId(fullpath, true);
258                 WorldSpec spec(fullpath, name, gameid);
259                 infostream<<"Old world found."<<std::endl;
260                 worlds.push_back(spec);
261         }while(0);
262         infostream<<worlds.size()<<" found."<<std::endl;
263         return worlds;
264 }
265
266 bool initializeWorld(const std::string &path, const std::string &gameid)
267 {
268         infostream<<"Initializing world at "<<path<<std::endl;
269         // Create world.mt if does not already exist
270         std::string worldmt_path = path + DIR_DELIM + "world.mt";
271         if(!fs::PathExists(worldmt_path)){
272                 infostream<<"Creating world.mt ("<<worldmt_path<<")"<<std::endl;
273                 fs::CreateAllDirs(path);
274                 std::ostringstream ss(std::ios_base::binary);
275                 ss<<"gameid = "<<gameid<< "\nbackend = sqlite3\n";
276                 fs::safeWriteToFile(worldmt_path, ss.str());
277         }
278         return true;
279 }
280
281