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