Skip to content

Commit

Permalink
Use std::map for addons instead of list (#2565)
Browse files Browse the repository at this point in the history
* Use std::map for addons instead of list

* Use C++17 stuff

* More C++17
  • Loading branch information
tobbi authored Aug 1, 2023
1 parent a4d0d32 commit 54c198c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 93 deletions.
83 changes: 33 additions & 50 deletions src/addon/addon_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,14 @@ MD5 md5_from_archive(const std::string& filename)
}
}

static Addon& get_addon(const AddonManager::AddonList& list, const AddonId& id,
static Addon& get_addon(const AddonManager::AddonMap& list, const AddonId& id,
bool installed)
{
auto it = std::find_if(list.begin(), list.end(),
[&id](const std::unique_ptr<Addon>& addon)
{
return addon->get_id() == id;
});
auto it = list.find(id);

if (it != list.end())
{
return **it;
return *(it->second);
}
else
{
Expand All @@ -100,15 +96,14 @@ static Addon& get_addon(const AddonManager::AddonList& list, const AddonId& id,
}
}

static std::vector<AddonId> get_addons(const AddonManager::AddonList& list)
static std::vector<AddonId> get_addons(const AddonManager::AddonMap& list)
{
// Use a map for storing sorted addon titles with their respective IDs.
std::map<std::string, AddonId> sorted_titles;
std::for_each(list.begin(), list.end(),
[&](const std::unique_ptr<Addon>& addon)
{
sorted_titles.insert({addon->get_title(), addon->get_id()});
});
for (const auto& [id, addon] : list)
{
sorted_titles.insert({addon->get_title(), id});
}
std::vector<AddonId> results;
results.reserve(sorted_titles.size());
std::transform(sorted_titles.begin(), sorted_titles.end(),
Expand Down Expand Up @@ -223,9 +218,9 @@ AddonManager::~AddonManager()
{
// sync enabled/disabled add-ons into the config for saving
m_addon_config.clear();
for (const auto& addon : m_installed_addons)
for (const auto& [id, addon] : m_installed_addons)
{
m_addon_config.push_back({addon->get_id(), addon->is_enabled()});
m_addon_config.push_back({id, addon->is_enabled()});
}

// Delete the add-on cache directory, if it exists.
Expand Down Expand Up @@ -307,17 +302,13 @@ TransferStatusListPtr
AddonManager::request_install_addon(const AddonId& addon_id)
{
// remove addon if it already exists
auto it = std::find_if(m_installed_addons.begin(), m_installed_addons.end(),
[&addon_id](const std::unique_ptr<Addon>& addon)
{
return addon->get_id() == addon_id;
});
auto it = m_installed_addons.find(addon_id);
if (it != m_installed_addons.end())
{
log_debug << "reinstalling addon " << addon_id << std::endl;
if ((*it)->is_enabled())
if (it->second->is_enabled())
{
disable_addon((*it)->get_id());
disable_addon(it->first);
}
m_installed_addons.erase(it);
}
Expand Down Expand Up @@ -413,17 +404,13 @@ void
AddonManager::install_addon(const AddonId& addon_id)
{
{ // remove addon if it already exists
auto it = std::find_if(m_installed_addons.begin(), m_installed_addons.end(),
[&addon_id](const std::unique_ptr<Addon>& addon)
{
return addon->get_id() == addon_id;
});
auto it = m_installed_addons.find(addon_id);
if (it != m_installed_addons.end())
{
log_debug << "reinstalling addon " << addon_id << std::endl;
if ((*it)->is_enabled())
if (it->second->is_enabled())
{
disable_addon((*it)->get_id());
disable_addon(it->first);
}
m_installed_addons.erase(it);
}
Expand Down Expand Up @@ -489,11 +476,7 @@ AddonManager::uninstall_addon(const AddonId& addon_id)
disable_addon(addon_id);
}
log_debug << "deleting file \"" << addon.get_install_filename() << "\"" << std::endl;
const auto it = std::find_if(m_installed_addons.begin(), m_installed_addons.end(),
[&addon](const std::unique_ptr<Addon>& rhs)
{
return addon.get_id() == rhs->get_id();
});
const auto it = m_installed_addons.find(addon.get_id());
if (it != m_installed_addons.end())
{
if (PHYSFS_delete(FileSystem::join(m_addon_directory, addon.get_filename()).c_str()) == 0)
Expand Down Expand Up @@ -568,10 +551,10 @@ AddonManager::enable_addon(const AddonId& addon_id)
{
if (addon.get_type() == Addon::RESOURCEPACK)
{
for (const auto& installed_addon : m_installed_addons)
for (const auto& [id, addon] : m_installed_addons)
{
if (installed_addon->get_type() == Addon::RESOURCEPACK &&
installed_addon->is_enabled())
if (addon->get_type() == Addon::RESOURCEPACK &&
addon->is_enabled())
{
throw std::runtime_error(_("Only one resource pack is allowed to be enabled at a time."));
}
Expand Down Expand Up @@ -662,9 +645,9 @@ AddonManager::is_old_enabled_addon(const std::unique_ptr<Addon>& addon) const
bool
AddonManager::is_old_addon_enabled() const {
auto it = std::find_if(m_installed_addons.begin(), m_installed_addons.end(),
[this](const std::unique_ptr<Addon>& addon)
[this](const auto& addon)
{
return is_old_enabled_addon(addon);
return is_old_enabled_addon(addon.second);
});

return it != m_installed_addons.end();
Expand All @@ -673,9 +656,9 @@ AddonManager::is_old_addon_enabled() const {
void
AddonManager::disable_old_addons()
{
for (auto& addon : m_installed_addons) {
for (auto& [id, addon] : m_installed_addons) {
if (is_old_enabled_addon(addon)) {
disable_addon(addon->get_id());
disable_addon(id);
}
}
}
Expand All @@ -684,7 +667,7 @@ void
AddonManager::mount_old_addons()
{
std::string mountpoint;
for (auto& addon : m_installed_addons) {
for (auto& [id, addon] : m_installed_addons) {
if (is_old_enabled_addon(addon)) {
if (PHYSFS_mount(addon->get_install_filename().c_str(), mountpoint.c_str(), !addon->overrides_data()) == 0)
{
Expand All @@ -698,7 +681,7 @@ AddonManager::mount_old_addons()
void
AddonManager::unmount_old_addons()
{
for (auto& addon : m_installed_addons) {
for (auto& [id, addon] : m_installed_addons) {
if (is_old_enabled_addon(addon)) {
if (PHYSFS_unmount(addon->get_install_filename().c_str()) == 0)
{
Expand All @@ -713,7 +696,7 @@ bool
AddonManager::is_from_old_addon(const std::string& filename) const
{
std::string real_path = PHYSFS_getRealDir(filename.c_str());
for (auto& addon : m_installed_addons) {
for (auto& [id, addon] : m_installed_addons) {
if (is_old_enabled_addon(addon) &&
addon->get_install_filename() == real_path) {
return true;
Expand All @@ -736,11 +719,11 @@ std::vector<AddonId>
AddonManager::get_depending_addons(const std::string& id) const
{
std::vector<AddonId> addons;
for (auto& addon : m_installed_addons)
for (auto& [id, addon] : m_installed_addons)
{
const auto& dependencies = addon->get_dependencies();
if (std::find(dependencies.begin(), dependencies.end(), id) != dependencies.end())
addons.push_back(addon->get_id());
addons.push_back(id);
}
return addons;
}
Expand Down Expand Up @@ -846,7 +829,7 @@ AddonManager::add_installed_archive(const std::string& archive, const std::strin
// save addon title and author on stack before std::move
const std::string addon_title = addon->get_title();
const std::string addon_author = addon->get_author();
m_installed_addons.push_back(std::move(addon));
m_installed_addons[addon_id] = std::move(addon);
if(user_install)
{
try
Expand Down Expand Up @@ -892,10 +875,10 @@ AddonManager::add_installed_addons()
}
}

AddonManager::AddonList
AddonManager::AddonMap
AddonManager::parse_addon_infos(const std::string& filename) const
{
AddonList m_addons;
AddonMap m_addons;

try
{
Expand All @@ -920,7 +903,7 @@ AddonManager::parse_addon_infos(const std::string& filename) const
try
{
std::unique_ptr<Addon> addon = Addon::parse(addon_node.get_mapping());
m_addons.push_back(std::move(addon));
m_addons[addon->get_id()] = std::move(addon);
}
catch(const std::exception& e)
{
Expand Down
9 changes: 5 additions & 4 deletions src/addon/addon_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <memory>
#include <string>
#include <map>
#include <vector>

#include "addon/downloader.hpp"
Expand All @@ -35,7 +36,7 @@ typedef std::string AddonId;
class AddonManager final : public Currenton<AddonManager>
{
public:
using AddonList = std::vector<std::unique_ptr<Addon> >;
using AddonMap = std::map<AddonId, std::unique_ptr<Addon> >;

private:
Downloader m_downloader;
Expand All @@ -45,8 +46,8 @@ class AddonManager final : public Currenton<AddonManager>
std::string m_repository_url;
std::vector<Config::Addon>& m_addon_config;

AddonList m_installed_addons;
AddonList m_repository_addons;
AddonMap m_installed_addons;
AddonMap m_repository_addons;

bool m_initialized;
bool m_has_been_updated;
Expand Down Expand Up @@ -108,7 +109,7 @@ class AddonManager final : public Currenton<AddonManager>

std::vector<std::string> scan_for_archives() const;
void add_installed_addons();
AddonList parse_addon_infos(const std::string& filename) const;
AddonMap parse_addon_infos(const std::string& filename) const;

/** add \a archive, given as physfs path, to the list of installed
archives */
Expand Down
Loading

0 comments on commit 54c198c

Please sign in to comment.