archive_extsavedata.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 <vector>
  6. #include "common/common_types.h"
  7. #include "common/file_util.h"
  8. #include "common/logging/log.h"
  9. #include "common/make_unique.h"
  10. #include "common/string_util.h"
  11. #include "core/file_sys/archive_extsavedata.h"
  12. #include "core/file_sys/disk_archive.h"
  13. #include "core/hle/service/fs/archive.h"
  14. ////////////////////////////////////////////////////////////////////////////////////////////////////
  15. // FileSys namespace
  16. namespace FileSys {
  17. std::string GetExtSaveDataPath(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[2];
  22. return Common::StringFromFormat("%s%08X/%08X/", mount_point.c_str(), save_high, save_low);
  23. }
  24. std::string GetExtDataContainerPath(const std::string& mount_point, bool shared) {
  25. if (shared)
  26. return Common::StringFromFormat("%sdata/%s/extdata/", mount_point.c_str(), SYSTEM_ID.c_str());
  27. return Common::StringFromFormat("%sNintendo 3DS/%s/%s/extdata/", mount_point.c_str(),
  28. SYSTEM_ID.c_str(), SDCARD_ID.c_str());
  29. }
  30. Path ConstructExtDataBinaryPath(u32 media_type, u32 high, u32 low) {
  31. std::vector<u8> binary_path;
  32. binary_path.reserve(12);
  33. // Append each word byte by byte
  34. // The first word is the media type
  35. for (unsigned i = 0; i < 4; ++i)
  36. binary_path.push_back((media_type >> (8 * i)) & 0xFF);
  37. // Next is the low word
  38. for (unsigned i = 0; i < 4; ++i)
  39. binary_path.push_back((low >> (8 * i)) & 0xFF);
  40. // Next is the high word
  41. for (unsigned i = 0; i < 4; ++i)
  42. binary_path.push_back((high >> (8 * i)) & 0xFF);
  43. return { binary_path };
  44. }
  45. ArchiveFactory_ExtSaveData::ArchiveFactory_ExtSaveData(const std::string& mount_location, bool shared)
  46. : mount_point(GetExtDataContainerPath(mount_location, shared)) {
  47. LOG_INFO(Service_FS, "Directory %s set as base for ExtSaveData.", mount_point.c_str());
  48. }
  49. bool ArchiveFactory_ExtSaveData::Initialize() {
  50. if (!FileUtil::CreateFullPath(mount_point)) {
  51. LOG_ERROR(Service_FS, "Unable to create ExtSaveData base path.");
  52. return false;
  53. }
  54. return true;
  55. }
  56. ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_ExtSaveData::Open(const Path& path) {
  57. std::string fullpath = GetExtSaveDataPath(mount_point, path) + "user/";
  58. if (!FileUtil::Exists(fullpath)) {
  59. // TODO(Subv): Check error code, this one is probably wrong
  60. return ResultCode(ErrorDescription::FS_NotFormatted, ErrorModule::FS,
  61. ErrorSummary::InvalidState, ErrorLevel::Status);
  62. }
  63. auto archive = Common::make_unique<DiskArchive>(fullpath);
  64. return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
  65. }
  66. ResultCode ArchiveFactory_ExtSaveData::Format(const Path& path) {
  67. // These folders are always created with the ExtSaveData
  68. std::string user_path = GetExtSaveDataPath(mount_point, path) + "user/";
  69. std::string boss_path = GetExtSaveDataPath(mount_point, path) + "boss/";
  70. FileUtil::CreateFullPath(user_path);
  71. FileUtil::CreateFullPath(boss_path);
  72. return RESULT_SUCCESS;
  73. }
  74. } // namespace FileSys