Make netplay's "same game" check more robust

Instead of comparing the game ID, revision, disc number and name,
we can compare a hash of important parts of the disc including
all the aforementioned data but also additional data such as the
FST. The primary reason why I'm making this change is to let us
catch more desyncs before they happen, but this should also fix
https://bugs.dolphin-emu.org/issues/12115. As a bonus, the UI can
now distinguish the case where a client doesn't have the game at
all from the case where a client has the wrong version of the game.
This commit is contained in:
JosJuice
2020-06-07 22:58:03 +02:00
parent 25ebc3c07c
commit a41166bb37
34 changed files with 502 additions and 148 deletions

View File

@ -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
{
std::vector<std::string> info;
if (!GetGameID().empty())
@ -566,6 +569,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));