VolumeGC.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "DiscIO/VolumeGC.h"
  4. #include <cstddef>
  5. #include <map>
  6. #include <memory>
  7. #include <optional>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include "Common/Assert.h"
  12. #include "Common/ColorUtil.h"
  13. #include "Common/CommonTypes.h"
  14. #include "Common/Crypto/SHA1.h"
  15. #include "Common/Logging/Log.h"
  16. #include "Common/MsgHandler.h"
  17. #include "Common/StringUtil.h"
  18. #include "DiscIO/Blob.h"
  19. #include "DiscIO/DiscExtractor.h"
  20. #include "DiscIO/DiscUtils.h"
  21. #include "DiscIO/Enums.h"
  22. #include "DiscIO/FileSystemGCWii.h"
  23. #include "DiscIO/Filesystem.h"
  24. #include "DiscIO/Volume.h"
  25. namespace DiscIO
  26. {
  27. VolumeGC::VolumeGC(std::unique_ptr<BlobReader> reader) : m_reader(std::move(reader))
  28. {
  29. ASSERT(m_reader);
  30. m_file_system = [this]() -> std::unique_ptr<FileSystem> {
  31. auto file_system = std::make_unique<FileSystemGCWii>(this, PARTITION_NONE);
  32. return file_system->IsValid() ? std::move(file_system) : nullptr;
  33. };
  34. m_converted_banner = [this] { return LoadBannerFile(); };
  35. }
  36. VolumeGC::~VolumeGC()
  37. {
  38. }
  39. bool VolumeGC::Read(u64 offset, u64 length, u8* buffer, const Partition& partition) const
  40. {
  41. if (partition != PARTITION_NONE)
  42. return false;
  43. return m_reader->Read(offset, length, buffer);
  44. }
  45. const FileSystem* VolumeGC::GetFileSystem(const Partition& partition) const
  46. {
  47. return m_file_system->get();
  48. }
  49. std::string VolumeGC::GetGameTDBID(const Partition& partition) const
  50. {
  51. // Datel discs for the GameCube can have one of two different game IDs:
  52. //
  53. // 1: GNHE5d. (Yes, with a lowercase d.) This game ID is used not only for
  54. // all kinds of Datel discs, but also for the licensed release NHL Hitz 2002.
  55. //
  56. // 2: DTLX01. This game ID is used for a few late Datel releases. Both Action Replay
  57. // and FreeLoader are known to have been released under this game ID.
  58. //
  59. // Since no game ID used for Datel discs uniquely represents one product,
  60. // never use the game ID of a Datel disc for looking up the title or cover art.
  61. if (IsDatelDisc())
  62. return "";
  63. // Normal case. Just return the usual game ID.
  64. return GetGameID(partition);
  65. }
  66. Region VolumeGC::GetRegion() const
  67. {
  68. return RegionCodeToRegion(m_reader->ReadSwapped<u32>(0x458));
  69. }
  70. std::map<Language, std::string> VolumeGC::GetShortNames() const
  71. {
  72. return m_converted_banner->short_names;
  73. }
  74. std::map<Language, std::string> VolumeGC::GetLongNames() const
  75. {
  76. return m_converted_banner->long_names;
  77. }
  78. std::map<Language, std::string> VolumeGC::GetShortMakers() const
  79. {
  80. return m_converted_banner->short_makers;
  81. }
  82. std::map<Language, std::string> VolumeGC::GetLongMakers() const
  83. {
  84. return m_converted_banner->long_makers;
  85. }
  86. std::map<Language, std::string> VolumeGC::GetDescriptions() const
  87. {
  88. return m_converted_banner->descriptions;
  89. }
  90. std::vector<u32> VolumeGC::GetBanner(u32* width, u32* height) const
  91. {
  92. *width = m_converted_banner->image_width;
  93. *height = m_converted_banner->image_height;
  94. return m_converted_banner->image_buffer;
  95. }
  96. BlobType VolumeGC::GetBlobType() const
  97. {
  98. return m_reader->GetBlobType();
  99. }
  100. u64 VolumeGC::GetDataSize() const
  101. {
  102. return m_reader->GetDataSize();
  103. }
  104. DataSizeType VolumeGC::GetDataSizeType() const
  105. {
  106. return m_reader->GetDataSizeType();
  107. }
  108. u64 VolumeGC::GetRawSize() const
  109. {
  110. return m_reader->GetRawSize();
  111. }
  112. const BlobReader& VolumeGC::GetBlobReader() const
  113. {
  114. return *m_reader;
  115. }
  116. Platform VolumeGC::GetVolumeType() const
  117. {
  118. return Platform::GameCubeDisc;
  119. }
  120. bool VolumeGC::IsDatelDisc() const
  121. {
  122. return GetGameID() == "DTLX01" || !GetBootDOLOffset(*this, PARTITION_NONE).has_value();
  123. }
  124. std::array<u8, 20> VolumeGC::GetSyncHash() const
  125. {
  126. auto context = Common::SHA1::CreateContext();
  127. AddGamePartitionToSyncHash(context.get());
  128. return context->Finish();
  129. }
  130. VolumeGC::ConvertedGCBanner VolumeGC::LoadBannerFile() const
  131. {
  132. GCBanner banner_file;
  133. const u64 file_size = ReadFile(*this, PARTITION_NONE, "opening.bnr",
  134. reinterpret_cast<u8*>(&banner_file), sizeof(GCBanner));
  135. if (file_size < 4)
  136. {
  137. WARN_LOG_FMT(DISCIO, "Could not read opening.bnr.");
  138. return {}; // Return early so that we don't access the uninitialized banner_file.id
  139. }
  140. constexpr u32 BNR1_MAGIC = 0x31524e42;
  141. constexpr u32 BNR2_MAGIC = 0x32524e42;
  142. bool is_bnr1;
  143. if (banner_file.id == BNR1_MAGIC && file_size == BNR1_SIZE)
  144. {
  145. is_bnr1 = true;
  146. }
  147. else if (banner_file.id == BNR2_MAGIC && file_size == BNR2_SIZE)
  148. {
  149. is_bnr1 = false;
  150. }
  151. else
  152. {
  153. WARN_LOG_FMT(DISCIO, "Invalid opening.bnr. Type: {:#0x} Size: {:#0x}", banner_file.id,
  154. file_size);
  155. return {};
  156. }
  157. return ExtractBannerInformation(banner_file, is_bnr1);
  158. }
  159. VolumeGC::ConvertedGCBanner VolumeGC::ExtractBannerInformation(const GCBanner& banner_file,
  160. bool is_bnr1) const
  161. {
  162. ConvertedGCBanner banner;
  163. u32 number_of_languages = 0;
  164. Language start_language = Language::Unknown;
  165. if (is_bnr1) // NTSC
  166. {
  167. number_of_languages = 1;
  168. start_language = GetRegion() == Region::NTSC_J ? Language::Japanese : Language::English;
  169. }
  170. else // PAL
  171. {
  172. number_of_languages = 6;
  173. start_language = Language::English;
  174. }
  175. banner.image_width = GC_BANNER_WIDTH;
  176. banner.image_height = GC_BANNER_HEIGHT;
  177. banner.image_buffer = std::vector<u32>(GC_BANNER_WIDTH * GC_BANNER_HEIGHT);
  178. Common::Decode5A3Image(banner.image_buffer.data(), banner_file.image, GC_BANNER_WIDTH,
  179. GC_BANNER_HEIGHT);
  180. for (u32 i = 0; i < number_of_languages; ++i)
  181. {
  182. const GCBannerInformation& info = banner_file.information[i];
  183. Language language = static_cast<Language>(static_cast<int>(start_language) + i);
  184. std::string description = DecodeString(info.description);
  185. if (!description.empty())
  186. banner.descriptions.emplace(language, description);
  187. std::string short_name = DecodeString(info.short_name);
  188. if (!short_name.empty())
  189. banner.short_names.emplace(language, short_name);
  190. std::string long_name = DecodeString(info.long_name);
  191. if (!long_name.empty())
  192. banner.long_names.emplace(language, long_name);
  193. std::string short_maker = DecodeString(info.short_maker);
  194. if (!short_maker.empty())
  195. banner.short_makers.emplace(language, short_maker);
  196. std::string long_maker = DecodeString(info.long_maker);
  197. if (!long_maker.empty())
  198. banner.long_makers.emplace(language, long_maker);
  199. }
  200. return banner;
  201. }
  202. VolumeGC::ConvertedGCBanner::ConvertedGCBanner() = default;
  203. VolumeGC::ConvertedGCBanner::~ConvertedGCBanner() = default;
  204. } // namespace DiscIO