patch_manager.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <array>
  5. #include <cstddef>
  6. #include <cstring>
  7. #include "common/hex_util.h"
  8. #include "common/logging/log.h"
  9. #include "common/settings.h"
  10. #ifndef _WIN32
  11. #include "common/string_util.h"
  12. #endif
  13. #include "core/core.h"
  14. #include "core/file_sys/common_funcs.h"
  15. #include "core/file_sys/content_archive.h"
  16. #include "core/file_sys/control_metadata.h"
  17. #include "core/file_sys/ips_layer.h"
  18. #include "core/file_sys/patch_manager.h"
  19. #include "core/file_sys/registered_cache.h"
  20. #include "core/file_sys/romfs.h"
  21. #include "core/file_sys/vfs_cached.h"
  22. #include "core/file_sys/vfs_layered.h"
  23. #include "core/file_sys/vfs_vector.h"
  24. #include "core/hle/service/filesystem/filesystem.h"
  25. #include "core/loader/loader.h"
  26. #include "core/loader/nso.h"
  27. #include "core/memory/cheat_engine.h"
  28. namespace FileSys {
  29. namespace {
  30. constexpr u32 SINGLE_BYTE_MODULUS = 0x100;
  31. constexpr std::array<const char*, 14> EXEFS_FILE_NAMES{
  32. "main", "main.npdm", "rtld", "sdk", "subsdk0", "subsdk1", "subsdk2",
  33. "subsdk3", "subsdk4", "subsdk5", "subsdk6", "subsdk7", "subsdk8", "subsdk9",
  34. };
  35. enum class TitleVersionFormat : u8 {
  36. ThreeElements, ///< vX.Y.Z
  37. FourElements, ///< vX.Y.Z.W
  38. };
  39. std::string FormatTitleVersion(u32 version,
  40. TitleVersionFormat format = TitleVersionFormat::ThreeElements) {
  41. std::array<u8, sizeof(u32)> bytes{};
  42. bytes[0] = static_cast<u8>(version % SINGLE_BYTE_MODULUS);
  43. for (std::size_t i = 1; i < bytes.size(); ++i) {
  44. version /= SINGLE_BYTE_MODULUS;
  45. bytes[i] = static_cast<u8>(version % SINGLE_BYTE_MODULUS);
  46. }
  47. if (format == TitleVersionFormat::FourElements) {
  48. return fmt::format("v{}.{}.{}.{}", bytes[3], bytes[2], bytes[1], bytes[0]);
  49. }
  50. return fmt::format("v{}.{}.{}", bytes[3], bytes[2], bytes[1]);
  51. }
  52. // Returns a directory with name matching name case-insensitive. Returns nullptr if directory
  53. // doesn't have a directory with name.
  54. VirtualDir FindSubdirectoryCaseless(const VirtualDir dir, std::string_view name) {
  55. #ifdef _WIN32
  56. return dir->GetSubdirectory(name);
  57. #else
  58. const auto subdirs = dir->GetSubdirectories();
  59. for (const auto& subdir : subdirs) {
  60. std::string dir_name = Common::ToLower(subdir->GetName());
  61. if (dir_name == name) {
  62. return subdir;
  63. }
  64. }
  65. return nullptr;
  66. #endif
  67. }
  68. std::optional<std::vector<Core::Memory::CheatEntry>> ReadCheatFileFromFolder(
  69. u64 title_id, const PatchManager::BuildID& build_id_, const VirtualDir& base_path, bool upper) {
  70. const auto build_id_raw = Common::HexToString(build_id_, upper);
  71. const auto build_id = build_id_raw.substr(0, sizeof(u64) * 2);
  72. const auto file = base_path->GetFile(fmt::format("{}.txt", build_id));
  73. if (file == nullptr) {
  74. LOG_INFO(Common_Filesystem, "No cheats file found for title_id={:016X}, build_id={}",
  75. title_id, build_id);
  76. return std::nullopt;
  77. }
  78. std::vector<u8> data(file->GetSize());
  79. if (file->Read(data.data(), data.size()) != data.size()) {
  80. LOG_INFO(Common_Filesystem, "Failed to read cheats file for title_id={:016X}, build_id={}",
  81. title_id, build_id);
  82. return std::nullopt;
  83. }
  84. const Core::Memory::TextCheatParser parser;
  85. return parser.Parse(std::string_view(reinterpret_cast<const char*>(data.data()), data.size()));
  86. }
  87. void AppendCommaIfNotEmpty(std::string& to, std::string_view with) {
  88. if (to.empty()) {
  89. to += with;
  90. } else {
  91. to += ", ";
  92. to += with;
  93. }
  94. }
  95. bool IsDirValidAndNonEmpty(const VirtualDir& dir) {
  96. return dir != nullptr && (!dir->GetFiles().empty() || !dir->GetSubdirectories().empty());
  97. }
  98. } // Anonymous namespace
  99. PatchManager::PatchManager(u64 title_id_,
  100. const Service::FileSystem::FileSystemController& fs_controller_,
  101. const ContentProvider& content_provider_)
  102. : title_id{title_id_}, fs_controller{fs_controller_}, content_provider{content_provider_} {}
  103. PatchManager::~PatchManager() = default;
  104. u64 PatchManager::GetTitleID() const {
  105. return title_id;
  106. }
  107. VirtualDir PatchManager::PatchExeFS(VirtualDir exefs) const {
  108. LOG_INFO(Loader, "Patching ExeFS for title_id={:016X}", title_id);
  109. if (exefs == nullptr)
  110. return exefs;
  111. const auto& disabled = Settings::values.disabled_addons[title_id];
  112. const auto update_disabled =
  113. std::find(disabled.cbegin(), disabled.cend(), "Update") != disabled.cend();
  114. // Game Updates
  115. const auto update_tid = GetUpdateTitleID(title_id);
  116. const auto update = content_provider.GetEntry(update_tid, ContentRecordType::Program);
  117. if (!update_disabled && update != nullptr && update->GetExeFS() != nullptr &&
  118. update->GetStatus() == Loader::ResultStatus::ErrorMissingBKTRBaseRomFS) {
  119. LOG_INFO(Loader, " ExeFS: Update ({}) applied successfully",
  120. FormatTitleVersion(content_provider.GetEntryVersion(update_tid).value_or(0)));
  121. exefs = update->GetExeFS();
  122. }
  123. // LayeredExeFS
  124. const auto load_dir = fs_controller.GetModificationLoadRoot(title_id);
  125. const auto sdmc_load_dir = fs_controller.GetSDMCModificationLoadRoot(title_id);
  126. std::vector<VirtualDir> patch_dirs = {sdmc_load_dir};
  127. if (load_dir != nullptr && load_dir->GetSize() > 0) {
  128. const auto load_patch_dirs = load_dir->GetSubdirectories();
  129. patch_dirs.insert(patch_dirs.end(), load_patch_dirs.begin(), load_patch_dirs.end());
  130. }
  131. std::sort(patch_dirs.begin(), patch_dirs.end(),
  132. [](const VirtualDir& l, const VirtualDir& r) { return l->GetName() < r->GetName(); });
  133. std::vector<VirtualDir> layers;
  134. layers.reserve(patch_dirs.size() + 1);
  135. for (const auto& subdir : patch_dirs) {
  136. if (std::find(disabled.begin(), disabled.end(), subdir->GetName()) != disabled.end())
  137. continue;
  138. auto exefs_dir = FindSubdirectoryCaseless(subdir, "exefs");
  139. if (exefs_dir != nullptr)
  140. layers.push_back(std::move(exefs_dir));
  141. }
  142. layers.push_back(exefs);
  143. auto layered = LayeredVfsDirectory::MakeLayeredDirectory(std::move(layers));
  144. if (layered != nullptr) {
  145. LOG_INFO(Loader, " ExeFS: LayeredExeFS patches applied successfully");
  146. exefs = std::move(layered);
  147. }
  148. if (Settings::values.dump_exefs) {
  149. LOG_INFO(Loader, "Dumping ExeFS for title_id={:016X}", title_id);
  150. const auto dump_dir = fs_controller.GetModificationDumpRoot(title_id);
  151. if (dump_dir != nullptr) {
  152. const auto exefs_dir = GetOrCreateDirectoryRelative(dump_dir, "/exefs");
  153. VfsRawCopyD(exefs, exefs_dir);
  154. }
  155. }
  156. return exefs;
  157. }
  158. std::vector<VirtualFile> PatchManager::CollectPatches(const std::vector<VirtualDir>& patch_dirs,
  159. const std::string& build_id) const {
  160. const auto& disabled = Settings::values.disabled_addons[title_id];
  161. const auto nso_build_id = fmt::format("{:0<64}", build_id);
  162. std::vector<VirtualFile> out;
  163. out.reserve(patch_dirs.size());
  164. for (const auto& subdir : patch_dirs) {
  165. if (std::find(disabled.cbegin(), disabled.cend(), subdir->GetName()) != disabled.cend())
  166. continue;
  167. auto exefs_dir = FindSubdirectoryCaseless(subdir, "exefs");
  168. if (exefs_dir != nullptr) {
  169. for (const auto& file : exefs_dir->GetFiles()) {
  170. if (file->GetExtension() == "ips") {
  171. auto name = file->GetName();
  172. const auto this_build_id =
  173. fmt::format("{:0<64}", name.substr(0, name.find('.')));
  174. if (nso_build_id == this_build_id)
  175. out.push_back(file);
  176. } else if (file->GetExtension() == "pchtxt") {
  177. IPSwitchCompiler compiler{file};
  178. if (!compiler.IsValid())
  179. continue;
  180. const auto this_build_id = Common::HexToString(compiler.GetBuildID());
  181. if (nso_build_id == this_build_id)
  182. out.push_back(file);
  183. }
  184. }
  185. }
  186. }
  187. return out;
  188. }
  189. std::vector<u8> PatchManager::PatchNSO(const std::vector<u8>& nso, const std::string& name) const {
  190. if (nso.size() < sizeof(Loader::NSOHeader)) {
  191. return nso;
  192. }
  193. Loader::NSOHeader header;
  194. std::memcpy(&header, nso.data(), sizeof(header));
  195. if (header.magic != Common::MakeMagic('N', 'S', 'O', '0')) {
  196. return nso;
  197. }
  198. const auto build_id_raw = Common::HexToString(header.build_id);
  199. const auto build_id = build_id_raw.substr(0, build_id_raw.find_last_not_of('0') + 1);
  200. if (Settings::values.dump_nso) {
  201. LOG_INFO(Loader, "Dumping NSO for name={}, build_id={}, title_id={:016X}", name, build_id,
  202. title_id);
  203. const auto dump_dir = fs_controller.GetModificationDumpRoot(title_id);
  204. if (dump_dir != nullptr) {
  205. const auto nso_dir = GetOrCreateDirectoryRelative(dump_dir, "/nso");
  206. const auto file = nso_dir->CreateFile(fmt::format("{}-{}.nso", name, build_id));
  207. file->Resize(nso.size());
  208. file->WriteBytes(nso);
  209. }
  210. }
  211. LOG_INFO(Loader, "Patching NSO for name={}, build_id={}", name, build_id);
  212. const auto load_dir = fs_controller.GetModificationLoadRoot(title_id);
  213. if (load_dir == nullptr) {
  214. LOG_ERROR(Loader, "Cannot load mods for invalid title_id={:016X}", title_id);
  215. return nso;
  216. }
  217. auto patch_dirs = load_dir->GetSubdirectories();
  218. std::sort(patch_dirs.begin(), patch_dirs.end(),
  219. [](const VirtualDir& l, const VirtualDir& r) { return l->GetName() < r->GetName(); });
  220. const auto patches = CollectPatches(patch_dirs, build_id);
  221. auto out = nso;
  222. for (const auto& patch_file : patches) {
  223. if (patch_file->GetExtension() == "ips") {
  224. LOG_INFO(Loader, " - Applying IPS patch from mod \"{}\"",
  225. patch_file->GetContainingDirectory()->GetParentDirectory()->GetName());
  226. const auto patched = PatchIPS(std::make_shared<VectorVfsFile>(out), patch_file);
  227. if (patched != nullptr)
  228. out = patched->ReadAllBytes();
  229. } else if (patch_file->GetExtension() == "pchtxt") {
  230. LOG_INFO(Loader, " - Applying IPSwitch patch from mod \"{}\"",
  231. patch_file->GetContainingDirectory()->GetParentDirectory()->GetName());
  232. const IPSwitchCompiler compiler{patch_file};
  233. const auto patched = compiler.Apply(std::make_shared<VectorVfsFile>(out));
  234. if (patched != nullptr)
  235. out = patched->ReadAllBytes();
  236. }
  237. }
  238. if (out.size() < sizeof(Loader::NSOHeader)) {
  239. return nso;
  240. }
  241. std::memcpy(out.data(), &header, sizeof(header));
  242. return out;
  243. }
  244. bool PatchManager::HasNSOPatch(const BuildID& build_id_) const {
  245. const auto build_id_raw = Common::HexToString(build_id_);
  246. const auto build_id = build_id_raw.substr(0, build_id_raw.find_last_not_of('0') + 1);
  247. LOG_INFO(Loader, "Querying NSO patch existence for build_id={}", build_id);
  248. const auto load_dir = fs_controller.GetModificationLoadRoot(title_id);
  249. if (load_dir == nullptr) {
  250. LOG_ERROR(Loader, "Cannot load mods for invalid title_id={:016X}", title_id);
  251. return false;
  252. }
  253. auto patch_dirs = load_dir->GetSubdirectories();
  254. std::sort(patch_dirs.begin(), patch_dirs.end(),
  255. [](const VirtualDir& l, const VirtualDir& r) { return l->GetName() < r->GetName(); });
  256. return !CollectPatches(patch_dirs, build_id).empty();
  257. }
  258. std::vector<Core::Memory::CheatEntry> PatchManager::CreateCheatList(
  259. const BuildID& build_id_) const {
  260. const auto load_dir = fs_controller.GetModificationLoadRoot(title_id);
  261. if (load_dir == nullptr) {
  262. LOG_ERROR(Loader, "Cannot load mods for invalid title_id={:016X}", title_id);
  263. return {};
  264. }
  265. const auto& disabled = Settings::values.disabled_addons[title_id];
  266. auto patch_dirs = load_dir->GetSubdirectories();
  267. std::sort(patch_dirs.begin(), patch_dirs.end(),
  268. [](const VirtualDir& l, const VirtualDir& r) { return l->GetName() < r->GetName(); });
  269. std::vector<Core::Memory::CheatEntry> out;
  270. for (const auto& subdir : patch_dirs) {
  271. if (std::find(disabled.cbegin(), disabled.cend(), subdir->GetName()) != disabled.cend()) {
  272. continue;
  273. }
  274. auto cheats_dir = FindSubdirectoryCaseless(subdir, "cheats");
  275. if (cheats_dir != nullptr) {
  276. if (const auto res = ReadCheatFileFromFolder(title_id, build_id_, cheats_dir, true)) {
  277. std::copy(res->begin(), res->end(), std::back_inserter(out));
  278. continue;
  279. }
  280. if (const auto res = ReadCheatFileFromFolder(title_id, build_id_, cheats_dir, false)) {
  281. std::copy(res->begin(), res->end(), std::back_inserter(out));
  282. }
  283. }
  284. }
  285. return out;
  286. }
  287. static void ApplyLayeredFS(VirtualFile& romfs, u64 title_id, ContentRecordType type,
  288. const Service::FileSystem::FileSystemController& fs_controller) {
  289. const auto load_dir = fs_controller.GetModificationLoadRoot(title_id);
  290. const auto sdmc_load_dir = fs_controller.GetSDMCModificationLoadRoot(title_id);
  291. if ((type != ContentRecordType::Program && type != ContentRecordType::Data) ||
  292. ((load_dir == nullptr || load_dir->GetSize() <= 0) &&
  293. (sdmc_load_dir == nullptr || sdmc_load_dir->GetSize() <= 0))) {
  294. return;
  295. }
  296. auto extracted = ExtractRomFS(romfs);
  297. if (extracted == nullptr) {
  298. return;
  299. }
  300. const auto& disabled = Settings::values.disabled_addons[title_id];
  301. std::vector<VirtualDir> patch_dirs = load_dir->GetSubdirectories();
  302. if (std::find(disabled.cbegin(), disabled.cend(), "SDMC") == disabled.cend()) {
  303. patch_dirs.push_back(sdmc_load_dir);
  304. }
  305. std::sort(patch_dirs.begin(), patch_dirs.end(),
  306. [](const VirtualDir& l, const VirtualDir& r) { return l->GetName() < r->GetName(); });
  307. std::vector<VirtualDir> layers;
  308. std::vector<VirtualDir> layers_ext;
  309. layers.reserve(patch_dirs.size() + 1);
  310. layers_ext.reserve(patch_dirs.size() + 1);
  311. for (const auto& subdir : patch_dirs) {
  312. if (std::find(disabled.cbegin(), disabled.cend(), subdir->GetName()) != disabled.cend()) {
  313. continue;
  314. }
  315. auto romfs_dir = FindSubdirectoryCaseless(subdir, "romfs");
  316. if (romfs_dir != nullptr)
  317. layers.push_back(std::make_shared<CachedVfsDirectory>(romfs_dir));
  318. auto ext_dir = FindSubdirectoryCaseless(subdir, "romfs_ext");
  319. if (ext_dir != nullptr)
  320. layers_ext.push_back(std::make_shared<CachedVfsDirectory>(ext_dir));
  321. }
  322. // When there are no layers to apply, return early as there is no need to rebuild the RomFS
  323. if (layers.empty() && layers_ext.empty()) {
  324. return;
  325. }
  326. layers.push_back(std::move(extracted));
  327. auto layered = LayeredVfsDirectory::MakeLayeredDirectory(std::move(layers));
  328. if (layered == nullptr) {
  329. return;
  330. }
  331. auto layered_ext = LayeredVfsDirectory::MakeLayeredDirectory(std::move(layers_ext));
  332. auto packed = CreateRomFS(std::move(layered), std::move(layered_ext));
  333. if (packed == nullptr) {
  334. return;
  335. }
  336. LOG_INFO(Loader, " RomFS: LayeredFS patches applied successfully");
  337. romfs = std::move(packed);
  338. }
  339. VirtualFile PatchManager::PatchRomFS(VirtualFile romfs, u64 ivfc_offset, ContentRecordType type,
  340. VirtualFile update_raw, bool apply_layeredfs) const {
  341. const auto log_string = fmt::format("Patching RomFS for title_id={:016X}, type={:02X}",
  342. title_id, static_cast<u8>(type));
  343. if (type == ContentRecordType::Program || type == ContentRecordType::Data) {
  344. LOG_INFO(Loader, "{}", log_string);
  345. } else {
  346. LOG_DEBUG(Loader, "{}", log_string);
  347. }
  348. if (romfs == nullptr) {
  349. return romfs;
  350. }
  351. // Game Updates
  352. const auto update_tid = GetUpdateTitleID(title_id);
  353. const auto update = content_provider.GetEntryRaw(update_tid, type);
  354. const auto& disabled = Settings::values.disabled_addons[title_id];
  355. const auto update_disabled =
  356. std::find(disabled.cbegin(), disabled.cend(), "Update") != disabled.cend();
  357. if (!update_disabled && update != nullptr) {
  358. const auto new_nca = std::make_shared<NCA>(update, romfs, ivfc_offset);
  359. if (new_nca->GetStatus() == Loader::ResultStatus::Success &&
  360. new_nca->GetRomFS() != nullptr) {
  361. LOG_INFO(Loader, " RomFS: Update ({}) applied successfully",
  362. FormatTitleVersion(content_provider.GetEntryVersion(update_tid).value_or(0)));
  363. romfs = new_nca->GetRomFS();
  364. }
  365. } else if (!update_disabled && update_raw != nullptr) {
  366. const auto new_nca = std::make_shared<NCA>(update_raw, romfs, ivfc_offset);
  367. if (new_nca->GetStatus() == Loader::ResultStatus::Success &&
  368. new_nca->GetRomFS() != nullptr) {
  369. LOG_INFO(Loader, " RomFS: Update (PACKED) applied successfully");
  370. romfs = new_nca->GetRomFS();
  371. }
  372. }
  373. // LayeredFS
  374. if (apply_layeredfs) {
  375. ApplyLayeredFS(romfs, title_id, type, fs_controller);
  376. }
  377. return romfs;
  378. }
  379. PatchManager::PatchVersionNames PatchManager::GetPatchVersionNames(VirtualFile update_raw) const {
  380. if (title_id == 0) {
  381. return {};
  382. }
  383. std::map<std::string, std::string, std::less<>> out;
  384. const auto& disabled = Settings::values.disabled_addons[title_id];
  385. // Game Updates
  386. const auto update_tid = GetUpdateTitleID(title_id);
  387. PatchManager update{update_tid, fs_controller, content_provider};
  388. const auto metadata = update.GetControlMetadata();
  389. const auto& nacp = metadata.first;
  390. const auto update_disabled =
  391. std::find(disabled.cbegin(), disabled.cend(), "Update") != disabled.cend();
  392. const auto update_label = update_disabled ? "[D] Update" : "Update";
  393. if (nacp != nullptr) {
  394. out.insert_or_assign(update_label, nacp->GetVersionString());
  395. } else {
  396. if (content_provider.HasEntry(update_tid, ContentRecordType::Program)) {
  397. const auto meta_ver = content_provider.GetEntryVersion(update_tid);
  398. if (meta_ver.value_or(0) == 0) {
  399. out.insert_or_assign(update_label, "");
  400. } else {
  401. out.insert_or_assign(update_label, FormatTitleVersion(*meta_ver));
  402. }
  403. } else if (update_raw != nullptr) {
  404. out.insert_or_assign(update_label, "PACKED");
  405. }
  406. }
  407. // General Mods (LayeredFS and IPS)
  408. const auto mod_dir = fs_controller.GetModificationLoadRoot(title_id);
  409. if (mod_dir != nullptr && mod_dir->GetSize() > 0) {
  410. for (const auto& mod : mod_dir->GetSubdirectories()) {
  411. std::string types;
  412. const auto exefs_dir = FindSubdirectoryCaseless(mod, "exefs");
  413. if (IsDirValidAndNonEmpty(exefs_dir)) {
  414. bool ips = false;
  415. bool ipswitch = false;
  416. bool layeredfs = false;
  417. for (const auto& file : exefs_dir->GetFiles()) {
  418. if (file->GetExtension() == "ips") {
  419. ips = true;
  420. } else if (file->GetExtension() == "pchtxt") {
  421. ipswitch = true;
  422. } else if (std::find(EXEFS_FILE_NAMES.begin(), EXEFS_FILE_NAMES.end(),
  423. file->GetName()) != EXEFS_FILE_NAMES.end()) {
  424. layeredfs = true;
  425. }
  426. }
  427. if (ips)
  428. AppendCommaIfNotEmpty(types, "IPS");
  429. if (ipswitch)
  430. AppendCommaIfNotEmpty(types, "IPSwitch");
  431. if (layeredfs)
  432. AppendCommaIfNotEmpty(types, "LayeredExeFS");
  433. }
  434. if (IsDirValidAndNonEmpty(FindSubdirectoryCaseless(mod, "romfs")))
  435. AppendCommaIfNotEmpty(types, "LayeredFS");
  436. if (IsDirValidAndNonEmpty(FindSubdirectoryCaseless(mod, "cheats")))
  437. AppendCommaIfNotEmpty(types, "Cheats");
  438. if (types.empty())
  439. continue;
  440. const auto mod_disabled =
  441. std::find(disabled.begin(), disabled.end(), mod->GetName()) != disabled.end();
  442. out.insert_or_assign(mod_disabled ? "[D] " + mod->GetName() : mod->GetName(), types);
  443. }
  444. }
  445. // SDMC mod directory (RomFS LayeredFS)
  446. const auto sdmc_mod_dir = fs_controller.GetSDMCModificationLoadRoot(title_id);
  447. if (sdmc_mod_dir != nullptr && sdmc_mod_dir->GetSize() > 0) {
  448. std::string types;
  449. if (IsDirValidAndNonEmpty(FindSubdirectoryCaseless(sdmc_mod_dir, "exefs"))) {
  450. AppendCommaIfNotEmpty(types, "LayeredExeFS");
  451. }
  452. if (IsDirValidAndNonEmpty(FindSubdirectoryCaseless(sdmc_mod_dir, "romfs"))) {
  453. AppendCommaIfNotEmpty(types, "LayeredFS");
  454. }
  455. if (!types.empty()) {
  456. const auto mod_disabled =
  457. std::find(disabled.begin(), disabled.end(), "SDMC") != disabled.end();
  458. out.insert_or_assign(mod_disabled ? "[D] SDMC" : "SDMC", types);
  459. }
  460. }
  461. // DLC
  462. const auto dlc_entries =
  463. content_provider.ListEntriesFilter(TitleType::AOC, ContentRecordType::Data);
  464. std::vector<ContentProviderEntry> dlc_match;
  465. dlc_match.reserve(dlc_entries.size());
  466. std::copy_if(dlc_entries.begin(), dlc_entries.end(), std::back_inserter(dlc_match),
  467. [this](const ContentProviderEntry& entry) {
  468. return GetBaseTitleID(entry.title_id) == title_id &&
  469. content_provider.GetEntry(entry)->GetStatus() ==
  470. Loader::ResultStatus::Success;
  471. });
  472. if (!dlc_match.empty()) {
  473. // Ensure sorted so DLC IDs show in order.
  474. std::sort(dlc_match.begin(), dlc_match.end());
  475. std::string list;
  476. for (size_t i = 0; i < dlc_match.size() - 1; ++i)
  477. list += fmt::format("{}, ", dlc_match[i].title_id & 0x7FF);
  478. list += fmt::format("{}", dlc_match.back().title_id & 0x7FF);
  479. const auto dlc_disabled =
  480. std::find(disabled.begin(), disabled.end(), "DLC") != disabled.end();
  481. out.insert_or_assign(dlc_disabled ? "[D] DLC" : "DLC", std::move(list));
  482. }
  483. return out;
  484. }
  485. std::optional<u32> PatchManager::GetGameVersion() const {
  486. const auto update_tid = GetUpdateTitleID(title_id);
  487. if (content_provider.HasEntry(update_tid, ContentRecordType::Program)) {
  488. return content_provider.GetEntryVersion(update_tid);
  489. }
  490. return content_provider.GetEntryVersion(title_id);
  491. }
  492. PatchManager::Metadata PatchManager::GetControlMetadata() const {
  493. const auto base_control_nca = content_provider.GetEntry(title_id, ContentRecordType::Control);
  494. if (base_control_nca == nullptr) {
  495. return {};
  496. }
  497. return ParseControlNCA(*base_control_nca);
  498. }
  499. PatchManager::Metadata PatchManager::ParseControlNCA(const NCA& nca) const {
  500. const auto base_romfs = nca.GetRomFS();
  501. if (base_romfs == nullptr) {
  502. return {};
  503. }
  504. const auto romfs = PatchRomFS(base_romfs, nca.GetBaseIVFCOffset(), ContentRecordType::Control);
  505. if (romfs == nullptr) {
  506. return {};
  507. }
  508. const auto extracted = ExtractRomFS(romfs);
  509. if (extracted == nullptr) {
  510. return {};
  511. }
  512. auto nacp_file = extracted->GetFile("control.nacp");
  513. if (nacp_file == nullptr) {
  514. nacp_file = extracted->GetFile("Control.nacp");
  515. }
  516. auto nacp = nacp_file == nullptr ? nullptr : std::make_unique<NACP>(nacp_file);
  517. VirtualFile icon_file;
  518. for (const auto& language : FileSys::LANGUAGE_NAMES) {
  519. icon_file = extracted->GetFile(std::string("icon_").append(language).append(".dat"));
  520. if (icon_file != nullptr) {
  521. break;
  522. }
  523. }
  524. return {std::move(nacp), icon_file};
  525. }
  526. } // namespace FileSys