Qt: use simple curl wrapper to avoid some pitfalls

This commit is contained in:
Megamouse 2020-03-22 17:49:33 +01:00
parent 3c63db93ed
commit b447e6f55d
9 changed files with 74 additions and 38 deletions

View File

@ -1266,6 +1266,7 @@
<ClCompile Include="rpcs3qt\breakpoint_handler.cpp" />
<ClCompile Include="rpcs3qt\breakpoint_list.cpp" />
<ClCompile Include="rpcs3qt\cheat_manager.cpp" />
<ClCompile Include="rpcs3qt\curl_handle.cpp" />
<ClCompile Include="rpcs3qt\custom_dialog.cpp" />
<ClCompile Include="rpcs3qt\debugger_list.cpp" />
<ClCompile Include="rpcs3qt\gui_application.cpp" />
@ -1785,6 +1786,7 @@
<Command Condition="'$(Configuration)|$(Platform)'=='Debug - LLVM|x64'">"$(QTDIR)\bin\moc.exe" "%(FullPath)" -o ".\QTGeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp" -D_WINDOWS -DUNICODE -DWIN32 -DWIN64 -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_WINEXTRAS_LIB -DQT_CONCURRENT_LIB -D%(PreprocessorDefinitions) "-I.\..\3rdparty\wolfssl" "-I.\..\3rdparty\curl\include" "-I.\..\3rdparty\libusb\libusb" "-I$(VULKAN_SDK)\Include" "-I.\..\3rdparty\XAudio2Redist\include" "-I$(QTDIR)\include" "-I$(QTDIR)\include\QtWidgets" "-I$(QTDIR)\include\QtGui" "-I$(QTDIR)\include\QtANGLE" "-I$(QTDIR)\include\QtCore" "-I.\debug" "-I$(QTDIR)\mkspecs\win32-msvc2015" "-I.\QTGeneratedFiles\$(ConfigurationName)" "-I.\QTGeneratedFiles" "-I$(QTDIR)\include\QtWinExtras" "-I$(QTDIR)\include\QtConcurrent"</Command>
</CustomBuild>
<ClInclude Include="rpcs3qt\category.h" />
<ClInclude Include="rpcs3qt\curl_handle.h" />
<ClInclude Include="rpcs3qt\custom_dock_widget.h" />
<CustomBuild Include="rpcs3qt\debugger_list.h">
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Release - LLVM|x64'">$(QTDIR)\bin\moc.exe;%(FullPath)</AdditionalInputs>

View File

@ -883,6 +883,9 @@
<ClCompile Include="QTGeneratedFiles\Debug - LLVM\moc_update_manager.cpp">
<Filter>Generated Files\Debug - LLVM</Filter>
</ClCompile>
<ClCompile Include="rpcs3qt\curl_handle.cpp">
<Filter>rpcs3</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="\rpcs3qt\*.h">
@ -984,6 +987,9 @@
<ClInclude Include="rpcs3qt\category.h">
<Filter>Gui\game list</Filter>
</ClInclude>
<ClInclude Include="rpcs3qt\curl_handle.h">
<Filter>rpcs3</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="debug\moc_predefs.h.cbt">

View File

@ -5,6 +5,7 @@
breakpoint_list.cpp
cg_disasm_window.cpp
cheat_manager.cpp
curl_handle.cpp
custom_dialog.cpp
debugger_frame.cpp
debugger_list.cpp

View File

@ -0,0 +1,24 @@
#include "stdafx.h"
#include "curl_handle.h"
#include "Emu/System.h"
curl_handle::curl_handle(QObject* parent) : QObject(parent)
{
m_curl = curl_easy_init();
#ifdef _WIN32
// This shouldn't be needed on linux
const std::string path_to_cert = Emulator::GetEmuDir() + "cacert.pem";
curl_easy_setopt(m_curl, CURLOPT_CAINFO, path_to_cert.c_str());
#endif
}
curl_handle::~curl_handle()
{
curl_easy_cleanup(m_curl);
}
CURL* curl_handle::get_curl()
{
return m_curl;
}

View File

@ -0,0 +1,20 @@
#pragma once
#include <QObject>
#ifndef CURL_STATICLIB
#define CURL_STATICLIB
#endif
#include <curl/curl.h>
class curl_handle : public QObject
{
private:
CURL* m_curl = nullptr;
public:
curl_handle(QObject* parent = nullptr);
~curl_handle();
CURL* get_curl();
};

View File

