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 override_default_settings
27 #include "mapgen/mapgen.h" // for MapgenParams
28 #include "util/string.h"
31 #include "client/tile.h" // getImagePath
34 bool getGameMinetestConfig(const std::string &game_path, Settings &conf)
36 std::string conf_path = game_path + DIR_DELIM + "minetest.conf";
37 return conf.readConfigFile(conf_path.c_str());
44 GameFindPath(const std::string &path, bool user_specific) :
45 path(path), user_specific(user_specific)
50 std::string getSubgamePathEnv()
52 char *subgame_path = getenv("MINETEST_SUBGAME_PATH");
53 return subgame_path ? std::string(subgame_path) : "";
56 SubgameSpec findSubgame(const std::string &id)
60 std::string share = porting::path_share;
61 std::string user = porting::path_user;
63 // Get games install locations
64 Strfnd search_paths(getSubgamePathEnv());
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 path.append(DIR_DELIM).append(id);
71 find_paths.emplace_back(path, false);
73 find_paths.emplace_back(path, false);
76 std::string game_base = DIR_DELIM;
77 game_base = game_base.append("games").append(DIR_DELIM).append(id);
78 std::string game_suffixed = game_base + "_game";
79 find_paths.emplace_back(user + game_suffixed, true);
80 find_paths.emplace_back(user + game_base, true);
81 find_paths.emplace_back(share + game_suffixed, false);
82 find_paths.emplace_back(share + game_base, false);
84 // Find game directory
85 std::string game_path;
86 bool user_game = true; // Game is in user's directory
87 for (const GameFindPath &find_path : find_paths) {
88 const std::string &try_path = find_path.path;
89 if (fs::PathExists(try_path)) {
91 user_game = find_path.user_specific;
96 if (game_path.empty())
99 std::string gamemod_path = game_path + DIR_DELIM + "mods";
101 // Find mod directories
102 std::set<std::string> mods_paths;
104 mods_paths.insert(share + DIR_DELIM + "mods");
105 if (user != share || user_game)
106 mods_paths.insert(user + DIR_DELIM + "mods");
109 std::string conf_path = game_path + DIR_DELIM + "game.conf";
111 conf.readConfigFile(conf_path.c_str());
113 std::string game_name;
114 if (conf.exists("name"))
115 game_name = conf.get("name");
119 std::string game_author;
120 if (conf.exists("author"))
121 game_author = conf.get("author");
123 int game_release = 0;
124 if (conf.exists("release"))
125 game_release = conf.getS32("release");
127 std::string menuicon_path;
129 menuicon_path = getImagePath(
130 game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
132 return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
133 menuicon_path, game_author, game_release);
136 SubgameSpec findWorldSubgame(const std::string &world_path)
138 std::string world_gameid = getWorldGameId(world_path, true);
139 // See if world contains an embedded game; if so, use it.
140 std::string world_gamepath = world_path + DIR_DELIM + "game";
141 if (fs::PathExists(world_gamepath)) {
142 SubgameSpec gamespec;
143 gamespec.id = world_gameid;
144 gamespec.path = world_gamepath;
145 gamespec.gamemods_path = world_gamepath + DIR_DELIM + "mods";
148 std::string conf_path = world_gamepath + DIR_DELIM + "game.conf";
149 conf.readConfigFile(conf_path.c_str());
151 if (conf.exists("name"))
152 gamespec.name = conf.get("name");
154 gamespec.name = world_gameid;
158 return findSubgame(world_gameid);
161 std::set<std::string> getAvailableGameIds()
163 std::set<std::string> gameids;
164 std::set<std::string> gamespaths;
165 gamespaths.insert(porting::path_share + DIR_DELIM + "games");
166 gamespaths.insert(porting::path_user + DIR_DELIM + "games");
168 Strfnd search_paths(getSubgamePathEnv());
170 while (!search_paths.at_end())
171 gamespaths.insert(search_paths.next(PATH_DELIM));
173 for (const std::string &gamespath : gamespaths) {
174 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(gamespath);
175 for (const fs::DirListNode &dln : dirlist) {
179 // If configuration file is not found or broken, ignore game
181 std::string conf_path = gamespath + DIR_DELIM + dln.name +
182 DIR_DELIM + "game.conf";
183 if (!conf.readConfigFile(conf_path.c_str()))
187 const char *ends[] = {"_game", NULL};
188 std::string shorter = removeStringEnd(dln.name, ends);
189 if (!shorter.empty())
190 gameids.insert(shorter);
192 gameids.insert(dln.name);
198 std::vector<SubgameSpec> getAvailableGames()
200 std::vector<SubgameSpec> specs;
201 std::set<std::string> gameids = getAvailableGameIds();
202 specs.reserve(gameids.size());
203 for (const auto &gameid : gameids)
204 specs.push_back(findSubgame(gameid));
208 #define LEGACY_GAMEID "minetest"
210 bool getWorldExists(const std::string &world_path)
212 return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
213 fs::PathExists(world_path + DIR_DELIM + "world.mt"));
216 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
218 std::string conf_path = world_path + DIR_DELIM + "world.mt";
220 bool succeeded = conf.readConfigFile(conf_path.c_str());
223 // If map_meta.txt exists, it is probably an old minetest world
224 if (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
225 return LEGACY_GAMEID;
229 if (!conf.exists("gameid"))
231 // The "mesetint" gameid has been discarded
232 if (conf.get("gameid") == "mesetint")
234 return conf.get("gameid");
237 std::string getWorldPathEnv()
239 char *world_path = getenv("MINETEST_WORLD_PATH");
240 return world_path ? std::string(world_path) : "";
243 std::vector<WorldSpec> getAvailableWorlds()
245 std::vector<WorldSpec> worlds;
246 std::set<std::string> worldspaths;
248 Strfnd search_paths(getWorldPathEnv());
250 while (!search_paths.at_end())
251 worldspaths.insert(search_paths.next(PATH_DELIM));
253 worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
254 infostream << "Searching worlds..." << std::endl;
255 for (const std::string &worldspath : worldspaths) {
256 infostream << " In " << worldspath << ": " << std::endl;
257 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(worldspath);
258 for (const fs::DirListNode &dln : dirvector) {
261 std::string fullpath = worldspath + DIR_DELIM + dln.name;
262 std::string name = dln.name;
263 // Just allow filling in the gameid always for now
264 bool can_be_legacy = true;
265 std::string gameid = getWorldGameId(fullpath, can_be_legacy);
266 WorldSpec spec(fullpath, name, gameid);
267 if (!spec.isValid()) {
268 infostream << "(invalid: " << name << ") ";
270 infostream << name << " ";
271 worlds.push_back(spec);
274 infostream << std::endl;
276 // Check old world location
278 std::string fullpath = porting::path_user + DIR_DELIM + "world";
279 if (!fs::PathExists(fullpath))
281 std::string name = "Old World";
282 std::string gameid = getWorldGameId(fullpath, true);
283 WorldSpec spec(fullpath, name, gameid);
284 infostream << "Old world found." << std::endl;
285 worlds.push_back(spec);
287 infostream << worlds.size() << " found." << std::endl;
291 bool loadGameConfAndInitWorld(const std::string &path, const SubgameSpec &gamespec)
293 // Override defaults with those provided by the game.
294 // We clear and reload the defaults because the defaults
295 // might have been overridden by other subgame config
296 // files that were loaded before.
297 g_settings->clearDefaults();
298 set_default_settings(g_settings);
299 Settings game_defaults;
300 getGameMinetestConfig(gamespec.path, game_defaults);
301 override_default_settings(g_settings, &game_defaults);
303 infostream << "Initializing world at " << path << std::endl;
305 fs::CreateAllDirs(path);
307 // Create world.mt if does not already exist
308 std::string worldmt_path = path + DIR_DELIM "world.mt";
309 if (!fs::PathExists(worldmt_path)) {
312 conf.set("gameid", gamespec.id);
313 conf.set("backend", "sqlite3");
314 conf.set("player_backend", "sqlite3");
315 conf.set("auth_backend", "sqlite3");
316 conf.setBool("creative_mode", g_settings->getBool("creative_mode"));
317 conf.setBool("enable_damage", g_settings->getBool("enable_damage"));
319 if (!conf.updateConfigFile(worldmt_path.c_str()))
323 // Create map_meta.txt if does not already exist
324 std::string map_meta_path = path + DIR_DELIM + "map_meta.txt";
325 if (!fs::PathExists(map_meta_path)) {
326 verbosestream << "Creating map_meta.txt (" << map_meta_path << ")"
328 fs::CreateAllDirs(path);
329 std::ostringstream oss(std::ios_base::binary);
334 params.readParams(g_settings);
335 params.writeParams(&conf);
336 conf.writeLines(oss);
337 oss << "[end_of_params]\n";
339 fs::safeWriteToFile(map_meta_path, oss.str());