GameList: Handle non-extension-suffixed urls based on content type

This commit is contained in:
Connor McLaughlin
2022-09-18 13:00:55 +10:00
parent cc0127d5ed
commit d9722516c3
9 changed files with 211 additions and 36 deletions

View File

@ -67,26 +67,29 @@ static Achievement* GetMutableAchievementByID(u32 id);
static void ClearGameInfo(bool clear_achievements = true, bool clear_leaderboards = true);
static void ClearGameHash();
static std::string GetUserAgent();
static void LoginCallback(s32 status_code, Common::HTTPDownloader::Request::Data data);
static void LoginASyncCallback(s32 status_code, Common::HTTPDownloader::Request::Data data);
static void LoginCallback(s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data);
static void LoginASyncCallback(s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data);
static void SendLogin(const char* username, const char* password, Common::HTTPDownloader* http_downloader,
Common::HTTPDownloader::Request::Callback callback);
static void DownloadImage(std::string url, std::string cache_filename);
static void DisplayAchievementSummary();
static void GetUserUnlocksCallback(s32 status_code, Common::HTTPDownloader::Request::Data data);
static void GetUserUnlocksCallback(s32 status_code, std::string content_type,
Common::HTTPDownloader::Request::Data data);
static void GetUserUnlocks();
static void GetPatchesCallback(s32 status_code, Common::HTTPDownloader::Request::Data data);
static void GetLbInfoCallback(s32 status_code, Common::HTTPDownloader::Request::Data data);
static void GetPatchesCallback(s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data);
static void GetLbInfoCallback(s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data);
static void GetPatches(u32 game_id);
static std::string GetGameHash(CDImage* image);
static void SetChallengeMode(bool enabled);
static void SendGetGameId();
static void GetGameIdCallback(s32 status_code, Common::HTTPDownloader::Request::Data data);
static void SendPlayingCallback(s32 status_code, Common::HTTPDownloader::Request::Data data);
static void GetGameIdCallback(s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data);
static void SendPlayingCallback(s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data);
static void UpdateRichPresence();
static void SendPingCallback(s32 status_code, Common::HTTPDownloader::Request::Data data);
static void UnlockAchievementCallback(s32 status_code, Common::HTTPDownloader::Request::Data data);
static void SubmitLeaderboardCallback(s32 status_code, Common::HTTPDownloader::Request::Data data);
static void SendPingCallback(s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data);
static void UnlockAchievementCallback(s32 status_code, std::string content_type,
Common::HTTPDownloader::Request::Data data);
static void SubmitLeaderboardCallback(s32 status_code, std::string content_type,
Common::HTTPDownloader::Request::Data data);
static bool s_active = false;
static bool s_logged_in = false;
@ -177,7 +180,7 @@ public:
if (error != RC_OK)
{
FormattedError("%s failed: error %d (%s)", RAPIStructName<T>(), error, rc_error_str(error));
callback(-1, Common::HTTPDownloader::Request::Data());
callback(-1, std::string(), Common::HTTPDownloader::Request::Data());
return;
}
@ -825,7 +828,7 @@ void Achievements::EnsureCacheDirectoriesExist()
}
}
void Achievements::LoginCallback(s32 status_code, Common::HTTPDownloader::Request::Data data)
void Achievements::LoginCallback(s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data)
{
std::unique_lock lock(s_achievements_mutex);
@ -858,11 +861,12 @@ void Achievements::LoginCallback(s32 status_code, Common::HTTPDownloader::Reques
}
}
void Achievements::LoginASyncCallback(s32 status_code, Common::HTTPDownloader::Request::Data data)
void Achievements::LoginASyncCallback(s32 status_code, std::string content_type,
Common::HTTPDownloader::Request::Data data)
{
ImGuiFullscreen::CloseBackgroundProgressDialog("cheevos_async_login");
LoginCallback(status_code, std::move(data));
LoginCallback(status_code, std::move(content_type), std::move(data));
}
void Achievements::SendLogin(const char* username, const char* password, Common::HTTPDownloader* http_downloader,
@ -943,7 +947,8 @@ void Achievements::Logout()
void Achievements::DownloadImage(std::string url, std::string cache_filename)
{
auto callback = [cache_filename](s32 status_code, Common::HTTPDownloader::Request::Data data) {
auto callback = [cache_filename](s32 status_code, std::string content_type,
Common::HTTPDownloader::Request::Data data) {
if (status_code != HTTP_OK)
return;
@ -998,7 +1003,8 @@ void Achievements::DisplayAchievementSummary()
});
}
void Achievements::GetUserUnlocksCallback(s32 status_code, Common::HTTPDownloader::Request::Data data)
void Achievements::GetUserUnlocksCallback(s32 status_code, std::string content_type,
Common::HTTPDownloader::Request::Data data)
{
if (!System::IsValid())
return;
@ -1046,7 +1052,8 @@ void Achievements::GetUserUnlocks()
request.Send(GetUserUnlocksCallback);
}
void Achievements::GetPatchesCallback(s32 status_code, Common::HTTPDownloader::Request::Data data)
void Achievements::GetPatchesCallback(s32 status_code, std::string content_type,
Common::HTTPDownloader::Request::Data data)
{
if (!System::IsValid())
return;
@ -1185,7 +1192,8 @@ void Achievements::GetPatchesCallback(s32 status_code, Common::HTTPDownloader::R
}
}
void Achievements::GetLbInfoCallback(s32 status_code, Common::HTTPDownloader::Request::Data data)
void Achievements::GetLbInfoCallback(s32 status_code, std::string content_type,
Common::HTTPDownloader::Request::Data data)
{
if (!System::IsValid())
return;
@ -1275,7 +1283,8 @@ std::string Achievements::GetGameHash(CDImage* image)
return hash_str;
}
void Achievements::GetGameIdCallback(s32 status_code, Common::HTTPDownloader::Request::Data data)
void Achievements::GetGameIdCallback(s32 status_code, std::string content_type,
Common::HTTPDownloader::Request::Data data)
{
if (!System::IsValid())
return;
@ -1393,7 +1402,8 @@ void Achievements::SendGetGameId()
request.Send(GetGameIdCallback);
}
void Achievements::SendPlayingCallback(s32 status_code, Common::HTTPDownloader::Request::Data data)
void Achievements::SendPlayingCallback(s32 status_code, std::string content_type,
Common::HTTPDownloader::Request::Data data)
{
if (!System::IsValid())
return;
@ -1445,7 +1455,8 @@ void Achievements::UpdateRichPresence()
Host::OnAchievementsRefreshed();
}
void Achievements::SendPingCallback(s32 status_code, Common::HTTPDownloader::Request::Data data)
void Achievements::SendPingCallback(s32 status_code, std::string content_type,
Common::HTTPDownloader::Request::Data data)
{
if (!System::IsValid())
return;
@ -1634,7 +1645,8 @@ void Achievements::DeactivateAchievement(Achievement* achievement)
Log_DevPrintf("Deactivated achievement %s (%u)", achievement->title.c_str(), achievement->id);
}
void Achievements::UnlockAchievementCallback(s32 status_code, Common::HTTPDownloader::Request::Data data)
void Achievements::UnlockAchievementCallback(s32 status_code, std::string content_type,
Common::HTTPDownloader::Request::Data data)
{
if (!System::IsValid())
return;
@ -1649,7 +1661,8 @@ void Achievements::UnlockAchievementCallback(s32 status_code, Common::HTTPDownlo
response.new_player_score);
}
void Achievements::SubmitLeaderboardCallback(s32 status_code, Common::HTTPDownloader::Request::Data data)
void Achievements::SubmitLeaderboardCallback(s32 status_code, std::string content_type,
Common::HTTPDownloader::Request::Data data)
{
if (!System::IsValid())
return;

View File

@ -779,7 +779,7 @@ bool GameList::DownloadCovers(const std::vector<std::string>& url_templates, boo
std::string filename(Common::HTTPDownloader::URLDecode(url));
downloader->CreateRequest(
std::move(url), [use_serial, &save_callback, entry_path = std::move(entry_path),
filename = std::move(filename)](s32 status_code, Common::HTTPDownloader::Request::Data data) {
filename = std::move(filename)](s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data) {
if (status_code != Common::HTTPDownloader::HTTP_OK || data.empty())
return;
@ -788,12 +788,26 @@ bool GameList::DownloadCovers(const std::vector<std::string>& url_templates, boo
if (!entry || !GetCoverImagePathForEntry(entry).empty())
return;
std::string write_path(GetNewCoverImagePathForEntry(entry, filename.c_str(), use_serial));
// prefer the content type from the response for the extension
// otherwise, if it's missing, and the request didn't have an extension.. fall back to jpegs.
std::string template_filename;
std::string content_type_extension(Common::HTTPDownloader::GetExtensionForContentType(content_type));
// don't treat the domain name as an extension..
const std::string::size_type last_slash = filename.find('/');
const std::string::size_type last_dot = filename.find('.');
if (!content_type_extension.empty())
template_filename = fmt::format("cover.{}", content_type_extension);
else if (last_slash != std::string::npos && last_dot != std::string::npos && last_dot > last_slash)
template_filename = Path::GetFileName(filename);
else
template_filename = "cover.jpg";
std::string write_path(GetNewCoverImagePathForEntry(entry, template_filename.c_str(), use_serial));
if (write_path.empty())
return;
FileSystem::WriteBinaryFile(write_path.c_str(), data.data(), data.size());
if (save_callback)
if (FileSystem::WriteBinaryFile(write_path.c_str(), data.data(), data.size()) && save_callback)
save_callback(entry, std::move(write_path));
});
downloader->WaitForAllRequests();