8678ae37fd03fe3fa1ab3364ed7adceeb631bd66
[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 getGameConfig(const std::string &game_path, Settings &conf)
28 {
29         std::string conf_path = game_path + DIR_DELIM + "game.conf";
30         return conf.readConfigFile(conf_path.c_str());
31 }
32
33 std::string getGameName(const std::string &game_path)
34 {
35         Settings conf;
36         if(!getGameConfig(game_path, conf))
37                 return "";
38         if(!conf.exists("name"))
39                 return "";
40         return conf.get("name");
41 }
42
43 struct GameFindPath
44 {
45         std::string path;
46         bool user_specific;
47         GameFindPath(const std::string &path, bool user_specific):
48                 path(path),
49                 user_specific(user_specific)
50         {}
51 };
52
53 SubgameSpec findSubgame(const std::string &id)
54 {
55         if(id == "")
56                 return SubgameSpec();
57         std::string share = porting::path_share;
58         std::string user = porting::path_user;
59         std::vector<GameFindPath> find_paths;
60         find_paths.push_back(GameFindPath(
61                         user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true));
62         find_paths.push_back(GameFindPath(
63                         user + DIR_DELIM + "games" + DIR_DELIM + id, true));
64         find_paths.push_back(GameFindPath(
65                         share + DIR_DELIM + "games" + DIR_DELIM + id + "_game", false));
66         find_paths.push_back(GameFindPath(
67                         share + DIR_DELIM + "games" + DIR_DELIM + id, false));
68         // Find game directory
69         std::string game_path;
70         bool user_game = true; // Game is in user's directory
71         for(u32 i=0; i<find_paths.size(); i++){
72                 const std::string &try_path = find_paths[i].path;
73                 if(fs::PathExists(try_path)){
74                         game_path = try_path;
75                         user_game = find_paths[i].user_specific;
76                         break;
77                 }
78         }
79         if(game_path == "")
80                 return SubgameSpec();
81         std::string gamemod_path = game_path + DIR_DELIM + "mods";
82         // Find mod directories
83         std::set<std::string> mods_paths;
84         if(!user_game)
85                 mods_paths.insert(share + DIR_DELIM + "mods" + DIR_DELIM + id);
86         if(user != share || user_game)
87                 mods_paths.insert(user + DIR_DELIM + "mods" + DIR_DELIM + id);
88         std::string game_name = getGameName(game_path);
89         if(game_name == "")
90                 game_name = id;
91         return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name);
92 }
93
94 SubgameSpec findWorldSubgame(const std::string &world_path)
95 {
96         std::string world_gameid = getWorldGameId(world_path, true);
97         // See if world contains an embedded game; if so, use it.
98         std::string world_gamepath = world_path + DIR_DELIM + "game";
99         if(fs::PathExists(world_gamepath)){
100                 SubgameSpec gamespec;
101                 gamespec.id = world_gameid;
102                 gamespec.path = world_gamepath;
103                 gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
104                 gamespec.name = getGameName(world_gamepath);
105                 if(gamespec.name == "")
106                         gamespec.name = "unknown";
107                 return gamespec;
108         }
109         return findSubgame(world_gameid);
110 }
111
112 std::set<std::string> getAvailableGameIds()
113 {
114         std::set<std::string> gameids;
115         std::set<std::string> gamespaths;
116         gamespaths.insert(porting::path_share + DIR_DELIM + "games");
117         gamespaths.insert(porting::path_user + DIR_DELIM + "games");
118         for(std::set<std::string>::const_iterator i = gamespaths.begin();
119                         i != gamespaths.end(); i++){
120                 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
121                 for(u32 j=0; j<dirlist.size(); j++){
122                         if(!dirlist[j].dir)
123                                 continue;
124                         // If configuration file is not found or broken, ignore game
125                         Settings conf;
126                         if(!getGameConfig(*i + DIR_DELIM + dirlist[j].name, conf))
127                                 continue;
128                         // Add it to result
129                         const char *ends[] = {"_game", NULL};
130                         std::string shorter = removeStringEnd(dirlist[j].name, ends);
131                         if(shorter != "")
132                                 gameids.insert(shorter);
133                         else
134                                 gameids.insert(dirlist[j].name);
135                 }
136         }
137         return gameids;
138 }
139
140 std::vector<SubgameSpec> getAvailableGames()
141 {
142         std::vector<SubgameSpec> specs;
143         std::set<std::string> gameids = getAvailableGameIds();
144         for(std::set<std::string>::const_iterator i = gameids.begin();
145                         i != gameids.end(); i++)
146                 specs.push_back(findSubgame(*i));
147         return specs;
148 }
149
150 #define LEGACY_GAMEID "minetest"
151
152 bool getWorldExists(const std::string &world_path)
153 {
154         return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
155                         fs::PathExists(world_path + DIR_DELIM + "world.mt"));
156 }
157
158 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
159 {
160         std::string conf_path = world_path + DIR_DELIM + "world.mt";
161         Settings conf;
162         bool succeeded = conf.readConfigFile(conf_path.c_str());
163         if(!succeeded){
164                 if(can_be_legacy){
165                         // If map_meta.txt exists, it is probably an old minetest world
166                         if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
167                                 return LEGACY_GAMEID;
168                 }
169                 return "";
170         }
171         if(!conf.exists("gameid"))
172                 return "";
173         // The "mesetint" gameid has been discarded
174         if(conf.get("gameid") == "mesetint")
175                 return "minetest";
176         return conf.get("gameid");
177 }
178
179 std::vector<WorldSpec> getAvailableWorlds()
180 {
181         std::vector<WorldSpec> worlds;
182         std::set<std::string> worldspaths;
183         worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
184         infostream<<"Searching worlds..."<<std::endl;
185         for(std::set<std::string>::const_iterator i = worldspaths.begin();
186                         i != worldspaths.end(); i++){
187                 infostream<<"  In "<<(*i)<<": "<<std::endl;
188                 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
189                 for(u32 j=0; j<dirvector.size(); j++){
190                         if(!dirvector[j].dir)
191                                 continue;
192                         std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
193                         std::string name = dirvector[j].name;
194                         // Just allow filling in the gameid always for now
195                         bool can_be_legacy = true;
196                         std::string gameid = getWorldGameId(fullpath, can_be_legacy);
197                         WorldSpec spec(fullpath, name, gameid);
198                         if(!spec.isValid()){
199                                 infostream<<"(invalid: "<<name<<") ";
200                         } else {
201                                 infostream<<name<<" ";
202                                 worlds.push_back(spec);
203                         }
204                 }
205                 infostream<<std::endl;
206         }
207         // Check old world location
208         do{
209                 std::string fullpath = porting::path_user + DIR_DELIM + "world";
210                 if(!fs::PathExists(fullpath))
211                         break;
212                 std::string name = "Old World";
213                 std::string gameid = getWorldGameId(fullpath, true);
214                 WorldSpec spec(fullpath, name, gameid);
215                 infostream<<"Old world found."<<std::endl;
216                 worlds.push_back(spec);
217         }while(0);
218         infostream<<worlds.size()<<" found."<<std::endl;
219         return worlds;
220 }
221
222 bool initializeWorld(const std::string &path, const std::string &gameid)
223 {
224         infostream<<"Initializing world at "<<path<<std::endl;
225         // Create world.mt if does not already exist
226         std::string worldmt_path = path + DIR_DELIM + "world.mt";
227         if(!fs::PathExists(worldmt_path)){
228                 infostream<<"Creating world.mt ("<<worldmt_path<<")"<<std::endl;
229                 fs::CreateAllDirs(path);
230                 std::ofstream of(worldmt_path.c_str(), std::ios::binary);
231                 of<<"gameid = "<<gameid<<"\n";
232         }
233         return true;
234 }
235
236