]> git.lizzy.rs Git - dragonfireclient.git/blob - src/content/subgames.cpp
Make sqlite3 the default auth backend (#8085)
[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         for (const auto &gameid : gameids)
199                 specs.push_back(findSubgame(gameid));
200         return specs;
201 }
202
203 #define LEGACY_GAMEID "minetest"
204
205 bool getWorldExists(const std::string &world_path)
206 {
207         return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
208                         fs::PathExists(world_path + DIR_DELIM + "world.mt"));
209 }
210
211 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
212 {
213         std::string conf_path = world_path + DIR_DELIM + "world.mt";
214         Settings conf;
215         bool succeeded = conf.readConfigFile(conf_path.c_str());
216         if (!succeeded) {
217                 if (can_be_legacy) {
218                         // If map_meta.txt exists, it is probably an old minetest world
219                         if (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
220                                 return LEGACY_GAMEID;
221                 }
222                 return "";
223         }
224         if (!conf.exists("gameid"))
225                 return "";
226         // The "mesetint" gameid has been discarded
227         if (conf.get("gameid") == "mesetint")
228                 return "minetest";
229         return conf.get("gameid");
230 }
231
232 std::string getWorldPathEnv()
233 {
234         char *world_path = getenv("MINETEST_WORLD_PATH");
235         return world_path ? std::string(world_path) : "";
236 }
237
238 std::vector<WorldSpec> getAvailableWorlds()
239 {
240         std::vector<WorldSpec> worlds;
241         std::set<std::string> worldspaths;
242
243         Strfnd search_paths(getWorldPathEnv());
244
245         while (!search_paths.at_end())
246                 worldspaths.insert(search_paths.next(PATH_DELIM));
247
248         worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
249         infostream << "Searching worlds..." << std::endl;
250         for (const std::string &worldspath : worldspaths) {
251                 infostream << "  In " << worldspath << ": " << std::endl;
252                 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(worldspath);
253                 for (const fs::DirListNode &dln : dirvector) {
254                         if (!dln.dir)
255                                 continue;
256                         std::string fullpath = worldspath + DIR_DELIM + dln.name;
257                         std::string name = dln.name;
258                         // Just allow filling in the gameid always for now
259                         bool can_be_legacy = true;
260                         std::string gameid = getWorldGameId(fullpath, can_be_legacy);
261                         WorldSpec spec(fullpath, name, gameid);
262                         if (!spec.isValid()) {
263                                 infostream << "(invalid: " << name << ") ";
264                         } else {
265                                 infostream << name << " ";
266                                 worlds.push_back(spec);
267                         }
268                 }
269                 infostream << std::endl;
270         }
271         // Check old world location
272         do {
273                 std::string fullpath = porting::path_user + DIR_DELIM + "world";
274                 if (!fs::PathExists(fullpath))
275                         break;
276                 std::string name = "Old World";
277                 std::string gameid = getWorldGameId(fullpath, true);
278                 WorldSpec spec(fullpath, name, gameid);
279                 infostream << "Old world found." << std::endl;
280                 worlds.push_back(spec);
281         } while (false);
282         infostream << worlds.size() << " found." << std::endl;
283         return worlds;
284 }
285
286 bool loadGameConfAndInitWorld(const std::string &path, const SubgameSpec &gamespec)
287 {
288         // Override defaults with those provided by the game.
289         // We clear and reload the defaults because the defaults
290         // might have been overridden by other subgame config
291         // files that were loaded before.
292         g_settings->clearDefaults();
293         set_default_settings(g_settings);
294         Settings game_defaults;
295         getGameMinetestConfig(gamespec.path, game_defaults);
296         override_default_settings(g_settings, &game_defaults);
297
298         infostream << "Initializing world at " << path << std::endl;
299
300         fs::CreateAllDirs(path);
301
302         // Create world.mt if does not already exist
303         std::string worldmt_path = path + DIR_DELIM "world.mt";
304         if (!fs::PathExists(worldmt_path)) {
305                 Settings conf;
306
307                 conf.set("gameid", gamespec.id);
308                 conf.set("backend", "sqlite3");
309                 conf.set("player_backend", "sqlite3");
310                 conf.set("auth_backend", "sqlite3");
311                 conf.setBool("creative_mode", g_settings->getBool("creative_mode"));
312                 conf.setBool("enable_damage", g_settings->getBool("enable_damage"));
313
314                 if (!conf.updateConfigFile(worldmt_path.c_str()))
315                         return false;
316         }
317
318         // Create map_meta.txt if does not already exist
319         std::string map_meta_path = path + DIR_DELIM + "map_meta.txt";
320         if (!fs::PathExists(map_meta_path)) {
321                 verbosestream << "Creating map_meta.txt (" << map_meta_path << ")"
322                               << std::endl;
323                 fs::CreateAllDirs(path);
324                 std::ostringstream oss(std::ios_base::binary);
325
326                 Settings conf;
327                 MapgenParams params;
328
329                 params.readParams(g_settings);
330                 params.writeParams(&conf);
331                 conf.writeLines(oss);
332                 oss << "[end_of_params]\n";
333
334                 fs::safeWriteToFile(map_meta_path, oss.str());
335         }
336         return true;
337 }