savedata_factory.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.h"
  11. namespace FileSys {
  12. namespace {
  13. void PrintSaveDataAttributeWarnings(SaveDataAttribute meta) {
  14. if (meta.type == SaveDataType::SystemSaveData || meta.type == SaveDataType::SaveData) {
  15. if (meta.zero_1 != 0) {
  16. LOG_WARNING(Service_FS,
  17. "Possibly incorrect SaveDataAttribute, type is "
  18. "SystemSaveData||SaveData but offset 0x28 is non-zero ({:016X}).",
  19. meta.zero_1);
  20. }
  21. if (meta.zero_2 != 0) {
  22. LOG_WARNING(Service_FS,
  23. "Possibly incorrect SaveDataAttribute, type is "
  24. "SystemSaveData||SaveData but offset 0x30 is non-zero ({:016X}).",
  25. meta.zero_2);
  26. }
  27. if (meta.zero_3 != 0) {
  28. LOG_WARNING(Service_FS,
  29. "Possibly incorrect SaveDataAttribute, type is "
  30. "SystemSaveData||SaveData but offset 0x38 is non-zero ({:016X}).",
  31. meta.zero_3);
  32. }
  33. }
  34. if (meta.type == SaveDataType::SystemSaveData && meta.title_id != 0) {
  35. LOG_WARNING(Service_FS,
  36. "Possibly incorrect SaveDataAttribute, type is SystemSaveData but title_id is "
  37. "non-zero ({:016X}).",
  38. meta.title_id);
  39. }
  40. if (meta.type == SaveDataType::DeviceSaveData && meta.user_id != u128{0, 0}) {
  41. LOG_WARNING(Service_FS,
  42. "Possibly incorrect SaveDataAttribute, type is DeviceSaveData but user_id is "
  43. "non-zero ({:016X}{:016X})",
  44. meta.user_id[1], meta.user_id[0]);
  45. }
  46. }
  47. bool ShouldSaveDataBeAutomaticallyCreated(SaveDataSpaceId space, const SaveDataAttribute& attr) {
  48. return attr.type == SaveDataType::CacheStorage || attr.type == SaveDataType::TemporaryStorage ||
  49. (space == SaveDataSpaceId::NandUser && ///< Normal Save Data -- Current Title & User
  50. (attr.type == SaveDataType::SaveData || attr.type == SaveDataType::DeviceSaveData) &&
  51. attr.title_id == 0 && attr.save_id == 0);
  52. }
  53. std::string GetFutureSaveDataPath(SaveDataSpaceId space_id, SaveDataType type, u64 title_id,
  54. u128 user_id) {
  55. // Only detect nand user saves.
  56. const auto space_id_path = [space_id]() -> std::string_view {
  57. switch (space_id) {
  58. case SaveDataSpaceId::NandUser:
  59. return "/user/save";
  60. default:
  61. return "";
  62. }
  63. }();
  64. if (space_id_path.empty()) {
  65. return "";
  66. }
  67. Common::UUID uuid;
  68. std::memcpy(uuid.uuid.data(), user_id.data(), sizeof(Common::UUID));
  69. // Only detect account/device saves from the future location.
  70. switch (type) {
  71. case SaveDataType::SaveData:
  72. return fmt::format("{}/account/{}/{:016X}/0", space_id_path, uuid.RawString(), title_id);
  73. case SaveDataType::DeviceSaveData:
  74. return fmt::format("{}/device/{:016X}/0", space_id_path, title_id);
  75. default:
  76. return "";
  77. }
  78. }
  79. } // Anonymous namespace
  80. std::string SaveDataAttribute::DebugInfo() const {
  81. return fmt::format("[title_id={:016X}, user_id={:016X}{:016X}, save_id={:016X}, type={:02X}, "
  82. "rank={}, index={}]",
  83. title_id, user_id[1], user_id[0], save_id, static_cast<u8>(type),
  84. static_cast<u8>(rank), index);
  85. }
  86. SaveDataFactory::SaveDataFactory(Core::System& system_, ProgramId program_id_,
  87. VirtualDir save_directory_)
  88. : system{system_}, program_id{program_id_}, dir{std::move(save_directory_)} {
  89. // Delete all temporary storages
  90. // On hardware, it is expected that temporary storage be empty at first use.
  91. dir->DeleteSubdirectoryRecursive("temp");
  92. }
  93. SaveDataFactory::~SaveDataFactory() = default;
  94. VirtualDir SaveDataFactory::Create(SaveDataSpaceId space, const SaveDataAttribute& meta) const {
  95. PrintSaveDataAttributeWarnings(meta);
  96. const auto save_directory =
  97. GetFullPath(program_id, dir, space, meta.type, meta.title_id, meta.user_id, meta.save_id);
  98. return dir->CreateDirectoryRelative(save_directory);
  99. }
  100. VirtualDir SaveDataFactory::Open(SaveDataSpaceId space, const SaveDataAttribute& meta) const {
  101. const auto save_directory =
  102. GetFullPath(program_id, dir, space, meta.type, meta.title_id, meta.user_id, meta.save_id);
  103. auto out = dir->GetDirectoryRelative(save_directory);
  104. if (out == nullptr && (ShouldSaveDataBeAutomaticallyCreated(space, meta) && auto_create)) {
  105. return Create(space, meta);
  106. }
  107. return out;
  108. }
  109. VirtualDir SaveDataFactory::GetSaveDataSpaceDirectory(SaveDataSpaceId space) const {
  110. return dir->GetDirectoryRelative(GetSaveDataSpaceIdPath(space));
  111. }
  112. std::string SaveDataFactory::GetSaveDataSpaceIdPath(SaveDataSpaceId space) {
  113. switch (space) {
  114. case SaveDataSpaceId::NandSystem:
  115. return "/system/";
  116. case SaveDataSpaceId::NandUser:
  117. return "/user/";
  118. case SaveDataSpaceId::TemporaryStorage:
  119. return "/temp/";
  120. default:
  121. ASSERT_MSG(false, "Unrecognized SaveDataSpaceId: {:02X}", static_cast<u8>(space));
  122. return "/unrecognized/"; ///< To prevent corruption when ignoring asserts.
  123. }
  124. }
  125. std::string SaveDataFactory::GetFullPath(ProgramId program_id, VirtualDir dir,
  126. SaveDataSpaceId space, SaveDataType type, u64 title_id,
  127. u128 user_id, u64 save_id) {
  128. // According to switchbrew, if a save is of type SaveData and the title id field is 0, it should
  129. // be interpreted as the title id of the current process.
  130. if (type == SaveDataType::SaveData || type == SaveDataType::DeviceSaveData) {
  131. if (title_id == 0) {
  132. title_id = program_id;
  133. }
  134. }
  135. // For compat with a future impl.
  136. if (std::string future_path =
  137. GetFutureSaveDataPath(space, type, title_id & ~(0xFFULL), user_id);
  138. !future_path.empty()) {
  139. // Check if this location exists, and prefer it over the old.
  140. if (const auto future_dir = dir->GetDirectoryRelative(future_path); future_dir != nullptr) {
  141. LOG_INFO(Service_FS, "Using save at new location: {}", future_path);
  142. return future_path;
  143. }
  144. }
  145. std::string out = GetSaveDataSpaceIdPath(space);
  146. switch (type) {
  147. case SaveDataType::SystemSaveData:
  148. return fmt::format("{}save/{:016X}/{:016X}{:016X}", out, save_id, user_id[1], user_id[0]);
  149. case SaveDataType::SaveData:
  150. case SaveDataType::DeviceSaveData:
  151. return fmt::format("{}save/{:016X}/{:016X}{:016X}/{:016X}", out, 0, user_id[1], user_id[0],
  152. title_id);
  153. case SaveDataType::TemporaryStorage:
  154. return fmt::format("{}{:016X}/{:016X}{:016X}/{:016X}", out, 0, user_id[1], user_id[0],
  155. title_id);
  156. case SaveDataType::CacheStorage:
  157. return fmt::format("{}save/cache/{:016X}", out, title_id);
  158. default:
  159. ASSERT_MSG(false, "Unrecognized SaveDataType: {:02X}", static_cast<u8>(type));
  160. return fmt::format("{}save/unknown_{:X}/{:016X}", out, static_cast<u8>(type), title_id);
  161. }
  162. }
  163. std::string SaveDataFactory::GetUserGameSaveDataRoot(u128 user_id, bool future) {
  164. if (future) {
  165. Common::UUID uuid;
  166. std::memcpy(uuid.uuid.data(), user_id.data(), sizeof(Common::UUID));
  167. return fmt::format("/user/save/account/{}", uuid.RawString());
  168. }
  169. return fmt::format("/user/save/{:016X}/{:016X}{:016X}", 0, user_id[1], user_id[0]);
  170. }
  171. SaveDataSize SaveDataFactory::ReadSaveDataSize(SaveDataType type, u64 title_id,
  172. u128 user_id) const {
  173. const auto path =
  174. GetFullPath(program_id, dir, SaveDataSpaceId::NandUser, type, title_id, user_id, 0);
  175. const auto relative_dir = GetOrCreateDirectoryRelative(dir, path);
  176. const auto size_file = relative_dir->GetFile(GetSaveDataSizeFileName());
  177. if (size_file == nullptr || size_file->GetSize() < sizeof(SaveDataSize)) {
  178. return {0, 0};
  179. }
  180. SaveDataSize out;
  181. if (size_file->ReadObject(&out) != sizeof(SaveDataSize)) {
  182. return {0, 0};
  183. }
  184. return out;
  185. }
  186. void SaveDataFactory::WriteSaveDataSize(SaveDataType type, u64 title_id, u128 user_id,
  187. SaveDataSize new_value) const {
  188. const auto path =
  189. GetFullPath(program_id, dir, SaveDataSpaceId::NandUser, type, title_id, user_id, 0);
  190. const auto relative_dir = GetOrCreateDirectoryRelative(dir, path);
  191. const auto size_file = relative_dir->CreateFile(GetSaveDataSizeFileName());
  192. if (size_file == nullptr) {
  193. return;
  194. }
  195. size_file->Resize(sizeof(SaveDataSize));
  196. size_file->WriteObject(new_value);
  197. }
  198. void SaveDataFactory::SetAutoCreate(bool state) {
  199. auto_create = state;
  200. }
  201. } // namespace FileSys