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");
95 if(user != share || user_game)
96 mods_paths.insert(user + DIR_DELIM + "mods");
97 std::string game_name = getGameName(game_path);
100 std::string menuicon_path;
102 menuicon_path = getImagePath(game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
104 return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
108 SubgameSpec findWorldSubgame(const std::string &world_path)
110 std::string world_gameid = getWorldGameId(world_path, true);
111 // See if world contains an embedded game; if so, use it.
112 std::string world_gamepath = world_path + DIR_DELIM + "game";
113 if(fs::PathExists(world_gamepath)){
114 SubgameSpec gamespec;
115 gamespec.id = world_gameid;
116 gamespec.path = world_gamepath;
117 gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
118 gamespec.name = getGameName(world_gamepath);
119 if(gamespec.name == "")
120 gamespec.name = "unknown";
123 return findSubgame(world_gameid);
126 std::set<std::string> getAvailableGameIds()
128 std::set<std::string> gameids;
129 std::set<std::string> gamespaths;
130 gamespaths.insert(porting::path_share + DIR_DELIM + "games");
131 gamespaths.insert(porting::path_user + DIR_DELIM + "games");
132 for(std::set<std::string>::const_iterator i = gamespaths.begin();
133 i != gamespaths.end(); i++){
134 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
135 for(u32 j=0; j<dirlist.size(); j++){
138 // If configuration file is not found or broken, ignore game
140 if(!getGameConfig(*i + DIR_DELIM + dirlist[j].name, conf))
143 const char *ends[] = {"_game", NULL};
144 std::string shorter = removeStringEnd(dirlist[j].name, ends);
146 gameids.insert(shorter);
148 gameids.insert(dirlist[j].name);
154 std::vector<SubgameSpec> getAvailableGames()
156 std::vector<SubgameSpec> specs;
157 std::set<std::string> gameids = getAvailableGameIds();
158 for(std::set<std::string>::const_iterator i = gameids.begin();
159 i != gameids.end(); i++)
160 specs.push_back(findSubgame(*i));
164 #define LEGACY_GAMEID "minetest"
166 bool getWorldExists(const std::string &world_path)
168 return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
169 fs::PathExists(world_path + DIR_DELIM + "world.mt"));
172 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
174 std::string conf_path = world_path + DIR_DELIM + "world.mt";
176 bool succeeded = conf.readConfigFile(conf_path.c_str());
179 // If map_meta.txt exists, it is probably an old minetest world
180 if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
181 return LEGACY_GAMEID;
185 if(!conf.exists("gameid"))
187 // The "mesetint" gameid has been discarded
188 if(conf.get("gameid") == "mesetint")
190 return conf.get("gameid");
193 std::vector<WorldSpec> getAvailableWorlds()
195 std::vector<WorldSpec> worlds;
196 std::set<std::string> worldspaths;
197 worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
198 infostream<<"Searching worlds..."<<std::endl;
199 for(std::set<std::string>::const_iterator i = worldspaths.begin();
200 i != worldspaths.end(); i++){
201 infostream<<" In "<<(*i)<<": "<<std::endl;
202 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
203 for(u32 j=0; j<dirvector.size(); j++){
204 if(!dirvector[j].dir)
206 std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
207 std::string name = dirvector[j].name;
208 // Just allow filling in the gameid always for now
209 bool can_be_legacy = true;
210 std::string gameid = getWorldGameId(fullpath, can_be_legacy);
211 WorldSpec spec(fullpath, name, gameid);
213 infostream<<"(invalid: "<<name<<") ";
215 infostream<<name<<" ";
216 worlds.push_back(spec);
219 infostream<<std::endl;
221 // Check old world location
223 std::string fullpath = porting::path_user + DIR_DELIM + "world";
224 if(!fs::PathExists(fullpath))
226 std::string name = "Old World";
227 std::string gameid = getWorldGameId(fullpath, true);
228 WorldSpec spec(fullpath, name, gameid);
229 infostream<<"Old world found."<<std::endl;
230 worlds.push_back(spec);
232 infostream<<worlds.size()<<" found."<<std::endl;
236 bool initializeWorld(const std::string &path, const std::string &gameid)
238 infostream<<"Initializing world at "<<path<<std::endl;
239 // Create world.mt if does not already exist
240 std::string worldmt_path = path + DIR_DELIM + "world.mt";
241 if(!fs::PathExists(worldmt_path)){
242 infostream<<"Creating world.mt ("<<worldmt_path<<")"<<std::endl;
243 fs::CreateAllDirs(path);
244 std::ostringstream ss(std::ios_base::binary);
245 ss<<"gameid = "<<gameid<<"\nbackend = sqlite3\n";
246 fs::safeWriteToFile(worldmt_path, ss.str());