summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/citra_qt/game_list.cpp10
-rw-r--r--src/citra_qt/game_list.h2
-rw-r--r--src/citra_qt/game_list_p.h106
-rw-r--r--src/core/loader/3dsx.cpp27
-rw-r--r--src/core/loader/3dsx.h9
-rw-r--r--src/core/loader/loader.cpp50
-rw-r--r--src/core/loader/loader.h57
-rw-r--r--src/core/loader/ncch.cpp22
-rw-r--r--src/core/loader/ncch.h7
9 files changed, 254 insertions, 36 deletions
diff --git a/src/citra_qt/game_list.cpp b/src/citra_qt/game_list.cpp
index d14532102..32339e6a6 100644
--- a/src/citra_qt/game_list.cpp
+++ b/src/citra_qt/game_list.cpp
@@ -34,8 +34,8 @@ GameList::GameList(QWidget* parent)
34 tree_view->setUniformRowHeights(true); 34 tree_view->setUniformRowHeights(true);
35 35
36 item_model->insertColumns(0, COLUMN_COUNT); 36 item_model->insertColumns(0, COLUMN_COUNT);
37 item_model->setHeaderData(COLUMN_FILE_TYPE, Qt::Horizontal, "File type");
38 item_model->setHeaderData(COLUMN_NAME, Qt::Horizontal, "Name"); 37 item_model->setHeaderData(COLUMN_NAME, Qt::Horizontal, "Name");
38 item_model->setHeaderData(COLUMN_FILE_TYPE, Qt::Horizontal, "File type");
39 item_model->setHeaderData(COLUMN_SIZE, Qt::Horizontal, "Size"); 39 item_model->setHeaderData(COLUMN_SIZE, Qt::Horizontal, "Size");
40 40
41 connect(tree_view, SIGNAL(activated(const QModelIndex&)), this, SLOT(ValidateEntry(const QModelIndex&))); 41 connect(tree_view, SIGNAL(activated(const QModelIndex&)), this, SLOT(ValidateEntry(const QModelIndex&)));
@@ -143,9 +143,15 @@ void GameListWorker::AddFstEntriesToGameList(const std::string& dir_path, bool d
143 LOG_WARNING(Frontend, "Filetype and extension of file %s do not match.", physical_name.c_str()); 143 LOG_WARNING(Frontend, "Filetype and extension of file %s do not match.", physical_name.c_str());
144 } 144 }
145 145
146 std::vector<u8> smdh;
147 std::unique_ptr<Loader::AppLoader> loader = Loader::GetLoader(FileUtil::IOFile(physical_name, "rb"), filetype, filename_filename, physical_name);
148
149 if (loader)
150 loader->ReadIcon(smdh);
151
146 emit EntryReady({ 152 emit EntryReady({
153 new GameListItemPath(QString::fromStdString(physical_name), smdh),
147 new GameListItem(QString::fromStdString(Loader::GetFileTypeString(filetype))), 154 new GameListItem(QString::fromStdString(Loader::GetFileTypeString(filetype))),
148 new GameListItemPath(QString::fromStdString(physical_name)),
149 new GameListItemSize(FileUtil::GetSize(physical_name)), 155 new GameListItemSize(FileUtil::GetSize(physical_name)),
150 }); 156 });
151 } 157 }
diff --git a/src/citra_qt/game_list.h b/src/citra_qt/game_list.h
index 48febdc60..198674f04 100644
--- a/src/citra_qt/game_list.h
+++ b/src/citra_qt/game_list.h
@@ -20,8 +20,8 @@ class GameList : public QWidget {
20 20
21public: 21public:
22 enum { 22 enum {
23 COLUMN_FILE_TYPE,
24 COLUMN_NAME, 23 COLUMN_NAME,
24 COLUMN_FILE_TYPE,
25 COLUMN_SIZE, 25 COLUMN_SIZE,
26 COLUMN_COUNT, // Number of columns 26 COLUMN_COUNT, // Number of columns
27 }; 27 };
diff --git a/src/citra_qt/game_list_p.h b/src/citra_qt/game_list_p.h
index 820012bce..284f5da81 100644
--- a/src/citra_qt/game_list_p.h
+++ b/src/citra_qt/game_list_p.h
@@ -6,13 +6,85 @@
6 6
7#include <atomic> 7#include <atomic>
8 8
9#include <QImage>
9#include <QRunnable> 10#include <QRunnable>
10#include <QStandardItem> 11#include <QStandardItem>
11#include <QString> 12#include <QString>
12 13
13#include "citra_qt/util/util.h" 14#include "citra_qt/util/util.h"
14#include "common/string_util.h" 15#include "common/string_util.h"
16#include "common/color.h"
15 17
18#include "core/loader/loader.h"
19
20#include "video_core/utils.h"
21
22/**
23 * Tests if data is a valid SMDH by its length and magic number.
24 * @param smdh_data data buffer to test
25 * @return bool test result
26 */
27static bool IsValidSMDH(const std::vector<u8>& smdh_data) {
28 if (smdh_data.size() < sizeof(Loader::SMDH))
29 return false;
30
31 u32 magic;
32 memcpy(&magic, smdh_data.data(), 4);
33
34 return Loader::MakeMagic('S', 'M', 'D', 'H') == magic;
35}
36
37/**
38 * Gets game icon from SMDH
39 * @param sdmh SMDH data
40 * @param large If true, returns large icon (48x48), otherwise returns small icon (24x24)
41 * @return QPixmap game icon
42 */
43static QPixmap GetIconFromSMDH(const Loader::SMDH& smdh, bool large) {
44 u32 size;
45 const u8* icon_data;
46
47 if (large) {
48 size = 48;
49 icon_data = smdh.large_icon.data();
50 } else {
51 size = 24;
52 icon_data = smdh.small_icon.data();
53 }
54
55 QImage icon(size, size, QImage::Format::Format_RGB888);
56 for (u32 x = 0; x < size; ++x) {
57 for (u32 y = 0; y < size; ++y) {
58 u32 coarse_y = y & ~7;
59 auto v = Color::DecodeRGB565(
60 icon_data + VideoCore::GetMortonOffset(x, y, 2) + coarse_y * size * 2);
61 icon.setPixel(x, y, qRgb(v.r(), v.g(), v.b()));
62 }
63 }
64 return QPixmap::fromImage(icon);
65}
66
67/**
68 * Gets the default icon (for games without valid SMDH)
69 * @param large If true, returns large icon (48x48), otherwise returns small icon (24x24)
70 * @return QPixmap default icon
71 */
72static QPixmap GetDefaultIcon(bool large) {
73 int size = large ? 48 : 24;
74 QPixmap icon(size, size);
75 icon.fill(Qt::transparent);
76 return icon;
77}
78
79/**
80 * Gets the short game title fromn SMDH
81 * @param sdmh SMDH data
82 * @param language title language
83 * @return QString short title
84 */
85static QString GetShortTitleFromSMDH(const Loader::SMDH& smdh, Loader::SMDH::TitleLanguage language) {
86 return QString::fromUtf16(smdh.titles[static_cast<int>(language)].short_title.data());
87}
16 88
17class GameListItem : public QStandardItem { 89class GameListItem : public QStandardItem {
18 90
@@ -27,29 +99,43 @@ public:
27 * A specialization of GameListItem for path values. 99 * A specialization of GameListItem for path values.
28 * This class ensures that for every full path value it holds, a correct string representation 100 * 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. 101 * of just the filename (with no extension) will be displayed to the user.
102 * If this class recieves valid SMDH data, it will also display game icons and titles.
30 */ 103 */
31class GameListItemPath : public GameListItem { 104class GameListItemPath : public GameListItem {
32 105
33public: 106public:
34 static const int FullPathRole = Qt::UserRole + 1; 107 static const int FullPathRole = Qt::UserRole + 1;
108 static const int TitleRole = Qt::UserRole + 2;
35 109
36 GameListItemPath(): GameListItem() {} 110 GameListItemPath(): GameListItem() {}
37 GameListItemPath(const QString& game_path): GameListItem() 111 GameListItemPath(const QString& game_path, const std::vector<u8>& smdh_data): GameListItem()
38 { 112 {
39 setData(game_path, FullPathRole); 113 setData(game_path, FullPathRole);
114
115 if (!IsValidSMDH(smdh_data)) {
116 // SMDH is not valid, set a default icon
117 setData(GetDefaultIcon(true), Qt::DecorationRole);
118 return;
119 }
120
121 Loader::SMDH smdh;
122 memcpy(&smdh, smdh_data.data(), sizeof(Loader::SMDH));
123
124 // Get icon from SMDH
125 setData(GetIconFromSMDH(smdh, true), Qt::DecorationRole);
126
127 // Get title form SMDH
128 setData(GetShortTitleFromSMDH(smdh, Loader::SMDH::TitleLanguage::English), TitleRole);
40 } 129 }
41 130
42 void setData(const QVariant& value, int role) override 131 QVariant data(int role) const override {
43 { 132 if (role == Qt::DisplayRole) {
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; 133 std::string filename;
48 Common::SplitPath(value.toString().toStdString(), nullptr, &filename, nullptr); 134 Common::SplitPath(data(FullPathRole).toString().toStdString(), nullptr, &filename, nullptr);
49 GameListItem::setData(QString::fromStdString(filename), Qt::DisplayRole); 135 QString title = data(TitleRole).toString();
50 GameListItem::setData(value, FullPathRole); 136 return QString::fromStdString(filename) + (title.isEmpty() ? "" : "\n " + title);
51 } else { 137 } else {
52 GameListItem::setData(value, role); 138 return GameListItem::data(role);
53 } 139 }
54 } 140 }
55}; 141};
diff --git a/src/core/loader/3dsx.cpp b/src/core/loader/3dsx.cpp
index 5fb3b9e2b..48a11ef81 100644
--- a/src/core/loader/3dsx.cpp
+++ b/src/core/loader/3dsx.cpp
@@ -303,4 +303,31 @@ ResultStatus AppLoader_THREEDSX::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& ro
303 return ResultStatus::ErrorNotUsed; 303 return ResultStatus::ErrorNotUsed;
304} 304}
305 305
306ResultStatus AppLoader_THREEDSX::ReadIcon(std::vector<u8>& buffer) {
307 if (!file.IsOpen())
308 return ResultStatus::Error;
309
310 // Reset read pointer in case this file has been read before.
311 file.Seek(0, SEEK_SET);
312
313 THREEDSX_Header hdr;
314 if (file.ReadBytes(&hdr, sizeof(THREEDSX_Header)) != sizeof(THREEDSX_Header))
315 return ResultStatus::Error;
316
317 if (hdr.header_size != sizeof(THREEDSX_Header))
318 return ResultStatus::Error;
319
320 // Check if the 3DSX has a SMDH...
321 if (hdr.smdh_offset != 0) {
322 file.Seek(hdr.smdh_offset, SEEK_SET);
323 buffer.resize(hdr.smdh_size);
324
325 if (file.ReadBytes(&buffer[0], hdr.smdh_size) != hdr.smdh_size)
326 return ResultStatus::Error;
327
328 return ResultStatus::Success;
329 }
330 return ResultStatus::ErrorNotUsed;
331}
332
306} // namespace Loader 333} // namespace Loader
diff --git a/src/core/loader/3dsx.h b/src/core/loader/3dsx.h
index 365ddb7a5..3ee686703 100644
--- a/src/core/loader/3dsx.h
+++ b/src/core/loader/3dsx.h
@@ -17,7 +17,7 @@ namespace Loader {
17/// Loads an 3DSX file 17/// Loads an 3DSX file
18class AppLoader_THREEDSX final : public AppLoader { 18class AppLoader_THREEDSX final : public AppLoader {
19public: 19public:
20 AppLoader_THREEDSX(FileUtil::IOFile&& file, std::string filename, const std::string& filepath) 20 AppLoader_THREEDSX(FileUtil::IOFile&& file, const std::string& filename, const std::string& filepath)
21 : AppLoader(std::move(file)), filename(std::move(filename)), filepath(filepath) {} 21 : AppLoader(std::move(file)), filename(std::move(filename)), filepath(filepath) {}
22 22
23 /** 23 /**
@@ -34,6 +34,13 @@ public:
34 ResultStatus Load() override; 34 ResultStatus Load() override;
35 35
36 /** 36 /**
37 * Get the icon (typically icon section) of the application
38 * @param buffer Reference to buffer to store data
39 * @return ResultStatus result of function
40 */
41 ResultStatus ReadIcon(std::vector<u8>& buffer) override;
42
43 /**
37 * Get the RomFS of the application 44 * Get the RomFS of the application
38 * @param romfs_file Reference to buffer to store data 45 * @param romfs_file Reference to buffer to store data
39 * @param offset Offset in the file to the RomFS 46 * @param offset Offset in the file to the RomFS
diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp
index 886501c41..0d4c1d351 100644
--- a/src/core/loader/loader.cpp
+++ b/src/core/loader/loader.cpp
@@ -90,6 +90,28 @@ const char* GetFileTypeString(FileType type) {
90 return "unknown"; 90 return "unknown";
91} 91}
92 92
93std::unique_ptr<AppLoader> GetLoader(FileUtil::IOFile&& file, FileType type,
94 const std::string& filename, const std::string& filepath) {
95 switch (type) {
96
97 // 3DSX file format.
98 case FileType::THREEDSX:
99 return std::make_unique<AppLoader_THREEDSX>(std::move(file), filename, filepath);
100
101 // Standard ELF file format.
102 case FileType::ELF:
103 return std::make_unique<AppLoader_ELF>(std::move(file), filename);
104
105 // NCCH/NCSD container formats.
106 case FileType::CXI:
107 case FileType::CCI:
108 return std::make_unique<AppLoader_NCCH>(std::move(file), filepath);
109
110 default:
111 return std::unique_ptr<AppLoader>();
112 }
113}
114
93ResultStatus LoadFile(const std::string& filename) { 115ResultStatus LoadFile(const std::string& filename) {
94 FileUtil::IOFile file(filename, "rb"); 116 FileUtil::IOFile file(filename, "rb");
95 if (!file.IsOpen()) { 117 if (!file.IsOpen()) {
@@ -111,15 +133,19 @@ ResultStatus LoadFile(const std::string& filename) {
111 133
112 LOG_INFO(Loader, "Loading file %s as %s...", filename.c_str(), GetFileTypeString(type)); 134 LOG_INFO(Loader, "Loading file %s as %s...", filename.c_str(), GetFileTypeString(type));
113 135
136 std::unique_ptr<AppLoader> app_loader = GetLoader(std::move(file), type, filename_filename, filename);
137
114 switch (type) { 138 switch (type) {
115 139
116 //3DSX file format... 140 // 3DSX file format...
141 // or NCCH/NCSD container formats...
117 case FileType::THREEDSX: 142 case FileType::THREEDSX:
143 case FileType::CXI:
144 case FileType::CCI:
118 { 145 {
119 AppLoader_THREEDSX app_loader(std::move(file), filename_filename, filename);
120 // Load application and RomFS 146 // Load application and RomFS
121 if (ResultStatus::Success == app_loader.Load()) { 147 if (ResultStatus::Success == app_loader->Load()) {
122 Service::FS::RegisterArchiveType(std::make_unique<FileSys::ArchiveFactory_RomFS>(app_loader), Service::FS::ArchiveIdCode::RomFS); 148 Service::FS::RegisterArchiveType(std::make_unique<FileSys::ArchiveFactory_RomFS>(*app_loader), Service::FS::ArchiveIdCode::RomFS);
123 return ResultStatus::Success; 149 return ResultStatus::Success;
124 } 150 }
125 break; 151 break;
@@ -127,21 +153,7 @@ ResultStatus LoadFile(const std::string& filename) {
127 153
128 // Standard ELF file format... 154 // Standard ELF file format...
129 case FileType::ELF: 155 case FileType::ELF:
130 return AppLoader_ELF(std::move(file), filename_filename).Load(); 156 return app_loader->Load();
131
132 // NCCH/NCSD container formats...
133 case FileType::CXI:
134 case FileType::CCI:
135 {
136 AppLoader_NCCH app_loader(std::move(file), filename);
137
138 // Load application and RomFS
139 ResultStatus result = app_loader.Load();
140 if (ResultStatus::Success == result) {
141 Service::FS::RegisterArchiveType(std::make_unique<FileSys::ArchiveFactory_RomFS>(app_loader), Service::FS::ArchiveIdCode::RomFS);
142 }
143 return result;
144 }
145 157
146 // CIA file format... 158 // CIA file format...
147 case FileType::CIA: 159 case FileType::CIA:
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h
index 84a4ce5fc..9d3e9ed3b 100644
--- a/src/core/loader/loader.h
+++ b/src/core/loader/loader.h
@@ -10,8 +10,10 @@
10#include <string> 10#include <string>
11#include <vector> 11#include <vector>
12 12
13#include "common/common_funcs.h"
13#include "common/common_types.h" 14#include "common/common_types.h"
14#include "common/file_util.h" 15#include "common/file_util.h"
16#include "common/swap.h"
15 17
16namespace Kernel { 18namespace Kernel {
17struct AddressMapping; 19struct AddressMapping;
@@ -78,6 +80,51 @@ constexpr u32 MakeMagic(char a, char b, char c, char d) {
78 return a | b << 8 | c << 16 | d << 24; 80 return a | b << 8 | c << 16 | d << 24;
79} 81}
80 82
83/// SMDH data structure that contains titles, icons etc. See https://www.3dbrew.org/wiki/SMDH
84struct SMDH {
85 u32_le magic;
86 u16_le version;
87 INSERT_PADDING_BYTES(2);
88
89 struct Title {
90 std::array<u16, 0x40> short_title;
91 std::array<u16, 0x80> long_title;
92 std::array<u16, 0x40> publisher;
93 };
94 std::array<Title, 16> titles;
95
96 std::array<u8, 16> ratings;
97 u32_le region_lockout;
98 u32_le match_maker_id;
99 u64_le match_maker_bit_id;
100 u32_le flags;
101 u16_le eula_version;
102 INSERT_PADDING_BYTES(2);
103 float_le banner_animation_frame;
104 u32_le cec_id;
105 INSERT_PADDING_BYTES(8);
106
107 std::array<u8, 0x480> small_icon;
108 std::array<u8, 0x1200> large_icon;
109
110 /// indicates the language used for each title entry
111 enum class TitleLanguage {
112 Japanese = 0,
113 English = 1,
114 French = 2,
115 German = 3,
116 Italian = 4,
117 Spanish = 5,
118 SimplifiedChinese = 6,
119 Korean= 7,
120 Dutch = 8,
121 Portuguese = 9,
122 Russian = 10,
123 TraditionalChinese = 11
124 };
125};
126static_assert(sizeof(SMDH) == 0x36C0, "SMDH structure size is wrong");
127
81/// Interface for loading an application 128/// Interface for loading an application
82class AppLoader : NonCopyable { 129class AppLoader : NonCopyable {
83public: 130public:
@@ -150,6 +197,16 @@ protected:
150extern const std::initializer_list<Kernel::AddressMapping> default_address_mappings; 197extern const std::initializer_list<Kernel::AddressMapping> default_address_mappings;
151 198
152/** 199/**
200 * Get a loader for a file with a specific type
201 * @param file The file to load
202 * @param type The type of the file
203 * @param filename the file name (without path)
204 * @param filepath the file full path (with name)
205 * @return std::unique_ptr<AppLoader> a pointer to a loader object; nullptr for unsupported type
206 */
207std::unique_ptr<AppLoader> GetLoader(FileUtil::IOFile&& file, FileType type, const std::string& filename, const std::string& filepath);
208
209/**
153 * Identifies and loads a bootable file 210 * Identifies and loads a bootable file
154 * @param filename String filename of bootable file 211 * @param filename String filename of bootable file
155 * @return ResultStatus result of function 212 * @return ResultStatus result of function
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp
index 066e91a9e..d362a4419 100644
--- a/src/core/loader/ncch.cpp
+++ b/src/core/loader/ncch.cpp
@@ -173,6 +173,10 @@ ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>&
173 if (!file.IsOpen()) 173 if (!file.IsOpen())
174 return ResultStatus::Error; 174 return ResultStatus::Error;
175 175
176 ResultStatus result = LoadExeFS();
177 if (result != ResultStatus::Success)
178 return result;
179
176 LOG_DEBUG(Loader, "%d sections:", kMaxSections); 180 LOG_DEBUG(Loader, "%d sections:", kMaxSections);
177 // Iterate through the ExeFs archive until we find a section with the specified name... 181 // Iterate through the ExeFs archive until we find a section with the specified name...
178 for (unsigned section_number = 0; section_number < kMaxSections; section_number++) { 182 for (unsigned section_number = 0; section_number < kMaxSections; section_number++) {
@@ -215,9 +219,9 @@ ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>&
215 return ResultStatus::ErrorNotUsed; 219 return ResultStatus::ErrorNotUsed;
216} 220}
217 221
218ResultStatus AppLoader_NCCH::Load() { 222ResultStatus AppLoader_NCCH::LoadExeFS() {
219 if (is_loaded) 223 if (is_exefs_loaded)
220 return ResultStatus::ErrorAlreadyLoaded; 224 return ResultStatus::Success;
221 225
222 if (!file.IsOpen()) 226 if (!file.IsOpen())
223 return ResultStatus::Error; 227 return ResultStatus::Error;
@@ -282,6 +286,18 @@ ResultStatus AppLoader_NCCH::Load() {
282 if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header)) 286 if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header))
283 return ResultStatus::Error; 287 return ResultStatus::Error;
284 288
289 is_exefs_loaded = true;
290 return ResultStatus::Success;
291}
292
293ResultStatus AppLoader_NCCH::Load() {
294 if (is_loaded)
295 return ResultStatus::ErrorAlreadyLoaded;
296
297 ResultStatus result = LoadExeFS();
298 if (result != ResultStatus::Success)
299 return result;
300
285 is_loaded = true; // Set state to loaded 301 is_loaded = true; // Set state to loaded
286 302
287 return LoadExec(); // Load the executable into memory for booting 303 return LoadExec(); // Load the executable into memory for booting
diff --git a/src/core/loader/ncch.h b/src/core/loader/ncch.h
index ca6772a78..fd852c3de 100644
--- a/src/core/loader/ncch.h
+++ b/src/core/loader/ncch.h
@@ -232,6 +232,13 @@ private:
232 */ 232 */
233 ResultStatus LoadExec(); 233 ResultStatus LoadExec();
234 234
235 /**
236 * Ensure ExeFS is loaded and ready for reading sections
237 * @return ResultStatus result of function
238 */
239 ResultStatus LoadExeFS();
240
241 bool is_exefs_loaded = false;
235 bool is_compressed = false; 242 bool is_compressed = false;
236 243
237 u32 entry_point = 0; 244 u32 entry_point = 0;