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