patch_manager.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. VirtualDir PatchManager::PatchExeFS(VirtualDir exefs) const {
  35. LOG_INFO(Loader, "Patching ExeFS for title_id={:016X}", title_id);
  36. if (exefs == nullptr)
  37. return exefs;
  38. const auto installed = Service::FileSystem::GetUnionContents();
  39. // Game Updates
  40. const auto update_tid = GetUpdateTitleID(title_id);
  41. const auto update = installed->GetEntry(update_tid, ContentRecordType::Program);
  42. if (update != nullptr) {
  43. if (update->GetStatus() == Loader::ResultStatus::ErrorMissingBKTRBaseRomFS &&
  44. update->GetExeFS() != nullptr) {
  45. LOG_INFO(Loader, " ExeFS: Update ({}) applied successfully",
  46. FormatTitleVersion(installed->GetEntryVersion(update_tid).get_value_or(0)));
  47. exefs = update->GetExeFS();
  48. }
  49. }
  50. return exefs;
  51. }
  52. VirtualFile PatchManager::PatchRomFS(VirtualFile romfs, u64 ivfc_offset,
  53. ContentRecordType type) const {
  54. LOG_INFO(Loader, "Patching RomFS for title_id={:016X}, type={:02X}", title_id,
  55. static_cast<u8>(type));
  56. if (romfs == nullptr)
  57. return romfs;
  58. const auto installed = Service::FileSystem::GetUnionContents();
  59. // Game Updates
  60. const auto update_tid = GetUpdateTitleID(title_id);
  61. const auto update = installed->GetEntryRaw(update_tid, type);
  62. if (update != nullptr) {
  63. const auto new_nca = std::make_shared<NCA>(update, romfs, ivfc_offset);
  64. if (new_nca->GetStatus() == Loader::ResultStatus::Success &&
  65. new_nca->GetRomFS() != nullptr) {
  66. LOG_INFO(Loader, " RomFS: Update ({}) applied successfully",
  67. FormatTitleVersion(installed->GetEntryVersion(update_tid).get_value_or(0)));
  68. romfs = new_nca->GetRomFS();
  69. }
  70. }
  71. return romfs;
  72. }
  73. std::map<PatchType, std::string> PatchManager::GetPatchVersionNames() const {
  74. std::map<PatchType, std::string> out;
  75. const auto installed = Service::FileSystem::GetUnionContents();
  76. const auto update_tid = GetUpdateTitleID(title_id);
  77. PatchManager update{update_tid};
  78. auto [nacp, discard_icon_file] = update.GetControlMetadata();
  79. if (nacp != nullptr) {
  80. out[PatchType::Update] = nacp->GetVersionString();
  81. } else {
  82. if (installed->HasEntry(update_tid, ContentRecordType::Program)) {
  83. const auto meta_ver = installed->GetEntryVersion(update_tid);
  84. if (meta_ver == boost::none || meta_ver.get() == 0) {
  85. out[PatchType::Update] = "";
  86. } else {
  87. out[PatchType::Update] =
  88. FormatTitleVersion(meta_ver.get(), TitleVersionFormat::ThreeElements);
  89. }
  90. }
  91. }
  92. return out;
  93. }
  94. std::pair<std::shared_ptr<NACP>, VirtualFile> PatchManager::GetControlMetadata() const {
  95. const auto& installed{Service::FileSystem::GetUnionContents()};
  96. const auto base_control_nca = installed->GetEntry(title_id, ContentRecordType::Control);
  97. if (base_control_nca == nullptr)
  98. return {};
  99. return ParseControlNCA(base_control_nca);
  100. }
  101. std::pair<std::shared_ptr<NACP>, VirtualFile> PatchManager::ParseControlNCA(
  102. const std::shared_ptr<NCA>& nca) const {
  103. const auto base_romfs = nca->GetRomFS();
  104. if (base_romfs == nullptr)
  105. return {};
  106. const auto romfs = PatchRomFS(base_romfs, nca->GetBaseIVFCOffset(), ContentRecordType::Control);
  107. if (romfs == nullptr)
  108. return {};
  109. const auto extracted = ExtractRomFS(romfs);
  110. if (extracted == nullptr)
  111. return {};
  112. auto nacp_file = extracted->GetFile("control.nacp");
  113. if (nacp_file == nullptr)
  114. nacp_file = extracted->GetFile("Control.nacp");
  115. const auto nacp = nacp_file == nullptr ? nullptr : std::make_shared<NACP>(nacp_file);
  116. VirtualFile icon_file;
  117. for (const auto& language : FileSys::LANGUAGE_NAMES) {
  118. icon_file = extracted->GetFile("icon_" + std::string(language) + ".dat");
  119. if (icon_file != nullptr)
  120. break;
  121. }
  122. return {nacp, icon_file};
  123. }
  124. } // namespace FileSys