MemoryCardImage: Report errors to caller

This commit is contained in:
Stenzek
2024-04-07 13:23:00 +10:00
parent 9dd686a994
commit 1ee5f737ed
3 changed files with 132 additions and 116 deletions

View File

@ -8,6 +8,7 @@
#include "core/settings.h"
#include "common/assert.h"
#include "common/error.h"
#include "common/file_system.h"
#include "common/path.h"
#include "common/string_util.h"
@ -424,16 +425,23 @@ void MemoryCardEditorWindow::doCopyFile()
return;
}
Error error;
std::vector<u8> buffer;
if (!MemoryCardImage::ReadFile(src->data, *fi, &buffer))
if (!MemoryCardImage::ReadFile(src->data, *fi, &buffer, &error))
{
QMessageBox::critical(this, tr("Error"), tr("Failed to read file %1").arg(QString::fromStdString(fi->filename)));
QMessageBox::critical(this, tr("Error"),
tr("Failed to read file %1:\n%2")
.arg(QString::fromStdString(fi->filename))
.arg(QString::fromStdString(error.GetDescription())));
return;
}
if (!MemoryCardImage::WriteFile(&dst->data, fi->filename, buffer))
if (!MemoryCardImage::WriteFile(&dst->data, fi->filename, buffer, &error))
{
QMessageBox::critical(this, tr("Error"), tr("Failed to write file %1").arg(QString::fromStdString(fi->filename)));
QMessageBox::critical(this, tr("Error"),
tr("Failed to write file %1:\n%2")
.arg(QString::fromStdString(fi->filename))
.arg(QString::fromStdString(error.GetDescription())));
return;
}
@ -497,11 +505,13 @@ void MemoryCardEditorWindow::doExportSaveFile()
if (!fi)
return;
if (!MemoryCardImage::ExportSave(&card->data, *fi, filename.toStdString().c_str()))
Error error;
if (!MemoryCardImage::ExportSave(&card->data, *fi, filename.toStdString().c_str(), &error))
{
QMessageBox::critical(
this, tr("Error"),
tr("Failed to export save file %1. Check the log for more details.").arg(QString::fromStdString(fi->filename)));
QMessageBox::critical(this, tr("Error"),
tr("Failed to export save file %1:\n%2")
.arg(QString::fromStdString(fi->filename))
.arg(QString::fromStdString(error.GetDescription())));
return;
}
}
@ -515,10 +525,14 @@ void MemoryCardEditorWindow::importCard(Card* card)
if (filename.isEmpty())
return;
Error error;
std::unique_ptr<MemoryCardImage::DataArray> temp = std::make_unique<MemoryCardImage::DataArray>();
if (!MemoryCardImage::ImportCard(temp.get(), filename.toStdString().c_str()))
if (!MemoryCardImage::ImportCard(temp.get(), filename.toStdString().c_str(), &error))
{
QMessageBox::critical(this, tr("Error"), tr("Failed to import memory card. The log may contain more information."));
QMessageBox::critical(this, tr("Error"),
tr("Failed to import memory card from %1:\n%2")
.arg(QFileInfo(filename).fileName())
.arg(QString::fromStdString(error.GetDescription())));
return;
}
@ -557,16 +571,18 @@ void MemoryCardEditorWindow::formatCard(Card* card)
void MemoryCardEditorWindow::importSaveFile(Card* card)
{
QString filename = QDir::toNativeSeparators(
QFileDialog::getOpenFileName(this, tr("Select Import Save File"), QString(), tr(SINGLE_SAVEFILE_FILTER)));
QFileDialog::getOpenFileName(this, tr("Select Save File"), QString(), tr(SINGLE_SAVEFILE_FILTER)));
if (filename.isEmpty())
return;
if (!MemoryCardImage::ImportSave(&card->data, filename.toStdString().c_str()))
Error error;
if (!MemoryCardImage::ImportSave(&card->data, filename.toStdString().c_str(), &error))
{
QMessageBox::critical(this, tr("Error"),
tr("Failed to import save. Check if there is enough room on the memory card or if an "
"existing save with the same name already exists."));
tr("Failed to import save from %1:\n%2")
.arg(QFileInfo(filename).fileName())
.arg(QString::fromStdString(error.GetDescription())));
return;
}