Merge pull request #24470 from fuzzard/release_20.3
[xbmc.git] / xbmc / addons / BinaryAddonCache.cpp
blob3005d44516f4a7f1fc5a9b98ae12f2e7b91bb59e
1 /*
2 * Copyright (C) 2016-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "BinaryAddonCache.h"
11 #include "ServiceBroker.h"
12 #include "addons/AddonEvents.h"
13 #include "addons/AddonManager.h"
14 #include "addons/addoninfo/AddonType.h"
16 #include <mutex>
18 namespace ADDON
21 const std::vector<AddonType> ADDONS_TO_CACHE = {AddonType::GAMEDLL};
23 CBinaryAddonCache::~CBinaryAddonCache()
25 Deinit();
28 void CBinaryAddonCache::Init()
30 CServiceBroker::GetAddonMgr().Events().Subscribe(this, &CBinaryAddonCache::OnEvent);
31 Update();
34 void CBinaryAddonCache::Deinit()
36 CServiceBroker::GetAddonMgr().Events().Unsubscribe(this);
39 void CBinaryAddonCache::GetAddons(VECADDONS& addons, AddonType type)
41 VECADDONS myAddons;
42 GetInstalledAddons(myAddons, type);
44 for (auto &addon : myAddons)
46 if (!CServiceBroker::GetAddonMgr().IsAddonDisabled(addon->ID()))
47 addons.emplace_back(std::move(addon));
51 void CBinaryAddonCache::GetDisabledAddons(VECADDONS& addons, AddonType type)
53 VECADDONS myAddons;
54 GetInstalledAddons(myAddons, type);
56 for (auto &addon : myAddons)
58 if (CServiceBroker::GetAddonMgr().IsAddonDisabled(addon->ID()))
59 addons.emplace_back(std::move(addon));
63 void CBinaryAddonCache::GetInstalledAddons(VECADDONS& addons, AddonType type)
65 std::unique_lock<CCriticalSection> lock(m_critSection);
66 auto it = m_addons.find(type);
67 if (it != m_addons.end())
68 addons = it->second;
71 AddonPtr CBinaryAddonCache::GetAddonInstance(const std::string& strId, AddonType type)
73 AddonPtr addon;
75 std::unique_lock<CCriticalSection> lock(m_critSection);
77 auto it = m_addons.find(type);
78 if (it != m_addons.end())
80 VECADDONS& addons = it->second;
81 auto itAddon = std::find_if(addons.begin(), addons.end(),
82 [&strId](const AddonPtr& addon)
84 return addon->ID() == strId;
85 });
87 if (itAddon != addons.end())
88 addon = *itAddon;
91 return addon;
94 void CBinaryAddonCache::OnEvent(const AddonEvent& event)
96 if (typeid(event) == typeid(AddonEvents::Enabled) ||
97 typeid(event) == typeid(AddonEvents::Disabled) ||
98 typeid(event) == typeid(AddonEvents::ReInstalled))
100 for (auto &type : ADDONS_TO_CACHE)
102 if (CServiceBroker::GetAddonMgr().HasType(event.addonId, type))
104 Update();
105 break;
109 else if (typeid(event) == typeid(AddonEvents::UnInstalled))
111 Update();
115 void CBinaryAddonCache::Update()
117 using AddonMap = std::multimap<AddonType, VECADDONS>;
118 AddonMap addonmap;
120 for (auto &addonType : ADDONS_TO_CACHE)
122 VECADDONS addons;
123 CServiceBroker::GetAddonMgr().GetInstalledAddons(addons, addonType);
124 addonmap.insert(AddonMap::value_type(addonType, addons));
128 std::unique_lock<CCriticalSection> lock(m_critSection);
129 m_addons = std::move(addonmap);