diff options
Diffstat (limited to 'src/FATStorage.cpp')
-rw-r--r-- | src/FATStorage.cpp | 90 |
1 files changed, 63 insertions, 27 deletions
diff --git a/src/FATStorage.cpp b/src/FATStorage.cpp index cd0a03c..8799cb4 100644 --- a/src/FATStorage.cpp +++ b/src/FATStorage.cpp @@ -29,39 +29,79 @@ namespace melonDS { namespace fs = std::filesystem; using namespace Platform; +using std::string; -FATStorage::FATStorage(const std::string& filename, u64 size, bool readonly, const std::string& sourcedir) +FATStorage::FATStorage(const std::string& filename, u64 size, bool readonly, const std::optional<string>& sourcedir) : + FilePath(filename), + FileSize(size), + ReadOnly(readonly), + SourceDir(sourcedir) { - ReadOnly = readonly; Load(filename, size, sourcedir); - File = nullptr; + File = Platform::OpenLocalFile(FilePath, FileMode::ReadWriteExisting); } -FATStorage::~FATStorage() +FATStorage::FATStorage(const FATStorageArgs& args) noexcept : + FATStorage(args.Filename, args.Size, args.ReadOnly, args.SourceDir) { - if (!ReadOnly) Save(); } +FATStorage::FATStorage(FATStorageArgs&& args) noexcept : + FilePath(std::move(args.Filename)), + FileSize(args.Size), + ReadOnly(args.ReadOnly), + SourceDir(std::move(args.SourceDir)) +{ + Load(FilePath, FileSize, SourceDir); + + File = nullptr; +} -bool FATStorage::Open() +FATStorage::FATStorage(FATStorage&& other) noexcept { - File = Platform::OpenLocalFile(FilePath, FileMode::ReadWriteExisting); - if (!File) + FilePath = std::move(other.FilePath); + IndexPath = std::move(other.IndexPath); + SourceDir = std::move(other.SourceDir); + ReadOnly = other.ReadOnly; + File = other.File; + FileSize = other.FileSize; + DirIndex = std::move(other.DirIndex); + FileIndex = std::move(other.FileIndex); + + other.File = nullptr; +} + +FATStorage& FATStorage::operator=(FATStorage&& other) noexcept +{ + if (this != &other) { - return false; + if (File) + CloseFile(File); + + FilePath = std::move(other.FilePath); + IndexPath = std::move(other.IndexPath); + SourceDir = std::move(other.SourceDir); + ReadOnly = other.ReadOnly; + File = other.File; + FileSize = other.FileSize; + DirIndex = std::move(other.DirIndex); + FileIndex = std::move(other.FileIndex); + + other.File = nullptr; } - return true; + return *this; } -void FATStorage::Close() +FATStorage::~FATStorage() { + if (!ReadOnly) Save(); + if (File) CloseFile(File); File = nullptr; } - bool FATStorage::InjectFile(const std::string& path, u8* data, u32 len) { if (!File) return false; @@ -930,19 +970,15 @@ u64 FATStorage::GetDirectorySize(fs::path sourcedir) return ret; } -bool FATStorage::Load(const std::string& filename, u64 size, const std::string& sourcedir) +bool FATStorage::Load(const std::string& filename, u64 size, const std::optional<string>& sourcedir) { - FilePath = filename; - FileSize = size; - SourceDir = sourcedir; - - bool hasdir = !sourcedir.empty(); - if (hasdir) + bool hasdir = sourcedir && !sourcedir->empty(); + if (sourcedir) { - if (!fs::is_directory(fs::u8path(sourcedir))) + if (!fs::is_directory(fs::u8path(*sourcedir))) { hasdir = false; - SourceDir = ""; + SourceDir = std::nullopt; } } @@ -1005,7 +1041,7 @@ bool FATStorage::Load(const std::string& filename, u64 size, const std::string& { if (hasdir) { - FileSize = GetDirectorySize(fs::u8path(sourcedir)); + FileSize = GetDirectorySize(fs::u8path(*sourcedir)); FileSize += 0x8000000ULL; // 128MB leeway // make it a power of two @@ -1054,7 +1090,7 @@ bool FATStorage::Load(const std::string& filename, u64 size, const std::string& if (res == FR_OK) { if (hasdir) - ImportDirectory(sourcedir); + ImportDirectory(*sourcedir); } f_unmount("0:"); @@ -1068,9 +1104,9 @@ bool FATStorage::Load(const std::string& filename, u64 size, const std::string& bool FATStorage::Save() { - if (SourceDir.empty()) - { - return true; + if (!SourceDir) + { // If we're not syncing the SD card image to a host directory... + return true; // Not an error. } FF_File = Platform::OpenLocalFile(FilePath, FileMode::ReadWriteExisting); @@ -1094,7 +1130,7 @@ bool FATStorage::Save() return false; } - ExportChanges(SourceDir); + ExportChanges(*SourceDir); SaveIndex(); |