From fe10e8aa6c990bd1d45a68377c50b3dd140cf5b4 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 16 Jul 2017 13:52:53 +0200 Subject: [PATCH 1/7] Fix RegionSwitchGC for the value 'K' Korean GameCube releases have their region set to NTSC-J. --- Source/Core/DiscIO/Enums.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Core/DiscIO/Enums.cpp b/Source/Core/DiscIO/Enums.cpp index 94af6c75a9..6add18d801 100644 --- a/Source/Core/DiscIO/Enums.cpp +++ b/Source/Core/DiscIO/Enums.cpp @@ -48,7 +48,7 @@ Country TypicalCountryForRegion(Region region) Region RegionSwitchGC(u8 country_code) { Region region = RegionSwitchWii(country_code); - return region == Region::NTSC_K ? Region::UNKNOWN_REGION : region; + return region == Region::NTSC_K ? Region::NTSC_J : region; } Region RegionSwitchWii(u8 country_code) From ebf0f64a01818fb9cea91e5fdecf615e5e45cd91 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 16 Jul 2017 13:53:22 +0200 Subject: [PATCH 2/7] Detect GC region based on the actual region value The county code isn't 100% reliable for detecting the region. For instance, some games released in Korea have the country code E even though they're region-locked to NTSC-J consoles. This commit makes the GC disc region detection match the Wii disc region detection (apart from the region value being in a different place on the disc). --- Source/Core/DiscIO/Enums.h | 4 ++-- Source/Core/DiscIO/VolumeGC.cpp | 14 ++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Source/Core/DiscIO/Enums.h b/Source/Core/DiscIO/Enums.h index cce235c38e..7aa06ef4a1 100644 --- a/Source/Core/DiscIO/Enums.h +++ b/Source/Core/DiscIO/Enums.h @@ -40,10 +40,10 @@ enum class Country NUMBER_OF_COUNTRIES }; -// Regions 0 - 2 and 4 match Nintendo's Wii region numbering. +// Regions 0 - 2 and 4 match Nintendo's GameCube/Wii region numbering. enum class Region { - NTSC_J = 0, // Japan and Taiwan + NTSC_J = 0, // Japan and Taiwan (and South Korea for GameCube only) NTSC_U = 1, // Mainly North America PAL = 2, // Mainly Europe and Oceania UNKNOWN_REGION = 3, // 3 seems to be unused? Anyway, we need an UNKNOWN_REGION. Let's put it here diff --git a/Source/Core/DiscIO/VolumeGC.cpp b/Source/Core/DiscIO/VolumeGC.cpp index 1a89917812..5941af4a56 100644 --- a/Source/Core/DiscIO/VolumeGC.cpp +++ b/Source/Core/DiscIO/VolumeGC.cpp @@ -60,14 +60,20 @@ std::string VolumeGC::GetGameID(const Partition& partition) const Region VolumeGC::GetRegion() const { - const std::optional country_code = ReadSwapped(3, PARTITION_NONE); - return country_code ? RegionSwitchGC(*country_code) : Region::UNKNOWN_REGION; + const std::optional region_code = ReadSwapped(0x458, PARTITION_NONE); + return region_code ? static_cast(*region_code) : Region::UNKNOWN_REGION; } Country VolumeGC::GetCountry(const Partition& partition) const { - const std::optional country_code = ReadSwapped(3, partition); - return country_code ? CountrySwitch(*country_code) : Country::COUNTRY_UNKNOWN; + // The 0 that we use as a default value is mapped to COUNTRY_UNKNOWN and UNKNOWN_REGION + const u8 country_byte = ReadSwapped(3, partition).value_or(0); + const Region region = GetRegion(); + + if (RegionSwitchGC(country_byte) != region) + return TypicalCountryForRegion(region); + + return CountrySwitch(country_byte); } std::string VolumeGC::GetMakerID(const Partition& partition) const From c8b4645039fb7bc846d4030a94d76f19d12c6564 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 16 Jul 2017 14:25:55 +0200 Subject: [PATCH 3/7] Detect Korean GC releases as Korean when possible According to http://scanlines16.com/en/blog-3/retro-gaming/game-cube/gamecube-korean-master-list/, Korean GC releases use the following country codes: - E or W for games in English - K for games in Korean - Unknown value for games in Japanese (my guess is that they might have made the discs bit-for-bit identical to Japanese releases because the regions of these games are already set to NTSC-J) As far as I know, the GC has no Taiwanese releases, which is what the W country code is used for on the Wii. But I could be wrong. A small note: The country_byte == 'K' check in the code isn't actually necessary as long as RegionSwitchGC returns NTSC_J for 'K', but I thought it would be better to not rely on that. --- Source/Core/DiscIO/VolumeGC.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Source/Core/DiscIO/VolumeGC.cpp b/Source/Core/DiscIO/VolumeGC.cpp index 5941af4a56..dd13ecdcd0 100644 --- a/Source/Core/DiscIO/VolumeGC.cpp +++ b/Source/Core/DiscIO/VolumeGC.cpp @@ -67,13 +67,21 @@ Region VolumeGC::GetRegion() const Country VolumeGC::GetCountry(const Partition& partition) const { // The 0 that we use as a default value is mapped to COUNTRY_UNKNOWN and UNKNOWN_REGION - const u8 country_byte = ReadSwapped(3, partition).value_or(0); + const u8 country = ReadSwapped(3, partition).value_or(0); const Region region = GetRegion(); - if (RegionSwitchGC(country_byte) != region) + // Korean GC releases use NTSC-J. + // E is normally used for America, but it's also used for English-language Korean GC releases. + // K is used by games that are in the Korean language. + // W means Taiwan for Wii games, but on the GC, it's used for English-language Korean releases. + // (There doesn't seem to be any pattern to which of E and W is used for Korean GC releases.) + if (region == Region::NTSC_J && (country == 'E' || country == 'K' || country == 'W')) + return Country::COUNTRY_KOREA; + + if (RegionSwitchGC(country) != region) return TypicalCountryForRegion(region); - return CountrySwitch(country_byte); + return CountrySwitch(country); } std::string VolumeGC::GetMakerID(const Partition& partition) const From cd60810d9db611d429f1d2f503cc3d3bfc0afc9f Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 16 Jul 2017 14:33:14 +0200 Subject: [PATCH 4/7] Improve Japanese check in TitleDatabase We should check the language of a game rather than the region. RegionSwitchGC counts W as NTSC-J, and W games aren't in Japanese. --- Source/Core/Core/TitleDatabase.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Core/Core/TitleDatabase.cpp b/Source/Core/Core/TitleDatabase.cpp index 2f3ec48541..ea8b2916f1 100644 --- a/Source/Core/Core/TitleDatabase.cpp +++ b/Source/Core/Core/TitleDatabase.cpp @@ -94,12 +94,12 @@ static bool IsWiiTitle(const std::string& game_id) static bool IsJapaneseGCTitle(const std::string& game_id) { - return IsGCTitle(game_id) && DiscIO::RegionSwitchGC(game_id[3]) == DiscIO::Region::NTSC_J; + return IsGCTitle(game_id) && DiscIO::CountrySwitch(game_id[3]) == DiscIO::Country::COUNTRY_JAPAN; } static bool IsNonJapaneseGCTitle(const std::string& game_id) { - return IsGCTitle(game_id) && DiscIO::RegionSwitchGC(game_id[3]) != DiscIO::Region::NTSC_J; + return IsGCTitle(game_id) && DiscIO::CountrySwitch(game_id[3]) != DiscIO::Country::COUNTRY_JAPAN; } // Note that this function will not overwrite entries that already are in the maps From db1c534b3ecddf23ad57b03c80b74de55466b062 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 16 Jul 2017 14:49:28 +0200 Subject: [PATCH 5/7] Add warning comments to RegionSwitch and code that uses it --- .../Core/HW/GCMemcard/GCMemcardDirectory.cpp | 1 + Source/Core/Core/IOS/ES/Formats.cpp | 16 ++++++++-------- Source/Core/Core/IOS/ES/Formats.h | 4 +++- Source/Core/DiscIO/Enums.h | 5 +++++ Source/Core/DiscIO/Volume.h | 1 + Source/Core/DiscIO/VolumeWad.h | 1 + 6 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Source/Core/Core/HW/GCMemcard/GCMemcardDirectory.cpp b/Source/Core/Core/HW/GCMemcard/GCMemcardDirectory.cpp index 23c0e4b467..344b9a56f7 100644 --- a/Source/Core/Core/HW/GCMemcard/GCMemcardDirectory.cpp +++ b/Source/Core/Core/HW/GCMemcard/GCMemcardDirectory.cpp @@ -44,6 +44,7 @@ int GCMemcardDirectory::LoadGCI(const std::string& file_name, DiscIO::Region car return NO_INDEX; } + // This isn't a reliable way to detect regions. TODO: Get rid of this? DiscIO::Region gci_region = DiscIO::RegionSwitchGC(gci.m_gci_header.Gamecode[3]); // Some special save files have game IDs that we parse as UNKNOWN_REGION. For instance: // - Datel Action Replay uses C as the fourth character. (Can be on any region's card.) diff --git a/Source/Core/Core/IOS/ES/Formats.cpp b/Source/Core/Core/IOS/ES/Formats.cpp index b7e1e9c106..9ac65e8373 100644 --- a/Source/Core/Core/IOS/ES/Formats.cpp +++ b/Source/Core/Core/IOS/ES/Formats.cpp @@ -232,14 +232,6 @@ u64 TMDReader::GetIOSId() const return Common::swap64(m_bytes.data() + offsetof(TMDHeader, ios_id)); } -DiscIO::Region TMDReader::GetRegion() const -{ - if (GetTitleId() == Titles::SYSTEM_MENU) - return DiscIO::GetSysMenuRegion(GetTitleVersion()); - - return DiscIO::RegionSwitchWii(static_cast(GetTitleId() & 0xff)); -} - u64 TMDReader::GetTitleId() const { return Common::swap64(m_bytes.data() + offsetof(TMDHeader, title_id)); @@ -260,6 +252,14 @@ u16 TMDReader::GetGroupId() const return Common::swap16(m_bytes.data() + offsetof(TMDHeader, group_id)); } +DiscIO::Region TMDReader::GetRegion() const +{ + if (GetTitleId() == Titles::SYSTEM_MENU) + return DiscIO::GetSysMenuRegion(GetTitleVersion()); + + return DiscIO::RegionSwitchWii(static_cast(GetTitleId() & 0xff)); +} + std::string TMDReader::GetGameID() const { char game_id[6]; diff --git a/Source/Core/Core/IOS/ES/Formats.h b/Source/Core/Core/IOS/ES/Formats.h index 3998076cbe..5f01e7eef6 100644 --- a/Source/Core/Core/IOS/ES/Formats.h +++ b/Source/Core/Core/IOS/ES/Formats.h @@ -185,12 +185,14 @@ public: u16 GetBootIndex() const; u64 GetIOSId() const; - DiscIO::Region GetRegion() const; u64 GetTitleId() const; u32 GetTitleFlags() const; u16 GetTitleVersion() const; u16 GetGroupId() const; + // Provides a best guess for the region. Might be inaccurate or UNKNOWN_REGION. + DiscIO::Region GetRegion() 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. diff --git a/Source/Core/DiscIO/Enums.h b/Source/Core/DiscIO/Enums.h index 7aa06ef4a1..fda94c43be 100644 --- a/Source/Core/DiscIO/Enums.h +++ b/Source/Core/DiscIO/Enums.h @@ -71,11 +71,16 @@ enum class Language bool IsDisc(Platform volume_type); bool IsWii(Platform volume_type); bool IsNTSC(Region region); + Country TypicalCountryForRegion(Region region); +// Avoid using this function if you can. Country codes aren't always reliable region indicators. Region RegionSwitchGC(u8 country_code); +// Avoid using this function if you can. Country codes aren't always reliable region indicators. Region RegionSwitchWii(u8 country_code); Country CountrySwitch(u8 country_code); + Region GetSysMenuRegion(u16 title_version); std::string GetSysMenuVersionString(u16 title_version); + std::string GetCompanyFromID(const std::string& company_id); } diff --git a/Source/Core/DiscIO/Volume.h b/Source/Core/DiscIO/Volume.h index e0978048ab..209a0d09a9 100644 --- a/Source/Core/DiscIO/Volume.h +++ b/Source/Core/DiscIO/Volume.h @@ -83,6 +83,7 @@ public: virtual Platform GetVolumeType() const = 0; virtual bool SupportsIntegrityCheck() const { return false; } virtual bool CheckIntegrity(const Partition& partition) const { return false; } + // May be inaccurate for WADs virtual Region GetRegion() const = 0; Country GetCountry() const { return GetCountry(GetGamePartition()); } virtual Country GetCountry(const Partition& partition) const = 0; diff --git a/Source/Core/DiscIO/VolumeWad.h b/Source/Core/DiscIO/VolumeWad.h index d8b45d3585..d1964bcfa1 100644 --- a/Source/Core/DiscIO/VolumeWad.h +++ b/Source/Core/DiscIO/VolumeWad.h @@ -50,6 +50,7 @@ public: return ""; } Platform GetVolumeType() const override; + // Provides a best guess for the region. Might be inaccurate or UNKNOWN_REGION. Region GetRegion() const override; Country GetCountry(const Partition& partition = PARTITION_NONE) const override; From be8e93f472ba8630622dd71a5b50a065ca942977 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 16 Jul 2017 14:52:17 +0200 Subject: [PATCH 6/7] Ensure that Volume::GetRegion doesn't return invalid enum values --- Source/Core/DiscIO/VolumeGC.cpp | 5 ++++- Source/Core/DiscIO/VolumeWii.cpp | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Source/Core/DiscIO/VolumeGC.cpp b/Source/Core/DiscIO/VolumeGC.cpp index dd13ecdcd0..46ac57278c 100644 --- a/Source/Core/DiscIO/VolumeGC.cpp +++ b/Source/Core/DiscIO/VolumeGC.cpp @@ -61,7 +61,10 @@ std::string VolumeGC::GetGameID(const Partition& partition) const Region VolumeGC::GetRegion() const { const std::optional region_code = ReadSwapped(0x458, PARTITION_NONE); - return region_code ? static_cast(*region_code) : Region::UNKNOWN_REGION; + if (!region_code) + return Region::UNKNOWN_REGION; + const Region region = static_cast(*region_code); + return region <= Region::PAL ? region : Region::UNKNOWN_REGION; } Country VolumeGC::GetCountry(const Partition& partition) const diff --git a/Source/Core/DiscIO/VolumeWii.cpp b/Source/Core/DiscIO/VolumeWii.cpp index cf570e39ea..7225a1e57f 100644 --- a/Source/Core/DiscIO/VolumeWii.cpp +++ b/Source/Core/DiscIO/VolumeWii.cpp @@ -233,7 +233,10 @@ std::string VolumeWii::GetGameID(const Partition& partition) const Region VolumeWii::GetRegion() const { const std::optional region_code = m_pReader->ReadSwapped(0x4E000); - return region_code ? static_cast(*region_code) : Region::UNKNOWN_REGION; + if (!region_code) + return Region::UNKNOWN_REGION; + const Region region = static_cast(*region_code); + return region <= Region::NTSC_K ? region : Region::UNKNOWN_REGION; } Country VolumeWii::GetCountry(const Partition& partition) const From c83cac7cca874de782109fdeee2990aab8606879 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 16 Jul 2017 16:31:12 +0200 Subject: [PATCH 7/7] Fix the handling of NTSC-K for GCI folders --- Source/Core/Core/ConfigManager.cpp | 24 ++++++++++++------- Source/Core/Core/ConfigManager.h | 3 +++ .../Core/Core/HW/EXI/EXI_DeviceMemoryCard.cpp | 2 +- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Source/Core/Core/ConfigManager.cpp b/Source/Core/Core/ConfigManager.cpp index 554f0dfd02..687bdadf8c 100644 --- a/Source/Core/Core/ConfigManager.cpp +++ b/Source/Core/Core/ConfigManager.cpp @@ -13,6 +13,7 @@ #include "AudioCommon/AudioCommon.h" +#include "Common/Assert.h" #include "Common/CDUtils.h" #include "Common/CommonPaths.h" #include "Common/CommonTypes.h" @@ -869,6 +870,18 @@ bool SConfig::IsUSBDeviceWhitelisted(const std::pair vid_pid) const return m_usb_passthrough_devices.find(vid_pid) != m_usb_passthrough_devices.end(); } +// The reason we need this function is because some memory card code +// expects to get a non-NTSC-K region even if we're emulating an NTSC-K Wii. +DiscIO::Region SConfig::ToGameCubeRegion(DiscIO::Region region) +{ + if (region != DiscIO::Region::NTSC_K) + return region; + + // GameCube has no NTSC-K region. No choice of replacement value is completely + // non-arbitrary, but let's go with NTSC-J since Korean GameCubes are NTSC-J. + return DiscIO::Region::NTSC_J; +} + const char* SConfig::GetDirectoryForRegion(DiscIO::Region region) { switch (region) @@ -883,13 +896,8 @@ const char* SConfig::GetDirectoryForRegion(DiscIO::Region region) return EUR_DIR; case DiscIO::Region::NTSC_K: - // This function can't return a Korean directory name, because this - // function is only used for GameCube things (memory cards, IPL), and - // GameCube has no NTSC-K region. Since NTSC-K doesn't correspond to any - // GameCube region, let's return an arbitrary pick. Returning nullptr like - // with unknown regions would be inappropriate, because Dolphin expects - // to get valid memory card paths even when running an NTSC-K Wii game. - return JAP_DIR; + _assert_msg_(BOOT, false, "NTSC-K is not a valid GameCube region"); + return nullptr; default: return nullptr; @@ -982,7 +990,7 @@ bool SConfig::SetPathsAndGameMetadata(const BootParameters& boot) return false; // Set up region - const char* retrieved_region_dir = GetDirectoryForRegion(region); + const char* retrieved_region_dir = GetDirectoryForRegion(ToGameCubeRegion(region)); m_region = retrieved_region_dir ? region : DiscIO::Region::PAL; const std::string set_region_dir = retrieved_region_dir ? retrieved_region_dir : EUR_DIR; if (!retrieved_region_dir && diff --git a/Source/Core/Core/ConfigManager.h b/Source/Core/Core/ConfigManager.h index f0ccff8cef..357907782d 100644 --- a/Source/Core/Core/ConfigManager.h +++ b/Source/Core/Core/ConfigManager.h @@ -217,6 +217,9 @@ struct SConfig : NonCopyable void SetRunningGameMetadata(const IOS::ES::TMDReader& tmd); void LoadDefaults(); + // Replaces NTSC-K with some other region, and doesn't replace non-NTSC-K regions + static DiscIO::Region ToGameCubeRegion(DiscIO::Region region); + // The region argument must be valid for GameCube (i.e. must not be NTSC-K) static const char* GetDirectoryForRegion(DiscIO::Region region); std::string GetBootROMPath(const std::string& region_directory) const; bool SetPathsAndGameMetadata(const BootParameters& boot); diff --git a/Source/Core/Core/HW/EXI/EXI_DeviceMemoryCard.cpp b/Source/Core/Core/HW/EXI/EXI_DeviceMemoryCard.cpp index 8c819eeb2f..98c6334a80 100644 --- a/Source/Core/Core/HW/EXI/EXI_DeviceMemoryCard.cpp +++ b/Source/Core/Core/HW/EXI/EXI_DeviceMemoryCard.cpp @@ -157,7 +157,7 @@ CEXIMemoryCard::CEXIMemoryCard(const int index, bool gciFolder) : card_index(ind void CEXIMemoryCard::SetupGciFolder(u16 sizeMb) { - DiscIO::Region region = SConfig::GetInstance().m_region; + const DiscIO::Region region = SConfig::ToGameCubeRegion(SConfig::GetInstance().m_region); const std::string& game_id = SConfig::GetInstance().GetGameID(); u32 CurrentGameId = 0;