Debian Codegraph @

Source/Core/DiscIO/VolumeGC.cpp

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