Skip to content

Commit 0fe6e91

Browse files
committed
HTTPDownloader: Move to util project
1 parent 3c6b6c5 commit 0fe6e91

16 files changed

+108
-107
lines changed

src/common/CMakeLists.txt

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ add_library(common
2323
hash_combine.h
2424
heap_array.h
2525
heterogeneous_containers.h
26-
http_downloader.cpp
27-
http_downloader.h
2826
layered_settings_interface.cpp
2927
layered_settings_interface.h
3028
log.cpp
@@ -67,13 +65,11 @@ target_link_libraries(common PRIVATE stb zlib minizip Zstd::Zstd "${CMAKE_DL_LIB
6765

6866
if(WIN32)
6967
target_sources(common PRIVATE
70-
http_downloader_winhttp.cpp
71-
http_downloader_winhttp.h
7268
thirdparty/StackWalker.cpp
7369
thirdparty/StackWalker.h
7470
windows_headers.h
7571
)
76-
target_link_libraries(common PRIVATE winhttp.lib OneCore.lib)
72+
target_link_libraries(common PRIVATE OneCore.lib)
7773
endif()
7874

7975
if(MSVC)
@@ -98,19 +94,9 @@ if(APPLE)
9894
target_link_libraries(common PRIVATE ${COCOA_LIBRARY})
9995
endif()
10096

101-
if(NOT WIN32 AND NOT ANDROID)
102-
target_sources(common PRIVATE
103-
http_downloader_curl.cpp
104-
http_downloader_curl.h
105-
)
106-
target_link_libraries(common PRIVATE
107-
CURL::libcurl
108-
)
109-
110-
if(LIBBACKTRACE_FOUND)
111-
target_compile_definitions(common PRIVATE "-DENABLE_LIBBACKTRACE=1")
112-
target_link_libraries(common PRIVATE libbacktrace::libbacktrace)
113-
endif()
97+
if(NOT WIN32 AND NOT ANDROID AND LIBBACKTRACE_FOUND)
98+
target_compile_definitions(common PRIVATE "-DENABLE_LIBBACKTRACE=1")
99+
target_link_libraries(common PRIVATE libbacktrace::libbacktrace)
114100
endif()
115101

116102
if(ANDROID)

src/common/common.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<ItemDefinitionGroup>
1212
<Link>
13-
<AdditionalDependencies>%(AdditionalDependencies);winhttp.lib;OneCore.lib</AdditionalDependencies>
13+
<AdditionalDependencies>%(AdditionalDependencies);OneCore.lib</AdditionalDependencies>
1414
</Link>
1515
</ItemDefinitionGroup>
1616
</Project>

src/common/common.vcxproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
<ClInclude Include="file_system.h" />
1818
<ClInclude Include="hash_combine.h" />
1919
<ClInclude Include="heap_array.h" />
20-
<ClInclude Include="http_downloader.h" />
21-
<ClInclude Include="http_downloader_winhttp.h" />
2220
<ClInclude Include="image.h" />
2321
<ClInclude Include="intrin.h" />
2422
<ClInclude Include="layered_settings_interface.h" />
@@ -51,8 +49,6 @@
5149
<ClCompile Include="error.cpp" />
5250
<ClCompile Include="fastjmp.cpp" />
5351
<ClCompile Include="file_system.cpp" />
54-
<ClCompile Include="http_downloader.cpp" />
55-
<ClCompile Include="http_downloader_winhttp.cpp" />
5652
<ClCompile Include="image.cpp" />
5753
<ClCompile Include="layered_settings_interface.cpp" />
5854
<ClCompile Include="log.cpp" />

src/common/common.vcxproj.filters

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
<ClInclude Include="lru_cache.h" />
2929
<ClInclude Include="easing.h" />
3030
<ClInclude Include="error.h" />
31-
<ClInclude Include="http_downloader_winhttp.h" />
32-
<ClInclude Include="http_downloader.h" />
3331
<ClInclude Include="path.h" />
3432
<ClInclude Include="windows_headers.h" />
3533
<ClInclude Include="settings_interface.h" />
@@ -62,8 +60,6 @@
6260
</ClCompile>
6361
<ClCompile Include="crash_handler.cpp" />
6462
<ClCompile Include="error.cpp" />
65-
<ClCompile Include="http_downloader_winhttp.cpp" />
66-
<ClCompile Include="http_downloader.cpp" />
6763
<ClCompile Include="layered_settings_interface.cpp" />
6864
<ClCompile Include="memory_settings_interface.cpp" />
6965
<ClCompile Include="threading.cpp" />

src/core/achievements.cpp

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "common/assert.h"
2121
#include "common/error.h"
2222
#include "common/file_system.h"
23-
#include "common/http_downloader.h"
2423
#include "common/log.h"
2524
#include "common/md5_digest.h"
2625
#include "common/path.h"
@@ -29,6 +28,7 @@
2928
#include "common/string_util.h"
3029

3130
#include "util/cd_image.h"
31+
#include "util/http_downloader.h"
3232
#include "util/imgui_fullscreen.h"
3333
#include "util/imgui_manager.h"
3434
#include "util/platform_misc.h"
@@ -130,8 +130,8 @@ static void BeginLoadGame();
130130
static void UpdateGameSummary();
131131
static void DownloadImage(std::string url, std::string cache_filename);
132132

133-
static bool CreateClient(rc_client_t** client, std::unique_ptr<Common::HTTPDownloader>* http);
134-
static void DestroyClient(rc_client_t** client, std::unique_ptr<Common::HTTPDownloader>* http);
133+
static bool CreateClient(rc_client_t** client, std::unique_ptr<HTTPDownloader>* http);
134+
static void DestroyClient(rc_client_t** client, std::unique_ptr<HTTPDownloader>* http);
135135
static void ClientMessageCallback(const char* message, const rc_client_t* client);
136136
static uint32_t ClientReadMemory(uint32_t address, uint8_t* buffer, uint32_t num_bytes, rc_client_t* client);
137137
static void ClientServerCall(const rc_api_request_t* request, rc_client_server_callback_t callback, void* callback_data,
@@ -187,7 +187,7 @@ static bool s_using_raintegration = false;
187187
static std::recursive_mutex s_achievements_mutex;
188188
static rc_client_t* s_client;
189189
static std::string s_image_directory;
190-
static std::unique_ptr<Common::HTTPDownloader> s_http_downloader;
190+
static std::unique_ptr<HTTPDownloader> s_http_downloader;
191191

192192
static std::string s_game_path;
193193
static std::string s_game_hash;
@@ -305,9 +305,8 @@ std::string Achievements::GetGameHash(CDImage* image)
305305

306306
void Achievements::DownloadImage(std::string url, std::string cache_filename)
307307
{
308-
auto callback = [cache_filename](s32 status_code, std::string content_type,
309-
Common::HTTPDownloader::Request::Data data) {
310-
if (status_code != Common::HTTPDownloader::HTTP_STATUS_OK)
308+
auto callback = [cache_filename](s32 status_code, std::string content_type, HTTPDownloader::Request::Data data) {
309+
if (status_code != HTTPDownloader::HTTP_STATUS_OK)
311310
return;
312311

313312
if (!FileSystem::WriteBinaryFile(cache_filename.c_str(), data.data(), data.size()))
@@ -430,9 +429,9 @@ bool Achievements::Initialize()
430429
return true;
431430
}
432431

433-
bool Achievements::CreateClient(rc_client_t** client, std::unique_ptr<Common::HTTPDownloader>* http)
432+
bool Achievements::CreateClient(rc_client_t** client, std::unique_ptr<HTTPDownloader>* http)
434433
{
435-
*http = Common::HTTPDownloader::Create(GetUserAgent().c_str());
434+
*http = HTTPDownloader::Create(GetUserAgent().c_str());
436435
if (!*http)
437436
{
438437
Host::ReportErrorAsync("Achievements Error", "Failed to create HTTPDownloader, cannot use achievements");
@@ -459,7 +458,7 @@ bool Achievements::CreateClient(rc_client_t** client, std::unique_ptr<Common::HT
459458
return true;
460459
}
461460

462-
void Achievements::DestroyClient(rc_client_t** client, std::unique_ptr<Common::HTTPDownloader>* http)
461+
void Achievements::DestroyClient(rc_client_t** client, std::unique_ptr<HTTPDownloader>* http)
463462
{
464463
(*http)->WaitForAllRequests();
465464

@@ -613,20 +612,20 @@ uint32_t Achievements::ClientReadMemory(uint32_t address, uint8_t* buffer, uint3
613612
void Achievements::ClientServerCall(const rc_api_request_t* request, rc_client_server_callback_t callback,
614613
void* callback_data, rc_client_t* client)
615614
{
616-
Common::HTTPDownloader::Request::Callback hd_callback =
617-
[callback, callback_data](s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data) {
618-
rc_api_server_response_t rr;
619-
rr.http_status_code = (status_code <= 0) ? (status_code == Common::HTTPDownloader::HTTP_STATUS_CANCELLED ?
620-
RC_API_SERVER_RESPONSE_CLIENT_ERROR :
621-
RC_API_SERVER_RESPONSE_RETRYABLE_CLIENT_ERROR) :
622-
status_code;
623-
rr.body_length = data.size();
624-
rr.body = reinterpret_cast<const char*>(data.data());
625-
626-
callback(&rr, callback_data);
627-
};
615+
HTTPDownloader::Request::Callback hd_callback = [callback, callback_data](s32 status_code, std::string content_type,
616+
HTTPDownloader::Request::Data data) {
617+
rc_api_server_response_t rr;
618+
rr.http_status_code = (status_code <= 0) ? (status_code == HTTPDownloader::HTTP_STATUS_CANCELLED ?
619+
RC_API_SERVER_RESPONSE_CLIENT_ERROR :
620+
RC_API_SERVER_RESPONSE_RETRYABLE_CLIENT_ERROR) :
621+
status_code;
622+
rr.body_length = data.size();
623+
rr.body = reinterpret_cast<const char*>(data.data());
624+
625+
callback(&rr, callback_data);
626+
};
628627

629-
Common::HTTPDownloader* http = static_cast<Common::HTTPDownloader*>(rc_client_get_userdata(client));
628+
HTTPDownloader* http = static_cast<HTTPDownloader*>(rc_client_get_userdata(client));
630629

631630
// TODO: Content-type for post
632631
if (request->post_data)
@@ -1598,9 +1597,9 @@ bool Achievements::Login(const char* username, const char* password, Error* erro
15981597

15991598
// We need to use a temporary client if achievements aren't currently active.
16001599
rc_client_t* client = s_client;
1601-
Common::HTTPDownloader* http = s_http_downloader.get();
1600+
HTTPDownloader* http = s_http_downloader.get();
16021601
const bool is_temporary_client = (client == nullptr);
1603-
std::unique_ptr<Common::HTTPDownloader> temporary_downloader;
1602+
std::unique_ptr<HTTPDownloader> temporary_downloader;
16041603
ScopedGuard temporary_client_guard = [&client, is_temporary_client, &temporary_downloader]() {
16051604
if (is_temporary_client)
16061605
DestroyClient(&client, &temporary_downloader);

src/core/game_list.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
#include "system.h"
1010

1111
#include "util/cd_image.h"
12+
#include "util/http_downloader.h"
1213

1314
#include "common/assert.h"
1415
#include "common/byte_stream.h"
1516
#include "common/file_system.h"
1617
#include "common/heterogeneous_containers.h"
17-
#include "common/http_downloader.h"
1818
#include "common/log.h"
1919
#include "common/path.h"
2020
#include "common/progress_callback.h"
@@ -1116,15 +1116,14 @@ bool GameList::DownloadCovers(const std::vector<std::string>& url_templates, boo
11161116
{
11171117
std::string url(url_template);
11181118
if (has_title)
1119-
StringUtil::ReplaceAll(&url, "${title}", Common::HTTPDownloader::URLEncode(entry.title));
1119+
StringUtil::ReplaceAll(&url, "${title}", HTTPDownloader::URLEncode(entry.title));
11201120
if (has_file_title)
11211121
{
11221122
std::string display_name(FileSystem::GetDisplayNameFromPath(entry.path));
1123-
StringUtil::ReplaceAll(&url, "${filetitle}",
1124-
Common::HTTPDownloader::URLEncode(Path::GetFileTitle(display_name)));
1123+
StringUtil::ReplaceAll(&url, "${filetitle}", HTTPDownloader::URLEncode(Path::GetFileTitle(display_name)));
11251124
}
11261125
if (has_serial)
1127-
StringUtil::ReplaceAll(&url, "${serial}", Common::HTTPDownloader::URLEncode(entry.serial));
1126+
StringUtil::ReplaceAll(&url, "${serial}", HTTPDownloader::URLEncode(entry.serial));
11281127

11291128
download_urls.emplace_back(entry.path, std::move(url));
11301129
}
@@ -1136,7 +1135,7 @@ bool GameList::DownloadCovers(const std::vector<std::string>& url_templates, boo
11361135
return false;
11371136
}
11381137

1139-
std::unique_ptr<Common::HTTPDownloader> downloader(Common::HTTPDownloader::Create());
1138+
std::unique_ptr<HTTPDownloader> downloader(HTTPDownloader::Create());
11401139
if (!downloader)
11411140
{
11421141
progress->DisplayError("Failed to create HTTP downloader.");
@@ -1165,11 +1164,11 @@ bool GameList::DownloadCovers(const std::vector<std::string>& url_templates, boo
11651164
}
11661165

11671166
// we could actually do a few in parallel here...
1168-
std::string filename(Common::HTTPDownloader::URLDecode(url));
1167+
std::string filename(HTTPDownloader::URLDecode(url));
11691168
downloader->CreateRequest(
11701169
std::move(url), [use_serial, &save_callback, entry_path = std::move(entry_path), filename = std::move(filename)](
1171-
s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data) {
1172-
if (status_code != Common::HTTPDownloader::HTTP_STATUS_OK || data.empty())
1170+
s32 status_code, std::string content_type, HTTPDownloader::Request::Data data) {
1171+
if (status_code != HTTPDownloader::HTTP_STATUS_OK || data.empty())
11731172
return;
11741173

11751174
std::unique_lock lock(s_mutex);
@@ -1180,7 +1179,7 @@ bool GameList::DownloadCovers(const std::vector<std::string>& url_templates, boo
11801179
// prefer the content type from the response for the extension
11811180
// otherwise, if it's missing, and the request didn't have an extension.. fall back to jpegs.
11821181
std::string template_filename;
1183-
std::string content_type_extension(Common::HTTPDownloader::GetExtensionForContentType(content_type));
1182+
std::string content_type_extension(HTTPDownloader::GetExtensionForContentType(content_type));
11841183

11851184
// don't treat the domain name as an extension..
11861185
const std::string::size_type last_slash = filename.find('/');

src/util/CMakeLists.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ add_library(util
2929
gpu_texture.h
3030
host.cpp
3131
host.h
32+
http_downloader.cpp
33+
http_downloader.h
3234
imgui_fullscreen.cpp
3335
imgui_fullscreen.h
3436
imgui_manager.cpp
@@ -234,6 +236,8 @@ if(WIN32)
234236
d3d12_texture.h
235237
dinput_source.cpp
236238
dinput_source.h
239+
http_downloader_winhttp.cpp
240+
http_downloader_winhttp.h
237241
platform_misc_win32.cpp
238242
win32_raw_input_source.cpp
239243
win32_raw_input_source.h
@@ -243,7 +247,7 @@ if(WIN32)
243247
xinput_source.h
244248
)
245249
target_link_libraries(util PRIVATE d3d12ma)
246-
target_link_libraries(util PRIVATE d3d11.lib d3d12.lib d3dcompiler.lib dxgi.lib winmm.lib Dwmapi.lib)
250+
target_link_libraries(util PRIVATE d3d11.lib d3d12.lib d3dcompiler.lib dxgi.lib winmm.lib Dwmapi.lib winhttp.lib)
247251

248252
if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
249253
target_link_libraries(util PRIVATE WinPixEventRuntime::WinPixEventRuntime)
@@ -279,6 +283,16 @@ elseif(NOT ANDROID)
279283
target_link_libraries(util PRIVATE ${DBUS_LINK_LIBRARIES})
280284
endif()
281285

286+
if(NOT WIN32 AND NOT ANDROID)
287+
target_sources(common PRIVATE
288+
http_downloader_curl.cpp
289+
http_downloader_curl.h
290+
)
291+
target_link_libraries(common PRIVATE
292+
CURL::libcurl
293+
)
294+
endif()
295+
282296
function(add_util_resources target)
283297
if(APPLE)
284298
get_property(UTIL_METAL_SOURCES GLOBAL PROPERTY UTIL_METAL_SOURCES)

src/common/http_downloader.cpp renamed to src/util/http_downloader.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <[email protected]>
1+
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <[email protected]>
22
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
33

44
#include "http_downloader.h"
5-
#include "assert.h"
6-
#include "log.h"
7-
#include "string_util.h"
8-
#include "timer.h"
5+
6+
#include "common/assert.h"
7+
#include "common/log.h"
8+
#include "common/string_util.h"
9+
#include "common/timer.h"
10+
911
Log_SetChannel(HTTPDownloader);
1012

1113
static constexpr float DEFAULT_TIMEOUT_IN_SECONDS = 30;
1214
static constexpr u32 DEFAULT_MAX_ACTIVE_REQUESTS = 4;
1315

14-
namespace Common {
15-
1616
const char HTTPDownloader::DEFAULT_USER_AGENT[] =
1717
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0";
1818

@@ -355,5 +355,3 @@ std::string HTTPDownloader::GetExtensionForContentType(const std::string& conten
355355
}
356356
return ret;
357357
}
358-
359-
} // namespace Common

src/common/http_downloader.h renamed to src/util/http_downloader.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <[email protected]>
1+
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <[email protected]>
22
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
33

44
#pragma once
5+
56
#include "common/types.h"
7+
68
#include <atomic>
79
#include <functional>
810
#include <memory>
@@ -11,8 +13,6 @@
1113
#include <string_view>
1214
#include <vector>
1315

14-
namespace Common {
15-
1616
class HTTPDownloader
1717
{
1818
public:
@@ -94,5 +94,3 @@ class HTTPDownloader
9494
std::mutex m_pending_http_request_lock;
9595
std::vector<Request*> m_pending_http_requests;
9696
};
97-
98-
} // namespace FrontendCommon

0 commit comments

Comments
 (0)