diff --git a/include/color_swapper.hpp b/include/color_swapper.hpp index 43cf307..c76cba6 100644 --- a/include/color_swapper.hpp +++ b/include/color_swapper.hpp @@ -6,7 +6,7 @@ namespace JC { int setColor(std::vector colors); int backupToJSON(nlohmann::json &profiles, const char* path); - std::tuple, std::vector>> getProfiles(const char* path); + std::vector>> getProfiles(const char* path); void changeJCColor(std::vector values); nlohmann::json backupProfile(); void backupJCColor(const char* path); @@ -17,7 +17,7 @@ namespace PC { int setColor(std::vector colors); int backupToJSON(nlohmann::json &profiles, const char* path); - std::tuple, std::vector>> getProfiles(const char* path); + std::vector>> getProfiles(const char* path); void changePCColor(std::vector values); nlohmann::json backupProfile(); void backupPCColor(const char* path); diff --git a/include/fs.hpp b/include/fs.hpp new file mode 100644 index 0000000..b62a3e2 --- /dev/null +++ b/include/fs.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include +#include +#include + +namespace fs +{ + + int removeDir(const char* path); + nlohmann::json parseJsonFile(const char* path); + void writeJsonToFile(nlohmann::json &data, const char* path); + bool copyFile(const char *to, const char *from); + std::string copyFiles(const char* path); + void createTree(std::string path); + std::set readLineByLine(const char * path); + +} diff --git a/include/utils.hpp b/include/utils.hpp index 7bdbf34..f27d478 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -9,39 +9,30 @@ namespace util { -typedef char NsApplicationName[0x201]; -typedef uint8_t NsApplicationIcon[0x20000]; + typedef char NsApplicationName[0x201]; + typedef uint8_t NsApplicationIcon[0x20000]; -struct app -{ - uint64_t tid; - NsApplicationName name; - NsApplicationIcon icon; - brls::ListItem* listItem; -}; + struct app { + uint64_t tid; + NsApplicationName name; + NsApplicationIcon icon; + brls::ListItem* listItem; + }; -void createTree(std::string path); -void clearConsole(); -bool isArchive(const char * path); -void downloadArchive(std::string url, archiveType type); -void extractArchive(archiveType type, std::string tag = "0"); -std::string formatListItemTitle(const std::string &str, size_t maxScore = 140); -std::string formatApplicationId(u64 ApplicationId); -std::set readLineByLine(const char * path); -std::vector fetchPayloads(); -void shut_down(bool reboot = false); -int showDialogBox(std::string text, std::string opt); -int showDialogBox(std::string text, std::string opt1, std::string opt2); -std::string getLatestTag(const char *url); -Result CopyFile(const char src_path[FS_MAX_PATH], const char dest_path[FS_MAX_PATH]); -void saveVersion(std::string version, const char* path); -std::string readVersion(const char* path); -void cp(const char *to, const char *from); -std::string copyFiles(const char* path); -int removeDir(const char* path); -bool isErista(); -void removeSysmodulesFlags(const char * directory); -nlohmann::json parseJsonFile(const char* path); -void writeJsonToFile(nlohmann::json &profiles, const char* path); + void clearConsole(); + bool isArchive(const char * path); + void downloadArchive(std::string url, archiveType type); + void extractArchive(archiveType type, std::string tag = "0"); + std::string formatListItemTitle(const std::string &str, size_t maxScore = 140); + std::string formatApplicationId(u64 ApplicationId); + std::vector fetchPayloads(); + void shutDown(bool reboot = false); + int showDialogBox(std::string text, std::string opt); + int showDialogBox(std::string text, std::string opt1, std::string opt2); + std::string getLatestTag(const char *url); + void saveVersion(std::string version, const char* path); + std::string readVersion(const char* path); + bool isErista(); + void removeSysmodulesFlags(const char * directory); } \ No newline at end of file diff --git a/source/JC_page.cpp b/source/JC_page.cpp index 0cc64b0..19953e0 100644 --- a/source/JC_page.cpp +++ b/source/JC_page.cpp @@ -33,12 +33,9 @@ JCPage::JCPage() : AppletFrame(true, true) list->addView(new brls::ListItemGroupSpacing(true)); auto profiles = JC::getProfiles(COLOR_PROFILES_PATH); - std::vector names = std::get<0>(profiles); - int nbProfiles = names.size(); - for (int i = nbProfiles - 1; i >= 0; i--){ - std::string name = std::get<0>(profiles)[i]; - std::vector value = std::get<1>(profiles)[i]; - listItem = new brls::ListItem(names[i]); + for (int i = profiles.size() - 1; i >= 0; i--){ + std::vector value = profiles[i].second; + listItem = new brls::ListItem(profiles[i].first); listItem->getClickEvent()->subscribe([&, value](brls::View* view) { brls::StagedAppletFrame* stagedFrame = new brls::StagedAppletFrame(); stagedFrame->setTitle("menus/joy_con/label"_i18n); diff --git a/source/PC_page.cpp b/source/PC_page.cpp index 6ecf8a4..f990277 100644 --- a/source/PC_page.cpp +++ b/source/PC_page.cpp @@ -32,12 +32,9 @@ PCPage::PCPage() : AppletFrame(true, true) list->addView(new brls::ListItemGroupSpacing(true)); auto profiles = PC::getProfiles(PC_COLOR_PATH); - std::vector names = std::get<0>(profiles); - int nbProfiles = names.size(); - for (int i = nbProfiles - 1; i >= 0; i--){ - std::string name = std::get<0>(profiles)[i]; - std::vector value = std::get<1>(profiles)[i]; - listItem = new brls::ListItem(names[i]); + for (int i = profiles.size() - 1; i >= 0; i--){ + std::vector value = profiles[i].second; + listItem = new brls::ListItem(profiles[i].first); listItem->getClickEvent()->subscribe([&, value](brls::View* view) { brls::StagedAppletFrame* stagedFrame = new brls::StagedAppletFrame(); stagedFrame->setTitle("menus/pro_con/label"_i18n); diff --git a/source/ams_tab.cpp b/source/ams_tab.cpp index f75d8e2..b6f49e8 100644 --- a/source/ams_tab.cpp +++ b/source/ams_tab.cpp @@ -21,16 +21,15 @@ AmsTab::AmsTab() : operation += "menus/main/ams"_i18n; links = download::getLinks(AMS_URL); - int nbLinks = links.size(); - if(nbLinks){ + if(links.size()){ auto hekate_link = download::getLinks(HEKATE_URL); std::string hekate_url = hekate_link[0].second; std::string text_hekate = "menus/common/download"_i18n + hekate_link[0].first; - for (int i = 0; i < nbLinks; i++){ - std::string url = links[i].second; - std::string text("menus/common/download"_i18n + links[i].first + "menus/common/from"_i18n + url); - listItem = new brls::ListItem(links[i].first); + for (const auto& link : links){ + std::string url = link.second; + std::string text("menus/common/download"_i18n + link.first + "menus/common/from"_i18n + url); + listItem = new brls::ListItem(link.first); listItem->setHeight(LISTITEM_HEIGHT); listItem->getClickEvent()->subscribe([&, text, text_hekate, url, hekate_url, operation](brls::View* view) { brls::StagedAppletFrame* stagedFrame = new brls::StagedAppletFrame(); diff --git a/source/app_page.cpp b/source/app_page.cpp index 34aad83..a986ce5 100644 --- a/source/app_page.cpp +++ b/source/app_page.cpp @@ -1,13 +1,14 @@ #include "app_page.hpp" #include -#include "utils.hpp" #include #include #include "current_cfw.hpp" #include "worker_page.hpp" #include "confirm_page.hpp" #include "download_cheats_page.hpp" - +#include "utils.hpp" +#include "fs.hpp" + namespace i18n = brls::i18n; using namespace i18n::literals; AppPage::AppPage(const bool cheatSlips) : AppletFrame(true, true) @@ -31,7 +32,7 @@ AppPage::AppPage(const bool cheatSlips) : AppletFrame(true, true) int recordCount = 0; size_t controlSize = 0; - titles = util::readLineByLine(UPDATED_TITLES_PATH); + titles = fs::readLineByLine(UPDATED_TITLES_PATH); if(!titles.empty() || cheatSlips){ while (true) diff --git a/source/color_swapper.cpp b/source/color_swapper.cpp index a425351..71d80de 100644 --- a/source/color_swapper.cpp +++ b/source/color_swapper.cpp @@ -4,11 +4,10 @@ #include #include #include -#include - #include "constants.hpp" #include "progress_event.hpp" #include "utils.hpp" +#include "fs.hpp" using json = nlohmann::json; @@ -83,7 +82,7 @@ namespace JC { {"R_BTN", ColorSwapper::BGRToHex(color_right.sub)} }); profiles.push_back(newBackup); - util::writeJsonToFile(profiles, path); + fs::writeJsonToFile(profiles, path); return 0; } else{ @@ -109,17 +108,11 @@ namespace JC { } - std::tuple, std::vector>> getProfiles(const char* path){ - std::vector names; - std::vector> colorValues; + std::vector>> getProfiles(const char* path){ + std::vector>> res; bool properData; std::fstream profilesFile; - json profilesJson; - if(std::filesystem::exists(path)){ - profilesFile.open(path, std::fstream::in); - profilesFile >> profilesJson; - profilesFile.close(); - } + json profilesJson = fs::parseJsonFile(path); if(profilesJson.empty()){ profilesJson = {{ {"L_BTN", "0A1E0A"}, @@ -128,7 +121,6 @@ namespace JC { {"R_JC", "96F5F5"}, {"name", "Animal Crossing: New Horizons"} }}; - util::writeJsonToFile(profilesJson, path); } for (const auto& x : profilesJson.items()){ std::string name = x.value()["name"]; @@ -146,16 +138,15 @@ namespace JC { } if(properData){ if(name == "") name = "Unamed"; - names.push_back(name); - colorValues.push_back({ + res.push_back(std::make_pair(name, (std::vector){ ColorSwapper::hexToBGR(values[0]), ColorSwapper::hexToBGR(values[1]), ColorSwapper::hexToBGR(values[2]), ColorSwapper::hexToBGR(values[3]) - }); + })); } } - return std::make_tuple(names, colorValues); + return res; } void changeJCColor(std::vector values){ @@ -204,7 +195,7 @@ namespace JC { profiles.push_back(backup); //backup.push_back(profiles); - util::writeJsonToFile(profiles, path); + fs::writeJsonToFile(profiles, path); hiddbgExit(); hidsysExit(); ProgressEvent::instance().setStep(ProgressEvent::instance().getMax()); @@ -252,7 +243,7 @@ namespace PC { {"BTN", ColorSwapper::BGRToHex(color.sub)} }); profiles.push_back(newBackup); - util::writeJsonToFile(profiles, path); + fs::writeJsonToFile(profiles, path); return 0; } else{ @@ -275,24 +266,17 @@ namespace PC { } - std::tuple, std::vector>> getProfiles(const char* path){ - std::vector names; - std::vector> colorValues; + std::vector>> getProfiles(const char* path){ + std::vector>> res; bool properData; std::fstream profilesFile; - json profilesJson; - if(std::filesystem::exists(path)){ - profilesFile.open(path, std::fstream::in); - profilesFile >> profilesJson; - profilesFile.close(); - } + json profilesJson = fs::parseJsonFile(path); if(profilesJson.empty()){ profilesJson = {{ {"BTN", "2d2d2d"}, {"BODY", "e6e6e6"}, {"name", "Default black"} }}; - util::writeJsonToFile(profilesJson, path); } for (const auto& x : profilesJson.items()){ std::string name = x.value()["name"]; @@ -308,14 +292,13 @@ namespace PC { } if(properData){ if(name == "") name = "Unamed"; - names.push_back(name); - colorValues.push_back({ + res.push_back(std::make_pair(name, (std::vector){ ColorSwapper::hexToBGR(values[0]), ColorSwapper::hexToBGR(values[1]) - }); + })); } } - return std::make_tuple(names, colorValues); + return res; } void changePCColor(std::vector values){ @@ -364,7 +347,7 @@ namespace PC { profiles.push_back(backup); //backup.push_back(profiles); - util::writeJsonToFile(profiles, path); + fs::writeJsonToFile(profiles, path); hiddbgExit(); hidsysExit(); ProgressEvent::instance().setStep(ProgressEvent::instance().getMax()); diff --git a/source/download_cheats_page.cpp b/source/download_cheats_page.cpp index 536249b..c6f6527 100644 --- a/source/download_cheats_page.cpp +++ b/source/download_cheats_page.cpp @@ -1,12 +1,14 @@ #include "download_cheats_page.hpp" +#include +#include #include "constants.hpp" #include "download.hpp" #include "utils.hpp" +#include "fs.hpp" #include "current_cfw.hpp" -#include -#include + //#include - + namespace i18n = brls::i18n; using namespace i18n::literals; using json = nlohmann::json; @@ -225,7 +227,7 @@ void DownloadCheatsPage::WriteCheats(uint64_t tid, std::string bid, std::string break; } path += tidstr + "/cheats/"; - util::createTree(path); + fs::createTree(path); path += bid + ".txt"; std::ofstream cheatFile; cheatFile.open(path, std::ios::app); diff --git a/source/download_payload_page.cpp b/source/download_payload_page.cpp index f037f36..ddf2504 100644 --- a/source/download_payload_page.cpp +++ b/source/download_payload_page.cpp @@ -1,9 +1,10 @@ #include "download_payload_page.hpp" -#include "utils.hpp" #include "confirm_page.hpp" #include "worker_page.hpp" #include "download.hpp" - +#include "utils.hpp" +#include "fs.hpp" + namespace i18n = brls::i18n; using namespace i18n::literals; DownloadPayloadPage::DownloadPayloadPage() : AppletFrame(true, true) @@ -18,15 +19,14 @@ DownloadPayloadPage::DownloadPayloadPage() : AppletFrame(true, true) list->addView(label); auto links = download::getLinks(PAYLOAD_URL); - int nbLinks = links.size(); - if(nbLinks){ - for (int i = 0; igetClickEvent()->subscribe([&, text, url, path](brls::View* view) { - util::createTree(BOOTLOADER_PL_PATH); + fs::createTree(BOOTLOADER_PL_PATH); brls::StagedAppletFrame* stagedFrame = new brls::StagedAppletFrame(); stagedFrame->setTitle("menus/getting_paylaod"_i18n); stagedFrame->addStage( diff --git a/source/exclude_page.cpp b/source/exclude_page.cpp index e155bc7..28ce952 100644 --- a/source/exclude_page.cpp +++ b/source/exclude_page.cpp @@ -5,6 +5,7 @@ #include #include "extract.hpp" #include "utils.hpp" +#include "fs.hpp" namespace i18n = brls::i18n; using namespace i18n::literals; @@ -29,7 +30,7 @@ ExcludePage::ExcludePage() : AppletFrame(true, true) int recordCount = 0; size_t controlSize = 0; - titles = util::readLineByLine(CHEATS_EXCLUDE); + titles = fs::readLineByLine(CHEATS_EXCLUDE); while (true) { diff --git a/source/extract.cpp b/source/extract.cpp index 04aca03..a4bf344 100644 --- a/source/extract.cpp +++ b/source/extract.cpp @@ -1,6 +1,4 @@ #include "extract.hpp" -#include "utils.hpp" -#include "download.hpp" #include #include #include @@ -12,6 +10,9 @@ #include #include #include "progress_event.hpp" +#include "utils.hpp" +#include "download.hpp" +#include "fs.hpp" namespace i18n = brls::i18n; using namespace i18n::literals; @@ -28,7 +29,7 @@ void extract(const char * filename, const char* workingPath, int overwriteInis){ ProgressEvent::instance().reset(); ProgressEvent::instance().setStep(1); chdir(workingPath); - std::set ignoreList = util::readLineByLine(FILES_IGNORE); + std::set ignoreList = fs::readLineByLine(FILES_IGNORE); std::set::iterator it; zipper::Unzipper unzipper(filename); std::vector entries = unzipper.entries(); @@ -59,16 +60,12 @@ void extract(const char * filename, const char* workingPath, int overwriteInis){ if(entries[i].name == "sept/payload.bin" || entries[i].name == "atmosphere/fusee-secondary.bin"){ unzipper.extractEntry(entries[i].name, CONFIG_PATH_UNZIP); } - else if(entries[i].name.substr(0, 13) == "hekate_ctcaer"){ + else { unzipper.extractEntry(entries[i].name); - int c = 0; - while(R_FAILED(util::CopyFile(("/" + entries[i].name).c_str(), UPDATE_BIN_PATH)) && c < 10){ - c++; + if(entries[i].name.substr(0, 13) == "hekate_ctcaer") { + fs::copyFile(("/" + entries[i].name).c_str(), UPDATE_BIN_PATH); } } - else{ - unzipper.extractEntry(entries[i].name); - } } ProgressEvent::instance().setStep(i); @@ -81,7 +78,7 @@ void extract(const char * filename, const char* workingPath, const char* toExclu ProgressEvent::instance().reset(); ProgressEvent::instance().setStep(1); chdir(workingPath); - std::set ignoreList = util::readLineByLine(FILES_IGNORE); + std::set ignoreList = fs::readLineByLine(FILES_IGNORE); ignoreList.insert(toExclude); std::set::iterator it; zipper::Unzipper unzipper(filename); diff --git a/source/fs.cpp b/source/fs.cpp new file mode 100644 index 0000000..2ea4c24 --- /dev/null +++ b/source/fs.cpp @@ -0,0 +1,100 @@ +#include "fs.hpp" +#include +#include +#include +#include "constants.hpp" + + +namespace i18n = brls::i18n; +using namespace i18n::literals; + +namespace fs { + + int removeDir(const char* path) { + Result ret = 0; + FsFileSystem *fs = fsdevGetDeviceFileSystem("sdmc"); + if (R_FAILED(ret = fsFsDeleteDirectoryRecursively(fs, path))) + return ret; + return 0; + } + + nlohmann::json parseJsonFile(const char* path) { + std::ifstream file(path); + + std::string fileContent((std::istreambuf_iterator(file) ), + (std::istreambuf_iterator() )); + + if(nlohmann::json::accept(fileContent)) return nlohmann::json::parse(fileContent); + else return nlohmann::json::object(); + } + + void writeJsonToFile(nlohmann::json &data, const char* path) { + std::ofstream out(path); + out << data.dump(4); + out.close(); + } + + bool copyFile(const char *from, const char *to){ + std::ifstream src(from, std::ios::binary); + std::ofstream dst(to, std::ios::binary); + + if (src.good() && dst.good()) { + dst << src.rdbuf(); + return true; + } + return false; + } + + void createTree(std::string path){ + std::string delimiter = "/"; + size_t pos = 0; + std::string token; + std::string directories(""); + while ((pos = path.find(delimiter)) != std::string::npos) { + token = path.substr(0, pos); + directories += token + "/"; + std::filesystem::create_directory(directories); + path.erase(0, pos + delimiter.length()); + } +} + + std::string copyFiles(const char* path) { + nlohmann::ordered_json toMove; + std::ifstream f(COPY_FILES_JSON); + f >> toMove; + f.close(); + std::string error = ""; + for (auto it = toMove.begin(); it != toMove.end(); ++it) { + if(std::filesystem::exists(it.key())) { + createTree(std::string(std::filesystem::path(it.value().get()).parent_path()) + "/"); + copyFile(it.key().c_str(), it.value().get().c_str()); + } + else { + error += it.key() + "\n"; + } + } + if(error == "") { + error = "menus/common/all_done"_i18n; + } + else { + error = "menus/tools/batch_copy_not_found"_i18n + error; + } + return error; + } + + std::set readLineByLine(const char * path){ + std::set titles; + std::string str; + std::ifstream in(path); + if(in){ + while (std::getline(in, str)) + { + if(str.size() > 0) + titles.insert(str); + } + in.close(); + } + return titles; + } + +} \ No newline at end of file diff --git a/source/hide_tabs_page.cpp b/source/hide_tabs_page.cpp index 28c87fb..2d058ce 100644 --- a/source/hide_tabs_page.cpp +++ b/source/hide_tabs_page.cpp @@ -2,7 +2,7 @@ #include #include #include "constants.hpp" -#include "utils.hpp" +#include "fs.hpp" namespace i18n = brls::i18n; @@ -19,7 +19,7 @@ HideTabsPage::HideTabsPage() : AppletFrame(true, true) { ); list->addView(label); - json hideStatus = util::parseJsonFile(HIDE_TABS_JSON); + json hideStatus = fs::parseJsonFile(HIDE_TABS_JSON); bool status = false; if(hideStatus.find("about") != hideStatus.end()) { @@ -71,9 +71,7 @@ HideTabsPage::HideTabsPage() : AppletFrame(true, true) { updatedStatus["sigpatches"] = sigpatches->getToggleState(); updatedStatus["firmwares"] = fws->getToggleState(); updatedStatus["cheats"] = cheats->getToggleState(); - std::ofstream out(HIDE_TABS_JSON); - out << updatedStatus.dump(4); - out.close(); + fs::writeJsonToFile(updatedStatus, HIDE_TABS_JSON); brls::Application::popView(); return true; }); diff --git a/source/list_download_tab.cpp b/source/list_download_tab.cpp index 9903e1d..47b8149 100644 --- a/source/list_download_tab.cpp +++ b/source/list_download_tab.cpp @@ -78,12 +78,11 @@ ListDownloadTab::ListDownloadTab(const archiveType type) : this->addView(description); - int nbLinks = links.size(); - if(nbLinks){ - for (int i = 0; isetHeight(LISTITEM_HEIGHT); listItem->getClickEvent()->subscribe([&, text, url, type, operation](brls::View* view) { brls::StagedAppletFrame* stagedFrame = new brls::StagedAppletFrame(); diff --git a/source/main.cpp b/source/main.cpp index afebf38..36ac225 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,16 +1,13 @@ -//#include -//#include -#include +#include #include #include +#include #include "main_frame.hpp" #include "constants.hpp" -#include "utils.hpp" +#include "fs.hpp" #include "current_cfw.hpp" #include "warning_page.hpp" -#include -#include "json.hpp" -#include + namespace i18n = brls::i18n; using namespace i18n::literals; @@ -28,7 +25,7 @@ int main(int argc, char* argv[]) return EXIT_FAILURE; } - nlohmann::json languageFile = util::parseJsonFile(LANGUAGE_JSON); + nlohmann::json languageFile = fs::parseJsonFile(LANGUAGE_JSON); if(languageFile.find("language") != languageFile.end()) i18n::loadTranslations(languageFile["language"]); else @@ -48,7 +45,7 @@ int main(int argc, char* argv[]) splInitialize(); romfsInit(); - util::createTree(CONFIG_PATH); + fs::createTree(CONFIG_PATH); brls::Logger::setLogLevel(brls::LogLevel::DEBUG); brls::Logger::debug("Start"); diff --git a/source/main_frame.cpp b/source/main_frame.cpp index f6b1fbe..c62ba6f 100644 --- a/source/main_frame.cpp +++ b/source/main_frame.cpp @@ -6,6 +6,7 @@ #include #include #include "utils.hpp" +#include "fs.hpp" namespace i18n = brls::i18n; using namespace i18n::literals; @@ -22,7 +23,7 @@ MainFrame::MainFrame() : TabFrame() else this->setFooterText("v" + std::string(APP_VERSION)); - json hideStatus = util::parseJsonFile(HIDE_TABS_JSON); + json hideStatus = fs::parseJsonFile(HIDE_TABS_JSON); bool erista = util::isErista(); diff --git a/source/net_page.cpp b/source/net_page.cpp index ddd3383..b0caa01 100644 --- a/source/net_page.cpp +++ b/source/net_page.cpp @@ -3,12 +3,11 @@ #include #include #include -#include #include +#include #include "constants.hpp" #include "main_frame.hpp" -#include -#include "utils.hpp" +#include "fs.hpp" namespace i18n = brls::i18n; using namespace i18n::literals; @@ -23,8 +22,7 @@ NetPage::NetPage() : AppletFrame(true, true) nifmGetCurrentNetworkProfile (&profile); nifmExit(); - int uuid = 0; - for (int j = 0; j < 16; j++) uuid += int(profile.uuid.uuid[j]); + int uuid = std::accumulate(profile.uuid.uuid, profile.uuid.uuid + 16, 0); std::string labelText = ""; if(uuid){ @@ -37,7 +35,7 @@ NetPage::NetPage() : AppletFrame(true, true) +"\nGateway: " + ipToString(profile.ip_setting_data.ip_address_setting.gateway.addr); } struct in_addr addr = {(in_addr_t) gethostid()}; - labelText += "\n Local IP addr" + std::string(inet_ntoa(addr)); + labelText += "\nLocal IP addr" + std::string(inet_ntoa(addr)); labelText += "\nMTU: " + std::to_string(unsigned(profile.ip_setting_data.mtu)); if(profile.ip_setting_data.dns_setting.is_automatic){ @@ -66,7 +64,7 @@ NetPage::NetPage() : AppletFrame(true, true) //dns_auto if(uuid){ - json profiles = util::parseJsonFile(INTERNET_JSON); + json profiles = fs::parseJsonFile(INTERNET_JSON); if(profiles.empty()) { profiles = {{ {"name", "90DNS (Europe)"}, diff --git a/source/payload_page.cpp b/source/payload_page.cpp index e77148f..18116d9 100644 --- a/source/payload_page.cpp +++ b/source/payload_page.cpp @@ -3,9 +3,11 @@ #include "reboot_payload.h" #include "current_cfw.hpp" #include "utils.hpp" - +#include "fs.hpp" + namespace i18n = brls::i18n; using namespace i18n::literals; + PayloadPage::PayloadPage() : AppletFrame(true, true) { this->setTitle("menus/payloads/reboot_title"_i18n); @@ -17,19 +19,18 @@ PayloadPage::PayloadPage() : AppletFrame(true, true) ); list->addView(label); std::vector payloads = util::fetchPayloads(); - int nbPayloads = payloads.size(); - for (int i = 0; i < nbPayloads; i++){ - std::string payload = payloads[i]; - listItem = new brls::ListItem(payload); + for (const auto& payload : payloads){ + std::string payload_path = payload; + listItem = new brls::ListItem(payload_path); listItem->getClickEvent()->subscribe([&, payload](brls::View* view) { reboot_to_payload(payload.c_str()); brls::Application::popView(); }); if(CurrentCfw::running_cfw == CFW::ams){ - listItem->registerAction("menus/payloads/set_reboot_payload"_i18n, brls::Key::X, [this, payload] { + listItem->registerAction("menus/payloads/set_reboot_payload"_i18n, brls::Key::X, [this, payload_path] { std::string res1; - if(R_SUCCEEDED(util::CopyFile(payload.c_str(), REBOOT_PAYLOAD_PATH))){ - res1 += "menus/payloads/copy_success"_i18n + payload + "menus/payloads/to"_i18n + std::string(REBOOT_PAYLOAD_PATH) + "'."; + if(fs::copyFile(payload_path.c_str(), REBOOT_PAYLOAD_PATH)){ + res1 += "menus/payloads/copy_success"_i18n + payload_path + "menus/payloads/to"_i18n + std::string(REBOOT_PAYLOAD_PATH) + "'."; } else{ @@ -45,9 +46,9 @@ PayloadPage::PayloadPage() : AppletFrame(true, true) return true; }); } - listItem->registerAction("menus/payloads/set_reboot_payload_up"_i18n, brls::Key::Y, [this, payload] { + listItem->registerAction("menus/payloads/set_reboot_payload"_i18n, brls::Key::Y, [this, payload] { std::string res2; - if(R_SUCCEEDED(util::CopyFile(payload.c_str(), UPDATE_BIN_PATH))){ + if(fs::copyFile(payload.c_str(), UPDATE_BIN_PATH)){ res2 += "menus/payloads/copy_success"_i18n + payload + "menus/payloads/to"_i18n + std::string(UPDATE_BIN_PATH) + "'."; } else{ @@ -68,14 +69,14 @@ PayloadPage::PayloadPage() : AppletFrame(true, true) shutDown = new brls::ListItem("menus/common/shut_down"_i18n); shutDown->getClickEvent()->subscribe([](brls::View* view) { - util::shut_down(false); + util::shutDown(false); brls::Application::popView(); }); list->addView(shutDown); reboot = new brls::ListItem("menus/payloads/reboot"_i18n); reboot->getClickEvent()->subscribe([](brls::View* view) { - util::shut_down(true); + util::shutDown(true); brls::Application::popView(); }); list->addView(reboot); diff --git a/source/tools_tab.cpp b/source/tools_tab.cpp index ce3a02b..b58eb67 100644 --- a/source/tools_tab.cpp +++ b/source/tools_tab.cpp @@ -10,6 +10,7 @@ #include "net_page.hpp" #include "extract.hpp" #include "utils.hpp" +#include "fs.hpp" #include "hide_tabs_page.hpp" #include #include @@ -156,7 +157,7 @@ ToolsTab::ToolsTab(std::string tag, bool erista) : brls::List() chdir("/"); std::string error = ""; if(std::filesystem::exists(COPY_FILES_JSON)){ - error = util::copyFiles(COPY_FILES_JSON); + error = fs::copyFiles(COPY_FILES_JSON); } else{ error = "menus/tools/batch_copy_config_not_found"_i18n; @@ -180,9 +181,9 @@ ToolsTab::ToolsTab(std::string tag, bool erista) : brls::List() std::filesystem::remove(FW_ZIP_PATH); std::filesystem::remove(CHEATS_ZIP_PATH); std::filesystem::remove(SIGPATCHES_ZIP_PATH); - util::removeDir(AMS_DIRECTORY_PATH); - util::removeDir(SEPT_DIRECTORY_PATH); - util::removeDir(FW_DIRECTORY_PATH); + fs::removeDir(AMS_DIRECTORY_PATH); + fs::removeDir(SEPT_DIRECTORY_PATH); + fs::removeDir(FW_DIRECTORY_PATH); brls::Dialog* dialog = new brls::Dialog("menus/common/all_done"_i18n); brls::GenericEvent::Callback callback = [dialog](brls::View* view) { dialog->close(); diff --git a/source/utils.cpp b/source/utils.cpp index 3789554..c7df544 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -1,4 +1,5 @@ #include "utils.hpp" +#include "fs.hpp" #include "current_cfw.hpp" #include #include "download.hpp" @@ -13,19 +14,6 @@ using namespace i18n::literals; namespace util { -void createTree(std::string path){ - std::string delimiter = "/"; - size_t pos = 0; - std::string token; - std::string directories(""); - while ((pos = path.find(delimiter)) != std::string::npos) { - token = path.substr(0, pos); - directories += token + "/"; - std::filesystem::create_directory(directories); - path.erase(0, pos + delimiter.length()); - } -} - bool isArchive(const char * path){ std::fstream file; std::string fileContent; @@ -38,7 +26,7 @@ bool isArchive(const char * path){ } void downloadArchive(std::string url, archiveType type){ - createTree(DOWNLOAD_PATH); + fs::createTree(DOWNLOAD_PATH); AppletType at; switch(type){ case archiveType::sigpatches: @@ -147,13 +135,13 @@ void extractArchive(archiveType type, std::string tag){ } else{ if (std::filesystem::exists(FIRMWARE_PATH)) std::filesystem::remove_all(FIRMWARE_PATH); - createTree(FIRMWARE_PATH); + fs::createTree(FIRMWARE_PATH); extract::extract(FIRMWARE_FILENAME, FIRMWARE_PATH); } break; case archiveType::app: extract::extract(APP_FILENAME, CONFIG_PATH); - cp(ROMFS_FORWARDER, FORWARDER_PATH); + fs::copyFile(ROMFS_FORWARDER, FORWARDER_PATH); envSetNextLoad(FORWARDER_PATH, ("\"" + std::string(FORWARDER_PATH) + "\"").c_str()); romfsExit(); brls::Application::quit(); @@ -179,16 +167,14 @@ void extractArchive(archiveType type, std::string tag){ break; } if(std::filesystem::exists(COPY_FILES_JSON)) - copyFiles(COPY_FILES_JSON); + fs::copyFiles(COPY_FILES_JSON); } std::string formatListItemTitle(const std::string &str, size_t maxScore) { size_t score = 0; - for (size_t i = 0; i < str.length(); i++) - { + for (size_t i = 0; i < str.length(); i++) { score += std::isupper(str[i]) ? 4 : 3; - if(score > maxScore) - { + if(score > maxScore) { return str.substr(0, i-1) + "\u2026"; } } @@ -199,21 +185,6 @@ std::string formatApplicationId(u64 ApplicationId){ return fmt::format("{:016X}", ApplicationId); } -std::set readLineByLine(const char * path){ - std::set titles; - std::string str; - std::ifstream in(path); - if(in){ - while (std::getline(in, str)) - { - if(str.size() > 0) - titles.insert(str); - } - in.close(); - } - return titles; -} - std::vector fetchPayloads(){ std::vector payloadPaths; payloadPaths.push_back(ROOT_PATH); @@ -224,8 +195,8 @@ std::vector fetchPayloads(){ if(std::filesystem::exists(BOOTLOADER_PL_PATH)) payloadPaths.push_back(BOOTLOADER_PL_PATH); if(std::filesystem::exists(SXOS_PATH)) payloadPaths.push_back(SXOS_PATH); std::vector res; - for (auto& path : payloadPaths){ - for (const auto & entry : std::filesystem::directory_iterator(path)){ + for (const auto& path : payloadPaths){ + for (const auto& entry : std::filesystem::directory_iterator(path)){ if(entry.path().extension().string() == ".bin"){ if(entry.path().string() != FUSEE_SECONDARY && entry.path().string() != FUSEE_MTC) res.push_back(entry.path().string().c_str()); @@ -236,7 +207,7 @@ std::vector fetchPayloads(){ return res; } -void shut_down(bool reboot){ +void shutDown(bool reboot){ bpcInitialize(); if(reboot) bpcRebootSystem(); else bpcShutdownSystem(); @@ -251,78 +222,6 @@ std::string getLatestTag(const char *url){ return ""; } -void cp(const char *from, const char *to){ - std::ifstream src(from, std::ios::binary); - std::ofstream dst(to, std::ios::binary); - - if (src.good() && dst.good()) { - dst << src.rdbuf(); - } -} - -Result CopyFile(const char src_path[FS_MAX_PATH], const char dest_path[FS_MAX_PATH]) { - FsFileSystem *fs; - Result ret = 0; - FsFile src_handle, dest_handle; - int PREVIOUS_BROWSE_STATE = 0; - FsFileSystem devices[4]; - devices[0] = *fsdevGetDeviceFileSystem("sdmc"); - fs = &devices[0]; - - ret = fsFsOpenFile(&devices[PREVIOUS_BROWSE_STATE], src_path, FsOpenMode_Read, &src_handle); - if (R_FAILED(ret)) { - return ret; - } - - s64 size = 0; - ret = fsFileGetSize(&src_handle, &size); - if (R_FAILED(ret)){ - fsFileClose(&src_handle); - return ret; - } - - std::filesystem::remove(dest_path); - fsFsCreateFile(fs, dest_path, size, 0); - - ret = fsFsOpenFile(fs, dest_path, FsOpenMode_Write, &dest_handle); - if (R_FAILED(ret)){ - fsFileClose(&src_handle); - return ret; - } - - uint64_t bytes_read = 0; - const u64 buf_size = 0x10000; - s64 offset = 0; - unsigned char *buf = new unsigned char[buf_size]; - std::string filename = std::filesystem::path(src_path).filename(); - - do { - std::memset(buf, 0, buf_size); - - ret = fsFileRead(&src_handle, offset, buf, buf_size, FsReadOption_None, &bytes_read); - if (R_FAILED(ret)) { - delete[] buf; - fsFileClose(&src_handle); - fsFileClose(&dest_handle); - return ret; - } - ret = fsFileWrite(&dest_handle, offset, buf, bytes_read, FsWriteOption_Flush); - if (R_FAILED(ret)) { - delete[] buf; - fsFileClose(&src_handle); - fsFileClose(&dest_handle); - return ret; - } - - offset += bytes_read; - } while(offset < size); - - delete[] buf; - fsFileClose(&src_handle); - fsFileClose(&dest_handle); - return 0; -} - void saveVersion(std::string version, const char* path){ std::fstream newVersion; newVersion.open(path, std::fstream::out | std::fstream::trunc); @@ -341,40 +240,6 @@ std::string readVersion(const char* path){ return version; } -std::string copyFiles(const char* path) { - nlohmann::ordered_json toMove; - std::ifstream f(COPY_FILES_JSON); - f >> toMove; - f.close(); - std::string error = ""; - for (auto it = toMove.begin(); it != toMove.end(); ++it) { - if(std::filesystem::exists(it.key())) { - createTree(std::string(std::filesystem::path(it.value().get()).parent_path()) + "/"); - cp(it.key().c_str(), it.value().get().c_str()); - //std::cout << it.key() << it.value().get() << std::endl; - } - else { - error += it.key() + "\n"; - } - } - if(error == "") { - error = "menus/common/all_done"_i18n; - } - else { - error = "menus/tools/batch_copy_not_found"_i18n + error; - } - return error; -} - -int removeDir(const char* path) { - Result ret = 0; - FsFileSystem *fs = fsdevGetDeviceFileSystem("sdmc"); - if (R_FAILED(ret = fsFsDeleteDirectoryRecursively(fs, path))) { - return ret; - } - return 0; -} - bool isErista() { splInitialize(); u64 hwType; @@ -427,21 +292,4 @@ void removeSysmodulesFlags(const char * directory) { } } -nlohmann::json parseJsonFile(const char* path) { - std::ifstream file(path); - - std::string fileContent((std::istreambuf_iterator(file) ), - (std::istreambuf_iterator() )); - - if(nlohmann::json::accept(fileContent)) return nlohmann::json::parse(fileContent); - else return nlohmann::json::object(); -} - -void writeJsonToFile(nlohmann::json &profiles, const char* path) { - std::fstream newProfiles; - newProfiles.open(path, std::fstream::out | std::fstream::trunc); - newProfiles << std::setw(4) << profiles << std::endl; - newProfiles.close(); -} - } \ No newline at end of file diff --git a/source/warning_page.cpp b/source/warning_page.cpp index 39155e4..990388f 100644 --- a/source/warning_page.cpp +++ b/source/warning_page.cpp @@ -1,15 +1,16 @@ #include "warning_page.hpp" +#include +#include #include "main_frame.hpp" #include "constants.hpp" #include "utils.hpp" -#include -#include - +#include "fs.hpp" + namespace i18n = brls::i18n; using namespace i18n::literals; WarningPage::WarningPage(std::string text) { - util::createTree(CONFIG_PATH); + fs::createTree(CONFIG_PATH); std::ofstream(HIDDEN_AIO_FILE); this->button = (new brls::Button(brls::ButtonStyle::PRIMARY))->setLabel("menus/common/continue"_i18n); this->button->setParent(this);