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.
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());
40 bool getGameConfig(const std::string &game_path, Settings &conf)
42 std::string conf_path = game_path + DIR_DELIM + "game.conf";
43 return conf.readConfigFile(conf_path.c_str());
46 std::string getGameName(const std::string &game_path)
49 if(!getGameConfig(game_path, conf))
51 if(!conf.exists("name"))
53 return conf.get("name");
60 GameFindPath(const std::string &path, bool user_specific):
62 user_specific(user_specific)
66 std::string getSubgamePathEnv()
68 char *subgame_path = getenv("MINETEST_SUBGAME_PATH");
69 return subgame_path ? std::string(subgame_path) : "";
72 SubgameSpec findSubgame(const std::string &id)
76 std::string share = porting::path_share;
77 std::string user = porting::path_user;
78 std::vector<GameFindPath> find_paths;
80 Strfnd search_paths(getSubgamePathEnv());
82 while (!search_paths.at_end()) {
83 std::string path = search_paths.next(PATH_DELIM);
84 find_paths.emplace_back(path + DIR_DELIM + id, false);
85 find_paths.emplace_back(path + DIR_DELIM + id + "_game", false);
88 find_paths.emplace_back(user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true);
89 find_paths.emplace_back(user + DIR_DELIM + "games" + DIR_DELIM + id, true);
90 find_paths.emplace_back(share + DIR_DELIM + "games" + DIR_DELIM + id + "_game",
92 find_paths.emplace_back(share + DIR_DELIM + "games" + DIR_DELIM + id, false);
93 // Find game directory
94 std::string game_path;
95 bool user_game = true; // Game is in user's directory
96 for (const GameFindPath &find_path : find_paths) {
97 const std::string &try_path = find_path.path;
98 if (fs::PathExists(try_path)) {
100 user_game = find_path.user_specific;
104 if (game_path.empty())
105 return SubgameSpec();
106 std::string gamemod_path = game_path + DIR_DELIM + "mods";
107 // Find mod directories
108 std::set<std::string> mods_paths;
110 mods_paths.insert(share + DIR_DELIM + "mods");
111 if(user != share || user_game)
112 mods_paths.insert(user + DIR_DELIM + "mods");
113 std::string game_name = getGameName(game_path);
114 if (game_name.empty())
116 std::string menuicon_path;
118 menuicon_path = getImagePath(game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
120 return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
124 SubgameSpec findWorldSubgame(const std::string &world_path)
126 std::string world_gameid = getWorldGameId(world_path, true);
127 // See if world contains an embedded game; if so, use it.
128 std::string world_gamepath = world_path + DIR_DELIM + "game";
129 if(fs::PathExists(world_gamepath)){
130 SubgameSpec gamespec;
131 gamespec.id = world_gameid;
132 gamespec.path = world_gamepath;
133 gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
134 gamespec.name = getGameName(world_gamepath);
135 if (gamespec.name.empty())
136 gamespec.name = "unknown";
139 return findSubgame(world_gameid);
142 std::set<std::string> getAvailableGameIds()
144 std::set<std::string> gameids;
145 std::set<std::string> gamespaths;
146 gamespaths.insert(porting::path_share + DIR_DELIM + "games");
147 gamespaths.insert(porting::path_user + DIR_DELIM + "games");
149 Strfnd search_paths(getSubgamePathEnv());
151 while (!search_paths.at_end())
152 gamespaths.insert(search_paths.next(PATH_DELIM));
154 for (const std::string &gamespath : gamespaths) {
155 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(gamespath);
156 for (const fs::DirListNode &dln : dirlist) {
159 // If configuration file is not found or broken, ignore game
161 if(!getGameConfig(gamespath + DIR_DELIM + dln.name, conf))
164 const char *ends[] = {"_game", NULL};
165 std::string shorter = removeStringEnd(dln.name, ends);
166 if (!shorter.empty())
167 gameids.insert(shorter);
169 gameids.insert(dln.name);
175 std::vector<SubgameSpec> getAvailableGames()
177 std::vector<SubgameSpec> specs;
178 std::set<std::string> gameids = getAvailableGameIds();
179 for (const auto &gameid : gameids)
180 specs.push_back(findSubgame(gameid));
184 #define LEGACY_GAMEID "minetest"
186 bool getWorldExists(const std::string &world_path)
188 return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
189 fs::PathExists(world_path + DIR_DELIM + "world.mt"));
192 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
194 std::string conf_path = world_path + DIR_DELIM + "world.mt";
196 bool succeeded = conf.readConfigFile(conf_path.c_str());
199 // If map_meta.txt exists, it is probably an old minetest world
200 if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
201 return LEGACY_GAMEID;
205 if(!conf.exists("gameid"))
207 // The "mesetint" gameid has been discarded
208 if(conf.get("gameid") == "mesetint")
210 return conf.get("gameid");
213 std::string getWorldPathEnv()
215 char *world_path = getenv("MINETEST_WORLD_PATH");
216 return world_path ? std::string(world_path) : "";
219 std::vector<WorldSpec> getAvailableWorlds()
221 std::vector<WorldSpec> worlds;
222 std::set<std::string> worldspaths;
224 Strfnd search_paths(getWorldPathEnv());
226 while (!search_paths.at_end())
227 worldspaths.insert(search_paths.next(PATH_DELIM));
229 worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
230 infostream << "Searching worlds..." << std::endl;
231 for (const std::string &worldspath : worldspaths) {
232 infostream << " In " << worldspath << ": " <<std::endl;
233 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(worldspath);
234 for (const fs::DirListNode &dln : dirvector) {
237 std::string fullpath = worldspath + DIR_DELIM + dln.name;
238 std::string name = dln.name;
239 // Just allow filling in the gameid always for now
240 bool can_be_legacy = true;
241 std::string gameid = getWorldGameId(fullpath, can_be_legacy);
242 WorldSpec spec(fullpath, name, gameid);
244 infostream<<"(invalid: "<<name<<") ";
246 infostream<<name<<" ";
247 worlds.push_back(spec);
250 infostream<<std::endl;
252 // Check old world location
254 std::string fullpath = porting::path_user + DIR_DELIM + "world";
255 if(!fs::PathExists(fullpath))
257 std::string name = "Old World";
258 std::string gameid = getWorldGameId(fullpath, true);
259 WorldSpec spec(fullpath, name, gameid);
260 infostream<<"Old world found."<<std::endl;
261 worlds.push_back(spec);
263 infostream<<worlds.size()<<" found."<<std::endl;
267 bool loadGameConfAndInitWorld(const std::string &path, const SubgameSpec &gamespec)
269 // Override defaults with those provided by the game.
270 // We clear and reload the defaults because the defaults
271 // might have been overridden by other subgame config
272 // files that were loaded before.
273 g_settings->clearDefaults();
274 set_default_settings(g_settings);
275 Settings game_defaults;
276 getGameMinetestConfig(gamespec.path, game_defaults);
277 override_default_settings(g_settings, &game_defaults);
279 infostream << "Initializing world at " << path << std::endl;
281 fs::CreateAllDirs(path);
283 // Create world.mt if does not already exist
284 std::string worldmt_path = path + DIR_DELIM "world.mt";
285 if (!fs::PathExists(worldmt_path)) {
288 conf.set("gameid", gamespec.id);
289 conf.set("backend", "sqlite3");
290 conf.set("player_backend", "sqlite3");
291 conf.setBool("creative_mode", g_settings->getBool("creative_mode"));
292 conf.setBool("enable_damage", g_settings->getBool("enable_damage"));
294 if (!conf.updateConfigFile(worldmt_path.c_str()))
298 // Create map_meta.txt if does not already exist
299 std::string map_meta_path = path + DIR_DELIM + "map_meta.txt";
300 if (!fs::PathExists(map_meta_path)){
301 verbosestream << "Creating map_meta.txt (" << map_meta_path << ")" << std::endl;
302 fs::CreateAllDirs(path);
303 std::ostringstream oss(std::ios_base::binary);
308 params.readParams(g_settings);
309 params.writeParams(&conf);
310 conf.writeLines(oss);
311 oss << "[end_of_params]\n";
313 fs::safeWriteToFile(map_meta_path, oss.str());