diff --git a/include/download/download.hpp b/include/download/download.hpp index 72ece2a..4b679d5 100644 --- a/include/download/download.hpp +++ b/include/download/download.hpp @@ -39,15 +39,8 @@ enum DownloadError { DL_ERROR_GIT, }; -struct ListEntry { - std::string downloadUrl; - std::string name; - std::string path; - std::string sdPath; -}; - -Result downloadToFile(std::string url, std::string path); -Result downloadFromRelease(std::string url, std::string asset, std::string path, bool includePrereleases); +Result downloadToFile(std::string url, std::string path, bool downloadToRAM); +Result downloadFromRelease(std::string url, std::string asset, std::string path, bool includePrereleases, bool downloadToRAM); void displayProgressBar(); @@ -76,7 +69,7 @@ void doneMsg(void); * item is that to get from the API. (Ex. "tag_name") * @return the string from the API. */ -std::string getLatestRelease(std::string repo, std::string item); +std::string getLatestRelease(std::string repo, std::string item, bool downloadToRAM); /** * Get info from the GitHub API about a Commit. @@ -84,7 +77,7 @@ std::string getLatestRelease(std::string repo, std::string item); * item is that to get from the API. (Ex. "sha") * @return the string from the API. */ -std::string getLatestCommit(std::string repo, std::string item); +std::string getLatestCommit(std::string repo, std::string item, bool downloadToRAM); /** * Get info from the GitHub API about a Commit. @@ -93,12 +86,4 @@ std::string getLatestCommit(std::string repo, std::string item); * item is that to get from the API. (Ex. "message") * @return the string from the API. */ -std::string getLatestCommit(std::string repo, std::string array, std::string item); - -/** - * Get a GitHub directory's contents with the GitHub API. - * repo is where to get from. (Ex. "DS-Homebrew/twlmenu-extras") - * path is the path within the repo (Ex. "contents/_nds/TWiLightMenu/dsimenu/themes") - * @return the string from the API. - */ -std::vector getList(std::string repo, std::string path); \ No newline at end of file +std::string getLatestCommit(std::string repo, std::string array, std::string item, bool downloadToRAM); \ No newline at end of file diff --git a/include/utils/scriptHelper.hpp b/include/utils/scriptHelper.hpp index 48482c5..65dc057 100644 --- a/include/utils/scriptHelper.hpp +++ b/include/utils/scriptHelper.hpp @@ -35,8 +35,8 @@ namespace ScriptHelper { int getNum(nlohmann::json json, const std::string &key, const std::string &key2); // Script Functions. - void downloadRelease(std::string repo, std::string file, std::string output, bool includePrereleases, std::string message); - void downloadFile(std::string file, std::string output, std::string message); + void downloadRelease(std::string repo, std::string file, std::string output, bool includePrereleases, bool downloadToRAM, std::string message); + void downloadFile(std::string file, std::string output, bool downloadToRAM, std::string message); void removeFile(std::string file, std::string message); void installFile(std::string file, std::string message); diff --git a/source/download/download.cpp b/source/download/download.cpp index 103956b..0c26442 100644 --- a/source/download/download.cpp +++ b/source/download/download.cpp @@ -161,9 +161,11 @@ static Result setupContextForDirectToFileDownload(CURL *hnd, const char * url) return 0; } -Result downloadToFile(std::string url, std::string path) +Result downloadToFile(std::string url, std::string path, bool downloadToRAM) { Result ret = 0; + u64 offset = 0; + u32 bytesWritten = 0; printf("Downloading from:\n%s\nto:\n%s\n", url.c_str(), path.c_str()); void *socubuf = memalign(0x1000, 0x100000); @@ -196,7 +198,11 @@ Result downloadToFile(std::string url, std::string path) result_fileHandle = &fileHandle; CURL *hnd = curl_easy_init(); - ret = setupContextForDirectToFileDownload(hnd, url.c_str()); + if (downloadToRAM == true) { + ret = setupContext(hnd, url.c_str()); + } else { + ret = setupContextForDirectToFileDownload(hnd, url.c_str()); + } if (ret != 0) { socExit(); free(result_buf); @@ -226,6 +232,9 @@ Result downloadToFile(std::string url, std::string path) FSFILE_Close(fileHandle); return -1; } + if (downloadToRAM == true) { + FSFILE_Write(fileHandle, &bytesWritten, offset, result_buf, result_written, 0); + } u64 endTime = osGetTime(); u64 totalTime = endTime - startTime; @@ -242,7 +251,7 @@ Result downloadToFile(std::string url, std::string path) return 0; } -Result downloadFromRelease(std::string url, std::string asset, std::string path, bool includePrereleases) +Result downloadFromRelease(std::string url, std::string asset, std::string path, bool includePrereleases, bool downloadToRAM) { Result ret = 0; void *socubuf = memalign(0x1000, 0x100000); @@ -272,7 +281,12 @@ Result downloadFromRelease(std::string url, std::string asset, std::string path, printf("Crafted API url:\n%s\n", apiurl.c_str()); CURL *hnd = curl_easy_init(); - ret = setupContext(hnd, apiurl.c_str()); + + if (downloadToRAM == true) { + ret = setupContext(hnd, apiurl.c_str()); + } else { + ret = setupContextForDirectToFileDownload(hnd, apiurl.c_str()); + } if (ret != 0) { socExit(); free(result_buf); @@ -325,7 +339,7 @@ Result downloadFromRelease(std::string url, std::string asset, std::string path, if (assetUrl.empty()) ret = DL_ERROR_GIT; else - ret = downloadToFile(assetUrl, path); + ret = downloadToFile(assetUrl, path, downloadToRAM); return ret; } @@ -373,7 +387,7 @@ void notConnectedMsg(void) { } } -std::string getLatestRelease(std::string repo, std::string item) +std::string getLatestRelease(std::string repo, std::string item, bool downloadToRAM) { Result ret = 0; void *socubuf = memalign(0x1000, 0x100000); @@ -394,7 +408,13 @@ std::string getLatestRelease(std::string repo, std::string item) std::string apiurl = apiurlStream.str(); CURL *hnd = curl_easy_init(); - ret = setupContext(hnd, apiurl.c_str()); + + if (downloadToRAM == true) { + ret = setupContext(hnd, apiurl.c_str()); + } else { + ret = setupContextForDirectToFileDownload(hnd, apiurl.c_str()); + } + if (ret != 0) { socExit(); free(result_buf); @@ -437,7 +457,7 @@ std::string getLatestRelease(std::string repo, std::string item) return jsonItem; } -std::string getLatestCommit(std::string repo, std::string item) +std::string getLatestCommit(std::string repo, std::string item, bool downloadToRAM) { Result ret = 0; void *socubuf = memalign(0x1000, 0x100000); @@ -458,7 +478,11 @@ std::string getLatestCommit(std::string repo, std::string item) std::string apiurl = apiurlStream.str(); CURL *hnd = curl_easy_init(); - ret = setupContext(hnd, apiurl.c_str()); + if (downloadToRAM == true) { + ret = setupContext(hnd, apiurl.c_str()); + } else { + ret = setupContextForDirectToFileDownload(hnd, apiurl.c_str()); + } if (ret != 0) { socExit(); free(result_buf); @@ -501,7 +525,7 @@ std::string getLatestCommit(std::string repo, std::string item) return jsonItem; } -std::string getLatestCommit(std::string repo, std::string array, std::string item) +std::string getLatestCommit(std::string repo, std::string array, std::string item, bool downloadToRAM) { Result ret = 0; void *socubuf = memalign(0x1000, 0x100000); @@ -522,7 +546,11 @@ std::string getLatestCommit(std::string repo, std::string array, std::string ite std::string apiurl = apiurlStream.str(); CURL *hnd = curl_easy_init(); - ret = setupContext(hnd, apiurl.c_str()); + if (downloadToRAM == true) { + ret = setupContext(hnd, apiurl.c_str()); + } else { + ret = setupContextForDirectToFileDownload(hnd, apiurl.c_str()); + } if (ret != 0) { socExit(); free(result_buf); @@ -565,90 +593,6 @@ std::string getLatestCommit(std::string repo, std::string array, std::string ite return jsonItem; } -std::vector getList(std::string repo, std::string path) -{ - Result ret = 0; - void *socubuf = memalign(0x1000, 0x100000); - std::vector emptyVector; - if (!socubuf) - { - return emptyVector; - } - - ret = socInit((u32*)socubuf, 0x100000); - if (R_FAILED(ret)) - { - free(socubuf); - return emptyVector; - } - - std::stringstream apiurlStream; - apiurlStream << "https://api.github.com/repos/" << repo << "/contents/" << path; - std::string apiurl = apiurlStream.str(); - - CURL *hnd = curl_easy_init(); - ret = setupContext(hnd, apiurl.c_str()); - if (ret != 0) { - socExit(); - free(result_buf); - free(socubuf); - result_buf = NULL; - result_sz = 0; - result_written = 0; - return emptyVector; - } - - CURLcode cres = curl_easy_perform(hnd); - curl_easy_cleanup(hnd); - char* newbuf = (char*)realloc(result_buf, result_written + 1); - result_buf = newbuf; - result_buf[result_written] = 0; //nullbyte to end it as a proper C style string - - if (cres != CURLE_OK) { - printf("Error in:\ncurl\n"); - socExit(); - free(result_buf); - free(socubuf); - result_buf = NULL; - result_sz = 0; - result_written = 0; - - return emptyVector; - } - - std::vector jsonItems; - json parsedAPI = json::parse(result_buf); - for(uint i=0;i tinyDBList; TinyDB::TinyDB() { DisplayMsg(Lang::get("TINYDB_DOWNLOADING")); - downloadToFile("https://tinydb.eiphax.tech/api/universal-updater.json?raw=true", tinyDBFile); + downloadToFile("https://tinydb.eiphax.tech/api/universal-updater.json?raw=true", tinyDBFile, true); tinyDBList = parseObjects(); selectedOption = tinyDBList[0]; } @@ -244,7 +244,7 @@ void TinyDB::execute() { if(tinyDBJson.at(selectedOption).at("script").at(i).contains("output")) output = tinyDBJson.at(selectedOption).at("script").at(i).at("output"); else missing = true; if(tinyDBJson.at(selectedOption).at("script").at(i).contains("message")) message = tinyDBJson.at(selectedOption).at("script").at(i).at("message"); - if(!missing) ScriptHelper::downloadFile(file, output, message); + if(!missing) ScriptHelper::downloadFile(file, output, false, message); } else if(type == "installCia") { bool missing = false; diff --git a/source/utils/scriptHelper.cpp b/source/utils/scriptHelper.cpp index ca51903..10ae8b7 100644 --- a/source/utils/scriptHelper.cpp +++ b/source/utils/scriptHelper.cpp @@ -67,12 +67,12 @@ int ScriptHelper::getNum(nlohmann::json json, const std::string &key, const std: } // Download from a Github Release. -void ScriptHelper::downloadRelease(std::string repo, std::string file, std::string output, bool includePrereleases, std::string message) { +void ScriptHelper::downloadRelease(std::string repo, std::string file, std::string output, bool includePrereleases, bool downloadToRAM, 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, includePrereleases) != 0) { + if (downloadFromRelease("https://github.com/" + repo, file, output, includePrereleases, downloadToRAM) != 0) { showProgressBar = false; downloadFailed(); return; @@ -81,12 +81,12 @@ void ScriptHelper::downloadRelease(std::string repo, std::string file, std::stri } // Download a File from everywhere. -void ScriptHelper::downloadFile(std::string file, std::string output, std::string message) { +void ScriptHelper::downloadFile(std::string file, std::string output, bool downloadToRAM, std::string message) { snprintf(progressBarMsg, sizeof(progressBarMsg), message.c_str()); showProgressBar = true; progressBarType = 0; Threads::create((ThreadFunc)displayProgressBar); - if (downloadToFile(file, output) != 0) { + if (downloadToFile(file, output, downloadToRAM) != 0) { showProgressBar = false; downloadFailed(); return;