SaveTool: remove dependency on cpr, switch to raw libcurl.

Took me pretty much a whole day, but I managed to do it.
This commit is contained in:
Guillaume Jacquemin 2022-03-31 17:57:53 +02:00
parent b909aa85b7
commit a166948aec
7 changed files with 86 additions and 25 deletions

6
.gitmodules vendored
View file

@ -26,7 +26,7 @@
path = third-party/efsw
url = https://github.com/SpartanJ/efsw
branch = master
[submodule "third-party/cpr"]
path = third-party/cpr
url = https://github.com/whoshuu/cpr
[submodule "libcurl"]
path = third-party/curl
url = https://github.com/curl/curl
branch = master

View file

@ -88,8 +88,14 @@ set(BUILD_TEST_APP OFF CACHE BOOL "" FORCE)
set(EFSW_INSTALL OFF CACHE BOOL "" FORCE)
add_subdirectory(third-party/efsw EXCLUDE_FROM_ALL)
set(CPR_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(CMAKE_USE_LIBSSH2 OFF CACHE BOOL "" FORCE) # For some reason, even when HTTP_ONLY is set to ON, libcurl will try to link to libssh2.
add_subdirectory(third-party/cpr EXCLUDE_FROM_ALL)
set(BUILD_CURL_EXE OFF CACHE BOOL "" FORCE)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
set(ENABLE_UNICODE ON CACHE BOOL "" FORCE)
set(ENABLE_DEBUG ON CACHE BOOL "" FORCE)
set(ENABLE_THREADED_RESOLVER OFF CACHE BOOL "" FORCE)
set(HTTP_ONLY ON CACHE BOOL "" FORCE)
set(CURL_USE_SCHANNEL ON CACHE BOOL "" FORCE)
set(CURL_USE_LIBSSH2 OFF CACHE BOOL "" FORCE) # For some reason, even when HTTP_ONLY is set to ON, libcurl will try to link to libssh2.
add_subdirectory(third-party/curl EXCLUDE_FROM_ALL)
add_subdirectory(src)

View file

@ -191,6 +191,6 @@ target_link_libraries(MassBuilderSaveTool PRIVATE
UESaveFile
efsw
zip
cpr::cpr
libcurl
imm32
wtsapi32)

View file

@ -33,7 +33,7 @@
#include <Magnum/ImGuiIntegration/Integration.h>
#include <Magnum/ImGuiIntegration/Context.hpp>
#include <cpr/cpr.h>
#include <curl/curl.h>
#include <windef.h>
#include <winuser.h>
@ -151,9 +151,10 @@ SaveTool::SaveTool(const Arguments& arguments):
break;
}
curl_global_init(CURL_GLOBAL_DEFAULT);
if(_checkUpdatesOnStartup) {
_updateThread = std::thread{[this]{ checkForUpdates(); }};
_queue.addToast(Toast::Type::Default, "Checking for updates..."_s);
_updateThread = std::thread{[this]{ checkForUpdates(); }};
}
if(GL::Context::current().isExtensionSupported<GL::Extensions::KHR::debug>() &&
@ -174,6 +175,9 @@ SaveTool::SaveTool(const Arguments& arguments):
SaveTool::~SaveTool() {
Utility::Debug{} << "===Perfoming cleanup===";
Utility::Debug{} << "Shutting libcurl down...";
curl_global_cleanup();
SDL_RemoveTimer(_gameCheckTimerId);
Utility::Debug{} << "Saving the configuration...";
@ -316,20 +320,22 @@ void SaveTool::initEvent(SDL_Event& event) {
void SaveTool::updateCheckEvent(SDL_Event& event) {
_updateThread.join();
cpr::Response r{std::move(*static_cast<cpr::Response*>(event.user.data1))};
delete static_cast<cpr::Response*>(event.user.data1);
if(r.elapsed > 10.0) {
if(event.user.code == CurlInitFailed) {
_queue.addToast(Toast::Type::Error, "Couldn't initialise libcurl. Update check aborted."_s);
return;
}
else if(event.user.code == CurlError) {
Containers::String error{static_cast<char*>(event.user.data2), CURL_ERROR_SIZE, nullptr};
_queue.addToast(Toast::Type::Error, error, std::chrono::milliseconds{5000});
_queue.addToast(Toast::Type::Error, static_cast<char*>(event.user.data1), std::chrono::milliseconds{5000});
return;
}
else if(event.user.code == CurlTimeout) {
_queue.addToast(Toast::Type::Error, "The request timed out."_s);
return;
}
if(r.status_code == 0) {
_queue.addToast(Toast::Type::Error, "Seems like the connection was blocked.\nPlease check your firewall's settings.");
}
if(r.status_code != 200) {
_queue.addToast(Toast::Type::Error, Utility::format("The request failed with error code {}:\n{}", r.status_code, r.reason.c_str()));
else if(event.user.code != 200) {
_queue.addToast(Toast::Type::Error, Utility::format("The request failed with error code {}", event.user.code));
return;
}
@ -373,13 +379,13 @@ void SaveTool::updateCheckEvent(SDL_Event& event) {
}
}
operator Containers::String() const {
return Utility::format("{}.{}.{}", major, minor, patch);
return Utility::format("{}.{}.{}{}", major, minor, patch, prerelease ? "-pre" : "");
}
};
static const Version current_ver{SAVETOOL_VERSION};
Containers::String response = r.text.c_str();
Containers::String response{static_cast<char*>(event.user.data1), strlen(static_cast<char*>(event.user.data1)), nullptr};
auto components = response.split('\n');
Version latest_ver{components.front()};
@ -890,12 +896,55 @@ void SaveTool::checkGameState() {
}
}
inline auto writeData(char* ptr, std::size_t size, std::size_t nmemb, Containers::String* buf)-> std::size_t {
if(!ptr || !buf) return 0;
(*buf) = Utility::format("{}{}", *buf, Containers::StringView{ptr, size * nmemb});
return size * nmemb;
}
void SaveTool::checkForUpdates() {
cpr::Response r = cpr::Get(cpr::Url{"https://williamjcm.ovh/mbst/version"}, cpr::Timeout{10000});
auto curl = curl_easy_init();
SDL_Event event;
SDL_zero(event);
event.type = _updateEventId;
event.user.data1 = new cpr::Response{std::move(r)};
if(!curl) {
event.user.code = CurlInitFailed;
}
if(curl) {
Containers::String response_body{Containers::AllocatedInit, ""};
Containers::String error_buffer{ValueInit, CURL_ERROR_SIZE * 2};
curl_easy_setopt(curl, CURLOPT_URL, "https://williamjcm.ovh/mbst/version");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_body);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer.data());
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 10000L);
auto code = curl_easy_perform(curl);
if(code == CURLE_OK) {
long status = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
event.user.code = Int(status);
event.user.data1 = response_body.release();
}
else if(code == CURLE_OPERATION_TIMEDOUT) {
event.user.code = CurlTimeout;
}
else {
event.user.code = CurlError;
event.user.data1 = const_cast<char*>(curl_easy_strerror(code));
event.user.data2 = Containers::String{error_buffer}.release();
}
curl_easy_cleanup(curl);
}
SDL_PushEvent(&event);
}

View file

@ -81,6 +81,12 @@ class SaveTool: public Platform::Sdl2Application, public efsw::FileWatchListener
ProfileManagerFailure
};
void initEvent(SDL_Event& event);
enum UpdateCheckStatus : Int {
CurlInitFailed = 0,
CurlError = 1,
CurlTimeout = 2,
};
void updateCheckEvent(SDL_Event& event);
enum FileEventType: Int {

1
third-party/cpr vendored

@ -1 +0,0 @@
Subproject commit eccea39e4b112a088ae665eda5854cc366f86128

1
third-party/curl vendored Submodule

@ -0,0 +1 @@
Subproject commit 64db5c575d9c5536bd273a890f50777ad1ca7c13