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