|
1 | 1 | // operating system level utilities |
2 | 2 | // contains directory utils, http utils, and helper methods |
3 | 3 |
|
| 4 | +// CURLOPT_PROGRESSFUNCTION deprecated in 7.32.0 and is replaced with CURLOPT_XFERINFOFUNCTION |
| 5 | +#include <curl/curl.h> |
| 6 | +#if LIBCURL_VERSION_NUM >= 0x072000 |
| 7 | + #define COMPAT_CURL_PROGRESS_OPTION CURLOPT_XFERINFOFUNCTION |
| 8 | +#else |
| 9 | + #define COMPAT_CURL_PROGRESS_OPTION CURLOPT_PROGRESSFUNCTION |
| 10 | +#endif |
| 11 | + |
4 | 12 | #if defined(WII) && !defined(NETWORK_MOCK) |
5 | 13 | #include <wiisocket.h> |
6 | 14 | #endif |
|
48 | 56 |
|
49 | 57 | #define BUF_SIZE 0x800000 // 8MB. |
50 | 58 |
|
51 | | -int (*networking_callback)(void*, double, double, double, double); |
52 | | -int (*libget_status_callback)(int, int, int); |
| 59 | +libget_progress_callback_t networking_callback = nullptr; |
| 60 | +int (*libget_status_callback)(int, int, int) = nullptr; |
53 | 61 | void* networking_callback_data = nullptr; // User data to pass to callback |
54 | 62 |
|
55 | 63 | static const char* USER_AGENT = "libget-unknown/0.0.0"; |
56 | 64 |
|
| 65 | +// different signature depending on curl version |
| 66 | +#ifndef NETWORK_MOCK |
| 67 | +#if LIBCURL_VERSION_NUM >= 0x072000 |
| 68 | +static int libget_curl_progress_wrapper(void* /*clientp*/, curl_off_t dltotal, curl_off_t dlnow, curl_off_t, curl_off_t) |
| 69 | +#else |
| 70 | +static int libget_curl_progress_wrapper(void* /*clientp*/, double dltotal, double dlnow, double, double) |
| 71 | +#endif |
| 72 | +{ |
| 73 | + if (networking_callback == nullptr) |
| 74 | + return 0; |
| 75 | + |
| 76 | + if (dltotal == 0) |
| 77 | + dltotal = 1; |
| 78 | + |
| 79 | + double progress = (double)dlnow / (double)dltotal; |
| 80 | + return networking_callback(networking_callback_data, progress); |
| 81 | +} |
| 82 | +#endif |
| 83 | + |
57 | 84 | // reference to the curl handle so that we can re-use the connection |
58 | 85 | #ifndef NETWORK_MOCK |
59 | 86 | CURL* curl = nullptr; |
@@ -181,7 +208,7 @@ void resetCurlToCleanState(CURL* c) |
181 | 208 | if (!c) return; |
182 | 209 |
|
183 | 210 | // clear callbacks which might reference freed objects |
184 | | - curl_easy_setopt(c, CURLOPT_PROGRESSFUNCTION, nullptr); |
| 211 | + curl_easy_setopt(c, COMPAT_CURL_PROGRESS_OPTION, nullptr); |
185 | 212 | curl_easy_setopt(c, CURLOPT_PROGRESSDATA, nullptr); |
186 | 213 | curl_easy_setopt(c, CURLOPT_NOPROGRESS, 1); |
187 | 214 |
|
@@ -274,12 +301,12 @@ bool downloadFileCommon(const std::string& path, std::string* buffer = nullptr, |
274 | 301 |
|
275 | 302 | if (networking_callback != nullptr) { |
276 | 303 | printf("[downloadFileCommon] Setting progress callback (networking_callback=%p)\n", networking_callback); |
277 | | - curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, networking_callback); |
278 | | - curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, networking_callback_data); |
| 304 | + curl_easy_setopt(curl, COMPAT_CURL_PROGRESS_OPTION, libget_curl_progress_wrapper); |
| 305 | + curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, curl); |
279 | 306 | curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0); |
280 | 307 | } else { |
281 | 308 | printf("[downloadFileCommon] Disabling progress callbacks (networking_callback is nullptr)\n"); |
282 | | - curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, nullptr); |
| 309 | + curl_easy_setopt(curl, COMPAT_CURL_PROGRESS_OPTION, nullptr); |
283 | 310 | curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, nullptr); |
284 | 311 | curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); |
285 | 312 | } |
|
0 commit comments