Skip to content

Commit

Permalink
Qt/GCMemcardManager and Qt/GameCubePane: Give detailed error messages…
Browse files Browse the repository at this point in the history
… for invalid memory cards.
  • Loading branch information
AdmiralCurtiss committed Aug 15, 2019
1 parent 112f65a commit 9acea25
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
54 changes: 48 additions & 6 deletions Source/Core/DolphinQt/GCMemcardManager.cpp
Expand Up @@ -17,6 +17,8 @@
#include <QLineEdit>
#include <QPixmap>
#include <QPushButton>
#include <QString>
#include <QStringList>
#include <QTableWidget>
#include <QTimer>

Expand Down Expand Up @@ -241,14 +243,20 @@ void GCMemcardManager::UpdateActions()

void GCMemcardManager::SetSlotFile(int slot, QString path)
{
// TODO: Check error codes and give reasonable error messages.
auto [error_code, memcard] = GCMemcard::Open(path.toStdString());

if (error_code.HasCriticalErrors() || !memcard || !memcard->IsValid())
return;

m_slot_file_edit[slot]->setText(path);
m_slot_memcard[slot] = std::make_unique<GCMemcard>(std::move(*memcard));
if (!error_code.HasCriticalErrors() && memcard && memcard->IsValid())
{
m_slot_file_edit[slot]->setText(path);
m_slot_memcard[slot] = std::make_unique<GCMemcard>(std::move(*memcard));
}
else
{
m_slot_memcard[slot] = nullptr;
ModalMessageBox::critical(
this, tr("Error"),
tr("Failed opening memory card:\n%1").arg(GetErrorMessagesForErrorCode(error_code)));
}

UpdateSlotTable(slot);
UpdateActions();
Expand Down Expand Up @@ -524,3 +532,37 @@ std::vector<QPixmap> GCMemcardManager::GetIconFromSaveFile(int file_index, int s

return frame_pixmaps;
}

QString GCMemcardManager::GetErrorMessagesForErrorCode(const GCMemcardErrorCode& code)
{
QStringList sl;

if (code.Test(GCMemcardValidityIssues::FAILED_TO_OPEN))
sl.push_back(tr("Couldn't open file."));

if (code.Test(GCMemcardValidityIssues::IO_ERROR))
sl.push_back(tr("Couldn't read file."));

if (code.Test(GCMemcardValidityIssues::INVALID_CARD_SIZE))
sl.push_back(tr("Filesize does not match any known GameCube Memory Card size."));

if (code.Test(GCMemcardValidityIssues::MISMATCHED_CARD_SIZE))
sl.push_back(tr("Filesize in header mismatches actual card size."));

if (code.Test(GCMemcardValidityIssues::INVALID_CHECKSUM))
sl.push_back(tr("Invalid checksums."));

if (code.Test(GCMemcardValidityIssues::FREE_BLOCK_MISMATCH))
sl.push_back(tr("Mismatch between free block count in header and actually unused blocks."));

if (code.Test(GCMemcardValidityIssues::DIR_BAT_INCONSISTENT))
sl.push_back(tr("Mismatch between internal data structures."));

if (code.Test(GCMemcardValidityIssues::DATA_IN_UNUSED_AREA))
sl.push_back(tr("Data in area of file that should be unused."));

if (sl.empty())
return QStringLiteral("No errors.");

return sl.join(QStringLiteral("\n"));
}
4 changes: 4 additions & 0 deletions Source/Core/DolphinQt/GCMemcardManager.h
Expand Up @@ -12,13 +12,15 @@
#include <QDialog>

class GCMemcard;
class GCMemcardErrorCode;

class QDialogButtonBox;
class QGroupBox;
class QLabel;
class QLineEdit;
class QPixmap;
class QPushButton;
class QString;
class QTableWidget;
class QTimer;

Expand All @@ -29,6 +31,8 @@ class GCMemcardManager : public QDialog
explicit GCMemcardManager(QWidget* parent = nullptr);
~GCMemcardManager();

static QString GetErrorMessagesForErrorCode(const GCMemcardErrorCode& code);

private:
void CreateWidgets();
void ConnectWidgets();
Expand Down
12 changes: 6 additions & 6 deletions Source/Core/DolphinQt/Settings/GameCubePane.cpp
Expand Up @@ -28,6 +28,7 @@
#include "Core/HW/GCMemcard/GCMemcard.h"

#include "DolphinQt/Config/Mapping/MappingWindow.h"
#include "DolphinQt/GCMemcardManager.h"
#include "DolphinQt/QtUtils/ModalMessageBox.h"

enum
Expand Down Expand Up @@ -212,16 +213,15 @@ void GameCubePane::OnConfigPressed(int slot)
{
if (File::Exists(filename.toStdString()))
{
// TODO: check error codes and give reasonable error messages
auto [error_code, mc] = GCMemcard::Open(filename.toStdString());

if (error_code.HasCriticalErrors() || !mc || !mc->IsValid())
{
ModalMessageBox::critical(this, tr("Error"),
tr("Cannot use that file as a memory card.\n%1\n"
"is not a valid GameCube memory card file")
.arg(filename));

ModalMessageBox::critical(
this, tr("Error"),
tr("The file\n%1\nis either corrupted or not a GameCube memory card file.\n%2")
.arg(filename)
.arg(GCMemcardManager::GetErrorMessagesForErrorCode(error_code)));
return;
}
}
Expand Down

0 comments on commit 9acea25

Please sign in to comment.