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 getGameMinetestConfig(const std::string &game_path, Settings &conf)
29 std::string conf_path = game_path + DIR_DELIM + "minetest.conf";
30 return conf.readConfigFile(conf_path.c_str());
33 bool getGameConfig(const std::string &game_path, Settings &conf)
35 std::string conf_path = game_path + DIR_DELIM + "game.conf";
36 return conf.readConfigFile(conf_path.c_str());
39 std::string getGameName(const std::string &game_path)
42 if(!getGameConfig(game_path, conf))
44 if(!conf.exists("name"))
46 return conf.get("name");
53 GameFindPath(const std::string &path, bool user_specific):
55 user_specific(user_specific)
59 SubgameSpec findSubgame(const std::string &id)
63 std::string share = porting::path_share;
64 std::string user = porting::path_user;
65 std::vector<GameFindPath> find_paths;
66 find_paths.push_back(GameFindPath(
67 user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true));
68 find_paths.push_back(GameFindPath(
69 user + DIR_DELIM + "games" + DIR_DELIM + id, true));
70 find_paths.push_back(GameFindPath(
71 share + DIR_DELIM + "games" + DIR_DELIM + id + "_game", false));
72 find_paths.push_back(GameFindPath(
73 share + DIR_DELIM + "games" + DIR_DELIM + id, false));
74 // Find game directory
75 std::string game_path;
76 bool user_game = true; // Game is in user's directory
77 for(u32 i=0; i<find_paths.size(); i++){
78 const std::string &try_path = find_paths[i].path;
79 if(fs::PathExists(try_path)){
81 user_game = find_paths[i].user_specific;
87 std::string gamemod_path = game_path + DIR_DELIM + "mods";
88 // Find mod directories
89 std::set<std::string> mods_paths;
91 mods_paths.insert(share + DIR_DELIM + "mods" + DIR_DELIM + id);
92 if(user != share || user_game)
93 mods_paths.insert(user + DIR_DELIM + "mods" + DIR_DELIM + id);
94 std::string game_name = getGameName(game_path);
97 return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name);
100 SubgameSpec findWorldSubgame(const std::string &world_path)
102 std::string world_gameid = getWorldGameId(world_path, true);
103 // See if world contains an embedded game; if so, use it.
104 std::string world_gamepath = world_path + DIR_DELIM + "game";
105 if(fs::PathExists(world_gamepath)){
106 SubgameSpec gamespec;
107 gamespec.id = world_gameid;
108 gamespec.path = world_gamepath;
109 gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
110 gamespec.name = getGameName(world_gamepath);
111 if(gamespec.name == "")
112 gamespec.name = "unknown";
115 return findSubgame(world_gameid);
118 std::set<std::string> getAvailableGameIds()
120 std::set<std::string> gameids;
121 std::set<std::string> gamespaths;
122 gamespaths.insert(porting::path_share + DIR_DELIM + "games");
123 gamespaths.insert(porting::path_user + DIR_DELIM + "games");
124 for(std::set<std::string>::const_iterator i = gamespaths.begin();
125 i != gamespaths.end(); i++){
126 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
127 for(u32 j=0; j<dirlist.size(); j++){
130 // If configuration file is not found or broken, ignore game
132 if(!getGameConfig(*i + DIR_DELIM + dirlist[j].name, conf))
135 const char *ends[] = {"_game", NULL};
136 std::string shorter = removeStringEnd(dirlist[j].name, ends);
138 gameids.insert(shorter);
140 gameids.insert(dirlist[j].name);
146 std::vector<SubgameSpec> getAvailableGames()
148 std::vector<SubgameSpec> specs;
149 std::set<std::string> gameids = getAvailableGameIds();
150 for(std::set<std::string>::const_iterator i = gameids.begin();
151 i != gameids.end(); i++)
152 specs.push_back(findSubgame(*i));
156 #define LEGACY_GAMEID "minetest"
158 bool getWorldExists(const std::string &world_path)
160 return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
161 fs::PathExists(world_path + DIR_DELIM + "world.mt"));
164 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
166 std::string conf_path = world_path + DIR_DELIM + "world.mt";
168 bool succeeded = conf.readConfigFile(conf_path.c_str());
171 // If map_meta.txt exists, it is probably an old minetest world
172 if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
173 return LEGACY_GAMEID;
177 if(!conf.exists("gameid"))
179 // The "mesetint" gameid has been discarded
180 if(conf.get("gameid") == "mesetint")
182 return conf.get("gameid");
185 std::vector<WorldSpec> getAvailableWorlds()
187 std::vector<WorldSpec> worlds;
188 std::set<std::string> worldspaths;
189 worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
190 infostream<<"Searching worlds..."<<std::endl;
191 for(std::set<std::string>::const_iterator i = worldspaths.begin();
192 i != worldspaths.end(); i++){
193 infostream<<" In "<<(*i)<<": "<<std::endl;
194 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
195 for(u32 j=0; j<dirvector.size(); j++){
196 if(!dirvector[j].dir)
198 std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
199 std::string name = dirvector[j].name;
200 // Just allow filling in the gameid always for now
201 bool can_be_legacy = true;
202 std::string gameid = getWorldGameId(fullpath, can_be_legacy);
203 WorldSpec spec(fullpath, name, gameid);
205 infostream<<"(invalid: "<<name<<") ";
207 infostream<<name<<" ";
208 worlds.push_back(spec);
211 infostream<<std::endl;
213 // Check old world location
215 std::string fullpath = porting::path_user + DIR_DELIM + "world";
216 if(!fs::PathExists(fullpath))
218 std::string name = "Old World";
219 std::string gameid = getWorldGameId(fullpath, true);
220 WorldSpec spec(fullpath, name, gameid);
221 infostream<<"Old world found."<<std::endl;
222 worlds.push_back(spec);
224 infostream<<worlds.size()<<" found."<<std::endl;
228 bool initializeWorld(const std::string &path, const std::string &gameid)
230 infostream<<"Initializing world at "<<path<<std::endl;
231 // Create world.mt if does not already exist
232 std::string worldmt_path = path + DIR_DELIM + "world.mt";
233 if(!fs::PathExists(worldmt_path)){
234 infostream<<"Creating world.mt ("<<worldmt_path<<")"<<std::endl;
235 fs::CreateAllDirs(path);
236 std::ofstream of(worldmt_path.c_str(), std::ios::binary);
237 of<<"gameid = "<<gameid<<"\n";