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 std::string getGameName(const std::string &game_path)
29 std::string conf_path = game_path + DIR_DELIM + "game.conf";
31 bool succeeded = conf.readConfigFile(conf_path.c_str());
34 if(!conf.exists("name"))
36 return conf.get("name");
43 GameFindPath(const std::string &path, bool user_specific):
45 user_specific(user_specific)
49 SubgameSpec findSubgame(const std::string &id)
53 std::string share = porting::path_share;
54 std::string user = porting::path_user;
55 std::vector<GameFindPath> find_paths;
56 find_paths.push_back(GameFindPath(
57 user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true));
58 find_paths.push_back(GameFindPath(
59 user + DIR_DELIM + "games" + DIR_DELIM + id, true));
60 find_paths.push_back(GameFindPath(
61 share + DIR_DELIM + "games" + DIR_DELIM + id + "_game", false));
62 find_paths.push_back(GameFindPath(
63 share + DIR_DELIM + "games" + DIR_DELIM + id, false));
64 // Find game directory
65 std::string game_path;
66 bool user_game = true; // Game is in user's directory
67 for(u32 i=0; i<find_paths.size(); i++){
68 const std::string &try_path = find_paths[i].path;
69 if(fs::PathExists(try_path)){
71 user_game = find_paths[i].user_specific;
77 std::string gamemod_path = game_path + DIR_DELIM + "mods";
78 // Find mod directories
79 std::set<std::string> mods_paths;
81 mods_paths.insert(share + DIR_DELIM + "mods" + DIR_DELIM + id);
82 if(user != share || user_game)
83 mods_paths.insert(user + DIR_DELIM + "mods" + DIR_DELIM + id);
84 std::string game_name = getGameName(game_path);
87 return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name);
90 SubgameSpec findWorldSubgame(const std::string &world_path)
92 std::string world_gameid = getWorldGameId(world_path, true);
93 // See if world contains an embedded game; if so, use it.
94 std::string world_gamepath = world_path + DIR_DELIM + "game";
95 if(fs::PathExists(world_gamepath)){
97 gamespec.id = world_gameid;
98 gamespec.path = world_gamepath;
99 gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
100 gamespec.name = getGameName(world_gamepath);
101 if(gamespec.name == "")
102 gamespec.name = "unknown";
105 return findSubgame(world_gameid);
108 std::set<std::string> getAvailableGameIds()
110 std::set<std::string> gameids;
111 std::set<std::string> gamespaths;
112 gamespaths.insert(porting::path_share + DIR_DELIM + "games");
113 gamespaths.insert(porting::path_user + DIR_DELIM + "games");
114 for(std::set<std::string>::const_iterator i = gamespaths.begin();
115 i != gamespaths.end(); i++){
116 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
117 for(u32 j=0; j<dirlist.size(); j++){
120 const char *ends[] = {"_game", NULL};
121 std::string shorter = removeStringEnd(dirlist[j].name, ends);
123 gameids.insert(shorter);
125 gameids.insert(dirlist[j].name);
131 std::vector<SubgameSpec> getAvailableGames()
133 std::vector<SubgameSpec> specs;
134 std::set<std::string> gameids = getAvailableGameIds();
135 for(std::set<std::string>::const_iterator i = gameids.begin();
136 i != gameids.end(); i++)
137 specs.push_back(findSubgame(*i));
141 #define LEGACY_GAMEID "minetest"
143 bool getWorldExists(const std::string &world_path)
145 return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
146 fs::PathExists(world_path + DIR_DELIM + "world.mt"));
149 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
151 std::string conf_path = world_path + DIR_DELIM + "world.mt";
153 bool succeeded = conf.readConfigFile(conf_path.c_str());
156 // If map_meta.txt exists, it is probably an old minetest world
157 if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
158 return LEGACY_GAMEID;
162 if(!conf.exists("gameid"))
164 // The "mesetint" gameid has been discarded
165 if(conf.get("gameid") == "mesetint")
167 return conf.get("gameid");
170 std::vector<WorldSpec> getAvailableWorlds()
172 std::vector<WorldSpec> worlds;
173 std::set<std::string> worldspaths;
174 worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
175 infostream<<"Searching worlds..."<<std::endl;
176 for(std::set<std::string>::const_iterator i = worldspaths.begin();
177 i != worldspaths.end(); i++){
178 infostream<<" In "<<(*i)<<": "<<std::endl;
179 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
180 for(u32 j=0; j<dirvector.size(); j++){
181 if(!dirvector[j].dir)
183 std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
184 std::string name = dirvector[j].name;
185 // Just allow filling in the gameid always for now
186 bool can_be_legacy = true;
187 std::string gameid = getWorldGameId(fullpath, can_be_legacy);
188 WorldSpec spec(fullpath, name, gameid);
190 infostream<<"(invalid: "<<name<<") ";
192 infostream<<name<<" ";
193 worlds.push_back(spec);
196 infostream<<std::endl;
198 // Check old world location
200 std::string fullpath = porting::path_user + DIR_DELIM + "world";
201 if(!fs::PathExists(fullpath))
203 std::string name = "Old World";
204 std::string gameid = getWorldGameId(fullpath, true);
205 WorldSpec spec(fullpath, name, gameid);
206 infostream<<"Old world found."<<std::endl;
207 worlds.push_back(spec);
209 infostream<<worlds.size()<<" found."<<std::endl;
213 bool initializeWorld(const std::string &path, const std::string &gameid)
215 infostream<<"Initializing world at "<<path<<std::endl;
216 // Create world.mt if does not already exist
217 std::string worldmt_path = path + DIR_DELIM + "world.mt";
218 if(!fs::PathExists(worldmt_path)){
219 infostream<<"Creating world.mt ("<<worldmt_path<<")"<<std::endl;
220 fs::CreateAllDirs(path);
221 std::ofstream of(worldmt_path.c_str(), std::ios::binary);
222 of<<"gameid = "<<gameid<<"\n";