Skip to content

Commit fd9251f

Browse files
authored
Remove curl from windows (#105)
* Implement * Fix * Lint
1 parent 4b21f8a commit fd9251f

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

CMakeLists.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE OpenCV)
7272

7373
if(APPLE)
7474

75+
elseif(MSVC)
76+
add_subdirectory(src/TextRecognizer/WinRTTextRecognizer)
77+
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE WinRTTextRecognizer)
78+
79+
add_subdirectory(src/UpdateChecker/WinRTHttpClient)
80+
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE WinRTHttpClient)
7581
else()
7682
include(cmake/BuildMyCurl.cmake)
7783
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE libcurl)
@@ -80,9 +86,4 @@ endif()
8086
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/vendor/nlohmann-json/include)
8187
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/vendor/nameof/include)
8288

83-
if(MSVC)
84-
add_subdirectory(src/TextRecognizer/WinRTTextRecognizer)
85-
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE WinRTTextRecognizer)
86-
endif()
87-
8889
set_target_properties_plugin(${CMAKE_PROJECT_NAME} PROPERTIES OUTPUT_NAME ${_name})

src/UpdateChecker/GitHubClient.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include "plugin-support.h"
1111

1212
#ifdef __APPLE__
13+
void fetchStringFromUrl(const char *urlString,
14+
std::function<void(std::string, int)> callback);
15+
#elif defined(_WIN32)
1316
void fetchStringFromUrl(const char *urlString,
1417
std::function<void(std::string, int)> callback);
1518
#else
@@ -48,7 +51,8 @@ class GitHubClient {
4851
obs_data_create_from_json(responseBody.c_str());
4952
if (!data) {
5053
obs_log(LOG_INFO,
51-
"Failed to parse the latest release info!");
54+
"Failed to parse the latest release info! %s",
55+
responseBody.c_str());
5256
callback({"", "", true});
5357
return;
5458
}
@@ -90,6 +94,8 @@ class GitHubClient {
9094
{
9195
#ifdef __APPLE__
9296
fetchStringFromUrl(url, callback);
97+
#elif defined(_WIN32)
98+
fetchStringFromUrl(url, callback);
9399
#else
94100
CURL *curl = curl_easy_init();
95101
if (!curl) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
add_library(WinRTHttpClient WinRTHttpClient.cpp)
2+
set_target_properties(
3+
WinRTHttpClient
4+
PROPERTIES CXX_STANDARD 17
5+
CXX_STANDARD_REQUIRED ON
6+
CXX_EXTENSIONS OFF
7+
VS_GLOBAL_CppWinRTOptimized true
8+
VS_GLOBAL_CppWinRTRootNamespaceAutoMerge true)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <functional>
2+
3+
#include <winrt/Windows.Foundation.h>
4+
#include <winrt/Windows.Web.Http.Headers.h>
5+
#include <winrt/windows.storage.streams.h>
6+
7+
using winrt::Windows::Foundation::Uri;
8+
using winrt::Windows::Storage::Streams::IBuffer;
9+
using winrt::Windows::Web::Http::HttpClient;
10+
using winrt::Windows::Web::Http::HttpResponseMessage;
11+
using winrt::Windows::Web::Http::IHttpContent;
12+
13+
void fetchStringFromUrl(const char *urlString,
14+
std::function<void(std::string, int)> callback)
15+
{
16+
HttpClient httpClient;
17+
auto headers(httpClient.DefaultRequestHeaders());
18+
headers.UserAgent().TryParseAdd(L"obs-pokemon-sv-screen-builder/0.1.1");
19+
Uri requestUri(winrt::to_hstring(urlString));
20+
HttpResponseMessage httpResponseMessage;
21+
IBuffer httpResponseBuffer;
22+
try {
23+
httpResponseMessage = httpClient.GetAsync(requestUri).get();
24+
httpResponseMessage.EnsureSuccessStatusCode();
25+
IHttpContent httpContent = httpResponseMessage.Content();
26+
httpResponseBuffer = httpContent.ReadAsBufferAsync().get();
27+
28+
uint8_t *data = httpResponseBuffer.data();
29+
std::string str((const char *)data,
30+
httpResponseBuffer.Length());
31+
callback(str, 0);
32+
} catch (winrt::hresult_error const &ex) {
33+
std::string str(winrt::to_string(ex.message()));
34+
callback(str, 0);
35+
}
36+
}

0 commit comments

Comments
 (0)