savedata_factory.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <memory>
  4. #include "common/assert.h"
  5. #include "common/common_types.h"
  6. #include "common/logging/log.h"
  7. #include "common/uuid.h"
  8. #include "core/core.h"
  9. #include "core/file_sys/savedata_factory.h"
  10. #include "core/file_sys/vfs/vfs.h"
  11. namespace FileSys {
  12. namespace {
  13. bool ShouldSaveDataBeAutomaticallyCreated(SaveDataSpaceId space, const SaveDataAttribute& attr) {
  14. return attr.type == SaveDataType::Cache || attr.type == SaveDataType::Temporary ||
  15. (space == SaveDataSpaceId::User && ///< Normal Save Data -- Current Title & User
  16. (attr.type == SaveDataType::Account || attr.type == SaveDataType::Device) &&
  17. attr.program_id == 0 && attr.system_save_data_id == 0);
  18. }
  19. std::string GetFutureSaveDataPath(SaveDataSpaceId space_id, SaveDataType type, u64 title_id,
  20. u128 user_id) {
  21. // Only detect nand user saves.
  22. const auto space_id_path = [space_id]() -> std::string_view {
  23. switch (space_id) {
  24. case SaveDataSpaceId::User:
  25. return "/user/save";
  26. default:
  27. return "";
  28. }
  29. }();
  30. if (space_id_path.empty()) {
  31. return "";
  32. }
  33. Common::UUID uuid;
  34. std::memcpy(uuid.uuid.data(), user_id.data(), sizeof(Common::UUID));
  35. // Only detect account/device saves from the future location.
  36. switch (type) {
  37. case SaveDataType::Account:
  38. return fmt::format("{}/account/{}/{:016X}/0", space_id_path, uuid.RawString(), title_id);
  39. case SaveDataType::Device:
  40. return fmt::format("{}/device/{:016X}/0", space_id_path, title_id);
  41. default:
  42. return "";
  43. }
  44. }
  45. } // Anonymous namespace
  46. SaveDataFactory::SaveDataFactory(Core::System& system_, ProgramId program_id_,
  47. VirtualDir save_directory_)
  48. : system{system_}, program_id{program_id_}, dir{std::move(save_directory_)} {
  49. // Delete all temporary storages
  50. // On hardware, it is expected that temporary storage be empty at first use.
  51. dir->DeleteSubdirectoryRecursive("temp");
  52. }
  53. SaveDataFactory::~SaveDataFactory() = default;
  54. VirtualDir SaveDataFactory::Create(SaveDataSpaceId space, const SaveDataAttribute& meta) const {
  55. const auto save_directory = GetFullPath(program_id, dir, space, meta.type, meta.program_id,
  56. meta.user_id, meta.system_save_data_id);
  57. return dir->CreateDirectoryRelative(save_directory);
  58. }
  59. VirtualDir SaveDataFactory::Open(SaveDataSpaceId space, const SaveDataAttribute& meta) const {
  60. const auto save_directory = GetFullPath(program_id, dir, space, meta.type, meta.program_id,
  61. meta.user_id, meta.system_save_data_id);
  62. auto out = dir->GetDirectoryRelative(save_directory);
  63. if (out == nullptr && (ShouldSaveDataBeAutomaticallyCreated(space, meta) && auto_create)) {
  64. return Create(space, meta);
  65. }
  66. return out;
  67. }
  68. VirtualDir SaveDataFactory::GetSaveDataSpaceDirectory(SaveDataSpaceId space) const {
  69. return dir->GetDirectoryRelative(GetSaveDataSpaceIdPath(space));
  70. }
  71. std::string SaveDataFactory::GetSaveDataSpaceIdPath(SaveDataSpaceId space) {
  72. switch (space) {
  73. case SaveDataSpaceId::System:
  74. return "/system/";
  75. case SaveDataSpaceId::User:
  76. return "/user/";
  77. case SaveDataSpaceId::Temporary:
  78. return "/temp/";
  79. default:
  80. ASSERT_MSG(false, "Unrecognized SaveDataSpaceId: {:02X}", static_cast<u8>(space));
  81. return "/unrecognized/"; ///< To prevent corruption when ignoring asserts.
  82. }
  83. }
  84. std::string SaveDataFactory::GetFullPath(ProgramId program_id, VirtualDir dir,
  85. SaveDataSpaceId space, SaveDataType type, u64 title_id,
  86. u128 user_id, u64 save_id) {
  87. // According to switchbrew, if a save is of type SaveData and the title id field is 0, it should
  88. // be interpreted as the title id of the current process.
  89. if (type == SaveDataType::Account || type == SaveDataType::Device) {
  90. if (title_id == 0) {
  91. title_id = program_id;
  92. }
  93. }
  94. // For compat with a future impl.
  95. if (std::string future_path =
  96. GetFutureSaveDataPath(space, type, title_id & ~(0xFFULL), user_id);
  97. !future_path.empty()) {
  98. // Check if this location exists, and prefer it over the old.
  99. if (const auto future_dir = dir->GetDirectoryRelative(future_path); future_dir != nullptr) {
  100. LOG_INFO(Service_FS, "Using save at new location: {}", future_path);
  101. return future_path;
  102. }
  103. }
  104. std::string out = GetSaveDataSpaceIdPath(space);
  105. switch (type) {
  106. case SaveDataType::System:
  107. return fmt::format("{}save/{:016X}/{:016X}{:016X}", out, save_id, user_id[1], user_id[0]);
  108. case SaveDataType::Account:
  109. case SaveDataType::Device:
  110. return fmt::format("{}save/{:016X}/{:016X}{:016X}/{:016X}", out, 0, user_id[1], user_id[0],
  111. title_id);
  112. case SaveDataType::Temporary:
  113. return fmt::format("{}{:016X}/{:016X}{:016X}/{:016X}", out, 0, user_id[1], user_id[0],
  114. title_id);
  115. case SaveDataType::Cache:
  116. return fmt::format("{}save/cache/{:016X}", out, title_id);
  117. default:
  118. ASSERT_MSG(false, "Unrecognized SaveDataType: {:02X}", static_cast<u8>(type));
  119. return fmt::format("{}save/unknown_{:X}/{:016X}", out, static_cast<u8>(type), title_id);
  120. }
  121. }
  122. std::string SaveDataFactory::GetUserGameSaveDataRoot(u128 user_id, bool future) {
  123. if (future) {
  124. Common::UUID uuid;
  125. std::memcpy(uuid.uuid.data(), user_id.data(), sizeof(Common::UUID));
  126. return fmt::format("/user/save/account/{}", uuid.RawString());
  127. }
  128. return fmt::format("/user/save/{:016X}/{:016X}{:016X}", 0, user_id[1], user_id[0]);
  129. }
  130. SaveDataSize SaveDataFactory::ReadSaveDataSize(SaveDataType type, u64 title_id,
  131. u128 user_id) const {
  132. const auto path =
  133. GetFullPath(program_id, dir, SaveDataSpaceId::User, type, title_id, user_id, 0);
  134. const auto relative_dir = GetOrCreateDirectoryRelative(dir, path);
  135. const auto size_file = relative_dir->GetFile(GetSaveDataSizeFileName());
  136. if (size_file == nullptr || size_file->GetSize() < sizeof(SaveDataSize)) {
  137. return {0, 0};
  138. }
  139. SaveDataSize out;
  140. if (size_file->ReadObject(&out) != sizeof(SaveDataSize)) {
  141. return {0, 0};
  142. }
  143. return out;
  144. }
  145. void SaveDataFactory::WriteSaveDataSize(SaveDataType type, u64 title_id, u128 user_id,
  146. SaveDataSize new_value) const {
  147. const auto path =
  148. GetFullPath(program_id, dir, SaveDataSpaceId::User, type, title_id, user_id, 0);
  149. const auto relative_dir = GetOrCreateDirectoryRelative(dir, path);
  150. const auto size_file = relative_dir->CreateFile(GetSaveDataSizeFileName());
  151. if (size_file == nullptr) {
  152. return;
  153. }
  154. size_file->Resize(sizeof(SaveDataSize));
  155. size_file->WriteObject(new_value);
  156. }
  157. void SaveDataFactory::SetAutoCreate(bool state) {
  158. auto_create = state;
  159. }
  160. } // namespace FileSys