3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "content/subgames.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"
31 #include "client/tile.h" // getImagePath
34 // The maximum number of identical world names allowed
35 #define MAX_WORLD_NAMES 100
40 bool getGameMinetestConfig(const std::string &game_path, Settings &conf)
42 std::string conf_path = game_path + DIR_DELIM + "minetest.conf";
43 return conf.readConfigFile(conf_path.c_str());
52 GameFindPath(const std::string &path, bool user_specific) :
53 path(path), user_specific(user_specific)
58 std::string getSubgamePathEnv()
60 char *subgame_path = getenv("MINETEST_SUBGAME_PATH");
61 return subgame_path ? std::string(subgame_path) : "";
64 SubgameSpec findSubgame(const std::string &id)
68 std::string share = porting::path_share;
69 std::string user = porting::path_user;
71 // Get games install locations
72 Strfnd search_paths(getSubgamePathEnv());
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);
81 find_paths.emplace_back(path, false);
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);
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)) {
99 user_game = find_path.user_specific;
104 if (game_path.empty())
105 return SubgameSpec();
107 std::string gamemod_path = game_path + DIR_DELIM + "mods";
109 // Find mod directories
110 std::set<std::string> mods_paths;
112 mods_paths.insert(share + DIR_DELIM + "mods");
113 if (user != share || user_game)
114 mods_paths.insert(user + DIR_DELIM + "mods");
117 std::string conf_path = game_path + DIR_DELIM + "game.conf";
119 conf.readConfigFile(conf_path.c_str());
121 std::string game_name;
122 if (conf.exists("name"))
123 game_name = conf.get("name");
127 std::string game_author;
128 if (conf.exists("author"))
129 game_author = conf.get("author");
131 int game_release = 0;
132 if (conf.exists("release"))
133 game_release = conf.getS32("release");
135 std::string menuicon_path;
137 menuicon_path = getImagePath(
138 game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
140 return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
141 menuicon_path, game_author, game_release);
144 SubgameSpec findWorldSubgame(const std::string &world_path)
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";
156 std::string conf_path = world_gamepath + DIR_DELIM + "game.conf";
157 conf.readConfigFile(conf_path.c_str());
159 if (conf.exists("name"))
160 gamespec.name = conf.get("name");
162 gamespec.name = world_gameid;
166 return findSubgame(world_gameid);
169 std::set<std::string> getAvailableGameIds()
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");
176 Strfnd search_paths(getSubgamePathEnv());
178 while (!search_paths.at_end())
179 gamespaths.insert(search_paths.next(PATH_DELIM));
181 for (const std::string &gamespath : gamespaths) {
182 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(gamespath);
183 for (const fs::DirListNode &dln : dirlist) {
187 // If configuration file is not found or broken, ignore game
189 std::string conf_path = gamespath + DIR_DELIM + dln.name +
190 DIR_DELIM + "game.conf";
191 if (!conf.readConfigFile(conf_path.c_str()))
195 const char *ends[] = {"_game", NULL};
196 std::string shorter = removeStringEnd(dln.name, ends);
197 if (!shorter.empty())
198 gameids.insert(shorter);
200 gameids.insert(dln.name);
206 std::vector<SubgameSpec> getAvailableGames()
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));
216 #define LEGACY_GAMEID "minetest"
218 bool getWorldExists(const std::string &world_path)
220 return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
221 fs::PathExists(world_path + DIR_DELIM + "world.mt"));
224 //! Try to get the displayed name of a world
225 std::string getWorldName(const std::string &world_path, const std::string &default_name)
227 std::string conf_path = world_path + DIR_DELIM + "world.mt";
229 bool succeeded = conf.readConfigFile(conf_path.c_str());
234 if (!conf.exists("world_name"))
236 return conf.get("world_name");
239 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
241 std::string conf_path = world_path + DIR_DELIM + "world.mt";
243 bool succeeded = conf.readConfigFile(conf_path.c_str());
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;
252 if (!conf.exists("gameid"))
254 // The "mesetint" gameid has been discarded
255 if (conf.get("gameid") == "mesetint")
257 return conf.get("gameid");
260 std::string getWorldPathEnv()
262 char *world_path = getenv("MINETEST_WORLD_PATH");
263 return world_path ? std::string(world_path) : "";
266 std::vector<WorldSpec> getAvailableWorlds()
268 std::vector<WorldSpec> worlds;
269 std::set<std::string> worldspaths;
271 Strfnd search_paths(getWorldPathEnv());
273 while (!search_paths.at_end())
274 worldspaths.insert(search_paths.next(PATH_DELIM));
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) {
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 << ") ";
293 infostream << name << " ";
294 worlds.push_back(spec);
297 infostream << std::endl;
299 // Check old world location
301 std::string fullpath = porting::path_user + DIR_DELIM + "world";
302 if (!fs::PathExists(fullpath))
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);
310 infostream << worlds.size() << " found." << std::endl;
314 void loadGameConfAndInitWorld(const std::string &path, const std::string &name,
315 const SubgameSpec &gamespec, bool create_world)
317 std::string final_path = path;
319 // If we're creating a new world, ensure that the path isn't already taken
322 while (fs::PathExists(final_path) && counter < MAX_WORLD_NAMES) {
323 final_path = path + "_" + std::to_string(counter);
327 if (fs::PathExists(final_path)) {
328 throw BaseException("Too many similar filenames");
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);
339 Settings game_defaults;
340 getGameMinetestConfig(gamespec.path, game_defaults);
341 game_defaults.removeSecureSettings();
343 g_settings->overrideDefaults(&game_defaults);
345 infostream << "Initializing world at " << final_path << std::endl;
347 fs::CreateAllDirs(final_path);
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)) {
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"));
362 if (!conf.updateConfigFile(worldmt_path.c_str())) {
363 throw BaseException("Failed to update the config file");
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 << ")"
372 std::ostringstream oss(std::ios_base::binary);
377 params.readParams(g_settings);
378 params.writeParams(&conf);
379 conf.writeLines(oss);
380 oss << "[end_of_params]\n";
382 fs::safeWriteToFile(map_meta_path, oss.str());