Translation to Portuguese of Brazil for Minetest
[oweals/minetest.git] / src / subgame.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "subgame.h"
21 #include "porting.h"
22 #include "filesys.h"
23 #include "settings.h"
24 #include "log.h"
25 #include "util/string.h"
26
27 bool getGameMinetestConfig(const std::string &game_path, Settings &conf)
28 {
29         std::string conf_path = game_path + DIR_DELIM + "minetest.conf";
30         return conf.readConfigFile(conf_path.c_str());
31 }
32
33 bool getGameConfig(const std::string &game_path, Settings &conf)
34 {
35         std::string conf_path = game_path + DIR_DELIM + "game.conf";
36         return conf.readConfigFile(conf_path.c_str());
37 }
38
39 std::string getGameName(const std::string &game_path)
40 {
41         Settings conf;
42         if(!getGameConfig(game_path, conf))
43                 return "";
44         if(!conf.exists("name"))
45                 return "";
46         return conf.get("name");
47 }
48
49 struct GameFindPath
50 {
51         std::string path;
52         bool user_specific;
53         GameFindPath(const std::string &path, bool user_specific):
54                 path(path),
55                 user_specific(user_specific)
56         {}
57 };
58
59 SubgameSpec findSubgame(const std::string &id)
60 {
61         if(id == "")
62                 return SubgameSpec();
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)){
80                         game_path = try_path;
81                         user_game = find_paths[i].user_specific;
82                         break;
83                 }
84         }
85         if(game_path == "")
86                 return SubgameSpec();
87         std::string gamemod_path = game_path + DIR_DELIM + "mods";
88         // Find mod directories
89         std::set<std::string> mods_paths;
90         if(!user_game)
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);
95         if(game_name == "")
96                 game_name = id;
97         return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name);
98 }
99
100 SubgameSpec findWorldSubgame(const std::string &world_path)
101 {
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";
113                 return gamespec;
114         }
115         return findSubgame(world_gameid);
116 }
117
118 std::set<std::string> getAvailableGameIds()
119 {
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++){
128                         if(!dirlist[j].dir)
129                                 continue;
130                         // If configuration file is not found or broken, ignore game
131                         Settings conf;
132                         if(!getGameConfig(*i + DIR_DELIM + dirlist[j].name, conf))
133                                 continue;
134                         // Add it to result
135                         const char *ends[] = {"_game", NULL};
136                         std::string shorter = removeStringEnd(dirlist[j].name, ends);
137                         if(shorter != "")
138                                 gameids.insert(shorter);
139                         else
140                                 gameids.insert(dirlist[j].name);
141                 }
142         }
143         return gameids;
144 }
145
146 std::vector<SubgameSpec> getAvailableGames()
147 {
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));
153         return specs;
154 }
155
156 #define LEGACY_GAMEID "minetest"
157
158 bool getWorldExists(const std::string &world_path)
159 {
160         return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
161                         fs::PathExists(world_path + DIR_DELIM + "world.mt"));
162 }
163
164 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
165 {
166         std::string conf_path = world_path + DIR_DELIM + "world.mt";
167         Settings conf;
168         bool succeeded = conf.readConfigFile(conf_path.c_str());
169         if(!succeeded){
170                 if(can_be_legacy){
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;
174                 }
175                 return "";
176         }
177         if(!conf.exists("gameid"))
178                 return "";
179         // The "mesetint" gameid has been discarded
180         if(conf.get("gameid") == "mesetint")
181                 return "minetest";
182         return conf.get("gameid");
183 }
184
185 std::vector<WorldSpec> getAvailableWorlds()
186 {
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)
197                                 continue;
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);
204                         if(!spec.isValid()){
205                                 infostream<<"(invalid: "<<name<<") ";
206                         } else {
207                                 infostream<<name<<" ";
208                                 worlds.push_back(spec);
209                         }
210                 }
211                 infostream<<std::endl;
212         }
213         // Check old world location
214         do{
215                 std::string fullpath = porting::path_user + DIR_DELIM + "world";
216                 if(!fs::PathExists(fullpath))
217                         break;
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);
223         }while(0);
224         infostream<<worlds.size()<<" found."<<std::endl;
225         return worlds;
226 }
227
228 bool initializeWorld(const std::string &path, const std::string &gameid)
229 {
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";
238         }
239         return true;
240 }
241
242