From 86b6adef8f3ae67d43c419c21c494ffd5f965925 Mon Sep 17 00:00:00 2001 From: StackZ <47382115+SuperSaiyajinStackZ@users.noreply.github.com> Date: Tue, 30 Jun 2020 23:20:47 +0200 Subject: [PATCH] Begin to add variables for 3DSX & NDS. --- include/utils/common.hpp | 4 +++- include/utils/config.hpp | 11 ++++++++++- source/utils/config.cpp | 30 +++++++++++++++++++++++++----- source/utils/scriptHelper.cpp | 13 +++++++++++-- source/utils/store.cpp | 9 +++++---- 5 files changed, 54 insertions(+), 13 deletions(-) diff --git a/include/utils/common.hpp b/include/utils/common.hpp index ce74db8..286083d 100644 --- a/include/utils/common.hpp +++ b/include/utils/common.hpp @@ -72,4 +72,6 @@ extern char * arg0; #define STORE_PATH "sdmc:/3ds/Universal-Updater/stores/" // Default Store path. #define ENTRIES_PER_SCREEN 3 #define ENTRIES_PER_LIST 7 -#define metaFile "sdmc:/3ds/Universal-Updater/ScriptInfo.json" \ No newline at end of file +#define metaFile "sdmc:/3ds/Universal-Updater/ScriptInfo.json" +#define _3DSX_PATH "sdmc:/3ds" +#define _NDS_PATH "sdmc:" \ No newline at end of file diff --git a/include/utils/config.hpp b/include/utils/config.hpp index 3c1f83b..e8fb784 100644 --- a/include/utils/config.hpp +++ b/include/utils/config.hpp @@ -124,6 +124,12 @@ public: bool showSpeed() { return this->v_showSpeed; } void showSpeed(bool v) { this->v_showSpeed = v; if (!this->changesMade) this->changesMade = true; } + // Variables. + std::string _3dsxpath() { return this->v_3dsx_install_path; } + void _3dsxpath(std::string v) { this->v_3dsx_install_path = v; if (!this->changesMade) this->changesMade = true; } + std::string ndspath() { return this->v_nds_install_path; } + void ndspath(std::string v) { this->v_nds_install_path = v; if (!this->changesMade) this->changesMade = true; } + // Mainly helper. bool getBool(const std::string &key); void setBool(const std::string &key, bool v); @@ -134,7 +140,7 @@ public: private: nlohmann::json json; // Our private JSON file. bool changesMade = false, initialChanges = false; - int configVersion = 1; + int configVersion = 2; // Color variables and more. u32 v_barColor, v_topBG, v_bottomBG, v_textColor, v_buttonColor, v_selectedColor, v_unselectedColor, v_progressbarColor, @@ -142,6 +148,9 @@ private: std::string v_scriptPath, v_musicPath, v_storePath, v_autobootFile, v_language; int v_langPath, v_viewMode, v_autoboot, v_keyDelay; bool v_logging, v_useBars, v_screenFade, v_progressDisplay, v_firstStartup, v_useScriptColor, v_showSpeed; + + // Some variables. + std::string v_3dsx_install_path, v_nds_install_path; }; #endif \ No newline at end of file diff --git a/source/utils/config.cpp b/source/utils/config.cpp index eb2d874..8155aad 100644 --- a/source/utils/config.cpp +++ b/source/utils/config.cpp @@ -30,9 +30,12 @@ #include #include -// Used to add missing stuff for the JSON. No new things for now. +// Used to add missing stuff for the JSON. void Config::addMissingThings() { - + if (this->json["VERSION"] < 2) { + this->setString("3DSX_PATH", _3DSX_PATH); + this->setString("NDS_PATH", _NDS_PATH); + } } // In case it doesn't exist. @@ -69,10 +72,13 @@ void Config::initialize() { this->setBool("FIRST_STARTUP", true); this->setBool("USE_SCRIPT_COLORS", true); this->setBool("SHOW_SPEED", false); + this->setString("3DSX_PATH", _3DSX_PATH); + this->setString("NDS_PATH", _NDS_PATH); this->setInt("VERSION", this->configVersion); // Write to file. - fwrite(this->json.dump(1, '\t').c_str(), 1, this->json.dump(1, '\t').size(), file); + const std::string dump = this->json.dump(1, '\t'); + fwrite(dump.c_str(), 1, this->json.dump(1, '\t').size(), file); fclose(file); // Now we have the file and can properly access it. } @@ -93,7 +99,6 @@ Config::Config() { // Here we add the missing things. if (this->json["VERSION"] < this->configVersion) { this->addMissingThings(); - this->save(); } if (!this->json.contains("BARCOLOR")) { @@ -265,6 +270,18 @@ Config::Config() { this->showSpeed(this->getBool("SHOW_SPEED")); } + if (!this->json.contains("3DSX_PATH")) { + this->_3dsxpath(_3DSX_PATH); + } else { + this->_3dsxpath(this->getString("3DSX_PATH")); + } + + if (!this->json.contains("NDS_PATH")) { + this->ndspath(_NDS_PATH); + } else { + this->ndspath(this->getString("NDS_PATH")); + } + this->changesMade = false; // No changes made yet. } @@ -302,8 +319,11 @@ void Config::save() { this->setBool("FIRST_STARTUP", this->firstStartup()); this->setBool("USE_SCRIPT_COLORS", this->useScriptColor()); this->setBool("SHOW_SPEED", this->showSpeed()); + this->setString("3DSX_PATH", this->_3dsxpath()); + this->setString("NDS_PATH", this->ndspath()); // Write changes to file. - fwrite(this->json.dump(1, '\t').c_str(), 1, this->json.dump(1, '\t').size(), file); + const std::string dump = this->json.dump(1, '\t'); + fwrite(dump.c_str(), 1, this->json.dump(1, '\t').size(), file); fclose(file); } } diff --git a/source/utils/scriptHelper.cpp b/source/utils/scriptHelper.cpp index 6e64f2e..4472adb 100644 --- a/source/utils/scriptHelper.cpp +++ b/source/utils/scriptHelper.cpp @@ -34,6 +34,7 @@ #include "thread.hpp" #include +#include #include extern bool showProgressBar; @@ -67,8 +68,12 @@ int ScriptHelper::getNum(nlohmann::json json, const std::string &key, const std: // Download from a Github Release. Result ScriptHelper::downloadRelease(std::string repo, std::string file, std::string output, bool includePrereleases, bool showVersions, std::string message) { + std::string out; + out = std::regex_replace(output, std::regex("%3DSX%"), config->_3dsxpath().c_str()); + out = std::regex_replace(out, std::regex("%NDS%"), config->ndspath().c_str()); + Result ret = NONE; - if (downloadFromRelease("https://github.com/" + repo, file, output, message, includePrereleases, showVersions) != 0) { + if (downloadFromRelease("https://github.com/" + repo, file, out, message, includePrereleases, showVersions) != 0) { showProgressBar = false; downloadFailed(); ret = FAILED_DOWNLOAD; @@ -81,12 +86,16 @@ Result ScriptHelper::downloadRelease(std::string repo, std::string file, std::st // Download a File from everywhere. Result ScriptHelper::downloadFile(std::string file, std::string output, std::string message) { + std::string out; + out = std::regex_replace(output, std::regex("%3DSX%"), config->_3dsxpath().c_str()); + out = std::regex_replace(out, std::regex("%NDS%"), config->ndspath().c_str()); + Result ret = NONE; snprintf(progressBarMsg, sizeof(progressBarMsg), message.c_str()); showProgressBar = true; progressbarType = ProgressBar::Downloading; Threads::create((ThreadFunc)displayProgressBar); - if (downloadToFile(file, output) != 0) { + if (downloadToFile(file, out) != 0) { showProgressBar = false; downloadFailed(); ret = FAILED_DOWNLOAD; diff --git a/source/utils/store.cpp b/source/utils/store.cpp index b402233..558164c 100644 --- a/source/utils/store.cpp +++ b/source/utils/store.cpp @@ -79,12 +79,12 @@ bool Store::updateAvailable(int index) { if (this->updateJSON.contains(this->updateFile)) { if (this->updateJSON[this->updateFile].contains(entry)) { const std::string updateEntry2 = (std::string)this->updateJSON[this->updateFile][entry]; - return strcasecmp(updateEntry.c_str(), updateEntry2.c_str()) > 0; + return strcasecmp(updateEntry.c_str(), updateEntry2.c_str()) > 0; } else { - return true; // Since we do not have this entry there yet. + return false; // Since we do not have this entry there yet. } } else { // Our update json don't have that yet.. so display available. - return true; + return false; } } else { // Since the Store doesn't have that feature. return false; @@ -97,7 +97,8 @@ bool Store::updateAvailable(int index) { void Store::writeToFile(int index) { FILE *file = fopen("sdmc:/3ds/Universal-Updater/updates.json", "w"); this->updateJSON[this->updateFile][this->sortedStore[index].title] = this->sortedStore[index].last_updated; - fwrite(this->updateJSON.dump(1, '\t').c_str(), 1, this->updateJSON.dump(1, '\t').size(), file); + const std::string dump = this->updateJSON.dump(1, '\t'); + fwrite(dump.c_str(), 1, this->updateJSON.dump(1, '\t').size(), file); fclose(file); this->sortedStore[index].updateAvailable = false;