diff --git a/pvr.stalker/addon.xml.in b/pvr.stalker/addon.xml.in index e601a10..8b06262 100644 --- a/pvr.stalker/addon.xml.in +++ b/pvr.stalker/addon.xml.in @@ -1,7 +1,7 @@ @ADDON_DEPENDS@ diff --git a/src/StalkerInstance.cpp b/src/StalkerInstance.cpp index ad50380..94a057d 100644 --- a/src/StalkerInstance.cpp +++ b/src/StalkerInstance.cpp @@ -254,6 +254,9 @@ bool StalkerInstance::ConfigureStalkerAPISettings() SError ret; +kodi::Log(ADDON_LOG_DEBUG, "ZZ ConfigureStalkerAPISettings 1"); + + sc_identity_defaults(&m_identity); SC_STR_SET(m_identity.mac, settings->mac.c_str()); SC_STR_SET(m_identity.time_zone, settings->timeZone.c_str()); @@ -295,10 +298,15 @@ bool StalkerInstance::ConfigureStalkerAPISettings() m_guideManager->SetGuidePreference(settings->guidePreference); m_guideManager->SetCacheOptions(settings->guideCache, settings->guideCacheHours * 3600); +kodi::Log(ADDON_LOG_DEBUG, "ZZ ConfigureStalkerAPISettings 2"); + ret = Authenticate(); +kodi::Log(ADDON_LOG_DEBUG, "ZZ ConfigureStalkerAPISettings 3"); if (ret != SERROR_OK) QueueErrorNotification(ret); +kodi::Log(ADDON_LOG_DEBUG, "ZZ ConfigureStalkerAPISettings 4"); + return ret == SERROR_OK; } @@ -309,6 +317,7 @@ bool StalkerInstance::IsAuthenticated() const SError StalkerInstance::Authenticate() { + kodi::Log(ADDON_LOG_DEBUG, "ZZ Authenticate 1"); kodi::Log(ADDON_LOG_DEBUG, "%s", __func__); SError ret; @@ -316,9 +325,13 @@ SError StalkerInstance::Authenticate() if (!m_sessionManager->IsAuthenticated() && SERROR_OK != (ret = m_sessionManager->Authenticate())) return ret; + kodi::Log(ADDON_LOG_DEBUG, "ZZ Authenticate 2"); + if (m_profile.store_auth_data_on_stb && !SaveCache()) return SERROR_UNKNOWN; + kodi::Log(ADDON_LOG_DEBUG, "ZZ Authenticate 3"); + return SERROR_OK; } diff --git a/src/stalker/SessionManager.cpp b/src/stalker/SessionManager.cpp index 547b6f1..64de85a 100644 --- a/src/stalker/SessionManager.cpp +++ b/src/stalker/SessionManager.cpp @@ -127,6 +127,7 @@ SError SessionManager::GetProfile(bool authSecondStep) SError SessionManager::Authenticate() { + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 1"); bool wasAuthenticated(m_authenticated); int maxRetires(5); int numRetries(0); @@ -134,19 +135,37 @@ SError SessionManager::Authenticate() if (m_isAuthenticating) return SERROR_OK; + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 2"); + StopWatchdog(); - m_authMutex.lock(); - m_isAuthenticating = true; - m_authenticated = false; - m_lastUnknownError.clear(); - m_authMutex.unlock(); + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 3"); + + { + std::lock_guard guard(m_authMutex); + //m_authMutex.lock(); + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 3a"); + m_isAuthenticating = true; + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 3b"); + m_authenticated = false; + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 3c"); + m_lastUnknownError.clear(); + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 3d"); + //m_authMutex.unlock(); + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 3e"); + } + + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 4"); if (wasAuthenticated && m_statusCallback != nullptr) m_statusCallback(SERROR_AUTHORIZATION); + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 5"); + while (!m_authenticated && ++numRetries <= maxRetires) { + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 6"); + // notify once after the first try failed if (numRetries == 2) { @@ -154,31 +173,58 @@ SError SessionManager::Authenticate() m_statusCallback(SERROR_AUTHENTICATION); } + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 7"); + // don't sleep on first try if (numRetries > 1) std::this_thread::sleep_for(std::chrono::milliseconds(5000)); + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 8"); + if (!m_hasUserDefinedToken && SERROR_OK != DoHandshake()) continue; + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 9"); + if (SERROR_OK != GetProfile()) continue; - m_authMutex.lock(); - m_authenticated = true; - m_isAuthenticating = false; - m_authMutex.unlock(); + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 10"); + + { + std::lock_guard guard(m_authMutex); + //m_authMutex.lock(); + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 10a"); + m_authenticated = true; + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 10b"); + m_isAuthenticating = false; + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 10c"); + //m_authMutex.unlock(); + } + + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 11"); + if (wasAuthenticated && m_statusCallback != nullptr) m_statusCallback(SERROR_OK); + + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 12"); + } + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 13"); + if (m_authenticated) { + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 14"); StartAuthInvoker(); + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 15"); StartWatchdog(); + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 16"); } + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 17"); + return SERROR_OK; } @@ -218,20 +264,29 @@ void SessionManager::StopAuthInvoker() void SessionManager::StartWatchdog() { + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::StartWatchdog 1"); if (!m_watchdog) { + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::StartWatchdog 2"); m_watchdog = new CWatchdog((unsigned int)m_profile->timeslot, m_api, [this](SError err) { if (err == SERROR_AUTHORIZATION) { - m_authMutex.lock(); + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::StartWatchdog 3"); + std::lock_guard guard(m_authMutex); + //m_authMutex.lock(); + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::StartWatchdog 4"); m_authenticated = false; - m_authMutex.unlock(); + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::StartWatchdog 5"); + // m_authMutex.unlock(); + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::StartWatchdog 6"); } }); } + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::StartWatchdog 7"); if (m_watchdog) m_watchdog->Start(); + kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::StartWatchdog 8"); } void SessionManager::StopWatchdog() diff --git a/src/stalker/SettingsMigration.cpp b/src/stalker/SettingsMigration.cpp index d89b7d9..755b6bc 100644 --- a/src/stalker/SettingsMigration.cpp +++ b/src/stalker/SettingsMigration.cpp @@ -44,10 +44,13 @@ const std::vector> boolMap = {{"guide_cache", true} bool SettingsMigration::MigrateSettings(kodi::addon::IAddonInstance& target) { + kodi::Log(ADDON_LOG_DEBUG, "MigrateSettings 1"); + std::string stringValue; bool boolValue{false}; int intValue{0}; + kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateSettings 2"); if (target.CheckInstanceSettingString("kodi_addon_instance_name", stringValue) && !stringValue.empty()) { @@ -55,37 +58,51 @@ bool SettingsMigration::MigrateSettings(kodi::addon::IAddonInstance& target) return false; } + kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateSettings 3"); + // Read pre-multi-instance settings from settings.xml, transfer to instance settings SettingsMigration mig(target); + kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateSettings 4"); for (const auto& setting : stringMap) mig.MigrateStringSetting(setting.first, setting.second); + kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateSettings 5"); for (const auto& setting : intMap) mig.MigrateIntSetting(setting.first, setting.second); + kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateSettings 6"); for (const auto& setting : floatMap) mig.MigrateFloatSetting(setting.first, setting.second); + kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateSettings 7"); for (const auto& setting : boolMap) mig.MigrateBoolSetting(setting.first, setting.second); + kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateSettings 8"); if (mig.Changed()) { + kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateSettings 9"); // Set a title for the new instance settings std::string title = "Migrated Add-on Config"; target.SetInstanceSettingString("kodi_addon_instance_name", title); return true; } + kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateSettings 10"); + return false; } bool SettingsMigration::IsMigrationSetting(const std::string& key) { +kodi::Log(ADDON_LOG_DEBUG, "ZZ IsMigrationSetting 1"); + std::string oldSettingsKey{key}; oldSettingsKey += "_0"; +kodi::Log(ADDON_LOG_DEBUG, "ZZ IsMigrationSetting 2"); + return std::any_of(stringMap.cbegin(), stringMap.cend(), [&key](const auto& entry) { return entry.first == key; }) || std::any_of(intMap.cbegin(), intMap.cend(), @@ -106,9 +123,13 @@ bool SettingsMigration::IsMigrationSetting(const std::string& key) void SettingsMigration::MigrateStringSetting(const char* key, const std::string& defaultValue) { +kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 1"); + std::string oldSettingsKey{key}; oldSettingsKey += "_0"; +kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 2"); + std::string value; if (kodi::addon::CheckSettingString(oldSettingsKey, value) && value != defaultValue) { @@ -120,65 +141,82 @@ void SettingsMigration::MigrateStringSetting(const char* key, const std::string& m_target.SetInstanceSettingString(key, value); m_changed = true; } +kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 3"); } void SettingsMigration::MigrateIntSetting(const char* key, int defaultValue) { +kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 1"); std::string oldSettingsKey{key}; oldSettingsKey += "_0"; +kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 2"); // Stalker uses the old settings format prior to migration and reading int and boolean reliably requires reading into // a string and then into a int. Luckily after this messy migration, the settings will be in the new format going forward. int value; std::string stringValue; if (kodi::addon::CheckSettingString(oldSettingsKey, stringValue) && stringValue != std::to_string(defaultValue)) { +kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 3"); value = std::atoi(stringValue.c_str()); m_target.SetInstanceSettingInt(key, value); m_changed = true; +kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 4"); } else if (kodi::addon::CheckSettingString(key, stringValue) && stringValue != std::to_string(defaultValue)) { +kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 5"); value = std::atoi(stringValue.c_str()); if (oldSettingsKey == "connection_timeout_0") value *= 5; m_target.SetInstanceSettingInt(key, value); m_changed = true; +kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 6"); } +kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 7"); } void SettingsMigration::MigrateFloatSetting(const char* key, float defaultValue) { +kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateFloatSetting 1"); std::string oldSettingsKey{key}; oldSettingsKey += "_0"; +kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateFloatSetting 2"); float value; if (kodi::addon::CheckSettingFloat(oldSettingsKey, value) && value != defaultValue) { +kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateFloatSetting 3"); m_target.SetInstanceSettingFloat(key, value); m_changed = true; } else if (kodi::addon::CheckSettingFloat(key, value) && value != defaultValue) { +kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateFloatSetting 4"); m_target.SetInstanceSettingFloat(key, value); m_changed = true; } +kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateFloatSetting 5"); } void SettingsMigration::MigrateBoolSetting(const char* key, bool defaultValue) { +kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateBoolSetting 1"); std::string oldSettingsKey{key}; oldSettingsKey += "_0"; +kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateBoolSetting 2"); // Stalker uses the old settings format prior to migration and reading int and boolean reliably requires reading into // a string and then into a int. Luckily after this messy migration, the settings will be in the new format going forward. bool value; std::string stringValue; if (kodi::addon::CheckSettingString(oldSettingsKey, stringValue) && stringValue != (defaultValue ? "true" : "false")) { +kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateBoolSetting 3"); value = stringValue == "true"; m_target.SetInstanceSettingBoolean(key, value); m_changed = true; } +kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateBoolSetting 4"); }