]> git.lizzy.rs Git - dragonfireclient.git/blob - src/subgame.cpp
ccf477e8f4476b8089898306d3f55b34b5b93e3a
[dragonfireclient.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 "main.h"
25 #include "log.h"
26 #include "strfnd.h"
27 #ifndef SERVER
28 #include "client/tile.h" // getImagePath
29 #endif
30 #include "util/string.h"
31
32 bool getGameMinetestConfig(const std::string &game_path, Settings &conf)
33 {
34         std::string conf_path = game_path + DIR_DELIM + "minetest.conf";
35         return conf.readConfigFile(conf_path.c_str());
36 }
37
38 bool getGameConfig(const std::string &game_path, Settings &conf)
39 {
40         std::string conf_path = game_path + DIR_DELIM + "game.conf";
41         return conf.readConfigFile(conf_path.c_str());
42 }
43
44 std::string getGameName(const std::string &game_path)
45 {
46         Settings conf;
47         if(!getGameConfig(game_path, conf))
48                 return "";
49         if(!conf.exists("name"))
50                 return "";
51         return conf.get("name");
52 }
53
54 struct GameFindPath
55 {
56         std::string path;
57         bool user_specific;
58         GameFindPath(const std::string &path, bool user_specific):
59                 path(path),
60                 user_specific(user_specific)
61         {}
62 };
63
64 Strfnd getSubgamePathEnv() {
65         std::string sp;
66         char *subgame_path = getenv("MINETEST_SUBGAME_PATH");
67
68         if(subgame_path) {
69                 sp = std::string(subgame_path);
70         }
71
72         return Strfnd(sp);
73 }
74
75 SubgameSpec findSubgame(const std::string &id)
76 {
77         if(id == "")
78                 return SubgameSpec();
79         std::string share = porting::path_share;
80         std::string user = porting::path_user;
81         std::vector<GameFindPath> find_paths;
82
83         Strfnd search_paths = getSubgamePathEnv();
84
85         while(!search_paths.atend()) {
86                 std::string path = search_paths.next(":");
87                 find_paths.push_back(GameFindPath(
88                                 path + DIR_DELIM + id, false));
89                 find_paths.push_back(GameFindPath(
90                                 path + DIR_DELIM + id + "_game", false));
91         }
92
93         find_paths.push_back(GameFindPath(
94                         user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true));
95         find_paths.push_back(GameFindPath(
96                         user + DIR_DELIM + "games" + DIR_DELIM + id, true));
97         find_paths.push_back(GameFindPath(
98                         share + DIR_DELIM + "games" + DIR_DELIM + id + "_game", false));
99         find_paths.push_back(GameFindPath(
100                         share + DIR_DELIM + "games" + DIR_DELIM + id, false));
101         // Find game directory
102         std::string game_path;
103         bool user_game = true; // Game is in user's directory
104         for(u32 i=0; i<find_paths.size(); i++){
105                 const std::string &try_path = find_paths[i].path;
106                 if(fs::PathExists(try_path)){
107                         game_path = try_path;
108                         user_game = find_paths[i].user_specific;
109                         break;
110                 }
111         }
112         if(game_path == "")
113                 return SubgameSpec();
114         std::string gamemod_path = game_path + DIR_DELIM + "mods";
115         // Find mod directories
116         std::set<std::string> mods_paths;
117         if(!user_game)
118                 mods_paths.insert(share + DIR_DELIM + "mods");
119         if(user != share || user_game)
120                 mods_paths.insert(user + DIR_DELIM + "mods");
121         std::string game_name = getGameName(game_path);
122         if(game_name == "")
123                 game_name = id;
124         std::string menuicon_path;
125 #ifndef SERVER
126         menuicon_path = getImagePath(game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
127 #endif
128         return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
129                         menuicon_path);
130 }
131
132 SubgameSpec findWorldSubgame(const std::string &world_path)
133 {
134         std::string world_gameid = getWorldGameId(world_path, true);
135         // See if world contains an embedded game; if so, use it.
136         std::string world_gamepath = world_path + DIR_DELIM + "game";
137         if(fs::PathExists(world_gamepath)){
138                 SubgameSpec gamespec;
139                 gamespec.id = world_gameid;
140                 gamespec.path = world_gamepath;
141                 gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
142                 gamespec.name = getGameName(world_gamepath);
143                 if(gamespec.name == "")
144                         gamespec.name = "unknown";
145                 return gamespec;
146         }
147         return findSubgame(world_gameid);
148 }
149
150 std::set<std::string> getAvailableGameIds()
151 {
152         std::set<std::string> gameids;
153         std::set<std::string> gamespaths;
154         gamespaths.insert(porting::path_share + DIR_DELIM + "games");
155         gamespaths.insert(porting::path_user + DIR_DELIM + "games");
156
157         Strfnd search_paths = getSubgamePathEnv();
158
159         while(!search_paths.atend()) {
160                 gamespaths.insert(search_paths.next(":"));
161         }
162
163         for(std::set<std::string>::const_iterator i = gamespaths.begin();
164                         i != gamespaths.end(); i++){
165                 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
166                 for(u32 j=0; j<dirlist.size(); j++){
167                         if(!dirlist[j].dir)
168                                 continue;
169                         // If configuration file is not found or broken, ignore game
170                         Settings conf;
171                         if(!getGameConfig(*i + DIR_DELIM + dirlist[j].name, conf))
172                                 continue;
173                         // Add it to result
174                         const char *ends[] = {"_game", NULL};
175                         std::string shorter = removeStringEnd(dirlist[j].name, ends);
176                         if(shorter != "")
177                                 gameids.insert(shorter);
178                         else
179                                 gameids.insert(dirlist[j].name);
180                 }
181         }
182         return gameids;
183 }
184
185 std::vector<SubgameSpec> getAvailableGames()
186 {
187         std::vector<SubgameSpec> specs;
188         std::set<std::string> gameids = getAvailableGameIds();
189         for(std::set<std::string>::const_iterator i = gameids.begin();
190                         i != gameids.end(); i++)
191                 specs.push_back(findSubgame(*i));
192         return specs;
193 }
194
195 #define LEGACY_GAMEID "minetest"
196
197 bool getWorldExists(const std::string &world_path)
198 {
199         return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
200                         fs::PathExists(world_path + DIR_DELIM + "world.mt"));
201 }
202
203 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
204 {
205         std::string conf_path = world_path + DIR_DELIM + "world.mt";
206         Settings conf;
207         bool succeeded = conf.readConfigFile(conf_path.c_str());
208         if(!succeeded){
209                 if(can_be_legacy){
210                         // If map_meta.txt exists, it is probably an old minetest world
211                         if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
212                                 return LEGACY_GAMEID;
213                 }
214                 return "";
215         }
216         if(!conf.exists("gameid"))
217                 return "";
218         // The "mesetint" gameid has been discarded
219         if(conf.get("gameid") == "mesetint")
220                 return "minetest";
221         return conf.get("gameid");
222 }
223
224 std::vector<WorldSpec> getAvailableWorlds()
225 {
226         std::vector<WorldSpec> worlds;
227         std::set<std::string> worldspaths;
228         worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
229         infostream<<"Searching worlds..."<<std::endl;
230         for(std::set<std::string>::const_iterator i = worldspaths.begin();
231                         i != worldspaths.end(); i++){
232                 infostream<<"  In "<<(*i)<<": "<<std::endl;
233                 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
234                 for(u32 j=0; j<dirvector.size(); j++){
235                         if(!dirvector[j].dir)
236                                 continue;
237                         std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
238                         std::string name = dirvector[j].name;
239                         // Just allow filling in the gameid always for now
240                         bool can_be_legacy = true;
241                         std::string gameid = getWorldGameId(fullpath, can_be_legacy);
242                         WorldSpec spec(fullpath, name, gameid);
243                         if(!spec.isValid()){
244                                 infostream<<"(invalid: "<<name<<") ";
245                         } else {
246                                 infostream<<name<<" ";
247                                 worlds.push_back(spec);
248                         }
249                 }
250                 infostream<<std::endl;
251         }
252         // Check old world location
253         do{
254                 std::string fullpath = porting::path_user + DIR_DELIM + "world";
255                 if(!fs::PathExists(fullpath))
256                         break;
257                 std::string name = "Old World";
258                 std::string gameid = getWorldGameId(fullpath, true);
259                 WorldSpec spec(fullpath, name, gameid);
260                 infostream<<"Old world found."<<std::endl;
261                 worlds.push_back(spec);
262         }while(0);
263         infostream<<worlds.size()<<" found."<<std::endl;
264         return worlds;
265 }
266
267 bool initializeWorld(const std::string &path, const std::string &gameid)
268 {
269         infostream << "Initializing world at " << path << std::endl;
270
271         fs::CreateAllDirs(path);
272
273         // Create world.mt if does not already exist
274         std::string worldmt_path = path + DIR_DELIM "world.mt";
275         if (!fs::PathExists(worldmt_path)) {
276                 std::ostringstream ss(std::ios_base::binary);
277                 ss << "gameid = " << gameid << "\nbackend = sqlite3\n";
278                 if (!fs::safeWriteToFile(worldmt_path, ss.str()))
279                         return false;
280
281                 infostream << "Wrote world.mt (" << worldmt_path << ")" << std::endl;
282         }
283
284         // Create map_meta.txt if does not already exist
285         std::string mapmeta_path = path + DIR_DELIM "map_meta.txt";
286         if (!fs::PathExists(mapmeta_path)) {
287                 std::ostringstream ss(std::ios_base::binary);
288                 ss
289                         << "mg_name = "       << g_settings->get("mg_name")
290                         << "\nseed = "        << g_settings->get("fixed_map_seed")
291                         << "\nchunksize = "   << g_settings->get("chunksize")
292                         << "\nwater_level = " << g_settings->get("water_level")
293                         << "\nmg_flags = "    << g_settings->get("mg_flags")
294                         << "\n[end_of_params]\n";
295                 if (!fs::safeWriteToFile(mapmeta_path, ss.str()))
296                         return false;
297
298                 infostream << "Wrote map_meta.txt (" << mapmeta_path << ")" << std::endl;
299         }
300
301         return true;
302 }