Fix for MSVC and move stuff around a bit in CMakeLists.txt and src/CMakeLists.txt
[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
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 addon directories
42         std::set<std::string> addon_paths;
43         if(!user_game)
44                 addon_paths.insert(share + DIR_DELIM + "addons" + DIR_DELIM + id);
45         addon_paths.insert(user + DIR_DELIM + "addons" + DIR_DELIM + id);
46         // TODO: Read proper name from game_path/game.conf
47         std::string game_name = id;
48         return SubgameSpec(id, game_path, addon_paths, game_name);
49 }
50
51 std::set<std::string> getAvailableGameIds()
52 {
53         std::set<std::string> gameids;
54         std::set<std::string> gamespaths;
55         gamespaths.insert(porting::path_share + DIR_DELIM + "games");
56         gamespaths.insert(porting::path_user + DIR_DELIM + "games");
57         for(std::set<std::string>::const_iterator i = gamespaths.begin();
58                         i != gamespaths.end(); i++){
59                 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
60                 for(u32 j=0; j<dirlist.size(); j++){
61                         if(!dirlist[j].dir)
62                                 continue;
63                         gameids.insert(dirlist[j].name);
64                 }
65         }
66         return gameids;
67 }
68
69 std::vector<SubgameSpec> getAvailableGames()
70 {
71         std::vector<SubgameSpec> specs;
72         std::set<std::string> gameids = getAvailableGameIds();
73         for(std::set<std::string>::const_iterator i = gameids.begin();
74                         i != gameids.end(); i++)
75                 specs.push_back(findSubgame(*i));
76         return specs;
77 }
78
79 #define LEGACY_GAMEID "mesetint"
80
81 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
82 {
83         std::string conf_path = world_path + DIR_DELIM + "world.mt";
84         Settings conf;
85         bool succeeded = conf.readConfigFile(conf_path.c_str());
86         if(!succeeded){
87                 if(can_be_legacy){
88                         // If map_meta.txt exists, it is probably an old minetest world
89                         if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
90                                 return LEGACY_GAMEID;
91                 }
92                 return "";
93         }
94         if(!conf.exists("gameid"))
95                 return "";
96         return conf.get("gameid");
97 }
98
99 std::vector<WorldSpec> getAvailableWorlds()
100 {
101         std::vector<WorldSpec> worlds;
102         std::set<std::string> worldspaths;
103         worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
104         infostream<<"Searching worlds..."<<std::endl;
105         for(std::set<std::string>::const_iterator i = worldspaths.begin();
106                         i != worldspaths.end(); i++){
107                 infostream<<"  In "<<(*i)<<": "<<std::endl;
108                 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
109                 for(u32 j=0; j<dirvector.size(); j++){
110                         if(!dirvector[j].dir)
111                                 continue;
112                         std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
113                         std::string name = dirvector[j].name;
114                         std::string gameid = getWorldGameId(fullpath);
115                         WorldSpec spec(fullpath, name, gameid);
116                         if(!spec.isValid()){
117                                 infostream<<"(invalid: "<<name<<") ";
118                         } else {
119                                 infostream<<name<<" ";
120                                 worlds.push_back(spec);
121                         }
122                 }
123                 infostream<<std::endl;
124         }
125         // Check old world location
126         do{
127                 std::string fullpath = porting::path_user + DIR_DELIM + "world";
128                 if(!fs::PathExists(fullpath))
129                         break;
130                 std::string name = "Old World";
131                 std::string gameid = getWorldGameId(fullpath, true);
132                 WorldSpec spec(fullpath, name, gameid);
133                 infostream<<"Old world found."<<std::endl;
134                 worlds.push_back(spec);
135         }while(0);
136         infostream<<worlds.size()<<" found."<<std::endl;
137         return worlds;
138 }
139
140 bool initializeWorld(const std::string &path, const std::string &gameid)
141 {
142         infostream<<"Initializing world at "<<path<<std::endl;
143         // Create world.mt if does not already exist
144         std::string worldmt_path = path + DIR_DELIM + "world.mt";
145         if(!fs::PathExists(worldmt_path)){
146                 infostream<<"Creating world.mt ("<<worldmt_path<<")"<<std::endl;
147                 fs::CreateAllDirs(path);
148                 std::ofstream of(worldmt_path.c_str(), std::ios::binary);
149                 of<<"gameid = "<<gameid<<"\n";
150         }
151         return true;
152 }
153
154