savedata_factory.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/common_types.h"
  6. #include "common/logging/log.h"
  7. #include "core/core.h"
  8. #include "core/file_sys/disk_filesystem.h"
  9. #include "core/file_sys/savedata_factory.h"
  10. #include "core/hle/kernel/process.h"
  11. namespace FileSys {
  12. std::string SaveDataDescriptor::DebugInfo() {
  13. return fmt::format("[type={:02X}, title_id={:016X}, user_id={:016X}{:016X}, save_id={:016X}]",
  14. static_cast<u8>(type), title_id, user_id[1], user_id[0], save_id);
  15. }
  16. SaveDataFactory::SaveDataFactory(std::string nand_directory)
  17. : nand_directory(std::move(nand_directory)) {}
  18. ResultVal<std::unique_ptr<FileSystemBackend>> SaveDataFactory::Open(SaveDataSpaceId space,
  19. SaveDataDescriptor meta) {
  20. if (meta.type == SaveDataType::SystemSaveData || meta.type == SaveDataType::SaveData) {
  21. if (meta.zero_1 != 0) {
  22. LOG_WARNING(Service_FS,
  23. "Possibly incorrect SaveDataDescriptor, type is "
  24. "SystemSaveData||SaveData but offset 0x28 is non-zero ({:016X}).",
  25. meta.zero_1);
  26. }
  27. if (meta.zero_2 != 0) {
  28. LOG_WARNING(Service_FS,
  29. "Possibly incorrect SaveDataDescriptor, type is "
  30. "SystemSaveData||SaveData but offset 0x30 is non-zero ({:016X}).",
  31. meta.zero_2);
  32. }
  33. if (meta.zero_3 != 0) {
  34. LOG_WARNING(Service_FS,
  35. "Possibly incorrect SaveDataDescriptor, type is "
  36. "SystemSaveData||SaveData but offset 0x38 is non-zero ({:016X}).",
  37. meta.zero_3);
  38. }
  39. }
  40. if (meta.type == SaveDataType::SystemSaveData && meta.title_id != 0) {
  41. LOG_WARNING(Service_FS,
  42. "Possibly incorrect SaveDataDescriptor, type is SystemSaveData but title_id is "
  43. "non-zero ({:016X}).",
  44. meta.title_id);
  45. }
  46. std::string save_directory =
  47. GetFullPath(space, meta.type, meta.title_id, meta.user_id, meta.save_id);
  48. // TODO(DarkLordZach): Try to not create when opening, there are dedicated create save methods.
  49. // But, user_ids don't match so this works for now.
  50. if (!FileUtil::Exists(save_directory)) {
  51. // TODO(bunnei): This is a work-around to always create a save data directory if it does not
  52. // already exist. This is a hack, as we do not understand yet how this works on hardware.
  53. // Without a save data directory, many games will assert on boot. This should not have any
  54. // bad side-effects.
  55. FileUtil::CreateFullPath(save_directory);
  56. }
  57. // TODO(DarkLordZach): For some reason, CreateFullPath doesn't create the last bit. Should be
  58. // fixed with VFS.
  59. if (!FileUtil::IsDirectory(save_directory)) {
  60. FileUtil::CreateDir(save_directory);
  61. }
  62. // Return an error if the save data doesn't actually exist.
  63. if (!FileUtil::IsDirectory(save_directory)) {
  64. // TODO(Subv): Find out correct error code.
  65. return ResultCode(-1);
  66. }
  67. auto archive = std::make_unique<Disk_FileSystem>(save_directory);
  68. return MakeResult<std::unique_ptr<FileSystemBackend>>(std::move(archive));
  69. }
  70. std::string SaveDataFactory::GetFullPath(SaveDataSpaceId space, SaveDataType type, u64 title_id,
  71. u128 user_id, u64 save_id) const {
  72. // According to switchbrew, if a save is of type SaveData and the title id field is 0, it should
  73. // be interpreted as the title id of the current process.
  74. if (type == SaveDataType::SaveData && title_id == 0)
  75. title_id = Core::CurrentProcess()->program_id;
  76. std::string prefix;
  77. switch (space) {
  78. case SaveDataSpaceId::NandSystem:
  79. prefix = nand_directory + "system/save/";
  80. break;
  81. case SaveDataSpaceId::NandUser:
  82. prefix = nand_directory + "user/save/";
  83. break;
  84. default:
  85. ASSERT_MSG(false, "Unrecognized SaveDataSpaceId: {:02X}", static_cast<u8>(space));
  86. }
  87. switch (type) {
  88. case SaveDataType::SystemSaveData:
  89. return fmt::format("{}{:016X}/{:016X}{:016X}", prefix, save_id, user_id[1], user_id[0]);
  90. case SaveDataType::SaveData:
  91. return fmt::format("{}{:016X}/{:016X}{:016X}/{:016X}", prefix, 0, user_id[1], user_id[0],
  92. title_id);
  93. default:
  94. ASSERT_MSG(false, "Unrecognized SaveDataType: {:02X}", static_cast<u8>(type));
  95. }
  96. }
  97. } // namespace FileSys