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/string.h"
27 bool getGameConfig(const std::string &game_path, Settings &conf)
29 std::string conf_path = game_path + DIR_DELIM + "game.conf";
30 return conf.readConfigFile(conf_path.c_str());
33 std::string getGameName(const std::string &game_path)
36 if(!getGameConfig(game_path, conf))
38 if(!conf.exists("name"))
40 return conf.get("name");
47 GameFindPath(const std::string &path, bool user_specific):
49 user_specific(user_specific)
53 SubgameSpec findSubgame(const std::string &id)
57 std::string share = porting::path_share;
58 std::string user = porting::path_user;
59 std::vector<GameFindPath> find_paths;
60 find_paths.push_back(GameFindPath(
61 user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true));
62 find_paths.push_back(GameFindPath(
63 user + DIR_DELIM + "games" + DIR_DELIM + id, true));
64 find_paths.push_back(GameFindPath(
65 share + DIR_DELIM + "games" + DIR_DELIM + id + "_game", false));
66 find_paths.push_back(GameFindPath(
67 share + DIR_DELIM + "games" + DIR_DELIM + id, false));
68 // Find game directory
69 std::string game_path;
70 bool user_game = true; // Game is in user's directory
71 for(u32 i=0; i<find_paths.size(); i++){
72 const std::string &try_path = find_paths[i].path;
73 if(fs::PathExists(try_path)){
75 user_game = find_paths[i].user_specific;
81 std::string gamemod_path = game_path + DIR_DELIM + "mods";
82 // Find mod directories
83 std::set<std::string> mods_paths;
85 mods_paths.insert(share + DIR_DELIM + "mods" + DIR_DELIM + id);
86 if(user != share || user_game)
87 mods_paths.insert(user + DIR_DELIM + "mods" + DIR_DELIM + id);
88 std::string game_name = getGameName(game_path);
91 return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name);
94 SubgameSpec findWorldSubgame(const std::string &world_path)
96 std::string world_gameid = getWorldGameId(world_path, true);
97 // See if world contains an embedded game; if so, use it.
98 std::string world_gamepath = world_path + DIR_DELIM + "game";
99 if(fs::PathExists(world_gamepath)){
100 SubgameSpec gamespec;
101 gamespec.id = world_gameid;
102 gamespec.path = world_gamepath;
103 gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
104 gamespec.name = getGameName(world_gamepath);
105 if(gamespec.name == "")
106 gamespec.name = "unknown";
109 return findSubgame(world_gameid);
112 std::set<std::string> getAvailableGameIds()
114 std::set<std::string> gameids;
115 std::set<std::string> gamespaths;
116 gamespaths.insert(porting::path_share + DIR_DELIM + "games");
117 gamespaths.insert(porting::path_user + DIR_DELIM + "games");
118 for(std::set<std::string>::const_iterator i = gamespaths.begin();
119 i != gamespaths.end(); i++){
120 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
121 for(u32 j=0; j<dirlist.size(); j++){
124 // If configuration file is not found or broken, ignore game
126 if(!getGameConfig(*i + DIR_DELIM + dirlist[j].name, conf))
129 const char *ends[] = {"_game", NULL};
130 std::string shorter = removeStringEnd(dirlist[j].name, ends);
132 gameids.insert(shorter);
134 gameids.insert(dirlist[j].name);
140 std::vector<SubgameSpec> getAvailableGames()
142 std::vector<SubgameSpec> specs;
143 std::set<std::string> gameids = getAvailableGameIds();
144 for(std::set<std::string>::const_iterator i = gameids.begin();
145 i != gameids.end(); i++)
146 specs.push_back(findSubgame(*i));
150 #define LEGACY_GAMEID "minetest"
152 bool getWorldExists(const std::string &world_path)
154 return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
155 fs::PathExists(world_path + DIR_DELIM + "world.mt"));
158 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
160 std::string conf_path = world_path + DIR_DELIM + "world.mt";
162 bool succeeded = conf.readConfigFile(conf_path.c_str());
165 // If map_meta.txt exists, it is probably an old minetest world
166 if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
167 return LEGACY_GAMEID;
171 if(!conf.exists("gameid"))
173 // The "mesetint" gameid has been discarded
174 if(conf.get("gameid") == "mesetint")
176 return conf.get("gameid");
179 std::vector<WorldSpec> getAvailableWorlds()
181 std::vector<WorldSpec> worlds;
182 std::set<std::string> worldspaths;
183 worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
184 infostream<<"Searching worlds..."<<std::endl;
185 for(std::set<std::string>::const_iterator i = worldspaths.begin();
186 i != worldspaths.end(); i++){
187 infostream<<" In "<<(*i)<<": "<<std::endl;
188 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
189 for(u32 j=0; j<dirvector.size(); j++){
190 if(!dirvector[j].dir)
192 std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
193 std::string name = dirvector[j].name;
194 // Just allow filling in the gameid always for now
195 bool can_be_legacy = true;
196 std::string gameid = getWorldGameId(fullpath, can_be_legacy);
197 WorldSpec spec(fullpath, name, gameid);
199 infostream<<"(invalid: "<<name<<") ";
201 infostream<<name<<" ";
202 worlds.push_back(spec);
205 infostream<<std::endl;
207 // Check old world location
209 std::string fullpath = porting::path_user + DIR_DELIM + "world";
210 if(!fs::PathExists(fullpath))
212 std::string name = "Old World";
213 std::string gameid = getWorldGameId(fullpath, true);
214 WorldSpec spec(fullpath, name, gameid);
215 infostream<<"Old world found."<<std::endl;
216 worlds.push_back(spec);
218 infostream<<worlds.size()<<" found."<<std::endl;
222 bool initializeWorld(const std::string &path, const std::string &gameid)
224 infostream<<"Initializing world at "<<path<<std::endl;
225 // Create world.mt if does not already exist
226 std::string worldmt_path = path + DIR_DELIM + "world.mt";
227 if(!fs::PathExists(worldmt_path)){
228 infostream<<"Creating world.mt ("<<worldmt_path<<")"<<std::endl;
229 fs::CreateAllDirs(path);
230 std::ofstream of(worldmt_path.c_str(), std::ios::binary);
231 of<<"gameid = "<<gameid<<"\n";