From ff7425214a998b44de8d35ff012b84f66f3c5bba Mon Sep 17 00:00:00 2001 From: JosJuice Date: Thu, 16 Mar 2017 10:28:17 +0100 Subject: [PATCH] Use 6-char game IDs for NAND tiles (if they are printable) 5.0-2712 made ES's code for setting the game ID use the title ID converted to hex (except for disc titles) instead of using a 6-char game ID like before. Then, 5.0-2830 made us use that code even when loading game INIs. This breaks the expectations of both users and the game INIs we ship with. This commit makes Dolphin use 6-char game IDs for all titles (unless the 6-char ID would contain unprintable characters, which is the case with e.g. the Wii Menu). I'm also putting unprintability checks in VolumeWad for consistency. --- Source/Core/Core/ConfigManager.cpp | 3 +-- Source/Core/Core/IOS/ES/Formats.cpp | 20 ++++++++++++++++++++ Source/Core/Core/IOS/ES/Formats.h | 6 ++++++ Source/Core/DiscIO/VolumeWad.cpp | 21 +++++++++------------ 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/Source/Core/Core/ConfigManager.cpp b/Source/Core/Core/ConfigManager.cpp index d96cb2073a..10dfa5008f 100644 --- a/Source/Core/Core/ConfigManager.cpp +++ b/Source/Core/Core/ConfigManager.cpp @@ -766,8 +766,7 @@ void SConfig::SetRunningGameMetadata(const IOS::ES::TMDReader& tmd) } // If not launching a disc game, just read everything from the TMD. - SetRunningGameMetadata(StringFromFormat("%016" PRIX64, tmd_title_id), tmd_title_id, - tmd.GetTitleVersion()); + SetRunningGameMetadata(tmd.GetGameID(), tmd_title_id, tmd.GetTitleVersion()); } void SConfig::SetRunningGameMetadata(const std::string& game_id, u64 title_id, u16 revision) diff --git a/Source/Core/Core/IOS/ES/Formats.cpp b/Source/Core/Core/IOS/ES/Formats.cpp index 484273d2d9..6309fa6c64 100644 --- a/Source/Core/Core/IOS/ES/Formats.cpp +++ b/Source/Core/Core/IOS/ES/Formats.cpp @@ -5,14 +5,18 @@ #include "Core/IOS/ES/Formats.h" #include +#include #include #include +#include +#include #include #include #include "Common/ChunkFile.h" #include "Common/CommonTypes.h" #include "Common/Crypto/AES.h" +#include "Common/StringUtil.h" #include "Common/Swap.h" #include "Core/ec_wii.h" @@ -138,6 +142,22 @@ u16 TMDReader::GetGroupId() const return Common::swap16(m_bytes.data() + offsetof(TMDHeader, group_id)); } +std::string TMDReader::GetGameID() const +{ + char game_id[6]; + std::memcpy(game_id, m_bytes.data() + offsetof(TMDHeader, title_id) + 4, 4); + std::memcpy(game_id + 4, m_bytes.data() + offsetof(TMDHeader, group_id), 2); + + const bool all_printable = std::all_of(std::begin(game_id), std::end(game_id), [](char c) { + return std::isprint(c, std::locale::classic()); + }); + + if (all_printable) + return std::string(game_id, sizeof(game_id)); + + return StringFromFormat("%016" PRIx64, GetTitleId()); +} + u16 TMDReader::GetNumContents() const { return Common::swap16(m_bytes.data() + offsetof(TMDHeader, num_contents)); diff --git a/Source/Core/Core/IOS/ES/Formats.h b/Source/Core/Core/IOS/ES/Formats.h index ed0ec73caf..4c55ed9dd0 100644 --- a/Source/Core/Core/IOS/ES/Formats.h +++ b/Source/Core/Core/IOS/ES/Formats.h @@ -8,6 +8,7 @@ #pragma once #include +#include #include #include "Common/ChunkFile.h" @@ -142,6 +143,11 @@ public: u16 GetTitleVersion() const; u16 GetGroupId() const; + // Constructs a 6-character game ID in the format typically used by Dolphin. + // If the 6-character game ID would contain unprintable characters, + // the title ID converted to hexadecimal is returned instead. + std::string GetGameID() const; + u16 GetNumContents() const; bool GetContent(u16 index, Content* content) const; std::vector GetContents() const; diff --git a/Source/Core/DiscIO/VolumeWad.cpp b/Source/Core/DiscIO/VolumeWad.cpp index 79e5423d54..29bc0afd96 100644 --- a/Source/Core/DiscIO/VolumeWad.cpp +++ b/Source/Core/DiscIO/VolumeWad.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -89,22 +90,18 @@ IOS::ES::TMDReader CVolumeWAD::GetTMD() const std::string CVolumeWAD::GetGameID() const { - char GameCode[6]; - if (!Read(m_offset + 0x01E0, 4, (u8*)GameCode)) - return "0"; - - std::string temp = GetMakerID(); - GameCode[4] = temp.at(0); - GameCode[5] = temp.at(1); - - return DecodeString(GameCode); + return m_tmd.GetGameID(); } std::string CVolumeWAD::GetMakerID() const { - char temp[2] = {1}; - // Some weird channels use 0x0000 in place of the MakerID, so we need a check there - if (!Read(0x198 + m_tmd_offset, 2, (u8*)temp) || temp[0] == 0 || temp[1] == 0) + char temp[2]; + if (!Read(0x198 + m_tmd_offset, 2, (u8*)temp)) + return "00"; + + // Some weird channels use 0x0000 in place of the MakerID, so we need a check here + const std::locale& c_locale = std::locale::classic(); + if (!std::isprint(temp[0], c_locale) || !std::isprint(temp[1], c_locale)) return "00"; return DecodeString(temp);