From f6153679b0781eea084b22f3ceecc74b1fe6b018 Mon Sep 17 00:00:00 2001
From: Yuri Kunde Schlesner <yuriks@yuriks.net>
Date: Mon, 15 Dec 2014 02:44:04 -0200
Subject: [PATCH] Service.FS: Do archive registration using IdCode instead of
 name

---
 src/core/file_sys/archive.h         | 17 ++---------------
 src/core/file_sys/archive_romfs.h   |  6 +-----
 src/core/file_sys/archive_sdmc.h    |  6 +-----
 src/core/hle/service/fs/archive.cpp | 20 ++++++++++----------
 src/core/hle/service/fs/archive.h   | 19 +++++++++++++++----
 src/core/hle/service/fs/fs_user.cpp |  4 ++--
 src/core/loader/loader.cpp          |  2 +-
 7 files changed, 32 insertions(+), 42 deletions(-)

diff --git a/src/core/file_sys/archive.h b/src/core/file_sys/archive.h
index 27ed23cd0..b7978bfbe 100644
--- a/src/core/file_sys/archive.h
+++ b/src/core/file_sys/archive.h
@@ -162,25 +162,12 @@ private:
 
 class Archive : NonCopyable {
 public:
-    /// Supported archive types
-    enum class IdCode : u32 {
-        RomFS               = 0x00000003,
-        SaveData            = 0x00000004,
-        ExtSaveData         = 0x00000006,
-        SharedExtSaveData   = 0x00000007,
-        SystemSaveData      = 0x00000008,
-        SDMC                = 0x00000009,
-        SDMCWriteOnly       = 0x0000000A,
-    };
-
-    Archive() { }
     virtual ~Archive() { }
 
     /**
-     * Get the IdCode of the archive (e.g. RomFS, SaveData, etc.)
-     * @return IdCode of the archive
+     * Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.)
      */
-    virtual IdCode GetIdCode() const = 0;
+    virtual std::string GetName() const = 0;
 
     /**
      * Open a file specified by its path, using the specified mode
diff --git a/src/core/file_sys/archive_romfs.h b/src/core/file_sys/archive_romfs.h
index 222bdc356..b60fcca99 100644
--- a/src/core/file_sys/archive_romfs.h
+++ b/src/core/file_sys/archive_romfs.h
@@ -22,11 +22,7 @@ public:
     Archive_RomFS(const Loader::AppLoader& app_loader);
     ~Archive_RomFS() override;
 
-    /**
-     * Get the IdCode of the archive (e.g. RomFS, SaveData, etc.)
-     * @return IdCode of the archive
-     */
-    IdCode GetIdCode() const override { return IdCode::RomFS; }
+    std::string GetName() const override { return "RomFS"; }
 
     /**
      * Open a file specified by its path, using the specified mode
diff --git a/src/core/file_sys/archive_sdmc.h b/src/core/file_sys/archive_sdmc.h
index 19f563a62..54c18cb0c 100644
--- a/src/core/file_sys/archive_sdmc.h
+++ b/src/core/file_sys/archive_sdmc.h
@@ -26,11 +26,7 @@ public:
      */
     bool Initialize();
 
-    /**
-     * Get the IdCode of the archive (e.g. RomFS, SaveData, etc.)
-     * @return IdCode of the archive
-     */
-    IdCode GetIdCode() const override { return IdCode::SDMC; }
+    std::string GetName() const override { return "SDMC"; }
 
     /**
      * Open a file specified by its path, using the specified mode
diff --git a/src/core/hle/service/fs/archive.cpp b/src/core/hle/service/fs/archive.cpp
index 5893a944b..61cbfa73c 100644
--- a/src/core/hle/service/fs/archive.cpp
+++ b/src/core/hle/service/fs/archive.cpp
@@ -43,9 +43,9 @@ enum class DirectoryCommand : u32 {
 
 class Archive : public Kernel::Session {
 public:
-    std::string GetName() const override { return "Archive: " + name; }
+    std::string GetName() const override { return "Archive: " + backend->GetName(); }
 
-    std::string name;           ///< Name of archive (optional)
+    ArchiveIdCode id_code;      ///< Id code of the archive
     FileSys::Archive* backend;  ///< Archive backend interface
 
     ResultVal<bool> SyncRequest() override {
@@ -91,7 +91,7 @@ public:
         case FileCommand::Close:
         {
             LOG_TRACE(Service_FS, "Close %s %s", GetTypeName().c_str(), GetName().c_str());
-            CloseArchive(backend->GetIdCode());
+            CloseArchive(id_code);
             break;
         }
         // Unknown command...
@@ -228,9 +228,9 @@ public:
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 
-std::map<FileSys::Archive::IdCode, Handle> g_archive_map; ///< Map of file archives by IdCode
+std::map<ArchiveIdCode, Handle> g_archive_map; ///< Map of file archives by IdCode
 
-ResultVal<Handle> OpenArchive(FileSys::Archive::IdCode id_code) {
+ResultVal<Handle> OpenArchive(ArchiveIdCode id_code) {
     auto itr = g_archive_map.find(id_code);
     if (itr == g_archive_map.end()) {
         return ResultCode(ErrorDescription::NotFound, ErrorModule::FS,
@@ -240,7 +240,7 @@ ResultVal<Handle> OpenArchive(FileSys::Archive::IdCode id_code) {
     return MakeResult<Handle>(itr->second);
 }
 
-ResultCode CloseArchive(FileSys::Archive::IdCode id_code) {
+ResultCode CloseArchive(ArchiveIdCode id_code) {
     auto itr = g_archive_map.find(id_code);
     if (itr == g_archive_map.end()) {
         LOG_ERROR(Service_FS, "Cannot close archive %d, does not exist!", (int)id_code);
@@ -256,7 +256,7 @@ ResultCode CloseArchive(FileSys::Archive::IdCode id_code) {
  * @param archive Pointer to the archive to mount
  */
 ResultCode MountArchive(Archive* archive) {
-    FileSys::Archive::IdCode id_code = archive->backend->GetIdCode();
+    ArchiveIdCode id_code = archive->id_code;
     ResultVal<Handle> archive_handle = OpenArchive(id_code);
     if (archive_handle.Succeeded()) {
         LOG_ERROR(Service_FS, "Cannot mount two archives with the same ID code! (%d)", (int) id_code);
@@ -267,10 +267,10 @@ ResultCode MountArchive(Archive* archive) {
     return RESULT_SUCCESS;
 }
 
-ResultCode CreateArchive(FileSys::Archive* backend, const std::string& name) {
+ResultCode CreateArchive(FileSys::Archive* backend, ArchiveIdCode id_code) {
     Archive* archive = new Archive;
     Handle handle = Kernel::g_object_pool.Create(archive);
-    archive->name = name;
+    archive->id_code = id_code;
     archive->backend = backend;
 
     ResultCode result = MountArchive(archive);
@@ -411,7 +411,7 @@ void ArchiveInit() {
     std::string sdmc_directory = FileUtil::GetUserPath(D_SDMC_IDX);
     auto archive = new FileSys::Archive_SDMC(sdmc_directory);
     if (archive->Initialize())
-        CreateArchive(archive, "SDMC");
+        CreateArchive(archive, ArchiveIdCode::SDMC);
     else
         LOG_ERROR(Service_FS, "Can't instantiate SDMC archive with path %s", sdmc_directory.c_str());
 }
diff --git a/src/core/hle/service/fs/archive.h b/src/core/hle/service/fs/archive.h
index a7ee212d3..9a1df5110 100644
--- a/src/core/hle/service/fs/archive.h
+++ b/src/core/hle/service/fs/archive.h
@@ -13,25 +13,36 @@
 namespace Service {
 namespace FS {
 
+/// Supported archive types
+enum class ArchiveIdCode : u32 {
+    RomFS               = 0x00000003,
+    SaveData            = 0x00000004,
+    ExtSaveData         = 0x00000006,
+    SharedExtSaveData   = 0x00000007,
+    SystemSaveData      = 0x00000008,
+    SDMC                = 0x00000009,
+    SDMCWriteOnly       = 0x0000000A,
+};
+
 /**
  * Opens an archive
  * @param id_code IdCode of the archive to open
  * @return Handle to the opened archive
  */
-ResultVal<Handle> OpenArchive(FileSys::Archive::IdCode id_code);
+ResultVal<Handle> OpenArchive(ArchiveIdCode id_code);
 
 /**
  * Closes an archive
  * @param id_code IdCode of the archive to open
  */
-ResultCode CloseArchive(FileSys::Archive::IdCode id_code);
+ResultCode CloseArchive(ArchiveIdCode id_code);
 
 /**
  * Creates an Archive
  * @param backend File system backend interface to the archive
- * @param name Name of Archive
+ * @param id_code Id code used to access this type of archive
  */
-ResultCode CreateArchive(FileSys::Archive* backend, const std::string& name);
+ResultCode CreateArchive(FileSys::Archive* backend, ArchiveIdCode id_code);
 
 /**
  * Open a File from an Archive
diff --git a/src/core/hle/service/fs/fs_user.cpp b/src/core/hle/service/fs/fs_user.cpp
index 76689c6af..eda7cb8ab 100644
--- a/src/core/hle/service/fs/fs_user.cpp
+++ b/src/core/hle/service/fs/fs_user.cpp
@@ -88,7 +88,7 @@ static void OpenFile(Service::Interface* self) {
 static void OpenFileDirectly(Service::Interface* self) {
     u32* cmd_buff = Kernel::GetCommandBuffer();
 
-    auto archive_id       = static_cast<FileSys::Archive::IdCode>(cmd_buff[2]);
+    auto archive_id       = static_cast<FS::ArchiveIdCode>(cmd_buff[2]);
     auto archivename_type = static_cast<FileSys::LowPathType>(cmd_buff[3]);
     u32 archivename_size  = cmd_buff[4];
     auto filename_type    = static_cast<FileSys::LowPathType>(cmd_buff[5]);
@@ -334,7 +334,7 @@ static void OpenDirectory(Service::Interface* self) {
 static void OpenArchive(Service::Interface* self) {
     u32* cmd_buff = Kernel::GetCommandBuffer();
 
-    auto archive_id       = static_cast<FileSys::Archive::IdCode>(cmd_buff[1]);
+    auto archive_id       = static_cast<FS::ArchiveIdCode>(cmd_buff[1]);
     auto archivename_type = static_cast<FileSys::LowPathType>(cmd_buff[2]);
     u32 archivename_size  = cmd_buff[3];
     u32 archivename_ptr   = cmd_buff[5];
diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp
index 6ce752561..49f8c458d 100644
--- a/src/core/loader/loader.cpp
+++ b/src/core/loader/loader.cpp
@@ -74,7 +74,7 @@ ResultStatus LoadFile(const std::string& filename) {
 
         // Load application and RomFS
         if (ResultStatus::Success == app_loader.Load()) {
-            Service::FS::CreateArchive(new FileSys::Archive_RomFS(app_loader), "RomFS");
+            Service::FS::CreateArchive(new FileSys::Archive_RomFS(app_loader), Service::FS::ArchiveIdCode::RomFS);
             return ResultStatus::Success;
         }
         break;