patch_manager.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "core/file_sys/patch_manager.h"
  5. #include "core/file_sys/registered_cache.h"
  6. #include "core/hle/service/filesystem/filesystem.h"
  7. namespace FileSys {
  8. constexpr u64 SINGLE_BYTE_MODULUS = 0x100;
  9. std::string FormatTitleVersion(u32 version, TitleVersionFormat format) {
  10. std::array<u8, sizeof(u32)> bytes{};
  11. bytes[0] = version % SINGLE_BYTE_MODULUS;
  12. for (size_t i = 1; i < bytes.size(); ++i) {
  13. version /= SINGLE_BYTE_MODULUS;
  14. bytes[i] = version % SINGLE_BYTE_MODULUS;
  15. }
  16. if (format == TitleVersionFormat::FourElements)
  17. return fmt::format("v{}.{}.{}.{}", bytes[3], bytes[2], bytes[1], bytes[0]);
  18. return fmt::format("v{}.{}.{}", bytes[3], bytes[2], bytes[1]);
  19. }
  20. constexpr std::array<const char*, 1> PATCH_TYPE_NAMES{
  21. "Update",
  22. };
  23. std::string FormatPatchTypeName(PatchType type) {
  24. return PATCH_TYPE_NAMES.at(static_cast<size_t>(type));
  25. }
  26. PatchManager::PatchManager(u64 title_id) : title_id(title_id) {}
  27. VirtualDir PatchManager::PatchExeFS(VirtualDir exefs) const {
  28. if (exefs == nullptr)
  29. return exefs;
  30. const auto installed = Service::FileSystem::GetUnionContents();
  31. // Game Updates
  32. const auto update_tid = GetUpdateTitleID(title_id);
  33. const auto update = installed->GetEntry(update_tid, ContentRecordType::Program);
  34. if (update != nullptr) {
  35. if (update->GetStatus() == Loader::ResultStatus::ErrorMissingBKTRBaseRomFS &&
  36. update->GetExeFS() != nullptr) {
  37. exefs = update->GetExeFS();
  38. }
  39. }
  40. return exefs;
  41. }
  42. VirtualFile PatchManager::PatchRomFS(VirtualFile romfs) const {
  43. if (romfs == nullptr)
  44. return romfs;
  45. const auto installed = Service::FileSystem::GetUnionContents();
  46. // Game Updates
  47. const auto update_tid = GetUpdateTitleID(title_id);
  48. const auto update = installed->GetEntryRaw(update_tid, ContentRecordType::Program);
  49. if (update != nullptr) {
  50. const auto nca = std::make_shared<NCA>(update, romfs);
  51. if (nca->GetStatus() == Loader::ResultStatus::Success && nca->GetRomFS() != nullptr)
  52. romfs = nca->GetRomFS();
  53. }
  54. return romfs;
  55. }
  56. std::map<PatchType, u32> PatchManager::GetPatchVersionNames() const {
  57. std::map<PatchType, u32> out;
  58. const auto installed = Service::FileSystem::GetUnionContents();
  59. const auto update_tid = GetUpdateTitleID(title_id);
  60. const auto update_version = installed->GetEntryVersion(update_tid);
  61. if (update_version != boost::none &&
  62. installed->HasEntry(update_tid, ContentRecordType::Program)) {
  63. out[PatchType::Update] = update_version.get();
  64. }
  65. return out;
  66. }
  67. } // namespace FileSys