-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownload.cpp
More file actions
177 lines (142 loc) · 4.75 KB
/
Download.cpp
File metadata and controls
177 lines (142 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "Download.h"
#include <SDL3/SDL.h>
#include <filesystem>
#include <fstream>
#include <string>
#include <curl/curl.h>
CURL* easy_handle = nullptr;
CURLM* multi_handle = curl_multi_init();
FILE* file = nullptr;
std::vector<std::string> urls_to_download;
std::string last_url = "";
std::function<void()> download_callback = nullptr;
void write_data(void* ptr, size_t size, size_t nmemb, FILE* stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
if (written != size * nmemb) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to write data to file");
}
}
void downloadList(std::string url) {
if (!easy_handle) {
easy_handle = curl_easy_init();
if (!easy_handle) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "curl_easy_init failed");
}
}
curl_easy_setopt(easy_handle, CURLOPT_URL, url.c_str());
curl_easy_setopt(easy_handle, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data);
long max_file_size_bytes = 25 * 1024 * 1024L; // 25 MB
curl_easy_setopt(easy_handle, CURLOPT_MAXFILESIZE, max_file_size_bytes);
std::string filename = "creatures/" + url.substr(url.find_last_of("/") + 1);
SDL_Log("Downloading %s to %s", url.c_str(), filename.c_str());
// create directory if it doesn't exist
std::filesystem::create_directories("creatures");
file = fopen(filename.c_str(), "wb");
if (!file) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to open file %s for writing",
filename.c_str());
return;
}
curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, file);
}
void downloadCreatures(std::function<void()> callback) {
if (!multi_handle) { // so make it god damn it >:(
multi_handle = curl_multi_init();
if (!multi_handle) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "curl_multi_init failed");
if (callback) callback();
return;
}
}
easy_handle = curl_easy_init();
if (!easy_handle) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "curl_easy_init failed");
curl_multi_cleanup(multi_handle);
if (callback) callback();
return;
}
// Grab the list of creatures to download
std::ifstream infile("creature_mirrors.txt");
if (!infile.is_open()) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to open creature_mirrors.txt");
curl_easy_cleanup(easy_handle);
curl_multi_cleanup(multi_handle);
if (callback) callback();
return;
}
download_callback = callback;
// line by line, download each URL to the creatures/ directory
std::string url;
while (std::getline(infile, url)) {
urls_to_download.push_back(url);
}
infile.close();
if (urls_to_download.empty()) {
SDL_Log("No URLs to download");
curl_easy_cleanup(easy_handle);
curl_multi_cleanup(multi_handle);
if (callback) callback();
return;
}
last_url = urls_to_download.back();
downloadList(last_url);
urls_to_download.pop_back();
curl_multi_add_handle(multi_handle, easy_handle);
}
curlStatus downloadCallbacks() {
int still_running;
CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
curlStatus status;
// Get current downloaded size
curl_off_t total_downloaded = 0;
curl_off_t total_size = 0;
curl_easy_getinfo(easy_handle, CURLINFO_SIZE_DOWNLOAD_T, &total_downloaded);
curl_easy_getinfo(easy_handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T,
&total_size);
status.downloaded = static_cast<size_t>(total_downloaded);
status.total = static_cast<size_t>(total_size);
if (mc != CURLM_OK) {
SDL_Log("curl_multi_perform() failed, code %d.", mc);
// Cleanup, retry
if (file) {
fclose(file);
file = nullptr;
}
curl_multi_remove_handle(multi_handle, easy_handle);
curl_easy_cleanup(easy_handle);
downloadList(last_url);
curl_multi_add_handle(multi_handle, easy_handle);
return status;
}
if (!still_running) {
SDL_Log("Downloaded %s, total %zu bytes", last_url.c_str(),
static_cast<size_t>(total_downloaded));
if (file) {
fclose(file);
file = nullptr;
}
if (!urls_to_download.empty()) {
// Start next download
last_url = urls_to_download.back();
urls_to_download.pop_back();
SDL_Log("Starting next download, %zu remaining", urls_to_download.size());
curl_multi_remove_handle(multi_handle, easy_handle);
curl_easy_cleanup(easy_handle);
easy_handle = nullptr;
downloadList(last_url);
curl_multi_add_handle(multi_handle, easy_handle);
return status;
}
curl_multi_remove_handle(multi_handle, easy_handle);
curl_easy_cleanup(easy_handle);
easy_handle = nullptr;
curl_multi_cleanup(multi_handle);
multi_handle = nullptr;
if (download_callback) {
download_callback();
download_callback = nullptr;
}
}
return status;
}