This commit is contained in:
Crunch (Chaz9) 2024-09-29 22:20:24 +01:00
parent 6803515773
commit c922d6559f

View file

@ -16,61 +16,75 @@
namespace Core { namespace Core {
NintendoSwitchLibrary::NintendoSwitchLibrary(Core::System& system) : system(system) {} /**
* NintendoSwitchLibrary class manages the operations related to installed games
* on the emulated Nintendo Switch, including listing games and launching them.
*/
class NintendoSwitchLibrary {
public:
explicit NintendoSwitchLibrary(Core::System& system) : system(system) {}
std::vector<NintendoSwitchLibrary::GameInfo> NintendoSwitchLibrary::GetInstalledGames() { struct GameInfo {
std::vector<GameInfo> games; u64 program_id;
const auto& cache = system.GetContentProvider().GetUserNANDCache(); std::string title_name;
std::string file_path;
};
for (const auto& nca : cache.GetAllEntries()) { [[nodiscard]] std::vector<GameInfo> GetInstalledGames() {
if (nca.second == FileSys::ContentRecordType::Program) { std::vector<GameInfo> games;
const auto program_id = nca.first; const auto& cache = system.GetContentProvider().GetUserNANDCache();
const auto title_name = GetGameName(program_id);
const auto file_path = cache.GetEntryUnparsed(program_id, FileSys::ContentRecordType::Program);
if (title_name.empty() || file_path.empty()) { for (const auto& [program_id, content_type] : cache.GetAllEntries()) {
continue; if (content_type == FileSys::ContentRecordType::Program) {
const auto title_name = GetGameName(program_id);
const auto file_path = cache.GetEntryUnparsed(program_id, FileSys::ContentRecordType::Program);
if (!title_name.empty() && !file_path.empty()) {
games.push_back({program_id, title_name, file_path});
}
} }
games.push_back({program_id, title_name, file_path});
} }
return games;
} }
return games; [[nodiscard]] std::string GetGameName(u64 program_id) {
} const auto& patch_manager = system.GetFileSystemController().GetPatchManager(program_id);
const auto metadata = patch_manager.GetControlMetadata();
std::string NintendoSwitchLibrary::GetGameName(u64 program_id) { if (metadata.first != nullptr) {
const auto& patch_manager = system.GetFileSystemController().GetPatchManager(program_id); return metadata.first->GetApplicationName();
const auto metadata = patch_manager.GetControlMetadata(); }
if (metadata.first != nullptr) { return "";
return metadata.first->GetApplicationName();
} }
return ""; [[nodiscard]] bool LaunchGame(u64 program_id) {
} const auto file_path = system.GetContentProvider().GetUserNANDCache().GetEntryUnparsed(program_id, FileSys::ContentRecordType::Program);
bool NintendoSwitchLibrary::LaunchGame(u64 program_id) { if (file_path.empty()) {
const auto file_path = system.GetContentProvider().GetUserNANDCache().GetEntryUnparsed(program_id, FileSys::ContentRecordType::Program); LOG_ERROR(Core, "Failed to launch game. File not found for program_id={:016X}", program_id);
return false;
}
if (file_path.empty()) { const auto loader = Loader::GetLoader(system, file_path);
LOG_ERROR(Core, "Failed to launch game. File not found for program_id={:016X}", program_id); if (!loader) {
return false; LOG_ERROR(Core, "Failed to create loader for game. program_id={:016X}", program_id);
return false;
}
const auto result = system.Load(*loader);
if (result != ResultStatus::Success) {
LOG_ERROR(Core, "Failed to load game. Error: {}, program_id={:016X}", result, program_id);
return false;
}
LOG_INFO(Core, "Successfully launched game. program_id={:016X}", program_id);
return true;
} }
const auto loader = Loader::GetLoader(system, file_path); private:
if (!loader) { const Core::System& system;
LOG_ERROR(Core, "Failed to create loader for game. program_id={:016X}", program_id); };
return false;
}
const auto result = system.Load(*loader);
if (result != ResultStatus::Success) {
LOG_ERROR(Core, "Failed to load game. Error: {}, program_id={:016X}", result, program_id);
return false;
}
return true;
}
} // namespace Core } // namespace Core