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