From 953e9538d9934449a142c412303407b10febbf1a Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Sun, 24 Aug 2014 04:12:02 +0200 Subject: [PATCH] Load/save the Addon enabled/disabled state to the config file --- src/addon/addon_manager.cpp | 19 +++++++++++++------ src/addon/addon_manager.hpp | 5 +++-- src/supertux/gameconfig.cpp | 32 +++++++++++++++++++++++++++++--- src/supertux/gameconfig.hpp | 7 ++++++- src/supertux/main.cpp | 2 +- 5 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/addon/addon_manager.cpp b/src/addon/addon_manager.cpp index 3c358d78d..7e1774691 100644 --- a/src/addon/addon_manager.cpp +++ b/src/addon/addon_manager.cpp @@ -72,12 +72,12 @@ bool has_suffix(const std::string& str, const std::string& suffix) } // namespace AddonManager::AddonManager(const std::string& addon_directory, - std::vector& ignored_addon_ids) : + std::vector& addon_config) : m_downloader(), m_addon_directory(addon_directory), //m_repository_url("http://addons.supertux.googlecode.com/git/index-0_3_5.nfo"), m_repository_url("http://localhost:8000/index-0_4_0.nfo"), - m_ignored_addon_ids(ignored_addon_ids), + m_addon_config(addon_config), m_installed_addons(), m_repository_addons(), m_has_been_updated(false) @@ -85,18 +85,25 @@ AddonManager::AddonManager(const std::string& addon_directory, PHYSFS_mkdir(m_addon_directory.c_str()); add_installed_addons(); - for(auto& addon : m_installed_addons) + + // FIXME: We should also restore the order here + for(auto& addon : m_addon_config) { - if (std::find(m_ignored_addon_ids.begin(), m_ignored_addon_ids.end(), - addon->get_id()) != m_ignored_addon_ids.end()) + if (addon.enabled) { - enable_addon(addon->get_id()); + enable_addon(addon.id); } } } AddonManager::~AddonManager() { + // sync enabled/disabled addons into the config for saving + m_addon_config.clear(); + for(auto& addon : m_installed_addons) + { + m_addon_config.push_back({addon->get_id(), addon->is_enabled()}); + } } Addon& diff --git a/src/addon/addon_manager.hpp b/src/addon/addon_manager.hpp index 419c401bb..b87d7ce28 100644 --- a/src/addon/addon_manager.hpp +++ b/src/addon/addon_manager.hpp @@ -23,6 +23,7 @@ #include #include "addon/downloader.hpp" +#include "supertux/gameconfig.hpp" #include "util/currenton.hpp" #include "util/reader_fwd.hpp" #include "util/writer_fwd.hpp" @@ -41,7 +42,7 @@ private: Downloader m_downloader; std::string m_addon_directory; std::string m_repository_url; - std::vector& m_ignored_addon_ids; + std::vector& m_addon_config; AddonList m_installed_addons; AddonList m_repository_addons; @@ -50,7 +51,7 @@ private: public: AddonManager(const std::string& addon_directory, - std::vector& enabled_addons_); + std::vector& addon_config); ~AddonManager(); bool has_online_support() const; diff --git a/src/supertux/gameconfig.cpp b/src/supertux/gameconfig.cpp index eea7955fb..879be82ba 100644 --- a/src/supertux/gameconfig.cpp +++ b/src/supertux/gameconfig.cpp @@ -21,8 +21,10 @@ #include "addon/addon_manager.hpp" #include "control/input_manager.hpp" #include "lisp/writer.hpp" +#include "lisp/list_iterator.hpp" #include "lisp/parser.hpp" #include "util/reader.hpp" +#include "util/log.hpp" #include "supertux/globals.hpp" Config::Config() : @@ -47,7 +49,7 @@ Config::Config() : locale(), keyboard_config(), joystick_config(), - disabled_addon_filenames(), + addons(), developer_mode(false) { } @@ -121,7 +123,25 @@ Config::load() const lisp::Lisp* config_addons_lisp = config_lisp->get_lisp("addons"); if (config_addons_lisp) { - config_addons_lisp->get("disabled-addons", disabled_addon_filenames); + lisp::ListIterator iter(config_addons_lisp); + while(iter.next()) + { + const std::string& token = iter.item(); + if (token == "addon") + { + std::string id; + bool enabled = false; + if (iter.lisp()->get("id", id) && + iter.lisp()->get("enabled", enabled)) + { + addons.push_back({id, enabled}); + } + } + else + { + log_warning << "Unknown token in config file: " << token << std::endl; + } + } } } @@ -175,7 +195,13 @@ Config::save() writer.end_list("control"); writer.start_list("addons"); - writer.write("disabled-addons", disabled_addon_filenames); + for(auto addon : addons) + { + writer.start_list("addon"); + writer.write("id", addon.id); + writer.write("enabled", addon.enabled); + writer.end_list("addon"); + } writer.end_list("addons"); writer.end_list("supertux-config"); diff --git a/src/supertux/gameconfig.hpp b/src/supertux/gameconfig.hpp index 9f9ffd28a..3b7dd06da 100644 --- a/src/supertux/gameconfig.hpp +++ b/src/supertux/gameconfig.hpp @@ -72,7 +72,12 @@ public: KeyboardConfig keyboard_config; JoystickConfig joystick_config; - std::vector disabled_addon_filenames; + struct Addon + { + std::string id; + bool enabled; + }; + std::vector addons; bool developer_mode; }; diff --git a/src/supertux/main.cpp b/src/supertux/main.cpp index d04eb5fb3..6dc7c3744 100644 --- a/src/supertux/main.cpp +++ b/src/supertux/main.cpp @@ -326,7 +326,7 @@ Main::launch_game() Resources resources; timelog("addons"); - AddonManager addon_manager("addons", g_config->disabled_addon_filenames); + AddonManager addon_manager("addons", g_config->addons); timelog(0); -- 2.11.0