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