archive_systemsavedata.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <memory>
  6. #include <vector>
  7. #include "common/common_types.h"
  8. #include "common/file_util.h"
  9. #include "common/string_util.h"
  10. #include "core/file_sys/archive_systemsavedata.h"
  11. #include "core/file_sys/savedata_archive.h"
  12. #include "core/hle/service/fs/archive.h"
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////
  14. // FileSys namespace
  15. namespace FileSys {
  16. std::string GetSystemSaveDataPath(const std::string& mount_point, const Path& path) {
  17. std::vector<u8> vec_data = path.AsBinary();
  18. const u32* data = reinterpret_cast<const u32*>(vec_data.data());
  19. u32 save_low = data[1];
  20. u32 save_high = data[0];
  21. return Common::StringFromFormat("%s%08X/%08X/", mount_point.c_str(), save_low, save_high);
  22. }
  23. std::string GetSystemSaveDataContainerPath(const std::string& mount_point) {
  24. return Common::StringFromFormat("%sdata/%s/sysdata/", mount_point.c_str(), SYSTEM_ID);
  25. }
  26. Path ConstructSystemSaveDataBinaryPath(u32 high, u32 low) {
  27. std::vector<u8> binary_path;
  28. binary_path.reserve(8);
  29. // Append each word byte by byte
  30. // First is the high word
  31. for (unsigned i = 0; i < 4; ++i)
  32. binary_path.push_back((high >> (8 * i)) & 0xFF);
  33. // Next is the low word
  34. for (unsigned i = 0; i < 4; ++i)
  35. binary_path.push_back((low >> (8 * i)) & 0xFF);
  36. return {binary_path};
  37. }
  38. ArchiveFactory_SystemSaveData::ArchiveFactory_SystemSaveData(const std::string& nand_path)
  39. : base_path(GetSystemSaveDataContainerPath(nand_path)) {}
  40. ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SystemSaveData::Open(const Path& path) {
  41. std::string fullpath = GetSystemSaveDataPath(base_path, path);
  42. if (!FileUtil::Exists(fullpath)) {
  43. // TODO(Subv): Check error code, this one is probably wrong
  44. return ResultCode(ErrorDescription::FS_NotFormatted, ErrorModule::FS,
  45. ErrorSummary::InvalidState, ErrorLevel::Status);
  46. }
  47. auto archive = std::make_unique<SaveDataArchive>(fullpath);
  48. return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
  49. }
  50. ResultCode ArchiveFactory_SystemSaveData::Format(const Path& path,
  51. const FileSys::ArchiveFormatInfo& format_info) {
  52. std::string fullpath = GetSystemSaveDataPath(base_path, path);
  53. FileUtil::DeleteDirRecursively(fullpath);
  54. FileUtil::CreateFullPath(fullpath);
  55. return RESULT_SUCCESS;
  56. }
  57. ResultVal<ArchiveFormatInfo> ArchiveFactory_SystemSaveData::GetFormatInfo(const Path& path) const {
  58. // TODO(Subv): Implement
  59. LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive %s", GetName().c_str());
  60. return ResultCode(-1);
  61. }
  62. } // namespace FileSys