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");
116 for (const std::string &mod_path : getEnvModPaths()) {
117 mods_paths.insert(mod_path);
121 std::string conf_path = game_path + DIR_DELIM + "game.conf";
123 conf.readConfigFile(conf_path.c_str());
125 std::string game_name;
126 if (conf.exists("name"))
127 game_name = conf.get("name");
131 std::string game_author;
132 if (conf.exists("author"))
133 game_author = conf.get("author");
135 int game_release = 0;
136 if (conf.exists("release"))
137 game_release = conf.getS32("release");
139 std::string menuicon_path;
141 menuicon_path = getImagePath(
142 game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
144 return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
145 menuicon_path, game_author, game_release);
148 SubgameSpec findWorldSubgame(const std::string &world_path)
150 std::string world_gameid = getWorldGameId(world_path, true);
151 // See if world contains an embedded game; if so, use it.
152 std::string world_gamepath = world_path + DIR_DELIM + "game";
153 if (fs::PathExists(world_gamepath)) {
154 SubgameSpec gamespec;
155 gamespec.id = world_gameid;
156 gamespec.path = world_gamepath;
157 gamespec.gamemods_path = world_gamepath + DIR_DELIM + "mods";
160 std::string conf_path = world_gamepath + DIR_DELIM + "game.conf";
161 conf.readConfigFile(conf_path.c_str());
163 if (conf.exists("name"))
164 gamespec.name = conf.get("name");
166 gamespec.name = world_gameid;
170 return findSubgame(world_gameid);
173 std::set<std::string> getAvailableGameIds()
175 std::set<std::string> gameids;
176 std::set<std::string> gamespaths;
177 gamespaths.insert(porting::path_share + DIR_DELIM + "games");
178 gamespaths.insert(porting::path_user + DIR_DELIM + "games");
180 Strfnd search_paths(getSubgamePathEnv());
182 while (!search_paths.at_end())
183 gamespaths.insert(search_paths.next(PATH_DELIM));
185 for (const std::string &gamespath : gamespaths) {
186 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(gamespath);
187 for (const fs::DirListNode &dln : dirlist) {
191 // If configuration file is not found or broken, ignore game
193 std::string conf_path = gamespath + DIR_DELIM + dln.name +
194 DIR_DELIM + "game.conf";
195 if (!conf.readConfigFile(conf_path.c_str()))
199 const char *ends[] = {"_game", NULL};
200 std::string shorter = removeStringEnd(dln.name, ends);
201 if (!shorter.empty())
202 gameids.insert(shorter);
204 gameids.insert(dln.name);
210 std::vector<SubgameSpec> getAvailableGames()
212 std::vector<SubgameSpec> specs;
213 std::set<std::string> gameids = getAvailableGameIds();
214 specs.reserve(gameids.size());
215 for (const auto &gameid : gameids)
216 specs.push_back(findSubgame(gameid));
220 #define LEGACY_GAMEID "minetest"
222 bool getWorldExists(const std::string &world_path)
224 return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
225 fs::PathExists(world_path + DIR_DELIM + "world.mt"));
228 //! Try to get the displayed name of a world
229 std::string getWorldName(const std::string &world_path, const std::string &default_name)
231 std::string conf_path = world_path + DIR_DELIM + "world.mt";
233 bool succeeded = conf.readConfigFile(conf_path.c_str());
238 if (!conf.exists("world_name"))
240 return conf.get("world_name");
243 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
245 std::string conf_path = world_path + DIR_DELIM + "world.mt";
247 bool succeeded = conf.readConfigFile(conf_path.c_str());
250 // If map_meta.txt exists, it is probably an old minetest world
251 if (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
252 return LEGACY_GAMEID;
256 if (!conf.exists("gameid"))
258 // The "mesetint" gameid has been discarded
259 if (conf.get("gameid") == "mesetint")
261 return conf.get("gameid");
264 std::string getWorldPathEnv()
266 char *world_path = getenv("MINETEST_WORLD_PATH");
267 return world_path ? std::string(world_path) : "";
270 std::vector<WorldSpec> getAvailableWorlds()
272 std::vector<WorldSpec> worlds;
273 std::set<std::string> worldspaths;
275 Strfnd search_paths(getWorldPathEnv());
277 while (!search_paths.at_end())
278 worldspaths.insert(search_paths.next(PATH_DELIM));
280 worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
281 infostream << "Searching worlds..." << std::endl;
282 for (const std::string &worldspath : worldspaths) {
283 infostream << " In " << worldspath << ": ";
284 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(worldspath);
285 for (const fs::DirListNode &dln : dirvector) {
288 std::string fullpath = worldspath + DIR_DELIM + dln.name;
289 std::string name = getWorldName(fullpath, dln.name);
290 // Just allow filling in the gameid always for now
291 bool can_be_legacy = true;
292 std::string gameid = getWorldGameId(fullpath, can_be_legacy);
293 WorldSpec spec(fullpath, name, gameid);
294 if (!spec.isValid()) {
295 infostream << "(invalid: " << name << ") ";
297 infostream << name << " ";
298 worlds.push_back(spec);
301 infostream << std::endl;
303 // Check old world location
305 std::string fullpath = porting::path_user + DIR_DELIM + "world";
306 if (!fs::PathExists(fullpath))
308 std::string name = "Old World";
309 std::string gameid = getWorldGameId(fullpath, true);
310 WorldSpec spec(fullpath, name, gameid);
311 infostream << "Old world found." << std::endl;
312 worlds.push_back(spec);
314 infostream << worlds.size() << " found." << std::endl;
318 void loadGameConfAndInitWorld(const std::string &path, const std::string &name,
319 const SubgameSpec &gamespec, bool create_world)
321 std::string final_path = path;
323 // If we're creating a new world, ensure that the path isn't already taken
326 while (fs::PathExists(final_path) && counter < MAX_WORLD_NAMES) {
327 final_path = path + "_" + std::to_string(counter);
331 if (fs::PathExists(final_path)) {
332 throw BaseException("Too many similar filenames");
336 Settings *game_settings = Settings::getLayer(SL_GAME);
337 const bool new_game_settings = (game_settings == nullptr);
338 if (new_game_settings) {
339 // Called by main-menu without a Server instance running
340 // -> create and free manually
341 game_settings = Settings::createLayer(SL_GAME);
344 getGameMinetestConfig(gamespec.path, *game_settings);
345 game_settings->removeSecureSettings();
347 infostream << "Initializing world at " << final_path << std::endl;
349 fs::CreateAllDirs(final_path);
351 // Create world.mt if does not already exist
352 std::string worldmt_path = final_path + DIR_DELIM "world.mt";
353 if (!fs::PathExists(worldmt_path)) {
356 conf.set("world_name", name);
357 conf.set("gameid", gamespec.id);
358 conf.set("backend", "sqlite3");
359 conf.set("player_backend", "sqlite3");
360 conf.set("auth_backend", "sqlite3");
361 conf.setBool("creative_mode", g_settings->getBool("creative_mode"));
362 conf.setBool("enable_damage", g_settings->getBool("enable_damage"));
364 if (!conf.updateConfigFile(worldmt_path.c_str())) {
365 throw BaseException("Failed to update the config file");
369 // Create map_meta.txt if does not already exist
370 std::string map_meta_path = final_path + DIR_DELIM + "map_meta.txt";
371 if (!fs::PathExists(map_meta_path)) {
372 verbosestream << "Creating map_meta.txt (" << map_meta_path << ")"
374 std::ostringstream oss(std::ios_base::binary);
379 params.readParams(g_settings);
380 params.writeParams(&conf);
381 conf.writeLines(oss);
382 oss << "[end_of_params]\n";
384 fs::safeWriteToFile(map_meta_path, oss.str());
387 // The Settings object is no longer needed for created worlds
388 if (new_game_settings)
389 delete game_settings;
392 std::vector<std::string> getEnvModPaths()
394 const char *c_mod_path = getenv("MINETEST_MOD_PATH");
395 std::vector<std::string> paths;
396 Strfnd search_paths(c_mod_path ? c_mod_path : "");
397 while (!search_paths.at_end())
398 paths.push_back(search_paths.next(PATH_DELIM));