]> git.lizzy.rs Git - minetest.git/blob - src/content/subgames.cpp
Add "MINETEST_MOD_PATH" environment variable (#11515)
[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 set_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 // The maximum number of identical world names allowed
35 #define MAX_WORLD_NAMES 100
36
37 namespace
38 {
39
40 bool getGameMinetestConfig(const std::string &game_path, Settings &conf)
41 {
42         std::string conf_path = game_path + DIR_DELIM + "minetest.conf";
43         return conf.readConfigFile(conf_path.c_str());
44 }
45
46 }
47
48 struct GameFindPath
49 {
50         std::string path;
51         bool user_specific;
52         GameFindPath(const std::string &path, bool user_specific) :
53                         path(path), user_specific(user_specific)
54         {
55         }
56 };
57
58 std::string getSubgamePathEnv()
59 {
60         char *subgame_path = getenv("MINETEST_SUBGAME_PATH");
61         return subgame_path ? std::string(subgame_path) : "";
62 }
63
64 SubgameSpec findSubgame(const std::string &id)
65 {
66         if (id.empty())
67                 return SubgameSpec();
68         std::string share = porting::path_share;
69         std::string user = porting::path_user;
70
71         // Get games install locations
72         Strfnd search_paths(getSubgamePathEnv());
73
74         // Get all possible paths fo game
75         std::vector<GameFindPath> find_paths;
76         while (!search_paths.at_end()) {
77                 std::string path = search_paths.next(PATH_DELIM);
78                 path.append(DIR_DELIM).append(id);
79                 find_paths.emplace_back(path, false);
80                 path.append("_game");
81                 find_paths.emplace_back(path, false);
82         }
83
84         std::string game_base = DIR_DELIM;
85         game_base = game_base.append("games").append(DIR_DELIM).append(id);
86         std::string game_suffixed = game_base + "_game";
87         find_paths.emplace_back(user + game_suffixed, true);
88         find_paths.emplace_back(user + game_base, true);
89         find_paths.emplace_back(share + game_suffixed, false);
90         find_paths.emplace_back(share + game_base, false);
91
92         // Find game directory
93         std::string game_path;
94         bool user_game = true; // Game is in user's directory
95         for (const GameFindPath &find_path : find_paths) {
96                 const std::string &try_path = find_path.path;
97                 if (fs::PathExists(try_path)) {
98                         game_path = try_path;
99                         user_game = find_path.user_specific;
100                         break;
101                 }
102         }
103
104         if (game_path.empty())
105                 return SubgameSpec();
106
107         std::string gamemod_path = game_path + DIR_DELIM + "mods";
108
109         // Find mod directories
110         std::set<std::string> mods_paths;
111         if (!user_game)
112                 mods_paths.insert(share + DIR_DELIM + "mods");
113         if (user != share || user_game)
114                 mods_paths.insert(user + DIR_DELIM + "mods");
115
116         for (const std::string &mod_path : getEnvModPaths()) {
117                 mods_paths.insert(mod_path);
118         }
119
120         // Get meta
121         std::string conf_path = game_path + DIR_DELIM + "game.conf";
122         Settings conf;
123         conf.readConfigFile(conf_path.c_str());
124
125         std::string game_name;
126         if (conf.exists("name"))
127                 game_name = conf.get("name");
128         else
129                 game_name = id;
130
131         std::string game_author;
132         if (conf.exists("author"))
133                 game_author = conf.get("author");
134
135         int game_release = 0;
136         if (conf.exists("release"))
137                 game_release = conf.getS32("release");
138
139         std::string menuicon_path;
140 #ifndef SERVER
141         menuicon_path = getImagePath(
142                         game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
143 #endif
144         return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
145                         menuicon_path, game_author, game_release);
146 }
147
148 SubgameSpec findWorldSubgame(const std::string &world_path)
149 {
150         std::string world_gameid = getWorldGameId(world_path, true);
151         // See if world contains an embedded game; if so, use it.
152         std::string world_gamepath = world_path + DIR_DELIM + "game";
153         if (fs::PathExists(world_gamepath)) {
154                 SubgameSpec gamespec;
155                 gamespec.id = world_gameid;
156                 gamespec.path = world_gamepath;
157                 gamespec.gamemods_path = world_gamepath + DIR_DELIM + "mods";
158
159                 Settings conf;
160                 std::string conf_path = world_gamepath + DIR_DELIM + "game.conf";
161                 conf.readConfigFile(conf_path.c_str());
162
163                 if (conf.exists("name"))
164                         gamespec.name = conf.get("name");
165                 else
166                         gamespec.name = world_gameid;
167
168                 return gamespec;
169         }
170         return findSubgame(world_gameid);
171 }
172
173 std::set<std::string> getAvailableGameIds()
174 {
175         std::set<std::string> gameids;
176         std::set<std::string> gamespaths;
177         gamespaths.insert(porting::path_share + DIR_DELIM + "games");
178         gamespaths.insert(porting::path_user + DIR_DELIM + "games");
179
180         Strfnd search_paths(getSubgamePathEnv());
181
182         while (!search_paths.at_end())
183                 gamespaths.insert(search_paths.next(PATH_DELIM));
184
185         for (const std::string &gamespath : gamespaths) {
186                 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(gamespath);
187                 for (const fs::DirListNode &dln : dirlist) {
188                         if (!dln.dir)
189                                 continue;
190
191                         // If configuration file is not found or broken, ignore game
192                         Settings conf;
193                         std::string conf_path = gamespath + DIR_DELIM + dln.name +
194                                                 DIR_DELIM + "game.conf";
195                         if (!conf.readConfigFile(conf_path.c_str()))
196                                 continue;
197
198                         // Add it to result
199                         const char *ends[] = {"_game", NULL};
200                         std::string shorter = removeStringEnd(dln.name, ends);
201                         if (!shorter.empty())
202                                 gameids.insert(shorter);
203                         else
204                                 gameids.insert(dln.name);
205                 }
206         }
207         return gameids;
208 }
209
210 std::vector<SubgameSpec> getAvailableGames()
211 {
212         std::vector<SubgameSpec> specs;
213         std::set<std::string> gameids = getAvailableGameIds();
214         specs.reserve(gameids.size());
215         for (const auto &gameid : gameids)
216                 specs.push_back(findSubgame(gameid));
217         return specs;
218 }
219
220 #define LEGACY_GAMEID "minetest"
221
222 bool getWorldExists(const std::string &world_path)
223 {
224         return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
225                         fs::PathExists(world_path + DIR_DELIM + "world.mt"));
226 }
227
228 //! Try to get the displayed name of a world
229 std::string getWorldName(const std::string &world_path, const std::string &default_name)
230 {
231         std::string conf_path = world_path + DIR_DELIM + "world.mt";
232         Settings conf;
233         bool succeeded = conf.readConfigFile(conf_path.c_str());
234         if (!succeeded) {
235                 return default_name;
236         }
237
238         if (!conf.exists("world_name"))
239                 return default_name;
240         return conf.get("world_name");
241 }
242
243 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
244 {
245         std::string conf_path = world_path + DIR_DELIM + "world.mt";
246         Settings conf;
247         bool succeeded = conf.readConfigFile(conf_path.c_str());
248         if (!succeeded) {
249                 if (can_be_legacy) {
250                         // If map_meta.txt exists, it is probably an old minetest world
251                         if (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
252                                 return LEGACY_GAMEID;
253                 }
254                 return "";
255         }
256         if (!conf.exists("gameid"))
257                 return "";
258         // The "mesetint" gameid has been discarded
259         if (conf.get("gameid") == "mesetint")
260                 return "minetest";
261         return conf.get("gameid");
262 }
263
264 std::string getWorldPathEnv()
265 {
266         char *world_path = getenv("MINETEST_WORLD_PATH");
267         return world_path ? std::string(world_path) : "";
268 }
269
270 std::vector<WorldSpec> getAvailableWorlds()
271 {
272         std::vector<WorldSpec> worlds;
273         std::set<std::string> worldspaths;
274
275         Strfnd search_paths(getWorldPathEnv());
276
277         while (!search_paths.at_end())
278                 worldspaths.insert(search_paths.next(PATH_DELIM));
279
280         worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
281         infostream << "Searching worlds..." << std::endl;
282         for (const std::string &worldspath : worldspaths) {
283                 infostream << "  In " << worldspath << ": ";
284                 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(worldspath);
285                 for (const fs::DirListNode &dln : dirvector) {
286                         if (!dln.dir)
287                                 continue;
288                         std::string fullpath = worldspath + DIR_DELIM + dln.name;
289                         std::string name = getWorldName(fullpath, dln.name);
290                         // Just allow filling in the gameid always for now
291                         bool can_be_legacy = true;
292                         std::string gameid = getWorldGameId(fullpath, can_be_legacy);
293                         WorldSpec spec(fullpath, name, gameid);
294                         if (!spec.isValid()) {
295                                 infostream << "(invalid: " << name << ") ";
296                         } else {
297                                 infostream << name << " ";
298                                 worlds.push_back(spec);
299                         }
300                 }
301                 infostream << std::endl;
302         }
303         // Check old world location
304         do {
305                 std::string fullpath = porting::path_user + DIR_DELIM + "world";
306                 if (!fs::PathExists(fullpath))
307                         break;
308                 std::string name = "Old World";
309                 std::string gameid = getWorldGameId(fullpath, true);
310                 WorldSpec spec(fullpath, name, gameid);
311                 infostream << "Old world found." << std::endl;
312                 worlds.push_back(spec);
313         } while (false);
314         infostream << worlds.size() << " found." << std::endl;
315         return worlds;
316 }
317
318 void loadGameConfAndInitWorld(const std::string &path, const std::string &name,
319                 const SubgameSpec &gamespec, bool create_world)
320 {
321         std::string final_path = path;
322
323         // If we're creating a new world, ensure that the path isn't already taken
324         if (create_world) {
325                 int counter = 1;
326                 while (fs::PathExists(final_path) && counter < MAX_WORLD_NAMES) {
327                         final_path = path + "_" + std::to_string(counter);
328                         counter++;
329                 }
330
331                 if (fs::PathExists(final_path)) {
332                         throw BaseException("Too many similar filenames");
333                 }
334         }
335
336         Settings *game_settings = Settings::getLayer(SL_GAME);
337         const bool new_game_settings = (game_settings == nullptr);
338         if (new_game_settings) {
339                 // Called by main-menu without a Server instance running
340                 // -> create and free manually
341                 game_settings = Settings::createLayer(SL_GAME);
342         }
343
344         getGameMinetestConfig(gamespec.path, *game_settings);
345         game_settings->removeSecureSettings();
346
347         infostream << "Initializing world at " << final_path << std::endl;
348
349         fs::CreateAllDirs(final_path);
350
351         // Create world.mt if does not already exist
352         std::string worldmt_path = final_path + DIR_DELIM "world.mt";
353         if (!fs::PathExists(worldmt_path)) {
354                 Settings conf;
355
356                 conf.set("world_name", name);
357                 conf.set("gameid", gamespec.id);
358                 conf.set("backend", "sqlite3");
359                 conf.set("player_backend", "sqlite3");
360                 conf.set("auth_backend", "sqlite3");
361                 conf.setBool("creative_mode", g_settings->getBool("creative_mode"));
362                 conf.setBool("enable_damage", g_settings->getBool("enable_damage"));
363
364                 if (!conf.updateConfigFile(worldmt_path.c_str())) {
365                         throw BaseException("Failed to update the config file");
366                 }
367         }
368
369         // Create map_meta.txt if does not already exist
370         std::string map_meta_path = final_path + DIR_DELIM + "map_meta.txt";
371         if (!fs::PathExists(map_meta_path)) {
372                 verbosestream << "Creating map_meta.txt (" << map_meta_path << ")"
373                               << std::endl;
374                 std::ostringstream oss(std::ios_base::binary);
375
376                 Settings conf;
377                 MapgenParams params;
378
379                 params.readParams(g_settings);
380                 params.writeParams(&conf);
381                 conf.writeLines(oss);
382                 oss << "[end_of_params]\n";
383
384                 fs::safeWriteToFile(map_meta_path, oss.str());
385         }
386
387         // The Settings object is no longer needed for created worlds
388         if (new_game_settings)
389                 delete game_settings;
390 }
391
392 std::vector<std::string> getEnvModPaths()
393 {
394         const char *c_mod_path = getenv("MINETEST_MOD_PATH");
395         std::vector<std::string> paths;
396         Strfnd search_paths(c_mod_path ? c_mod_path : "");
397         while (!search_paths.at_end())
398                 paths.push_back(search_paths.next(PATH_DELIM));
399         return paths;
400 }