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