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 "tile.h" // getImagePath
29 #include "util/string.h"
31 bool getGameMinetestConfig(const std::string &game_path, Settings &conf)
33 std::string conf_path = game_path + DIR_DELIM + "minetest.conf";
34 return conf.readConfigFile(conf_path.c_str());
37 bool getGameConfig(const std::string &game_path, Settings &conf)
39 std::string conf_path = game_path + DIR_DELIM + "game.conf";
40 return conf.readConfigFile(conf_path.c_str());
43 std::string getGameName(const std::string &game_path)
46 if(!getGameConfig(game_path, conf))
48 if(!conf.exists("name"))
50 return conf.get("name");
57 GameFindPath(const std::string &path, bool user_specific):
59 user_specific(user_specific)
63 Strfnd getSubgamePathEnv() {
65 char *subgame_path = getenv("MINETEST_SUBGAME_PATH");
68 sp = std::string(subgame_path);
74 SubgameSpec findSubgame(const std::string &id)
78 std::string share = porting::path_share;
79 std::string user = porting::path_user;
80 std::vector<GameFindPath> find_paths;
82 Strfnd search_paths = getSubgamePathEnv();
84 while(!search_paths.atend()) {
85 std::string path = search_paths.next(":");
86 find_paths.push_back(GameFindPath(
87 path + DIR_DELIM + id, false));
88 find_paths.push_back(GameFindPath(
89 path + DIR_DELIM + id + "_game", false));
92 find_paths.push_back(GameFindPath(
93 user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true));
94 find_paths.push_back(GameFindPath(
95 user + DIR_DELIM + "games" + DIR_DELIM + id, true));
96 find_paths.push_back(GameFindPath(
97 share + DIR_DELIM + "games" + DIR_DELIM + id + "_game", false));
98 find_paths.push_back(GameFindPath(
99 share + DIR_DELIM + "games" + DIR_DELIM + id, false));
100 // Find game directory
101 std::string game_path;
102 bool user_game = true; // Game is in user's directory
103 for(u32 i=0; i<find_paths.size(); i++){
104 const std::string &try_path = find_paths[i].path;
105 if(fs::PathExists(try_path)){
106 game_path = try_path;
107 user_game = find_paths[i].user_specific;
112 return SubgameSpec();
113 std::string gamemod_path = game_path + DIR_DELIM + "mods";
114 // Find mod directories
115 std::set<std::string> mods_paths;
117 mods_paths.insert(share + DIR_DELIM + "mods");
118 if(user != share || user_game)
119 mods_paths.insert(user + DIR_DELIM + "mods");
120 std::string game_name = getGameName(game_path);
123 std::string menuicon_path;
125 menuicon_path = getImagePath(game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
127 return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
131 SubgameSpec findWorldSubgame(const std::string &world_path)
133 std::string world_gameid = getWorldGameId(world_path, true);
134 // See if world contains an embedded game; if so, use it.
135 std::string world_gamepath = world_path + DIR_DELIM + "game";
136 if(fs::PathExists(world_gamepath)){
137 SubgameSpec gamespec;
138 gamespec.id = world_gameid;
139 gamespec.path = world_gamepath;
140 gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
141 gamespec.name = getGameName(world_gamepath);
142 if(gamespec.name == "")
143 gamespec.name = "unknown";
146 return findSubgame(world_gameid);
149 std::set<std::string> getAvailableGameIds()
151 std::set<std::string> gameids;
152 std::set<std::string> gamespaths;
153 gamespaths.insert(porting::path_share + DIR_DELIM + "games");
154 gamespaths.insert(porting::path_user + DIR_DELIM + "games");
156 Strfnd search_paths = getSubgamePathEnv();
158 while(!search_paths.atend()) {
159 gamespaths.insert(search_paths.next(":"));
162 for(std::set<std::string>::const_iterator i = gamespaths.begin();
163 i != gamespaths.end(); i++){
164 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
165 for(u32 j=0; j<dirlist.size(); j++){
168 // If configuration file is not found or broken, ignore game
170 if(!getGameConfig(*i + DIR_DELIM + dirlist[j].name, conf))
173 const char *ends[] = {"_game", NULL};
174 std::string shorter = removeStringEnd(dirlist[j].name, ends);
176 gameids.insert(shorter);
178 gameids.insert(dirlist[j].name);
184 std::vector<SubgameSpec> getAvailableGames()
186 std::vector<SubgameSpec> specs;
187 std::set<std::string> gameids = getAvailableGameIds();
188 for(std::set<std::string>::const_iterator i = gameids.begin();
189 i != gameids.end(); i++)
190 specs.push_back(findSubgame(*i));
194 #define LEGACY_GAMEID "minetest"
196 bool getWorldExists(const std::string &world_path)
198 return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
199 fs::PathExists(world_path + DIR_DELIM + "world.mt"));
202 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
204 std::string conf_path = world_path + DIR_DELIM + "world.mt";
206 bool succeeded = conf.readConfigFile(conf_path.c_str());
209 // If map_meta.txt exists, it is probably an old minetest world
210 if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
211 return LEGACY_GAMEID;
215 if(!conf.exists("gameid"))
217 // The "mesetint" gameid has been discarded
218 if(conf.get("gameid") == "mesetint")
220 return conf.get("gameid");
223 std::vector<WorldSpec> getAvailableWorlds()
225 std::vector<WorldSpec> worlds;
226 std::set<std::string> worldspaths;
227 worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
228 infostream<<"Searching worlds..."<<std::endl;
229 for(std::set<std::string>::const_iterator i = worldspaths.begin();
230 i != worldspaths.end(); i++){
231 infostream<<" In "<<(*i)<<": "<<std::endl;
232 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
233 for(u32 j=0; j<dirvector.size(); j++){
234 if(!dirvector[j].dir)
236 std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
237 std::string name = dirvector[j].name;
238 // Just allow filling in the gameid always for now
239 bool can_be_legacy = true;
240 std::string gameid = getWorldGameId(fullpath, can_be_legacy);
241 WorldSpec spec(fullpath, name, gameid);
243 infostream<<"(invalid: "<<name<<") ";
245 infostream<<name<<" ";
246 worlds.push_back(spec);
249 infostream<<std::endl;
251 // Check old world location
253 std::string fullpath = porting::path_user + DIR_DELIM + "world";
254 if(!fs::PathExists(fullpath))
256 std::string name = "Old World";
257 std::string gameid = getWorldGameId(fullpath, true);
258 WorldSpec spec(fullpath, name, gameid);
259 infostream<<"Old world found."<<std::endl;
260 worlds.push_back(spec);
262 infostream<<worlds.size()<<" found."<<std::endl;
266 bool initializeWorld(const std::string &path, const std::string &gameid)
268 infostream<<"Initializing world at "<<path<<std::endl;
269 // Create world.mt if does not already exist
270 std::string worldmt_path = path + DIR_DELIM + "world.mt";
271 if(!fs::PathExists(worldmt_path)){
272 infostream<<"Creating world.mt ("<<worldmt_path<<")"<<std::endl;
273 fs::CreateAllDirs(path);
274 std::ostringstream ss(std::ios_base::binary);
275 ss<<"gameid = "<<gameid<< "\nbackend = sqlite3\n";
276 fs::safeWriteToFile(worldmt_path, ss.str());