Debian Codegraph @

xbmc/cores/RetroPlayer/cheevos/Cheevos.cpp

raw
1 /*
2 * Copyright (C) 2020-2021 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 "Cheevos.h"
10
11 #include "FileItem.h"
12 #include "ServiceBroker.h"
13 #include "URL.h"
14 #include "filesystem/CurlFile.h"
15 #include "filesystem/File.h"
16 #include "games/addons/GameClient.h"
17 #include "games/addons/cheevos/GameClientCheevos.h"
18 #include "games/tags/GameInfoTag.h"
19 #include "messaging/ApplicationMessenger.h"
20 #include "utils/JSONVariantParser.h"
21 #include "utils/URIUtils.h"
22 #include "utils/Variant.h"
23 #include "utils/log.h"
24 #include "vector"
25
26 using namespace KODI;
27 using namespace RETRO;
28
29 namespace
30 {
31 // API JSON Field names
32 constexpr auto SUCCESS = "Success";
33 constexpr auto GAME_ID = "GameID";
34 constexpr auto PATCH_DATA = "PatchData";
35 constexpr auto RICH_PRESENCE = "RichPresencePatch";
36 constexpr auto GAME_TITLE = "Title";
37 constexpr auto PUBLISHER = "Publisher";
38 constexpr auto DEVELOPER = "Developer";
39 constexpr auto GENRE = "Genre";
40 constexpr auto CONSOLE_NAME = "ConsoleName";
41
42 constexpr int RESPONSE_SIZE = 64;
43 } // namespace
44
45 CCheevos::CCheevos(GAME::CGameClient* gameClient,
46 const std::string& userName,
47 const std::string& loginToken)
48 : m_gameClient(gameClient), m_userName(userName), m_loginToken(loginToken)
49 {
50 }
51
52 void CCheevos::ResetRuntime()
53 {
54 m_gameClient->Cheevos().RCResetRuntime();
55 }
56
57 bool CCheevos::LoadData()
58 {
59 if (m_userName.empty() || m_loginToken.empty())
60 return false;
61
62 if (m_romHash.empty())
63 {
64 m_consoleID = ConsoleID();
65 if (m_consoleID == RConsoleID::RC_INVALID_ID)
66 return false;
67
68 std::string hash;
69 if (!m_gameClient->Cheevos().RCGenerateHashFromFile(hash, m_consoleID,
70 m_gameClient->GetGamePath().c_str()))
71 {
72 return false;
73 }
74
75 m_romHash = hash;
76 }
77
78 std::string requestURL;
79
80 if (!m_gameClient->Cheevos().RCGetGameIDUrl(requestURL, m_romHash))
81 return false;
82
83 XFILE::CFile response;
84 response.CURLCreate(requestURL);
85 response.CURLOpen(0);
86
87 char responseStr[RESPONSE_SIZE];
88 response.ReadString(responseStr, RESPONSE_SIZE);
89
90 response.Close();
91
92 CVariant data(CVariant::VariantTypeObject);
93 CJSONVariantParser::Parse(responseStr, data);
94
95 if (!data[SUCCESS].asBoolean())
96 return false;
97
98 m_gameID = data[GAME_ID].asUnsignedInteger32();
99
100 // For some reason RetroAchievements returns Success = true when the hash isn't found
101 if (m_gameID == 0)
102 return false;
103
104 if (!m_gameClient->Cheevos().RCGetPatchFileUrl(requestURL, m_userName, m_loginToken, m_gameID))
105 return false;
106
107 CURL curl(requestURL);
108 std::vector<uint8_t> patchData;
109 response.LoadFile(curl, patchData);
110
111 std::string strResponse(patchData.begin(), patchData.end());
112 CJSONVariantParser::Parse(strResponse, data);
113
114 if (!data[SUCCESS].asBoolean())
115 return false;
116
117 m_richPresenceScript = data[PATCH_DATA][RICH_PRESENCE].asString();
118 m_richPresenceLoaded = true;
119
120 std::unique_ptr<CFileItem> file{std::make_unique<CFileItem>()};
121
122 GAME::CGameInfoTag& tag = *file->GetGameInfoTag();
123 tag.SetTitle(data[PATCH_DATA][GAME_TITLE].asString());
124 tag.SetPublisher(data[PATCH_DATA][PUBLISHER].asString());
125 tag.SetDeveloper(data[PATCH_DATA][DEVELOPER].asString());
126 tag.SetGenres({data[PATCH_DATA][GENRE].asString()});
127 tag.SetPlatform(data[PATCH_DATA][CONSOLE_NAME].asString());
128
129 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_UPDATE_PLAYER_ITEM, -1, -1,
130 static_cast<void*>(file.release()));
131
132 return true;
133 }
134
135 void CCheevos::EnableRichPresence()
136 {
137 if (!m_richPresenceLoaded)
138 {
139 if (!LoadData())
140 {
141 CLog::Log(LOGERROR, "Cheevos: Couldn't load patch file");
142 return;
143 }
144 }
145
146 m_gameClient->Cheevos().RCEnableRichPresence(m_richPresenceScript);
147 m_richPresenceScript.clear();
148 }
149
150 std::string CCheevos::GetRichPresenceEvaluation()
151 {
152 if (!m_richPresenceLoaded)
153 {
154 CLog::Log(LOGERROR, "Cheevos: Rich Presence script was not found");
155 return "";
156 }
157
158 std::string evaluation;
159 m_gameClient->Cheevos().RCGetRichPresenceEvaluation(evaluation, m_consoleID);
160
161 std::string url;
162 std::string postData;
163 if (m_gameClient->Cheevos().RCPostRichPresenceUrl(url, postData, m_userName, m_loginToken,
164 m_gameID, evaluation))
165 {
166 XFILE::CCurlFile curl;
167 std::string res;
168 curl.Post(url, postData, res);
169 }
170
171 return evaluation;
172 }
173
174 RConsoleID CCheevos::ConsoleID()
175 {
176 const std::string extension = URIUtils::GetExtension(m_gameClient->GetGamePath());
177 auto it = m_extensionToConsole.find(extension);
178
179 if (it == m_extensionToConsole.end())
180 return RConsoleID::RC_INVALID_ID;
181
182 return it->second;
183 }