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.
27 #include "defaultsettings.h" // for override_default_settings
28 #include "mapgen.h" // for MapgenParams
29 #include "main.h" // for g_settings
31 #include "client/tile.h" // getImagePath
33 #include "util/string.h"
35 bool getGameMinetestConfig(const std::string &game_path, Settings &conf)
37 std::string conf_path = game_path + DIR_DELIM + "minetest.conf";
38 return conf.readConfigFile(conf_path.c_str());
41 bool getGameConfig(const std::string &game_path, Settings &conf)
43 std::string conf_path = game_path + DIR_DELIM + "game.conf";
44 return conf.readConfigFile(conf_path.c_str());
47 std::string getGameName(const std::string &game_path)
50 if(!getGameConfig(game_path, conf))
52 if(!conf.exists("name"))
54 return conf.get("name");
61 GameFindPath(const std::string &path, bool user_specific):
63 user_specific(user_specific)
67 Strfnd getSubgamePathEnv() {
69 char *subgame_path = getenv("MINETEST_SUBGAME_PATH");
72 sp = std::string(subgame_path);
78 SubgameSpec findSubgame(const std::string &id)
82 std::string share = porting::path_share;
83 std::string user = porting::path_user;
84 std::vector<GameFindPath> find_paths;
86 Strfnd search_paths = getSubgamePathEnv();
88 while(!search_paths.atend()) {
89 std::string path = search_paths.next(":");
90 find_paths.push_back(GameFindPath(
91 path + DIR_DELIM + id, false));
92 find_paths.push_back(GameFindPath(
93 path + DIR_DELIM + id + "_game", false));
96 find_paths.push_back(GameFindPath(
97 user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true));
98 find_paths.push_back(GameFindPath(
99 user + DIR_DELIM + "games" + DIR_DELIM + id, true));
100 find_paths.push_back(GameFindPath(
101 share + DIR_DELIM + "games" + DIR_DELIM + id + "_game", false));
102 find_paths.push_back(GameFindPath(
103 share + DIR_DELIM + "games" + DIR_DELIM + id, false));
104 // Find game directory
105 std::string game_path;
106 bool user_game = true; // Game is in user's directory
107 for(u32 i=0; i<find_paths.size(); i++){
108 const std::string &try_path = find_paths[i].path;
109 if(fs::PathExists(try_path)){
110 game_path = try_path;
111 user_game = find_paths[i].user_specific;
116 return SubgameSpec();
117 std::string gamemod_path = game_path + DIR_DELIM + "mods";
118 // Find mod directories
119 std::set<std::string> mods_paths;
121 mods_paths.insert(share + DIR_DELIM + "mods");
122 if(user != share || user_game)
123 mods_paths.insert(user + DIR_DELIM + "mods");
124 std::string game_name = getGameName(game_path);
127 std::string menuicon_path;
129 menuicon_path = getImagePath(game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
131 return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
135 SubgameSpec findWorldSubgame(const std::string &world_path)
137 std::string world_gameid = getWorldGameId(world_path, true);
138 // See if world contains an embedded game; if so, use it.
139 std::string world_gamepath = world_path + DIR_DELIM + "game";
140 if(fs::PathExists(world_gamepath)){
141 SubgameSpec gamespec;
142 gamespec.id = world_gameid;
143 gamespec.path = world_gamepath;
144 gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
145 gamespec.name = getGameName(world_gamepath);
146 if(gamespec.name == "")
147 gamespec.name = "unknown";
150 return findSubgame(world_gameid);
153 std::set<std::string> getAvailableGameIds()
155 std::set<std::string> gameids;
156 std::set<std::string> gamespaths;
157 gamespaths.insert(porting::path_share + DIR_DELIM + "games");
158 gamespaths.insert(porting::path_user + DIR_DELIM + "games");
160 Strfnd search_paths = getSubgamePathEnv();
162 while(!search_paths.atend()) {
163 gamespaths.insert(search_paths.next(":"));
166 for(std::set<std::string>::const_iterator i = gamespaths.begin();
167 i != gamespaths.end(); i++){
168 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
169 for(u32 j=0; j<dirlist.size(); j++){
172 // If configuration file is not found or broken, ignore game
174 if(!getGameConfig(*i + DIR_DELIM + dirlist[j].name, conf))
177 const char *ends[] = {"_game", NULL};
178 std::string shorter = removeStringEnd(dirlist[j].name, ends);
180 gameids.insert(shorter);
182 gameids.insert(dirlist[j].name);
188 std::vector<SubgameSpec> getAvailableGames()
190 std::vector<SubgameSpec> specs;
191 std::set<std::string> gameids = getAvailableGameIds();
192 for(std::set<std::string>::const_iterator i = gameids.begin();
193 i != gameids.end(); i++)
194 specs.push_back(findSubgame(*i));
198 #define LEGACY_GAMEID "minetest"
200 bool getWorldExists(const std::string &world_path)
202 return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
203 fs::PathExists(world_path + DIR_DELIM + "world.mt"));
206 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
208 std::string conf_path = world_path + DIR_DELIM + "world.mt";
210 bool succeeded = conf.readConfigFile(conf_path.c_str());
213 // If map_meta.txt exists, it is probably an old minetest world
214 if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
215 return LEGACY_GAMEID;
219 if(!conf.exists("gameid"))
221 // The "mesetint" gameid has been discarded
222 if(conf.get("gameid") == "mesetint")
224 return conf.get("gameid");
227 std::vector<WorldSpec> getAvailableWorlds()
229 std::vector<WorldSpec> worlds;
230 std::set<std::string> worldspaths;
231 worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
232 infostream<<"Searching worlds..."<<std::endl;
233 for(std::set<std::string>::const_iterator i = worldspaths.begin();
234 i != worldspaths.end(); i++){
235 infostream<<" In "<<(*i)<<": "<<std::endl;
236 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
237 for(u32 j=0; j<dirvector.size(); j++){
238 if(!dirvector[j].dir)
240 std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
241 std::string name = dirvector[j].name;
242 // Just allow filling in the gameid always for now
243 bool can_be_legacy = true;
244 std::string gameid = getWorldGameId(fullpath, can_be_legacy);
245 WorldSpec spec(fullpath, name, gameid);
247 infostream<<"(invalid: "<<name<<") ";
249 infostream<<name<<" ";
250 worlds.push_back(spec);
253 infostream<<std::endl;
255 // Check old world location
257 std::string fullpath = porting::path_user + DIR_DELIM + "world";
258 if(!fs::PathExists(fullpath))
260 std::string name = "Old World";
261 std::string gameid = getWorldGameId(fullpath, true);
262 WorldSpec spec(fullpath, name, gameid);
263 infostream<<"Old world found."<<std::endl;
264 worlds.push_back(spec);
266 infostream<<worlds.size()<<" found."<<std::endl;
270 bool initializeWorld(const std::string &path, const std::string &gameid)
272 infostream << "Initializing world at " << path << std::endl;
274 fs::CreateAllDirs(path);
276 // Initialize default settings and override defaults with those
277 // provided by the game
278 Settings game_defaults;
279 getGameMinetestConfig(path, game_defaults);
280 override_default_settings(g_settings, &game_defaults);
282 // Create world.mt if does not already exist
283 std::string worldmt_path = path + DIR_DELIM "world.mt";
284 if (!fs::PathExists(worldmt_path)) {
285 std::ostringstream ss(std::ios_base::binary);
286 ss << "gameid = " << gameid << "\nbackend = sqlite3\n";
287 if (!fs::safeWriteToFile(worldmt_path, ss.str()))
290 infostream << "Wrote world.mt (" << worldmt_path << ")" << std::endl;
293 // Create map_meta.txt if does not already exist
294 std::string map_meta_path = path + DIR_DELIM + "map_meta.txt";
295 if (!fs::PathExists(map_meta_path)){
296 verbosestream << "Creating map_meta.txt (" << map_meta_path << ")" << std::endl;
297 fs::CreateAllDirs(path);
298 std::ostringstream oss(std::ios_base::binary);
303 params.load(*g_settings);
305 conf.writeLines(oss);
306 oss << "[end_of_params]\n";
308 fs::safeWriteToFile(map_meta_path, oss.str());