From 314ce5e505aca066ad4d0385be46d7e8de9f6dfb Mon Sep 17 00:00:00 2001
From: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
Date: Wed, 18 May 2016 22:06:50 +0100
Subject: [PATCH] CitraQt: Simplify the game list loader code

---
 src/citra/citra.cpp        |  2 +-
 src/citra_qt/game_list.cpp | 22 ++++------------------
 src/citra_qt/main.cpp      |  2 +-
 src/core/loader/loader.cpp | 14 +++++++++++---
 src/core/loader/loader.h   | 12 +-----------
 5 files changed, 18 insertions(+), 34 deletions(-)

diff --git a/src/citra/citra.cpp b/src/citra/citra.cpp
index aa3bf94ca4..e01216734c 100644
--- a/src/citra/citra.cpp
+++ b/src/citra/citra.cpp
@@ -114,7 +114,7 @@ int main(int argc, char **argv) {
     System::Init(emu_window.get());
     SCOPE_EXIT({ System::Shutdown(); });
 
-    std::unique_ptr<Loader::AppLoader> loader = Loader::GetFileLoader(boot_filename);
+    std::unique_ptr<Loader::AppLoader> loader = Loader::GetLoader(boot_filename);
     if (!loader) {
         LOG_CRITICAL(Frontend, "Failed to obtain loader for %s!", boot_filename.c_str());
         return -1;
diff --git a/src/citra_qt/game_list.cpp b/src/citra_qt/game_list.cpp
index d4ac9c96e4..5706475392 100644
--- a/src/citra_qt/game_list.cpp
+++ b/src/citra_qt/game_list.cpp
@@ -132,30 +132,16 @@ void GameListWorker::AddFstEntriesToGameList(const std::string& dir_path, bool d
         if (deep_scan && FileUtil::IsDirectory(physical_name)) {
             AddFstEntriesToGameList(physical_name, true);
         } else {
-            std::string filename_filename, filename_extension;
-            Common::SplitPath(physical_name, nullptr, &filename_filename, &filename_extension);
-
-            Loader::FileType guessed_filetype = Loader::GuessFromExtension(filename_extension);
-            if (guessed_filetype == Loader::FileType::Unknown)
-                return true;
-            Loader::FileType filetype = Loader::IdentifyFile(physical_name);
-            if (filetype == Loader::FileType::Unknown) {
-                LOG_WARNING(Frontend, "File %s is of indeterminate type and is possibly corrupted.", physical_name.c_str());
+            std::unique_ptr<Loader::AppLoader> loader = Loader::GetLoader(physical_name);
+            if (!loader)
                 return true;
-            }
-            if (guessed_filetype != filetype) {
-                LOG_WARNING(Frontend, "Filetype and extension of file %s do not match.", physical_name.c_str());
-            }
 
             std::vector<u8> smdh;
-            std::unique_ptr<Loader::AppLoader> loader = Loader::GetLoader(FileUtil::IOFile(physical_name, "rb"), filetype, filename_filename, physical_name);
-
-            if (loader)
-                loader->ReadIcon(smdh);
+            loader->ReadIcon(smdh);
 
             emit EntryReady({
                 new GameListItemPath(QString::fromStdString(physical_name), smdh),
-                new GameListItem(QString::fromStdString(Loader::GetFileTypeString(filetype))),
+                new GameListItem(QString::fromStdString(Loader::GetFileTypeString(loader->GetFileType()))),
                 new GameListItemSize(FileUtil::GetSize(physical_name)),
             });
         }
diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp
index 9d47014aa1..6239160bcd 100644
--- a/src/citra_qt/main.cpp
+++ b/src/citra_qt/main.cpp
@@ -272,7 +272,7 @@ bool GMainWindow::InitializeSystem() {
 }
 
 bool GMainWindow::LoadROM(const std::string& filename) {
-    std::unique_ptr<Loader::AppLoader> app_loader = Loader::GetFileLoader(filename);
+    std::unique_ptr<Loader::AppLoader> app_loader = Loader::GetLoader(filename);
     if (!app_loader) {
         LOG_CRITICAL(Frontend, "Failed to obtain loader for %s!", filename.c_str());
         QMessageBox::critical(this, tr("Error while loading ROM!"),
diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp
index c82688026e..9719d30d5f 100644
--- a/src/core/loader/loader.cpp
+++ b/src/core/loader/loader.cpp
@@ -91,7 +91,15 @@ const char* GetFileTypeString(FileType type) {
     return "unknown";
 }
 
-std::unique_ptr<AppLoader> GetLoader(FileUtil::IOFile&& file, FileType type,
+/**
+ * Get a loader for a file with a specific type
+ * @param file The file to load
+ * @param type The type of the file
+ * @param filename the file name (without path)
+ * @param filepath the file full path (with name)
+ * @return std::unique_ptr<AppLoader> a pointer to a loader object;  nullptr for unsupported type
+ */
+static std::unique_ptr<AppLoader> GetFileLoader(FileUtil::IOFile&& file, FileType type,
     const std::string& filename, const std::string& filepath) {
     switch (type) {
 
@@ -113,7 +121,7 @@ std::unique_ptr<AppLoader> GetLoader(FileUtil::IOFile&& file, FileType type,
     }
 }
 
-std::unique_ptr<AppLoader> GetFileLoader(const std::string& filename) {
+std::unique_ptr<AppLoader> GetLoader(const std::string& filename) {
     FileUtil::IOFile file(filename, "rb");
     if (!file.IsOpen()) {
         LOG_ERROR(Loader, "Failed to load file %s", filename.c_str());
@@ -134,7 +142,7 @@ std::unique_ptr<AppLoader> GetFileLoader(const std::string& filename) {
 
     LOG_INFO(Loader, "Loading file %s as %s...", filename.c_str(), GetFileTypeString(type));
 
-    return GetLoader(std::move(file), type, filename_filename, filename);
+    return GetFileLoader(std::move(file), type, filename_filename, filename);
 }
 
 } // namespace Loader
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h
index 08bab84e5f..39aedfeebe 100644
--- a/src/core/loader/loader.h
+++ b/src/core/loader/loader.h
@@ -202,21 +202,11 @@ protected:
  */
 extern const std::initializer_list<Kernel::AddressMapping> default_address_mappings;
 
-/**
- * Get a loader for a file with a specific type
- * @param file The file to load
- * @param type The type of the file
- * @param filename the file name (without path)
- * @param filepath the file full path (with name)
- * @return std::unique_ptr<AppLoader> a pointer to a loader object;  nullptr for unsupported type
- */
-std::unique_ptr<AppLoader> GetLoader(FileUtil::IOFile&& file, FileType type, const std::string& filename, const std::string& filepath);
-
 /**
  * Identifies a bootable file and return a suitable loader
  * @param filename String filename of bootable file
  * @return best loader for this file
  */
-std::unique_ptr<AppLoader> GetFileLoader(const std::string& filename);
+std::unique_ptr<AppLoader> GetLoader(const std::string& filename);
 
 } // namespace
-- 
GitLab