1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "AddonDll.h"
#include "ServiceBroker.h"
#include "addons/AddonStatusHandler.h"
#include "addons/addoninfo/AddonInfo.h"
#include "addons/binary-addons/BinaryAddonBase.h"
#include "addons/binary-addons/BinaryAddonManager.h"
#include "addons/binary-addons/DllAddon.h"
#include "addons/interfaces/AddonBase.h"
#include "addons/kodi-dev-kit/include/kodi/versions.h"
#include "addons/settings/AddonSettings.h"
#include "events/EventLog.h"
#include "events/NotificationEvent.h"
#include "filesystem/File.h"
#include "filesystem/SpecialProtocol.h"
#include "messaging/helpers/DialogOKHelper.h"
#include "settings/lib/SettingSection.h"
#include "utils/URIUtils.h"
#include "utils/Variant.h"
#include "utils/log.h"
#include <utility>
using namespace KODI::MESSAGING;
namespace ADDON
{
CAddonDll::CAddonDll(const AddonInfoPtr& addonInfo, BinaryAddonBasePtr addonBase)
: CAddon(addonInfo, addonInfo->MainType()), m_binaryAddonBase(std::move(addonBase))
{
}
CAddonDll::CAddonDll(const AddonInfoPtr& addonInfo, AddonType addonType)
: CAddon(addonInfo, addonType),
m_binaryAddonBase(CServiceBroker::GetBinaryAddonManager().GetRunningAddonBase(addonInfo->ID()))
{
}
CAddonDll::~CAddonDll()
{
if (m_initialized)
Destroy();
}
std::string CAddonDll::GetDllPath(const std::string &libPath)
{
std::string strFileName = libPath;
std::string strLibName = URIUtils::GetFileName(strFileName);
if (strLibName.empty())
return "";
/* Check if lib being loaded exists, else check in XBMC binary location */
#if defined(TARGET_ANDROID)
if (XFILE::CFile::Exists(strFileName))
{
bool doCopy = true;
std::string dstfile = URIUtils::AddFileToFolder(CSpecialProtocol::TranslatePath("special://xbmcaltbinaddons/"), strLibName);
struct __stat64 dstFileStat;
if (XFILE::CFile::Stat(dstfile, &dstFileStat) == 0)
{
struct __stat64 srcFileStat;
if (XFILE::CFile::Stat(strFileName, &srcFileStat) == 0)
{
if (dstFileStat.st_size == srcFileStat.st_size && dstFileStat.st_mtime > srcFileStat.st_mtime)
doCopy = false;
}
}
if (doCopy)
{
CLog::Log(LOGDEBUG, "ADDON: caching {} to {}", strFileName, dstfile);
XFILE::CFile::Copy(strFileName, dstfile);
}
strFileName = dstfile;
}
if (!XFILE::CFile::Exists(strFileName))
{
std::string tempbin = getenv("KODI_ANDROID_LIBS");
strFileName = tempbin + "/" + strLibName;
}
#endif
if (!XFILE::CFile::Exists(strFileName))
{
std::string strAltFileName;
std::string altbin = CSpecialProtocol::TranslatePath("special://xbmcaltbinaddons/");
if (!altbin.empty())
{
strAltFileName = altbin + strLibName;
if (!XFILE::CFile::Exists(strAltFileName))
{
std::string temp = CSpecialProtocol::TranslatePath("special://xbmc/addons/");
strAltFileName = strFileName;
strAltFileName.erase(0, temp.size());
strAltFileName = altbin + strAltFileName;
}
CLog::Log(LOGDEBUG, "ADDON: Trying to load {}", strAltFileName);
}
if (XFILE::CFile::Exists(strAltFileName))
strFileName = strAltFileName;
else
{
std::string temp = CSpecialProtocol::TranslatePath("special://xbmc/");
std::string tempbin = CSpecialProtocol::TranslatePath("special://xbmcbin/");
strFileName.erase(0, temp.size());
strFileName = tempbin + strFileName;
if (!XFILE::CFile::Exists(strFileName))
{
CLog::Log(LOGERROR, "ADDON: Could not locate {}", strLibName);
strFileName.clear();
}
}
}
return strFileName;
}
std::string CAddonDll::LibPath() const
{
return GetDllPath(CAddon::LibPath());
}
bool CAddonDll::LoadDll()
{
if (m_pDll)
return true;
std::string strFileName = LibPath();
if (strFileName.empty())
return false;
/* Load the Dll */
m_pDll = new DllAddon;
m_pDll->SetFile(strFileName);
m_pDll->EnableDelayedUnload(false);
if (!m_pDll->Load())
{
delete m_pDll;
m_pDll = nullptr;
std::string heading =
StringUtils::Format("{}: {}", CAddonInfo::TranslateType(Type(), true), Name());
HELPERS::ShowOKDialogLines(CVariant{heading}, CVariant{24070}, CVariant{24071});
return false;
}
return true;
}
ADDON_STATUS CAddonDll::Create(KODI_ADDON_INSTANCE_STRUCT* firstKodiInstance)
{
CLog::Log(LOGDEBUG, "ADDON: Dll Initializing - {}", Name());
m_initialized = false;
if (!LoadDll())
{
return ADDON_STATUS_PERMANENT_FAILURE;
}
/* Check versions about global parts on add-on (parts used on all types) */
for (unsigned int id = ADDON_GLOBAL_MAIN; id <= ADDON_GLOBAL_MAX; ++id)
{
if (!CheckAPIVersion(id))
return ADDON_STATUS_PERMANENT_FAILURE;
}
/* Allocate the helper function class to allow crosstalk over
helper add-on headers */
if (!Interface_Base::InitInterface(this, m_interface, firstKodiInstance))
return ADDON_STATUS_PERMANENT_FAILURE;
/* Call Create to make connections, initializing data or whatever is
needed to become the AddOn running */
ADDON_STATUS status = m_pDll->Create(&m_interface);
// "C" ABI related call, if on add-on used.
if (status == ADDON_STATUS_OK && m_interface.toAddon->create)
status = m_interface.toAddon->create(m_interface.firstKodiInstance, &m_interface.addonBase);
if (status == ADDON_STATUS_OK)
{
m_initialized = true;
}
else if (status == ADDON_STATUS_NEED_SETTINGS)
{
if ((status = TransferSettings(ADDON_SETTINGS_ID)) == ADDON_STATUS_OK)
m_initialized = true;
else
new CAddonStatusHandler(ID(), ADDON_SETTINGS_ID, status, false);
}
else
{ // Addon failed initialization
CLog::Log(LOGERROR,
"ADDON: Dll {} - Client returned bad status ({}) from Create and is not usable",
Name(), status);
// @todo currently a copy and paste from other function and becomes improved.
std::string heading =
StringUtils::Format("{}: {}", CAddonInfo::TranslateType(Type(), true), Name());
HELPERS::ShowOKDialogLines(CVariant{ heading }, CVariant{ 24070 }, CVariant{ 24071 });
}
return status;
}
void CAddonDll::Destroy()
{
/* Unload library file */
if (m_pDll)
{
if (m_interface.toAddon->destroy)
m_interface.toAddon->destroy(m_interface.addonBase);
m_pDll->Unload();
}
Interface_Base::DeInitInterface(m_interface);
if (m_pDll)
{
delete m_pDll;
m_pDll = nullptr;
CLog::Log(LOGINFO, "ADDON: Dll Destroyed - {}", Name());
}
ResetSettings(ADDON_SETTINGS_ID);
m_initialized = false;
}
ADDON_STATUS CAddonDll::CreateInstance(KODI_ADDON_INSTANCE_STRUCT* instance)
{
assert(instance != nullptr);
assert(instance->functions != nullptr);
assert(instance->info != nullptr);
assert(instance->info->functions != nullptr);
ADDON_STATUS status = ADDON_STATUS_OK;
if (!m_initialized)
status = Create(instance);
if (status != ADDON_STATUS_OK)
return status;
/* Check version of requested instance type */
if (!CheckAPIVersion(instance->info->type))
return ADDON_STATUS_PERMANENT_FAILURE;
status = m_interface.toAddon->create_instance(m_interface.addonBase, instance);
if (instance->info)
{
m_usedInstances[instance->info->kodi] = instance;
}
return status;
}
void CAddonDll::DestroyInstance(KODI_ADDON_INSTANCE_STRUCT* instance)
{
if (m_usedInstances.empty())
return;
auto it = m_usedInstances.find(instance->info->kodi);
if (it != m_usedInstances.end())
{
m_interface.toAddon->destroy_instance(m_interface.addonBase, it->second);
m_usedInstances.erase(it);
}
if (m_usedInstances.empty())
Destroy();
}
bool CAddonDll::IsInUse() const
{
if (m_informer)
return m_informer->IsInUse(ID());
return false;
}
void CAddonDll::RegisterInformer(CAddonDllInformer* informer)
{
m_informer = informer;
}
AddonPtr CAddonDll::GetRunningInstance() const
{
if (CServiceBroker::IsAddonInterfaceUp())
return CServiceBroker::GetBinaryAddonManager().GetRunningAddon(ID());
return AddonPtr();
}
void CAddonDll::OnPreInstall()
{
if (m_binaryAddonBase)
m_binaryAddonBase->OnPreInstall();
}
void CAddonDll::OnPostInstall(bool update, bool modal)
{
if (m_binaryAddonBase)
m_binaryAddonBase->OnPostInstall(update, modal);
}
void CAddonDll::OnPreUnInstall()
{
if (m_binaryAddonBase)
m_binaryAddonBase->OnPreUnInstall();
}
void CAddonDll::OnPostUnInstall()
{
if (m_binaryAddonBase)
m_binaryAddonBase->OnPostUnInstall();
}
bool CAddonDll::DllLoaded(void) const
{
return m_pDll != nullptr;
}
CAddonVersion CAddonDll::GetTypeVersionDll(int type) const
{
return CAddonVersion(m_pDll ? m_pDll->GetAddonTypeVersion(type) : nullptr);
}
CAddonVersion CAddonDll::GetTypeMinVersionDll(int type) const
{
return CAddonVersion(m_pDll ? m_pDll->GetAddonTypeMinVersion(type) : nullptr);
}
void CAddonDll::SaveSettings(AddonInstanceId id /* = ADDON_SETTINGS_ID */)
{
// must save first, as TransferSettings() reloads saved settings!
CAddon::SaveSettings(id);
if (m_initialized)
TransferSettings(id);
}
ADDON_STATUS CAddonDll::TransferSettings(AddonInstanceId instanceId)
{
bool restart = false;
ADDON_STATUS reportStatus = ADDON_STATUS_OK;
CLog::Log(LOGDEBUG, "Calling TransferSettings for: {}", Name());
LoadSettings(false, true, instanceId);
auto settings = GetSettings(instanceId);
if (settings != nullptr)
{
KODI_ADDON_INSTANCE_FUNC* instanceTarget{nullptr};
KODI_ADDON_INSTANCE_HDL instanceHandle{nullptr};
if (instanceId != ADDON_SETTINGS_ID)
{
const auto it = std::find_if(
m_usedInstances.begin(), m_usedInstances.end(),
[instanceId](const auto& data) { return data.second->info->number == instanceId; });
if (it == m_usedInstances.end())
return ADDON_STATUS_UNKNOWN;
instanceTarget = it->second->functions;
instanceHandle = it->second->hdl;
}
for (const auto& section : settings->GetSections())
{
for (const auto& category : section->GetCategories())
{
for (const auto& group : category->GetGroups())
{
for (const auto& setting : group->GetSettings())
{
if (StringUtils::StartsWith(setting->GetId(), ADDON_SETTING_INSTANCE_GROUP))
continue; // skip internal settings
ADDON_STATUS status = ADDON_STATUS_OK;
const char* id = setting->GetId().c_str();
switch (setting->GetType())
{
case SettingType::Boolean:
{
bool tmp = std::static_pointer_cast<CSettingBool>(setting)->GetValue();
if (instanceId == ADDON_SETTINGS_ID)
{
if (m_interface.toAddon->setting_change_boolean)
status =
m_interface.toAddon->setting_change_boolean(m_interface.addonBase, id, tmp);
}
else if (instanceTarget && instanceHandle)
{
if (instanceTarget->instance_setting_change_boolean)
status =
instanceTarget->instance_setting_change_boolean(instanceHandle, id, tmp);
}
break;
}
case SettingType::Integer:
{
int tmp = std::static_pointer_cast<CSettingInt>(setting)->GetValue();
if (instanceId == ADDON_SETTINGS_ID)
{
if (m_interface.toAddon->setting_change_integer)
status =
m_interface.toAddon->setting_change_integer(m_interface.addonBase, id, tmp);
}
else if (instanceTarget && instanceHandle)
{
if (instanceTarget->instance_setting_change_integer)
status =
instanceTarget->instance_setting_change_integer(instanceHandle, id, tmp);
}
break;
}
case SettingType::Number:
{
float tmpf = static_cast<float>(std::static_pointer_cast<CSettingNumber>(setting)->GetValue());
if (instanceId == ADDON_SETTINGS_ID)
{
if (m_interface.toAddon->setting_change_float)
status =
m_interface.toAddon->setting_change_float(m_interface.addonBase, id, tmpf);
}
else if (instanceTarget && instanceHandle)
{
if (instanceTarget->instance_setting_change_float)
status =
instanceTarget->instance_setting_change_float(instanceHandle, id, tmpf);
}
break;
}
case SettingType::String:
{
if (instanceId == ADDON_SETTINGS_ID)
{
if (m_interface.toAddon->setting_change_string)
status = m_interface.toAddon->setting_change_string(
m_interface.addonBase, id,
std::static_pointer_cast<CSettingString>(setting)->GetValue().c_str());
}
else if (instanceTarget && instanceHandle)
{
if (instanceTarget->instance_setting_change_string)
status = instanceTarget->instance_setting_change_string(
instanceHandle, id,
std::static_pointer_cast<CSettingString>(setting)->GetValue().c_str());
}
break;
}
default:
{
// log unknowns as an error, but go ahead and transfer the string
CLog::Log(LOGERROR, "Unknown setting type of '{}' for {}", id, Name());
if (instanceId == ADDON_SETTINGS_ID)
{
if (m_interface.toAddon->setting_change_string)
status = m_interface.toAddon->setting_change_string(
m_interface.addonBase, id, setting->ToString().c_str());
}
else if (instanceTarget && instanceHandle)
{
if (instanceTarget->instance_setting_change_string)
status = instanceTarget->instance_setting_change_string(
instanceHandle, id, setting->ToString().c_str());
}
break;
}
}
if (status == ADDON_STATUS_NEED_RESTART)
restart = true;
else if (status != ADDON_STATUS_OK)
reportStatus = status;
}
}
}
}
}
if (restart || reportStatus != ADDON_STATUS_OK)
{
new CAddonStatusHandler(ID(), instanceId, restart ? ADDON_STATUS_NEED_RESTART : reportStatus,
true);
}
return ADDON_STATUS_OK;
}
bool CAddonDll::CheckAPIVersion(int type)
{
/* check the API version */
CAddonVersion kodiMinVersion(kodi::addon::GetTypeMinVersion(type));
CAddonVersion addonVersion(m_pDll->GetAddonTypeVersion(type));
CAddonVersion addonMinVersion = m_pDll->GetAddonTypeMinVersion_available()
? CAddonVersion(m_pDll->GetAddonTypeMinVersion(type))
: addonVersion;
/* Check the global usage from addon
* if not used from addon, empty version is returned
*/
if (type <= ADDON_GLOBAL_MAX && addonVersion.empty())
return true;
/* If a instance (not global) version becomes checked must be the version
* present.
*/
if (kodiMinVersion > addonVersion ||
addonMinVersion > CAddonVersion(kodi::addon::GetTypeVersion(type)))
{
CLog::Log(LOGERROR, "Add-on '{}' is using an incompatible API version for type '{}'. Kodi API min version = '{}/{}', add-on API version '{}/{}'",
Name(),
kodi::addon::GetTypeName(type),
kodi::addon::GetTypeVersion(type),
kodiMinVersion.asString(),
addonMinVersion.asString(),
addonVersion.asString());
if (CServiceBroker::GetGUI())
{
CEventLog* eventLog = CServiceBroker::GetEventLog();
if (eventLog)
eventLog->AddWithNotification(
EventPtr(new CNotificationEvent(Name(), 24152, EventLevel::Error)));
}
return false;
}
return true;
}
} /* namespace ADDON */
|