2 // Copyright (C) 2009 Ingo Ruhnke <grumbel@gmail.com>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "supertux/menu/contrib_menu.hpp"
22 #include "gui/menu_item.hpp"
23 #include "gui/menu_manager.hpp"
24 #include "supertux/game_manager.hpp"
25 #include "supertux/gameconfig.hpp"
26 #include "supertux/menu/contrib_levelset_menu.hpp"
27 #include "supertux/menu/menu_storage.hpp"
28 #include "supertux/title_screen.hpp"
29 #include "supertux/world.hpp"
30 #include "util/file_system.hpp"
31 #include "util/gettext.hpp"
33 ContribMenu::ContribMenu() :
36 /** Generating contrib levels list by making use of Level Subset */
37 std::vector<std::string> level_worlds;
38 char** files = PHYSFS_enumerateFiles("levels/");
39 for(const char* const* filename = files; *filename != 0; ++filename) {
40 std::string filepath = std::string("levels/") + *filename;
41 if(PHYSFS_isDirectory(filepath.c_str()))
42 level_worlds.push_back(filepath);
44 PHYSFS_freeList(files);
46 add_label(_("Contrib Levels"));
50 for (std::vector<std::string>::const_iterator it = level_worlds.begin(); it != level_worlds.end(); ++it)
54 std::unique_ptr<World> world = World::load(*it);
56 if (!world->hide_from_contribs())
58 Savegame savegame(world->get_savegame_filename());
61 if (world->is_levelset())
66 const auto& state = savegame.get_levelset_state(world->get_basedir());
67 for(const auto& level_state : state.level_states)
69 if (level_state.solved)
76 std::ostringstream title;
77 title << "[" << world->get_title() << "]";
84 title << " (" << solved_count << "/" << level_count << ")";
86 add_entry(i++, title.str());
87 m_contrib_worlds.push_back(std::move(world));
89 else if (world->is_worldmap())
94 const auto& state = savegame.get_worldmap_state(world->get_worldmap_filename());
95 for(const auto& level_state : state.level_states)
97 if (level_state.solved)
104 std::ostringstream title;
105 title << world->get_title();
106 if (level_count == 0)
112 title << " (" << solved_count << "/" << level_count << ")";
114 add_entry(i++, title.str());
115 m_contrib_worlds.push_back(std::move(world));
119 log_warning << "unknown World type" << std::endl;
123 catch(std::exception& e)
125 log_info << "Couldn't parse levelset info for '" << *it << "': " << e.what() << std::endl;
133 ContribMenu::~ContribMenu()
138 ContribMenu::menu_action(MenuItem* item)
140 int index = item->id;
143 // reload the World so that we have something that we can safely
144 // std::move() around without wreaking the ContribMenu
145 std::unique_ptr<World> world = World::load(m_contrib_worlds[index]->get_basedir());
146 if (!world->is_levelset())
148 GameManager::current()->start_worldmap(std::move(world));
152 MenuManager::instance().push_menu(std::unique_ptr<Menu>(new ContribLevelsetMenu(std::move(world))));