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 find_paths.emplace_back(path + DIR_DELIM + id, false);
71 find_paths.emplace_back(path + DIR_DELIM + id + "_game", false);
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);
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)) {
87 user_game = find_path.user_specific;
92 if (game_path.empty())
95 std::string gamemod_path = game_path + DIR_DELIM + "mods";
97 // Find mod directories
98 std::set<std::string> mods_paths;
100 mods_paths.insert(share + DIR_DELIM + "mods");
101 if (user != share || user_game)
102 mods_paths.insert(user + DIR_DELIM + "mods");
105 std::string conf_path = game_path + DIR_DELIM + "game.conf";
107 conf.readConfigFile(conf_path.c_str());
109 std::string game_name;
110 if (conf.exists("name"))
111 game_name = conf.get("name");
115 std::string game_author;
116 if (conf.exists("author"))
117 game_author = conf.get("author");
119 std::string menuicon_path;
121 menuicon_path = getImagePath(
122 game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
124 return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
125 menuicon_path, game_author);
128 SubgameSpec findWorldSubgame(const std::string &world_path)
130 std::string world_gameid = getWorldGameId(world_path, true);
131 // See if world contains an embedded game; if so, use it.
132 std::string world_gamepath = world_path + DIR_DELIM + "game";
133 if (fs::PathExists(world_gamepath)) {
134 SubgameSpec gamespec;
135 gamespec.id = world_gameid;
136 gamespec.path = world_gamepath;
137 gamespec.gamemods_path = world_gamepath + DIR_DELIM + "mods";
140 std::string conf_path = world_gamepath + DIR_DELIM + "game.conf";
141 conf.readConfigFile(conf_path.c_str());
143 if (conf.exists("name"))
144 gamespec.name = conf.get("name");
146 gamespec.name = world_gameid;
150 return findSubgame(world_gameid);
153 std::set<std::string> getAvailableGameIds()
155 std::set<std::string> gameids;
156 std::set<std::string> gamespaths;
157 gamespaths.insert(porting::path_share + DIR_DELIM + "games");
158 gamespaths.insert(porting::path_user + DIR_DELIM + "games");
160 Strfnd search_paths(getSubgamePathEnv());
162 while (!search_paths.at_end())
163 gamespaths.insert(search_paths.next(PATH_DELIM));
165 for (const std::string &gamespath : gamespaths) {
166 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(gamespath);
167 for (const fs::DirListNode &dln : dirlist) {
171 // If configuration file is not found or broken, ignore game
173 std::string conf_path = gamespath + DIR_DELIM + dln.name +
174 DIR_DELIM + "game.conf";
175 if (!conf.readConfigFile(conf_path.c_str()))
179 const char *ends[] = {"_game", NULL};
180 std::string shorter = removeStringEnd(dln.name, ends);
181 if (!shorter.empty())
182 gameids.insert(shorter);
184 gameids.insert(dln.name);
190 std::vector<SubgameSpec> getAvailableGames()
192 std::vector<SubgameSpec> specs;
193 std::set<std::string> gameids = getAvailableGameIds();
194 for (const auto &gameid : gameids)
195 specs.push_back(findSubgame(gameid));
199 #define LEGACY_GAMEID "minetest"
201 bool getWorldExists(const std::string &world_path)
203 return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
204 fs::PathExists(world_path + DIR_DELIM + "world.mt"));
207 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
209 std::string conf_path = world_path + DIR_DELIM + "world.mt";
211 bool succeeded = conf.readConfigFile(conf_path.c_str());
214 // If map_meta.txt exists, it is probably an old minetest world
215 if (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
216 return LEGACY_GAMEID;
220 if (!conf.exists("gameid"))
222 // The "mesetint" gameid has been discarded
223 if (conf.get("gameid") == "mesetint")
225 return conf.get("gameid");
228 std::string getWorldPathEnv()
230 char *world_path = getenv("MINETEST_WORLD_PATH");
231 return world_path ? std::string(world_path) : "";
234 std::vector<WorldSpec> getAvailableWorlds()
236 std::vector<WorldSpec> worlds;
237 std::set<std::string> worldspaths;
239 Strfnd search_paths(getWorldPathEnv());
241 while (!search_paths.at_end())
242 worldspaths.insert(search_paths.next(PATH_DELIM));
244 worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
245 infostream << "Searching worlds..." << std::endl;
246 for (const std::string &worldspath : worldspaths) {
247 infostream << " In " << worldspath << ": " << std::endl;
248 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(worldspath);
249 for (const fs::DirListNode &dln : dirvector) {
252 std::string fullpath = worldspath + DIR_DELIM + dln.name;
253 std::string name = dln.name;
254 // Just allow filling in the gameid always for now
255 bool can_be_legacy = true;
256 std::string gameid = getWorldGameId(fullpath, can_be_legacy);
257 WorldSpec spec(fullpath, name, gameid);
258 if (!spec.isValid()) {
259 infostream << "(invalid: " << name << ") ";
261 infostream << name << " ";
262 worlds.push_back(spec);
265 infostream << std::endl;
267 // Check old world location
269 std::string fullpath = porting::path_user + DIR_DELIM + "world";
270 if (!fs::PathExists(fullpath))
272 std::string name = "Old World";
273 std::string gameid = getWorldGameId(fullpath, true);
274 WorldSpec spec(fullpath, name, gameid);
275 infostream << "Old world found." << std::endl;
276 worlds.push_back(spec);
278 infostream << worlds.size() << " found." << std::endl;
282 bool loadGameConfAndInitWorld(const std::string &path, const SubgameSpec &gamespec)
284 // Override defaults with those provided by the game.
285 // We clear and reload the defaults because the defaults
286 // might have been overridden by other subgame config
287 // files that were loaded before.
288 g_settings->clearDefaults();
289 set_default_settings(g_settings);
290 Settings game_defaults;
291 getGameMinetestConfig(gamespec.path, game_defaults);
292 override_default_settings(g_settings, &game_defaults);
294 infostream << "Initializing world at " << path << std::endl;
296 fs::CreateAllDirs(path);
298 // Create world.mt if does not already exist
299 std::string worldmt_path = path + DIR_DELIM "world.mt";
300 if (!fs::PathExists(worldmt_path)) {
303 conf.set("gameid", gamespec.id);
304 conf.set("backend", "sqlite3");
305 conf.set("player_backend", "sqlite3");
306 conf.setBool("creative_mode", g_settings->getBool("creative_mode"));
307 conf.setBool("enable_damage", g_settings->getBool("enable_damage"));
309 if (!conf.updateConfigFile(worldmt_path.c_str()))
313 // Create map_meta.txt if does not already exist
314 std::string map_meta_path = path + DIR_DELIM + "map_meta.txt";
315 if (!fs::PathExists(map_meta_path)) {
316 verbosestream << "Creating map_meta.txt (" << map_meta_path << ")"
318 fs::CreateAllDirs(path);
319 std::ostringstream oss(std::ios_base::binary);
324 params.readParams(g_settings);
325 params.writeParams(&conf);
326 conf.writeLines(oss);
327 oss << "[end_of_params]\n";
329 fs::safeWriteToFile(map_meta_path, oss.str());