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 "tile.h" // getImagePath
28 #include "util/string.h"
30 bool getGameMinetestConfig(const std::string &game_path, Settings &conf)
32 std::string conf_path = game_path + DIR_DELIM + "minetest.conf";
33 return conf.readConfigFile(conf_path.c_str());
36 bool getGameConfig(const std::string &game_path, Settings &conf)
38 std::string conf_path = game_path + DIR_DELIM + "game.conf";
39 return conf.readConfigFile(conf_path.c_str());
42 std::string getGameName(const std::string &game_path)
45 if(!getGameConfig(game_path, conf))
47 if(!conf.exists("name"))
49 return conf.get("name");
56 GameFindPath(const std::string &path, bool user_specific):
58 user_specific(user_specific)
62 SubgameSpec findSubgame(const std::string &id)
66 std::string share = porting::path_share;
67 std::string user = porting::path_user;
68 std::vector<GameFindPath> find_paths;
69 find_paths.push_back(GameFindPath(
70 user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true));
71 find_paths.push_back(GameFindPath(
72 user + DIR_DELIM + "games" + DIR_DELIM + id, true));
73 find_paths.push_back(GameFindPath(
74 share + DIR_DELIM + "games" + DIR_DELIM + id + "_game", false));
75 find_paths.push_back(GameFindPath(
76 share + DIR_DELIM + "games" + DIR_DELIM + id, false));
77 // Find game directory
78 std::string game_path;
79 bool user_game = true; // Game is in user's directory
80 for(u32 i=0; i<find_paths.size(); i++){
81 const std::string &try_path = find_paths[i].path;
82 if(fs::PathExists(try_path)){
84 user_game = find_paths[i].user_specific;
90 std::string gamemod_path = game_path + DIR_DELIM + "mods";
91 // Find mod directories
92 std::set<std::string> mods_paths;
94 mods_paths.insert(share + DIR_DELIM + "mods" + DIR_DELIM + id);
95 if(user != share || user_game)
96 mods_paths.insert(user + DIR_DELIM + "mods" + DIR_DELIM + id);
97 std::string game_name = getGameName(game_path);
100 std::string menubackground_path;
101 std::string menuoverlay_path;
102 std::string menuicon_path;
104 menubackground_path = getImagePath(game_path + DIR_DELIM + "menu" + DIR_DELIM + "background.png");
105 menuoverlay_path = getImagePath(game_path + DIR_DELIM + "menu" + DIR_DELIM + "overlay.png");
106 menuicon_path = getImagePath(game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
108 return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
109 menubackground_path, menuoverlay_path, menuicon_path);
112 SubgameSpec findWorldSubgame(const std::string &world_path)
114 std::string world_gameid = getWorldGameId(world_path, true);
115 // See if world contains an embedded game; if so, use it.
116 std::string world_gamepath = world_path + DIR_DELIM + "game";
117 if(fs::PathExists(world_gamepath)){
118 SubgameSpec gamespec;
119 gamespec.id = world_gameid;
120 gamespec.path = world_gamepath;
121 gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
122 gamespec.name = getGameName(world_gamepath);
123 if(gamespec.name == "")
124 gamespec.name = "unknown";
127 return findSubgame(world_gameid);
130 std::set<std::string> getAvailableGameIds()
132 std::set<std::string> gameids;
133 std::set<std::string> gamespaths;
134 gamespaths.insert(porting::path_share + DIR_DELIM + "games");
135 gamespaths.insert(porting::path_user + DIR_DELIM + "games");
136 for(std::set<std::string>::const_iterator i = gamespaths.begin();
137 i != gamespaths.end(); i++){
138 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
139 for(u32 j=0; j<dirlist.size(); j++){
142 // If configuration file is not found or broken, ignore game
144 if(!getGameConfig(*i + DIR_DELIM + dirlist[j].name, conf))
147 const char *ends[] = {"_game", NULL};
148 std::string shorter = removeStringEnd(dirlist[j].name, ends);
150 gameids.insert(shorter);
152 gameids.insert(dirlist[j].name);
158 std::vector<SubgameSpec> getAvailableGames()
160 std::vector<SubgameSpec> specs;
161 std::set<std::string> gameids = getAvailableGameIds();
162 for(std::set<std::string>::const_iterator i = gameids.begin();
163 i != gameids.end(); i++)
164 specs.push_back(findSubgame(*i));
168 #define LEGACY_GAMEID "minetest"
170 bool getWorldExists(const std::string &world_path)
172 return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
173 fs::PathExists(world_path + DIR_DELIM + "world.mt"));
176 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
178 std::string conf_path = world_path + DIR_DELIM + "world.mt";
180 bool succeeded = conf.readConfigFile(conf_path.c_str());
183 // If map_meta.txt exists, it is probably an old minetest world
184 if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
185 return LEGACY_GAMEID;
189 if(!conf.exists("gameid"))
191 // The "mesetint" gameid has been discarded
192 if(conf.get("gameid") == "mesetint")
194 return conf.get("gameid");
197 std::vector<WorldSpec> getAvailableWorlds()
199 std::vector<WorldSpec> worlds;
200 std::set<std::string> worldspaths;
201 worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
202 infostream<<"Searching worlds..."<<std::endl;
203 for(std::set<std::string>::const_iterator i = worldspaths.begin();
204 i != worldspaths.end(); i++){
205 infostream<<" In "<<(*i)<<": "<<std::endl;
206 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
207 for(u32 j=0; j<dirvector.size(); j++){
208 if(!dirvector[j].dir)
210 std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
211 std::string name = dirvector[j].name;
212 // Just allow filling in the gameid always for now
213 bool can_be_legacy = true;
214 std::string gameid = getWorldGameId(fullpath, can_be_legacy);
215 WorldSpec spec(fullpath, name, gameid);
217 infostream<<"(invalid: "<<name<<") ";
219 infostream<<name<<" ";
220 worlds.push_back(spec);
223 infostream<<std::endl;
225 // Check old world location
227 std::string fullpath = porting::path_user + DIR_DELIM + "world";
228 if(!fs::PathExists(fullpath))
230 std::string name = "Old World";
231 std::string gameid = getWorldGameId(fullpath, true);
232 WorldSpec spec(fullpath, name, gameid);
233 infostream<<"Old world found."<<std::endl;
234 worlds.push_back(spec);
236 infostream<<worlds.size()<<" found."<<std::endl;
240 bool initializeWorld(const std::string &path, const std::string &gameid)
242 infostream<<"Initializing world at "<<path<<std::endl;
243 // Create world.mt if does not already exist
244 std::string worldmt_path = path + DIR_DELIM + "world.mt";
245 if(!fs::PathExists(worldmt_path)){
246 infostream<<"Creating world.mt ("<<worldmt_path<<")"<<std::endl;
247 fs::CreateAllDirs(path);
248 std::ofstream of(worldmt_path.c_str(), std::ios::binary);
249 of<<"gameid = "<<gameid<<"\n";