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.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.push_back(GameFindPath(
85 path + DIR_DELIM + id, false));
86 find_paths.push_back(GameFindPath(
87 path + DIR_DELIM + id + "_game", false));
90 find_paths.push_back(GameFindPath(
91 user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true));
92 find_paths.push_back(GameFindPath(
93 user + DIR_DELIM + "games" + DIR_DELIM + id, true));
94 find_paths.push_back(GameFindPath(
95 share + DIR_DELIM + "games" + DIR_DELIM + id + "_game", false));
96 find_paths.push_back(GameFindPath(
97 share + DIR_DELIM + "games" + DIR_DELIM + id, false));
98 // Find game directory
99 std::string game_path;
100 bool user_game = true; // Game is in user's directory
101 for(u32 i=0; i<find_paths.size(); i++){
102 const std::string &try_path = find_paths[i].path;
103 if(fs::PathExists(try_path)){
104 game_path = try_path;
105 user_game = find_paths[i].user_specific;
110 return SubgameSpec();
111 std::string gamemod_path = game_path + DIR_DELIM + "mods";
112 // Find mod directories
113 std::set<std::string> mods_paths;
115 mods_paths.insert(share + DIR_DELIM + "mods");
116 if(user != share || user_game)
117 mods_paths.insert(user + DIR_DELIM + "mods");
118 std::string game_name = getGameName(game_path);
121 std::string menuicon_path;
123 menuicon_path = getImagePath(game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
125 return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
129 SubgameSpec findWorldSubgame(const std::string &world_path)
131 std::string world_gameid = getWorldGameId(world_path, true);
132 // See if world contains an embedded game; if so, use it.
133 std::string world_gamepath = world_path + DIR_DELIM + "game";
134 if(fs::PathExists(world_gamepath)){
135 SubgameSpec gamespec;
136 gamespec.id = world_gameid;
137 gamespec.path = world_gamepath;
138 gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
139 gamespec.name = getGameName(world_gamepath);
140 if(gamespec.name == "")
141 gamespec.name = "unknown";
144 return findSubgame(world_gameid);
147 std::set<std::string> getAvailableGameIds()
149 std::set<std::string> gameids;
150 std::set<std::string> gamespaths;
151 gamespaths.insert(porting::path_share + DIR_DELIM + "games");
152 gamespaths.insert(porting::path_user + DIR_DELIM + "games");
154 Strfnd search_paths(getSubgamePathEnv());
156 while (!search_paths.at_end())
157 gamespaths.insert(search_paths.next(PATH_DELIM));
159 for (std::set<std::string>::const_iterator i = gamespaths.begin();
160 i != gamespaths.end(); ++i){
161 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
162 for(u32 j=0; j<dirlist.size(); j++){
165 // If configuration file is not found or broken, ignore game
167 if(!getGameConfig(*i + DIR_DELIM + dirlist[j].name, conf))
170 const char *ends[] = {"_game", NULL};
171 std::string shorter = removeStringEnd(dirlist[j].name, ends);
173 gameids.insert(shorter);
175 gameids.insert(dirlist[j].name);
181 std::vector<SubgameSpec> getAvailableGames()
183 std::vector<SubgameSpec> specs;
184 std::set<std::string> gameids = getAvailableGameIds();
185 for(std::set<std::string>::const_iterator i = gameids.begin();
186 i != gameids.end(); ++i)
187 specs.push_back(findSubgame(*i));
191 #define LEGACY_GAMEID "minetest"
193 bool getWorldExists(const std::string &world_path)
195 return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
196 fs::PathExists(world_path + DIR_DELIM + "world.mt"));
199 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
201 std::string conf_path = world_path + DIR_DELIM + "world.mt";
203 bool succeeded = conf.readConfigFile(conf_path.c_str());
206 // If map_meta.txt exists, it is probably an old minetest world
207 if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
208 return LEGACY_GAMEID;
212 if(!conf.exists("gameid"))
214 // The "mesetint" gameid has been discarded
215 if(conf.get("gameid") == "mesetint")
217 return conf.get("gameid");
220 std::string getWorldPathEnv()
222 char *world_path = getenv("MINETEST_WORLD_PATH");
223 return world_path ? std::string(world_path) : "";
226 std::vector<WorldSpec> getAvailableWorlds()
228 std::vector<WorldSpec> worlds;
229 std::set<std::string> worldspaths;
231 Strfnd search_paths(getWorldPathEnv());
233 while (!search_paths.at_end())
234 worldspaths.insert(search_paths.next(PATH_DELIM));
236 worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
237 infostream << "Searching worlds..." << std::endl;
238 for (std::set<std::string>::const_iterator i = worldspaths.begin();
239 i != worldspaths.end(); ++i) {
240 infostream << " In " << (*i) << ": " <<std::endl;
241 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
242 for(u32 j=0; j<dirvector.size(); j++){
243 if(!dirvector[j].dir)
245 std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
246 std::string name = dirvector[j].name;
247 // Just allow filling in the gameid always for now
248 bool can_be_legacy = true;
249 std::string gameid = getWorldGameId(fullpath, can_be_legacy);
250 WorldSpec spec(fullpath, name, gameid);
252 infostream<<"(invalid: "<<name<<") ";
254 infostream<<name<<" ";
255 worlds.push_back(spec);
258 infostream<<std::endl;
260 // Check old world location
262 std::string fullpath = porting::path_user + DIR_DELIM + "world";
263 if(!fs::PathExists(fullpath))
265 std::string name = "Old World";
266 std::string gameid = getWorldGameId(fullpath, true);
267 WorldSpec spec(fullpath, name, gameid);
268 infostream<<"Old world found."<<std::endl;
269 worlds.push_back(spec);
271 infostream<<worlds.size()<<" found."<<std::endl;
275 bool loadGameConfAndInitWorld(const std::string &path, const SubgameSpec &gamespec)
277 // Override defaults with those provided by the game.
278 // We clear and reload the defaults because the defaults
279 // might have been overridden by other subgame config
280 // files that were loaded before.
281 g_settings->clearDefaults();
282 set_default_settings(g_settings);
283 Settings game_defaults;
284 getGameMinetestConfig(gamespec.path, game_defaults);
285 override_default_settings(g_settings, &game_defaults);
287 infostream << "Initializing world at " << path << std::endl;
289 fs::CreateAllDirs(path);
291 // Create world.mt if does not already exist
292 std::string worldmt_path = path + DIR_DELIM "world.mt";
293 if (!fs::PathExists(worldmt_path)) {
294 std::ostringstream ss(std::ios_base::binary);
295 ss << "gameid = " << gamespec.id
296 << "\nbackend = sqlite3"
297 << "\ncreative_mode = " << g_settings->get("creative_mode")
298 << "\nenable_damage = " << g_settings->get("enable_damage")
300 if (!fs::safeWriteToFile(worldmt_path, ss.str()))
303 infostream << "Wrote world.mt (" << worldmt_path << ")" << std::endl;
306 // Create map_meta.txt if does not already exist
307 std::string map_meta_path = path + DIR_DELIM + "map_meta.txt";
308 if (!fs::PathExists(map_meta_path)){
309 verbosestream << "Creating map_meta.txt (" << map_meta_path << ")" << std::endl;
310 fs::CreateAllDirs(path);
311 std::ostringstream oss(std::ios_base::binary);
316 params.load(*g_settings);
318 conf.writeLines(oss);
319 oss << "[end_of_params]\n";
321 fs::safeWriteToFile(map_meta_path, oss.str());