Add downloadToRAM as a param.

To-Do: Maybe cleanup or something?
This commit is contained in:
SuperSaiyajinStackZ
2019-12-22 16:14:55 +01:00
parent 9e87033aff
commit 11e7664706
7 changed files with 62 additions and 129 deletions
+5 -20
View File
@@ -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<ListEntry> getList(std::string repo, std::string path);
std::string getLatestCommit(std::string repo, std::string array, std::string item, bool downloadToRAM);
+2 -2
View File
@@ -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);
+39 -95
View File
@@ -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<ListEntry> getList(std::string repo, std::string path)
{
Result ret = 0;
void *socubuf = memalign(0x1000, 0x100000);
std::vector<ListEntry> 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<ListEntry> jsonItems;
json parsedAPI = json::parse(result_buf);
for(uint i=0;i<parsedAPI.size();i++) {
ListEntry listEntry;
if (parsedAPI[i]["name"].is_string()) {
listEntry.name = parsedAPI[i]["name"];
}
if (parsedAPI[i]["download_url"].is_string()) {
listEntry.downloadUrl = parsedAPI[i]["download_url"];
}
if (parsedAPI[i]["path"].is_string()) {
listEntry.sdPath = "sdmc:/";
listEntry.sdPath += parsedAPI[i]["path"];
listEntry.path = parsedAPI[i]["path"];
size_t pos;
while ((pos = listEntry.path.find(" ")) != std::string::npos) {
listEntry.path.replace(pos, 1, "%20");
}
}
jsonItems.push_back(listEntry);
}
socExit();
free(result_buf);
free(socubuf);
result_buf = NULL;
result_sz = 0;
result_written = 0;
return jsonItems;
}
void displayProgressBar() {
char str[256];
while(showProgressBar) {
+2 -2
View File
@@ -100,7 +100,7 @@ ScriptBrowse::ScriptBrowse() {
DisplayMsg(Lang::get("GETTING_SCRIPT_LIST"));
// Get repo info
downloadToFile("https://github.com/Universal-Team/extras/raw/scripts/info/scriptInfo.json", metaFile);
downloadToFile("https://github.com/Universal-Team/extras/raw/scripts/info/scriptInfo.json", metaFile, true);
FILE* file = fopen(metaFile, "r");
if(file) infoJson = nlohmann::json::parse(file, nullptr, false);
fclose(file);
@@ -230,7 +230,7 @@ void ScriptBrowse::Logic(u32 hDown, u32 hHeld, touchPosition touch) {
}
DisplayMsg(fileName);
downloadToFile(infoJson[selection]["url"], Config::ScriptPath + titleFix + ".json");
downloadToFile(infoJson[selection]["url"], Config::ScriptPath + titleFix + ".json", true);
infoJson[selection]["curRevision"] = infoJson[selection]["revision"];
}
}
+8 -4
View File
@@ -130,17 +130,19 @@ void runFunctions(nlohmann::json &json) {
if(!missing) ScriptHelper::removeFile(file, message);
} else if(type == "downloadFile") {
bool missing = false;
bool missing = false, downloadToRAM = false;
std::string file, output, message;
if(json.at(choice).at(i).contains("file")) file = json.at(choice).at(i).at("file");
else missing = true;
if(json.at(choice).at(i).contains("output")) output = json.at(choice).at(i).at("output");
else missing = true;
if(json.at(choice).at(i).contains("downloadToRAM") && json.at(choice).at(i).at("downloadToRAM").is_boolean())
downloadToRAM = json.at(choice).at(i).at("downloadToRAM");
if(json.at(choice).at(i).contains("message")) message = json.at(choice).at(i).at("message");
if(!missing) ScriptHelper::downloadFile(file, output, message);
if(!missing) ScriptHelper::downloadFile(file, output, downloadToRAM, message);
} else if(type == "downloadRelease") {
bool missing = false, includePrereleases = false;
bool missing = false, includePrereleases = false, downloadToRAM = false;
std::string repo, file, output, message;
if(json.at(choice).at(i).contains("repo")) repo = json.at(choice).at(i).at("repo");
else missing = true;
@@ -150,8 +152,10 @@ void runFunctions(nlohmann::json &json) {
else missing = true;
if(json.at(choice).at(i).contains("includePrereleases") && json.at(choice).at(i).at("includePrereleases").is_boolean())
includePrereleases = json.at(choice).at(i).at("includePrereleases");
if(json.at(choice).at(i).contains("downloadToRAM") && json.at(choice).at(i).at("downloadToRAM").is_boolean())
downloadToRAM = json.at(choice).at(i).at("downloadToRAM");
if(json.at(choice).at(i).contains("message")) message = json.at(choice).at(i).at("message");
if(!missing) ScriptHelper::downloadRelease(repo, file, output, includePrereleases, message);
if(!missing) ScriptHelper::downloadRelease(repo, file, output, includePrereleases, downloadToRAM, message);
} else if(type == "extractFile") {
bool missing = false;
+2 -2
View File
@@ -73,7 +73,7 @@ std::vector<std::string> 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;
+4 -4
View File
@@ -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;