command-line/world game selection
[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
25 SubgameSpec findSubgame(const std::string &id)
26 {
27         if(id == "")
28                 return SubgameSpec();
29         std::string share_server = porting::path_share + DIR_DELIM + "server";
30         std::string user_server = porting::path_user + DIR_DELIM + "server";
31         // Find game directory
32         std::string game_path =
33                         user_server + 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_server + 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_server + DIR_DELIM + "addons"
45                                 + DIR_DELIM + id);
46         addon_paths.insert(user_server + DIR_DELIM + "addons"
47                         + DIR_DELIM + id);
48         return SubgameSpec(id, game_path, addon_paths);
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 + "server"
56                         + DIR_DELIM + "games");
57         gamespaths.insert(porting::path_user + DIR_DELIM + "server"
58                         + 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::string getWorldGameId(const std::string &world_path)
72 {
73         std::string conf_path = world_path + DIR_DELIM + "world.mt";
74         Settings conf;
75         bool succeeded = conf.readConfigFile(conf_path.c_str());
76         if(!succeeded)
77                 return "";
78         if(!conf.exists("gameid"))
79                 return "";
80         return conf.get("gameid");
81 }
82