Updated addon repository URL and improved debug output on download
[supertux.git] / src / supertux / menu / contrib_menu.cpp
1 //  SuperTux
2 //  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmail.com>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "supertux/menu/contrib_menu.hpp"
18
19 #include <physfs.h>
20 #include <sstream>
21
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"
32
33 ContribMenu::ContribMenu() :
34   m_contrib_worlds()
35 {
36   // Generating contrib levels list by making use of Level Subset
37   std::vector<std::string> level_worlds;
38
39   std::unique_ptr<char*, decltype(&PHYSFS_freeList)>
40     files(PHYSFS_enumerateFiles("levels"),
41           PHYSFS_freeList);
42   for(const char* const* filename = files.get(); *filename != 0; ++filename)
43   {
44     std::string filepath = FileSystem::join("levels", *filename);
45     if(PHYSFS_isDirectory(filepath.c_str()))
46     {
47       level_worlds.push_back(filepath);
48     }
49   }
50
51   add_label(_("Contrib Levels"));
52   add_hl();
53
54   int i = 0;
55   for (std::vector<std::string>::const_iterator it = level_worlds.begin(); it != level_worlds.end(); ++it)
56   {
57     try
58     {
59       std::unique_ptr<World> world = World::load(*it);
60
61       if (!world->hide_from_contribs())
62       {
63         Savegame savegame(world->get_savegame_filename());
64         savegame.load();
65
66         if (world->is_levelset())
67         {
68           int level_count = 0;
69           int solved_count = 0;
70
71           const auto& state = savegame.get_levelset_state(world->get_basedir());
72           for(const auto& level_state : state.level_states)
73           {
74             if (level_state.solved)
75             {
76               solved_count += 1;
77             }
78             level_count += 1;
79           }
80
81           std::ostringstream title;
82           title << "[" << world->get_title() << "]";
83           if (level_count == 0)
84           {
85             title << " *NEW*";
86           }
87           else
88           {
89             title << " (" << solved_count << "/" << level_count << ")";
90           }
91           add_entry(i++, title.str());
92           m_contrib_worlds.push_back(std::move(world));
93         }
94         else if (world->is_worldmap())
95         {
96           int level_count = 0;
97           int solved_count = 0;
98
99           const auto& state = savegame.get_worldmap_state(world->get_worldmap_filename());
100           for(const auto& level_state : state.level_states)
101           {
102             if (level_state.solved)
103             {
104               solved_count += 1;
105             }
106             level_count += 1;
107           }
108
109           std::ostringstream title;
110           title << world->get_title();
111           if (level_count == 0)
112           {
113             title << " *NEW*";
114           }
115           else
116           {
117             title << " (" << solved_count << "/" << level_count << ")";
118           }
119           add_entry(i++, title.str());
120           m_contrib_worlds.push_back(std::move(world));
121         }
122         else
123         {
124           log_warning << "unknown World type" << std::endl;
125         }
126       }
127     }
128     catch(std::exception& e)
129     {
130       log_info << "Couldn't parse levelset info for '" << *it << "': " << e.what() << std::endl;
131     }
132   }
133
134   add_hl();
135   add_back(_("Back"));
136 }
137
138 ContribMenu::~ContribMenu()
139 {
140 }
141
142 void
143 ContribMenu::menu_action(MenuItem* item)
144 {
145   int index = item->id;
146   if (index != -1)
147   {
148     // reload the World so that we have something that we can safely
149     // std::move() around without wreaking the ContribMenu
150     std::unique_ptr<World> world = World::load(m_contrib_worlds[index]->get_basedir());
151     if (!world->is_levelset())
152     {
153       GameManager::current()->start_worldmap(std::move(world));
154     }
155     else
156     {
157       MenuManager::instance().push_menu(std::unique_ptr<Menu>(new ContribLevelsetMenu(std::move(world))));
158     }
159   }
160 }
161
162 /* EOF */