]> git.lizzy.rs Git - dragonfireclient.git/blob - src/content/subgames.cpp
23355990ec442671acd4e7fe8bf73853cd63645a
[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 set_default_settings
27 #include "map_settings_manager.h"
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::unordered_map<std::string, std::string> mods_paths;
111         mods_paths["mods"] = user + DIR_DELIM + "mods";
112         if (!user_game && user != share)
113                 mods_paths["share"] = share + DIR_DELIM + "mods";
114
115         for (const std::string &mod_path : getEnvModPaths()) {
116                 mods_paths[fs::AbsolutePath(mod_path)] = mod_path;
117         }
118
119         // Get meta
120         std::string conf_path = game_path + DIR_DELIM + "game.conf";
121         Settings conf;
122         conf.readConfigFile(conf_path.c_str());
123
124         std::string game_name;
125         if (conf.exists("name"))
126                 game_name = conf.get("name");
127         else
128                 game_name = id;
129
130         std::string game_author;
131         if (conf.exists("author"))
132                 game_author = conf.get("author");
133
134         int game_release = 0;
135         if (conf.exists("release"))
136                 game_release = conf.getS32("release");
137
138         std::string menuicon_path;
139 #ifndef SERVER
140         menuicon_path = getImagePath(
141                         game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
142 #endif
143         return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
144                         menuicon_path, game_author, game_release);
145 }
146
147 SubgameSpec findWorldSubgame(const std::string &world_path)
148 {
149         std::string world_gameid = getWorldGameId(world_path, true);
150         // See if world contains an embedded game; if so, use it.
151         std::string world_gamepath = world_path + DIR_DELIM + "game";
152         if (fs::PathExists(world_gamepath)) {
153                 SubgameSpec gamespec;
154                 gamespec.id = world_gameid;
155                 gamespec.path = world_gamepath;
156                 gamespec.gamemods_path = world_gamepath + DIR_DELIM + "mods";
157
158                 Settings conf;
159                 std::string conf_path = world_gamepath + DIR_DELIM + "game.conf";
160                 conf.readConfigFile(conf_path.c_str());
161
162                 if (conf.exists("name"))
163                         gamespec.name = conf.get("name");
164                 else
165                         gamespec.name = world_gameid;
166
167                 return gamespec;
168         }
169         return findSubgame(world_gameid);
170 }
171
172 std::set<std::string> getAvailableGameIds()
173 {
174         std::set<std::string> gameids;
175         std::set<std::string> gamespaths;
176         gamespaths.insert(porting::path_share + DIR_DELIM + "games");
177         gamespaths.insert(porting::path_user + DIR_DELIM + "games");
178
179         Strfnd search_paths(getSubgamePathEnv());
180
181         while (!search_paths.at_end())
182                 gamespaths.insert(search_paths.next(PATH_DELIM));
183
184         for (const std::string &gamespath : gamespaths) {
185                 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(gamespath);
186                 for (const fs::DirListNode &dln : dirlist) {
187                         if (!dln.dir)
188                                 continue;
189
190                         // If configuration file is not found or broken, ignore game
191                         Settings conf;
192                         std::string conf_path = gamespath + DIR_DELIM + dln.name +
193                                                 DIR_DELIM + "game.conf";
194                         if (!conf.readConfigFile(conf_path.c_str()))
195                                 continue;
196
197                         // Add it to result
198                         const char *ends[] = {"_game", NULL};
199                         std::string shorter = removeStringEnd(dln.name, ends);
200                         if (!shorter.empty())
201                                 gameids.insert(shorter);
202                         else
203                                 gameids.insert(dln.name);
204                 }
205         }
206         return gameids;
207 }
208
209 std::vector<SubgameSpec> getAvailableGames()
210 {
211         std::vector<SubgameSpec> specs;
212         std::set<std::string> gameids = getAvailableGameIds();
213         specs.reserve(gameids.size());
214         for (const auto &gameid : gameids)
215                 specs.push_back(findSubgame(gameid));
216         return specs;
217 }
218
219 #define LEGACY_GAMEID "minetest"
220
221 bool getWorldExists(const std::string &world_path)
222 {
223         return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
224                         fs::PathExists(world_path + DIR_DELIM + "world.mt"));
225 }
226
227 //! Try to get the displayed name of a world
228 std::string getWorldName(const std::string &world_path, const std::string &default_name)
229 {
230         std::string conf_path = world_path + DIR_DELIM + "world.mt";
231         Settings conf;
232         bool succeeded = conf.readConfigFile(conf_path.c_str());
233         if (!succeeded) {
234                 return default_name;
235         }
236
237         if (!conf.exists("world_name"))
238                 return default_name;
239         return conf.get("world_name");
240 }
241
242 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
243 {
244         std::string conf_path = world_path + DIR_DELIM + "world.mt";
245         Settings conf;
246         bool succeeded = conf.readConfigFile(conf_path.c_str());
247         if (!succeeded) {
248                 if (can_be_legacy) {
249                         // If map_meta.txt exists, it is probably an old minetest world
250                         if (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
251                                 return LEGACY_GAMEID;
252                 }
253                 return "";
254         }
255         if (!conf.exists("gameid"))
256                 return "";
257         // The "mesetint" gameid has been discarded
258         if (conf.get("gameid") == "mesetint")
259                 return "minetest";
260         return conf.get("gameid");
261 }
262
263 std::string getWorldPathEnv()
264 {
265         char *world_path = getenv("MINETEST_WORLD_PATH");
266         return world_path ? std::string(world_path) : "";
267 }
268
269 std::vector<WorldSpec> getAvailableWorlds()
270 {
271         std::vector<WorldSpec> worlds;
272         std::set<std::string> worldspaths;
273
274         Strfnd search_paths(getWorldPathEnv());
275
276         while (!search_paths.at_end())
277                 worldspaths.insert(search_paths.next(PATH_DELIM));
278
279         worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
280         infostream << "Searching worlds..." << std::endl;
281         for (const std::string &worldspath : worldspaths) {
282                 infostream << "  In " << worldspath << ": ";
283                 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(worldspath);
284                 for (const fs::DirListNode &dln : dirvector) {
285                         if (!dln.dir)
286                                 continue;
287                         std::string fullpath = worldspath + DIR_DELIM + dln.name;
288                         std::string name = getWorldName(fullpath, dln.name);
289                         // Just allow filling in the gameid always for now
290                         bool can_be_legacy = true;
291                         std::string gameid = getWorldGameId(fullpath, can_be_legacy);
292                         WorldSpec spec(fullpath, name, gameid);
293                         if (!spec.isValid()) {
294                                 infostream << "(invalid: " << name << ") ";
295                         } else {
296                                 infostream << name << " ";
297                                 worlds.push_back(spec);
298                         }
299                 }
300                 infostream << std::endl;
301         }
302         // Check old world location
303         do {
304                 std::string fullpath = porting::path_user + DIR_DELIM + "world";
305                 if (!fs::PathExists(fullpath))
306                         break;
307                 std::string name = "Old World";
308                 std::string gameid = getWorldGameId(fullpath, true);
309                 WorldSpec spec(fullpath, name, gameid);
310                 infostream << "Old world found." << std::endl;
311                 worlds.push_back(spec);
312         } while (false);
313         infostream << worlds.size() << " found." << std::endl;
314         return worlds;
315 }
316
317 void loadGameConfAndInitWorld(const std::string &path, const std::string &name,
318                 const SubgameSpec &gamespec, bool create_world)
319 {
320         std::string final_path = path;
321
322         // If we're creating a new world, ensure that the path isn't already taken
323         if (create_world) {
324                 int counter = 1;
325                 while (fs::PathExists(final_path) && counter < MAX_WORLD_NAMES) {
326                         final_path = path + "_" + std::to_string(counter);
327                         counter++;
328                 }
329
330                 if (fs::PathExists(final_path)) {
331                         throw BaseException("Too many similar filenames");
332                 }
333         }
334
335         Settings *game_settings = Settings::getLayer(SL_GAME);
336         const bool new_game_settings = (game_settings == nullptr);
337         if (new_game_settings) {
338                 // Called by main-menu without a Server instance running
339                 // -> create and free manually
340                 game_settings = Settings::createLayer(SL_GAME);
341         }
342
343         getGameMinetestConfig(gamespec.path, *game_settings);
344         game_settings->removeSecureSettings();
345
346         infostream << "Initializing world at " << final_path << std::endl;
347
348         fs::CreateAllDirs(final_path);
349
350         // Create world.mt if does not already exist
351         std::string worldmt_path = final_path + DIR_DELIM "world.mt";
352         if (!fs::PathExists(worldmt_path)) {
353                 Settings conf;
354
355                 conf.set("world_name", name);
356                 conf.set("gameid", gamespec.id);
357                 conf.set("backend", "sqlite3");
358                 conf.set("player_backend", "sqlite3");
359                 conf.set("auth_backend", "sqlite3");
360                 conf.set("mod_storage_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                 MapSettingsManager mgr(map_meta_path);
373
374                 mgr.setMapSetting("seed", g_settings->get("fixed_map_seed"));
375
376                 mgr.makeMapgenParams();
377                 mgr.saveMapMeta();
378         }
379
380         // The Settings object is no longer needed for created worlds
381         if (new_game_settings)
382                 delete game_settings;
383 }
384
385 std::vector<std::string> getEnvModPaths()
386 {
387         const char *c_mod_path = getenv("MINETEST_MOD_PATH");
388         std::vector<std::string> paths;
389         Strfnd search_paths(c_mod_path ? c_mod_path : "");
390         while (!search_paths.at_end())
391                 paths.push_back(search_paths.next(PATH_DELIM));
392         return paths;
393 }