patch_manager.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <array>
  5. #include <cstddef>
  6. #include "common/logging/log.h"
  7. #include "core/file_sys/content_archive.h"
  8. #include "core/file_sys/control_metadata.h"
  9. #include "core/file_sys/patch_manager.h"
  10. #include "core/file_sys/registered_cache.h"
  11. #include "core/file_sys/romfs.h"
  12. #include "core/hle/service/filesystem/filesystem.h"
  13. #include "core/loader/loader.h"
  14. namespace FileSys {
  15. constexpr u64 SINGLE_BYTE_MODULUS = 0x100;
  16. std::string FormatTitleVersion(u32 version, TitleVersionFormat format) {
  17. std::array<u8, sizeof(u32)> bytes{};
  18. bytes[0] = version % SINGLE_BYTE_MODULUS;
  19. for (std::size_t i = 1; i < bytes.size(); ++i) {
  20. version /= SINGLE_BYTE_MODULUS;
  21. bytes[i] = version % SINGLE_BYTE_MODULUS;
  22. }
  23. if (format == TitleVersionFormat::FourElements)
  24. return fmt::format("v{}.{}.{}.{}", bytes[3], bytes[2], bytes[1], bytes[0]);
  25. return fmt::format("v{}.{}.{}", bytes[3], bytes[2], bytes[1]);
  26. }
  27. constexpr std::array<const char*, 1> PATCH_TYPE_NAMES{
  28. "Update",
  29. };
  30. std::string FormatPatchTypeName(PatchType type) {
  31. return PATCH_TYPE_NAMES.at(static_cast<std::size_t>(type));
  32. }
  33. PatchManager::PatchManager(u64 title_id) : title_id(title_id) {}
  34. PatchManager::~PatchManager() = default;
  35. VirtualDir PatchManager::PatchExeFS(VirtualDir exefs) const {
  36. LOG_INFO(Loader, "Patching ExeFS for title_id={:016X}", title_id);
  37. if (exefs == nullptr)
  38. return exefs;
  39. const auto installed = Service::FileSystem::GetUnionContents();
  40. // Game Updates
  41. const auto update_tid = GetUpdateTitleID(title_id);
  42. const auto update = installed->GetEntry(update_tid, ContentRecordType::Program);
  43. if (update != nullptr) {
  44. if (update->GetStatus() == Loader::ResultStatus::ErrorMissingBKTRBaseRomFS &&
  45. update->GetExeFS() != nullptr) {
  46. LOG_INFO(Loader, " ExeFS: Update ({}) applied successfully",
  47. FormatTitleVersion(installed->GetEntryVersion(update_tid).get_value_or(0)));
  48. exefs = update->GetExeFS();
  49. }
  50. }
  51. return exefs;
  52. }
  53. VirtualFile PatchManager::PatchRomFS(VirtualFile romfs, u64 ivfc_offset,
  54. ContentRecordType type) const {
  55. LOG_INFO(Loader, "Patching RomFS for title_id={:016X}, type={:02X}", title_id,
  56. static_cast<u8>(type));
  57. if (romfs == nullptr)
  58. return romfs;
  59. const auto installed = Service::FileSystem::GetUnionContents();
  60. // Game Updates
  61. const auto update_tid = GetUpdateTitleID(title_id);
  62. const auto update = installed->GetEntryRaw(update_tid, type);
  63. if (update != nullptr) {
  64. const auto new_nca = std::make_shared<NCA>(update, romfs, ivfc_offset);
  65. if (new_nca->GetStatus() == Loader::ResultStatus::Success &&
  66. new_nca->GetRomFS() != nullptr) {
  67. LOG_INFO(Loader, " RomFS: Update ({}) applied successfully",
  68. FormatTitleVersion(installed->GetEntryVersion(update_tid).get_value_or(0)));
  69. romfs = new_nca->GetRomFS();
  70. }
  71. }
  72. return romfs;
  73. }
  74. std::map<PatchType, std::string> PatchManager::GetPatchVersionNames() const {
  75. std::map<PatchType, std::string> out;
  76. const auto installed = Service::FileSystem::GetUnionContents();
  77. const auto update_tid = GetUpdateTitleID(title_id);
  78. PatchManager update{update_tid};
  79. auto [nacp, discard_icon_file] = update.GetControlMetadata();
  80. if (nacp != nullptr) {
  81. out[PatchType::Update] = nacp->GetVersionString();
  82. } else {
  83. if (installed->HasEntry(update_tid, ContentRecordType::Program)) {
  84. const auto meta_ver = installed->GetEntryVersion(update_tid);
  85. if (meta_ver == boost::none || meta_ver.get() == 0) {
  86. out[PatchType::Update] = "";
  87. } else {
  88. out[PatchType::Update] =
  89. FormatTitleVersion(meta_ver.get(), TitleVersionFormat::ThreeElements);
  90. }
  91. }
  92. }
  93. return out;
  94. }
  95. std::pair<std::shared_ptr<NACP>, VirtualFile> PatchManager::GetControlMetadata() const {
  96. const auto& installed{Service::FileSystem::GetUnionContents()};
  97. const auto base_control_nca = installed->GetEntry(title_id, ContentRecordType::Control);
  98. if (base_control_nca == nullptr)
  99. return {};
  100. return ParseControlNCA(base_control_nca);
  101. }
  102. std::pair<std::shared_ptr<NACP>, VirtualFile> PatchManager::ParseControlNCA(
  103. const std::shared_ptr<NCA>& nca) const {
  104. const auto base_romfs = nca->GetRomFS();
  105. if (base_romfs == nullptr)
  106. return {};
  107. const auto romfs = PatchRomFS(base_romfs, nca->GetBaseIVFCOffset(), ContentRecordType::Control);
  108. if (romfs == nullptr)
  109. return {};
  110. const auto extracted = ExtractRomFS(romfs);
  111. if (extracted == nullptr)
  112. return {};
  113. auto nacp_file = extracted->GetFile("control.nacp");
  114. if (nacp_file == nullptr)
  115. nacp_file = extracted->GetFile("Control.nacp");
  116. const auto nacp = nacp_file == nullptr ? nullptr : std::make_shared<NACP>(nacp_file);
  117. VirtualFile icon_file;
  118. for (const auto& language : FileSys::LANGUAGE_NAMES) {
  119. icon_file = extracted->GetFile("icon_" + std::string(language) + ".dat");
  120. if (icon_file != nullptr)
  121. break;
  122. }
  123. return {nacp, icon_file};
  124. }
  125. } // namespace FileSys