forked from RPCS3/rpcs3
-
Notifications
You must be signed in to change notification settings - Fork 4
/
game_compatibility.cpp
263 lines (210 loc) · 6.72 KB
/
game_compatibility.cpp
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
#include "game_compatibility.h"
#include "gui_settings.h"
#include "progress_dialog.h"
#include "curl_handle.h"
#include <QApplication>
#include <QMessageBox>
#include <QJsonDocument>
#include <QThread>
LOG_CHANNEL(compat_log, "Compat");
constexpr auto qstr = QString::fromStdString;
inline std::string sstr(const QString& _in) { return _in.toStdString(); }
size_t curl_write_cb_compat(char* ptr, size_t /*size*/, size_t nmemb, void* userdata)
{
game_compatibility* gm_cmp = reinterpret_cast<game_compatibility*>(userdata);
return gm_cmp->update_buffer(ptr, nmemb);
}
game_compatibility::game_compatibility(std::shared_ptr<gui_settings> settings) : m_xgui_settings(settings)
{
m_filepath = m_xgui_settings->GetSettingsDir() + "/compat_database.dat";
m_url = "https://rpcs3.net/compatibility?api=v1&export";
m_curl = new curl_handle(this);
RequestCompatibility();
// We need this signal in order to update the GUI from the main thread
connect(this, &game_compatibility::signal_buffer_update, this, &game_compatibility::handle_buffer_update);
}
void game_compatibility::handle_buffer_update(int size, int max)
{
if (m_progress_dialog)
{
m_progress_dialog->setMaximum(max);
m_progress_dialog->setValue(size);
QApplication::processEvents();
}
}
size_t game_compatibility::update_buffer(char* data, size_t size)
{
if (m_curl_abort)
{
return 0;
}
const auto old_size = m_curl_buf.size();
const auto new_size = old_size + size;
m_curl_buf.resize(static_cast<int>(new_size));
memcpy(m_curl_buf.data() + old_size, data, size);
int max = m_progress_dialog ? m_progress_dialog->maximum() : 0;
if (m_actual_dwnld_size < 0)
{
if (curl_easy_getinfo(m_curl->get_curl(), CURLINFO_CONTENT_LENGTH_DOWNLOAD, &m_actual_dwnld_size) == CURLE_OK && m_actual_dwnld_size > 0)
{
max = static_cast<int>(m_actual_dwnld_size);
}
}
Q_EMIT signal_buffer_update(static_cast<int>(new_size), max);
return size;
}
bool game_compatibility::ReadJSON(const QJsonObject& json_data, bool after_download)
{
int return_code = json_data["return_code"].toInt();
if (return_code < 0)
{
if (after_download)
{
std::string error_message;
switch (return_code)
{
case -1:
error_message = "Server Error - Internal Error";
break;
case -2:
error_message = "Server Error - Maintenance Mode";
break;
default:
error_message = "Server Error - Unknown Error";
break;
}
compat_log.error("%s: return code %d", error_message, return_code);
Q_EMIT DownloadError(qstr(error_message) + " " + QString::number(return_code));
}
else
{
compat_log.error("Database Error - Invalid: return code %d", return_code);
}
return false;
}
if (!json_data["results"].isObject())
{
compat_log.error("Database Error - No Results found");
return false;
}
m_compat_database.clear();
QJsonObject json_results = json_data["results"].toObject();
// Retrieve status data for every valid entry
for (const auto& key : json_results.keys())
{
if (!json_results[key].isObject())
{
compat_log.error("Database Error - Unusable object %s", sstr(key));
continue;
}
QJsonObject json_result = json_results[key].toObject();
// Retrieve compatibility information from json
compat_status status = Status_Data.at(json_result.value("status").toString("NoResult"));
// Add date if possible
status.date = json_result.value("date").toString();
// Add latest version if possible
status.latest_version = json_result.value("update").toString();
// Add status to map
m_compat_database.emplace(std::pair<std::string, compat_status>(sstr(key), status));
}
return true;
}
void game_compatibility::RequestCompatibility(bool online)
{
if (!online)
{
// Retrieve database from file
QFile file(m_filepath);
if (!file.exists())
{
compat_log.notice("Database file not found: %s", sstr(m_filepath));
return;
}
if (!file.open(QIODevice::ReadOnly))
{
compat_log.error("Database Error - Could not read database from file: %s", sstr(m_filepath));
return;
}
QByteArray data = file.readAll();
file.close();
compat_log.notice("Finished reading database from file: %s", sstr(m_filepath));
// Create new map from database
ReadJSON(QJsonDocument::fromJson(data).object(), online);
return;
}
compat_log.notice("Beginning compatibility database download from: %s", m_url);
// Show Progress
m_progress_dialog = new progress_dialog(tr("Downloading Database"), tr("Please wait ..."), tr("Abort"), 0, 100, true);
m_progress_dialog->show();
curl_easy_setopt(m_curl->get_curl(), CURLOPT_URL, m_url.c_str());
curl_easy_setopt(m_curl->get_curl(), CURLOPT_WRITEFUNCTION, curl_write_cb_compat);
curl_easy_setopt(m_curl->get_curl(), CURLOPT_WRITEDATA, this);
curl_easy_setopt(m_curl->get_curl(), CURLOPT_FOLLOWLOCATION, 1);
m_curl_buf.clear();
m_curl_abort = false;
// Handle abort
connect(m_progress_dialog, &QProgressDialog::canceled, [this] { m_curl_abort = true; });
connect(m_progress_dialog, &QProgressDialog::finished, m_progress_dialog, &QProgressDialog::deleteLater);
auto thread = QThread::create([&]
{
const auto result = curl_easy_perform(m_curl->get_curl());
m_curl_result = result == CURLE_OK;
if (!m_curl_result)
{
Q_EMIT DownloadError(qstr("Curl error: ") + qstr(curl_easy_strerror(result)));
}
});
connect(thread, &QThread::finished, this, [this, online]()
{
if (m_progress_dialog)
{
m_progress_dialog->close();
m_progress_dialog = nullptr;
}
if (!m_curl_result)
{
return;
}
compat_log.notice("Database download finished");
// Create new map from database and write database to file if database was valid
if (ReadJSON(QJsonDocument::fromJson(m_curl_buf).object(), online))
{
// We have a new database in map, therefore refresh gamelist to new state
Q_EMIT DownloadFinished();
// Write database to file
QFile file(m_filepath);
if (file.exists())
{
compat_log.notice("Database file found: %s", sstr(m_filepath));
}
if (!file.open(QIODevice::WriteOnly))
{
compat_log.error("Database Error - Could not write database to file: %s", sstr(m_filepath));
return;
}
file.write(m_curl_buf);
file.close();
compat_log.success("Wrote database to file: %s", sstr(m_filepath));
}
});
thread->setObjectName("Compat Update");
thread->start();
// We want to retrieve a new database, therefore refresh gamelist and indicate that
Q_EMIT DownloadStarted();
}
compat_status game_compatibility::GetCompatibility(const std::string& title_id)
{
if (m_compat_database.empty())
{
return Status_Data.at("NoData");
}
else if (m_compat_database.count(title_id) > 0)
{
return m_compat_database[title_id];
}
return Status_Data.at("NoResult");
}
compat_status game_compatibility::GetStatusData(const QString& status)
{
return Status_Data.at(status);
}