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.
26 #include "defaultsettings.h" // for override_default_settings
27 #include "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 Strfnd getSubgamePathEnv() {
68 char *subgame_path = getenv("MINETEST_SUBGAME_PATH");
71 sp = std::string(subgame_path);
77 SubgameSpec findSubgame(const std::string &id)
81 std::string share = porting::path_share;
82 std::string user = porting::path_user;
83 std::vector<GameFindPath> find_paths;
85 Strfnd search_paths = getSubgamePathEnv();
87 while(!search_paths.atend()) {
88 std::string path = search_paths.next(":");
89 find_paths.push_back(GameFindPath(
90 path + DIR_DELIM + id, false));
91 find_paths.push_back(GameFindPath(
92 path + DIR_DELIM + id + "_game", false));
95 find_paths.push_back(GameFindPath(
96 user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true));
97 find_paths.push_back(GameFindPath(
98 user + DIR_DELIM + "games" + DIR_DELIM + id, true));
99 find_paths.push_back(GameFindPath(
100 share + DIR_DELIM + "games" + DIR_DELIM + id + "_game", false));
101 find_paths.push_back(GameFindPath(
102 share + DIR_DELIM + "games" + DIR_DELIM + id, false));
103 // Find game directory
104 std::string game_path;
105 bool user_game = true; // Game is in user's directory
106 for(u32 i=0; i<find_paths.size(); i++){
107 const std::string &try_path = find_paths[i].path;
108 if(fs::PathExists(try_path)){
109 game_path = try_path;
110 user_game = find_paths[i].user_specific;
115 return SubgameSpec();
116 std::string gamemod_path = game_path + DIR_DELIM + "mods";
117 // Find mod directories
118 std::set<std::string> mods_paths;
120 mods_paths.insert(share + DIR_DELIM + "mods");
121 if(user != share || user_game)
122 mods_paths.insert(user + DIR_DELIM + "mods");
123 std::string game_name = getGameName(game_path);
126 std::string menuicon_path;
128 menuicon_path = getImagePath(game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
130 return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
134 SubgameSpec findWorldSubgame(const std::string &world_path)
136 std::string world_gameid = getWorldGameId(world_path, true);
137 // See if world contains an embedded game; if so, use it.
138 std::string world_gamepath = world_path + DIR_DELIM + "game";
139 if(fs::PathExists(world_gamepath)){
140 SubgameSpec gamespec;
141 gamespec.id = world_gameid;
142 gamespec.path = world_gamepath;
143 gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
144 gamespec.name = getGameName(world_gamepath);
145 if(gamespec.name == "")
146 gamespec.name = "unknown";
149 return findSubgame(world_gameid);
152 std::set<std::string> getAvailableGameIds()
154 std::set<std::string> gameids;
155 std::set<std::string> gamespaths;
156 gamespaths.insert(porting::path_share + DIR_DELIM + "games");
157 gamespaths.insert(porting::path_user + DIR_DELIM + "games");
159 Strfnd search_paths = getSubgamePathEnv();
161 while(!search_paths.atend()) {
162 gamespaths.insert(search_paths.next(":"));
165 for(std::set<std::string>::const_iterator i = gamespaths.begin();
166 i != gamespaths.end(); i++){
167 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
168 for(u32 j=0; j<dirlist.size(); j++){
171 // If configuration file is not found or broken, ignore game
173 if(!getGameConfig(*i + DIR_DELIM + dirlist[j].name, conf))
176 const char *ends[] = {"_game", NULL};
177 std::string shorter = removeStringEnd(dirlist[j].name, ends);
179 gameids.insert(shorter);
181 gameids.insert(dirlist[j].name);
187 std::vector<SubgameSpec> getAvailableGames()
189 std::vector<SubgameSpec> specs;
190 std::set<std::string> gameids = getAvailableGameIds();
191 for(std::set<std::string>::const_iterator i = gameids.begin();
192 i != gameids.end(); i++)
193 specs.push_back(findSubgame(*i));
197 #define LEGACY_GAMEID "minetest"
199 bool getWorldExists(const std::string &world_path)
201 return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
202 fs::PathExists(world_path + DIR_DELIM + "world.mt"));
205 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
207 std::string conf_path = world_path + DIR_DELIM + "world.mt";
209 bool succeeded = conf.readConfigFile(conf_path.c_str());
212 // If map_meta.txt exists, it is probably an old minetest world
213 if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
214 return LEGACY_GAMEID;
218 if(!conf.exists("gameid"))
220 // The "mesetint" gameid has been discarded
221 if(conf.get("gameid") == "mesetint")
223 return conf.get("gameid");
226 std::vector<WorldSpec> getAvailableWorlds()
228 std::vector<WorldSpec> worlds;
229 std::set<std::string> worldspaths;
230 worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
231 infostream<<"Searching worlds..."<<std::endl;
232 for(std::set<std::string>::const_iterator i = worldspaths.begin();
233 i != worldspaths.end(); i++){
234 infostream<<" In "<<(*i)<<": "<<std::endl;
235 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
236 for(u32 j=0; j<dirvector.size(); j++){
237 if(!dirvector[j].dir)
239 std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
240 std::string name = dirvector[j].name;
241 // Just allow filling in the gameid always for now
242 bool can_be_legacy = true;
243 std::string gameid = getWorldGameId(fullpath, can_be_legacy);
244 WorldSpec spec(fullpath, name, gameid);
246 infostream<<"(invalid: "<<name<<") ";
248 infostream<<name<<" ";
249 worlds.push_back(spec);
252 infostream<<std::endl;
254 // Check old world location
256 std::string fullpath = porting::path_user + DIR_DELIM + "world";
257 if(!fs::PathExists(fullpath))
259 std::string name = "Old World";
260 std::string gameid = getWorldGameId(fullpath, true);
261 WorldSpec spec(fullpath, name, gameid);
262 infostream<<"Old world found."<<std::endl;
263 worlds.push_back(spec);
265 infostream<<worlds.size()<<" found."<<std::endl;
269 bool loadGameConfAndInitWorld(const std::string &path, const SubgameSpec &gamespec)
271 // Override defaults with those provided by the game.
272 // We clear and reload the defaults because the defaults
273 // might have been overridden by other subgame config
274 // files that were loaded before.
275 g_settings->clearDefaults();
276 set_default_settings(g_settings);
277 Settings game_defaults;
278 getGameMinetestConfig(gamespec.path, game_defaults);
279 override_default_settings(g_settings, &game_defaults);
281 infostream << "Initializing world at " << path << std::endl;
283 fs::CreateAllDirs(path);
285 // Create world.mt if does not already exist
286 std::string worldmt_path = path + DIR_DELIM "world.mt";
287 if (!fs::PathExists(worldmt_path)) {
288 std::ostringstream ss(std::ios_base::binary);
289 ss << "gameid = " << gamespec.id
290 << "\nbackend = sqlite3"
291 << "\ncreative_mode = " << g_settings->get("creative_mode")
292 << "\nenable_damage = " << g_settings->get("enable_damage")
294 if (!fs::safeWriteToFile(worldmt_path, ss.str()))
297 infostream << "Wrote world.mt (" << worldmt_path << ")" << std::endl;
300 // Create map_meta.txt if does not already exist
301 std::string map_meta_path = path + DIR_DELIM + "map_meta.txt";
302 if (!fs::PathExists(map_meta_path)){
303 verbosestream << "Creating map_meta.txt (" << map_meta_path << ")" << std::endl;
304 fs::CreateAllDirs(path);
305 std::ostringstream oss(std::ios_base::binary);
310 params.load(*g_settings);
312 conf.writeLines(oss);
313 oss << "[end_of_params]\n";
315 fs::safeWriteToFile(map_meta_path, oss.str());