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