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