2018-09-07 21:08:08 +01:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
|
|
|
#include "common/common_paths.h"
|
|
|
|
#include "common/file_util.h"
|
|
|
|
#include "core/file_sys/content_archive.h"
|
|
|
|
#include "core/file_sys/control_metadata.h"
|
|
|
|
#include "core/file_sys/mode.h"
|
|
|
|
#include "core/file_sys/nca_metadata.h"
|
|
|
|
#include "core/file_sys/patch_manager.h"
|
|
|
|
#include "core/file_sys/registered_cache.h"
|
|
|
|
#include "core/hle/service/filesystem/filesystem.h"
|
|
|
|
#include "core/loader/loader.h"
|
2018-09-10 00:09:37 +01:00
|
|
|
#include "yuzu/compatibility_list.h"
|
2018-09-07 21:08:08 +01:00
|
|
|
#include "yuzu/game_list.h"
|
|
|
|
#include "yuzu/game_list_p.h"
|
|
|
|
#include "yuzu/game_list_worker.h"
|
|
|
|
#include "yuzu/ui_settings.h"
|
|
|
|
|
|
|
|
namespace {
|
2018-10-09 19:22:31 +01:00
|
|
|
void GetMetadataFromControlNCA(const FileSys::PatchManager& patch_manager, const FileSys::NCA& nca,
|
|
|
|
std::vector<u8>& icon, std::string& name) {
|
2018-09-07 21:08:08 +01:00
|
|
|
auto [nacp, icon_file] = patch_manager.ParseControlNCA(nca);
|
|
|
|
if (icon_file != nullptr)
|
|
|
|
icon = icon_file->ReadAllBytes();
|
|
|
|
if (nacp != nullptr)
|
|
|
|
name = nacp->GetApplicationName();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasSupportedFileExtension(const std::string& file_name) {
|
|
|
|
const QFileInfo file = QFileInfo(QString::fromStdString(file_name));
|
|
|
|
return GameList::supported_file_extensions.contains(file.suffix(), Qt::CaseInsensitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsExtractedNCAMain(const std::string& file_name) {
|
|
|
|
return QFileInfo(QString::fromStdString(file_name)).fileName() == "main";
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FormatGameName(const std::string& physical_name) {
|
|
|
|
const QString physical_name_as_qstring = QString::fromStdString(physical_name);
|
|
|
|
const QFileInfo file_info(physical_name_as_qstring);
|
|
|
|
|
|
|
|
if (IsExtractedNCAMain(physical_name)) {
|
|
|
|
return file_info.dir().path();
|
|
|
|
}
|
|
|
|
|
|
|
|
return physical_name_as_qstring;
|
|
|
|
}
|
|
|
|
|
2018-09-25 14:21:06 +01:00
|
|
|
QString FormatPatchNameVersions(const FileSys::PatchManager& patch_manager,
|
|
|
|
Loader::AppLoader& loader, bool updatable = true) {
|
2018-09-07 21:08:08 +01:00
|
|
|
QString out;
|
2018-09-25 14:21:06 +01:00
|
|
|
FileSys::VirtualFile update_raw;
|
|
|
|
loader.ReadUpdateRaw(update_raw);
|
|
|
|
for (const auto& kv : patch_manager.GetPatchVersionNames(update_raw)) {
|
2018-10-24 16:25:55 +01:00
|
|
|
const bool is_update = kv.first == "Update";
|
|
|
|
if (!updatable && is_update) {
|
2018-09-07 21:08:08 +01:00
|
|
|
continue;
|
2018-10-24 16:25:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString type = QString::fromStdString(kv.first);
|
2018-09-07 21:08:08 +01:00
|
|
|
|
|
|
|
if (kv.second.empty()) {
|
2018-10-24 16:25:55 +01:00
|
|
|
out.append(QStringLiteral("%1\n").arg(type));
|
2018-09-07 21:08:08 +01:00
|
|
|
} else {
|
2018-09-25 19:07:13 +01:00
|
|
|
auto ver = kv.second;
|
|
|
|
|
|
|
|
// Display container name for packed updates
|
2018-10-24 16:25:55 +01:00
|
|
|
if (is_update && ver == "PACKED") {
|
2018-09-25 19:07:13 +01:00
|
|
|
ver = Loader::GetFileTypeString(loader.GetFileType());
|
2018-10-24 16:25:55 +01:00
|
|
|
}
|
2018-09-25 19:07:13 +01:00
|
|
|
|
2018-10-24 16:25:55 +01:00
|
|
|
out.append(QStringLiteral("%1 (%2)\n").arg(type, QString::fromStdString(ver)));
|
2018-09-07 21:08:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out.chop(1);
|
|
|
|
return out;
|
|
|
|
}
|
2018-12-02 04:23:02 +00:00
|
|
|
|
|
|
|
QList<QStandardItem*> MakeGameListEntry(const std::string& path, const std::string& name,
|
|
|
|
const std::vector<u8>& icon, Loader::AppLoader& loader,
|
|
|
|
u64 program_id, const CompatibilityList& compatibility_list,
|
|
|
|
const FileSys::PatchManager& patch) {
|
|
|
|
const auto it = FindMatchingCompatibilityEntry(compatibility_list, program_id);
|
|
|
|
|
|
|
|
// The game list uses this as compatibility number for untested games
|
|
|
|
QString compatibility{"99"};
|
|
|
|
if (it != compatibility_list.end()) {
|
|
|
|
compatibility = it->second.first;
|
|
|
|
}
|
|
|
|
|
2018-12-05 22:47:03 +00:00
|
|
|
const auto file_type = loader.GetFileType();
|
|
|
|
const auto file_type_string = QString::fromStdString(Loader::GetFileTypeString(file_type));
|
|
|
|
|
2018-12-02 04:23:02 +00:00
|
|
|
QList<QStandardItem*> list{
|
2018-12-05 22:47:03 +00:00
|
|
|
new GameListItemPath(FormatGameName(path), icon, QString::fromStdString(name),
|
|
|
|
file_type_string, program_id),
|
2018-12-02 04:23:02 +00:00
|
|
|
new GameListItemCompat(compatibility),
|
2018-12-05 22:47:03 +00:00
|
|
|
new GameListItem(file_type_string),
|
2018-12-02 04:23:02 +00:00
|
|
|
new GameListItemSize(FileUtil::GetSize(path)),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (UISettings::values.show_add_ons) {
|
|
|
|
list.insert(
|
|
|
|
2, new GameListItem(FormatPatchNameVersions(patch, loader, loader.IsRomFSUpdatable())));
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
2018-09-07 21:08:08 +01:00
|
|
|
} // Anonymous namespace
|
|
|
|
|
2018-09-10 00:09:37 +01:00
|
|
|
GameListWorker::GameListWorker(FileSys::VirtualFilesystem vfs, QString dir_path, bool deep_scan,
|
|
|
|
const CompatibilityList& compatibility_list)
|
2018-09-07 21:08:08 +01:00
|
|
|
: vfs(std::move(vfs)), dir_path(std::move(dir_path)), deep_scan(deep_scan),
|
|
|
|
compatibility_list(compatibility_list) {}
|
|
|
|
|
|
|
|
GameListWorker::~GameListWorker() = default;
|
|
|
|
|
|
|
|
void GameListWorker::AddInstalledTitlesToGameList() {
|
|
|
|
const auto cache = Service::FileSystem::GetUnionContents();
|
2018-12-02 01:32:38 +00:00
|
|
|
const auto installed_games = cache.ListEntriesFilter(FileSys::TitleType::Application,
|
|
|
|
FileSys::ContentRecordType::Program);
|
2018-09-07 21:08:08 +01:00
|
|
|
|
|
|
|
for (const auto& game : installed_games) {
|
2018-12-02 01:32:38 +00:00
|
|
|
const auto file = cache.GetEntryUnparsed(game);
|
2018-09-07 21:08:08 +01:00
|
|
|
std::unique_ptr<Loader::AppLoader> loader = Loader::GetLoader(file);
|
|
|
|
if (!loader)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::vector<u8> icon;
|
|
|
|
std::string name;
|
|
|
|
u64 program_id = 0;
|
|
|
|
loader->ReadProgramId(program_id);
|
|
|
|
|
|
|
|
const FileSys::PatchManager patch{program_id};
|
2018-12-02 01:32:38 +00:00
|
|
|
const auto control = cache.GetEntry(game.title_id, FileSys::ContentRecordType::Control);
|
2018-09-07 21:08:08 +01:00
|
|
|
if (control != nullptr)
|
2018-10-09 19:22:31 +01:00
|
|
|
GetMetadataFromControlNCA(patch, *control, icon, name);
|
2018-09-07 21:08:08 +01:00
|
|
|
|
2018-12-02 04:23:02 +00:00
|
|
|
emit EntryReady(MakeGameListEntry(file->GetFullPath(), name, icon, *loader, program_id,
|
|
|
|
compatibility_list, patch));
|
2018-09-07 21:08:08 +01:00
|
|
|
}
|
|
|
|
|
2018-12-02 01:32:38 +00:00
|
|
|
const auto control_data = cache.ListEntriesFilter(FileSys::TitleType::Application,
|
|
|
|
FileSys::ContentRecordType::Control);
|
2018-09-07 21:08:08 +01:00
|
|
|
|
|
|
|
for (const auto& entry : control_data) {
|
2018-12-02 01:32:38 +00:00
|
|
|
auto nca = cache.GetEntry(entry);
|
2018-10-16 14:05:47 +01:00
|
|
|
if (nca != nullptr) {
|
|
|
|
nca_control_map.insert_or_assign(entry.title_id, std::move(nca));
|
|
|
|
}
|
2018-09-07 21:08:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameListWorker::FillControlMap(const std::string& dir_path) {
|
|
|
|
const auto nca_control_callback = [this](u64* num_entries_out, const std::string& directory,
|
|
|
|
const std::string& virtual_name) -> bool {
|
2018-12-02 03:48:57 +00:00
|
|
|
if (stop_processing) {
|
|
|
|
// Breaks the callback loop
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-07 21:08:08 +01:00
|
|
|
|
2018-12-04 23:39:32 +00:00
|
|
|
const std::string physical_name = directory + DIR_SEP + virtual_name;
|
2018-12-02 03:48:57 +00:00
|
|
|
const QFileInfo file_info(QString::fromStdString(physical_name));
|
|
|
|
if (!file_info.isDir() && file_info.suffix() == QStringLiteral("nca")) {
|
2018-09-07 21:08:08 +01:00
|
|
|
auto nca =
|
2018-10-16 14:05:47 +01:00
|
|
|
std::make_unique<FileSys::NCA>(vfs->OpenFile(physical_name, FileSys::Mode::Read));
|
|
|
|
if (nca->GetType() == FileSys::NCAContentType::Control) {
|
|
|
|
const u64 title_id = nca->GetTitleId();
|
|
|
|
nca_control_map.insert_or_assign(title_id, std::move(nca));
|
|
|
|
}
|
2018-09-07 21:08:08 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
FileUtil::ForeachDirectoryEntry(nullptr, dir_path, nca_control_callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameListWorker::AddFstEntriesToGameList(const std::string& dir_path, unsigned int recursion) {
|
|
|
|
const auto callback = [this, recursion](u64* num_entries_out, const std::string& directory,
|
|
|
|
const std::string& virtual_name) -> bool {
|
2018-12-04 23:39:32 +00:00
|
|
|
if (stop_processing) {
|
|
|
|
// Breaks the callback loop.
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-07 21:08:08 +01:00
|
|
|
|
2018-12-04 23:39:32 +00:00
|
|
|
const std::string physical_name = directory + DIR_SEP + virtual_name;
|
|
|
|
const bool is_dir = FileUtil::IsDirectory(physical_name);
|
2018-09-07 21:08:08 +01:00
|
|
|
if (!is_dir &&
|
|
|
|
(HasSupportedFileExtension(physical_name) || IsExtractedNCAMain(physical_name))) {
|
2018-12-05 22:58:11 +00:00
|
|
|
auto loader = Loader::GetLoader(vfs->OpenFile(physical_name, FileSys::Mode::Read));
|
|
|
|
if (!loader) {
|
2018-09-07 21:08:08 +01:00
|
|
|
return true;
|
2018-12-05 22:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto file_type = loader->GetFileType();
|
|
|
|
if ((file_type == Loader::FileType::Unknown || file_type == Loader::FileType::Error) &&
|
|
|
|
!UISettings::values.show_unknown) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-07 21:08:08 +01:00
|
|
|
|
|
|
|
std::vector<u8> icon;
|
|
|
|
const auto res1 = loader->ReadIcon(icon);
|
|
|
|
|
|
|
|
u64 program_id = 0;
|
|
|
|
const auto res2 = loader->ReadProgramId(program_id);
|
|
|
|
|
|
|
|
std::string name = " ";
|
|
|
|
const auto res3 = loader->ReadTitle(name);
|
|
|
|
|
|
|
|
const FileSys::PatchManager patch{program_id};
|
|
|
|
|
|
|
|
if (res1 != Loader::ResultStatus::Success && res3 != Loader::ResultStatus::Success &&
|
|
|
|
res2 == Loader::ResultStatus::Success) {
|
|
|
|
// Use from metadata pool.
|
|
|
|
if (nca_control_map.find(program_id) != nca_control_map.end()) {
|
2018-10-09 19:22:31 +01:00
|
|
|
const auto& nca = nca_control_map[program_id];
|
|
|
|
GetMetadataFromControlNCA(patch, *nca, icon, name);
|
2018-09-07 21:08:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-02 04:23:02 +00:00
|
|
|
emit EntryReady(MakeGameListEntry(physical_name, name, icon, *loader, program_id,
|
|
|
|
compatibility_list, patch));
|
2018-09-07 21:08:08 +01:00
|
|
|
} else if (is_dir && recursion > 0) {
|
|
|
|
watch_list.append(QString::fromStdString(physical_name));
|
|
|
|
AddFstEntriesToGameList(physical_name, recursion - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
FileUtil::ForeachDirectoryEntry(nullptr, dir_path, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameListWorker::run() {
|
|
|
|
stop_processing = false;
|
|
|
|
watch_list.append(dir_path);
|
|
|
|
FillControlMap(dir_path.toStdString());
|
|
|
|
AddInstalledTitlesToGameList();
|
|
|
|
AddFstEntriesToGameList(dir_path.toStdString(), deep_scan ? 256 : 0);
|
|
|
|
nca_control_map.clear();
|
|
|
|
emit Finished(watch_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameListWorker::Cancel() {
|
|
|
|
this->disconnect();
|
|
|
|
stop_processing = true;
|
|
|
|
}
|