Tune caves
[oweals/minetest.git] / src / subgame.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2012 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 "utility_string.h"
26
27 std::string getGameName(const std::string &game_path)
28 {
29         std::string conf_path = game_path + DIR_DELIM + "game.conf";
30         Settings conf;
31         bool succeeded = conf.readConfigFile(conf_path.c_str());
32         if(!succeeded)
33                 return "";
34         if(!conf.exists("name"))
35                 return "";
36         return conf.get("name");
37 }
38
39 struct GameFindPath
40 {
41         std::string path;
42         bool user_specific;
43         GameFindPath(const std::string &path, bool user_specific):
44                 path(path),
45                 user_specific(user_specific)
46         {}
47 };
48
49 SubgameSpec findSubgame(const std::string &id)
50 {
51         if(id == "")
52                 return SubgameSpec();
53         std::string share = porting::path_share;
54         std::string user = porting::path_user;
55         std::vector<GameFindPath> find_paths;
56         find_paths.push_back(GameFindPath(
57                         user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true));
58         find_paths.push_back(GameFindPath(
59                         user + DIR_DELIM + "games" + DIR_DELIM + id, true));
60         find_paths.push_back(GameFindPath(
61                         share + DIR_DELIM + "games" + DIR_DELIM + id + "_game", false));
62         find_paths.push_back(GameFindPath(
63                         share + DIR_DELIM + "games" + DIR_DELIM + id, false));
64         // Find game directory
65         std::string game_path;
66         bool user_game = true; // Game is in user's directory
67         for(u32 i=0; i<find_paths.size(); i++){
68                 const std::string &try_path = find_paths[i].path;
69                 if(fs::PathExists(try_path)){
70                         game_path = try_path;
71                         user_game = find_paths[i].user_specific;
72                         break;
73                 }
74         }
75         if(game_path == "")
76                 return SubgameSpec();
77         // Find mod directories
78         std::set<std::string> mods_paths;
79         mods_paths.insert(game_path + DIR_DELIM + "mods");
80         if(!user_game)
81                 mods_paths.insert(share + DIR_DELIM + "mods" + DIR_DELIM + id);
82         if(user != share || user_game)
83                 mods_paths.insert(user + DIR_DELIM + "mods" + DIR_DELIM + id);
84         std::string game_name = getGameName(game_path);
85         if(game_name == "")
86                 game_name = id;
87         return SubgameSpec(id, game_path, mods_paths, game_name);
88 }
89
90 std::set<std::string> getAvailableGameIds()
91 {
92         std::set<std::string> gameids;
93         std::set<std::string> gamespaths;
94         gamespaths.insert(porting::path_share + DIR_DELIM + "games");
95         gamespaths.insert(porting::path_user + DIR_DELIM + "games");
96         for(std::set<std::string>::const_iterator i = gamespaths.begin();
97                         i != gamespaths.end(); i++){
98                 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
99                 for(u32 j=0; j<dirlist.size(); j++){
100                         if(!dirlist[j].dir)
101                                 continue;
102                         const char *ends[] = {"_game", NULL};
103                         std::string shorter = removeStringEnd(dirlist[j].name, ends);
104                         if(shorter != "")
105                                 gameids.insert(shorter);
106                         else
107                                 gameids.insert(dirlist[j].name);
108                 }
109         }
110         return gameids;
111 }
112
113 std::vector<SubgameSpec> getAvailableGames()
114 {
115         std::vector<SubgameSpec> specs;
116         std::set<std::string> gameids = getAvailableGameIds();
117         for(std::set<std::string>::const_iterator i = gameids.begin();
118                         i != gameids.end(); i++)
119                 specs.push_back(findSubgame(*i));
120         return specs;
121 }
122
123 #define LEGACY_GAMEID "minetest"
124
125 bool getWorldExists(const std::string &world_path)
126 {
127         return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
128                         fs::PathExists(world_path + DIR_DELIM + "world.mt"));
129 }
130
131 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
132 {
133         std::string conf_path = world_path + DIR_DELIM + "world.mt";
134         Settings conf;
135         bool succeeded = conf.readConfigFile(conf_path.c_str());
136         if(!succeeded){
137                 if(can_be_legacy){
138                         // If map_meta.txt exists, it is probably an old minetest world
139                         if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
140                                 return LEGACY_GAMEID;
141                 }
142                 return "";
143         }
144         if(!conf.exists("gameid"))
145                 return "";
146         // The "mesetint" gameid has been discarded
147         if(conf.get("gameid") == "mesetint")
148                 return "minetest";
149         return conf.get("gameid");
150 }
151
152 std::vector<WorldSpec> getAvailableWorlds()
153 {
154         std::vector<WorldSpec> worlds;
155         std::set<std::string> worldspaths;
156         worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
157         infostream<<"Searching worlds..."<<std::endl;
158         for(std::set<std::string>::const_iterator i = worldspaths.begin();
159                         i != worldspaths.end(); i++){
160                 infostream<<"  In "<<(*i)<<": "<<std::endl;
161                 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
162                 for(u32 j=0; j<dirvector.size(); j++){
163                         if(!dirvector[j].dir)
164                                 continue;
165                         std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
166                         std::string name = dirvector[j].name;
167                         // Just allow filling in the gameid always for now
168                         bool can_be_legacy = true;
169                         std::string gameid = getWorldGameId(fullpath, can_be_legacy);
170                         WorldSpec spec(fullpath, name, gameid);
171                         if(!spec.isValid()){
172                                 infostream<<"(invalid: "<<name<<") ";
173                         } else {
174                                 infostream<<name<<" ";
175                                 worlds.push_back(spec);
176                         }
177                 }
178                 infostream<<std::endl;
179         }
180         // Check old world location
181         do{
182                 std::string fullpath = porting::path_user + DIR_DELIM + "world";
183                 if(!fs::PathExists(fullpath))
184                         break;
185                 std::string name = "Old World";
186                 std::string gameid = getWorldGameId(fullpath, true);
187                 WorldSpec spec(fullpath, name, gameid);
188                 infostream<<"Old world found."<<std::endl;
189                 worlds.push_back(spec);
190         }while(0);
191         infostream<<worlds.size()<<" found."<<std::endl;
192         return worlds;
193 }
194
195 bool initializeWorld(const std::string &path, const std::string &gameid)
196 {
197         infostream<<"Initializing world at "<<path<<std::endl;
198         // Create world.mt if does not already exist
199         std::string worldmt_path = path + DIR_DELIM + "world.mt";
200         if(!fs::PathExists(worldmt_path)){
201                 infostream<<"Creating world.mt ("<<worldmt_path<<")"<<std::endl;
202                 fs::CreateAllDirs(path);
203                 std::ofstream of(worldmt_path.c_str(), std::ios::binary);
204                 of<<"gameid = "<<gameid<<"\n";
205         }
206         return true;
207 }
208
209