From 12563fb636f27b2eaa6b2a25342bed6ca9268bb0 Mon Sep 17 00:00:00 2001 From: RSDuck Date: Mon, 29 Jul 2024 00:15:19 +0200 Subject: [PATCH] use platform file functions for savestates and NAND importing file check should fix character encoding issues on Windows --- src/frontend/qt_sdl/EmuInstance.cpp | 33 +++++++++------------- src/frontend/qt_sdl/TitleManagerDialog.cpp | 7 ++--- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/frontend/qt_sdl/EmuInstance.cpp b/src/frontend/qt_sdl/EmuInstance.cpp index 96e1ef59..999fb803 100644 --- a/src/frontend/qt_sdl/EmuInstance.cpp +++ b/src/frontend/qt_sdl/EmuInstance.cpp @@ -552,7 +552,7 @@ bool EmuInstance::savestateExists(int slot) bool EmuInstance::loadState(const std::string& filename) { - FILE* file = fopen(filename.c_str(), "rb"); + Platform::FileHandle* file = Platform::OpenFile(filename, Platform::FileMode::Read); if (file == nullptr) { // If we couldn't open the state file... Platform::Log(Platform::LogLevel::Error, "Failed to open state file \"%s\"\n", filename.c_str()); @@ -563,38 +563,31 @@ bool EmuInstance::loadState(const std::string& filename) if (backup->Error) { // If we couldn't allocate memory for the backup... Platform::Log(Platform::LogLevel::Error, "Failed to allocate memory for state backup\n"); - fclose(file); + Platform::CloseFile(file); return false; } if (!nds->DoSavestate(backup.get()) || backup->Error) { // Back up the emulator's state. If that failed... Platform::Log(Platform::LogLevel::Error, "Failed to back up state, aborting load (from \"%s\")\n", filename.c_str()); - fclose(file); + Platform::CloseFile(file); return false; } // We'll store the backup once we're sure that the state was loaded. // Now that we know the file and backup are both good, let's load the new state. // Get the size of the file that we opened - if (fseek(file, 0, SEEK_END) != 0) - { - Platform::Log(Platform::LogLevel::Error, "Failed to seek to end of state file \"%s\"\n", filename.c_str()); - fclose(file); - return false; - } - size_t size = ftell(file); - rewind(file); // reset the filebuf's position + size_t size = Platform::FileLength(file); // Allocate exactly as much memory as we need for the savestate std::vector buffer(size); - if (fread(buffer.data(), size, 1, file) == 0) + if (Platform::FileRead(buffer.data(), size, 1, file) == 0) { // Read the state file into the buffer. If that failed... Platform::Log(Platform::LogLevel::Error, "Failed to read %u-byte state file \"%s\"\n", size, filename.c_str()); - fclose(file); + Platform::CloseFile(file); return false; } - fclose(file); // done with the file now + Platform::CloseFile(file); // done with the file now // Get ready to load the state from the buffer into the emulator std::unique_ptr state = std::make_unique(buffer.data(), size, false); @@ -626,7 +619,7 @@ bool EmuInstance::loadState(const std::string& filename) bool EmuInstance::saveState(const std::string& filename) { - FILE* file = fopen(filename.c_str(), "wb"); + Platform::FileHandle* file = Platform::OpenFile(filename, Platform::FileMode::Write); if (file == nullptr) { // If the file couldn't be opened... @@ -636,7 +629,7 @@ bool EmuInstance::saveState(const std::string& filename) Savestate state; if (state.Error) { // If there was an error creating the state (and allocating its memory)... - fclose(file); + Platform::CloseFile(file); return false; } @@ -645,22 +638,22 @@ bool EmuInstance::saveState(const std::string& filename) if (state.Error) { - fclose(file); + Platform::CloseFile(file); return false; } - if (fwrite(state.Buffer(), state.Length(), 1, file) == 0) + if (Platform::FileWrite(state.Buffer(), state.Length(), 1, file) == 0) { // Write the Savestate buffer to the file. If that fails... Platform::Log(Platform::Error, "Failed to write %d-byte savestate to %s\n", state.Length(), filename.c_str() ); - fclose(file); + Platform::CloseFile(file); return false; } - fclose(file); + Platform::CloseFile(file); if (globalCfg.GetBool("Savestate.RelocSRAM") && ndsSave) { diff --git a/src/frontend/qt_sdl/TitleManagerDialog.cpp b/src/frontend/qt_sdl/TitleManagerDialog.cpp index 908ae70f..30f29d24 100644 --- a/src/frontend/qt_sdl/TitleManagerDialog.cpp +++ b/src/frontend/qt_sdl/TitleManagerDialog.cpp @@ -303,7 +303,7 @@ void TitleManagerDialog::onImportTitleData() if (file.isEmpty()) return; - FILE* f = fopen(file.toStdString().c_str(), "rb"); + Platform::FileHandle* f = Platform::OpenFile(file.toStdString(), Platform::Read); if (!f) { QMessageBox::critical(this, @@ -312,9 +312,8 @@ void TitleManagerDialog::onImportTitleData() return; } - fseek(f, 0, SEEK_END); - u64 len = ftell(f); - fclose(f); + u64 len = Platform::FileLength(f); + Platform::CloseFile(f); if (len != wantedsize) {