diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/citra_qt/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/citra_qt/game_list.cpp | 171 | ||||
| -rw-r--r-- | src/citra_qt/game_list.h | 52 | ||||
| -rw-r--r-- | src/citra_qt/game_list_p.h | 130 | ||||
| -rw-r--r-- | src/citra_qt/main.cpp | 49 | ||||
| -rw-r--r-- | src/citra_qt/main.h | 8 | ||||
| -rw-r--r-- | src/citra_qt/main.ui | 11 | ||||
| -rw-r--r-- | src/citra_qt/util/util.cpp | 12 | ||||
| -rw-r--r-- | src/citra_qt/util/util.h | 4 | ||||
| -rw-r--r-- | src/common/file_util.cpp | 160 | ||||
| -rw-r--r-- | src/common/file_util.h | 26 | ||||
| -rw-r--r-- | src/core/loader/loader.cpp | 26 | ||||
| -rw-r--r-- | src/core/loader/loader.h | 28 |
13 files changed, 556 insertions, 123 deletions
diff --git a/src/citra_qt/CMakeLists.txt b/src/citra_qt/CMakeLists.txt index a82e8a85b..51a574629 100644 --- a/src/citra_qt/CMakeLists.txt +++ b/src/citra_qt/CMakeLists.txt | |||
| @@ -17,6 +17,7 @@ set(SRCS | |||
| 17 | debugger/profiler.cpp | 17 | debugger/profiler.cpp |
| 18 | debugger/ramview.cpp | 18 | debugger/ramview.cpp |
| 19 | debugger/registers.cpp | 19 | debugger/registers.cpp |
| 20 | game_list.cpp | ||
| 20 | util/spinbox.cpp | 21 | util/spinbox.cpp |
| 21 | util/util.cpp | 22 | util/util.cpp |
| 22 | bootmanager.cpp | 23 | bootmanager.cpp |
| @@ -42,6 +43,7 @@ set(HEADERS | |||
| 42 | debugger/profiler.h | 43 | debugger/profiler.h |
| 43 | debugger/ramview.h | 44 | debugger/ramview.h |
| 44 | debugger/registers.h | 45 | debugger/registers.h |
| 46 | game_list.h | ||
| 45 | util/spinbox.h | 47 | util/spinbox.h |
| 46 | util/util.h | 48 | util/util.h |
| 47 | bootmanager.h | 49 | bootmanager.h |
diff --git a/src/citra_qt/game_list.cpp b/src/citra_qt/game_list.cpp new file mode 100644 index 000000000..dade3c212 --- /dev/null +++ b/src/citra_qt/game_list.cpp | |||
| @@ -0,0 +1,171 @@ | |||
| 1 | // Copyright 2015 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <QHeaderView> | ||
| 6 | #include <QThreadPool> | ||
| 7 | #include <QVBoxLayout> | ||
| 8 | |||
| 9 | #include "game_list.h" | ||
| 10 | #include "game_list_p.h" | ||
| 11 | |||
| 12 | #include "core/loader/loader.h" | ||
| 13 | |||
| 14 | #include "common/common_paths.h" | ||
| 15 | #include "common/logging/log.h" | ||
| 16 | #include "common/string_util.h" | ||
| 17 | |||
| 18 | GameList::GameList(QWidget* parent) | ||
| 19 | { | ||
| 20 | QVBoxLayout* layout = new QVBoxLayout; | ||
| 21 | |||
| 22 | tree_view = new QTreeView; | ||
| 23 | item_model = new QStandardItemModel(tree_view); | ||
| 24 | tree_view->setModel(item_model); | ||
| 25 | |||
| 26 | tree_view->setAlternatingRowColors(true); | ||
| 27 | tree_view->setSelectionMode(QHeaderView::SingleSelection); | ||
| 28 | tree_view->setSelectionBehavior(QHeaderView::SelectRows); | ||
| 29 | tree_view->setVerticalScrollMode(QHeaderView::ScrollPerPixel); | ||
| 30 | tree_view->setHorizontalScrollMode(QHeaderView::ScrollPerPixel); | ||
| 31 | tree_view->setSortingEnabled(true); | ||
| 32 | tree_view->setEditTriggers(QHeaderView::NoEditTriggers); | ||
| 33 | tree_view->setUniformRowHeights(true); | ||
| 34 | |||
| 35 | item_model->insertColumns(0, COLUMN_COUNT); | ||
| 36 | item_model->setHeaderData(COLUMN_FILE_TYPE, Qt::Horizontal, "File type"); | ||
| 37 | item_model->setHeaderData(COLUMN_NAME, Qt::Horizontal, "Name"); | ||
| 38 | item_model->setHeaderData(COLUMN_SIZE, Qt::Horizontal, "Size"); | ||
| 39 | |||
| 40 | connect(tree_view, SIGNAL(activated(const QModelIndex&)), this, SLOT(ValidateEntry(const QModelIndex&))); | ||
| 41 | |||
| 42 | // We must register all custom types with the Qt Automoc system so that we are able to use it with | ||
| 43 | // signals/slots. In this case, QList falls under the umbrells of custom types. | ||
| 44 | qRegisterMetaType<QList<QStandardItem*>>("QList<QStandardItem*>"); | ||
| 45 | |||
| 46 | layout->addWidget(tree_view); | ||
| 47 | setLayout(layout); | ||
| 48 | } | ||
| 49 | |||
| 50 | GameList::~GameList() | ||
| 51 | { | ||
| 52 | emit ShouldCancelWorker(); | ||
| 53 | } | ||
| 54 | |||
| 55 | void GameList::AddEntry(QList<QStandardItem*> entry_items) | ||
| 56 | { | ||
| 57 | item_model->invisibleRootItem()->appendRow(entry_items); | ||
| 58 | } | ||
| 59 | |||
| 60 | void GameList::ValidateEntry(const QModelIndex& item) | ||
| 61 | { | ||
| 62 | // We don't care about the individual QStandardItem that was selected, but its row. | ||
| 63 | int row = item_model->itemFromIndex(item)->row(); | ||
| 64 | QStandardItem* child_file = item_model->invisibleRootItem()->child(row, COLUMN_NAME); | ||
| 65 | QString file_path = child_file->data(GameListItemPath::FullPathRole).toString(); | ||
| 66 | |||
| 67 | if (file_path.isEmpty()) | ||
| 68 | return; | ||
| 69 | std::string std_file_path = file_path.toStdString(); | ||
| 70 | if (!FileUtil::Exists(std_file_path) || FileUtil::IsDirectory(std_file_path)) | ||
| 71 | return; | ||
| 72 | emit GameChosen(file_path); | ||
| 73 | } | ||
| 74 | |||
| 75 | void GameList::DonePopulating() | ||
| 76 | { | ||
| 77 | tree_view->setEnabled(true); | ||
| 78 | } | ||
| 79 | |||
| 80 | void GameList::PopulateAsync(const QString& dir_path, bool deep_scan) | ||
| 81 | { | ||
| 82 | if (!FileUtil::Exists(dir_path.toStdString()) || !FileUtil::IsDirectory(dir_path.toStdString())) { | ||
| 83 | LOG_ERROR(Frontend, "Could not find game list folder at %s", dir_path.toLatin1().data()); | ||
| 84 | return; | ||
| 85 | } | ||
| 86 | |||
| 87 | tree_view->setEnabled(false); | ||
| 88 | // Delete any rows that might already exist if we're repopulating | ||
| 89 | item_model->removeRows(0, item_model->rowCount()); | ||
| 90 | |||
| 91 | emit ShouldCancelWorker(); | ||
| 92 | GameListWorker* worker = new GameListWorker(dir_path, deep_scan); | ||
| 93 | |||
| 94 | connect(worker, SIGNAL(EntryReady(QList<QStandardItem*>)), this, SLOT(AddEntry(QList<QStandardItem*>)), Qt::QueuedConnection); | ||
| 95 | connect(worker, SIGNAL(Finished()), this, SLOT(DonePopulating()), Qt::QueuedConnection); | ||
| 96 | // Use DirectConnection here because worker->Cancel() is thread-safe and we want it to cancel without delay. | ||
| 97 | connect(this, SIGNAL(ShouldCancelWorker()), worker, SLOT(Cancel()), Qt::DirectConnection); | ||
| 98 | |||
| 99 | QThreadPool::globalInstance()->start(worker); | ||
| 100 | current_worker = std::move(worker); | ||
| 101 | } | ||
| 102 | |||
| 103 | void GameList::SaveInterfaceLayout(QSettings& settings) | ||
| 104 | { | ||
| 105 | settings.beginGroup("UILayout"); | ||
| 106 | settings.setValue("gameListHeaderState", tree_view->header()->saveState()); | ||
| 107 | settings.endGroup(); | ||
| 108 | } | ||
| 109 | |||
| 110 | void GameList::LoadInterfaceLayout(QSettings& settings) | ||
| 111 | { | ||
| 112 | auto header = tree_view->header(); | ||
| 113 | settings.beginGroup("UILayout"); | ||
| 114 | header->restoreState(settings.value("gameListHeaderState").toByteArray()); | ||
| 115 | settings.endGroup(); | ||
| 116 | |||
| 117 | item_model->sort(header->sortIndicatorSection(), header->sortIndicatorOrder()); | ||
| 118 | } | ||
| 119 | |||
| 120 | void GameListWorker::AddFstEntriesToGameList(const std::string& dir_path, bool deep_scan) | ||
| 121 | { | ||
| 122 | const auto callback = [&](const std::string& directory, | ||
| 123 | const std::string& virtual_name) -> int { | ||
| 124 | |||
| 125 | std::string physical_name = directory + DIR_SEP + virtual_name; | ||
| 126 | |||
| 127 | if (stop_processing) | ||
| 128 | return -1; // A negative return value breaks the callback loop. | ||
| 129 | |||
| 130 | if (deep_scan && FileUtil::IsDirectory(physical_name)) { | ||
| 131 | AddFstEntriesToGameList(physical_name, true); | ||
| 132 | } else { | ||
| 133 | std::string filename_filename, filename_extension; | ||
| 134 | Common::SplitPath(physical_name, nullptr, &filename_filename, &filename_extension); | ||
| 135 | |||
| 136 | Loader::FileType guessed_filetype = Loader::GuessFromExtension(filename_extension); | ||
| 137 | if (guessed_filetype == Loader::FileType::Unknown) | ||
| 138 | return 0; | ||
| 139 | Loader::FileType filetype = Loader::IdentifyFile(physical_name); | ||
| 140 | if (filetype == Loader::FileType::Unknown) { | ||
| 141 | LOG_WARNING(Frontend, "File %s is of indeterminate type and is possibly corrupted.", physical_name.c_str()); | ||
| 142 | return 0; | ||
| 143 | } | ||
| 144 | if (guessed_filetype != filetype) { | ||
| 145 | LOG_WARNING(Frontend, "Filetype and extension of file %s do not match.", physical_name.c_str()); | ||
| 146 | } | ||
| 147 | |||
| 148 | emit EntryReady({ | ||
| 149 | new GameListItem(QString::fromStdString(Loader::GetFileTypeString(filetype))), | ||
| 150 | new GameListItemPath(QString::fromStdString(physical_name)), | ||
| 151 | new GameListItemSize(FileUtil::GetSize(physical_name)), | ||
| 152 | }); | ||
| 153 | } | ||
| 154 | |||
| 155 | return 0; // We don't care about the found entries | ||
| 156 | }; | ||
| 157 | FileUtil::ScanDirectoryTreeAndCallback(dir_path, callback); | ||
| 158 | } | ||
| 159 | |||
| 160 | void GameListWorker::run() | ||
| 161 | { | ||
| 162 | stop_processing = false; | ||
| 163 | AddFstEntriesToGameList(dir_path.toStdString(), deep_scan); | ||
| 164 | emit Finished(); | ||
| 165 | } | ||
| 166 | |||
| 167 | void GameListWorker::Cancel() | ||
| 168 | { | ||
| 169 | disconnect(this, 0, 0, 0); | ||
| 170 | stop_processing = true; | ||
| 171 | } | ||
diff --git a/src/citra_qt/game_list.h b/src/citra_qt/game_list.h new file mode 100644 index 000000000..0950d9622 --- /dev/null +++ b/src/citra_qt/game_list.h | |||
| @@ -0,0 +1,52 @@ | |||
| 1 | // Copyright 2015 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <QModelIndex> | ||
| 8 | #include <QSettings> | ||
| 9 | #include <QStandardItem> | ||
| 10 | #include <QStandardItemModel> | ||
| 11 | #include <QString> | ||
| 12 | #include <QTreeView> | ||
| 13 | #include <QWidget> | ||
| 14 | |||
| 15 | class GameListWorker; | ||
| 16 | |||
| 17 | |||
| 18 | class GameList : public QWidget { | ||
| 19 | Q_OBJECT | ||
| 20 | |||
| 21 | public: | ||
| 22 | enum { | ||
| 23 | COLUMN_FILE_TYPE, | ||
| 24 | COLUMN_NAME, | ||
| 25 | COLUMN_SIZE, | ||
| 26 | COLUMN_COUNT, // Number of columns | ||
| 27 | }; | ||
| 28 | |||
| 29 | GameList(QWidget* parent = nullptr); | ||
| 30 | ~GameList() override; | ||
| 31 | |||
| 32 | void PopulateAsync(const QString& dir_path, bool deep_scan); | ||
| 33 | |||
| 34 | void SaveInterfaceLayout(QSettings& settings); | ||
| 35 | void LoadInterfaceLayout(QSettings& settings); | ||
| 36 | |||
| 37 | public slots: | ||
| 38 | void AddEntry(QList<QStandardItem*> entry_items); | ||
| 39 | |||
| 40 | private slots: | ||
| 41 | void ValidateEntry(const QModelIndex& item); | ||
| 42 | void DonePopulating(); | ||
| 43 | |||
| 44 | signals: | ||
| 45 | void GameChosen(QString game_path); | ||
| 46 | void ShouldCancelWorker(); | ||
| 47 | |||
| 48 | private: | ||
| 49 | QTreeView* tree_view = nullptr; | ||
| 50 | QStandardItemModel* item_model = nullptr; | ||
| 51 | GameListWorker* current_worker = nullptr; | ||
| 52 | }; | ||
diff --git a/src/citra_qt/game_list_p.h b/src/citra_qt/game_list_p.h new file mode 100644 index 000000000..820012bce --- /dev/null +++ b/src/citra_qt/game_list_p.h | |||
| @@ -0,0 +1,130 @@ | |||
| 1 | // Copyright 2015 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <atomic> | ||
| 8 | |||
| 9 | #include <QRunnable> | ||
| 10 | #include <QStandardItem> | ||
| 11 | #include <QString> | ||
| 12 | |||
| 13 | #include "citra_qt/util/util.h" | ||
| 14 | #include "common/string_util.h" | ||
| 15 | |||
| 16 | |||
| 17 | class GameListItem : public QStandardItem { | ||
| 18 | |||
| 19 | public: | ||
| 20 | GameListItem(): QStandardItem() {} | ||
| 21 | GameListItem(const QString& string): QStandardItem(string) {} | ||
| 22 | virtual ~GameListItem() override {} | ||
| 23 | }; | ||
| 24 | |||
| 25 | |||
| 26 | /** | ||
| 27 | * A specialization of GameListItem for path values. | ||
| 28 | * This class ensures that for every full path value it holds, a correct string representation | ||
| 29 | * of just the filename (with no extension) will be displayed to the user. | ||
| 30 | */ | ||
| 31 | class GameListItemPath : public GameListItem { | ||
| 32 | |||
| 33 | public: | ||
| 34 | static const int FullPathRole = Qt::UserRole + 1; | ||
| 35 | |||
| 36 | GameListItemPath(): GameListItem() {} | ||
| 37 | GameListItemPath(const QString& game_path): GameListItem() | ||
| 38 | { | ||
| 39 | setData(game_path, FullPathRole); | ||
| 40 | } | ||
| 41 | |||
| 42 | void setData(const QVariant& value, int role) override | ||
| 43 | { | ||
| 44 | // By specializing setData for FullPathRole, we can ensure that the two string | ||
| 45 | // representations of the data are always accurate and in the correct format. | ||
| 46 | if (role == FullPathRole) { | ||
| 47 | std::string filename; | ||
| 48 | Common::SplitPath(value.toString().toStdString(), nullptr, &filename, nullptr); | ||
| 49 | GameListItem::setData(QString::fromStdString(filename), Qt::DisplayRole); | ||
| 50 | GameListItem::setData(value, FullPathRole); | ||
| 51 | } else { | ||
| 52 | GameListItem::setData(value, role); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | }; | ||
| 56 | |||
| 57 | |||
| 58 | /** | ||
| 59 | * A specialization of GameListItem for size values. | ||
| 60 | * This class ensures that for every numerical size value it holds (in bytes), a correct | ||
| 61 | * human-readable string representation will be displayed to the user. | ||
| 62 | */ | ||
| 63 | class GameListItemSize : public GameListItem { | ||
| 64 | |||
| 65 | public: | ||
| 66 | static const int SizeRole = Qt::UserRole + 1; | ||
| 67 | |||
| 68 | GameListItemSize(): GameListItem() {} | ||
| 69 | GameListItemSize(const qulonglong size_bytes): GameListItem() | ||
| 70 | { | ||
| 71 | setData(size_bytes, SizeRole); | ||
| 72 | } | ||
| 73 | |||
| 74 | void setData(const QVariant& value, int role) override | ||
| 75 | { | ||
| 76 | // By specializing setData for SizeRole, we can ensure that the numerical and string | ||
| 77 | // representations of the data are always accurate and in the correct format. | ||
| 78 | if (role == SizeRole) { | ||
| 79 | qulonglong size_bytes = value.toULongLong(); | ||
| 80 | GameListItem::setData(ReadableByteSize(size_bytes), Qt::DisplayRole); | ||
| 81 | GameListItem::setData(value, SizeRole); | ||
| 82 | } else { | ||
| 83 | GameListItem::setData(value, role); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | /** | ||
| 88 | * This operator is, in practice, only used by the TreeView sorting systems. | ||
| 89 | * Override it so that it will correctly sort by numerical value instead of by string representation. | ||
| 90 | */ | ||
| 91 | bool operator<(const QStandardItem& other) const override | ||
| 92 | { | ||
| 93 | return data(SizeRole).toULongLong() < other.data(SizeRole).toULongLong(); | ||
| 94 | } | ||
| 95 | }; | ||
| 96 | |||
| 97 | |||
| 98 | /** | ||
| 99 | * Asynchronous worker object for populating the game list. | ||
| 100 | * Communicates with other threads through Qt's signal/slot system. | ||
| 101 | */ | ||
| 102 | class GameListWorker : public QObject, public QRunnable { | ||
| 103 | Q_OBJECT | ||
| 104 | |||
| 105 | public: | ||
| 106 | GameListWorker(QString dir_path, bool deep_scan): | ||
| 107 | QObject(), QRunnable(), dir_path(dir_path), deep_scan(deep_scan) {} | ||
| 108 | |||
| 109 | public slots: | ||
| 110 | /// Starts the processing of directory tree information. | ||
| 111 | void run() override; | ||
| 112 | /// Tells the worker that it should no longer continue processing. Thread-safe. | ||
| 113 | void Cancel(); | ||
| 114 | |||
| 115 | signals: | ||
| 116 | /** | ||
| 117 | * The `EntryReady` signal is emitted once an entry has been prepared and is ready | ||
| 118 | * to be added to the game list. | ||
| 119 | * @param entry_items a list with `QStandardItem`s that make up the columns of the new entry. | ||
| 120 | */ | ||
| 121 | void EntryReady(QList<QStandardItem*> entry_items); | ||
| 122 | void Finished(); | ||
| 123 | |||
| 124 | private: | ||
| 125 | QString dir_path; | ||
| 126 | bool deep_scan; | ||
| 127 | std::atomic_bool stop_processing; | ||
| 128 | |||
| 129 | void AddFstEntriesToGameList(const std::string& dir_path, bool deep_scan); | ||
| 130 | }; | ||
diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp index 01841b33c..298649aaf 100644 --- a/src/citra_qt/main.cpp +++ b/src/citra_qt/main.cpp | |||
| @@ -12,6 +12,7 @@ | |||
| 12 | 12 | ||
| 13 | #include "citra_qt/bootmanager.h" | 13 | #include "citra_qt/bootmanager.h" |
| 14 | #include "citra_qt/config.h" | 14 | #include "citra_qt/config.h" |
| 15 | #include "citra_qt/game_list.h" | ||
| 15 | #include "citra_qt/hotkeys.h" | 16 | #include "citra_qt/hotkeys.h" |
| 16 | #include "citra_qt/main.h" | 17 | #include "citra_qt/main.h" |
| 17 | 18 | ||
| @@ -59,6 +60,9 @@ GMainWindow::GMainWindow() : emu_thread(nullptr) | |||
| 59 | render_window = new GRenderWindow(this, emu_thread.get()); | 60 | render_window = new GRenderWindow(this, emu_thread.get()); |
| 60 | render_window->hide(); | 61 | render_window->hide(); |
| 61 | 62 | ||
| 63 | game_list = new GameList(); | ||
| 64 | ui.horizontalLayout->addWidget(game_list); | ||
| 65 | |||
| 62 | profilerWidget = new ProfilerWidget(this); | 66 | profilerWidget = new ProfilerWidget(this); |
| 63 | addDockWidget(Qt::BottomDockWidgetArea, profilerWidget); | 67 | addDockWidget(Qt::BottomDockWidgetArea, profilerWidget); |
| 64 | profilerWidget->hide(); | 68 | profilerWidget->hide(); |
| @@ -137,6 +141,8 @@ GMainWindow::GMainWindow() : emu_thread(nullptr) | |||
| 137 | microProfileDialog->setVisible(settings.value("microProfileDialogVisible").toBool()); | 141 | microProfileDialog->setVisible(settings.value("microProfileDialogVisible").toBool()); |
| 138 | settings.endGroup(); | 142 | settings.endGroup(); |
| 139 | 143 | ||
| 144 | game_list->LoadInterfaceLayout(settings); | ||
| 145 | |||
| 140 | ui.action_Use_Hardware_Renderer->setChecked(Settings::values.use_hw_renderer); | 146 | ui.action_Use_Hardware_Renderer->setChecked(Settings::values.use_hw_renderer); |
| 141 | SetHardwareRendererEnabled(ui.action_Use_Hardware_Renderer->isChecked()); | 147 | SetHardwareRendererEnabled(ui.action_Use_Hardware_Renderer->isChecked()); |
| 142 | 148 | ||
| @@ -160,8 +166,10 @@ GMainWindow::GMainWindow() : emu_thread(nullptr) | |||
| 160 | UpdateRecentFiles(); | 166 | UpdateRecentFiles(); |
| 161 | 167 | ||
| 162 | // Setup connections | 168 | // Setup connections |
| 169 | connect(game_list, SIGNAL(GameChosen(QString)), this, SLOT(OnGameListLoadFile(QString))); | ||
| 163 | connect(ui.action_Load_File, SIGNAL(triggered()), this, SLOT(OnMenuLoadFile())); | 170 | connect(ui.action_Load_File, SIGNAL(triggered()), this, SLOT(OnMenuLoadFile())); |
| 164 | connect(ui.action_Load_Symbol_Map, SIGNAL(triggered()), this, SLOT(OnMenuLoadSymbolMap())); | 171 | connect(ui.action_Load_Symbol_Map, SIGNAL(triggered()), this, SLOT(OnMenuLoadSymbolMap())); |
| 172 | connect(ui.action_Select_Game_List_Root, SIGNAL(triggered()), this, SLOT(OnMenuSelectGameListRoot())); | ||
| 165 | connect(ui.action_Start, SIGNAL(triggered()), this, SLOT(OnStartGame())); | 173 | connect(ui.action_Start, SIGNAL(triggered()), this, SLOT(OnStartGame())); |
| 166 | connect(ui.action_Pause, SIGNAL(triggered()), this, SLOT(OnPauseGame())); | 174 | connect(ui.action_Pause, SIGNAL(triggered()), this, SLOT(OnPauseGame())); |
| 167 | connect(ui.action_Stop, SIGNAL(triggered()), this, SLOT(OnStopGame())); | 175 | connect(ui.action_Stop, SIGNAL(triggered()), this, SLOT(OnStopGame())); |
| @@ -193,6 +201,8 @@ GMainWindow::GMainWindow() : emu_thread(nullptr) | |||
| 193 | 201 | ||
| 194 | show(); | 202 | show(); |
| 195 | 203 | ||
| 204 | game_list->PopulateAsync(settings.value("gameListRootDir").toString(), settings.value("gameListDeepScan").toBool()); | ||
| 205 | |||
| 196 | QStringList args = QApplication::arguments(); | 206 | QStringList args = QApplication::arguments(); |
| 197 | if (args.length() >= 2) { | 207 | if (args.length() >= 2) { |
| 198 | BootGame(args[1].toStdString()); | 208 | BootGame(args[1].toStdString()); |
| @@ -264,8 +274,12 @@ void GMainWindow::BootGame(const std::string& filename) { | |||
| 264 | // Update the GUI | 274 | // Update the GUI |
| 265 | registersWidget->OnDebugModeEntered(); | 275 | registersWidget->OnDebugModeEntered(); |
| 266 | callstackWidget->OnDebugModeEntered(); | 276 | callstackWidget->OnDebugModeEntered(); |
| 277 | if (ui.action_Single_Window_Mode->isChecked()) { | ||
| 278 | game_list->hide(); | ||
| 279 | } | ||
| 267 | render_window->show(); | 280 | render_window->show(); |
| 268 | 281 | ||
| 282 | emulation_running = true; | ||
| 269 | OnStartGame(); | 283 | OnStartGame(); |
| 270 | } | 284 | } |
| 271 | 285 | ||
| @@ -294,6 +308,9 @@ void GMainWindow::ShutdownGame() { | |||
| 294 | ui.action_Pause->setEnabled(false); | 308 | ui.action_Pause->setEnabled(false); |
| 295 | ui.action_Stop->setEnabled(false); | 309 | ui.action_Stop->setEnabled(false); |
| 296 | render_window->hide(); | 310 | render_window->hide(); |
| 311 | game_list->show(); | ||
| 312 | |||
| 313 | emulation_running = false; | ||
| 297 | } | 314 | } |
| 298 | 315 | ||
| 299 | void GMainWindow::StoreRecentFile(const QString& filename) | 316 | void GMainWindow::StoreRecentFile(const QString& filename) |
| @@ -337,12 +354,16 @@ void GMainWindow::UpdateRecentFiles() { | |||
| 337 | } | 354 | } |
| 338 | } | 355 | } |
| 339 | 356 | ||
| 357 | void GMainWindow::OnGameListLoadFile(QString game_path) { | ||
| 358 | BootGame(game_path.toLatin1().data()); | ||
| 359 | } | ||
| 360 | |||
| 340 | void GMainWindow::OnMenuLoadFile() { | 361 | void GMainWindow::OnMenuLoadFile() { |
| 341 | QSettings settings; | 362 | QSettings settings; |
| 342 | QString rom_path = settings.value("romsPath", QString()).toString(); | 363 | QString rom_path = settings.value("romsPath", QString()).toString(); |
| 343 | 364 | ||
| 344 | QString filename = QFileDialog::getOpenFileName(this, tr("Load File"), rom_path, tr("3DS executable (*.3ds *.3dsx *.elf *.axf *.cci *.cxi)")); | 365 | QString filename = QFileDialog::getOpenFileName(this, tr("Load File"), rom_path, tr("3DS executable (*.3ds *.3dsx *.elf *.axf *.cci *.cxi)")); |
| 345 | if (filename.size()) { | 366 | if (!filename.isEmpty()) { |
| 346 | settings.setValue("romsPath", QFileInfo(filename).path()); | 367 | settings.setValue("romsPath", QFileInfo(filename).path()); |
| 347 | StoreRecentFile(filename); | 368 | StoreRecentFile(filename); |
| 348 | 369 | ||
| @@ -355,13 +376,23 @@ void GMainWindow::OnMenuLoadSymbolMap() { | |||
| 355 | QString symbol_path = settings.value("symbolsPath", QString()).toString(); | 376 | QString symbol_path = settings.value("symbolsPath", QString()).toString(); |
| 356 | 377 | ||
| 357 | QString filename = QFileDialog::getOpenFileName(this, tr("Load Symbol Map"), symbol_path, tr("Symbol map (*)")); | 378 | QString filename = QFileDialog::getOpenFileName(this, tr("Load Symbol Map"), symbol_path, tr("Symbol map (*)")); |
| 358 | if (filename.size()) { | 379 | if (!filename.isEmpty()) { |
| 359 | settings.setValue("symbolsPath", QFileInfo(filename).path()); | 380 | settings.setValue("symbolsPath", QFileInfo(filename).path()); |
| 360 | 381 | ||
| 361 | LoadSymbolMap(filename.toLatin1().data()); | 382 | LoadSymbolMap(filename.toLatin1().data()); |
| 362 | } | 383 | } |
| 363 | } | 384 | } |
| 364 | 385 | ||
| 386 | void GMainWindow::OnMenuSelectGameListRoot() { | ||
| 387 | QSettings settings; | ||
| 388 | |||
| 389 | QString dir_path = QFileDialog::getExistingDirectory(this, tr("Select Directory")); | ||
| 390 | if (!dir_path.isEmpty()) { | ||
| 391 | settings.setValue("gameListRootDir", dir_path); | ||
| 392 | game_list->PopulateAsync(dir_path, settings.value("gameListDeepScan").toBool()); | ||
| 393 | } | ||
| 394 | } | ||
| 395 | |||
| 365 | void GMainWindow::OnMenuRecentFile() { | 396 | void GMainWindow::OnMenuRecentFile() { |
| 366 | QAction* action = qobject_cast<QAction*>(sender()); | 397 | QAction* action = qobject_cast<QAction*>(sender()); |
| 367 | assert(action); | 398 | assert(action); |
| @@ -423,17 +454,22 @@ void GMainWindow::ToggleWindowMode() { | |||
| 423 | // Render in the main window... | 454 | // Render in the main window... |
| 424 | render_window->BackupGeometry(); | 455 | render_window->BackupGeometry(); |
| 425 | ui.horizontalLayout->addWidget(render_window); | 456 | ui.horizontalLayout->addWidget(render_window); |
| 426 | render_window->setVisible(true); | ||
| 427 | render_window->setFocusPolicy(Qt::ClickFocus); | 457 | render_window->setFocusPolicy(Qt::ClickFocus); |
| 428 | render_window->setFocus(); | 458 | if (emulation_running) { |
| 459 | render_window->setVisible(true); | ||
| 460 | render_window->setFocus(); | ||
| 461 | } | ||
| 429 | 462 | ||
| 430 | } else { | 463 | } else { |
| 431 | // Render in a separate window... | 464 | // Render in a separate window... |
| 432 | ui.horizontalLayout->removeWidget(render_window); | 465 | ui.horizontalLayout->removeWidget(render_window); |
| 433 | render_window->setParent(nullptr); | 466 | render_window->setParent(nullptr); |
| 434 | render_window->setVisible(true); | ||
| 435 | render_window->RestoreGeometry(); | ||
| 436 | render_window->setFocusPolicy(Qt::NoFocus); | 467 | render_window->setFocusPolicy(Qt::NoFocus); |
| 468 | if (emulation_running) { | ||
| 469 | render_window->setVisible(true); | ||
| 470 | render_window->RestoreGeometry(); | ||
| 471 | game_list->show(); | ||
| 472 | } | ||
| 437 | } | 473 | } |
| 438 | } | 474 | } |
| 439 | 475 | ||
| @@ -456,6 +492,7 @@ void GMainWindow::closeEvent(QCloseEvent* event) { | |||
| 456 | settings.setValue("singleWindowMode", ui.action_Single_Window_Mode->isChecked()); | 492 | settings.setValue("singleWindowMode", ui.action_Single_Window_Mode->isChecked()); |
| 457 | settings.setValue("displayTitleBars", ui.actionDisplay_widget_title_bars->isChecked()); | 493 | settings.setValue("displayTitleBars", ui.actionDisplay_widget_title_bars->isChecked()); |
| 458 | settings.setValue("firstStart", false); | 494 | settings.setValue("firstStart", false); |
| 495 | game_list->SaveInterfaceLayout(settings); | ||
| 459 | SaveHotkeys(settings); | 496 | SaveHotkeys(settings); |
| 460 | 497 | ||
| 461 | // Shutdown session if the emu thread is active... | 498 | // Shutdown session if the emu thread is active... |
diff --git a/src/citra_qt/main.h b/src/citra_qt/main.h index 32523fded..6d27ce6a9 100644 --- a/src/citra_qt/main.h +++ b/src/citra_qt/main.h | |||
| @@ -10,6 +10,7 @@ | |||
| 10 | 10 | ||
| 11 | #include "ui_main.h" | 11 | #include "ui_main.h" |
| 12 | 12 | ||
| 13 | class GameList; | ||
| 13 | class GImageInfo; | 14 | class GImageInfo; |
| 14 | class GRenderWindow; | 15 | class GRenderWindow; |
| 15 | class EmuThread; | 16 | class EmuThread; |
| @@ -87,8 +88,12 @@ private slots: | |||
| 87 | void OnStartGame(); | 88 | void OnStartGame(); |
| 88 | void OnPauseGame(); | 89 | void OnPauseGame(); |
| 89 | void OnStopGame(); | 90 | void OnStopGame(); |
| 91 | /// Called whenever a user selects a game in the game list widget. | ||
| 92 | void OnGameListLoadFile(QString game_path); | ||
| 90 | void OnMenuLoadFile(); | 93 | void OnMenuLoadFile(); |
| 91 | void OnMenuLoadSymbolMap(); | 94 | void OnMenuLoadSymbolMap(); |
| 95 | /// Called whenever a user selects the "File->Select Game List Root" menu item | ||
| 96 | void OnMenuSelectGameListRoot(); | ||
| 92 | void OnMenuRecentFile(); | 97 | void OnMenuRecentFile(); |
| 93 | void OnOpenHotkeysDialog(); | 98 | void OnOpenHotkeysDialog(); |
| 94 | void OnConfigure(); | 99 | void OnConfigure(); |
| @@ -101,7 +106,10 @@ private: | |||
| 101 | Ui::MainWindow ui; | 106 | Ui::MainWindow ui; |
| 102 | 107 | ||
| 103 | GRenderWindow* render_window; | 108 | GRenderWindow* render_window; |
| 109 | GameList* game_list; | ||
| 104 | 110 | ||
| 111 | // Whether emulation is currently running in Citra. | ||
| 112 | bool emulation_running = false; | ||
| 105 | std::unique_ptr<EmuThread> emu_thread; | 113 | std::unique_ptr<EmuThread> emu_thread; |
| 106 | 114 | ||
| 107 | ProfilerWidget* profilerWidget; | 115 | ProfilerWidget* profilerWidget; |
diff --git a/src/citra_qt/main.ui b/src/citra_qt/main.ui index 1ba700a3a..997597642 100644 --- a/src/citra_qt/main.ui +++ b/src/citra_qt/main.ui | |||
| @@ -45,7 +45,7 @@ | |||
| 45 | <x>0</x> | 45 | <x>0</x> |
| 46 | <y>0</y> | 46 | <y>0</y> |
| 47 | <width>1081</width> | 47 | <width>1081</width> |
| 48 | <height>21</height> | 48 | <height>22</height> |
| 49 | </rect> | 49 | </rect> |
| 50 | </property> | 50 | </property> |
| 51 | <widget class="QMenu" name="menu_File"> | 51 | <widget class="QMenu" name="menu_File"> |
| @@ -60,6 +60,7 @@ | |||
| 60 | <addaction name="action_Load_File"/> | 60 | <addaction name="action_Load_File"/> |
| 61 | <addaction name="action_Load_Symbol_Map"/> | 61 | <addaction name="action_Load_Symbol_Map"/> |
| 62 | <addaction name="separator"/> | 62 | <addaction name="separator"/> |
| 63 | <addaction name="action_Select_Game_List_Root"/> | ||
| 63 | <addaction name="menu_recent_files"/> | 64 | <addaction name="menu_recent_files"/> |
| 64 | <addaction name="separator"/> | 65 | <addaction name="separator"/> |
| 65 | <addaction name="action_Exit"/> | 66 | <addaction name="action_Exit"/> |
| @@ -182,6 +183,14 @@ | |||
| 182 | <string>Display Dock Widget Headers</string> | 183 | <string>Display Dock Widget Headers</string> |
| 183 | </property> | 184 | </property> |
| 184 | </action> | 185 | </action> |
| 186 | <action name="action_Select_Game_List_Root"> | ||
| 187 | <property name="text"> | ||
| 188 | <string>Select Game Directory...</string> | ||
| 189 | </property> | ||
| 190 | <property name="toolTip"> | ||
| 191 | <string>Selects a folder to display in the game list</string> | ||
| 192 | </property> | ||
| 193 | </action> | ||
| 185 | </widget> | 194 | </widget> |
| 186 | <resources/> | 195 | <resources/> |
| 187 | <connections> | 196 | <connections> |
diff --git a/src/citra_qt/util/util.cpp b/src/citra_qt/util/util.cpp index f292046b7..8734a8efd 100644 --- a/src/citra_qt/util/util.cpp +++ b/src/citra_qt/util/util.cpp | |||
| @@ -2,6 +2,9 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <array> | ||
| 6 | #include <cmath> | ||
| 7 | |||
| 5 | #include "citra_qt/util/util.h" | 8 | #include "citra_qt/util/util.h" |
| 6 | 9 | ||
| 7 | QFont GetMonospaceFont() { | 10 | QFont GetMonospaceFont() { |
| @@ -11,3 +14,12 @@ QFont GetMonospaceFont() { | |||
| 11 | font.setFixedPitch(true); | 14 | font.setFixedPitch(true); |
| 12 | return font; | 15 | return font; |
| 13 | } | 16 | } |
| 17 | |||
| 18 | QString ReadableByteSize(qulonglong size) { | ||
| 19 | static const std::array<const char*, 6> units = { "B", "KiB", "MiB", "GiB", "TiB", "PiB" }; | ||
| 20 | if (size == 0) | ||
| 21 | return "0"; | ||
| 22 | int digit_groups = std::min<int>((int)(std::log10(size) / std::log10(1024)), units.size()); | ||
| 23 | return QString("%L1 %2").arg(size / std::pow(1024, digit_groups), 0, 'f', 1) | ||
| 24 | .arg(units[digit_groups]); | ||
| 25 | } | ||
diff --git a/src/citra_qt/util/util.h b/src/citra_qt/util/util.h index 98a944047..ab443ef9b 100644 --- a/src/citra_qt/util/util.h +++ b/src/citra_qt/util/util.h | |||
| @@ -5,6 +5,10 @@ | |||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <QFont> | 7 | #include <QFont> |
| 8 | #include <QString> | ||
| 8 | 9 | ||
| 9 | /// Returns a QFont object appropriate to use as a monospace font for debugging widgets, etc. | 10 | /// Returns a QFont object appropriate to use as a monospace font for debugging widgets, etc. |
| 10 | QFont GetMonospaceFont(); | 11 | QFont GetMonospaceFont(); |
| 12 | |||
| 13 | /// Convert a size in bytes into a readable format (KiB, MiB, etc.) | ||
| 14 | QString ReadableByteSize(qulonglong size); | ||
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index dcb9baa08..1e0d33313 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp | |||
| @@ -420,28 +420,23 @@ bool CreateEmptyFile(const std::string &filename) | |||
| 420 | } | 420 | } |
| 421 | 421 | ||
| 422 | 422 | ||
| 423 | // Scans the directory tree gets, starting from _Directory and adds the | 423 | int ScanDirectoryTreeAndCallback(const std::string &directory, std::function<int(const std::string&, const std::string&)> callback) |
| 424 | // results into parentEntry. Returns the number of files+directories found | ||
| 425 | u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry) | ||
| 426 | { | 424 | { |
| 427 | LOG_TRACE(Common_Filesystem, "directory %s", directory.c_str()); | 425 | LOG_TRACE(Common_Filesystem, "directory %s", directory.c_str()); |
| 428 | // How many files + directories we found | 426 | // How many files + directories we found |
| 429 | u32 foundEntries = 0; | 427 | int found_entries = 0; |
| 430 | #ifdef _WIN32 | 428 | #ifdef _WIN32 |
| 431 | // Find the first file in the directory. | 429 | // Find the first file in the directory. |
| 432 | WIN32_FIND_DATA ffd; | 430 | WIN32_FIND_DATA ffd; |
| 433 | 431 | ||
| 434 | HANDLE hFind = FindFirstFile(Common::UTF8ToTStr(directory + "\\*").c_str(), &ffd); | 432 | HANDLE handle_find = FindFirstFile(Common::UTF8ToTStr(directory + "\\*").c_str(), &ffd); |
| 435 | if (hFind == INVALID_HANDLE_VALUE) | 433 | if (handle_find == INVALID_HANDLE_VALUE) { |
| 436 | { | 434 | FindClose(handle_find); |
| 437 | FindClose(hFind); | 435 | return found_entries; |
| 438 | return foundEntries; | ||
| 439 | } | 436 | } |
| 440 | // windows loop | 437 | // windows loop |
| 441 | do | 438 | do { |
| 442 | { | 439 | const std::string virtual_name(Common::TStrToUTF8(ffd.cFileName)); |
| 443 | FSTEntry entry; | ||
| 444 | const std::string virtualName(Common::TStrToUTF8(ffd.cFileName)); | ||
| 445 | #else | 440 | #else |
| 446 | struct dirent dirent, *result = nullptr; | 441 | struct dirent dirent, *result = nullptr; |
| 447 | 442 | ||
| @@ -450,115 +445,80 @@ u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry) | |||
| 450 | return 0; | 445 | return 0; |
| 451 | 446 | ||
| 452 | // non windows loop | 447 | // non windows loop |
| 453 | while (!readdir_r(dirp, &dirent, &result) && result) | 448 | while (!readdir_r(dirp, &dirent, &result) && result) { |
| 454 | { | 449 | const std::string virtual_name(result->d_name); |
| 455 | FSTEntry entry; | ||
| 456 | const std::string virtualName(result->d_name); | ||
| 457 | #endif | 450 | #endif |
| 458 | // check for "." and ".." | 451 | // check for "." and ".." |
| 459 | if (((virtualName[0] == '.') && (virtualName[1] == '\0')) || | 452 | if (((virtual_name[0] == '.') && (virtual_name[1] == '\0')) || |
| 460 | ((virtualName[0] == '.') && (virtualName[1] == '.') && | 453 | ((virtual_name[0] == '.') && (virtual_name[1] == '.') && |
| 461 | (virtualName[2] == '\0'))) | 454 | (virtual_name[2] == '\0'))) |
| 462 | continue; | 455 | continue; |
| 463 | entry.virtualName = virtualName; | ||
| 464 | entry.physicalName = directory; | ||
| 465 | entry.physicalName += DIR_SEP + entry.virtualName; | ||
| 466 | 456 | ||
| 467 | if (IsDirectory(entry.physicalName.c_str())) | 457 | int ret = callback(directory, virtual_name); |
| 468 | { | 458 | if (ret < 0) { |
| 469 | entry.isDirectory = true; | 459 | if (ret != -1) |
| 470 | // is a directory, lets go inside | 460 | found_entries = ret; |
| 471 | entry.size = ScanDirectoryTree(entry.physicalName, entry); | 461 | break; |
| 472 | foundEntries += (u32)entry.size; | ||
| 473 | } | ||
| 474 | else | ||
| 475 | { // is a file | ||
| 476 | entry.isDirectory = false; | ||
| 477 | entry.size = GetSize(entry.physicalName.c_str()); | ||
| 478 | } | 462 | } |
| 479 | ++foundEntries; | 463 | found_entries += ret; |
| 480 | // Push into the tree | 464 | |
| 481 | parentEntry.children.push_back(entry); | ||
| 482 | #ifdef _WIN32 | 465 | #ifdef _WIN32 |
| 483 | } while (FindNextFile(hFind, &ffd) != 0); | 466 | } while (FindNextFile(handle_find, &ffd) != 0); |
| 484 | FindClose(hFind); | 467 | FindClose(handle_find); |
| 485 | #else | 468 | #else |
| 486 | } | 469 | } |
| 487 | closedir(dirp); | 470 | closedir(dirp); |
| 488 | #endif | 471 | #endif |
| 489 | // Return number of entries found. | 472 | // Return number of entries found. |
| 490 | return foundEntries; | 473 | return found_entries; |
| 491 | } | 474 | } |
| 492 | 475 | ||
| 493 | 476 | int ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry) | |
| 494 | // Deletes the given directory and anything under it. Returns true on success. | ||
| 495 | bool DeleteDirRecursively(const std::string &directory) | ||
| 496 | { | 477 | { |
| 497 | LOG_TRACE(Common_Filesystem, "%s", directory.c_str()); | 478 | const auto callback = [&parent_entry](const std::string& directory, |
| 498 | #ifdef _WIN32 | 479 | const std::string& virtual_name) -> int { |
| 499 | // Find the first file in the directory. | 480 | FSTEntry entry; |
| 500 | WIN32_FIND_DATA ffd; | 481 | int found_entries = 0; |
| 501 | HANDLE hFind = FindFirstFile(Common::UTF8ToTStr(directory + "\\*").c_str(), &ffd); | 482 | entry.virtualName = virtual_name; |
| 502 | 483 | entry.physicalName = directory + DIR_SEP + virtual_name; | |
| 503 | if (hFind == INVALID_HANDLE_VALUE) | ||
| 504 | { | ||
| 505 | FindClose(hFind); | ||
| 506 | return false; | ||
| 507 | } | ||
| 508 | |||
| 509 | // windows loop | ||
| 510 | do | ||
| 511 | { | ||
| 512 | const std::string virtualName(Common::TStrToUTF8(ffd.cFileName)); | ||
| 513 | #else | ||
| 514 | struct dirent dirent, *result = nullptr; | ||
| 515 | DIR *dirp = opendir(directory.c_str()); | ||
| 516 | if (!dirp) | ||
| 517 | return false; | ||
| 518 | |||
| 519 | // non windows loop | ||
| 520 | while (!readdir_r(dirp, &dirent, &result) && result) | ||
| 521 | { | ||
| 522 | const std::string virtualName = result->d_name; | ||
| 523 | #endif | ||
| 524 | 484 | ||
| 525 | // check for "." and ".." | 485 | if (IsDirectory(entry.physicalName)) { |
| 526 | if (((virtualName[0] == '.') && (virtualName[1] == '\0')) || | 486 | entry.isDirectory = true; |
| 527 | ((virtualName[0] == '.') && (virtualName[1] == '.') && | 487 | // is a directory, lets go inside |
| 528 | (virtualName[2] == '\0'))) | 488 | entry.size = ScanDirectoryTree(entry.physicalName, entry); |
| 529 | continue; | 489 | found_entries += (int)entry.size; |
| 490 | } else { // is a file | ||
| 491 | entry.isDirectory = false; | ||
| 492 | entry.size = GetSize(entry.physicalName); | ||
| 493 | } | ||
| 494 | ++found_entries; | ||
| 495 | // Push into the tree | ||
| 496 | parent_entry.children.push_back(entry); | ||
| 497 | return found_entries; | ||
| 498 | }; | ||
| 530 | 499 | ||
| 531 | std::string newPath = directory + DIR_SEP_CHR + virtualName; | 500 | return ScanDirectoryTreeAndCallback(directory, callback); |
| 532 | if (IsDirectory(newPath)) | 501 | } |
| 533 | { | ||
| 534 | if (!DeleteDirRecursively(newPath)) | ||
| 535 | { | ||
| 536 | #ifndef _WIN32 | ||
| 537 | closedir(dirp); | ||
| 538 | #endif | ||
| 539 | 502 | ||
| 540 | return false; | ||
| 541 | } | ||
| 542 | } | ||
| 543 | else | ||
| 544 | { | ||
| 545 | if (!FileUtil::Delete(newPath)) | ||
| 546 | { | ||
| 547 | #ifndef _WIN32 | ||
| 548 | closedir(dirp); | ||
| 549 | #endif | ||
| 550 | 503 | ||
| 551 | return false; | 504 | bool DeleteDirRecursively(const std::string &directory) |
| 505 | { | ||
| 506 | const static auto callback = [](const std::string& directory, | ||
| 507 | const std::string& virtual_name) -> int { | ||
| 508 | std::string new_path = directory + DIR_SEP_CHR + virtual_name; | ||
| 509 | if (IsDirectory(new_path)) { | ||
| 510 | if (!DeleteDirRecursively(new_path)) { | ||
| 511 | return -2; | ||
| 552 | } | 512 | } |
| 513 | } else if (!Delete(new_path)) { | ||
| 514 | return -2; | ||
| 553 | } | 515 | } |
| 516 | return 0; | ||
| 517 | }; | ||
| 554 | 518 | ||
| 555 | #ifdef _WIN32 | 519 | if (ScanDirectoryTreeAndCallback(directory, callback) == -2) { |
| 556 | } while (FindNextFile(hFind, &ffd) != 0); | 520 | return false; |
| 557 | FindClose(hFind); | ||
| 558 | #else | ||
| 559 | } | 521 | } |
| 560 | closedir(dirp); | ||
| 561 | #endif | ||
| 562 | FileUtil::DeleteDir(directory); | 522 | FileUtil::DeleteDir(directory); |
| 563 | 523 | ||
| 564 | return true; | 524 | return true; |
diff --git a/src/common/file_util.h b/src/common/file_util.h index e71a9b2fa..3d617f573 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | 6 | ||
| 7 | #include <array> | 7 | #include <array> |
| 8 | #include <fstream> | 8 | #include <fstream> |
| 9 | #include <functional> | ||
| 9 | #include <cstddef> | 10 | #include <cstddef> |
| 10 | #include <cstdio> | 11 | #include <cstdio> |
| 11 | #include <string> | 12 | #include <string> |
| @@ -96,9 +97,28 @@ bool Copy(const std::string &srcFilename, const std::string &destFilename); | |||
| 96 | // creates an empty file filename, returns true on success | 97 | // creates an empty file filename, returns true on success |
| 97 | bool CreateEmptyFile(const std::string &filename); | 98 | bool CreateEmptyFile(const std::string &filename); |
| 98 | 99 | ||
| 99 | // Scans the directory tree gets, starting from _Directory and adds the | 100 | /** |
| 100 | // results into parentEntry. Returns the number of files+directories found | 101 | * Scans the directory tree, calling the callback for each file/directory found. |
| 101 | u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry); | 102 | * The callback must return the number of files and directories which the provided path contains. |
| 103 | * If the callback's return value is -1, the callback loop is broken immediately. | ||
| 104 | * If the callback's return value is otherwise negative, the callback loop is broken immediately | ||
| 105 | * and the callback's return value is returned from this function (to allow for error handling). | ||
| 106 | * @param directory the parent directory to start scanning from | ||
| 107 | * @param callback The callback which will be called for each file/directory. It is called | ||
| 108 | * with the arguments (const std::string& directory, const std::string& virtual_name). | ||
| 109 | * The `directory `parameter is the path to the directory which contains the file/directory. | ||
| 110 | * The `virtual_name` parameter is the incomplete file path, without any directory info. | ||
| 111 | * @return the total number of files/directories found | ||
| 112 | */ | ||
| 113 | int ScanDirectoryTreeAndCallback(const std::string &directory, std::function<int(const std::string&, const std::string&)> callback); | ||
| 114 | |||
| 115 | /** | ||
| 116 | * Scans the directory tree, storing the results. | ||
| 117 | * @param directory the parent directory to start scanning from | ||
| 118 | * @param parent_entry FSTEntry where the filesystem tree results will be stored. | ||
| 119 | * @return the total number of files/directories found | ||
| 120 | */ | ||
| 121 | int ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry); | ||
| 102 | 122 | ||
| 103 | // deletes the given directory and anything under it. Returns true on success. | 123 | // deletes the given directory and anything under it. Returns true on success. |
| 104 | bool DeleteDirRecursively(const std::string &directory); | 124 | bool DeleteDirRecursively(const std::string &directory); |
diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp index c4b4f5a5d..6b88169e1 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp | |||
| @@ -26,12 +26,7 @@ const std::initializer_list<Kernel::AddressMapping> default_address_mappings = { | |||
| 26 | { 0x1F000000, 0x600000, false }, // entire VRAM | 26 | { 0x1F000000, 0x600000, false }, // entire VRAM |
| 27 | }; | 27 | }; |
| 28 | 28 | ||
| 29 | /** | 29 | FileType IdentifyFile(FileUtil::IOFile& file) { |
| 30 | * Identifies the type of a bootable file | ||
| 31 | * @param file open file | ||
| 32 | * @return FileType of file | ||
| 33 | */ | ||
| 34 | static FileType IdentifyFile(FileUtil::IOFile& file) { | ||
| 35 | FileType type; | 30 | FileType type; |
| 36 | 31 | ||
| 37 | #define CHECK_TYPE(loader) \ | 32 | #define CHECK_TYPE(loader) \ |
| @@ -48,12 +43,17 @@ static FileType IdentifyFile(FileUtil::IOFile& file) { | |||
| 48 | return FileType::Unknown; | 43 | return FileType::Unknown; |
| 49 | } | 44 | } |
| 50 | 45 | ||
| 51 | /** | 46 | FileType IdentifyFile(const std::string& file_name) { |
| 52 | * Guess the type of a bootable file from its extension | 47 | FileUtil::IOFile file(file_name, "rb"); |
| 53 | * @param extension_ String extension of bootable file | 48 | if (!file.IsOpen()) { |
| 54 | * @return FileType of file | 49 | LOG_ERROR(Loader, "Failed to load file %s", file_name.c_str()); |
| 55 | */ | 50 | return FileType::Unknown; |
| 56 | static FileType GuessFromExtension(const std::string& extension_) { | 51 | } |
| 52 | |||
| 53 | return IdentifyFile(file); | ||
| 54 | } | ||
| 55 | |||
| 56 | FileType GuessFromExtension(const std::string& extension_) { | ||
| 57 | std::string extension = Common::ToLower(extension_); | 57 | std::string extension = Common::ToLower(extension_); |
| 58 | 58 | ||
| 59 | if (extension == ".elf" || extension == ".axf") | 59 | if (extension == ".elf" || extension == ".axf") |
| @@ -71,7 +71,7 @@ static FileType GuessFromExtension(const std::string& extension_) { | |||
| 71 | return FileType::Unknown; | 71 | return FileType::Unknown; |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | static const char* GetFileTypeString(FileType type) { | 74 | const char* GetFileTypeString(FileType type) { |
| 75 | switch (type) { | 75 | switch (type) { |
| 76 | case FileType::CCI: | 76 | case FileType::CCI: |
| 77 | return "NCSD"; | 77 | return "NCSD"; |
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h index a37d3348c..8de95dacf 100644 --- a/src/core/loader/loader.h +++ b/src/core/loader/loader.h | |||
| @@ -33,6 +33,34 @@ enum class FileType { | |||
| 33 | THREEDSX, //3DSX | 33 | THREEDSX, //3DSX |
| 34 | }; | 34 | }; |
| 35 | 35 | ||
| 36 | /** | ||
| 37 | * Identifies the type of a bootable file based on the magic value in its header. | ||
| 38 | * @param file open file | ||
| 39 | * @return FileType of file | ||
| 40 | */ | ||
| 41 | FileType IdentifyFile(FileUtil::IOFile& file); | ||
| 42 | |||
| 43 | /** | ||
| 44 | * Identifies the type of a bootable file based on the magic value in its header. | ||
| 45 | * @param file_name path to file | ||
| 46 | * @return FileType of file. Note: this will return FileType::Unknown if it is unable to determine | ||
| 47 | * a filetype, and will never return FileType::Error. | ||
| 48 | */ | ||
| 49 | FileType IdentifyFile(const std::string& file_name); | ||
| 50 | |||
| 51 | /** | ||
| 52 | * Guess the type of a bootable file from its extension | ||
| 53 | * @param extension String extension of bootable file | ||
| 54 | * @return FileType of file. Note: this will return FileType::Unknown if it is unable to determine | ||
| 55 | * a filetype, and will never return FileType::Error. | ||
| 56 | */ | ||
| 57 | FileType GuessFromExtension(const std::string& extension_); | ||
| 58 | |||
| 59 | /** | ||
| 60 | * Convert a FileType into a string which can be displayed to the user. | ||
| 61 | */ | ||
| 62 | const char* GetFileTypeString(FileType type); | ||
| 63 | |||
| 36 | /// Return type for functions in Loader namespace | 64 | /// Return type for functions in Loader namespace |
| 37 | enum class ResultStatus { | 65 | enum class ResultStatus { |
| 38 | Success, | 66 | Success, |