]> git.lizzy.rs Git - minetest.git/blob - src/subgame.cpp
9a8ebe69f2de1c1389bd0bab65b79b6e7feb2d5e
[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
26 std::string getGameName(const std::string &game_path)
27 {
28         std::string conf_path = game_path + DIR_DELIM + "game.conf";
29         Settings conf;
30         bool succeeded = conf.readConfigFile(conf_path.c_str());
31         if(!succeeded)
32                 return "";
33         if(!conf.exists("name"))
34                 return "";
35         return conf.get("name");
36 }
37
38 SubgameSpec findSubgame(const std::string &id)
39 {
40         if(id == "")
41                 return SubgameSpec();
42         std::string share = porting::path_share;
43         std::string user = porting::path_user;
44         // Find game directory
45         std::string game_path = user + DIR_DELIM + "games" + DIR_DELIM + id;
46         bool user_game = true; // Game is in user's directory
47         if(!fs::PathExists(game_path)){
48                 game_path = share + DIR_DELIM + "games" + DIR_DELIM + id;
49                 user_game = false;
50         }
51         if(!fs::PathExists(game_path))
52                 return SubgameSpec();
53         // Find mod directories
54         std::set<std::string> mods_paths;
55         mods_paths.insert(game_path + DIR_DELIM + "mods");
56         if(!user_game)
57                 mods_paths.insert(share + DIR_DELIM + "mods" + DIR_DELIM + id);
58         if(user != share || user_game)
59                 mods_paths.insert(user + DIR_DELIM + "mods" + DIR_DELIM + id);
60         // TODO: Read proper name from game_path/game.conf
61         std::string game_name = getGameName(game_path);
62         if(game_name == "")
63                 game_name = id;
64         return SubgameSpec(id, game_path, mods_paths, game_name);
65 }
66
67 std::set<std::string> getAvailableGameIds()
68 {
69         std::set<std::string> gameids;
70         std::set<std::string> gamespaths;
71         gamespaths.insert(porting::path_share + DIR_DELIM + "games");
72         gamespaths.insert(porting::path_user + DIR_DELIM + "games");
73         for(std::set<std::string>::const_iterator i = gamespaths.begin();
74                         i != gamespaths.end(); i++){
75                 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
76                 for(u32 j=0; j<dirlist.size(); j++){
77                         if(!dirlist[j].dir)
78                                 continue;
79                         gameids.insert(dirlist[j].name);
80                 }
81         }
82         return gameids;
83 }
84
85 std::vector<SubgameSpec> getAvailableGames()
86 {
87         std::vector<SubgameSpec> specs;
88         std::set<std::string> gameids = getAvailableGameIds();
89         for(std::set<std::string>::const_iterator i = gameids.begin();
90                         i != gameids.end(); i++)
91                 specs.push_back(findSubgame(*i));
92         return specs;
93 }
94
95 #define LEGACY_GAMEID "minetest"
96
97 bool getWorldExists(const std::string &world_path)
98 {
99         return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
100                         fs::PathExists(world_path + DIR_DELIM + "world.mt"));
101 }
102
103 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
104 {
105         std::string conf_path = world_path + DIR_DELIM + "world.mt";
106         Settings conf;
107         bool succeeded = conf.readConfigFile(conf_path.c_str());
108         if(!succeeded){
109                 if(can_be_legacy){
110                         // If map_meta.txt exists, it is probably an old minetest world
111                         if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
112                                 return LEGACY_GAMEID;
113                 }
114                 return "";
115         }
116         if(!conf.exists("gameid"))
117                 return "";
118         // The "mesetint" gameid has been discarded
119         if(conf.get("gameid") == "mesetint")
120                 return "minetest";
121         return conf.get("gameid");
122 }
123
124 std::vector<WorldSpec> getAvailableWorlds()
125 {
126         std::vector<WorldSpec> worlds;
127         std::set<std::string> worldspaths;
128         worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
129         infostream<<"Searching worlds..."<<std::endl;
130         for(std::set<std::string>::const_iterator i = worldspaths.begin();
131                         i != worldspaths.end(); i++){
132                 infostream<<"  In "<<(*i)<<": "<<std::endl;
133                 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
134                 for(u32 j=0; j<dirvector.size(); j++){
135                         if(!dirvector[j].dir)
136                                 continue;
137                         std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
138                         std::string name = dirvector[j].name;
139                         // Just allow filling in the gameid always for now
140                         bool can_be_legacy = true;
141                         std::string gameid = getWorldGameId(fullpath, can_be_legacy);
142                         WorldSpec spec(fullpath, name, gameid);
143                         if(!spec.isValid()){
144                                 infostream<<"(invalid: "<<name<<") ";
145                         } else {
146                                 infostream<<name<<" ";
147                                 worlds.push_back(spec);
148                         }
149                 }
150                 infostream<<std::endl;
151         }
152         // Check old world location
153         do{
154                 std::string fullpath = porting::path_user + DIR_DELIM + "world";
155                 if(!fs::PathExists(fullpath))
156                         break;
157                 std::string name = "Old World";
158                 std::string gameid = getWorldGameId(fullpath, true);
159                 WorldSpec spec(fullpath, name, gameid);
160                 infostream<<"Old world found."<<std::endl;
161                 worlds.push_back(spec);
162         }while(0);
163         infostream<<worlds.size()<<" found."<<std::endl;
164         return worlds;
165 }
166
167 bool initializeWorld(const std::string &path, const std::string &gameid)
168 {
169         infostream<<"Initializing world at "<<path<<std::endl;
170         // Create world.mt if does not already exist
171         std::string worldmt_path = path + DIR_DELIM + "world.mt";
172         if(!fs::PathExists(worldmt_path)){
173                 infostream<<"Creating world.mt ("<<worldmt_path<<")"<<std::endl;
174                 fs::CreateAllDirs(path);
175                 std::ofstream of(worldmt_path.c_str(), std::ios::binary);
176                 of<<"gameid = "<<gameid<<"\n";
177         }
178         return true;
179 }
180
181