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 int game_release = 0;
120 if (conf.exists("release"))
121 game_release = conf.getS32("release");
123 std::string menuicon_path;
125 menuicon_path = getImagePath(
126 game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
128 return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
129 menuicon_path, game_author, game_release);
132 SubgameSpec findWorldSubgame(const std::string &world_path)
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";
144 std::string conf_path = world_gamepath + DIR_DELIM + "game.conf";
145 conf.readConfigFile(conf_path.c_str());
147 if (conf.exists("name"))
148 gamespec.name = conf.get("name");
150 gamespec.name = world_gameid;
154 return findSubgame(world_gameid);
157 std::set<std::string> getAvailableGameIds()
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");
164 Strfnd search_paths(getSubgamePathEnv());
166 while (!search_paths.at_end())
167 gamespaths.insert(search_paths.next(PATH_DELIM));
169 for (const std::string &gamespath : gamespaths) {
170 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(gamespath);
171 for (const fs::DirListNode &dln : dirlist) {
175 // If configuration file is not found or broken, ignore game
177 std::string conf_path = gamespath + DIR_DELIM + dln.name +
178 DIR_DELIM + "game.conf";
179 if (!conf.readConfigFile(conf_path.c_str()))
183 const char *ends[] = {"_game", NULL};
184 std::string shorter = removeStringEnd(dln.name, ends);
185 if (!shorter.empty())
186 gameids.insert(shorter);
188 gameids.insert(dln.name);
194 std::vector<SubgameSpec> getAvailableGames()
196 std::vector<SubgameSpec> specs;
197 std::set<std::string> gameids = getAvailableGameIds();
198 for (const auto &gameid : gameids)
199 specs.push_back(findSubgame(gameid));
203 #define LEGACY_GAMEID "minetest"
205 bool getWorldExists(const std::string &world_path)
207 return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
208 fs::PathExists(world_path + DIR_DELIM + "world.mt"));
211 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
213 std::string conf_path = world_path + DIR_DELIM + "world.mt";
215 bool succeeded = conf.readConfigFile(conf_path.c_str());
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;
224 if (!conf.exists("gameid"))
226 // The "mesetint" gameid has been discarded
227 if (conf.get("gameid") == "mesetint")
229 return conf.get("gameid");
232 std::string getWorldPathEnv()
234 char *world_path = getenv("MINETEST_WORLD_PATH");
235 return world_path ? std::string(world_path) : "";
238 std::vector<WorldSpec> getAvailableWorlds()
240 std::vector<WorldSpec> worlds;
241 std::set<std::string> worldspaths;
243 Strfnd search_paths(getWorldPathEnv());
245 while (!search_paths.at_end())
246 worldspaths.insert(search_paths.next(PATH_DELIM));
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) {
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 << ") ";
265 infostream << name << " ";
266 worlds.push_back(spec);
269 infostream << std::endl;
271 // Check old world location
273 std::string fullpath = porting::path_user + DIR_DELIM + "world";
274 if (!fs::PathExists(fullpath))
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);
282 infostream << worlds.size() << " found." << std::endl;
286 bool loadGameConfAndInitWorld(const std::string &path, const SubgameSpec &gamespec)
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);
298 infostream << "Initializing world at " << path << std::endl;
300 fs::CreateAllDirs(path);
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)) {
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"));
314 if (!conf.updateConfigFile(worldmt_path.c_str()))
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 << ")"
323 fs::CreateAllDirs(path);
324 std::ostringstream oss(std::ios_base::binary);
329 params.readParams(g_settings);
330 params.writeParams(&conf);
331 conf.writeLines(oss);
332 oss << "[end_of_params]\n";
334 fs::safeWriteToFile(map_meta_path, oss.str());