dolphin/Source/Core/DolphinQt/GCMemcardManager.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

856 lines
28 KiB
C++
Raw Normal View History

2018-01-25 18:54:50 +00:00
// Copyright 2018 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
2018-07-06 22:40:15 +00:00
#include "DolphinQt/GCMemcardManager.h"
2018-01-25 18:54:50 +00:00
#include <algorithm>
#include <cassert>
#include <string>
#include <vector>
#include <fmt/format.h>
2018-01-25 18:54:50 +00:00
#include <QDialogButtonBox>
2018-05-13 19:43:57 +00:00
#include <QDir>
2018-01-25 18:54:50 +00:00
#include <QFileDialog>
#include <QGridLayout>
#include <QGroupBox>
#include <QHeaderView>
#include <QImage>
#include <QLabel>
#include <QLineEdit>
#include <QMenu>
2018-01-25 18:54:50 +00:00
#include <QPixmap>
#include <QPushButton>
#include <QString>
#include <QStringList>
2018-01-25 18:54:50 +00:00
#include <QTableWidget>
#include <QTimer>
#include <QToolButton>
2018-01-25 18:54:50 +00:00
#include "Common/CommonPaths.h"
#include "Common/Config/Config.h"
2018-01-25 18:54:50 +00:00
#include "Common/FileUtil.h"
#include "Common/MsgHandler.h"
#include "Common/StringUtil.h"
#include "Common/VariantUtil.h"
2018-01-25 18:54:50 +00:00
#include "Core/Config/MainSettings.h"
2018-01-25 18:54:50 +00:00
#include "Core/HW/GCMemcard/GCMemcard.h"
#include "Core/HW/GCMemcard/GCMemcardUtils.h"
2018-01-25 18:54:50 +00:00
#include "DolphinQt/GCMemcardCreateNewDialog.h"
2019-03-04 19:49:00 +00:00
#include "DolphinQt/QtUtils/ModalMessageBox.h"
constexpr int ROW_HEIGHT = 36;
2020-08-10 19:11:04 +00:00
constexpr int COLUMN_WIDTH_FILENAME = 100;
constexpr int COLUMN_WIDTH_BANNER = Memcard::MEMORY_CARD_BANNER_WIDTH + 6;
constexpr int COLUMN_WIDTH_TEXT = 160;
constexpr int COLUMN_WIDTH_ICON = Memcard::MEMORY_CARD_ICON_WIDTH + 6;
constexpr int COLUMN_WIDTH_BLOCKS = 40;
2020-08-10 19:11:04 +00:00
constexpr int COLUMN_INDEX_FILENAME = 0;
constexpr int COLUMN_INDEX_BANNER = 1;
constexpr int COLUMN_INDEX_TEXT = 2;
constexpr int COLUMN_INDEX_ICON = 3;
constexpr int COLUMN_INDEX_BLOCKS = 4;
constexpr int COLUMN_COUNT = 5;
2018-01-25 18:54:50 +00:00
struct GCMemcardManager::IconAnimationData
{
// the individual frames
std::vector<QPixmap> m_frames;
// vector containing a list of frame indices that indicate, for each time unit,
// the frame that should be displayed when at that time unit
std::vector<u8> m_frame_timing;
};
2018-01-25 18:54:50 +00:00
GCMemcardManager::GCMemcardManager(QWidget* parent) : QDialog(parent)
{
CreateWidgets();
ConnectWidgets();
SetActiveSlot(0);
UpdateActions();
m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, this, &GCMemcardManager::DrawIcons);
// individual frames of icon animations can stay on screen for 4, 8, or 12 frames at 60 FPS,
// which means the fastest animation and common denominator is 15 FPS or 66 milliseconds per frame
m_timer->start(1000 / 15);
2018-01-25 18:54:50 +00:00
LoadDefaultMemcards();
2018-01-25 18:54:50 +00:00
// Make the dimensions more reasonable on startup
resize(650, 500);
setWindowTitle(tr("GameCube Memory Card Manager"));
2018-05-05 00:29:16 +00:00
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
2018-01-25 18:54:50 +00:00
}
GCMemcardManager::~GCMemcardManager() = default;
void GCMemcardManager::CreateWidgets()
{
2018-03-20 09:12:11 +00:00
m_button_box = new QDialogButtonBox(QDialogButtonBox::Close);
2018-01-25 18:54:50 +00:00
// Actions
m_select_button = new QPushButton;
m_copy_button = new QPushButton;
m_delete_button = new QPushButton(tr("&Delete"));
m_export_button = new QToolButton(this);
m_export_menu = new QMenu(m_export_button);
m_export_gci_action = new QAction(tr("&Export as .gci..."), m_export_menu);
m_export_gcs_action = new QAction(tr("Export as .&gcs..."), m_export_menu);
m_export_sav_action = new QAction(tr("Export as .&sav..."), m_export_menu);
m_export_menu->addAction(m_export_gci_action);
m_export_menu->addAction(m_export_gcs_action);
m_export_menu->addAction(m_export_sav_action);
m_export_button->setDefaultAction(m_export_gci_action);
m_export_button->setPopupMode(QToolButton::MenuButtonPopup);
m_export_button->setMenu(m_export_menu);
2018-01-25 18:54:50 +00:00
m_import_button = new QPushButton(tr("&Import..."));
m_fix_checksums_button = new QPushButton(tr("Fix Checksums"));
auto* layout = new QGridLayout;
for (int i = 0; i < SLOT_COUNT; i++)
{
m_slot_group[i] = new QGroupBox(i == 0 ? tr("Slot A") : tr("Slot B"));
m_slot_file_edit[i] = new QLineEdit;
m_slot_open_button[i] = new QPushButton(tr("&Open..."));
m_slot_create_button[i] = new QPushButton(tr("&Create..."));
2018-01-25 18:54:50 +00:00
m_slot_table[i] = new QTableWidget;
m_slot_table[i]->setTabKeyNavigation(false);
2018-01-25 18:54:50 +00:00
m_slot_stat_label[i] = new QLabel;
m_slot_table[i]->setSelectionMode(QAbstractItemView::ExtendedSelection);
m_slot_table[i]->setSelectionBehavior(QAbstractItemView::SelectRows);
m_slot_table[i]->setSortingEnabled(true);
m_slot_table[i]->horizontalHeader()->setHighlightSections(false);
m_slot_table[i]->horizontalHeader()->setMinimumSectionSize(0);
m_slot_table[i]->horizontalHeader()->setSortIndicatorShown(true);
m_slot_table[i]->setColumnCount(COLUMN_COUNT);
2020-08-10 19:11:04 +00:00
m_slot_table[i]->setHorizontalHeaderItem(COLUMN_INDEX_FILENAME,
new QTableWidgetItem(tr("Filename")));
m_slot_table[i]->setHorizontalHeaderItem(COLUMN_INDEX_BANNER,
new QTableWidgetItem(tr("Banner")));
m_slot_table[i]->setHorizontalHeaderItem(COLUMN_INDEX_TEXT, new QTableWidgetItem(tr("Title")));
m_slot_table[i]->setHorizontalHeaderItem(COLUMN_INDEX_ICON, new QTableWidgetItem(tr("Icon")));
m_slot_table[i]->setHorizontalHeaderItem(COLUMN_INDEX_BLOCKS,
new QTableWidgetItem(tr("Blocks")));
2020-08-10 19:11:04 +00:00
m_slot_table[i]->setColumnWidth(COLUMN_INDEX_FILENAME, COLUMN_WIDTH_FILENAME);
m_slot_table[i]->setColumnWidth(COLUMN_INDEX_BANNER, COLUMN_WIDTH_BANNER);
m_slot_table[i]->setColumnWidth(COLUMN_INDEX_TEXT, COLUMN_WIDTH_TEXT);
m_slot_table[i]->setColumnWidth(COLUMN_INDEX_ICON, COLUMN_WIDTH_ICON);
m_slot_table[i]->setColumnWidth(COLUMN_INDEX_BLOCKS, COLUMN_WIDTH_BLOCKS);
m_slot_table[i]->verticalHeader()->setDefaultSectionSize(ROW_HEIGHT);
2018-01-25 18:54:50 +00:00
m_slot_table[i]->verticalHeader()->hide();
m_slot_table[i]->setShowGrid(false);
2018-01-25 18:54:50 +00:00
auto* slot_layout = new QGridLayout;
m_slot_group[i]->setLayout(slot_layout);
slot_layout->addWidget(m_slot_file_edit[i], 0, 0);
slot_layout->addWidget(m_slot_open_button[i], 0, 1);
slot_layout->addWidget(m_slot_create_button[i], 0, 2);
slot_layout->addWidget(m_slot_table[i], 1, 0, 1, 3);
2018-01-25 18:54:50 +00:00
slot_layout->addWidget(m_slot_stat_label[i], 2, 0);
layout->addWidget(m_slot_group[i], 0, i * 2, 8, 1);
UpdateSlotTable(i);
2018-01-25 18:54:50 +00:00
}
layout->addWidget(m_select_button, 1, 1);
layout->addWidget(m_copy_button, 2, 1);
layout->addWidget(m_delete_button, 3, 1);
layout->addWidget(m_export_button, 4, 1);
layout->addWidget(m_import_button, 5, 1);
layout->addWidget(m_fix_checksums_button, 6, 1);
layout->addWidget(m_button_box, 8, 2);
2018-01-25 18:54:50 +00:00
setLayout(layout);
}
void GCMemcardManager::ConnectWidgets()
{
2018-03-20 09:12:11 +00:00
connect(m_button_box, &QDialogButtonBox::rejected, this, &QDialog::reject);
connect(m_select_button, &QPushButton::clicked, [this] { SetActiveSlot(!m_active_slot); });
connect(m_export_gci_action, &QAction::triggered,
[this] { ExportFiles(Memcard::SavefileFormat::GCI); });
connect(m_export_gcs_action, &QAction::triggered,
[this] { ExportFiles(Memcard::SavefileFormat::GCS); });
connect(m_export_sav_action, &QAction::triggered,
[this] { ExportFiles(Memcard::SavefileFormat::SAV); });
connect(m_delete_button, &QPushButton::clicked, this, &GCMemcardManager::DeleteFiles);
connect(m_import_button, &QPushButton::clicked, this, &GCMemcardManager::ImportFile);
connect(m_copy_button, &QPushButton::clicked, this, &GCMemcardManager::CopyFiles);
connect(m_fix_checksums_button, &QPushButton::clicked, this, &GCMemcardManager::FixChecksums);
2018-01-25 18:54:50 +00:00
for (int slot = 0; slot < SLOT_COUNT; slot++)
{
connect(m_slot_file_edit[slot], &QLineEdit::textChanged,
2018-01-25 18:54:50 +00:00
[this, slot](const QString& path) { SetSlotFile(slot, path); });
connect(m_slot_open_button[slot], &QPushButton::clicked,
2018-01-25 18:54:50 +00:00
[this, slot] { SetSlotFileInteractive(slot); });
connect(m_slot_create_button[slot], &QPushButton::clicked,
[this, slot] { CreateNewCard(slot); });
2018-01-25 18:54:50 +00:00
connect(m_slot_table[slot], &QTableWidget::itemSelectionChanged, this,
&GCMemcardManager::UpdateActions);
}
}
void GCMemcardManager::LoadDefaultMemcards()
{
for (int i = 0; i < SLOT_COUNT; i++)
{
if (Config::Get(i == 0 ? Config::MAIN_SLOT_A : Config::MAIN_SLOT_B) !=
ExpansionInterface::EXIDEVICE_MEMORYCARD)
{
continue;
}
const QString path = QString::fromStdString(
Config::Get(i == 0 ? Config::MAIN_MEMCARD_A_PATH : Config::MAIN_MEMCARD_B_PATH));
SetSlotFile(i, path);
}
}
2018-01-25 18:54:50 +00:00
void GCMemcardManager::SetActiveSlot(int slot)
{
for (int i = 0; i < SLOT_COUNT; i++)
m_slot_table[i]->setEnabled(i == slot);
2018-01-25 18:54:50 +00:00
m_select_button->setText(slot == 0 ? tr("Switch to B") : tr("Switch to A"));
m_copy_button->setText(slot == 0 ? tr("Copy to B") : tr("Copy to A"));
2018-01-25 18:54:50 +00:00
m_active_slot = slot;
UpdateSlotTable(slot);
UpdateActions();
}
void GCMemcardManager::UpdateSlotTable(int slot)
{
m_slot_active_icons[slot].clear();
2018-01-25 18:54:50 +00:00
if (m_slot_memcard[slot] == nullptr)
{
m_slot_table[slot]->setRowCount(0);
m_slot_stat_label[slot]->clear();
2018-01-25 18:54:50 +00:00
return;
}
2018-01-25 18:54:50 +00:00
auto& memcard = m_slot_memcard[slot];
auto* table = m_slot_table[slot];
table->setSortingEnabled(false);
2018-01-25 18:54:50 +00:00
const u8 num_files = memcard->GetNumFiles();
const u8 free_files = Memcard::DIRLEN - num_files;
const u16 free_blocks = memcard->GetFreeBlocks();
table->setRowCount(num_files);
for (int i = 0; i < num_files; i++)
2018-01-25 18:54:50 +00:00
{
const u8 file_index = memcard->GetFileIndex(i);
2018-01-25 18:54:50 +00:00
const auto file_comments = memcard->GetSaveComments(file_index);
const u16 block_count = memcard->DEntry_BlockCount(file_index);
2020-08-10 19:11:04 +00:00
const auto entry = memcard->GetDEntry(file_index);
const std::string filename = entry ? Memcard::GenerateFilename(*entry) : "";
const QString title =
file_comments ? QString::fromStdString(file_comments->first).trimmed() : QString();
const QString comment =
file_comments ? QString::fromStdString(file_comments->second).trimmed() : QString();
auto banner = GetBannerFromSaveFile(file_index, slot);
auto icon_data = GetIconFromSaveFile(file_index, slot);
2018-01-25 18:54:50 +00:00
2020-08-10 19:11:04 +00:00
auto* item_filename = new QTableWidgetItem(QString::fromStdString(filename));
auto* item_banner = new QTableWidgetItem();
auto* item_text = new QTableWidgetItem(QStringLiteral("%1\n%2").arg(title, comment));
auto* item_icon = new QTableWidgetItem();
auto* item_blocks = new QTableWidgetItem();
2018-01-25 18:54:50 +00:00
item_banner->setData(Qt::DecorationRole, banner);
item_icon->setData(Qt::DecorationRole, icon_data.m_frames[0]);
item_blocks->setData(Qt::DisplayRole, block_count);
2018-01-25 18:54:50 +00:00
2020-08-10 19:11:04 +00:00
for (auto* item : {item_filename, item_banner, item_text, item_icon, item_blocks})
{
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
item->setData(Qt::UserRole, static_cast<int>(file_index));
}
2018-01-25 18:54:50 +00:00
m_slot_active_icons[slot].emplace(file_index, std::move(icon_data));
2018-01-25 18:54:50 +00:00
2020-08-10 19:11:04 +00:00
table->setItem(i, COLUMN_INDEX_FILENAME, item_filename);
table->setItem(i, COLUMN_INDEX_BANNER, item_banner);
table->setItem(i, COLUMN_INDEX_TEXT, item_text);
table->setItem(i, COLUMN_INDEX_ICON, item_icon);
table->setItem(i, COLUMN_INDEX_BLOCKS, item_blocks);
2018-01-25 18:54:50 +00:00
}
const QString free_blocks_string = tr("Free Blocks: %1").arg(free_blocks);
const QString free_files_string = tr("Free Files: %1").arg(free_files);
m_slot_stat_label[slot]->setText(
QStringLiteral("%1 %2").arg(free_blocks_string, free_files_string));
table->setSortingEnabled(true);
2018-01-25 18:54:50 +00:00
}
void GCMemcardManager::UpdateActions()
{
auto selection = m_slot_table[m_active_slot]->selectedItems();
bool have_selection = selection.count();
bool have_memcard = m_slot_memcard[m_active_slot] != nullptr;
bool have_memcard_other = m_slot_memcard[!m_active_slot] != nullptr;
m_copy_button->setEnabled(have_selection && have_memcard_other);
m_export_button->setEnabled(have_selection);
m_import_button->setEnabled(have_memcard);
m_delete_button->setEnabled(have_selection);
m_fix_checksums_button->setEnabled(have_memcard);
}
void GCMemcardManager::SetSlotFile(int slot, QString path)
{
auto [error_code, memcard] = Memcard::GCMemcard::Open(path.toStdString());
2018-01-25 18:54:50 +00:00
if (!error_code.HasCriticalErrors() && memcard && memcard->IsValid())
{
m_slot_file_edit[slot]->setText(path);
m_slot_memcard[slot] = std::make_unique<Memcard::GCMemcard>(std::move(*memcard));
}
else
{
m_slot_memcard[slot] = nullptr;
2020-08-02 14:18:07 +00:00
ModalMessageBox::warning(
this, tr("Error"),
tr("Failed opening memory card:\n%1").arg(GetErrorMessagesForErrorCode(error_code)));
}
2018-01-25 18:54:50 +00:00
UpdateSlotTable(slot);
UpdateActions();
}
void GCMemcardManager::SetSlotFileInteractive(int slot)
{
2018-05-13 19:43:57 +00:00
QString path = QDir::toNativeSeparators(QFileDialog::getOpenFileName(
2018-04-12 12:18:04 +00:00
this,
slot == 0 ? tr("Set memory card file for Slot A") : tr("Set memory card file for Slot B"),
QString::fromStdString(File::GetUserPath(D_GCUSER_IDX)),
2020-08-02 14:18:07 +00:00
QStringLiteral("%1 (*.raw *.gcp);;%2 (*)")
.arg(tr("GameCube Memory Cards"), tr("All Files"))));
2018-01-25 18:54:50 +00:00
if (!path.isEmpty())
m_slot_file_edit[slot]->setText(path);
}
std::vector<u8> GCMemcardManager::GetSelectedFileIndices()
2018-01-25 18:54:50 +00:00
{
const auto selection = m_slot_table[m_active_slot]->selectedItems();
std::vector<bool> lookup(Memcard::DIRLEN);
for (const auto* item : selection)
{
const int index = item->data(Qt::UserRole).toInt();
if (index < 0 || index >= static_cast<int>(Memcard::DIRLEN))
{
ModalMessageBox::warning(this, tr("Error"),
tr("Data inconsistency in GCMemcardManager, aborting action."));
return {};
}
lookup[index] = true;
}
2018-01-25 18:54:50 +00:00
std::vector<u8> selected_indices;
for (u8 i = 0; i < Memcard::DIRLEN; ++i)
{
if (lookup[i])
selected_indices.push_back(i);
}
return selected_indices;
}
2018-01-25 18:54:50 +00:00
static QString GetFormatDescription(Memcard::SavefileFormat format)
{
switch (format)
2018-01-25 18:54:50 +00:00
{
case Memcard::SavefileFormat::GCI:
return QObject::tr("Native GCI File");
case Memcard::SavefileFormat::GCS:
return QObject::tr("MadCatz Gameshark files");
case Memcard::SavefileFormat::SAV:
return QObject::tr("Datel MaxDrive/Pro files");
default:
assert(0);
return QObject::tr("Native GCI File");
}
}
2018-01-25 18:54:50 +00:00
void GCMemcardManager::ExportFiles(Memcard::SavefileFormat format)
{
const auto& memcard = m_slot_memcard[m_active_slot];
if (!memcard)
return;
const auto selected_indices = GetSelectedFileIndices();
if (selected_indices.empty())
return;
const auto savefiles = Memcard::GetSavefiles(*memcard, selected_indices);
if (savefiles.empty())
{
ModalMessageBox::warning(this, tr("Export Failed"),
tr("Failed to read selected savefile(s) from memory card."));
return;
}
std::string extension = Memcard::GetDefaultExtension(format);
if (savefiles.size() == 1)
{
// when exporting a single save file, let user specify exact path
const std::string basename = Memcard::GenerateFilename(savefiles[0].dir_entry);
const QString qformatdesc = GetFormatDescription(format);
const std::string default_path =
fmt::format("{}/{}{}", File::GetUserPath(D_GCUSER_IDX), basename, extension);
const QString qfilename = QFileDialog::getSaveFileName(
this, tr("Export Save File"), QString::fromStdString(default_path),
QStringLiteral("%1 (*%2);;%3 (*)")
.arg(qformatdesc, QString::fromStdString(extension), tr("All Files")));
if (qfilename.isEmpty())
2018-01-25 18:54:50 +00:00
return;
const std::string filename = qfilename.toStdString();
if (!Memcard::WriteSavefile(filename, savefiles[0], format))
2018-01-25 18:54:50 +00:00
{
File::Delete(filename);
ModalMessageBox::warning(this, tr("Export Failed"), tr("Failed to write savefile to disk."));
2018-01-25 18:54:50 +00:00
}
return;
}
const QString qdirpath =
QFileDialog::getExistingDirectory(this, QObject::tr("Export Save Files"),
QString::fromStdString(File::GetUserPath(D_GCUSER_IDX)));
if (qdirpath.isEmpty())
return;
const std::string dirpath = qdirpath.toStdString();
size_t failures = 0;
for (const auto& savefile : savefiles)
{
// find a free filename so we don't overwrite anything
const std::string basepath = dirpath + DIR_SEP + Memcard::GenerateFilename(savefile.dir_entry);
std::string filename = basepath + extension;
if (File::Exists(filename))
2018-01-25 18:54:50 +00:00
{
size_t tmp = 0;
std::string free_name;
do
{
free_name = fmt::format("{}_{}{}", basepath, tmp, extension);
++tmp;
} while (File::Exists(free_name));
filename = free_name;
2018-01-25 18:54:50 +00:00
}
if (!Memcard::WriteSavefile(filename, savefile, format))
2018-01-25 18:54:50 +00:00
{
File::Delete(filename);
++failures;
2018-01-25 18:54:50 +00:00
}
}
if (failures > 0)
{
QString failure_string =
tr("Failed to export %n out of %1 save file(s).", "", static_cast<int>(failures))
.arg(savefiles.size());
if (failures == savefiles.size())
{
ModalMessageBox::warning(this, tr("Export Failed"), failure_string);
}
else
{
QString success_string = tr("Successfully exported %n out of %1 save file(s).", "",
static_cast<int>(savefiles.size() - failures))
.arg(savefiles.size());
ModalMessageBox::warning(this, tr("Export Failed"),
QStringLiteral("%1\n%2").arg(failure_string, success_string));
}
}
2018-01-25 18:54:50 +00:00
}
void GCMemcardManager::ImportFiles(int slot, const std::vector<Memcard::Savefile>& savefiles)
2018-01-25 18:54:50 +00:00
{
auto& card = m_slot_memcard[slot];
if (!card)
return;
const size_t number_of_files = savefiles.size();
const size_t number_of_blocks = Memcard::GetBlockCount(savefiles);
const size_t free_files = Memcard::DIRLEN - card->GetNumFiles();
const size_t free_blocks = card->GetFreeBlocks();
QStringList error_messages;
if (number_of_files > free_files)
{
error_messages.push_back(
tr("Not enough free files on the target memory card. At least %n free file(s) required.",
"", static_cast<int>(number_of_files)));
}
if (number_of_blocks > free_blocks)
{
error_messages.push_back(
tr("Not enough free blocks on the target memory card. At least %n free block(s) required.",
"", static_cast<int>(number_of_blocks)));
}
2018-01-25 18:54:50 +00:00
if (Memcard::HasDuplicateIdentity(savefiles))
{
error_messages.push_back(
tr("At least two of the selected save files have the same internal filename."));
}
for (const Memcard::Savefile& savefile : savefiles)
{
if (card->TitlePresent(savefile.dir_entry))
{
const std::string filename = Memcard::GenerateFilename(savefile.dir_entry);
error_messages.push_back(tr("The target memory card already contains a file \"%1\".")
.arg(QString::fromStdString(filename)));
}
}
if (!error_messages.empty())
{
ModalMessageBox::warning(this, tr("Import Failed"), error_messages.join(QLatin1Char('\n')));
2018-01-25 18:54:50 +00:00
return;
}
2018-01-25 18:54:50 +00:00
for (const Memcard::Savefile& savefile : savefiles)
{
const auto result = card->ImportFile(savefile);
// we've already checked everything that could realistically fail here, so this should only
// happen if the memory card data is corrupted in some way
if (result != Memcard::GCMemcardImportFileRetVal::SUCCESS)
{
const std::string filename = Memcard::GenerateFilename(savefile.dir_entry);
ModalMessageBox::warning(
this, tr("Import Failed"),
tr("Failed to import \"%1\".").arg(QString::fromStdString(filename)));
break;
}
}
if (!card->Save())
{
ModalMessageBox::warning(this, tr("Import Failed"),
tr("Failed to write modified memory card to disk."));
}
UpdateSlotTable(slot);
}
void GCMemcardManager::ImportFile()
{
auto& card = m_slot_memcard[m_active_slot];
if (!card)
return;
const QStringList paths = QFileDialog::getOpenFileNames(
this, tr("Import Save File(s)"), QString::fromStdString(File::GetUserPath(D_GCUSER_IDX)),
QStringLiteral("%1 (*.gci *.gcs *.sav);;%2 (*.gci);;%3 (*.gcs);;%4 (*.sav);;%5 (*)")
.arg(tr("Supported file formats"), GetFormatDescription(Memcard::SavefileFormat::GCI),
GetFormatDescription(Memcard::SavefileFormat::GCS),
GetFormatDescription(Memcard::SavefileFormat::SAV), tr("All Files")));
if (paths.isEmpty())
return;
std::vector<Memcard::Savefile> savefiles;
savefiles.reserve(paths.size());
QStringList errors;
for (const QString& path : paths)
{
auto read_result = Memcard::ReadSavefile(path.toStdString());
std::visit(overloaded{
[&](Memcard::Savefile savefile) { savefiles.emplace_back(std::move(savefile)); },
[&](Memcard::ReadSavefileErrorCode error_code) {
errors.push_back(
tr("%1: %2").arg(path, GetErrorMessageForErrorCode(error_code)));
},
},
std::move(read_result));
}
2018-01-25 18:54:50 +00:00
if (!errors.empty())
{
ModalMessageBox::warning(
this, tr("Import Failed"),
tr("Encountered the following errors while opening save files:\n%1\n\nAborting import.")
.arg(errors.join(QStringLiteral("\n"))));
return;
}
2018-01-25 18:54:50 +00:00
ImportFiles(m_active_slot, savefiles);
2018-01-25 18:54:50 +00:00
}
void GCMemcardManager::CopyFiles()
{
const auto& source_card = m_slot_memcard[m_active_slot];
if (!source_card)
return;
2018-01-25 18:54:50 +00:00
auto& target_card = m_slot_memcard[!m_active_slot];
if (!target_card)
return;
2018-01-25 18:54:50 +00:00
const auto selected_indices = GetSelectedFileIndices();
if (selected_indices.empty())
return;
2018-01-25 18:54:50 +00:00
const auto savefiles = Memcard::GetSavefiles(*source_card, selected_indices);
if (savefiles.empty())
{
ModalMessageBox::warning(this, tr("Copy Failed"),
tr("Failed to read selected savefile(s) from memory card."));
return;
}
ImportFiles(!m_active_slot, savefiles);
2018-01-25 18:54:50 +00:00
}
void GCMemcardManager::DeleteFiles()
{
auto& card = m_slot_memcard[m_active_slot];
if (!card)
return;
2018-01-25 18:54:50 +00:00
const auto selected_indices = GetSelectedFileIndices();
if (selected_indices.empty())
return;
2018-01-25 18:54:50 +00:00
const QString text = tr("Do you want to delete the %n selected save file(s)?", "",
static_cast<int>(selected_indices.size()));
const auto response = ModalMessageBox::question(this, tr("Question"), text);
if (response != QMessageBox::Yes)
return;
for (const u8 index : selected_indices)
{
if (card->RemoveFile(index) != Memcard::GCMemcardRemoveFileRetVal::SUCCESS)
{
ModalMessageBox::warning(this, tr("Remove Failed"), tr("Failed to remove file."));
break;
}
2018-01-25 18:54:50 +00:00
}
if (!card->Save())
{
ModalMessageBox::warning(this, tr("Remove Failed"),
tr("Failed to write modified memory card to disk."));
}
2018-01-25 18:54:50 +00:00
UpdateSlotTable(m_active_slot);
UpdateActions();
}
void GCMemcardManager::FixChecksums()
{
auto& memcard = m_slot_memcard[m_active_slot];
memcard->FixChecksums();
if (!memcard->Save())
{
ModalMessageBox::warning(this, tr("Fix Checksums Failed"),
tr("Failed to write modified memory card to disk."));
}
2018-01-25 18:54:50 +00:00
}
void GCMemcardManager::CreateNewCard(int slot)
{
GCMemcardCreateNewDialog dialog(this);
if (dialog.exec() == QDialog::Accepted)
m_slot_file_edit[slot]->setText(QString::fromStdString(dialog.GetMemoryCardPath()));
}
2018-01-25 18:54:50 +00:00
void GCMemcardManager::DrawIcons()
{
const int column = COLUMN_INDEX_ICON;
for (int slot = 0; slot < SLOT_COUNT; slot++)
2018-01-25 18:54:50 +00:00
{
QTableWidget* table = m_slot_table[slot];
const int row_count = table->rowCount();
if (row_count <= 0)
continue;
const auto viewport = table->viewport();
const int viewport_first_row = table->indexAt(viewport->rect().topLeft()).row();
if (viewport_first_row >= row_count)
2019-11-17 19:45:24 +00:00
continue;
const int first_row = viewport_first_row < 0 ? 0 : viewport_first_row;
const int viewport_last_row = table->indexAt(viewport->rect().bottomLeft()).row();
const int last_row =
viewport_last_row < 0 ? (row_count - 1) : std::min(viewport_last_row, row_count - 1);
2019-11-17 19:45:24 +00:00
for (int row = first_row; row <= last_row; ++row)
{
auto* item = table->item(row, column);
if (!item)
continue;
const u8 index = static_cast<u8>(item->data(Qt::UserRole).toInt());
auto it = m_slot_active_icons[slot].find(index);
if (it == m_slot_active_icons[slot].end())
continue;
const auto& icon = it->second;
2019-11-17 19:45:24 +00:00
// this icon doesn't have an animation
if (icon.m_frames.size() <= 1)
continue;
const u64 prev_time_in_animation = (m_current_frame - 1) % icon.m_frame_timing.size();
const u8 prev_frame = icon.m_frame_timing[prev_time_in_animation];
const u64 current_time_in_animation = m_current_frame % icon.m_frame_timing.size();
const u8 current_frame = icon.m_frame_timing[current_time_in_animation];
2018-01-25 18:54:50 +00:00
2019-11-17 19:45:24 +00:00
if (prev_frame == current_frame)
continue;
item->setData(Qt::DecorationRole, icon.m_frames[current_frame]);
}
2018-01-25 18:54:50 +00:00
}
++m_current_frame;
2018-01-25 18:54:50 +00:00
}
QPixmap GCMemcardManager::GetBannerFromSaveFile(int file_index, int slot)
2018-01-25 18:54:50 +00:00
{
auto& memcard = m_slot_memcard[slot];
2018-01-25 18:54:50 +00:00
auto pxdata = memcard->ReadBannerRGBA8(file_index);
2018-01-25 18:54:50 +00:00
QImage image;
if (pxdata)
2018-01-25 18:54:50 +00:00
{
image = QImage(reinterpret_cast<u8*>(pxdata->data()), Memcard::MEMORY_CARD_BANNER_WIDTH,
Memcard::MEMORY_CARD_BANNER_HEIGHT, QImage::Format_ARGB32);
2018-01-25 18:54:50 +00:00
}
return QPixmap::fromImage(image);
}
GCMemcardManager::IconAnimationData GCMemcardManager::GetIconFromSaveFile(int file_index, int slot)
2018-01-25 18:54:50 +00:00
{
auto& memcard = m_slot_memcard[slot];
2018-01-25 18:54:50 +00:00
IconAnimationData frame_data;
2018-01-25 18:54:50 +00:00
const auto decoded_data = memcard->ReadAnimRGBA8(file_index);
2018-01-25 18:54:50 +00:00
// Decode Save File Animation
if (decoded_data && !decoded_data->empty())
2018-01-25 18:54:50 +00:00
{
frame_data.m_frames.reserve(decoded_data->size());
for (size_t f = 0; f < decoded_data->size(); ++f)
2018-01-25 18:54:50 +00:00
{
QImage img(reinterpret_cast<const u8*>((*decoded_data)[f].image_data.data()),
Memcard::MEMORY_CARD_ICON_WIDTH, Memcard::MEMORY_CARD_ICON_HEIGHT,
QImage::Format_ARGB32);
frame_data.m_frames.push_back(QPixmap::fromImage(img));
for (int i = 0; i < (*decoded_data)[f].delay; ++i)
{
frame_data.m_frame_timing.push_back(static_cast<u8>(f));
}
2018-01-25 18:54:50 +00:00
}
const bool is_pingpong = memcard->DEntry_IsPingPong(file_index);
if (is_pingpong && decoded_data->size() >= 3)
2018-01-25 18:54:50 +00:00
{
// if the animation 'ping-pongs' between start and end then the animation frame order is
// something like 'abcdcbabcdcba' instead of the usual 'abcdabcdabcd'
// to display that correctly just append all except the first and last frame in reverse order
// at the end of the animation
for (size_t f = decoded_data->size() - 2; f > 0; --f)
2018-01-25 18:54:50 +00:00
{
for (int i = 0; i < (*decoded_data)[f].delay; ++i)
2018-01-25 18:54:50 +00:00
{
frame_data.m_frame_timing.push_back(static_cast<u8>(f));
2018-01-25 18:54:50 +00:00
}
}
}
}
else
{
// No Animation found, use an empty placeholder instead.
frame_data.m_frames.emplace_back();
frame_data.m_frame_timing.push_back(0);
2018-01-25 18:54:50 +00:00
}
return frame_data;
2018-01-25 18:54:50 +00:00
}
QString GCMemcardManager::GetErrorMessagesForErrorCode(const Memcard::GCMemcardErrorCode& code)
{
QStringList sl;
if (code.Test(Memcard::GCMemcardValidityIssues::FAILED_TO_OPEN))
sl.push_back(tr("Couldn't open file."));
if (code.Test(Memcard::GCMemcardValidityIssues::IO_ERROR))
sl.push_back(tr("Couldn't read file."));
if (code.Test(Memcard::GCMemcardValidityIssues::INVALID_CARD_SIZE))
sl.push_back(tr("Filesize does not match any known GameCube Memory Card size."));
if (code.Test(Memcard::GCMemcardValidityIssues::MISMATCHED_CARD_SIZE))
sl.push_back(tr("Filesize in header mismatches actual card size."));
if (code.Test(Memcard::GCMemcardValidityIssues::INVALID_CHECKSUM))
sl.push_back(tr("Invalid checksums."));
if (code.Test(Memcard::GCMemcardValidityIssues::FREE_BLOCK_MISMATCH))
sl.push_back(tr("Mismatch between free block count in header and actually unused blocks."));
if (code.Test(Memcard::GCMemcardValidityIssues::DIR_BAT_INCONSISTENT))
sl.push_back(tr("Mismatch between internal data structures."));
if (code.Test(Memcard::GCMemcardValidityIssues::DATA_IN_UNUSED_AREA))
sl.push_back(tr("Data in area of file that should be unused."));
if (sl.empty())
return tr("No errors.");
return sl.join(QLatin1Char{'\n'});
}
QString GCMemcardManager::GetErrorMessageForErrorCode(Memcard::ReadSavefileErrorCode code)
{
switch (code)
{
case Memcard::ReadSavefileErrorCode::OpenFileFail:
return tr("Failed to open file.");
case Memcard::ReadSavefileErrorCode::IOError:
return tr("Failed to read from file.");
case Memcard::ReadSavefileErrorCode::DataCorrupted:
return tr("Data in unrecognized format or corrupted.");
default:
return tr("Unknown error.");
}
}