]> git.lizzy.rs Git - minetest.git/blob - src/subgame.cpp
Fix msvc2012 build
[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 #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");
95         if(user != share || user_game)
96                 mods_paths.insert(user + DIR_DELIM + "mods");
97         std::string game_name = getGameName(game_path);
98         if(game_name == "")
99                 game_name = id;
100         std::string menuicon_path;
101 #ifndef SERVER
102         menuicon_path = getImagePath(game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
103 #endif
104         return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
105                         menuicon_path);
106 }
107
108 SubgameSpec findWorldSubgame(const std::string &world_path)
109 {
110         std::string world_gameid = getWorldGameId(world_path, true);
111         // See if world contains an embedded game; if so, use it.
112         std::string world_gamepath = world_path + DIR_DELIM + "game";
113         if(fs::PathExists(world_gamepath)){
114                 SubgameSpec gamespec;
115                 gamespec.id = world_gameid;
116                 gamespec.path = world_gamepath;
117                 gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
118                 gamespec.name = getGameName(world_gamepath);
119                 if(gamespec.name == "")
120                         gamespec.name = "unknown";
121                 return gamespec;
122         }
123         return findSubgame(world_gameid);
124 }
125
126 std::set<std::string> getAvailableGameIds()
127 {
128         std::set<std::string> gameids;
129         std::set<std::string> gamespaths;
130         gamespaths.insert(porting::path_share + DIR_DELIM + "games");
131         gamespaths.insert(porting::path_user + DIR_DELIM + "games");
132         for(std::set<std::string>::const_iterator i = gamespaths.begin();
133                         i != gamespaths.end(); i++){
134                 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
135                 for(u32 j=0; j<dirlist.size(); j++){
136                         if(!dirlist[j].dir)
137                                 continue;
138                         // If configuration file is not found or broken, ignore game
139                         Settings conf;
140                         if(!getGameConfig(*i + DIR_DELIM + dirlist[j].name, conf))
141                                 continue;
142                         // Add it to result
143                         const char *ends[] = {"_game", NULL};
144                         std::string shorter = removeStringEnd(dirlist[j].name, ends);
145                         if(shorter != "")
146                                 gameids.insert(shorter);
147                         else
148                                 gameids.insert(dirlist[j].name);
149                 }
150         }
151         return gameids;
152 }
153
154 std::vector<SubgameSpec> getAvailableGames()
155 {
156         std::vector<SubgameSpec> specs;
157         std::set<std::string> gameids = getAvailableGameIds();
158         for(std::set<std::string>::const_iterator i = gameids.begin();
159                         i != gameids.end(); i++)
160                 specs.push_back(findSubgame(*i));
161         return specs;
162 }
163
164 #define LEGACY_GAMEID "minetest"
165
166 bool getWorldExists(const std::string &world_path)
167 {
168         return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
169                         fs::PathExists(world_path + DIR_DELIM + "world.mt"));
170 }
171
172 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
173 {
174         std::string conf_path = world_path + DIR_DELIM + "world.mt";
175         Settings conf;
176         bool succeeded = conf.readConfigFile(conf_path.c_str());
177         if(!succeeded){
178                 if(can_be_legacy){
179                         // If map_meta.txt exists, it is probably an old minetest world
180                         if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
181                                 return LEGACY_GAMEID;
182                 }
183                 return "";
184         }
185         if(!conf.exists("gameid"))
186                 return "";
187         // The "mesetint" gameid has been discarded
188         if(conf.get("gameid") == "mesetint")
189                 return "minetest";
190         return conf.get("gameid");
191 }
192
193 std::vector<WorldSpec> getAvailableWorlds()
194 {
195         std::vector<WorldSpec> worlds;
196         std::set<std::string> worldspaths;
197         worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
198         infostream<<"Searching worlds..."<<std::endl;
199         for(std::set<std::string>::const_iterator i = worldspaths.begin();
200                         i != worldspaths.end(); i++){
201                 infostream<<"  In "<<(*i)<<": "<<std::endl;
202                 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
203                 for(u32 j=0; j<dirvector.size(); j++){
204                         if(!dirvector[j].dir)
205                                 continue;
206                         std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
207                         std::string name = dirvector[j].name;
208                         // Just allow filling in the gameid always for now
209                         bool can_be_legacy = true;
210                         std::string gameid = getWorldGameId(fullpath, can_be_legacy);
211                         WorldSpec spec(fullpath, name, gameid);
212                         if(!spec.isValid()){
213                                 infostream<<"(invalid: "<<name<<") ";
214                         } else {
215                                 infostream<<name<<" ";
216                                 worlds.push_back(spec);
217                         }
218                 }
219                 infostream<<std::endl;
220         }
221         // Check old world location
222         do{
223                 std::string fullpath = porting::path_user + DIR_DELIM + "world";
224                 if(!fs::PathExists(fullpath))
225                         break;
226                 std::string name = "Old World";
227                 std::string gameid = getWorldGameId(fullpath, true);
228                 WorldSpec spec(fullpath, name, gameid);
229                 infostream<<"Old world found."<<std::endl;
230                 worlds.push_back(spec);
231         }while(0);
232         infostream<<worlds.size()<<" found."<<std::endl;
233         return worlds;
234 }
235
236 bool initializeWorld(const std::string &path, const std::string &gameid)
237 {
238         infostream<<"Initializing world at "<<path<<std::endl;
239         // Create world.mt if does not already exist
240         std::string worldmt_path = path + DIR_DELIM + "world.mt";
241         if(!fs::PathExists(worldmt_path)){
242                 infostream<<"Creating world.mt ("<<worldmt_path<<")"<<std::endl;
243                 fs::CreateAllDirs(path);
244                 std::ostringstream ss(std::ios_base::binary);
245                 ss<<"gameid = "<<gameid<<"\nbackend = sqlite3\n";
246                 fs::safeWriteToFile(worldmt_path, ss.str());
247         }
248         return true;
249 }
250
251