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