@ -1,18 +1,13 @@
#include "game_compatibility.h"
#include "gui_settings.h"
#include "progress_dialog.h"
#include "curl_handle.h"
#include <QApplication>
#include <QMessageBox>
#include <QJsonDocument>
#include <QThread>
#define NOMINMAX
#ifndef CURL_STATICLIB
#define CURL_STATICLIB
#endif
#include <curl/curl.h>
LOG_CHANNEL(compat_log, "Compat");
constexpr auto qstr = QString::fromStdString;
@ -29,12 +24,7 @@ game_compatibility::game_compatibility(std::shared_ptr<gui_settings> settings) :
m_filepath = m_xgui_settings->GetSettingsDir() + "/compat_database.dat";
m_url = "https://rpcs3.net/compatibility?api=v1&export";
m_curl = curl_easy_init();
#ifdef _WIN32
// This shouldn't be needed on linux
curl_easy_setopt(m_curl, CURLOPT_CAINFO, "cacert.pem");
#endif
m_curl = new curl_handle(this);
RequestCompatibility();
@ -68,7 +58,7 @@ size_t game_compatibility::update_buffer(char* data, size_t size)
if (m_actual_dwnld_size < 0)
{
if (curl_easy_getinfo(m_curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &m_actual_dwnld_size) == CURLE_OK && 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);
}
@ -183,10 +173,10 @@ void game_compatibility::RequestCompatibility(bool online)
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, CURLOPT_URL, m_url.c_str());
curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, curl_write_cb_compat);
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, this);
curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1);
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;
@ -197,7 +187,7 @@ void game_compatibility::RequestCompatibility(bool online)
auto thread = QThread::create([&]
{
const auto result = curl_easy_perform(m_curl);
const auto result = curl_easy_perform(m_curl->get_curl());
m_curl_result = result == CURLE_OK;
if (!m_curl_result)

View File

@ -5,6 +5,7 @@
#include <QPainter>
#include <QJsonObject>
class curl_handle;
class gui_settings;
class progress_dialog;
@ -40,7 +41,7 @@ private:
std::atomic<bool> m_curl_result = false;
std::atomic<bool> m_curl_abort = false;
double m_actual_dwnld_size = -1.0;
void* m_curl = nullptr;
curl_handle* m_curl = nullptr;
QByteArray m_curl_buf;
progress_dialog* m_progress_dialog = nullptr;
std::shared_ptr<gui_settings> m_xgui_settings;

View File

@ -3,6 +3,7 @@
#include "progress_dialog.h"
#include "localized.h"
#include "rpcs3_version.h"
#include "curl_handle.h"
#include "Utilities/StrUtil.h"
#include "Crypto/sha256.h"
#include "Emu/System.h"
@ -32,11 +33,6 @@
#include <sys/stat.h>
#endif
#ifndef CURL_STATICLIB
#define CURL_STATICLIB
#endif
#include <curl/curl.h>
LOG_CHANNEL(update_log, "UPDATER");
size_t curl_write_cb(char* ptr, size_t /*size*/, size_t nmemb, void* userdata)
@ -47,12 +43,7 @@ size_t curl_write_cb(char* ptr, size_t /*size*/, size_t nmemb, void* userdata)
update_manager::update_manager()
{
m_curl = curl_easy_init();
#ifdef _WIN32
// This shouldn't be needed on linux
curl_easy_setopt(m_curl, CURLOPT_CAINFO, "cacert.pem");
#endif
m_curl = new curl_handle(this);
// We need this signal in order to update the GUI from the main thread
connect(this, &update_manager::signal_buffer_update, this, &update_manager::handle_buffer_update);
@ -108,13 +99,13 @@ void update_manager::check_for_updates(bool automatic, QWidget* parent)
connect(m_progress_dialog, &QProgressDialog::finished, m_progress_dialog, &QProgressDialog::deleteLater);
const std::string request_url = "https://update.rpcs3.net/?api=v1&c=" + rpcs3::get_commit_and_hash().second;
curl_easy_setopt(m_curl, CURLOPT_URL, request_url.c_str());
curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, curl_write_cb);
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, this);
curl_easy_setopt(m_curl->get_curl(), CURLOPT_URL, request_url.c_str());
curl_easy_setopt(m_curl->get_curl(), CURLOPT_WRITEFUNCTION, curl_write_cb);
curl_easy_setopt(m_curl->get_curl(), CURLOPT_WRITEDATA, this);
auto thread = QThread::create([this]
{
const auto curl_result = curl_easy_perform(m_curl);
const auto curl_result = curl_easy_perform(m_curl->get_curl());
m_curl_result = curl_result == CURLE_OK;
if (!m_curl_result)
@ -270,14 +261,14 @@ bool update_manager::handle_json(bool automatic)
m_update_dialog = true;
const std::string request_url = latest[os]["download"].toString().toStdString();
curl_easy_setopt(m_curl, CURLOPT_URL, request_url.c_str());
curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(m_curl->get_curl(), CURLOPT_URL, request_url.c_str());
curl_easy_setopt(m_curl->get_curl(), CURLOPT_FOLLOWLOCATION, 1);
m_curl_buf.clear();
auto thread = QThread::create([this]
{
const auto curl_result = curl_easy_perform(m_curl);
const auto curl_result = curl_easy_perform(m_curl->get_curl());
m_curl_result = curl_result == CURLE_OK;
if (!m_curl_result)

View File

@ -4,6 +4,7 @@
#include <QObject>
#include <QByteArray>
class curl_handle;
class progress_dialog;
class update_manager final : public QObject
@ -15,7 +16,7 @@ private:
progress_dialog* m_progress_dialog = nullptr;
QWidget* m_parent = nullptr;
void* m_curl = nullptr;
curl_handle* m_curl = nullptr;
QByteArray m_curl_buf;
std::atomic<bool> m_curl_abort = false;
std::atomic<bool> m_curl_result = false;