use platform file functions for savestates and NAND importing file check

should fix character encoding issues on Windows
This commit is contained in:
RSDuck 2024-07-29 00:15:19 +02:00
parent c0ada2f445
commit 12563fb636
2 changed files with 16 additions and 24 deletions

View File

@ -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<u8> 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<Savestate> state = std::make_unique<Savestate>(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)
{

View File

@ -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)
{