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