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 "map_settings_manager.h"
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::unordered_map<std::string, std::string> mods_paths;
111 mods_paths["mods"] = user + DIR_DELIM + "mods";
112 if (!user_game && user != share)
113 mods_paths["share"] = share + DIR_DELIM + "mods";
115 for (const std::string &mod_path : getEnvModPaths()) {
116 mods_paths[fs::AbsolutePath(mod_path)] = mod_path;
120 std::string conf_path = game_path + DIR_DELIM + "game.conf";
122 conf.readConfigFile(conf_path.c_str());
124 std::string game_name;
125 if (conf.exists("name"))
126 game_name = conf.get("name");
130 std::string game_author;
131 if (conf.exists("author"))
132 game_author = conf.get("author");
134 int game_release = 0;
135 if (conf.exists("release"))
136 game_release = conf.getS32("release");
138 std::string menuicon_path;
140 menuicon_path = getImagePath(
141 game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
143 return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
144 menuicon_path, game_author, game_release);
147 SubgameSpec findWorldSubgame(const std::string &world_path)
149 std::string world_gameid = getWorldGameId(world_path, true);
150 // See if world contains an embedded game; if so, use it.
151 std::string world_gamepath = world_path + DIR_DELIM + "game";
152 if (fs::PathExists(world_gamepath)) {
153 SubgameSpec gamespec;
154 gamespec.id = world_gameid;
155 gamespec.path = world_gamepath;
156 gamespec.gamemods_path = world_gamepath + DIR_DELIM + "mods";
159 std::string conf_path = world_gamepath + DIR_DELIM + "game.conf";
160 conf.readConfigFile(conf_path.c_str());
162 if (conf.exists("name"))
163 gamespec.name = conf.get("name");
165 gamespec.name = world_gameid;
169 return findSubgame(world_gameid);
172 std::set<std::string> getAvailableGameIds()
174 std::set<std::string> gameids;
175 std::set<std::string> gamespaths;
176 gamespaths.insert(porting::path_share + DIR_DELIM + "games");
177 gamespaths.insert(porting::path_user + DIR_DELIM + "games");
179 Strfnd search_paths(getSubgamePathEnv());
181 while (!search_paths.at_end())
182 gamespaths.insert(search_paths.next(PATH_DELIM));
184 for (const std::string &gamespath : gamespaths) {
185 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(gamespath);
186 for (const fs::DirListNode &dln : dirlist) {
190 // If configuration file is not found or broken, ignore game
192 std::string conf_path = gamespath + DIR_DELIM + dln.name +
193 DIR_DELIM + "game.conf";
194 if (!conf.readConfigFile(conf_path.c_str()))
198 const char *ends[] = {"_game", NULL};
199 std::string shorter = removeStringEnd(dln.name, ends);
200 if (!shorter.empty())
201 gameids.insert(shorter);
203 gameids.insert(dln.name);
209 std::vector<SubgameSpec> getAvailableGames()
211 std::vector<SubgameSpec> specs;
212 std::set<std::string> gameids = getAvailableGameIds();
213 specs.reserve(gameids.size());
214 for (const auto &gameid : gameids)
215 specs.push_back(findSubgame(gameid));
219 #define LEGACY_GAMEID "minetest"
221 bool getWorldExists(const std::string &world_path)
223 return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
224 fs::PathExists(world_path + DIR_DELIM + "world.mt"));
227 //! Try to get the displayed name of a world
228 std::string getWorldName(const std::string &world_path, const std::string &default_name)
230 std::string conf_path = world_path + DIR_DELIM + "world.mt";
232 bool succeeded = conf.readConfigFile(conf_path.c_str());
237 if (!conf.exists("world_name"))
239 return conf.get("world_name");
242 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
244 std::string conf_path = world_path + DIR_DELIM + "world.mt";
246 bool succeeded = conf.readConfigFile(conf_path.c_str());
249 // If map_meta.txt exists, it is probably an old minetest world
250 if (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
251 return LEGACY_GAMEID;
255 if (!conf.exists("gameid"))
257 // The "mesetint" gameid has been discarded
258 if (conf.get("gameid") == "mesetint")
260 return conf.get("gameid");
263 std::string getWorldPathEnv()
265 char *world_path = getenv("MINETEST_WORLD_PATH");
266 return world_path ? std::string(world_path) : "";
269 std::vector<WorldSpec> getAvailableWorlds()
271 std::vector<WorldSpec> worlds;
272 std::set<std::string> worldspaths;
274 Strfnd search_paths(getWorldPathEnv());
276 while (!search_paths.at_end())
277 worldspaths.insert(search_paths.next(PATH_DELIM));
279 worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
280 infostream << "Searching worlds..." << std::endl;
281 for (const std::string &worldspath : worldspaths) {
282 infostream << " In " << worldspath << ": ";
283 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(worldspath);
284 for (const fs::DirListNode &dln : dirvector) {
287 std::string fullpath = worldspath + DIR_DELIM + dln.name;
288 std::string name = getWorldName(fullpath, dln.name);
289 // Just allow filling in the gameid always for now
290 bool can_be_legacy = true;
291 std::string gameid = getWorldGameId(fullpath, can_be_legacy);
292 WorldSpec spec(fullpath, name, gameid);
293 if (!spec.isValid()) {
294 infostream << "(invalid: " << name << ") ";
296 infostream << name << " ";
297 worlds.push_back(spec);
300 infostream << std::endl;
302 // Check old world location
304 std::string fullpath = porting::path_user + DIR_DELIM + "world";
305 if (!fs::PathExists(fullpath))
307 std::string name = "Old World";
308 std::string gameid = getWorldGameId(fullpath, true);
309 WorldSpec spec(fullpath, name, gameid);
310 infostream << "Old world found." << std::endl;
311 worlds.push_back(spec);
313 infostream << worlds.size() << " found." << std::endl;
317 void loadGameConfAndInitWorld(const std::string &path, const std::string &name,
318 const SubgameSpec &gamespec, bool create_world)
320 std::string final_path = path;
322 // If we're creating a new world, ensure that the path isn't already taken
325 while (fs::PathExists(final_path) && counter < MAX_WORLD_NAMES) {
326 final_path = path + "_" + std::to_string(counter);
330 if (fs::PathExists(final_path)) {
331 throw BaseException("Too many similar filenames");
335 Settings *game_settings = Settings::getLayer(SL_GAME);
336 const bool new_game_settings = (game_settings == nullptr);
337 if (new_game_settings) {
338 // Called by main-menu without a Server instance running
339 // -> create and free manually
340 game_settings = Settings::createLayer(SL_GAME);
343 getGameMinetestConfig(gamespec.path, *game_settings);
344 game_settings->removeSecureSettings();
346 infostream << "Initializing world at " << final_path << std::endl;
348 fs::CreateAllDirs(final_path);
350 // Create world.mt if does not already exist
351 std::string worldmt_path = final_path + DIR_DELIM "world.mt";
352 if (!fs::PathExists(worldmt_path)) {
355 conf.set("world_name", name);
356 conf.set("gameid", gamespec.id);
357 conf.set("backend", "sqlite3");
358 conf.set("player_backend", "sqlite3");
359 conf.set("auth_backend", "sqlite3");
360 conf.set("mod_storage_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 MapSettingsManager mgr(map_meta_path);
374 mgr.setMapSetting("seed", g_settings->get("fixed_map_seed"));
376 mgr.makeMapgenParams();
380 // The Settings object is no longer needed for created worlds
381 if (new_game_settings)
382 delete game_settings;
385 std::vector<std::string> getEnvModPaths()
387 const char *c_mod_path = getenv("MINETEST_MOD_PATH");
388 std::vector<std::string> paths;
389 Strfnd search_paths(c_mod_path ? c_mod_path : "");
390 while (!search_paths.at_end())
391 paths.push_back(search_paths.next(PATH_DELIM));