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.
28 #include "tile.h" // getImagePath
30 #include "util/string.h"
32 bool getGameMinetestConfig(const std::string &game_path, Settings &conf)
34 std::string conf_path = game_path + DIR_DELIM + "minetest.conf";
35 return conf.readConfigFile(conf_path.c_str());
38 bool getGameConfig(const std::string &game_path, Settings &conf)
40 std::string conf_path = game_path + DIR_DELIM + "game.conf";
41 return conf.readConfigFile(conf_path.c_str());
44 std::string getGameName(const std::string &game_path)
47 if(!getGameConfig(game_path, conf))
49 if(!conf.exists("name"))
51 return conf.get("name");
58 GameFindPath(const std::string &path, bool user_specific):
60 user_specific(user_specific)
64 Strfnd getSubgamePathEnv() {
66 char *subgame_path = getenv("MINETEST_SUBGAME_PATH");
69 sp = std::string(subgame_path);
75 SubgameSpec findSubgame(const std::string &id)
79 std::string share = porting::path_share;
80 std::string user = porting::path_user;
81 std::vector<GameFindPath> find_paths;
83 Strfnd search_paths = getSubgamePathEnv();
85 while(!search_paths.atend()) {
86 std::string path = search_paths.next(":");
87 find_paths.push_back(GameFindPath(
88 path + DIR_DELIM + id, false));
89 find_paths.push_back(GameFindPath(
90 path + DIR_DELIM + id + "_game", false));
93 find_paths.push_back(GameFindPath(
94 user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true));
95 find_paths.push_back(GameFindPath(
96 user + DIR_DELIM + "games" + DIR_DELIM + id, true));
97 find_paths.push_back(GameFindPath(
98 share + DIR_DELIM + "games" + DIR_DELIM + id + "_game", false));
99 find_paths.push_back(GameFindPath(
100 share + DIR_DELIM + "games" + DIR_DELIM + id, false));
101 // Find game directory
102 std::string game_path;
103 bool user_game = true; // Game is in user's directory
104 for(u32 i=0; i<find_paths.size(); i++){
105 const std::string &try_path = find_paths[i].path;
106 if(fs::PathExists(try_path)){
107 game_path = try_path;
108 user_game = find_paths[i].user_specific;
113 return SubgameSpec();
114 std::string gamemod_path = game_path + DIR_DELIM + "mods";
115 // Find mod directories
116 std::set<std::string> mods_paths;
118 mods_paths.insert(share + DIR_DELIM + "mods");
119 if(user != share || user_game)
120 mods_paths.insert(user + DIR_DELIM + "mods");
121 std::string game_name = getGameName(game_path);
124 std::string menuicon_path;
126 menuicon_path = getImagePath(game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
128 return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
132 SubgameSpec findWorldSubgame(const std::string &world_path)
134 std::string world_gameid = getWorldGameId(world_path, true);
135 // See if world contains an embedded game; if so, use it.
136 std::string world_gamepath = world_path + DIR_DELIM + "game";
137 if(fs::PathExists(world_gamepath)){
138 SubgameSpec gamespec;
139 gamespec.id = world_gameid;
140 gamespec.path = world_gamepath;
141 gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
142 gamespec.name = getGameName(world_gamepath);
143 if(gamespec.name == "")
144 gamespec.name = "unknown";
147 return findSubgame(world_gameid);
150 std::set<std::string> getAvailableGameIds()
152 std::set<std::string> gameids;
153 std::set<std::string> gamespaths;
154 gamespaths.insert(porting::path_share + DIR_DELIM + "games");
155 gamespaths.insert(porting::path_user + DIR_DELIM + "games");
157 Strfnd search_paths = getSubgamePathEnv();
159 while(!search_paths.atend()) {
160 gamespaths.insert(search_paths.next(":"));
163 for(std::set<std::string>::const_iterator i = gamespaths.begin();
164 i != gamespaths.end(); i++){
165 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
166 for(u32 j=0; j<dirlist.size(); j++){
169 // If configuration file is not found or broken, ignore game
171 if(!getGameConfig(*i + DIR_DELIM + dirlist[j].name, conf))
174 const char *ends[] = {"_game", NULL};
175 std::string shorter = removeStringEnd(dirlist[j].name, ends);
177 gameids.insert(shorter);
179 gameids.insert(dirlist[j].name);
185 std::vector<SubgameSpec> getAvailableGames()
187 std::vector<SubgameSpec> specs;
188 std::set<std::string> gameids = getAvailableGameIds();
189 for(std::set<std::string>::const_iterator i = gameids.begin();
190 i != gameids.end(); i++)
191 specs.push_back(findSubgame(*i));
195 #define LEGACY_GAMEID "minetest"
197 bool getWorldExists(const std::string &world_path)
199 return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
200 fs::PathExists(world_path + DIR_DELIM + "world.mt"));
203 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
205 std::string conf_path = world_path + DIR_DELIM + "world.mt";
207 bool succeeded = conf.readConfigFile(conf_path.c_str());
210 // If map_meta.txt exists, it is probably an old minetest world
211 if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
212 return LEGACY_GAMEID;
216 if(!conf.exists("gameid"))
218 // The "mesetint" gameid has been discarded
219 if(conf.get("gameid") == "mesetint")
221 return conf.get("gameid");
224 std::vector<WorldSpec> getAvailableWorlds()
226 std::vector<WorldSpec> worlds;
227 std::set<std::string> worldspaths;
228 worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
229 infostream<<"Searching worlds..."<<std::endl;
230 for(std::set<std::string>::const_iterator i = worldspaths.begin();
231 i != worldspaths.end(); i++){
232 infostream<<" In "<<(*i)<<": "<<std::endl;
233 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
234 for(u32 j=0; j<dirvector.size(); j++){
235 if(!dirvector[j].dir)
237 std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
238 std::string name = dirvector[j].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 initializeWorld(const std::string &path, const std::string &gameid)
269 infostream << "Initializing world at " << path << std::endl;
271 fs::CreateAllDirs(path);
273 // Create world.mt if does not already exist
274 std::string worldmt_path = path + DIR_DELIM "world.mt";
275 if (!fs::PathExists(worldmt_path)) {
276 std::ostringstream ss(std::ios_base::binary);
277 ss << "gameid = " << gameid << "\nbackend = sqlite3\n";
278 if (!fs::safeWriteToFile(worldmt_path, ss.str()))
281 infostream << "Wrote world.mt (" << worldmt_path << ")" << std::endl;
284 // Create map_meta.txt if does not already exist
285 std::string mapmeta_path = path + DIR_DELIM "map_meta.txt";
286 if (!fs::PathExists(mapmeta_path)) {
287 std::ostringstream ss(std::ios_base::binary);
289 << "mg_name = " << g_settings->get("mg_name")
290 << "\nseed = " << g_settings->get("fixed_map_seed")
291 << "\nchunksize = " << g_settings->get("chunksize")
292 << "\nwater_level = " << g_settings->get("water_level")
293 << "\nmg_flags = " << g_settings->get("mg_flags")
294 << "\n[end_of_params]\n";
295 if (!fs::safeWriteToFile(mapmeta_path, ss.str()))
298 infostream << "Wrote map_meta.txt (" << mapmeta_path << ")" << std::endl;