Debian Codegraph @

Source/Core/UICommon/GameFile.cpp

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "UICommon/GameFile.h"
5
6 #include <algorithm>
7 #include <array>
8 #include <cstdio>
9 #include <cstring>
10 #include <map>
11 #include <memory>
12 #include <string>
13 #include <string_view>
14 #include <utility>
15 #include <vector>
16
17 #include <fmt/format.h>
18 #include <fmt/ranges.h>
19 #include <pugixml.hpp>
20
21 #include "Common/BitUtils.h"
22 #include "Common/ChunkFile.h"
23 #include "Common/CommonPaths.h"
24 #include "Common/CommonTypes.h"
25 #include "Common/Crypto/SHA1.h"
26 #include "Common/FileUtil.h"
27 #include "Common/HttpRequest.h"
28 #include "Common/IOFile.h"
29 #include "Common/Image.h"
30 #include "Common/MsgHandler.h"
31 #include "Common/NandPaths.h"
32 #include "Common/StringUtil.h"
33 #include "Common/Swap.h"
34
35 #include "Core/Config/UISettings.h"
36 #include "Core/ConfigManager.h"
37 #include "Core/IOS/ES/Formats.h"
38 #include "Core/TitleDatabase.h"
39
40 #include "DiscIO/Blob.h"
41 #include "DiscIO/DiscExtractor.h"
42 #include "DiscIO/Enums.h"
43 #include "DiscIO/GameModDescriptor.h"
44 #include "DiscIO/Volume.h"
45 #include "DiscIO/WiiSaveBanner.h"
46
47 namespace UICommon
48 {
49 namespace
50 {
51 const std::string EMPTY_STRING;
52
53 bool UseGameCovers()
54 {
55 #ifdef ANDROID
56 // Android has its own code for handling covers, written completely in Java.
57 // It's best if we disable the C++ cover code on Android to avoid duplicated data and such.
58 return false;
59 #else
60 return Config::Get(Config::MAIN_USE_GAME_COVERS);
61 #endif
62 }
63 } // Anonymous namespace
64
65 DiscIO::Language GameFile::GetConfigLanguage() const
66 {
67 return SConfig::GetInstance().GetLanguageAdjustedForRegion(DiscIO::IsWii(m_platform), m_region);
68 }
69
70 const std::string& GameFile::Lookup(DiscIO::Language language,
71 const std::map<DiscIO::Language, std::string>& strings)
72 {
73 auto end = strings.end();
74 auto it = strings.find(language);
75 if (it != end)
76 return it->second;
77
78 // English tends to be a good fallback when the requested language isn't available
79 if (language != DiscIO::Language::English)
80 {
81 it = strings.find(DiscIO::Language::English);
82 if (it != end)
83 return it->second;
84 }
85
86 // If English isn't available either, just pick something
87 if (!strings.empty())
88 return strings.cbegin()->second;
89
90 return EMPTY_STRING;
91 }
92
93 const std::string&
94 GameFile::LookupUsingConfigLanguage(const std::map<DiscIO::Language, std::string>& strings) const
95 {
96 return Lookup(GetConfigLanguage(), strings);
97 }
98
99 GameFile::GameFile() = default;
100
101 GameFile::GameFile(std::string path) : m_file_path(std::move(path))
102 {
103 m_file_name = PathToFileName(m_file_path);
104
105 {
106 std::unique_ptr<DiscIO::Volume> volume(DiscIO::CreateVolume(m_file_path));
107 if (volume != nullptr)
108 {
109 m_platform = volume->GetVolumeType();
110
111 m_short_names = volume->GetShortNames();
112 m_long_names = volume->GetLongNames();
113 m_short_makers = volume->GetShortMakers();
114 m_long_makers = volume->GetLongMakers();
115 m_descriptions = volume->GetDescriptions();
116
117 m_region = volume->GetRegion();
118 m_country = volume->GetCountry();
119 m_blob_type = volume->GetBlobType();
120 m_block_size = volume->GetBlobReader().GetBlockSize();
121 m_compression_method = volume->GetBlobReader().GetCompressionMethod();
122 m_file_size = volume->GetRawSize();
123 m_volume_size = volume->GetDataSize();
124 m_volume_size_type = volume->GetDataSizeType();
125 m_is_datel_disc = volume->IsDatelDisc();
126 m_is_nkit = volume->IsNKit();
127
128 m_internal_name = volume->GetInternalName();
129 m_game_id = volume->GetGameID();
130 m_gametdb_id = volume->GetGameTDBID();
131 m_title_id = volume->GetTitleID().value_or(0);
132 m_maker_id = volume->GetMakerID();
133 m_revision = volume->GetRevision().value_or(0);
134 m_disc_number = volume->GetDiscNumber().value_or(0);
135 m_is_two_disc_game = CheckIfTwoDiscGame(m_game_id);
136 m_apploader_date = volume->GetApploaderDate();
137
138 m_volume_banner.buffer = volume->GetBanner(&m_volume_banner.width, &m_volume_banner.height);
139
140 m_valid = true;
141 }
142 }
143
144 if (!IsValid() && IsElfOrDol())
145 {
146 m_valid = true;
147 m_file_size = m_volume_size = File::GetSize(m_file_path);
148 m_game_id = SConfig::MakeGameID(m_file_name);
149 m_volume_size_type = DiscIO::DataSizeType::Accurate;
150 m_is_datel_disc = false;
151 m_is_nkit = false;
152 m_platform = DiscIO::Platform::ELFOrDOL;
153 m_blob_type = DiscIO::BlobType::DIRECTORY;
154 }
155
156 if (!IsValid() && GetExtension() == ".json")
157 {
158 auto descriptor = DiscIO::ParseGameModDescriptorFile(m_file_path);
159 if (descriptor)
160 {
161 GameFile proxy(descriptor->base_file);
162 if (proxy.IsValid())
163 {
164 m_valid = true;
165 m_file_size = File::GetSize(m_file_path);
166 m_long_names.emplace(DiscIO::Language::English, std::move(descriptor->display_name));
167 if (!descriptor->maker.empty())
168 m_long_makers.emplace(DiscIO::Language::English, std::move(descriptor->maker));
169 m_internal_name = proxy.GetInternalName();
170 m_game_id = proxy.GetGameID();
171 m_gametdb_id = proxy.GetGameTDBID();
172 m_title_id = proxy.GetTitleID();
173 m_maker_id = proxy.GetMakerID();
174 m_region = proxy.GetRegion();
175 m_country = proxy.GetCountry();
176 m_platform = proxy.GetPlatform();
177 m_revision = proxy.GetRevision();
178 m_disc_number = proxy.GetDiscNumber();
179 m_blob_type = DiscIO::BlobType::MOD_DESCRIPTOR;
180 }
181 }
182 }
183 }
184
185 GameFile::~GameFile() = default;
186
187 bool GameFile::IsValid() const
188 {
189 if (!m_valid)
190 return false;
191
192 if (m_platform == DiscIO::Platform::WiiWAD && !IOS::ES::IsChannel(m_title_id))
193 return false;
194
195 return true;
196 }
197
198 bool GameFile::CustomCoverChanged()
199 {
200 if (!m_custom_cover.buffer.empty() || !UseGameCovers())
201 return false;
202
203 std::string path, name;
204 SplitPath(m_file_path, &path, &name, nullptr);
205
206 std::string contents;
207
208 // This icon naming format is intended as an alternative to Homebrew Channel icons
209 // for those who don't want to have a Homebrew Channel style folder structure.
210 const std::string cover_path = path + name + ".cover.png";
211 bool success = File::Exists(cover_path) && File::ReadFileToString(cover_path, contents);
212
213 if (!success)
214 {
215 const std::string alt_cover_path = path + "cover.png";
216 success = File::Exists(alt_cover_path) && File::ReadFileToString(alt_cover_path, contents);
217 }
218
219 if (success)
220 m_pending.custom_cover.buffer = {contents.begin(), contents.end()};
221
222 return success;
223 }
224
225 void GameFile::DownloadDefaultCover()
226 {
227 if (!m_default_cover.buffer.empty() || !UseGameCovers() || m_gametdb_id.empty())
228 return;
229
230 const auto cover_path = File::GetUserPath(D_COVERCACHE_IDX) + DIR_SEP;
231 const auto png_path = cover_path + m_gametdb_id + ".png";
232
233 // If the cover has already been downloaded, abort
234 if (File::Exists(png_path))
235 return;
236
237 const std::string region_code =
238 SConfig::GetInstance().GetGameTDBImageRegionCode(DiscIO::IsWii(GetPlatform()), m_region);
239
240 Common::HttpRequest request;
241 static constexpr char cover_url[] = "https://art.gametdb.com/wii/cover/{}/{}.png";
242 const auto response = request.Get(fmt::format(cover_url, region_code, m_gametdb_id));
243
244 if (!response)
245 return;
246
247 File::WriteStringToFile(png_path, std::string(response->begin(), response->end()));
248 }
249
250 bool GameFile::DefaultCoverChanged()
251 {
252 if (!m_default_cover.buffer.empty() || !UseGameCovers())
253 return false;
254
255 const auto cover_path = File::GetUserPath(D_COVERCACHE_IDX) + DIR_SEP;
256
257 std::string contents;
258
259 File::ReadFileToString(cover_path + m_gametdb_id + ".png", contents);
260
261 if (contents.empty())
262 return false;
263
264 m_pending.default_cover.buffer = {contents.begin(), contents.end()};
265
266 return true;
267 }
268
269 void GameFile::CustomCoverCommit()
270 {
271 m_custom_cover = std::move(m_pending.custom_cover);
272 }
273
274 void GameFile::DefaultCoverCommit()
275 {
276 m_default_cover = std::move(m_pending.default_cover);
277 }
278
279 void GameBanner::DoState(PointerWrap& p)
280 {
281 p.Do(buffer);
282 p.Do(width);
283 p.Do(height);
284 }
285
286 void GameCover::DoState(PointerWrap& p)
287 {
288 p.Do(buffer);
289 }
290
291 void GameFile::DoState(PointerWrap& p)
292 {
293 p.Do(m_valid);
294 p.Do(m_file_path);
295 p.Do(m_file_name);
296
297 p.Do(m_file_size);
298 p.Do(m_volume_size);
299 p.Do(m_volume_size_type);
300 p.Do(m_is_datel_disc);
301 p.Do(m_is_nkit);
302
303 p.Do(m_short_names);
304 p.Do(m_long_names);
305 p.Do(m_short_makers);
306 p.Do(m_long_makers);
307 p.Do(m_descriptions);
308 p.Do(m_internal_name);
309 p.Do(m_game_id);
310 p.Do(m_gametdb_id);
311 p.Do(m_title_id);
312 p.Do(m_maker_id);
313
314 p.Do(m_region);
315 p.Do(m_country);
316 p.Do(m_platform);
317 p.Do(m_blob_type);
318 p.Do(m_block_size);
319 p.Do(m_compression_method);
320 p.Do(m_revision);
321 p.Do(m_disc_number);
322 p.Do(m_is_two_disc_game);
323 p.Do(m_apploader_date);
324
325 p.Do(m_custom_name);
326 p.Do(m_custom_description);
327 p.Do(m_custom_maker);
328 m_volume_banner.DoState(p);
329 m_custom_banner.DoState(p);
330 m_default_cover.DoState(p);
331 m_custom_cover.DoState(p);
332 }
333
334 std::string GameFile::GetExtension() const
335 {
336 std::string extension;
337 SplitPath(m_file_path, nullptr, nullptr, &extension);
338 Common::ToLower(&extension);
339 return extension;
340 }
341
342 bool GameFile::IsElfOrDol() const
343 {
344 const std::string extension = GetExtension();
345 return extension == ".elf" || extension == ".dol";
346 }
347
348 bool GameFile::ReadXMLMetadata(const std::string& path)
349 {
350 std::string data;
351 if (!File::ReadFileToString(path, data))
352 return false;
353
354 pugi::xml_document doc;
355 // We use load_buffer instead of load_file to avoid path encoding problems on Windows
356 if (!doc.load_buffer(data.data(), data.size()))
357 return false;
358
359 const pugi::xml_node app_node = doc.child("app");
360 m_pending.custom_name = app_node.child("name").text().as_string();
361 m_pending.custom_maker = app_node.child("coder").text().as_string();
362 m_pending.custom_description = app_node.child("short_description").text().as_string();
363
364 // Elements that we aren't using:
365 // version (can be written in any format)
366 // release_date (YYYYmmddHHMMSS format)
367 // long_description (can be several screens long!)
368
369 return true;
370 }
371
372 bool GameFile::XMLMetadataChanged()
373 {
374 std::string path, name;
375 SplitPath(m_file_path, &path, &name, nullptr);
376
377 // This XML file naming format is intended as an alternative to the Homebrew Channel naming
378 // for those who don't want to have a Homebrew Channel style folder structure.
379 if (!ReadXMLMetadata(path + name + ".xml"))
380 {
381 // Homebrew Channel naming. Typical for DOLs and ELFs, but we also support it for volumes.
382 if (!ReadXMLMetadata(path + "meta.xml"))
383 {
384 // If no XML metadata is found, remove any old XML metadata from memory.
385 m_pending.custom_banner = {};
386 }
387 }
388
389 return m_pending.custom_name != m_custom_name && m_pending.custom_maker != m_custom_maker &&
390 m_pending.custom_description != m_custom_description;
391 }
392
393 void GameFile::XMLMetadataCommit()
394 {
395 m_custom_name = std::move(m_pending.custom_name);
396 m_custom_description = std::move(m_pending.custom_description);
397 m_custom_maker = std::move(m_pending.custom_maker);
398 }
399
400 bool GameFile::WiiBannerChanged()
401 {
402 // Wii banners can only be read if there is a save file.
403 // In case the cache was created without a save file existing,
404 // let's try reading the save file again, because it might exist now.
405
406 if (!m_volume_banner.empty())
407 return false;
408 if (!DiscIO::IsWii(m_platform))
409 return false;
410
411 m_pending.volume_banner.buffer =
412 DiscIO::WiiSaveBanner(m_title_id)
413 .GetBanner(&m_pending.volume_banner.width, &m_pending.volume_banner.height);
414
415 // We only reach here if the old banner was empty, so if the new banner isn't empty,
416 // the new banner is guaranteed to be different from the old banner
417 return !m_pending.volume_banner.buffer.empty();
418 }
419
420 void GameFile::WiiBannerCommit()
421 {
422 m_volume_banner = std::move(m_pending.volume_banner);
423 }
424
425 bool GameFile::ReadPNGBanner(const std::string& path)
426 {
427 File::IOFile file(path, "rb");
428 if (!file)
429 return false;
430
431 std::vector<u8> png_data(file.GetSize());
432 if (!file.ReadBytes(png_data.data(), png_data.size()))
433 return false;
434
435 GameBanner& banner = m_pending.custom_banner;
436 Common::UniqueBuffer<u8> data_out;
437 if (!Common::LoadPNG(png_data, &data_out, &banner.width, &banner.height))
438 return false;
439
440 // Make an ARGB copy of the RGBA data
441 banner.buffer.resize(data_out.size() / sizeof(u32));
442 for (size_t i = 0; i < banner.buffer.size(); i++)
443 {
444 const size_t j = i * sizeof(u32);
445 banner.buffer[i] = (Common::swap32(data_out.data() + j) >> 8) + (data_out[j] << 24);
446 }
447
448 return true;
449 }
450
451 bool GameFile::TryLoadGameModDescriptorBanner()
452 {
453 if (m_blob_type != DiscIO::BlobType::MOD_DESCRIPTOR)
454 return false;
455
456 auto descriptor = DiscIO::ParseGameModDescriptorFile(m_file_path);
457 if (!descriptor)
458 return false;
459
460 return ReadPNGBanner(descriptor->banner);
461 }
462
463 bool GameFile::CustomBannerChanged()
464 {
465 std::string path, name;
466 SplitPath(m_file_path, &path, &name, nullptr);
467
468 // This icon naming format is intended as an alternative to the Homebrew Channel naming
469 // for those who don't want to have a Homebrew Channel style folder structure.
470 if (!ReadPNGBanner(path + name + ".png"))
471 {
472 // Homebrew Channel icon naming. Typical for DOLs and ELFs, but we also support it for volumes.
473 if (!ReadPNGBanner(path + "icon.png"))
474 {
475 // If it's a game mod descriptor file, it may specify its own custom banner.
476 if (!TryLoadGameModDescriptorBanner())
477 {
478 // If no custom icon is found, go back to the non-custom one.
479 m_pending.custom_banner = {};
480 }
481 }
482 }
483
484 return m_pending.custom_banner != m_custom_banner;
485 }
486
487 void GameFile::CustomBannerCommit()
488 {
489 m_custom_banner = std::move(m_pending.custom_banner);
490 }
491
492 const std::string& GameFile::GetName(const Core::TitleDatabase& title_database) const
493 {
494 if (!m_custom_name.empty())
495 return m_custom_name;
496 if (IsModDescriptor())
497 return GetName(Variant::LongAndPossiblyCustom);
498
499 const std::string& database_name = title_database.GetTitleName(m_gametdb_id, GetConfigLanguage());
500 return database_name.empty() ? GetName(Variant::LongAndPossiblyCustom) : database_name;
501 }
502
503 const std::string& GameFile::GetName(Variant variant) const
504 {
505 if (variant == Variant::LongAndPossiblyCustom && !m_custom_name.empty())
506 return m_custom_name;
507
508 const std::string& name = variant == Variant::ShortAndNotCustom ? GetShortName() : GetLongName();
509 if (!name.empty())
510 return name;
511
512 // No usable name, return filename (better than nothing)
513 return m_file_name;
514 }
515
516 const std::string& GameFile::GetMaker(Variant variant) const
517 {
518 if (variant == Variant::LongAndPossiblyCustom && !m_custom_maker.empty())
519 return m_custom_maker;
520
521 const std::string& maker =
522 variant == Variant::ShortAndNotCustom ? GetShortMaker() : GetLongMaker();
523 if (!maker.empty())
524 return maker;
525
526 if (m_game_id.size() >= 6)
527 return DiscIO::GetCompanyFromID(m_maker_id);
528
529 return EMPTY_STRING;
530 }
531
532 const std::string& GameFile::GetDescription(Variant variant) const
533 {
534 if (variant == Variant::LongAndPossiblyCustom && !m_custom_description.empty())
535 return m_custom_description;
536
537 return LookupUsingConfigLanguage(m_descriptions);
538 }
539
540 std::vector<DiscIO::Language> GameFile::GetLanguages() const
541 {
542 std::vector<DiscIO::Language> languages;
543 // TODO: What if some languages don't have long names but have other strings?
544 for (const auto& name : m_long_names)
545 languages.push_back(name.first);
546 return languages;
547 }
548
549 bool GameFile::CheckIfTwoDiscGame(const std::string& game_id) const
550 {
551 constexpr size_t GAME_ID_PREFIX_SIZE = 3;
552 if (game_id.size() < GAME_ID_PREFIX_SIZE)
553 return false;
554
555 static constexpr std::array<std::string_view, 30> two_disc_game_id_prefixes = {
556 // Resident Evil
557 "DBJ",
558 // The Lord of the Rings: The Third Age
559 "G3A",
560 // Teenage Mutant Ninja Turtles 3: Mutant Nightmare
561 "G3Q",
562 // Resident Evil 4
563 "G4B",
564 // Tiger Woods PGA Tour 2005
565 "G5T",
566 // Resident Evil
567 "GBI",
568 // Resident Evil Zero
569 "GBZ",
570 // Conan
571 "GC9",
572 // Resident Evil Code: Veronica X
573 "GCD",
574 // Tom Clancy's Splinter Cell: Chaos Theory
575 "GCJ",
576 // Freaky Flyers
577 "GFF",
578 // GoldenEye: Rogue Agent
579 "GGI",
580 // Metal Gear Solid: The Twin Snakes
581 "GGS",
582 // Baten Kaitos Origins
583 "GK4",
584 // Killer7
585 "GK7",
586 // Baten Kaitos: Eternal Wings and the Lost Ocean
587 "GKB",
588 // Lupin the 3rd: Lost Treasure by the Sea
589 "GL3",
590 // Enter the Matrix
591 "GMX",
592 // Teenage Mutant Ninja Turtles 2: Battle Nexus
593 "GNI",
594 // GoldenEye: Rogue Agent
595 "GOY",
596 // Tales of Symphonia
597 "GQS",
598 // Medal of Honor: Rising Sun
599 "GR8",
600 "GRZ",
601 // Tales of Symphonia
602 "GTO",
603 // Tiger Woods PGA Tour 2004
604 "GW4",
605 // Tom Clancy's Splinter Cell: Double Agent (GC)
606 "GWY",
607 // Dragon Quest X: Mezameshi Itsutsu no Shuzoku Online
608 "S4M",
609 "S4S",
610 "S6T",
611 "SDQ",
612 };
613 static_assert(std::ranges::is_sorted(two_disc_game_id_prefixes));
614
615 std::string_view game_id_prefix(game_id.data(), GAME_ID_PREFIX_SIZE);
616 return std::ranges::binary_search(two_disc_game_id_prefixes, game_id_prefix);
617 }
618
619 std::string GameFile::GetNetPlayName(const Core::TitleDatabase& title_database) const
620 {
621 std::vector<std::string> info;
622 if (!GetGameID().empty())
623 info.push_back(GetGameID());
624 if (GetRevision() != 0)
625 info.push_back("Revision " + std::to_string(GetRevision()));
626
627 const std::string name = GetName(title_database);
628
629 int disc_number = GetDiscNumber() + 1;
630
631 std::string lower_name = name;
632 Common::ToLower(&lower_name);
633 if (disc_number > 1 &&
634 lower_name.find(fmt::format("disc {}", disc_number)) == std::string::npos &&
635 lower_name.find(fmt::format("disc{}", disc_number)) == std::string::npos)
636 {
637 std::string disc_text = "Disc ";
638 info.push_back(disc_text + std::to_string(disc_number));
639 }
640 if (info.empty())
641 return name;
642 return fmt::format("{} ({})", name, fmt::join(info, ", "));
643 }
644
645 static Common::SHA1::Digest GetHash(u32 value)
646 {
647 auto data = Common::BitCastToArray<u8>(value);
648 return Common::SHA1::CalculateDigest(data);
649 }
650
651 static Common::SHA1::Digest GetHash(std::string_view str)
652 {
653 return Common::SHA1::CalculateDigest(str);
654 }
655
656 static std::optional<Common::SHA1::Digest> GetFileHash(const std::string& path)
657 {
658 std::string buffer;
659 if (!File::ReadFileToString(path, buffer))
660 return std::nullopt;
661 return GetHash(buffer);
662 }
663
664 static std::optional<Common::SHA1::Digest> MixHash(const std::optional<Common::SHA1::Digest>& lhs,
665 const std::optional<Common::SHA1::Digest>& rhs)
666 {
667 if (!lhs && !rhs)
668 return std::nullopt;
669 if (!lhs || !rhs)
670 return !rhs ? lhs : rhs;
671 Common::SHA1::Digest result;
672 for (size_t i = 0; i < result.size(); ++i)
673 result[i] = (*lhs)[i] ^ (*rhs)[(i + 1) % result.size()];
674 return result;
675 }
676
677 Common::SHA1::Digest GameFile::GetSyncHash() const
678 {
679 std::optional<Common::SHA1::Digest> hash;
680
681 if (m_platform == DiscIO::Platform::ELFOrDOL)
682 {
683 hash = GetFileHash(m_file_path);
684 }
685 else if (m_blob_type == DiscIO::BlobType::MOD_DESCRIPTOR)
686 {
687 auto descriptor = DiscIO::ParseGameModDescriptorFile(m_file_path);
688 if (descriptor)
689 {
690 GameFile proxy(descriptor->base_file);
691 if (proxy.IsValid())
692 hash = proxy.GetSyncHash();
693
694 // add patches to hash if they're enabled
695 if (descriptor->riivolution)
696 {
697 for (const auto& patch : descriptor->riivolution->patches)
698 {
699 hash = MixHash(hash, GetFileHash(patch.xml));
700 for (const auto& option : patch.options)
701 {
702 hash = MixHash(hash, GetHash(option.section_name));
703 hash = MixHash(hash, GetHash(option.option_id));
704 hash = MixHash(hash, GetHash(option.option_name));
705 hash = MixHash(hash, GetHash(option.choice));
706 }
707 }
708 }
709 }
710 }
711 else
712 {
713 if (std::unique_ptr<DiscIO::Volume> volume = DiscIO::CreateVolume(m_file_path))
714 hash = volume->GetSyncHash();
715 }
716
717 return hash.value_or(Common::SHA1::Digest{});
718 }
719
720 NetPlay::SyncIdentifier GameFile::GetSyncIdentifier() const
721 {
722 const u64 dol_elf_size = m_platform == DiscIO::Platform::ELFOrDOL ? m_file_size : 0;
723 return NetPlay::SyncIdentifier{dol_elf_size, m_game_id, m_revision,
724 m_disc_number, m_is_datel_disc, GetSyncHash()};
725 }
726
727 NetPlay::SyncIdentifierComparison
728 GameFile::CompareSyncIdentifier(const NetPlay::SyncIdentifier& sync_identifier) const
729 {
730 const bool is_elf_or_dol = m_platform == DiscIO::Platform::ELFOrDOL;
731 if ((is_elf_or_dol ? m_file_size : 0) != sync_identifier.dol_elf_size)
732 return NetPlay::SyncIdentifierComparison::DifferentGame;
733
734 if (m_is_datel_disc != sync_identifier.is_datel)
735 return NetPlay::SyncIdentifierComparison::DifferentGame;
736
737 if (m_game_id.size() != sync_identifier.game_id.size())
738 return NetPlay::SyncIdentifierComparison::DifferentGame;
739
740 if (!is_elf_or_dol && !m_is_datel_disc && m_game_id.size() >= 4 && m_game_id.size() <= 6)
741 {
742 // This is a game ID, following specific rules which we can use to give clearer information to
743 // the user.
744
745 // If the first 3 characters don't match, then these are probably different games.
746 // (There are exceptions; in particular Japanese-region games sometimes use a different ID;
747 // for instance, GOYE69, GOYP69, and GGIJ13 are used by GoldenEye: Rogue Agent.)
748 if (std::string_view(&m_game_id[0], 3) != std::string_view(&sync_identifier.game_id[0], 3))
749 return NetPlay::SyncIdentifierComparison::DifferentGame;
750
751 // If the first 3 characters match but the region doesn't match, reject it as such.
752 if (m_game_id[3] != sync_identifier.game_id[3])
753 return NetPlay::SyncIdentifierComparison::DifferentRegion;
754
755 // If the maker code is present, and doesn't match between the two but the main ID does,
756 // these might be different revisions of the same game (e.g. a rerelease with a different
757 // publisher), which we should treat as a different revision.
758 if (std::string_view(&m_game_id[4]) != std::string_view(&sync_identifier.game_id[4]))
759 return NetPlay::SyncIdentifierComparison::DifferentRevision;
760 }
761 else
762 {
763 // Numeric title ID or another generated ID that does not follow the rules above
764 if (m_game_id != sync_identifier.game_id)
765 return NetPlay::SyncIdentifierComparison::DifferentGame;
766 }
767
768 if (m_revision != sync_identifier.revision)
769 return NetPlay::SyncIdentifierComparison::DifferentRevision;
770
771 if (m_disc_number != sync_identifier.disc_number)
772 return NetPlay::SyncIdentifierComparison::DifferentDiscNumber;
773
774 if (GetSyncHash() != sync_identifier.sync_hash)
775 {
776 // Most Datel titles re-use the same game ID (GNHE5d, with that lowercase D, or DTLX01).
777 // So if the hash differs, then it's probably a different game even if the game ID matches.
778 if (m_is_datel_disc)
779 return NetPlay::SyncIdentifierComparison::DifferentGame;
780 else
781 return NetPlay::SyncIdentifierComparison::DifferentHash;
782 }
783
784 return NetPlay::SyncIdentifierComparison::SameGame;
785 }
786
787 std::string GameFile::GetWiiFSPath() const
788 {
789 ASSERT(DiscIO::IsWii(m_platform));
790 return Common::GetTitleDataPath(m_title_id, Common::FromWhichRoot::Configured);
791 }
792
793 bool GameFile::ShouldShowFileFormatDetails() const
794 {
795 switch (m_blob_type)
796 {
797 case DiscIO::BlobType::PLAIN:
798 break;
799 case DiscIO::BlobType::DRIVE:
800 case DiscIO::BlobType::MOD_DESCRIPTOR:
801 return false;
802 default:
803 return true;
804 }
805
806 switch (m_platform)
807 {
808 case DiscIO::Platform::WiiWAD:
809 return false;
810 case DiscIO::Platform::ELFOrDOL:
811 return false;
812 default:
813 return true;
814 }
815 }
816
817 std::string GameFile::GetFileFormatName() const
818 {
819 switch (m_platform)
820 {
821 case DiscIO::Platform::WiiWAD:
822 return "WAD";
823
824 case DiscIO::Platform::ELFOrDOL:
825 {
826 std::string extension = GetExtension();
827 Common::ToUpper(&extension);
828
829 // substr removes the dot
830 return extension.substr(std::min<size_t>(1, extension.size()));
831 }
832
833 default:
834 {
835 std::string name = DiscIO::GetName(m_blob_type, true);
836 if (m_is_nkit)
837 return Common::FmtFormatT("{0} (NKit)", name);
838 return name;
839 }
840 }
841 }
842
843 bool GameFile::ShouldAllowConversion() const
844 {
845 return DiscIO::IsDisc(m_platform) && m_volume_size_type == DiscIO::DataSizeType::Accurate;
846 }
847
848 bool GameFile::IsModDescriptor() const
849 {
850 return m_blob_type == DiscIO::BlobType::MOD_DESCRIPTOR;
851 }
852
853 const GameBanner& GameFile::GetBannerImage() const
854 {
855 return m_custom_banner.empty() ? m_volume_banner : m_custom_banner;
856 }
857
858 const GameCover& GameFile::GetCoverImage() const
859 {
860 return m_custom_cover.empty() ? m_default_cover : m_custom_cover;
861 }
862
863 } // namespace UICommon