Keep proper track of MD5 checksums of addons
[supertux.git] / src / supertux / menu / contrib_menu.cpp
index 4078059..8bbc717 100644 (file)
@@ -1,5 +1,5 @@
 //  SuperTux
-//  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de>
+//  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmail.com>
 //
 //  This program is free software: you can redistribute it and/or modify
 //  it under the terms of the GNU General Public License as published by
 #include <physfs.h>
 #include <sstream>
 
+#include "gui/menu_item.hpp"
 #include "gui/menu_manager.hpp"
 #include "supertux/game_manager.hpp"
 #include "supertux/gameconfig.hpp"
-#include "supertux/menu/contrib_world_menu.hpp"
+#include "supertux/menu/contrib_levelset_menu.hpp"
 #include "supertux/menu/menu_storage.hpp"
 #include "supertux/title_screen.hpp"
 #include "supertux/world.hpp"
 ContribMenu::ContribMenu() :
   m_contrib_worlds()
 {
-  /** Generating contrib levels list by making use of Level Subset  */
+  // Generating contrib levels list by making use of Level Subset
   std::vector<std::string> level_worlds;
-  char** files = PHYSFS_enumerateFiles("levels/");
-  for(const char* const* filename = files; *filename != 0; ++filename) {
-    std::string filepath = std::string("levels/") + *filename;
+
+  std::unique_ptr<char*, decltype(&PHYSFS_freeList)>
+    files(PHYSFS_enumerateFiles("levels"),
+          PHYSFS_freeList);
+  for(const char* const* filename = files.get(); *filename != 0; ++filename)
+  {
+    std::string filepath = FileSystem::join("levels", *filename);
     if(PHYSFS_isDirectory(filepath.c_str()))
+    {
       level_worlds.push_back(filepath);
+    }
   }
-  PHYSFS_freeList(files);
 
   add_label(_("Contrib Levels"));
   add_hl();
@@ -50,25 +56,73 @@ ContribMenu::ContribMenu() :
   {
     try
     {
-      std::unique_ptr<World> world (new World);
-
-      world->load(*it + "/info");
+      std::unique_ptr<World> world = World::load(*it);
 
-      if (!world->hide_from_contribs
+      if (!world->hide_from_contribs())
       {
-        { // FIXME: yuck, this should be easier
-          std::ostringstream stream;
-          std::string worlddirname = FileSystem::basename(*it);
-          stream << "profile" << g_config->profile << "/" << worlddirname << ".stsg";
-          std::string slotfile = stream.str();
-          world->set_savegame_filename(stream.str());
-          world->load_state();
+        Savegame savegame(world->get_savegame_filename());
+        savegame.load();
+
+        if (world->is_levelset())
+        {
+          int level_count = 0;
+          int solved_count = 0;
+
+          const auto& state = savegame.get_levelset_state(world->get_basedir());
+          for(const auto& level_state : state.level_states)
+          {
+            if (level_state.solved)
+            {
+              solved_count += 1;
+            }
+            level_count += 1;
+          }
+
+          std::ostringstream title;
+          title << "[" << world->get_title() << "]";
+          if (level_count == 0)
+          {
+            title << " *NEW*";
+          }
+          else
+          {
+            title << " (" << solved_count << "/" << level_count << ")";
+          }
+          add_entry(i++, title.str());
+          m_contrib_worlds.push_back(std::move(world));
         }
+        else if (world->is_worldmap())
+        {
+          int level_count = 0;
+          int solved_count = 0;
+
+          const auto& state = savegame.get_worldmap_state(world->get_worldmap_filename());
+          for(const auto& level_state : state.level_states)
+          {
+            if (level_state.solved)
+            {
+              solved_count += 1;
+            }
+            level_count += 1;
+          }
 
-        std::ostringstream title;
-        title << world->get_title() << " (" << world->get_num_solved_levels() << "/" << world->get_num_levels() << ")";
-        add_entry(i++, title.str());
-        m_contrib_worlds.push_back(std::move(world));
+          std::ostringstream title;
+          title << world->get_title();
+          if (level_count == 0)
+          {
+            title << " *NEW*";
+          }
+          else
+          {
+            title << " (" << solved_count << "/" << level_count << ")";
+          }
+          add_entry(i++, title.str());
+          m_contrib_worlds.push_back(std::move(world));
+        }
+        else
+        {
+          log_warning << "unknown World type" << std::endl;
+        }
       }
     }
     catch(std::exception& e)
@@ -86,22 +140,21 @@ ContribMenu::~ContribMenu()
 }
 
 void
-ContribMenu::check_menu()
+ContribMenu::menu_action(MenuItem* item)
 {
-  int index = check();
+  int index = item->id;
   if (index != -1)
   {
-    World* world = m_contrib_worlds[index].get();
-    if (!world->is_levelset) 
+    // reload the World so that we have something that we can safely
+    // std::move() around without wreaking the ContribMenu
+    std::unique_ptr<World> world = World::load(m_contrib_worlds[index]->get_basedir());
+    if (!world->is_levelset())
     {
-      // FIXME: not the most elegant of solutions to std::move() the
-      // World, but the ContribMenu should get destructed after this,
-      // so it might be ok
-      GameManager::current()->start_game(std::move(m_contrib_worlds[index]));
+      GameManager::current()->start_worldmap(std::move(world));
     }
-    else 
+    else
     {
-      MenuManager::instance().push_menu(std::unique_ptr<Menu>(new ContribWorldMenu(std::move(m_contrib_worlds[index]))));
+      MenuManager::instance().push_menu(std::unique_ptr<Menu>(new ContribLevelsetMenu(std::move(world))));
     }
   }
 }