From b193374dd4a8625bcc631436e0c8c39851275d9f Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Wed, 23 Sep 2020 23:43:32 +1000 Subject: [PATCH] GameList: Add method to look up a cover image --- src/frontend-common/common_host_interface.cpp | 1 + src/frontend-common/game_list.cpp | 37 +++++++++++++++++++ src/frontend-common/game_list.h | 2 + 3 files changed, 40 insertions(+) diff --git a/src/frontend-common/common_host_interface.cpp b/src/frontend-common/common_host_interface.cpp index 2a9e15e71..cbea0e300 100644 --- a/src/frontend-common/common_host_interface.cpp +++ b/src/frontend-common/common_host_interface.cpp @@ -118,6 +118,7 @@ void CommonHostInterface::InitializeUserDirectory() result &= FileSystem::CreateDirectory(GetUserDirectoryRelativePath("bios").c_str(), false); result &= FileSystem::CreateDirectory(GetUserDirectoryRelativePath("cache").c_str(), false); result &= FileSystem::CreateDirectory(GetUserDirectoryRelativePath("cheats").c_str(), false); + result &= FileSystem::CreateDirectory(GetUserDirectoryRelativePath("covers").c_str(), false); result &= FileSystem::CreateDirectory(GetUserDirectoryRelativePath("dump").c_str(), false); result &= FileSystem::CreateDirectory(GetUserDirectoryRelativePath("dump/audio").c_str(), false); result &= FileSystem::CreateDirectory(GetUserDirectoryRelativePath("inputprofiles").c_str(), false); diff --git a/src/frontend-common/game_list.cpp b/src/frontend-common/game_list.cpp index d2a3ff126..f1607a7fb 100644 --- a/src/frontend-common/game_list.cpp +++ b/src/frontend-common/game_list.cpp @@ -1109,3 +1109,40 @@ void GameList::UpdateGameSettings(const std::string& filename, const std::string save_to_user ? m_user_game_settings_filename.c_str() : m_game_settings_filename.c_str()); } } + +std::string GameList::GetCoverImagePathForEntry(const GameListEntry* entry) +{ + static constexpr std::array extensions = {{"jpg", "png"}}; + + PathString cover_path; + for (const char* extension : extensions) + { + // try the title + cover_path.Format("%s" FS_OSPATH_SEPARATOR_STR "covers" FS_OSPATH_SEPARATOR_STR "%s.%s", + g_host_interface->GetUserDirectory().c_str(), entry->title.c_str(), extension); + if (FileSystem::FileExists(cover_path)) + return std::string(cover_path.GetCharArray()); + + // then the code + cover_path.Format("%s" FS_OSPATH_SEPARATOR_STR "covers" FS_OSPATH_SEPARATOR_STR "%s.%s", + g_host_interface->GetUserDirectory().c_str(), entry->title.c_str(), extension); + if (FileSystem::FileExists(cover_path)) + return std::string(cover_path.GetCharArray()); + + // and the file title if it differs + const std::string_view file_title = GetFileNameFromPath(entry->path.c_str()); + if (!file_title.empty() && entry->title != file_title) + { + cover_path.Clear(); + cover_path.AppendString(g_host_interface->GetUserDirectory().c_str()); + cover_path.AppendCharacter(FS_OSPATH_SEPERATOR_CHARACTER); + cover_path.AppendString(file_title.data(), static_cast(file_title.size())); + cover_path.AppendCharacter('.'); + cover_path.AppendString(extension); + if (FileSystem::FileExists(cover_path)) + return std::string(cover_path.GetCharArray()); + } + } + + return std::string(); +} diff --git a/src/frontend-common/game_list.h b/src/frontend-common/game_list.h index 54955c0b7..999ffd77e 100644 --- a/src/frontend-common/game_list.h +++ b/src/frontend-common/game_list.h @@ -108,6 +108,8 @@ public: void UpdateGameSettings(const std::string& filename, const std::string& game_code, const std::string& game_title, const GameSettings::Entry& new_entry, bool save_to_list = true, bool save_to_user = true); + std::string GetCoverImagePathForEntry(const GameListEntry* entry); + private: enum : u32 {