romfs_factory.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <memory>
  5. #include "common/assert.h"
  6. #include "common/common_types.h"
  7. #include "common/logging/log.h"
  8. #include "core/core.h"
  9. #include "core/file_sys/card_image.h"
  10. #include "core/file_sys/content_archive.h"
  11. #include "core/file_sys/nca_metadata.h"
  12. #include "core/file_sys/patch_manager.h"
  13. #include "core/file_sys/registered_cache.h"
  14. #include "core/file_sys/romfs_factory.h"
  15. #include "core/hle/kernel/process.h"
  16. #include "core/hle/service/filesystem/filesystem.h"
  17. #include "core/loader/loader.h"
  18. namespace FileSys {
  19. RomFSFactory::RomFSFactory(Loader::AppLoader& app_loader) {
  20. // Load the RomFS from the app
  21. if (app_loader.ReadRomFS(file) != Loader::ResultStatus::Success) {
  22. LOG_ERROR(Service_FS, "Unable to read RomFS!");
  23. }
  24. updatable = app_loader.IsRomFSUpdatable();
  25. ivfc_offset = app_loader.ReadRomFSIVFCOffset();
  26. }
  27. RomFSFactory::~RomFSFactory() = default;
  28. void RomFSFactory::SetPackedUpdate(VirtualFile update_raw) {
  29. this->update_raw = std::move(update_raw);
  30. }
  31. ResultVal<VirtualFile> RomFSFactory::OpenCurrentProcess(u64 current_process_title_id) const {
  32. if (!updatable)
  33. return MakeResult<VirtualFile>(file);
  34. const PatchManager patch_manager(current_process_title_id);
  35. return MakeResult<VirtualFile>(
  36. patch_manager.PatchRomFS(file, ivfc_offset, ContentRecordType::Program, update_raw));
  37. }
  38. ResultVal<VirtualFile> RomFSFactory::Open(u64 title_id, StorageId storage,
  39. ContentRecordType type) const {
  40. std::shared_ptr<NCA> res;
  41. switch (storage) {
  42. case StorageId::None:
  43. res = Core::System::GetInstance().GetContentProvider().GetEntry(title_id, type);
  44. break;
  45. case StorageId::NandSystem:
  46. res =
  47. Core::System::GetInstance().GetFileSystemController().GetSystemNANDContents()->GetEntry(
  48. title_id, type);
  49. break;
  50. case StorageId::NandUser:
  51. res = Core::System::GetInstance().GetFileSystemController().GetUserNANDContents()->GetEntry(
  52. title_id, type);
  53. break;
  54. case StorageId::SdCard:
  55. res = Core::System::GetInstance().GetFileSystemController().GetSDMCContents()->GetEntry(
  56. title_id, type);
  57. break;
  58. default:
  59. UNIMPLEMENTED_MSG("Unimplemented storage_id={:02X}", static_cast<u8>(storage));
  60. }
  61. if (res == nullptr) {
  62. // TODO(DarkLordZach): Find the right error code to use here
  63. return RESULT_UNKNOWN;
  64. }
  65. const auto romfs = res->GetRomFS();
  66. if (romfs == nullptr) {
  67. // TODO(DarkLordZach): Find the right error code to use here
  68. return RESULT_UNKNOWN;
  69. }
  70. return MakeResult<VirtualFile>(romfs);
  71. }
  72. } // namespace FileSys