diff --git a/include/ui/netInstPage.hpp b/include/ui/netInstPage.hpp index 0354737..8d3f049 100644 --- a/include/ui/netInstPage.hpp +++ b/include/ui/netInstPage.hpp @@ -22,6 +22,7 @@ namespace inst::ui { std::vector selectedUrls; std::vector alternativeNames; std::vector> installedTitles; + std::vector menuIndices; TextBlock::Ref butText; Rectangle::Ref topRect; Rectangle::Ref infoRect; diff --git a/include/ui/sdInstPage.hpp b/include/ui/sdInstPage.hpp index 020c402..35dc46b 100644 --- a/include/ui/sdInstPage.hpp +++ b/include/ui/sdInstPage.hpp @@ -24,6 +24,7 @@ namespace inst::ui { std::vector ourFiles; std::vector selectedTitles; std::vector> installedTitles; + std::vector menuIndices; std::filesystem::path currentDir; TextBlock::Ref butText; Rectangle::Ref topRect; diff --git a/include/ui/usbHDDInstPage.hpp b/include/ui/usbHDDInstPage.hpp index a0bbb06..4c8cd89 100644 --- a/include/ui/usbHDDInstPage.hpp +++ b/include/ui/usbHDDInstPage.hpp @@ -24,6 +24,7 @@ namespace inst::ui { std::vector ourFiles; std::vector selectedTitles; std::vector> installedTitles; + std::vector menuIndices; std::filesystem::path currentDir; TextBlock::Ref butText; Rectangle::Ref topRect; diff --git a/include/util/util.hpp b/include/util/util.hpp index b6d6923..8ab769b 100644 --- a/include/util/util.hpp +++ b/include/util/util.hpp @@ -25,5 +25,6 @@ namespace inst::util { void lightningStop(); std::string* getBatteryCharge(); std::vector> listInstalledTitles(); + bool isTitleInstalled(std::string filename, const std::vector> &installedTitles); std::vector checkForAppUpdate(); } \ No newline at end of file diff --git a/source/ui/netInstPage.cpp b/source/ui/netInstPage.cpp index a4e0dfd..7ee987e 100644 --- a/source/ui/netInstPage.cpp +++ b/source/ui/netInstPage.cpp @@ -65,46 +65,32 @@ namespace inst::ui { if (clearItems) this->selectedUrls = {}; if (clearItems) this->alternativeNames = {}; this->menu->ClearItems(); + this->menuIndices = {}; - const std::regex idRegex(".*\\[([0-9a-fA-F]+)]\\[v(\\d+)].*"); + for (long unsigned int i = 0; i < this->ourUrls.size(); i++) { + auto& url = this->ourUrls[i]; - for (auto& url: this->ourUrls) { std::string formattedURL = inst::util::formatUrlString(url); - std::smatch match; - if (hideInstalled and std::regex_match(formattedURL, match, idRegex)) { - u64 id = stol(match[1], nullptr, 16); - u32 version = stoi(match[2]); - bool installed = false; - for (const auto &title: installedTitles) - if (id == title.first and version <= title.second) { - installed = true; - break; - } - if (installed) - continue; - } + if (hideInstalled and inst::util::isTitleInstalled(formattedURL, installedTitles)) + continue; std::string itm = inst::util::shortenString(formattedURL, 56, true); auto ourEntry = pu::ui::elm::MenuItem::New(itm); ourEntry->SetColor(COLOR("#FFFFFFFF")); ourEntry->SetIcon("romfs:/images/icons/checkbox-blank-outline.png"); - for (long unsigned int i = 0; i < this->selectedUrls.size(); i++) { - if (this->selectedUrls[i] == url) { + for (long unsigned int j = 0; j < this->selectedUrls.size(); j++) { + if (this->selectedUrls[j] == url) { ourEntry->SetIcon("romfs:/images/icons/check-box-outline.png"); } } this->menu->AddItem(ourEntry); + this->menuIndices.push_back(i); } } void netInstPage::selectTitle(int selectedIndex) { - long unsigned int urlIndex = 0; - for (long unsigned int i = 0; i < this->ourUrls.size(); i++) - if (inst::util::shortenString(inst::util::formatUrlString(this->ourUrls[i]), 56, true) == this->menu->GetItems()[selectedIndex]->GetName()) { - urlIndex = i; - break; - } + long unsigned int urlIndex = this->menuIndices[selectedIndex]; if (this->menu->GetItems()[selectedIndex]->GetIcon() == "romfs:/images/icons/check-box-outline.png") { for (long unsigned int i = 0; i < this->selectedUrls.size(); i++) { @@ -164,7 +150,7 @@ namespace inst::ui { netConnected = true; this->pageInfoText->SetText("inst.net.top_info"_lang); this->butText->SetText(hideInstalled ? "inst.net.buttons1_show"_lang : "inst.net.buttons1"_lang); - installedTitles = installedTitles = inst::util::listInstalledTitles(); + installedTitles = inst::util::listInstalledTitles(); this->drawMenuItems(true); this->menu->SetSelectedIndex(0); mainApp->CallForRender(); diff --git a/source/ui/sdInstPage.cpp b/source/ui/sdInstPage.cpp index 7365db6..4613c78 100644 --- a/source/ui/sdInstPage.cpp +++ b/source/ui/sdInstPage.cpp @@ -63,8 +63,7 @@ namespace inst::ui { if (ourPath == "sdmc:") this->currentDir = std::filesystem::path(ourPath.string() + "/"); else this->currentDir = ourPath; this->menu->ClearItems(); - - const std::regex idRegex(".*\\[([0-9a-fA-F]+)]\\[v(\\d+)].*"); + this->menuIndices = {}; try { this->ourDirectories = util::getDirsAtPath(this->currentDir); @@ -88,32 +87,24 @@ namespace inst::ui { ourEntry->SetIcon("romfs:/images/icons/folder.png"); this->menu->AddItem(ourEntry); } - for (auto& file: this->ourFiles) { + for (long unsigned int i = 0; i < this->ourFiles.size(); i++) { + auto& file = this->ourFiles[i]; + std::string itm = file.filename().string(); - std::smatch match; - if (hideInstalled and std::regex_match(itm, match, idRegex)) { - u64 id = stol(match[1], nullptr, 16); - u32 version = stoi(match[2]); - bool installed = false; - for (const auto &title: installedTitles) - if (id == title.first and version <= title.second) { - installed = true; - break; - } - if (installed) - continue; - } + if (hideInstalled and inst::util::isTitleInstalled(itm, installedTitles)) + continue; auto ourEntry = pu::ui::elm::MenuItem::New(itm); ourEntry->SetColor(COLOR("#FFFFFFFF")); ourEntry->SetIcon("romfs:/images/icons/checkbox-blank-outline.png"); - for (long unsigned int i = 0; i < this->selectedTitles.size(); i++) { - if (this->selectedTitles[i] == file) { + for (long unsigned int j = 0; j < this->selectedTitles.size(); j++) { + if (this->selectedTitles[j] == file) { ourEntry->SetIcon("romfs:/images/icons/check-box-outline.png"); } } this->menu->AddItem(ourEntry); + this->menuIndices.push_back(i); } } @@ -147,12 +138,7 @@ namespace inst::ui { } void sdInstPage::selectNsp(int selectedIndex) { - long unsigned int nspIndex = 0; - for (long unsigned int i = 0; i < this->ourFiles.size(); i++) - if (this->ourFiles[i].filename().string() == this->menu->GetItems()[selectedIndex]->GetName()) { - nspIndex = i; - break; - } + long unsigned int nspIndex = this->menuIndices[selectedIndex]; int dirListSize = this->ourDirectories.size(); if (this->currentDir != "sdmc:/") dirListSize++; diff --git a/source/ui/usbHDDInstPage.cpp b/source/ui/usbHDDInstPage.cpp index b93c3cc..5a2c12f 100644 --- a/source/ui/usbHDDInstPage.cpp +++ b/source/ui/usbHDDInstPage.cpp @@ -72,8 +72,7 @@ namespace inst::ui { } this->menu->ClearItems(); - - const std::regex idRegex(".*\\[([0-9a-fA-F]+)]\\[v(\\d+)].*"); + this->menuIndices = {}; try { this->ourDirectories = util::getDirsAtPath(this->currentDir); @@ -97,32 +96,25 @@ namespace inst::ui { ourEntry->SetIcon("romfs:/images/icons/folder.png"); this->menu->AddItem(ourEntry); } - for (auto& file: this->ourFiles) { + + for (long unsigned int i = 0; i < this->ourFiles.size(); i++) { + auto& file = this->ourFiles[i]; + std::string itm = file.filename().string(); - std::smatch match; - if (hideInstalled and std::regex_match(itm, match, idRegex)) { - u64 id = stol(match[1], nullptr, 16); - u32 version = stoi(match[2]); - bool installed = false; - for (const auto &title: installedTitles) - if (id == title.first and version <= title.second) { - installed = true; - break; - } - if (installed) - continue; - } + if (hideInstalled and inst::util::isTitleInstalled(itm, installedTitles)) + continue; auto ourEntry = pu::ui::elm::MenuItem::New(itm); ourEntry->SetColor(COLOR("#FFFFFFFF")); ourEntry->SetIcon("romfs:/images/icons/checkbox-blank-outline.png"); - for (long unsigned int i = 0; i < this->selectedTitles.size(); i++) { - if (this->selectedTitles[i] == file) { + for (long unsigned int j = 0; j < this->selectedTitles.size(); j++) { + if (this->selectedTitles[j] == file) { ourEntry->SetIcon("romfs:/images/icons/check-box-outline.png"); } } this->menu->AddItem(ourEntry); + this->menuIndices.push_back(i); } } @@ -152,12 +144,7 @@ namespace inst::ui { } void usbHDDInstPage::selectNsp(int selectedIndex) { - long unsigned int nspIndex = 0; - for (long unsigned int i = 0; i < this->ourFiles.size(); i++) - if (this->ourFiles[i].filename().string() == this->menu->GetItems()[selectedIndex]->GetName()) { - nspIndex = i; - break; - } + long unsigned int nspIndex = this->menuIndices[selectedIndex]; int dirListSize = this->ourDirectories.size(); dirListSize++; diff --git a/source/util/util.cpp b/source/util/util.cpp index 49d89bd..4758173 100644 --- a/source/util/util.cpp +++ b/source/util/util.cpp @@ -439,6 +439,19 @@ namespace inst::util { return installedTitles; } + bool isTitleInstalled(std::string filename, const std::vector> &installedTitles) { + static const std::regex idRegex(".*\\[([0-9a-fA-F]+)]\\[v(\\d+)].*"); + std::smatch match; + if (std::regex_match(filename, match, idRegex)) { + u64 id = stol(match[1], nullptr, 16); + u32 version = stoi(match[2]); + for (const auto &title: installedTitles) + if (id == title.first and version <= title.second) + return true; + } + return false; + } + std::vector checkForAppUpdate () { try { std::string jsonData = inst::curl::downloadToBuffer("https://api.github.com/repos/dezem/AtmoXL-Titel-Installer/releases/latest", 0, 0, 1000L);