Working stuff! :) Last steps -> Add error checking.

This commit is contained in:
VoltZ
2019-11-02 02:01:53 +01:00
committed by GitHub
parent da49c38c9f
commit 44c31c46d5
5 changed files with 223 additions and 51 deletions
+10 -4
View File
@@ -67,6 +67,9 @@ void notConnectedMsg(void);
*/ */
void notImplemented(void); void notImplemented(void);
// Display the done msg.
void doneMsg(void);
/** /**
* Get info from the GitHub API about a Release. * Get info from the GitHub API about a Release.
* repo is where to get from. (Ex. "RocketRobz/TWiLightMenu") * 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<ThemeEntry> getThemeList(std::string repo, std::string path); std::vector<ThemeEntry> getThemeList(std::string repo, std::string path);
/** namespace download {
* Update nds-bootstrap to the latest build. 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 updateBootstrap(bool nightly); 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);
}
+10 -2
View File
@@ -37,15 +37,23 @@ public:
void Draw(void) const override; void Draw(void) const override;
void Logic(u32 hDown, u32 hHeld, touchPosition touch) override; void Logic(u32 hDown, u32 hHeld, touchPosition touch) override;
ScriptList(); ScriptList();
void showParsedObjects(void) const;
private: 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<DirEntry> dirContents; std::vector<DirEntry> dirContents;
mutable int screenPos = 0; mutable int screenPos = 0;
mutable int selection = 0; mutable int selection = 0;
mutable int screenPos2 = 0;
mutable int selection2 = 0;
int keyRepeatDelay = 0; int keyRepeatDelay = 0;
int fastMode = false; int fastMode = false;
bool dirChanged = true;
}; };
#endif #endif
+43 -39
View File
@@ -626,45 +626,49 @@ void displayProgressBar() {
} }
} }
void download::downloadRelease(std::string repo, std::string file, std::string output, std::string message) {
void updateBootstrap(bool nightly) { snprintf(progressBarMsg, sizeof(progressBarMsg), message.c_str());
if(nightly) { showProgressBar = true;
snprintf(progressBarMsg, sizeof(progressBarMsg), (Lang::get("DOWNLOAD_NDSBOOTSTRAP_NIGHTLY")).c_str()); progressBarType = 0;
showProgressBar = true; Threads::create((ThreadFunc)displayProgressBar);
progressBarType = 0; if (downloadFromRelease("https://github.com/" + repo, file, output) != 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/");
showProgressBar = false; showProgressBar = false;
downloadFailed();
deleteFile("sdmc:/nds-bootstrap-nightly.7z"); return;
} 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");
} }
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;
} }
+152 -4
View File
@@ -37,10 +37,13 @@
#define ENTRIES_PER_SCREEN 3 #define ENTRIES_PER_SCREEN 3
// Information like Title and Description.
struct Info { struct Info {
std::string title; std::string title;
std::string description; std::string description;
}; };
std::string choice;
std::string currentFile;
Info parseInfo(std::string fileName) { Info parseInfo(std::string fileName) {
FILE* file = fopen(fileName.c_str(), "rt"); FILE* file = fopen(fileName.c_str(), "rt");
@@ -58,8 +61,70 @@ Info parseInfo(std::string fileName) {
return info; return info;
} }
// Objects like Release or Nightly.
std::vector<std::string> 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<std::string> 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<Info> fileInfo; std::vector<Info> fileInfo;
std::vector<std::string> fileInfo2;
ScriptList::ScriptList() { ScriptList::ScriptList() {
dirContents.clear(); dirContents.clear();
@@ -70,8 +135,7 @@ ScriptList::ScriptList() {
} }
} }
void ScriptList::DrawList(void) const {
void ScriptList::Draw(void) const {
std::string titleinfo; std::string titleinfo;
Gui::DrawTop(); Gui::DrawTop();
Gui::DrawStringCentered(0, 2, 0.7f, TextColor, "Universal-Updater", 400); 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<ENTRIES_PER_SCREEN && i<(int)fileInfo2.size();i++) {
info = fileInfo2[screenPos2 + i];
if(screenPos2 + i == selection2) {
Gui::Draw_Rect(0, 40+(i*57), 320, 45, SelectedColor);
} else {
Gui::Draw_Rect(0, 40+(i*57), 320, 45, UnselectedColor);
}
Gui::DrawString(0, 40+(i*57), 0.7f, WHITE, info, 320);
}
}
void ScriptList::ListSelection(u32 hDown, u32 hHeld) {
if (keyRepeatDelay) keyRepeatDelay--; if (keyRepeatDelay) keyRepeatDelay--;
if (hDown & KEY_B) { if (hDown & KEY_B) {
fileInfo.clear();
Gui::screenBack(); Gui::screenBack();
return; return;
} }
@@ -108,7 +197,6 @@ void ScriptList::Logic(u32 hDown, u32 hHeld, touchPosition touch) {
keyRepeatDelay = 6; keyRepeatDelay = 6;
} }
} }
if (hHeld & KEY_UP && !keyRepeatDelay) { if (hHeld & KEY_UP && !keyRepeatDelay) {
if (selection > 0) { if (selection > 0) {
selection--; 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) { if(selection < screenPos) {
screenPos = selection; screenPos = selection;
} else if (selection > screenPos + ENTRIES_PER_SCREEN - 1) { } else if (selection > screenPos + ENTRIES_PER_SCREEN - 1) {
screenPos = selection - 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);
}
}
+8 -2
View File
@@ -1,6 +1,6 @@
#include "utils/cia.h" #include "utils/cia.h"
bool updatingSelf; bool updatingSelf = false;
static Result CIA_LaunchTitle(u64 titleId, FS_MediaType mediaType) { static Result CIA_LaunchTitle(u64 titleId, FS_MediaType mediaType) {
Result ret = 0; Result ret = 0;
@@ -74,6 +74,12 @@ Result installCia(const char * ciaPath)
AM_TitleEntry info; AM_TitleEntry info;
Result ret = 0; Result ret = 0;
ret = amInit();
if (R_FAILED(ret)) {
printf("Error in:\namInit();\n");
return ret;
}
FS_MediaType media = MEDIATYPE_SD; FS_MediaType media = MEDIATYPE_SD;
ret = openFile(&fileHandle, ciaPath, false); ret = openFile(&fileHandle, ciaPath, false);
@@ -131,6 +137,6 @@ Result installCia(const char * ciaPath)
if (R_FAILED(ret = CIA_LaunchTitle(info.titleID, MEDIATYPE_SD))) if (R_FAILED(ret = CIA_LaunchTitle(info.titleID, MEDIATYPE_SD)))
return ret; return ret;
} }
amExit();
return 0; return 0;
} }