Use curl
This commit is contained in:
@ -138,21 +138,37 @@ namespace discord
|
||||
{
|
||||
const auto data = utils::http::get_data(
|
||||
utils::string::va(AVATAR_URL, id.data(), avatar.data()));
|
||||
if (data.has_value())
|
||||
if (!data.has_value())
|
||||
{
|
||||
materials::add(utils::string::va(AVATAR, id.data()), data.value());
|
||||
return;
|
||||
}
|
||||
|
||||
const auto& value = data.value();
|
||||
if (value.code != CURLE_OK)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
materials::add(utils::string::va(AVATAR, id.data()), value.buffer);
|
||||
}
|
||||
|
||||
bool has_default_avatar = false;
|
||||
void download_default_avatar()
|
||||
{
|
||||
const auto data = utils::http::get_data(DEFAULT_AVATAR_URL);
|
||||
if (data.has_value())
|
||||
if (!data.has_value())
|
||||
{
|
||||
has_default_avatar = true;
|
||||
materials::add(DEFAULT_AVATAR, data.value());
|
||||
return;
|
||||
}
|
||||
|
||||
const auto& value = data.value();
|
||||
if (value.code != CURLE_OK)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
has_default_avatar = true;
|
||||
materials::add(DEFAULT_AVATAR, value.buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -375,6 +375,23 @@ namespace server_list
|
||||
insert_server(std::move(server));
|
||||
}
|
||||
|
||||
int get_player_count()
|
||||
{
|
||||
std::lock_guard<std::mutex> _(mutex);
|
||||
auto count = 0;
|
||||
for (const auto& server : servers)
|
||||
{
|
||||
count += server.clients - server.bots;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int get_server_count()
|
||||
{
|
||||
std::lock_guard<std::mutex> _(mutex);
|
||||
return static_cast<int>(servers.size());
|
||||
}
|
||||
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
|
@ -26,9 +26,9 @@
|
||||
#define DATA_PATH "data/"
|
||||
#define DATA_PATH_DEV "data-dev/"
|
||||
|
||||
#define ERR_UPDATE_CHECK_FAIL "Failed to check for updates"
|
||||
#define ERR_UPDATE_CHECK_FAIL "Failed to check for updates:\n%s"
|
||||
#define ERR_UPDATE_CHECK_FAIL_BAD_RESPONSE "Bad response"
|
||||
#define ERR_DOWNLOAD_FAIL "Failed to download file "
|
||||
#define ERR_DOWNLOAD_FAIL "Failed to download file %s:\n%s"
|
||||
#define ERR_WRITE_FAIL "Failed to write file "
|
||||
|
||||
#define BINARY_NAME "h1-mod.exe"
|
||||
@ -139,7 +139,7 @@ namespace updater
|
||||
return utils::string::va("%i", uint32_t(time(nullptr)));
|
||||
}
|
||||
|
||||
std::optional<std::string> download_file(const std::string& name)
|
||||
std::optional<utils::http::result> download_file(const std::string& name)
|
||||
{
|
||||
return utils::http::get_data(MASTER + select(DATA_PATH, DATA_PATH_DEV) + name + "?" + get_time_str());
|
||||
}
|
||||
@ -191,6 +191,12 @@ namespace updater
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string curl_error(CURLcode code)
|
||||
{
|
||||
const auto str_error = curl_easy_strerror(code);
|
||||
return utils::string::va("%s (%i)", str_error, code);
|
||||
}
|
||||
}
|
||||
|
||||
// workaround
|
||||
@ -337,12 +343,20 @@ namespace updater
|
||||
|
||||
if (!files_data.has_value())
|
||||
{
|
||||
set_update_check_status(true, false, ERR_UPDATE_CHECK_FAIL);
|
||||
set_update_check_status(true, false, utils::string::va(ERR_UPDATE_CHECK_FAIL, "Unknown error"));
|
||||
return;
|
||||
}
|
||||
|
||||
const auto& value = files_data.value();
|
||||
if (value.code != CURLE_OK)
|
||||
{
|
||||
const auto error = curl_error(value.code);
|
||||
set_update_check_status(true, false, utils::string::va(ERR_UPDATE_CHECK_FAIL, error.data()));
|
||||
return;
|
||||
}
|
||||
|
||||
rapidjson::Document j;
|
||||
j.Parse(files_data.value().data());
|
||||
j.Parse(value.buffer.data());
|
||||
|
||||
if (!j.IsArray())
|
||||
{
|
||||
@ -433,11 +447,19 @@ namespace updater
|
||||
|
||||
if (!data.has_value())
|
||||
{
|
||||
set_update_download_status(true, false, ERR_DOWNLOAD_FAIL + file);
|
||||
set_update_download_status(true, false, utils::string::va(ERR_DOWNLOAD_FAIL, file.data(), "Unknown error"));
|
||||
return;
|
||||
}
|
||||
|
||||
downloads.push_back({file, data.value()});
|
||||
const auto& value = data.value();
|
||||
if (value.code != CURLE_OK)
|
||||
{
|
||||
const auto error = curl_error(value.code);
|
||||
set_update_download_status(true, false, utils::string::va(ERR_DOWNLOAD_FAIL, file.data(), error.data()));
|
||||
return;
|
||||
}
|
||||
|
||||
downloads.push_back({file, value.buffer});
|
||||
}
|
||||
|
||||
for (const auto& download : downloads)
|
||||
|
Reference in New Issue
Block a user