archive_systemsavedata.cpp 2.7 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/errors.h"
  12. #include "core/file_sys/savedata_archive.h"
  13. #include "core/hle/service/fs/archive.h"
  14. ////////////////////////////////////////////////////////////////////////////////////////////////////
  15. // FileSys namespace
  16. namespace FileSys {
  17. std::string GetSystemSaveDataPath(const std::string& mount_point, const Path& path) {
  18. std::vector<u8> vec_data = path.AsBinary();
  19. const u32* data = reinterpret_cast<const u32*>(vec_data.data());
  20. u32 save_low = data[1];
  21. u32 save_high = data[0];
  22. return Common::StringFromFormat("%s%08X/%08X/", mount_point.c_str(), save_low, save_high);
  23. }
  24. std::string GetSystemSaveDataContainerPath(const std::string& mount_point) {
  25. return Common::StringFromFormat("%sdata/%s/sysdata/", mount_point.c_str(), SYSTEM_ID);
  26. }
  27. Path ConstructSystemSaveDataBinaryPath(u32 high, u32 low) {
  28. std::vector<u8> binary_path;
  29. binary_path.reserve(8);
  30. // Append each word byte by byte
  31. // First is the high word
  32. for (unsigned i = 0; i < 4; ++i)
  33. binary_path.push_back((high >> (8 * i)) & 0xFF);
  34. // Next is the low word
  35. for (unsigned i = 0; i < 4; ++i)
  36. binary_path.push_back((low >> (8 * i)) & 0xFF);
  37. return {binary_path};
  38. }
  39. ArchiveFactory_SystemSaveData::ArchiveFactory_SystemSaveData(const std::string& nand_path)
  40. : base_path(GetSystemSaveDataContainerPath(nand_path)) {}
  41. ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SystemSaveData::Open(const Path& path) {
  42. std::string fullpath = GetSystemSaveDataPath(base_path, path);
  43. if (!FileUtil::Exists(fullpath)) {
  44. // TODO(Subv): Check error code, this one is probably wrong
  45. return ERR_NOT_FORMATTED;
  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