3 Copyright (C) 2012 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
26 SubgameSpec findSubgame(const std::string &id)
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;
40 if(!fs::PathExists(game_path))
42 // Find addon directories
43 std::set<std::string> addon_paths;
45 addon_paths.insert(share_server + DIR_DELIM + "addons"
47 addon_paths.insert(user_server + DIR_DELIM + "addons"
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);
54 std::set<std::string> getAvailableGameIds()
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++){
68 gameids.insert(dirlist[j].name);
74 std::vector<SubgameSpec> getAvailableGames()
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));
84 #define LEGACY_GAMEID "mesetint"
86 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
88 std::string conf_path = world_path + DIR_DELIM + "world.mt";
90 bool succeeded = conf.readConfigFile(conf_path.c_str());
93 // If map_meta.txt exists, it is probably an old minetest world
94 if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
99 if(!conf.exists("gameid"))
101 return conf.get("gameid");
104 std::vector<WorldSpec> getAvailableWorlds()
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)
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);
123 infostream<<"(invalid: "<<name<<") ";
125 infostream<<name<<" ";
126 worlds.push_back(spec);
129 infostream<<std::endl;
131 // Check old world location
133 std::string fullpath = porting::path_user + DIR_DELIM + ".."
134 + DIR_DELIM + "world";
135 if(!fs::PathExists(fullpath))
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);
143 infostream<<worlds.size()<<" found."<<std::endl;
147 bool initializeWorld(const std::string &path, const std::string &gameid)
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";