summaryrefslogtreecommitdiff
path: root/Source/Core/UICommon/GameFile.cpp
diff options
context:
space:
mode:
authorJMC47 <JMC4789@gmail.com>2020-09-06 17:14:08 -0400
committerGitHub <noreply@github.com>2020-09-06 17:14:08 -0400
commite7e51756068a450b6e920c7783c81d7df4d2d133 (patch)
tree94f0bfe833986f8d35f0d2a40842807601f53b9f /Source/Core/UICommon/GameFile.cpp
parent344fdabf23c048b81b8e87008a7a16837d180f55 (diff)
parentfc6c1931fa09513388f35507b724bc378a0758e1 (diff)
Merge pull request #8861 from JosJuice/netplay-hash
Make netplay's "same game" check more robust
Diffstat (limited to 'Source/Core/UICommon/GameFile.cpp')
-rw-r--r--Source/Core/UICommon/GameFile.cpp72
1 files changed, 65 insertions, 7 deletions
diff --git a/Source/Core/UICommon/GameFile.cpp b/Source/Core/UICommon/GameFile.cpp
index b28d54e944..30641d58e5 100644
--- a/Source/Core/UICommon/GameFile.cpp
+++ b/Source/Core/UICommon/GameFile.cpp
@@ -5,6 +5,7 @@
#include "UICommon/GameFile.h"
#include <algorithm>
+#include <array>
#include <cinttypes>
#include <cstdio>
#include <cstring>
@@ -13,11 +14,13 @@
#include <memory>
#include <sstream>
#include <string>
+#include <string_view>
#include <tuple>
#include <utility>
#include <vector>
#include <fmt/format.h>
+#include <mbedtls/sha1.h>
#include <pugixml.hpp>
#include "Common/ChunkFile.h"
@@ -532,7 +535,7 @@ std::vector<DiscIO::Language> GameFile::GetLanguages() const
return languages;
}
-std::string GameFile::GetUniqueIdentifier() const
+std::string GameFile::GetNetPlayName(const Core::TitleDatabase& title_database) const
{
std::vector<std::string> info;
if (!GetGameID().empty())
@@ -540,12 +543,7 @@ std::string GameFile::GetUniqueIdentifier() const
if (GetRevision() != 0)
info.push_back("Revision " + std::to_string(GetRevision()));
- std::string name = GetLongName(DiscIO::Language::English);
- if (name.empty())
- {
- // Use the file name as a fallback. Not necessarily consistent, but it's the best we have
- name = m_file_name;
- }
+ const std::string name = GetName(title_database);
int disc_number = GetDiscNumber() + 1;
@@ -566,6 +564,66 @@ std::string GameFile::GetUniqueIdentifier() const
return name + " (" + ss.str() + ")";
}
+std::array<u8, 20> GameFile::GetSyncHash() const
+{
+ 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());
+ }
+ else
+ {
+ if (std::unique_ptr<DiscIO::Volume> volume = DiscIO::CreateVolume(m_file_path))
+ hash = volume->GetSyncHash();
+ }
+
+ return hash;
+}
+
+NetPlay::SyncIdentifier GameFile::GetSyncIdentifier() const
+{
+ const u64 dol_elf_size = m_platform == DiscIO::Platform::ELFOrDOL ? m_file_size : 0;
+ return NetPlay::SyncIdentifier{dol_elf_size, m_game_id, m_revision,
+ m_disc_number, m_is_datel_disc, GetSyncHash()};
+}
+
+NetPlay::SyncIdentifierComparison
+GameFile::CompareSyncIdentifier(const NetPlay::SyncIdentifier& sync_identifier) const
+{
+ const bool is_elf_or_dol = m_platform == DiscIO::Platform::ELFOrDOL;
+ if ((is_elf_or_dol ? m_file_size : 0) != sync_identifier.dol_elf_size)
+ return NetPlay::SyncIdentifierComparison::DifferentGame;
+
+ const auto trim = [](const std::string& str, size_t n) {
+ return std::string_view(str.data(), std::min(n, str.size()));
+ };
+
+ if (trim(m_game_id, 3) != trim(sync_identifier.game_id, 3))
+ return NetPlay::SyncIdentifierComparison::DifferentGame;
+
+ if (m_disc_number != sync_identifier.disc_number || m_is_datel_disc != sync_identifier.is_datel)
+ return NetPlay::SyncIdentifierComparison::DifferentGame;
+
+ const NetPlay::SyncIdentifierComparison mismatch_result =
+ is_elf_or_dol || m_is_datel_disc ? NetPlay::SyncIdentifierComparison::DifferentGame :
+ NetPlay::SyncIdentifierComparison::DifferentVersion;
+
+ if (m_game_id != sync_identifier.game_id)
+ {
+ const bool game_id_is_title_id = m_game_id.size() > 6 || sync_identifier.game_id.size() > 6;
+ return game_id_is_title_id ? NetPlay::SyncIdentifierComparison::DifferentGame : mismatch_result;
+ }
+
+ if (m_revision != sync_identifier.revision)
+ return mismatch_result;
+
+ return GetSyncHash() == sync_identifier.sync_hash ? NetPlay::SyncIdentifierComparison::SameGame :
+ mismatch_result;
+}
+
std::string GameFile::GetWiiFSPath() const
{
ASSERT(DiscIO::IsWii(m_platform));