summaryrefslogtreecommitdiffstats
path: root/xbmc/games/GameSettings.cpp
blob: acbccdc6fb047888a316a482d7a6277170a25b34 (plain)
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
/*
 *  Copyright (C) 2012-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 "GameSettings.h"

#include "ServiceBroker.h"
#include "URL.h"
#include "events/EventLog.h"
#include "events/NotificationEvent.h"
#include "filesystem/File.h"
#include "settings/Settings.h"
#include "settings/SettingsComponent.h"
#include "settings/lib/Setting.h"
#include "utils/JSONVariantParser.h"
#include "utils/StringUtils.h"
#include "utils/Variant.h"
#include "utils/log.h"

#include <algorithm>
#include <vector>

using namespace KODI;
using namespace GAME;

namespace
{
const std::string SETTING_GAMES_ENABLE = "gamesgeneral.enable";
const std::string SETTING_GAMES_SHOW_OSD_HELP = "gamesgeneral.showosdhelp";
const std::string SETTING_GAMES_ENABLEAUTOSAVE = "gamesgeneral.enableautosave";
const std::string SETTING_GAMES_ENABLEREWIND = "gamesgeneral.enablerewind";
const std::string SETTING_GAMES_REWINDTIME = "gamesgeneral.rewindtime";
const std::string SETTING_GAMES_ACHIEVEMENTS_USERNAME = "gamesachievements.username";
const std::string SETTING_GAMES_ACHIEVEMENTS_PASSWORD = "gamesachievements.password";
const std::string SETTING_GAMES_ACHIEVEMENTS_TOKEN = "gamesachievements.token";
const std::string SETTING_GAMES_ACHIEVEMENTS_LOGGED_IN = "gamesachievements.loggedin";

constexpr auto LOGIN_TO_RETRO_ACHIEVEMENTS_URL_TEMPLATE =
    "http://retroachievements.org/dorequest.php?r=login&u={}&p={}";
constexpr auto GET_PATCH_DATA_URL_TEMPLATE =
    "http://retroachievements.org/dorequest.php?r=patch&u={}&t={}&g=0";
constexpr auto SUCCESS = "Success";
constexpr auto TOKEN = "Token";
} // namespace

CGameSettings::CGameSettings()
{
  m_settings = CServiceBroker::GetSettingsComponent()->GetSettings();

  m_settings->RegisterCallback(this, {SETTING_GAMES_ENABLEREWIND, SETTING_GAMES_REWINDTIME,
                                      SETTING_GAMES_ACHIEVEMENTS_USERNAME,
                                      SETTING_GAMES_ACHIEVEMENTS_PASSWORD,
                                      SETTING_GAMES_ACHIEVEMENTS_LOGGED_IN});
}

CGameSettings::~CGameSettings()
{
  m_settings->UnregisterCallback(this);
}

bool CGameSettings::GamesEnabled()
{
  return m_settings->GetBool(SETTING_GAMES_ENABLE);
}

bool CGameSettings::ShowOSDHelp()
{
  return m_settings->GetBool(SETTING_GAMES_SHOW_OSD_HELP);
}

void CGameSettings::SetShowOSDHelp(bool bShow)
{
  if (m_settings->GetBool(SETTING_GAMES_SHOW_OSD_HELP) != bShow)
  {
    m_settings->SetBool(SETTING_GAMES_SHOW_OSD_HELP, bShow);

    //! @todo Asynchronous save
    m_settings->Save();
  }
}

void CGameSettings::ToggleGames()
{
  m_settings->ToggleBool(SETTING_GAMES_ENABLE);
}

bool CGameSettings::AutosaveEnabled()
{
  return m_settings->GetBool(SETTING_GAMES_ENABLEAUTOSAVE);
}

bool CGameSettings::RewindEnabled()
{
  return m_settings->GetBool(SETTING_GAMES_ENABLEREWIND);
}

unsigned int CGameSettings::MaxRewindTimeSec()
{
  int rewindTimeSec = m_settings->GetInt(SETTING_GAMES_REWINDTIME);

  return static_cast<unsigned int>(std::max(rewindTimeSec, 0));
}

std::string CGameSettings::GetRAUsername() const
{
  return m_settings->GetString(SETTING_GAMES_ACHIEVEMENTS_USERNAME);
}

std::string CGameSettings::GetRAToken() const
{
  return m_settings->GetString(SETTING_GAMES_ACHIEVEMENTS_TOKEN);
}

void CGameSettings::OnSettingChanged(const std::shared_ptr<const CSetting>& setting)
{
  if (setting == nullptr)
    return;

  const std::string& settingId = setting->GetId();

  if (settingId == SETTING_GAMES_ENABLEREWIND || settingId == SETTING_GAMES_REWINDTIME)
  {
    SetChanged();
    NotifyObservers(ObservableMessageSettingsChanged);
  }
  else if (settingId == SETTING_GAMES_ACHIEVEMENTS_LOGGED_IN &&
           std::dynamic_pointer_cast<const CSettingBool>(setting)->GetValue())
  {
    const std::string username = m_settings->GetString(SETTING_GAMES_ACHIEVEMENTS_USERNAME);
    const std::string password = m_settings->GetString(SETTING_GAMES_ACHIEVEMENTS_PASSWORD);
    std::string token = m_settings->GetString(SETTING_GAMES_ACHIEVEMENTS_TOKEN);

    token = LoginToRA(username, password, std::move(token));

    m_settings->SetString(SETTING_GAMES_ACHIEVEMENTS_TOKEN, token);

    if (!token.empty())
    {
      m_settings->SetBool(SETTING_GAMES_ACHIEVEMENTS_LOGGED_IN, true);
    }
    else
    {
      if (settingId == SETTING_GAMES_ACHIEVEMENTS_PASSWORD)
        m_settings->SetString(SETTING_GAMES_ACHIEVEMENTS_PASSWORD, "");
      m_settings->SetBool(SETTING_GAMES_ACHIEVEMENTS_LOGGED_IN, false);
    }

    m_settings->Save();
  }
  else if (settingId == SETTING_GAMES_ACHIEVEMENTS_LOGGED_IN &&
           !std::dynamic_pointer_cast<const CSettingBool>(setting)->GetValue())
  {
    m_settings->SetString(SETTING_GAMES_ACHIEVEMENTS_TOKEN, "");
    m_settings->Save();
  }
  else if (settingId == SETTING_GAMES_ACHIEVEMENTS_USERNAME ||
           settingId == SETTING_GAMES_ACHIEVEMENTS_PASSWORD)
  {
    m_settings->SetBool(SETTING_GAMES_ACHIEVEMENTS_LOGGED_IN, false);
    m_settings->SetString(SETTING_GAMES_ACHIEVEMENTS_TOKEN, "");
    m_settings->Save();
  }
}

std::string CGameSettings::LoginToRA(const std::string& username,
                                     const std::string& password,
                                     std::string token) const
{
  if (username.empty() || password.empty())
    return token;

  XFILE::CFile request;
  const CURL loginUrl(
      StringUtils::Format(LOGIN_TO_RETRO_ACHIEVEMENTS_URL_TEMPLATE, username, password));

  std::vector<uint8_t> response;
  if (request.LoadFile(loginUrl, response) > 0)
  {
    std::string strResponse(response.begin(), response.end());
    CVariant data(CVariant::VariantTypeObject);
    if (CJSONVariantParser::Parse(strResponse, data))
    {
      if (data[SUCCESS].asBoolean())
      {
        token = data[TOKEN].asString();
        if (!IsAccountVerified(username, token))
        {
          token.clear();
          // "RetroAchievements", "Your account is not verified, please check your emails to complete your sign up"
          CServiceBroker::GetEventLog()->AddWithNotification(
              EventPtr(new CNotificationEvent(35264, 35270, EventLevel::Error)));
        }
      }
      else
      {
        token.clear();

        // "RetroAchievements", "Incorrect User/Password!"
        CServiceBroker::GetEventLog()->AddWithNotification(
            EventPtr(new CNotificationEvent(35264, 35265, EventLevel::Error)));
      }
    }
    else
    {
      // "RetroAchievements", "Invalid response from server"
      CServiceBroker::GetEventLog()->AddWithNotification(
          EventPtr(new CNotificationEvent(35264, 35267, EventLevel::Error)));

      CLog::Log(LOGERROR, "Invalid server response: {}", strResponse);
    }
  }
  else
  {
    // "RetroAchievements", "Failed to contact server"
    CServiceBroker::GetEventLog()->AddWithNotification(
        EventPtr(new CNotificationEvent(35264, 35266, EventLevel::Error)));
  }
  return token;
}

bool CGameSettings::IsAccountVerified(const std::string& username, const std::string& token) const
{
  XFILE::CFile request;
  const CURL getPatchFileUrl(StringUtils::Format(GET_PATCH_DATA_URL_TEMPLATE, username, token));
  std::vector<uint8_t> response;
  if (request.LoadFile(getPatchFileUrl, response) > 0)
  {
    std::string strResponse(response.begin(), response.end());
    CVariant data(CVariant::VariantTypeObject);

    if (CJSONVariantParser::Parse(strResponse, data))
    {
      return data[SUCCESS].asBoolean();
    }
  }

  return false;
}