iw4x-client/src/Utils/Cache.cpp

43 lines
859 B
C++
Raw Normal View History

2022-02-27 12:53:44 +00:00
#include <STDInclude.hpp>
2023-01-28 23:38:24 +00:00
#include "WebIO.hpp"
2016-11-21 10:50:55 +01:00
namespace Utils
{
const char* Cache::Urls[] =
2016-11-21 10:50:55 +01:00
{
2022-11-23 15:31:48 +00:00
"https://raw.githubusercontent.com/XLabsProject/iw4x-client",
2020-12-09 14:55:28 -06:00
"https://xlabs.dev",
2016-11-21 10:50:55 +01:00
};
2022-07-21 21:30:44 +02:00
std::string Cache::ValidUrl;
2016-11-21 11:09:42 +01:00
std::mutex Cache::CacheMutex;
2016-11-21 10:50:55 +01:00
2018-12-17 14:29:18 +01:00
std::string Cache::GetUrl(const std::string& url, const std::string& path)
2016-11-21 10:50:55 +01:00
{
return url + path;
}
2018-12-17 14:29:18 +01:00
std::string Cache::GetFile(const std::string& path, int timeout, const std::string& useragent)
2016-11-21 10:50:55 +01:00
{
2022-11-23 15:31:48 +00:00
std::lock_guard _(CacheMutex);
2022-11-23 15:31:48 +00:00
if (ValidUrl.empty())
2016-11-21 10:50:55 +01:00
{
2022-11-23 15:31:48 +00:00
for (std::size_t i = 0; i < ARRAYSIZE(Urls); ++i)
2016-11-21 10:50:55 +01:00
{
2022-11-23 15:31:48 +00:00
std::string result = WebIO(useragent, GetUrl(Urls[i], path)).setTimeout(timeout)->get();
2016-11-21 11:09:42 +01:00
if (!result.empty())
{
2022-11-23 15:31:48 +00:00
ValidUrl = Urls[i];
2016-11-21 11:09:42 +01:00
return result;
}
2016-11-21 10:50:55 +01:00
}
2022-11-23 15:31:48 +00:00
return {};
2016-11-21 11:09:42 +01:00
}
2022-11-23 15:31:48 +00:00
return WebIO(useragent, GetUrl(ValidUrl, path)).setTimeout(timeout)->get();
2016-11-21 10:50:55 +01:00
}
}