patch_manager: implement serials and app_versions

This commit is contained in:
Megamouse 2020-06-26 02:58:06 +02:00
parent a7a5034958
commit 12dded403f
9 changed files with 596 additions and 278 deletions

View file

@ -850,7 +850,7 @@ game_boot_result Emulator::Load(const std::string& title_id, bool add_only, bool
m_title_id = psf::get_string(_psf, "TITLE_ID");
m_cat = psf::get_string(_psf, "CATEGORY");
const std::string version_app = psf::get_string(_psf, "APP_VER", "Unknown");
m_app_version = psf::get_string(_psf, "APP_VER", "Unknown");
const std::string version_disc = psf::get_string(_psf, "VERSION", "Unknown");
if (!_psf.empty() && m_cat.empty())
@ -862,7 +862,7 @@ game_boot_result Emulator::Load(const std::string& title_id, bool add_only, bool
sys_log.notice("Title: %s", GetTitle());
sys_log.notice("Serial: %s", GetTitleID());
sys_log.notice("Category: %s", GetCat());
sys_log.notice("Version: %s / %s", version_app, version_disc);
sys_log.notice("Version: %s / %s", GetAppVersion(), version_disc);
if (!add_only && !force_global_config)
{

View file

@ -64,6 +64,7 @@ class Emulator final
std::string m_path_old;
std::string m_title_id;
std::string m_title;
std::string m_app_version;
std::string m_cat;
std::string m_dir;
std::string m_sfo_dir;
@ -131,6 +132,11 @@ public:
return m_title + (m_title_id.empty() ? "" : " [" + m_title_id + "]");
}
const std::string& GetAppVersion() const
{
return m_app_version;
}
const std::string& GetCat() const
{
return m_cat;

View file

@ -32,7 +32,11 @@ enum patch_column : int
enum patch_role : int
{
hash_role = Qt::UserRole,
title_role,
serial_role,
app_version_role,
description_role,
patch_group_role,
persistance_role,
node_level_role
};
@ -41,6 +45,7 @@ enum node_level : int
{
title_level,
serial_level,
app_version_level,
patch_level
};
@ -166,77 +171,122 @@ void patch_manager_dialog::populate_tree()
continue;
}
const QString q_title = patch.title.empty() ? tr("Unknown Title") : QString::fromStdString(patch.title);
const QString q_serials = patch.serials.empty() ? tr("Unknown Version") : QString::fromStdString(patch.serials);
const QString q_patch_group = QString::fromStdString(patch.patch_group);
QTreeWidgetItem* title_level_item = nullptr;
// Find top level item for this title
if (const auto list = ui->patch_tree->findItems(q_title, Qt::MatchFlag::MatchExactly, 0); list.size() > 0)
for (const auto& [title, serials] : patch.titles)
{
title_level_item = list[0];
}
// Add a top level item for this title if it doesn't exist yet
if (!title_level_item)
{
title_level_item = new QTreeWidgetItem();
title_level_item->setText(0, q_title);
title_level_item->setData(0, hash_role, q_hash);
title_level_item->setData(0, node_level_role, node_level::title_level);
ui->patch_tree->addTopLevelItem(title_level_item);
}
assert(title_level_item);
// Find out if there is a node item for this serial
QTreeWidgetItem* serial_level_item = gui::utils::find_child(title_level_item, q_serials);
// Add a node item for this serial if it doesn't exist yet
if (!serial_level_item)
{
serial_level_item = new QTreeWidgetItem();
serial_level_item->setText(0, q_serials);
serial_level_item->setData(0, hash_role, q_hash);
serial_level_item->setData(0, node_level_role, node_level::serial_level);
title_level_item->addChild(serial_level_item);
}
assert(serial_level_item);
// Add a checkable leaf item for this patch
const QString q_description = QString::fromStdString(description);
QString visible_description = q_description;
const auto match_criteria = QList<QPair<int, QVariant>>() << QPair(description_role, q_description) << QPair(persistance_role, true);
// Add counter to leafs if the name already exists due to different hashes of the same game (PPU, SPU, PRX, OVL)
if (const auto matches = gui::utils::find_children_by_data(serial_level_item, match_criteria); matches.count() > 0)
{
if (auto only_match = matches.count() == 1 ? matches[0] : nullptr)
if (serials.empty())
{
only_match->setText(0, q_description + QStringLiteral(" (1)"));
continue;
}
const QString q_title = QString::fromStdString(title);
QTreeWidgetItem* title_level_item = nullptr;
// Find top level item for this title
if (const auto list = ui->patch_tree->findItems(q_title, Qt::MatchFlag::MatchExactly, 0); list.size() > 0)
{
title_level_item = list[0];
}
// Add a top level item for this title if it doesn't exist yet
if (!title_level_item)
{
title_level_item = new QTreeWidgetItem();
title_level_item->setText(0, q_title);
title_level_item->setData(0, title_role, q_title);
title_level_item->setData(0, node_level_role, node_level::title_level);
title_level_item->setData(0, persistance_role, true);
ui->patch_tree->addTopLevelItem(title_level_item);
}
assert(title_level_item);
for (const auto& [serial, app_versions] : serials)
{
if (app_versions.empty())
{
continue;
}
const QString q_serial = QString::fromStdString(serial);
// Find out if there is a node item for this serial
QTreeWidgetItem* serial_level_item = gui::utils::find_child(title_level_item, q_serial);
// Add a node item for this serial if it doesn't exist yet
if (!serial_level_item)
{
serial_level_item = new QTreeWidgetItem();
serial_level_item->setText(0, q_serial);
serial_level_item->setData(0, title_role, q_title);
serial_level_item->setData(0, serial_role, q_serial);
serial_level_item->setData(0, node_level_role, node_level::serial_level);
serial_level_item->setData(0, persistance_role, true);
title_level_item->addChild(serial_level_item);
}
assert(serial_level_item);
for (const auto& [app_version, enabled] : app_versions)
{
const QString q_app_version = QString::fromStdString(app_version);
// Find out if there is a node item for this app version
QTreeWidgetItem* app_version_level_item = gui::utils::find_child(serial_level_item, q_app_version);
// Add a node item for this app version if it doesn't exist yet
if (!app_version_level_item)
{
app_version_level_item = new QTreeWidgetItem();
app_version_level_item->setText(0, q_app_version);
app_version_level_item->setData(0, title_role, q_title);
app_version_level_item->setData(0, serial_role, q_serial);
app_version_level_item->setData(0, app_version_role, q_app_version);
app_version_level_item->setData(0, node_level_role, node_level::app_version_level);
app_version_level_item->setData(0, persistance_role, true);
serial_level_item->addChild(app_version_level_item);
}
assert(app_version_level_item);
// Add a checkable leaf item for this patch
const QString q_description = QString::fromStdString(description);
QString visible_description = q_description;
const auto match_criteria = QList<QPair<int, QVariant>>() << QPair(description_role, q_description) << QPair(persistance_role, true);
// Add counter to leafs if the name already exists due to different hashes of the same game (PPU, SPU, PRX, OVL)
if (const auto matches = gui::utils::find_children_by_data(app_version_level_item, match_criteria, false); matches.count() > 0)
{
if (auto only_match = matches.count() == 1 ? matches[0] : nullptr)
{
only_match->setText(0, q_description + QStringLiteral(" (1)"));
}
visible_description += QStringLiteral(" (") + QString::number(matches.count() + 1) + QStringLiteral(")");
}
QTreeWidgetItem* patch_level_item = new QTreeWidgetItem();
patch_level_item->setText(0, visible_description);
patch_level_item->setCheckState(0, enabled ? Qt::CheckState::Checked : Qt::CheckState::Unchecked);
patch_level_item->setData(0, hash_role, q_hash);
patch_level_item->setData(0, title_role, q_title);
patch_level_item->setData(0, serial_role, q_serial);
patch_level_item->setData(0, app_version_role, q_app_version);
patch_level_item->setData(0, description_role, q_description);
patch_level_item->setData(0, patch_group_role, q_patch_group);
patch_level_item->setData(0, node_level_role, node_level::patch_level);
patch_level_item->setData(0, persistance_role, true);
app_version_level_item->addChild(patch_level_item);
}
}
visible_description += QStringLiteral(" (") + QString::number(matches.count() + 1) + QStringLiteral(")");
}
QTreeWidgetItem* patch_level_item = new QTreeWidgetItem();
patch_level_item->setText(0, visible_description);
patch_level_item->setCheckState(0, patch.enabled ? Qt::CheckState::Checked : Qt::CheckState::Unchecked);
patch_level_item->setData(0, hash_role, q_hash);
patch_level_item->setData(0, description_role, q_description);
patch_level_item->setData(0, node_level_role, node_level::patch_level);
serial_level_item->addChild(patch_level_item);
// Persist items
title_level_item->setData(0, persistance_role, true);
serial_level_item->setData(0, persistance_role, true);
patch_level_item->setData(0, persistance_role, true);
}
}
ui->patch_tree->sortByColumn(0, Qt::SortOrder::AscendingOrder);
const auto match_criteria = QList<QPair<int, QVariant>>() << QPair(persistance_role, true);
for (int i = ui->patch_tree->topLevelItemCount() - 1; i >= 0; i--)
{
@ -248,34 +298,11 @@ void patch_manager_dialog::populate_tree()
continue;
}
title_level_item->sortChildren(0, Qt::SortOrder::AscendingOrder);
for (int j = title_level_item->childCount() - 1; j >= 0; j--)
{
if (auto serial_level_item = title_level_item->child(j))
{
if (!serial_level_item->data(0, persistance_role).toBool())
{
delete title_level_item->takeChild(j);
continue;
}
for (int k = serial_level_item->childCount() - 1; k >= 0; k--)
{
if (auto leaf_item = serial_level_item->child(k))
{
if (!leaf_item->data(0, persistance_role).toBool())
{
delete serial_level_item->takeChild(k);
}
}
}
serial_level_item->sortChildren(0, Qt::SortOrder::AscendingOrder);
}
}
gui::utils::remove_children(title_level_item, match_criteria, true);
}
}
gui::utils::sort_tree(ui->patch_tree, Qt::SortOrder::AscendingOrder, true);
}
void patch_manager_dialog::save_config()
@ -314,73 +341,74 @@ void patch_manager_dialog::filter_patches(const QString& term)
}
}
void patch_manager_dialog::update_patch_info(const patch_engine::patch_info& info)
void patch_manager_dialog::update_patch_info(const patch_manager_dialog::gui_patch_info& info)
{
ui->label_hash->setText(QString::fromStdString(info.hash));
ui->label_author->setText(QString::fromStdString(info.author));
ui->label_notes->setText(QString::fromStdString(info.notes));
ui->label_description->setText(QString::fromStdString(info.description));
ui->label_patch_version->setText(QString::fromStdString(info.patch_version));
ui->label_serials->setText(QString::fromStdString(info.serials));
ui->label_title->setText(QString::fromStdString(info.title));
ui->label_hash->setText(info.hash);
ui->label_author->setText(info.author);
ui->label_notes->setText(info.notes);
ui->label_description->setText(info.description);
ui->label_patch_version->setText(info.patch_version);
ui->label_serial->setText(info.serial);
ui->label_title->setText(info.title);
ui->label_app_version->setText(info.app_version);
}
void patch_manager_dialog::on_item_selected(QTreeWidgetItem *current, QTreeWidgetItem * /*previous*/)
{
if (!current)
{
// Clear patch info if no item is selected
update_patch_info({});
return;
}
// Get patch identifiers stored in item data
const node_level level = static_cast<node_level>(current->data(0, node_level_role).toInt());
const std::string hash = current->data(0, hash_role).toString().toStdString();
std::string description = current->data(0, description_role).toString().toStdString();
if (m_map.find(hash) != m_map.end())
patch_manager_dialog::gui_patch_info info{};
switch (level)
{
const auto& container = m_map.at(hash);
case node_level::patch_level:
{
// Get patch identifiers stored in item data
info.hash = current->data(0, hash_role).toString();
const std::string hash = info.hash.toStdString();
const std::string description = current->data(0, description_role).toString().toStdString();
// Find the patch for this item and show its metadata
if (!container.is_legacy && container.patch_info_map.find(description) != container.patch_info_map.end())
// Find the patch for this item and get its metadata
if (m_map.find(hash) != m_map.end())
{
update_patch_info(container.patch_info_map.at(description));
return;
}
const auto& container = m_map.at(hash);
// Show shared info if no patch was found
patch_engine::patch_info info{};
info.hash = hash;
info.version = container.version;
// Use the first entry for more shared information
const auto match_criteria = QList<QPair<int, QVariant>>() << QPair(node_level_role, node_level::patch_level);
if (const auto matches = gui::utils::find_children_by_data(current, match_criteria, true); matches.count() > 0 && matches[0])
{
description = matches[0]->data(0, description_role).toString().toStdString();
if (container.patch_info_map.find(description) != container.patch_info_map.end())
if (!container.is_legacy && container.patch_info_map.find(description) != container.patch_info_map.end())
{
const auto& fallback_info = container.patch_info_map.at(description);
if (level >= node_level::title_level)
{
info.title = fallback_info.title;
}
if (level >= node_level::serial_level)
{
info.serials = fallback_info.serials;
}
const auto& found_info = container.patch_info_map.at(description);
info.author = QString::fromStdString(found_info.author);
info.notes = QString::fromStdString(found_info.notes);
info.description = QString::fromStdString(found_info.description);
info.patch_version = QString::fromStdString(found_info.patch_version);
}
}
update_patch_info(info);
return;
[[fallthrough]];
}
case node_level::app_version_level:
{
info.app_version = current->data(0, app_version_role).toString();
[[fallthrough]];
}
case node_level::serial_level:
{
info.serial = current->data(0, serial_role).toString();
[[fallthrough]];
}
case node_level::title_level:
default:
{
info.title = current->data(0, title_role).toString();
break;
}
}
// Clear patch info if no info was found
patch_engine::patch_info info{};
update_patch_info(info);
}
@ -395,8 +423,30 @@ void patch_manager_dialog::on_item_changed(QTreeWidgetItem *item, int /*column*/
const bool enabled = item->checkState(0) == Qt::CheckState::Checked;
// Get patch identifiers stored in item data
const node_level level = static_cast<node_level>(item->data(0, node_level_role).toInt());
const std::string hash = item->data(0, hash_role).toString().toStdString();
const std::string title = item->data(0, title_role).toString().toStdString();
const std::string serial = item->data(0, serial_role).toString().toStdString();
const std::string app_version = item->data(0, app_version_role).toString().toStdString();
const std::string description = item->data(0, description_role).toString().toStdString();
const std::string patch_group = item->data(0, patch_group_role).toString().toStdString();
// Uncheck other patches with the same patch_group if this patch was enabled
if (const auto node = item->parent(); node && enabled && !patch_group.empty() && level == node_level::patch_level)
{
for (int i = 0; i < node->childCount(); i++)
{
if (const auto other = node->child(i); other && other != item)
{
const std::string other_patch_group = other->data(0, patch_group_role).toString().toStdString();
if (other_patch_group == patch_group)
{
other->setCheckState(0, Qt::CheckState::Unchecked);
}
}
}
}
// Enable/disable the patch for this item and show its metadata
if (m_map.find(hash) != m_map.end())
@ -405,9 +455,8 @@ void patch_manager_dialog::on_item_changed(QTreeWidgetItem *item, int /*column*/
if (!container.is_legacy && container.patch_info_map.find(description) != container.patch_info_map.end())
{
auto& patch = m_map[hash].patch_info_map[description];
patch.enabled = enabled;
update_patch_info(patch);
m_map[hash].patch_info_map[description].titles[title][serial][app_version] = enabled;
on_item_selected(item, nullptr);
return;
}
}

View file

@ -18,6 +18,18 @@ class patch_manager_dialog : public QDialog
{
Q_OBJECT
struct gui_patch_info
{
QString hash;
QString title;
QString serial;
QString app_version;
QString author;
QString notes;
QString description;
QString patch_version;
};
public:
explicit patch_manager_dialog(std::shared_ptr<gui_settings> gui_settings, QWidget* parent = nullptr);
~patch_manager_dialog();
@ -36,7 +48,7 @@ private:
void load_patches();
void populate_tree();
void save_config();
void update_patch_info(const patch_engine::patch_info& info);
void update_patch_info(const gui_patch_info& info);
bool is_valid_file(const QMimeData& md, QStringList* drop_paths = nullptr);
std::shared_ptr<gui_settings> m_gui_settings;

View file

@ -87,25 +87,6 @@
<string>Patch Information</string>
</property>
<layout class="QVBoxLayout" name="patch_info_gb_layout">
<item>
<widget class="QGroupBox" name="gb_hash">
<property name="title">
<string>Hash</string>
</property>
<layout class="QVBoxLayout" name="hash_layout">
<item>
<widget class="QLabel" name="label_hash">
<property name="text">
<string/>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="gb_title">
<property name="title">
@ -126,13 +107,48 @@
</widget>
</item>
<item>
<widget class="QGroupBox" name="gb_serials">
<widget class="QGroupBox" name="gb_serial">
<property name="title">
<string>Serials</string>
<string>Serial</string>
</property>
<layout class="QVBoxLayout" name="serials_layout">
<layout class="QVBoxLayout" name="serial_layout">
<item>
<widget class="QLabel" name="label_serials">
<widget class="QLabel" name="label_serial">
<property name="text">
<string/>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="gb_app_version">
<property name="title">
<string>App Version</string>
</property>
<layout class="QVBoxLayout" name="layout_app_version">
<item>
<widget class="QLabel" name="label_app_version">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="gb_hash">
<property name="title">
<string>Hash</string>
</property>
<layout class="QVBoxLayout" name="hash_layout">
<item>
<widget class="QLabel" name="label_hash">
<property name="text">
<string/>
</property>

View file

@ -382,11 +382,75 @@ namespace gui
{
if (parent)
{
for (int i = 0; i < parent->childCount(); i++)
for (int i = parent->childCount() - 1; i >= 0; i--)
{
parent->removeChild(parent->child(i));
}
}
}
void remove_children(QTreeWidgetItem* parent, const QList<QPair<int /*role*/, QVariant /*data*/>>& criteria, bool recursive)
{
if (parent)
{
for (int i = parent->childCount() - 1; i >= 0; i--)
{
if (auto item = parent->child(i))
{
bool match = true;
for (const auto [role, data] : criteria)
{
if (item->data(0, role) != data)
{
match = false;
break;
}
}
if (!match)
{
parent->removeChild(item);
}
else if (recursive)
{
remove_children(item, criteria, recursive);
}
}
}
}
}
void sort_tree_item(QTreeWidgetItem* item, Qt::SortOrder sort_order, bool recursive)
{
if (item)
{
item->sortChildren(0, sort_order);
if (recursive)
{
for (int i = 0; i < item->childCount(); i++)
{
sort_tree_item(item->child(i), sort_order, recursive);
}
}
}
}
void sort_tree(QTreeWidget* tree, Qt::SortOrder sort_order, bool recursive)
{
if (tree)
{
tree->sortByColumn(0, sort_order);
if (recursive)
{
for (int i = 0; i < tree->topLevelItemCount(); i++)
{
sort_tree_item(tree->topLevelItem(i), sort_order, recursive);
}
}
}
}
} // utils
} // gui

View file

@ -71,12 +71,18 @@ namespace gui
QTreeWidgetItem* find_child(QTreeWidgetItem* parent, const QString& text);
// Finds all children of a QTreeWidgetItem that match the given criteria
QList<QTreeWidgetItem*> find_children_by_data(QTreeWidgetItem* parent, const QList<QPair<int /*role*/, QVariant /*data*/>>& criteria, bool recursive = false);
QList<QTreeWidgetItem*> find_children_by_data(QTreeWidgetItem* parent, const QList<QPair<int /*role*/, QVariant /*data*/>>& criteria, bool recursive);
// Constructs and adds a child to a QTreeWidgetItem
QTreeWidgetItem* add_child(QTreeWidgetItem* parent, const QString& text, int column = 0);
// Removes all children of a QTreeWidgetItem
void remove_children(QTreeWidgetItem* parent);
// Removes all children of a QTreeWidgetItem that don't match the given criteria
void remove_children(QTreeWidgetItem* parent, const QList<QPair<int /*role*/, QVariant /*data*/>>& criteria, bool recursive);
// Sort a QTreeWidget (currently only column 0)
void sort_tree(QTreeWidget* tree, Qt::SortOrder sort_order, bool recursive);
} // utils
} // gui