| 1 |
/* |
| 2 |
* Copyright (C) 2012-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "GameSettings.h" |
| 10 |
|
| 11 |
#include "ServiceBroker.h" |
| 12 |
#include "URL.h" |
| 13 |
#include "events/EventLog.h" |
| 14 |
#include "events/NotificationEvent.h" |
| 15 |
#include "filesystem/File.h" |
| 16 |
#include "settings/Settings.h" |
| 17 |
#include "settings/SettingsComponent.h" |
| 18 |
#include "settings/lib/Setting.h" |
| 19 |
#include "utils/JSONVariantParser.h" |
| 20 |
#include "utils/StringUtils.h" |
| 21 |
#include "utils/Variant.h" |
| 22 |
#include "utils/log.h" |
| 23 |
|
| 24 |
#include <algorithm> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
using namespace KODI; |
| 28 |
using namespace GAME; |
| 29 |
|
| 30 |
namespace |
| 31 |
{ |
| 32 |
const std::string SETTING_GAMES_ENABLE = "gamesgeneral.enable"; |
| 33 |
const std::string SETTING_GAMES_SHOW_OSD_HELP = "gamesgeneral.showosdhelp"; |
| 34 |
const std::string SETTING_GAMES_ENABLEAUTOSAVE = "gamesgeneral.enableautosave"; |
| 35 |
const std::string SETTING_GAMES_ENABLEREWIND = "gamesgeneral.enablerewind"; |
| 36 |
const std::string SETTING_GAMES_REWINDTIME = "gamesgeneral.rewindtime"; |
| 37 |
const std::string SETTING_GAMES_ACHIEVEMENTS_USERNAME = "gamesachievements.username"; |
| 38 |
const std::string SETTING_GAMES_ACHIEVEMENTS_PASSWORD = "gamesachievements.password"; |
| 39 |
const std::string SETTING_GAMES_ACHIEVEMENTS_TOKEN = "gamesachievements.token"; |
| 40 |
const std::string SETTING_GAMES_ACHIEVEMENTS_LOGGED_IN = "gamesachievements.loggedin"; |
| 41 |
|
| 42 |
constexpr auto LOGIN_TO_RETRO_ACHIEVEMENTS_URL_TEMPLATE = |
| 43 |
"http://retroachievements.org/dorequest.php?r=login&u={}&p={}"; |
| 44 |
constexpr auto GET_PATCH_DATA_URL_TEMPLATE = |
| 45 |
"http://retroachievements.org/dorequest.php?r=patch&u={}&t={}&g=0"; |
| 46 |
constexpr auto SUCCESS = "Success"; |
| 47 |
constexpr auto TOKEN = "Token"; |
| 48 |
} // namespace |
| 49 |
|
| 50 |
CGameSettings::CGameSettings() |
| 51 |
{ |
| 52 |
m_settings = CServiceBroker::GetSettingsComponent()->GetSettings(); |
| 53 |
|
| 54 |
m_settings->RegisterCallback(this, {SETTING_GAMES_ENABLEREWIND, SETTING_GAMES_REWINDTIME, |
| 55 |
SETTING_GAMES_ACHIEVEMENTS_USERNAME, |
| 56 |
SETTING_GAMES_ACHIEVEMENTS_PASSWORD, |
| 57 |
SETTING_GAMES_ACHIEVEMENTS_LOGGED_IN}); |
| 58 |
} |
| 59 |
|
| 60 |
CGameSettings::~CGameSettings() |
| 61 |
{ |
| 62 |
m_settings->UnregisterCallback(this); |
| 63 |
} |
| 64 |
|
| 65 |
bool CGameSettings::GamesEnabled() |
| 66 |
{ |
| 67 |
return m_settings->GetBool(SETTING_GAMES_ENABLE); |
| 68 |
} |
| 69 |
|
| 70 |
bool CGameSettings::ShowOSDHelp() |
| 71 |
{ |
| 72 |
return m_settings->GetBool(SETTING_GAMES_SHOW_OSD_HELP); |
| 73 |
} |
| 74 |
|
| 75 |
void CGameSettings::SetShowOSDHelp(bool bShow) |
| 76 |
{ |
| 77 |
if (m_settings->GetBool(SETTING_GAMES_SHOW_OSD_HELP) != bShow) |
| 78 |
{ |
| 79 |
m_settings->SetBool(SETTING_GAMES_SHOW_OSD_HELP, bShow); |
| 80 |
|
| 81 |
//! @todo Asynchronous save |
| 82 |
m_settings->Save(); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
void CGameSettings::ToggleGames() |
| 87 |
{ |
| 88 |
m_settings->ToggleBool(SETTING_GAMES_ENABLE); |
| 89 |
} |
| 90 |
|
| 91 |
bool CGameSettings::AutosaveEnabled() |
| 92 |
{ |
| 93 |
return m_settings->GetBool(SETTING_GAMES_ENABLEAUTOSAVE); |
| 94 |
} |
| 95 |
|
| 96 |
bool CGameSettings::RewindEnabled() |
| 97 |
{ |
| 98 |
return m_settings->GetBool(SETTING_GAMES_ENABLEREWIND); |
| 99 |
} |
| 100 |
|
| 101 |
unsigned int CGameSettings::MaxRewindTimeSec() |
| 102 |
{ |
| 103 |
int rewindTimeSec = m_settings->GetInt(SETTING_GAMES_REWINDTIME); |
| 104 |
|
| 105 |
return static_cast<unsigned int>(std::max(rewindTimeSec, 0)); |
| 106 |
} |
| 107 |
|
| 108 |
std::string CGameSettings::GetRAUsername() const |
| 109 |
{ |
| 110 |
return m_settings->GetString(SETTING_GAMES_ACHIEVEMENTS_USERNAME); |
| 111 |
} |
| 112 |
|
| 113 |
std::string CGameSettings::GetRAToken() const |
| 114 |
{ |
| 115 |
return m_settings->GetString(SETTING_GAMES_ACHIEVEMENTS_TOKEN); |
| 116 |
} |
| 117 |
|
| 118 |
void CGameSettings::OnSettingChanged(const std::shared_ptr<const CSetting>& setting) |
| 119 |
{ |
| 120 |
if (setting == nullptr) |
| 121 |
return; |
| 122 |
|
| 123 |
const std::string& settingId = setting->GetId(); |
| 124 |
|
| 125 |
if (settingId == SETTING_GAMES_ENABLEREWIND || settingId == SETTING_GAMES_REWINDTIME) |
| 126 |
{ |
| 127 |
SetChanged(); |
| 128 |
NotifyObservers(ObservableMessageSettingsChanged); |
| 129 |
} |
| 130 |
else if (settingId == SETTING_GAMES_ACHIEVEMENTS_LOGGED_IN && |
| 131 |
std::dynamic_pointer_cast<const CSettingBool>(setting)->GetValue()) |
| 132 |
{ |
| 133 |
const std::string username = m_settings->GetString(SETTING_GAMES_ACHIEVEMENTS_USERNAME); |
| 134 |
const std::string password = m_settings->GetString(SETTING_GAMES_ACHIEVEMENTS_PASSWORD); |
| 135 |
std::string token = m_settings->GetString(SETTING_GAMES_ACHIEVEMENTS_TOKEN); |
| 136 |
|
| 137 |
token = LoginToRA(username, password, std::move(token)); |
| 138 |
|
| 139 |
m_settings->SetString(SETTING_GAMES_ACHIEVEMENTS_TOKEN, token); |
| 140 |
|
| 141 |
if (!token.empty()) |
| 142 |
{ |
| 143 |
m_settings->SetBool(SETTING_GAMES_ACHIEVEMENTS_LOGGED_IN, true); |
| 144 |
} |
| 145 |
else |
| 146 |
{ |
| 147 |
if (settingId == SETTING_GAMES_ACHIEVEMENTS_PASSWORD) |
| 148 |
m_settings->SetString(SETTING_GAMES_ACHIEVEMENTS_PASSWORD, ""); |
| 149 |
m_settings->SetBool(SETTING_GAMES_ACHIEVEMENTS_LOGGED_IN, false); |
| 150 |
} |
| 151 |
|
| 152 |
m_settings->Save(); |
| 153 |
} |
| 154 |
else if (settingId == SETTING_GAMES_ACHIEVEMENTS_LOGGED_IN && |
| 155 |
!std::dynamic_pointer_cast<const CSettingBool>(setting)->GetValue()) |
| 156 |
{ |
| 157 |
m_settings->SetString(SETTING_GAMES_ACHIEVEMENTS_TOKEN, ""); |
| 158 |
m_settings->Save(); |
| 159 |
} |
| 160 |
else if (settingId == SETTING_GAMES_ACHIEVEMENTS_USERNAME || |
| 161 |
settingId == SETTING_GAMES_ACHIEVEMENTS_PASSWORD) |
| 162 |
{ |
| 163 |
m_settings->SetBool(SETTING_GAMES_ACHIEVEMENTS_LOGGED_IN, false); |
| 164 |
m_settings->SetString(SETTING_GAMES_ACHIEVEMENTS_TOKEN, ""); |
| 165 |
m_settings->Save(); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
std::string CGameSettings::LoginToRA(const std::string& username, |
| 170 |
const std::string& password, |
| 171 |
std::string token) const |
| 172 |
{ |
| 173 |
if (username.empty() || password.empty()) |
| 174 |
return token; |
| 175 |
|
| 176 |
XFILE::CFile request; |
| 177 |
const CURL loginUrl( |
| 178 |
StringUtils::Format(LOGIN_TO_RETRO_ACHIEVEMENTS_URL_TEMPLATE, username, password)); |
| 179 |
|
| 180 |
std::vector<uint8_t> response; |
| 181 |
if (request.LoadFile(loginUrl, response) > 0) |
| 182 |
{ |
| 183 |
std::string strResponse(response.begin(), response.end()); |
| 184 |
CVariant data(CVariant::VariantTypeObject); |
| 185 |
if (CJSONVariantParser::Parse(strResponse, data)) |
| 186 |
{ |
| 187 |
if (data[SUCCESS].asBoolean()) |
| 188 |
{ |
| 189 |
token = data[TOKEN].asString(); |
| 190 |
if (!IsAccountVerified(username, token)) |
| 191 |
{ |
| 192 |
token.clear(); |
| 193 |
// "RetroAchievements", "Your account is not verified, please check your emails to complete your sign up" |
| 194 |
CServiceBroker::GetEventLog()->AddWithNotification( |
| 195 |
EventPtr(new CNotificationEvent(35264, 35270, EventLevel::Error))); |
| 196 |
} |
| 197 |
} |
| 198 |
else |
| 199 |
{ |
| 200 |
token.clear(); |
| 201 |
|
| 202 |
// "RetroAchievements", "Incorrect User/Password!" |
| 203 |
CServiceBroker::GetEventLog()->AddWithNotification( |
| 204 |
EventPtr(new CNotificationEvent(35264, 35265, EventLevel::Error))); |
| 205 |
} |
| 206 |
} |
| 207 |
else |
| 208 |
{ |
| 209 |
// "RetroAchievements", "Invalid response from server" |
| 210 |
CServiceBroker::GetEventLog()->AddWithNotification( |
| 211 |
EventPtr(new CNotificationEvent(35264, 35267, EventLevel::Error))); |
| 212 |
|
| 213 |
CLog::Log(LOGERROR, "Invalid server response: {}", strResponse); |
| 214 |
} |
| 215 |
} |
| 216 |
else |
| 217 |
{ |
| 218 |
// "RetroAchievements", "Failed to contact server" |
| 219 |
CServiceBroker::GetEventLog()->AddWithNotification( |
| 220 |
EventPtr(new CNotificationEvent(35264, 35266, EventLevel::Error))); |
| 221 |
} |
| 222 |
return token; |
| 223 |
} |
| 224 |
|
| 225 |
bool CGameSettings::IsAccountVerified(const std::string& username, const std::string& token) const |
| 226 |
{ |
| 227 |
XFILE::CFile request; |
| 228 |
const CURL getPatchFileUrl(StringUtils::Format(GET_PATCH_DATA_URL_TEMPLATE, username, token)); |
| 229 |
std::vector<uint8_t> response; |
| 230 |
if (request.LoadFile(getPatchFileUrl, response) > 0) |
| 231 |
{ |
| 232 |
std::string strResponse(response.begin(), response.end()); |
| 233 |
CVariant data(CVariant::VariantTypeObject); |
| 234 |
|
| 235 |
if (CJSONVariantParser::Parse(strResponse, data)) |
| 236 |
{ |
| 237 |
return data[SUCCESS].asBoolean(); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
return false; |
| 242 |
} |