diff options
| author | Léo Lam <leo@leolam.fr> | 2021-12-14 11:08:38 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-14 11:08:38 +0100 |
| commit | 185475fe03c758a2346c170bd9395227d78b0f63 (patch) | |
| tree | dc496e8bda35b3a9b1efb1382bdc20e3dac2f62f /Source/Core/UICommon/GameFile.cpp | |
| parent | 45fac5137b6cf3da1ee8ccc65eced6d969152a93 (diff) | |
| parent | b928900f6e1097a189cd78491df00ed7890949d6 (diff) | |
Merge pull request #10187 from AdmiralCurtiss/json-gamelist
Support for GameModDescriptor files in Game List.
Diffstat (limited to 'Source/Core/UICommon/GameFile.cpp')
| -rw-r--r-- | Source/Core/UICommon/GameFile.cpp | 126 |
1 files changed, 119 insertions, 7 deletions
diff --git a/Source/Core/UICommon/GameFile.cpp b/Source/Core/UICommon/GameFile.cpp index 367ea65642..c575d4018b 100644 --- a/Source/Core/UICommon/GameFile.cpp +++ b/Source/Core/UICommon/GameFile.cpp @@ -22,6 +22,7 @@ #include <mbedtls/sha1.h> #include <pugixml.hpp> +#include "Common/BitUtils.h" #include "Common/ChunkFile.h" #include "Common/CommonPaths.h" #include "Common/CommonTypes.h" @@ -43,6 +44,7 @@ #include "DiscIO/Blob.h" #include "DiscIO/DiscExtractor.h" #include "DiscIO/Enums.h" +#include "DiscIO/GameModDescriptor.h" #include "DiscIO/Volume.h" #include "DiscIO/WiiSaveBanner.h" @@ -163,6 +165,32 @@ GameFile::GameFile(std::string path) : m_file_path(std::move(path)) m_platform = DiscIO::Platform::ELFOrDOL; m_blob_type = DiscIO::BlobType::DIRECTORY; } + + if (!IsValid() && GetExtension() == ".json") + { + auto descriptor = DiscIO::ParseGameModDescriptorFile(m_file_path); + if (descriptor) + { + GameFile proxy(descriptor->base_file); + if (proxy.IsValid()) + { + m_valid = true; + m_file_size = File::GetSize(m_file_path); + m_long_names.emplace(DiscIO::Language::English, std::move(descriptor->display_name)); + m_internal_name = proxy.GetInternalName(); + m_game_id = proxy.GetGameID(); + m_gametdb_id = proxy.GetGameTDBID(); + m_title_id = proxy.GetTitleID(); + m_maker_id = proxy.GetMakerID(); + m_region = proxy.GetRegion(); + m_country = proxy.GetCountry(); + m_platform = proxy.GetPlatform(); + m_revision = proxy.GetRevision(); + m_disc_number = proxy.GetDiscNumber(); + m_blob_type = DiscIO::BlobType::MOD_DESCRIPTOR; + } + } + } } GameFile::~GameFile() = default; @@ -470,6 +498,18 @@ bool GameFile::ReadPNGBanner(const std::string& path) return true; } +bool GameFile::TryLoadGameModDescriptorBanner() +{ + if (m_blob_type != DiscIO::BlobType::MOD_DESCRIPTOR) + return false; + + auto descriptor = DiscIO::ParseGameModDescriptorFile(m_file_path); + if (!descriptor) + return false; + + return ReadPNGBanner(descriptor->banner); +} + bool GameFile::CustomBannerChanged() { std::string path, name; @@ -482,8 +522,12 @@ bool GameFile::CustomBannerChanged() // Homebrew Channel icon naming. Typical for DOLs and ELFs, but we also support it for volumes. if (!ReadPNGBanner(path + "icon.png")) { - // If no custom icon is found, go back to the non-custom one. - m_pending.custom_banner = {}; + // If it's a game mod descriptor file, it may specify its own custom banner. + if (!TryLoadGameModDescriptorBanner()) + { + // If no custom icon is found, go back to the non-custom one. + m_pending.custom_banner = {}; + } } } @@ -499,6 +543,8 @@ const std::string& GameFile::GetName(const Core::TitleDatabase& title_database) { if (!m_custom_name.empty()) return m_custom_name; + if (IsModDescriptor()) + return GetName(Variant::LongAndPossiblyCustom); const std::string& database_name = title_database.GetTitleName(m_gametdb_id, GetConfigLanguage()); return database_name.empty() ? GetName(Variant::LongAndPossiblyCustom) : database_name; @@ -579,15 +625,75 @@ std::string GameFile::GetNetPlayName(const Core::TitleDatabase& title_database) return name + " (" + ss.str() + ")"; } +static std::array<u8, 20> GetHash(u32 value) +{ + auto data = Common::BitCastToArray<u8>(value); + std::array<u8, 20> hash; + mbedtls_sha1_ret(reinterpret_cast<const unsigned char*>(data.data()), data.size(), hash.data()); + return hash; +} + +static std::array<u8, 20> GetHash(std::string_view str) +{ + std::array<u8, 20> hash; + mbedtls_sha1_ret(reinterpret_cast<const unsigned char*>(str.data()), str.size(), hash.data()); + return hash; +} + +static std::optional<std::array<u8, 20>> GetFileHash(const std::string& path) +{ + std::string buffer; + if (!File::ReadFileToString(path, buffer)) + return std::nullopt; + return GetHash(buffer); +} + +static std::optional<std::array<u8, 20>> MixHash(const std::optional<std::array<u8, 20>>& lhs, + const std::optional<std::array<u8, 20>>& rhs) +{ + if (!lhs && !rhs) + return std::nullopt; + if (!lhs || !rhs) + return !rhs ? lhs : rhs; + std::array<u8, 20> result; + for (size_t i = 0; i < result.size(); ++i) + result[i] = (*lhs)[i] ^ (*rhs)[(i + 1) % result.size()]; + return result; +} + std::array<u8, 20> GameFile::GetSyncHash() const { - std::array<u8, 20> hash{}; + std::optional<std::array<u8, 20>> hash; if (m_platform == DiscIO::Platform::ELFOrDOL) { - std::string buffer; - if (File::ReadFileToString(m_file_path, buffer)) - mbedtls_sha1_ret(reinterpret_cast<unsigned char*>(buffer.data()), buffer.size(), hash.data()); + hash = GetFileHash(m_file_path); + } + else if (m_blob_type == DiscIO::BlobType::MOD_DESCRIPTOR) + { + auto descriptor = DiscIO::ParseGameModDescriptorFile(m_file_path); + if (descriptor) + { + GameFile proxy(descriptor->base_file); + if (proxy.IsValid()) + hash = proxy.GetSyncHash(); + + // add patches to hash if they're enabled + if (descriptor->riivolution) + { + for (const auto& patch : descriptor->riivolution->patches) + { + hash = MixHash(hash, GetFileHash(patch.xml)); + for (const auto& option : patch.options) + { + hash = MixHash(hash, GetHash(option.section_name)); + hash = MixHash(hash, GetHash(option.option_id)); + hash = MixHash(hash, GetHash(option.option_name)); + hash = MixHash(hash, GetHash(option.choice)); + } + } + } + } } else { @@ -595,7 +701,7 @@ std::array<u8, 20> GameFile::GetSyncHash() const hash = volume->GetSyncHash(); } - return hash; + return hash.value_or(std::array<u8, 20>{}); } NetPlay::SyncIdentifier GameFile::GetSyncIdentifier() const @@ -652,6 +758,7 @@ bool GameFile::ShouldShowFileFormatDetails() const case DiscIO::BlobType::PLAIN: break; case DiscIO::BlobType::DRIVE: + case DiscIO::BlobType::MOD_DESCRIPTOR: return false; default: return true; @@ -699,6 +806,11 @@ bool GameFile::ShouldAllowConversion() const return DiscIO::IsDisc(m_platform) && m_volume_size_is_accurate; } +bool GameFile::IsModDescriptor() const +{ + return m_blob_type == DiscIO::BlobType::MOD_DESCRIPTOR; +} + const GameBanner& GameFile::GetBannerImage() const { return m_custom_banner.empty() ? m_volume_banner : m_custom_banner; |
