diff --git a/include/download/download.hpp b/include/download/download.hpp index 4192e34..3e48a94 100644 --- a/include/download/download.hpp +++ b/include/download/download.hpp @@ -67,6 +67,9 @@ void notConnectedMsg(void); */ void notImplemented(void); +// Display the done msg. +void doneMsg(void); + /** * Get info from the GitHub API about a Release. * repo is where to get from. (Ex. "RocketRobz/TWiLightMenu") @@ -100,7 +103,10 @@ std::string getLatestCommit(std::string repo, std::string array, std::string ite */ std::vector getThemeList(std::string repo, std::string path); -/** - * Update nds-bootstrap to the latest build. - */ -void updateBootstrap(bool nightly); \ No newline at end of file +namespace download { + void downloadRelease(std::string repo, std::string file, std::string output, std::string message); + void downloadFile(std::string file, std::string output, std::string message); + void deleteFileList(std::string file, std::string message); + void installFileList(std::string file, std::string message); + void extractFileList(std::string file, std::string input, std::string output, std::string message); +} \ No newline at end of file diff --git a/include/screens/scriptlist.hpp b/include/screens/scriptlist.hpp index 12f1cab..c3421e8 100644 --- a/include/screens/scriptlist.hpp +++ b/include/screens/scriptlist.hpp @@ -37,15 +37,23 @@ public: void Draw(void) const override; void Logic(u32 hDown, u32 hHeld, touchPosition touch) override; ScriptList(); - void showParsedObjects(void) const; private: + void DrawList(void) const; + void DrawSingleObject(void) const; + + void ListSelection(u32 hDown, u32 hHeld); + void SelectFunction(u32 hDown, u32 hHeld); + + int mode = 0; std::vector dirContents; mutable int screenPos = 0; mutable int selection = 0; + mutable int screenPos2 = 0; + mutable int selection2 = 0; + int keyRepeatDelay = 0; int fastMode = false; - bool dirChanged = true; }; #endif \ No newline at end of file diff --git a/source/download/download.cpp b/source/download/download.cpp index 8c3e566..97cadb2 100644 --- a/source/download/download.cpp +++ b/source/download/download.cpp @@ -626,45 +626,49 @@ void displayProgressBar() { } } - -void updateBootstrap(bool nightly) { - if(nightly) { - snprintf(progressBarMsg, sizeof(progressBarMsg), (Lang::get("DOWNLOAD_NDSBOOTSTRAP_NIGHTLY")).c_str()); - showProgressBar = true; - progressBarType = 0; - Threads::create((ThreadFunc)displayProgressBar); - if (downloadToFile("https://github.com/TWLBot/Builds/blob/master/nds-bootstrap.7z?raw=true", "/nds-bootstrap-nightly.7z") != 0) { - showProgressBar = false; - downloadFailed(); - return; - } - - snprintf(progressBarMsg, sizeof(progressBarMsg), (Lang::get("EXTRACT_NDSBOOTSTRAP_NIGHTLY")).c_str()); - filesExtracted = 0; - progressBarType = 1; - extractArchive("/nds-bootstrap-nightly.7z", "nds-bootstrap/", "/_nds/"); +void download::downloadRelease(std::string repo, std::string file, std::string output, std::string message) { + snprintf(progressBarMsg, sizeof(progressBarMsg), message.c_str()); + showProgressBar = true; + progressBarType = 0; + Threads::create((ThreadFunc)displayProgressBar); + if (downloadFromRelease("https://github.com/" + repo, file, output) != 0) { showProgressBar = false; - - deleteFile("sdmc:/nds-bootstrap-nightly.7z"); - } else { - DisplayMsg(Lang::get("DOWNLOAD_NDSBOOTSTRAP_RELEASE")); - snprintf(progressBarMsg, sizeof(progressBarMsg), (Lang::get("DOWNLOAD_NDSBOOTSTRAP_RELEASE")).c_str()); - showProgressBar = true; - progressBarType = 0; - Threads::create((ThreadFunc)displayProgressBar); - if (downloadFromRelease("https://github.com/ahezard/nds-bootstrap", "nds-bootstrap\\.zip", "/nds-bootstrap-release.zip") != 0) { - showProgressBar = false; - downloadFailed(); - return; - } - - snprintf(progressBarMsg, sizeof(progressBarMsg), (Lang::get("EXTRACT_NDSBOOTSTRAP_RELEASE")).c_str()); - filesExtracted = 0; - progressBarType = 1; - extractArchive("/nds-bootstrap-release.zip", "/", "/_nds/"); - showProgressBar = false; - - deleteFile("sdmc:/nds-bootstrap-release.zip"); + downloadFailed(); + return; } - doneMsg(); + showProgressBar = false; +} + + +void download::downloadFile(std::string file, std::string output, std::string message) { + snprintf(progressBarMsg, sizeof(progressBarMsg), message.c_str()); + showProgressBar = true; + progressBarType = 0; + Threads::create((ThreadFunc)displayProgressBar); + if (downloadToFile(file, output) != 0) { + showProgressBar = false; + downloadFailed(); + return; + } + showProgressBar = false; +} + +void download::deleteFileList(std::string file, std::string message) { + DisplayMsg(message); + deleteFile(file.c_str()); +} + +void download::installFileList(std::string file, std::string message) { + DisplayMsg(message); + installCia(file.c_str()); +} + +void download::extractFileList(std::string file, std::string input, std::string output, std::string message) { + snprintf(progressBarMsg, sizeof(progressBarMsg), message.c_str()); + showProgressBar = true; + filesExtracted = 0; + progressBarType = 1; + Threads::create((ThreadFunc)displayProgressBar); + extractArchive(file, input, output); + showProgressBar = false; } \ No newline at end of file diff --git a/source/screens/scriptlist.cpp b/source/screens/scriptlist.cpp index adfeeea..b591da0 100644 --- a/source/screens/scriptlist.cpp +++ b/source/screens/scriptlist.cpp @@ -37,10 +37,13 @@ #define ENTRIES_PER_SCREEN 3 +// Information like Title and Description. struct Info { std::string title; std::string description; }; +std::string choice; +std::string currentFile; Info parseInfo(std::string fileName) { FILE* file = fopen(fileName.c_str(), "rt"); @@ -58,8 +61,70 @@ Info parseInfo(std::string fileName) { return info; } +// Objects like Release or Nightly. +std::vector parseObjects(std::string fileName) { + FILE* file = fopen(fileName.c_str(), "rt"); + if(!file) { + printf("File not found\n"); + fclose(file); + return {{""}}; + } + nlohmann::json json = nlohmann::json::parse(file, nullptr, false); + fclose(file); + + std::vector objs; + for(auto it = json.begin();it != json.end(); it++) { + if(it.key() != "info") { + objs.push_back(it.key()); + } + } + return objs; +} + +void runFunctions(void) { + FILE* file = fopen(currentFile.c_str(), "rt"); + nlohmann::json json = nlohmann::json::parse(file, nullptr, false); + fclose(file); + for(int i=0;i<(int)json.at(choice).size();i++) { + std::string type = json.at(choice).at(i).at("type"); + + if(type == "deleteFile") { + std::string file = json.at(choice).at(i).at("file"); + std::string message = json.at(choice).at(i).at("message"); + download::deleteFileList(file, message); + + } else if(type == "downloadFile") { + std::string file = json.at(choice).at(i).at("file"); + std::string output = json.at(choice).at(i).at("output"); + std::string message = json.at(choice).at(i).at("message"); + download::downloadFile(file, output, message); + + } else if(type == "downloadRelease") { + std::string repo = json.at(choice).at(i).at("repo"); + std::string file = json.at(choice).at(i).at("file"); + std::string output = json.at(choice).at(i).at("output"); + std::string message = json.at(choice).at(i).at("message"); + download::downloadRelease(repo, file, output, message); + + } else if(type == "extractFile") { + std::string file = json.at(choice).at(i).at("file"); + std::string input = json.at(choice).at(i).at("input"); + std::string output = json.at(choice).at(i).at("output"); + std::string message = json.at(choice).at(i).at("message"); + download::extractFileList(file, input, output, message); + + } else if(type == "installCia") { + std::string file = json.at(choice).at(i).at("file"); + std::string message = json.at(choice).at(i).at("message"); + download::installFileList(file, message); + } + } + doneMsg(); +} + std::vector fileInfo; +std::vector fileInfo2; ScriptList::ScriptList() { dirContents.clear(); @@ -70,8 +135,7 @@ ScriptList::ScriptList() { } } - -void ScriptList::Draw(void) const { +void ScriptList::DrawList(void) const { std::string titleinfo; Gui::DrawTop(); Gui::DrawStringCentered(0, 2, 0.7f, TextColor, "Universal-Updater", 400); @@ -88,10 +152,35 @@ void ScriptList::Draw(void) const { } +void ScriptList::Draw(void) const { + if (mode == 0){ + DrawList(); + } else if (mode == 1) { + DrawSingleObject(); + } +} -void ScriptList::Logic(u32 hDown, u32 hHeld, touchPosition touch) { +void ScriptList::DrawSingleObject(void) const { + std::string info; + Gui::DrawTop(); + Gui::DrawStringCentered(0, 2, 0.7f, TextColor, "Universal-Updater", 400); + Gui::DrawBottom(); + for(int i=0;i 0) { selection--; @@ -122,9 +210,69 @@ void ScriptList::Logic(u32 hDown, u32 hHeld, touchPosition touch) { } } + if (hDown & KEY_A) { + currentFile = dirContents[selection].name; + fileInfo2 = parseObjects(currentFile); + selection = 0; + mode = 1; + } + if(selection < screenPos) { screenPos = selection; } else if (selection > screenPos + ENTRIES_PER_SCREEN - 1) { screenPos = selection - ENTRIES_PER_SCREEN + 1; } +} + +void ScriptList::SelectFunction(u32 hDown, u32 hHeld) { + if (keyRepeatDelay) keyRepeatDelay--; + if (hHeld & KEY_DOWN && !keyRepeatDelay) { + if (selection2 < (int)fileInfo2.size()-1) { + selection2++; + } else { + selection2 = 0; + } + if (fastMode == true) { + keyRepeatDelay = 3; + } else if (fastMode == false){ + keyRepeatDelay = 6; + } + } + if (hHeld & KEY_UP && !keyRepeatDelay) { + if (selection2 > 0) { + selection2--; + } else { + selection2 = (int)fileInfo2.size()-1; + } + if (fastMode == true) { + keyRepeatDelay = 3; + } else if (fastMode == false){ + keyRepeatDelay = 6; + } + } + if (hDown & KEY_A) { + choice = fileInfo2[selection2]; + runFunctions(); + } + + if (hDown & KEY_B) { + selection2 = 0; + fileInfo2.clear(); + mode = 0; + } + + if(selection2 < screenPos2) { + screenPos2 = selection2; + } else if (selection2 > screenPos2 + ENTRIES_PER_SCREEN - 1) { + screenPos2 = selection2 - ENTRIES_PER_SCREEN + 1; + } +} + + +void ScriptList::Logic(u32 hDown, u32 hHeld, touchPosition touch) { + if (mode == 0) { + ListSelection(hDown, hHeld); + } else if (mode == 1) { + SelectFunction(hDown, hHeld); + } } \ No newline at end of file diff --git a/source/utils/cia.c b/source/utils/cia.c index f1005da..3d077d3 100644 --- a/source/utils/cia.c +++ b/source/utils/cia.c @@ -1,6 +1,6 @@ #include "utils/cia.h" -bool updatingSelf; +bool updatingSelf = false; static Result CIA_LaunchTitle(u64 titleId, FS_MediaType mediaType) { Result ret = 0; @@ -74,6 +74,12 @@ Result installCia(const char * ciaPath) AM_TitleEntry info; Result ret = 0; + ret = amInit(); + if (R_FAILED(ret)) { + printf("Error in:\namInit();\n"); + return ret; + } + FS_MediaType media = MEDIATYPE_SD; ret = openFile(&fileHandle, ciaPath, false); @@ -131,6 +137,6 @@ Result installCia(const char * ciaPath) if (R_FAILED(ret = CIA_LaunchTitle(info.titleID, MEDIATYPE_SD))) return ret; } - + amExit(); return 0; } \ No newline at end of file