Universal-Updater Full Rewrite based of UniStore v3.0.0. (#51)

* No Nightlies for the Full-Rewrite.

* Initial push, i guess.

* Forgot to push the Test UniStore + T3X...

* Use C2D flags for wrapping and centering

* gitignore t3x correctly

* Remove Test Store and hardcode to `sdmc:/3ds/Universal-Updater/stores/Universal-DB.unistore` for now.

* Is functional now.

* *More special checks and work.*

* const <typename T> &.

* Universal-DB, not Universal DB.

* Derp.

* Make 3DSX, NDS & Archive path configurable.

* Last fixes + Fade out screen on exit.

* See Desc. for more.

- Add QR Code scan for downloading UniStores.
- Add new Graphics.
- Some fixes + improvements.

* Fix search filtering, re-sort after search

* Fix update check

* Clear search items with X, not just reset results

* The next progress.

* PLEASE tell me, this is the only error..

Co-authored-by: Pk11 <epicpkmn11@outlook.com>
This commit is contained in:
StackZ
2020-10-30 03:31:20 +01:00
committed by GitHub
parent 5d38c98698
commit 913475eabf
142 changed files with 13937 additions and 14588 deletions
@@ -24,24 +24,16 @@
* reasonable ways as different from the original version.
*/
#include "colorHelper.hpp"
#include "animation.hpp"
#include "common.hpp"
int ColorHelper::getColorValue(int color, int bgr) {
char colorName[10];
int i;
std::stringstream ss;
/*
Draw the progressbar.
itoa(color, colorName, 16);
std::string colorNamePart(colorName, 2*bgr+2, 2);
ss << std::hex << colorNamePart.c_str();
ss >> i;
return i;
}
std::string ColorHelper::getColorName(int color, int bgr) {
char colorName[10];
int i = getColorValue(color, bgr);
itoa(i, colorName, 10);
return colorName;
const u64 &currentProgress: Const Reference to the current progress.
const u64 &totalProgress: Const Reference to the total progress.
*/
void Animation::DrawProgressBar(const u64 &currentProgress, const u64 &totalProgress) {
Gui::Draw_Rect(30, 120, 340, 30, PROGRESSBAR_OUT_COLOR);
Gui::Draw_Rect(31, 121, (int)(((float)currentProgress / (float)totalProgress) * 338.0f), 28, PROGRESSBAR_IN_COLOR);
}
+42 -14
View File
@@ -1,6 +1,33 @@
#include "cia.hpp"
/*
* This file is part of Universal-Updater
* Copyright (C) 2019-2020 Universal-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
Result CIA_LaunchTitle(u64 titleId, FS_MediaType mediaType) {
#include "cia.hpp"
#include "files.hpp"
Result CIA_LaunchTitle(const u64 &titleId, const FS_MediaType &mediaType) {
Result ret = 0;
u8 param[0x300];
u8 hmac[0x20];
@@ -9,6 +36,7 @@ Result CIA_LaunchTitle(u64 titleId, FS_MediaType mediaType) {
printf("Error In:\nAPT_PrepareToDoApplicationJump");
return ret;
}
if (R_FAILED(ret = APT_DoApplicationJump(param, sizeof(param), hmac))) {
printf("Error In:\nAPT_DoApplicationJump");
return ret;
@@ -17,10 +45,10 @@ Result CIA_LaunchTitle(u64 titleId, FS_MediaType mediaType) {
return 0;
}
Result deletePrevious(u64 titleid, FS_MediaType media) {
Result deletePrevious(const u64 &titleid, const FS_MediaType &media) {
Result ret = 0;
u32 titles_amount = 0;
ret = AM_GetTitleCount(media, &titles_amount);
if (R_FAILED(ret)) {
printf("Error in:\nAM_GetTitleCount\n");
@@ -28,7 +56,8 @@ Result deletePrevious(u64 titleid, FS_MediaType media) {
}
u32 read_titles = 0;
u64 * titleIDs = (u64*)malloc(titles_amount * sizeof(u64));
u64 *titleIDs = (u64 *)malloc(titles_amount * sizeof(u64));
ret = AM_GetTitleList(&read_titles, media, titles_amount, titleIDs);
if (R_FAILED(ret)) {
free(titleIDs);
@@ -44,6 +73,7 @@ Result deletePrevious(u64 titleid, FS_MediaType media) {
}
free(titleIDs);
if (R_FAILED(ret)) {
printf("Error in:\nAM_DeleteAppTitle\n");
return ret;
@@ -52,7 +82,7 @@ Result deletePrevious(u64 titleid, FS_MediaType media) {
return 0;
}
FS_MediaType getTitleDestination(u64 titleId) {
FS_MediaType getTitleDestination(const u64 &titleId) {
u16 platform = (u16) ((titleId >> 48) & 0xFFFF);
u16 category = (u16) ((titleId >> 32) & 0xFFFF);
u8 variation = (u8) (titleId & 0xFF);
@@ -61,10 +91,9 @@ FS_MediaType getTitleDestination(u64 titleId) {
return platform == 0x0003 || (platform == 0x0004 && ((category & 0x8011) != 0 || (category == 0x0000 && variation == 0x02))) ? MEDIATYPE_NAND : MEDIATYPE_SD;
}
// Variables.
u64 installSize = 0, installOffset = 0;
Result installCia(const char * ciaPath, bool updatingSelf) {
Result installCia(const char *ciaPath, const bool &updatingSelf) {
u32 bytes_read = 0, bytes_written;
installSize = 0, installOffset = 0; u64 size = 0;
Handle ciaHandle, fileHandle;
@@ -88,8 +117,7 @@ Result installCia(const char * ciaPath, bool updatingSelf) {
if (!updatingSelf) {
ret = deletePrevious(info.titleID, media);
if (R_FAILED(ret))
return ret;
if (R_FAILED(ret)) return ret;
}
ret = FSFILE_GetSize(fileHandle, &size);
@@ -97,6 +125,7 @@ Result installCia(const char * ciaPath, bool updatingSelf) {
printf("Error in:\nFSFILE_GetSize\n");
return ret;
}
ret = AM_StartCiaInstall(media, &ciaHandle);
if (R_FAILED(ret)) {
printf("Error in:\nAM_StartCiaInstall\n");
@@ -105,9 +134,8 @@ Result installCia(const char * ciaPath, bool updatingSelf) {
u32 toRead = 0x200000;
u8 *buf = new u8[toRead];
if(buf == nullptr) {
return -1;
}
if (!buf) return -1;
installSize = size;
do {
@@ -130,7 +158,7 @@ Result installCia(const char * ciaPath, bool updatingSelf) {
}
if (updatingSelf) {
if (R_FAILED(ret = CIA_LaunchTitle(info.titleID, MEDIATYPE_SD))) return ret;
if (R_FAILED(ret = CIA_LaunchTitle(info.titleID, MEDIATYPE_SD))) return ret;
}
return 0;
+173 -427
View File
@@ -1,427 +1,173 @@
/*
* This file is part of Universal-Updater
* Copyright (C) 2019-2020 Universal-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#include "colorHelper.hpp"
#include "common.hpp"
#include "config.hpp"
#include <citro2d.h>
#include <unistd.h>
// 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);
this->setString("ARCHIVE_PATH", ARCHIVES_DEFAULT);
this->setBool("CITRA", false);
}
}
//Detects system language and is used later to set app language to system language
void Config::sysLang() {
u8 language = 0;
CFGU_GetSystemLanguage(&language);
switch(language) {
case 0:
this->language("jp");
break;
case 1:
this->language("en");
break;
case 2:
this->language("fr");
break;
case 3:
this->language("de");
break;
case 4:
this->language("it");
break;
case 5:
this->language("es");
break;
case 6:
this->language("en"); //Simplified chinese, not translated
break;
case 7:
this->language("en"); //Korean, not translated
break;
case 8:
this->language("nl");
break;
case 9:
this->language("pt");
break;
case 10:
this->language("ru");
break;
case 11:
this->language("en"); //traditional chinese, not translated
break;
}
}
// In case it doesn't exist.
void Config::initialize() {
// Create through fopen "Write".
FILE *file = fopen("sdmc:/3ds/Universal-Updater/Settings.json", "w");
// Set default values.
this->setInt("BARCOLOR", BarColor);
this->setInt("TOPBGCOLOR", TopBGColor);
this->setInt("BOTTOMBGCOLOR", BottomBGColor);
this->setInt("TEXTCOLOR", WHITE);
this->setInt("BUTTON", C2D_Color32(0, 0, 50, 255));
this->setInt("SELECTEDCOLOR", SelectedColordefault);
this->setInt("UNSELECTEDCOLOR", UnselectedColordefault);
this->setString("SCRIPTPATH", SCRIPTS_PATH);
this->setInt("LANGPATH", 0);
this->setInt("VIEWMODE", 0);
this->setInt("PROGRESSBARCOLOR", WHITE);
this->setString("MUSICPATH", MUSIC_PATH);
this->setBool("LOGGING", false);
this->setBool("BARS", true);
this->setInt("AUTOBOOT", 0);
this->setString("STOREPATH", STORE_PATH);
this->setString("AUTOBOOT_FILE", "");
this->setInt("OUTDATED", C2D_Color32(0xfb, 0x5b, 0x5b, 255));
this->setInt("UPTODATE", C2D_Color32(0xa5, 0xdd, 0x81, 255));
this->setInt("NOTFOUND", C2D_Color32(255, 128, 0, 255));
this->setInt("FUTURE", C2D_Color32(255, 255, 0, 255));
this->setInt("KEY_DELAY", 5);
this->setBool("SCREEN_FADE", false);
this->setBool("PROGRESS_DISPLAY", true);
this->sysLang();
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->setString("ARCHIVE_PATH", ARCHIVES_DEFAULT);
this->setBool("CITRA", false);
this->setInt("VERSION", this->configVersion);
// Write to 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.
}
Config::Config() {
if (access("sdmc:/3ds/Universal-Updater/Settings.json", F_OK) != 0 ) {
this->initialize();
}
FILE* file = fopen("sdmc:/3ds/Universal-Updater/Settings.json", "r");
this->json = nlohmann::json::parse(file, nullptr, false);
fclose(file);
if (!this->json.contains("VERSION")) {
// Let us create a new one.
this->initialize();
}
// Here we add the missing things.
if (this->json["VERSION"] < this->configVersion) {
this->addMissingThings();
}
if (!this->json.contains("BARCOLOR")) {
this->barColor(BarColor);
} else {
this->barColor(this->getInt("BARCOLOR"));
}
if (!this->json.contains("TOPBGCOLOR")) {
this->topBG(TopBGColor);
} else {
this->topBG(this->getInt("TOPBGCOLOR"));
}
if (!this->json.contains("BOTTOMBGCOLOR")) {
this->bottomBG(BottomBGColor);
} else {
this->bottomBG(this->getInt("BOTTOMBGCOLOR"));
}
if (!this->json.contains("TEXTCOLOR")) {
this->textColor(WHITE);
} else {
this->textColor(this->getInt("TEXTCOLOR"));
}
if (!this->json.contains("BUTTON")) {
this->buttonColor(C2D_Color32(0, 0, 50, 255));
} else {
this->buttonColor(this->getInt("BUTTON"));
}
if (!this->json.contains("SELECTEDCOLOR")) {
this->selectedColor(SelectedColordefault);
} else {
this->selectedColor(this->getInt("SELECTEDCOLOR"));
}
if (!this->json.contains("UNSELECTEDCOLOR")) {
this->unselectedColor(UnselectedColordefault);
} else {
this->unselectedColor(this->getInt("UNSELECTEDCOLOR"));
}
if (!this->json.contains("SCRIPTPATH")) {
this->scriptPath(SCRIPTS_PATH);
} else {
this->scriptPath(this->getString("SCRIPTPATH"));
}
if (!this->json.contains("LANGPATH")) {
this->langPath(0);
} else {
this->langPath(this->getInt("LANGPATH"));
}
if (!this->json.contains("VIEWMODE")) {
this->viewMode(0);
} else {
this->viewMode(this->getInt("VIEWMODE"));
}
if (!this->json.contains("PROGRESSBARCOLOR")) {
this->progressbarColor(WHITE);
} else {
this->progressbarColor(this->getInt("PROGRESSBARCOLOR"));
}
if (!this->json.contains("MUSICPATH")) {
this->musicPath(MUSIC_PATH);
} else {
this->musicPath(this->getString("MUSICPATH"));
}
if (!this->json.contains("LOGGING")) {
this->logging(false);
} else {
this->logging(this->getBool("LOGGING"));
}
if (!this->json.contains("BARS")) {
this->useBars(true);
} else {
this->useBars(this->getBool("BARS"));
}
if (!this->json.contains("AUTOBOOT")) {
this->autoboot(0);
} else {
this->autoboot(this->getInt("AUTOBOOT"));
}
if (!this->json.contains("STOREPATH")) {
this->storePath(STORE_PATH);
} else {
this->storePath(this->getString("STOREPATH"));
}
if (!this->json.contains("AUTOBOOT_FILE")) {
this->autobootFile("");
} else {
this->autobootFile(this->getString("AUTOBOOT_FILE"));
}
if (!this->json.contains("OUTDATED")) {
this->outdatedColor(C2D_Color32(0xfb, 0x5b, 0x5b, 255));
} else {
this->outdatedColor(this->getInt("OUTDATED"));
}
if (!this->json.contains("UPTODATE")) {
this->uptodateColor(C2D_Color32(0xa5, 0xdd, 0x81, 255));
} else {
this->uptodateColor(this->getInt("UPTODATE"));
}
if (!this->json.contains("NOTFOUND")) {
this->notfoundColor(C2D_Color32(255, 128, 0, 255));
} else {
this->notfoundColor(this->getInt("NOTFOUND"));
}
if (!this->json.contains("FUTURE")) {
this->futureColor(C2D_Color32(255, 255, 0, 255));
} else {
this->futureColor(this->getInt("FUTURE"));
}
if (!this->json.contains("KEY_DELAY")) {
this->keyDelay(5);
} else {
this->keyDelay(this->getInt("KEY_DELAY"));
}
if (!this->json.contains("SCREEN_FADE")) {
this->screenFade(false);
} else {
this->screenFade(this->getBool("SCREEN_FADE"));
}
if (!this->json.contains("PROGRESS_DISPLAY")) {
this->progressDisplay(true);
} else {
this->progressDisplay(this->getBool("PROGRESS_DISPLAY"));
}
if (!this->json.contains("LANGUAGE") || this->json.at("LANGUAGE").is_number()) {
this->sysLang();
this->initialChanges = true;
} else {
this->language(this->getString("LANGUAGE"));
}
if (!this->json.contains("FIRST_STARTUP")) {
this->firstStartup(true);
} else {
this->firstStartup(this->getBool("FIRST_STARTUP"));
}
if (!this->json.contains("USE_SCRIPT_COLORS")) {
this->useScriptColor(true);
} else {
this->useScriptColor(this->getBool("USE_SCRIPT_COLORS"));
}
if (!this->json.contains("SHOW_SPEED")) {
this->showSpeed(false);
} else {
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"));
}
if (!this->json.contains("ARCHIVE_PATH")) {
this->archivepath(ARCHIVES_DEFAULT);
} else {
this->archivepath(this->getString("ARCHIVE_PATH"));
}
if (!this->json.contains("CITRA")) {
this->citra(false);
} else {
this->citra(this->getBool("CITRA"));
}
this->changesMade = false; // No changes made yet.
}
// Write to config if changesMade.
void Config::save() {
if (this->changesMade) {
this->changesMade = false;
FILE *file = fopen("sdmc:/3ds/Universal-Updater/Settings.json", "w");
// Set values.
this->setInt("BARCOLOR", this->barColor());
this->setInt("TOPBGCOLOR", this->topBG());
this->setInt("BOTTOMBGCOLOR", this->bottomBG());
this->setInt("TEXTCOLOR", this->textColor());
this->setInt("BUTTON", this->buttonColor());
this->setInt("SELECTEDCOLOR", this->selectedColor());
this->setInt("UNSELECTEDCOLOR", this->unselectedColor());
this->setString("SCRIPTPATH", this->scriptPath());
this->setInt("LANGPATH", this->langPath());
this->setInt("VIEWMODE", this->viewMode());
this->setInt("PROGRESSBARCOLOR", this->progressbarColor());
this->setString("MUSICPATH", this->musicPath());
this->setBool("LOGGING", this->logging());
this->setBool("BARS", this->useBars());
this->setInt("AUTOBOOT", this->autoboot());
this->setString("STOREPATH", this->storePath());
this->setString("AUTOBOOT_FILE", this->autobootFile());
this->setInt("OUTDATED", this->outdatedColor());
this->setInt("UPTODATE", this->uptodateColor());
this->setInt("NOTFOUND", this->notfoundColor());
this->setInt("FUTURE", this->futureColor());
this->setInt("KEY_DELAY", this->keyDelay());
this->setBool("SCREEN_FADE", this->screenFade());
this->setBool("PROGRESS_DISPLAY", this->progressDisplay());
this->setString("LANGUAGE", this->language());
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());
this->setString("ARCHIVE_PATH", this->archivepath());
this->setBool("CITRA", this->citra());
// Write changes to file.
const std::string dump = this->json.dump(1, '\t');
fwrite(dump.c_str(), 1, this->json.dump(1, '\t').size(), file);
fclose(file);
}
}
// Helper functions.
bool Config::getBool(const std::string &key) {
if (!this->json.contains(key)) {
return false;
}
return this->json.at(key).get_ref<const bool&>();
}
void Config::setBool(const std::string &key, bool v) {
this->json[key] = v;
}
int Config::getInt(const std::string &key) {
if (!this->json.contains(key)) {
return 0;
}
return this->json.at(key).get_ref<const int64_t&>();
}
void Config::setInt(const std::string &key, int v) {
this->json[key] = v;
}
std::string Config::getString(const std::string &key) {
if (!this->json.contains(key)) {
return "";
}
return this->json.at(key).get_ref<const std::string&>();
}
void Config::setString(const std::string &key, const std::string &v) {
this->json[key] = v;
}
/*
* This file is part of Universal-Updater
* Copyright (C) 2019-2020 Universal-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#include "common.hpp"
#include "config.hpp"
#include "json.hpp"
#include <string>
#include <unistd.h>
/*
Detects system language and is used later to set app language to system language.
*/
void Config::sysLang() {
u8 language = 0;
CFGU_GetSystemLanguage(&language);
switch(language) {
case 0:
this->language("jp");
break;
case 1:
this->language("en");
break;
case 2:
this->language("fr");
break;
case 3:
this->language("de");
break;
case 4:
this->language("it");
break;
case 5:
this->language("es");
break;
case 6:
this->language("en"); // Simplified chinese, not translated.
break;
case 7:
this->language("en"); // Korean, not translated.
break;
case 8:
this->language("nl");
break;
case 9:
this->language("pt");
break;
case 10:
this->language("ru");
break;
case 11:
this->language("en"); // traditional chinese, not translated.
break;
}
}
/*
In case it doesn't exist.
*/
void Config::initialize() {
FILE *temp = fopen("sdmc:/3ds/Universal-Updater/Config.json", "w");
char tmp[2] = { '{', '}' };
fwrite(tmp, sizeof(tmp), 1, temp);
fclose(temp);
}
/*
Constructor of the config.
*/
Config::Config() {
if (access("sdmc:/3ds/Universal-Updater/Config.json", F_OK) != 0) {
this->initialize();
}
FILE *file = fopen("sdmc:/3ds/Universal-Updater/Config.json", "r");
this->json = nlohmann::json::parse(file, nullptr, false);
fclose(file);
/* Let us create a new one. */
if (!this->json.contains("Version")) this->initialize();
if (!this->json.contains("Language")) this->sysLang();
else this->language(this->getString("Language"));
if (this->json.contains("LastStore")) this->lastStore(this->getString("LastStore"));
if (this->json.contains("List")) this->list(this->getBool("List"));
if (this->json.contains("AutoUpdate")) this->autoupdate(this->getBool("AutoUpdate"));
if (this->json.contains("_3DSX_Path")) this->_3dsxPath(this->getString("_3DSX_Path"));
if (this->json.contains("NDS_Path")) this->ndsPath(this->getString("NDS_Path"));
if (this->json.contains("Archive_Path")) this->archPath(this->getString("Archive_Path"));
if (this->json.contains("MetaData")) this->metadata(this->getBool("MetaData"));
if (this->json.contains("UpdateCheck")) this->updatecheck(this->getBool("UpdateCheck"));
this->changesMade = false; // No changes made yet.
}
/* Write to config if changesMade. */
void Config::save() {
if (this->changesMade) {
FILE *file = fopen("sdmc:/3ds/Universal-Updater/Config.json", "w");
/* Set values. */
this->setString("Language", this->language());
this->setInt("Version", 1);
this->setString("LastStore", this->lastStore());
this->setBool("List", this->list());
this->setBool("AutoUpdate", this->autoupdate());
this->setString("_3DSX_Path", this->_3dsxPath());
this->setString("NDS_Path", this->ndsPath());
this->setString("Archive_Path", this->archPath());
this->setBool("MetaData", this->metadata());
this->setBool("UpdateCheck", this->updatecheck());
/* Write changes to file. */
const std::string dump = this->json.dump(1, '\t');
fwrite(dump.c_str(), 1, this->json.dump(1, '\t').size(), file);
fclose(file);
}
}
/* Helper functions. */
bool Config::getBool(const std::string &key) {
if (!this->json.contains(key)) return false;
return this->json.at(key).get_ref<const bool&>();
}
void Config::setBool(const std::string &key, bool v) { this->json[key] = v; };
int Config::getInt(const std::string &key) {
if (!this->json.contains(key)) return 0;
return this->json.at(key).get_ref<const int64_t&>();
}
void Config::setInt(const std::string &key, int v) { this->json[key] = v; };
std::string Config::getString(const std::string &key) {
if (!this->json.contains(key)) return "";
return this->json.at(key).get_ref<const std::string&>();
}
void Config::setString(const std::string &key, const std::string &v) { this->json[key] = v; };
+899
View File
@@ -0,0 +1,899 @@
/*
* This file is part of Universal-Updater
* Copyright (C) 2019-2020 Universal-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#include "animation.hpp"
#include "download.hpp"
#include "files.hpp"
#include "json.hpp"
#include "lang.hpp"
#include "scriptUtils.hpp"
#include "stringutils.hpp"
#include <3ds.h>
#include <curl/curl.h>
#include <dirent.h>
#include <malloc.h>
#include <regex>
#include <string>
#include <vector>
#define USER_AGENT APP_TITLE "-" VERSION_STRING
static char *result_buf = nullptr;
static size_t result_sz = 0;
static size_t result_written = 0;
std::vector<std::string> _topText;
std::string jsonName;
#define TIME_IN_US 1
#define TIMETYPE curl_off_t
#define TIMEOPT CURLINFO_TOTAL_TIME_T
#define MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL 3000000
curl_off_t downloadTotal = 1; // Dont initialize with 0 to avoid division by zero later.
curl_off_t downloadNow = 0;
static FILE *downfile = nullptr;
static size_t file_buffer_pos = 0;
static size_t file_toCommit_size = 0;
static char *g_buffers[2] = { nullptr };
static u8 g_index = 0;
static Thread fsCommitThread;
static LightEvent readyToCommit;
static LightEvent waitCommit;
static bool killThread = false;
static bool writeError = false;
#define FILE_ALLOC_SIZE 0x60000
extern int filesExtracted;
extern std::string extractingFile;
char progressBarMsg[128] = "";
bool showProgressBar = false;
ProgressBar progressbarType = ProgressBar::Downloading;
extern u64 extractSize, writeOffset;
extern u64 installSize, installOffset;
static int curlProgress(CURL *hnd,
curl_off_t dltotal, curl_off_t dlnow,
curl_off_t ultotal, curl_off_t ulnow)
{
downloadTotal = dltotal;
downloadNow = dlnow;
return 0;
}
bool filecommit() {
if (!downfile) return false;
fseek(downfile, 0, SEEK_END);
u32 byteswritten = fwrite(g_buffers[!g_index], 1, file_toCommit_size, downfile);
if (byteswritten != file_toCommit_size) return false;
file_toCommit_size = 0;
return true;
}
static void commitToFileThreadFunc(void *args) {
LightEvent_Signal(&waitCommit);
while (true) {
LightEvent_Wait(&readyToCommit);
LightEvent_Clear(&readyToCommit);
if (killThread) threadExit(0);
writeError = !filecommit();
LightEvent_Signal(&waitCommit);
}
}
static size_t file_handle_data(char *ptr, size_t size, size_t nmemb, void *userdata) {
(void)userdata;
const size_t bsz = size * nmemb;
size_t tofill = 0;
if (writeError) return 0;
if (!g_buffers[g_index]) {
LightEvent_Init(&waitCommit, RESET_STICKY);
LightEvent_Init(&readyToCommit, RESET_STICKY);
s32 prio = 0;
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
fsCommitThread = threadCreate(commitToFileThreadFunc, NULL, 0x1000, prio - 1, -2, true);
g_buffers[0] = (char*)memalign(0x1000, FILE_ALLOC_SIZE);
g_buffers[1] = (char*)memalign(0x1000, FILE_ALLOC_SIZE);
if (!fsCommitThread || !g_buffers[0] || !g_buffers[1]) return 0;
}
if (file_buffer_pos + bsz >= FILE_ALLOC_SIZE) {
tofill = FILE_ALLOC_SIZE - file_buffer_pos;
memcpy(g_buffers[g_index] + file_buffer_pos, ptr, tofill);
LightEvent_Wait(&waitCommit);
LightEvent_Clear(&waitCommit);
file_toCommit_size = file_buffer_pos + tofill;
file_buffer_pos = 0;
svcFlushProcessDataCache(CUR_PROCESS_HANDLE, (u32)g_buffers[g_index], file_toCommit_size);
g_index = !g_index;
LightEvent_Signal(&readyToCommit);
}
memcpy(g_buffers[g_index] + file_buffer_pos, ptr + tofill, bsz - tofill);
file_buffer_pos += bsz - tofill;
return bsz;
}
Result downloadToFile(const std::string &url, const std::string &path) {
downloadTotal = 1;
downloadNow = 0;
CURLcode curlResult;
CURL *hnd;
Result retcode = 0;
downloadTotal = 1;
int res;
printf("Downloading from:\n%s\nto:\n%s\n", url.c_str(), path.c_str());
const char *filepath = path.c_str();
void *socubuf = memalign(0x1000, 0x100000);
if (!socubuf) {
retcode = -1;
goto exit;
}
res = socInit((u32 *)socubuf, 0x100000);
if (R_FAILED(res)) {
retcode = res;
goto exit;
}
makeDirs(strdup(filepath));
downfile = fopen(filepath, "wb");
if (!downfile) {
retcode = -2;
goto exit;
}
hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, FILE_ALLOC_SIZE);
curl_easy_setopt(hnd, CURLOPT_URL, url.c_str());
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(hnd, CURLOPT_USERAGENT, USER_AGENT);
curl_easy_setopt(hnd, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(hnd, CURLOPT_FAILONERROR, 1L);
curl_easy_setopt(hnd, CURLOPT_ACCEPT_ENCODING, "gzip");
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(hnd, CURLOPT_XFERINFOFUNCTION, curlProgress);
curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2TLS);
curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, file_handle_data);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(hnd, CURLOPT_STDERR, stdout);
curlResult = curl_easy_perform(hnd);
curl_easy_cleanup(hnd);
if (curlResult != CURLE_OK) {
retcode = -curlResult;
goto exit;
}
LightEvent_Wait(&waitCommit);
LightEvent_Clear(&waitCommit);
file_toCommit_size = file_buffer_pos;
svcFlushProcessDataCache(CUR_PROCESS_HANDLE, (u32)g_buffers[g_index], file_toCommit_size);
g_index = !g_index;
if (!filecommit()) {
retcode = -3;
goto exit;
}
fflush(downfile);
exit:
if (fsCommitThread) {
killThread = true;
LightEvent_Signal(&readyToCommit);
threadJoin(fsCommitThread, U64_MAX);
killThread = false;
fsCommitThread = nullptr;
}
socExit();
if (socubuf) free(socubuf);
if (downfile) {
fclose(downfile);
downfile = nullptr;
}
if (g_buffers[0]) {
free(g_buffers[0]);
g_buffers[0] = nullptr;
}
if (g_buffers[1]) {
free(g_buffers[1]);
g_buffers[1] = nullptr;
}
g_index = 0;
file_buffer_pos = 0;
file_toCommit_size = 0;
writeError = false;
return retcode;
}
/*
following function is from
https://github.com/angelsl/libctrfgh/blob/master/curl_test/src/main.c
*/
static size_t handle_data(char *ptr, size_t size, size_t nmemb, void *userdata) {
(void)userdata;
const size_t bsz = size*nmemb;
if (result_sz == 0 || !result_buf) {
result_sz = 0x1000;
result_buf = (char *)malloc(result_sz);
}
bool need_realloc = false;
while (result_written + bsz > result_sz) {
result_sz <<= 1;
need_realloc = true;
}
if (need_realloc) {
char *new_buf = (char *)realloc(result_buf, result_sz);
if (!new_buf) return 0;
result_buf = new_buf;
}
if (!result_buf) return 0;
memcpy(result_buf + result_written, ptr, bsz);
result_written += bsz;
return bsz;
}
/*
This + Above is Used for No File Write and instead into RAM.
*/
static Result setupContext(CURL *hnd, const char *url) {
curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, 102400L);
curl_easy_setopt(hnd, CURLOPT_URL, url);
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(hnd, CURLOPT_USERAGENT, USER_AGENT);
curl_easy_setopt(hnd, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2TLS);
curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, handle_data);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(hnd, CURLOPT_STDERR, stdout);
return 0;
}
/*
Download a file of a GitHub Release.
const std::string &url: Const Reference to the URL. (https://github.com/Owner/Repo)
const std::string &asset: Const Reference to the Asset. (File.filetype)
const std::string &path: Const Reference, where to store. (sdmc:/File.filetype)
const bool &includePrereleases: Const Reference, if including Pre-Releases.
*/
Result downloadFromRelease(const std::string &url, const std::string &asset, const std::string &path, const bool &includePrereleases) {
Result ret = 0;
CURL *hnd;
void *socubuf = memalign(0x1000, 0x100000);
if (!socubuf) {
return -1;
}
ret = socInit((u32*)socubuf, 0x100000);
if (R_FAILED(ret)) {
free(socubuf);
return ret;
}
std::regex parseUrl("github\\.com\\/(.+)\\/(.+)");
std::smatch result;
regex_search(url, result, parseUrl);
std::string repoOwner = result[1].str(), repoName = result[2].str();
std::stringstream apiurlStream;
apiurlStream << "https://api.github.com/repos/" << repoOwner << "/" << repoName << (includePrereleases ? "/releases" : "/releases/latest");
std::string apiurl = apiurlStream.str();
printf("Downloading latest release from repo:\n%s\nby:\n%s\n", repoName.c_str(), repoOwner.c_str());
printf("Crafted API url:\n%s\n", apiurl.c_str());
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 ret;
}
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 = nullptr;
result_sz = 0;
result_written = 0;
return -1;
}
printf("Looking for asset with matching name:\n%s\n", asset.c_str());
std::string assetUrl;
nlohmann::json parsedAPI = nlohmann::json::parse(result_buf);
if (parsedAPI.size() == 0) return -2; // All were prereleases and those are being ignored.
if (includePrereleases) parsedAPI = parsedAPI[0];
if (parsedAPI["assets"].is_array()) {
for (auto jsonAsset : parsedAPI["assets"]) {
if (jsonAsset.is_object() && jsonAsset["name"].is_string() && jsonAsset["browser_download_url"].is_string()) {
std::string assetName = jsonAsset["name"];
if (ScriptUtils::matchPattern(asset, assetName)) {
assetUrl = jsonAsset["browser_download_url"];
break;
}
}
}
}
socExit();
free(result_buf);
free(socubuf);
result_buf = nullptr;
result_sz = 0;
result_written = 0;
if (assetUrl.empty()) {
ret = DL_ERROR_GIT;
} else {
ret = downloadToFile(assetUrl, path);
}
return ret;
}
/*
Check Wi-Fi status.
@return True if Wi-Fi is connected; false if not.
*/
bool checkWifiStatus(void) {
//return true; // For citra.
u32 wifiStatus;
bool res = false;
if (R_SUCCEEDED(ACU_GetWifiStatus(&wifiStatus)) && wifiStatus) res = true;
return res;
}
void downloadFailed(void) { Msg::waitMsg(Lang::get("DOWNLOAD_FAILED")); }
void notImplemented(void) { Msg::waitMsg(Lang::get("NOT_IMPLEMENTED")); }
void doneMsg(void) { Msg::waitMsg(Lang::get("DONE")); }
void notConnectedMsg(void) { Msg::waitMsg(Lang::get("CONNECT_WIFI")); }
/*
Display the progressbar.
*/
void displayProgressBar() {
char str[256];
while(showProgressBar) {
switch(progressbarType) {
case ProgressBar::Downloading:
if (downloadTotal < 1.0f) downloadTotal = 1.0f;
if (downloadTotal < downloadNow) downloadTotal = downloadNow;
snprintf(str, sizeof(str), "%s / %s (%.2f%%)",
StringUtils::formatBytes(downloadNow).c_str(),
StringUtils::formatBytes(downloadTotal).c_str(),
((float)downloadNow/(float)downloadTotal) * 100.0f);
break;
case ProgressBar::Extracting:
snprintf(str, sizeof(str), "%s / %s (%.2f%%)",
StringUtils::formatBytes(writeOffset).c_str(),
StringUtils::formatBytes(extractSize).c_str(),
((float)writeOffset/(float)extractSize) * 100.0f);
break;
case ProgressBar::Installing:
snprintf(str, sizeof(str), "%s / %s (%.2f%%)",
StringUtils::formatBytes(installOffset).c_str(),
StringUtils::formatBytes(installSize).c_str(),
((float)installOffset/(float)installSize) * 100.0f);
break;
}
Gui::clearTextBufs();
C3D_FrameBegin(C3D_FRAME_SYNCDRAW);
C2D_TargetClear(Top, TRANSPARENT);
C2D_TargetClear(Bottom, TRANSPARENT);
GFX::DrawTop();
Gui::DrawStringCentered(0, 1, 0.7f, TEXT_COLOR, progressBarMsg, 400);
switch(progressbarType) {
case ProgressBar::Downloading:
Gui::DrawStringCentered(0, 80, 0.6f, TEXT_COLOR, str, 400);
Animation::DrawProgressBar(downloadNow, downloadTotal);
break;
case ProgressBar::Extracting:
Gui::DrawStringCentered(0, 180, 0.6f, TEXT_COLOR, str, 400);
Gui::DrawStringCentered(0, 100, 0.6f, TEXT_COLOR, std::to_string(filesExtracted) + " " + (filesExtracted == 1 ? (Lang::get("FILE_EXTRACTED")).c_str() :(Lang::get("FILES_EXTRACTED"))), 400);
Gui::DrawStringCentered(0, 40, 0.6f, TEXT_COLOR, Lang::get("CURRENTLY_EXTRACTING") + "\n" + extractingFile, 400);
Animation::DrawProgressBar(writeOffset, extractSize);
break;
case ProgressBar::Installing:
Gui::DrawStringCentered(0, 80, 0.6f, TEXT_COLOR, str, 400);
Animation::DrawProgressBar(installOffset, installSize);
break;
}
GFX::DrawBottom();
C3D_FrameEnd(0);
}
}
/*
Return, if an update is available.
const std::string &URL: Const Reference to the URL of the UniStore.
const int &revCurrent: Const Reference to the current Revision. (-1 if unused)
*/
bool IsUpdateAvailable(const std::string &URL, const int &revCurrent) {
Msg::DisplayMsg(Lang::get("CHECK_UNISTORE_UPDATES"));
Result ret = 0;
void *socubuf = memalign(0x1000, 0x100000);
if (!socubuf) return false;
ret = socInit((u32 *)socubuf, 0x100000);
if (R_FAILED(ret)) {
free(socubuf);
return false;
}
CURL *hnd = curl_easy_init();
ret = setupContext(hnd, URL.c_str());
if (ret != 0) {
socExit();
free(result_buf);
free(socubuf);
result_buf = nullptr;
result_sz = 0;
result_written = 0;
return false;
}
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 = nullptr;
result_sz = 0;
result_written = 0;
return false;
}
if (nlohmann::json::accept(result_buf)) {
nlohmann::json parsedAPI = nlohmann::json::parse(result_buf);
if (parsedAPI.contains("storeInfo") && parsedAPI.contains("storeContent")) {
if (parsedAPI["storeInfo"].contains("revision") && parsedAPI["storeInfo"]["revision"].is_number()) {
const int rev = parsedAPI["storeInfo"]["revision"];
socExit();
free(result_buf);
free(socubuf);
result_buf = nullptr;
result_sz = 0;
result_written = 0;
return rev > revCurrent;
}
}
}
socExit();
free(result_buf);
free(socubuf);
result_buf = nullptr;
result_sz = 0;
result_written = 0;
return false;
}
/*
Download a UniStore and return, if revision is higher than current.
const std::string &URL: Const Reference to the URL of the UniStore.
const int &currentRev: Const Reference to the current Revision. (-1 if unused)
const bool &isDownload: Const Reference, if download or updating.
const bool &isUDB: Const Reference, if Universal-DB download or not.
*/
bool DownloadUniStore(const std::string &URL, const int &currentRev, std::string &fl, const bool &isDownload, const bool &isUDB) {
if (isUDB) Msg::DisplayMsg(Lang::get("DOWNLOADING_UNIVERSAL_DB"));
else {
if (currentRev > -1) Msg::DisplayMsg(Lang::get("CHECK_UNISTORE_UPDATES"));
else Msg::DisplayMsg((isDownload ? Lang::get("DOWNLOADING_UNISTORE") : Lang::get("UPDATING_UNISTORE")));
}
Result ret = 0;
void *socubuf = memalign(0x1000, 0x100000);
if (!socubuf) return false;
ret = socInit((u32 *)socubuf, 0x100000);
if (R_FAILED(ret)) {
free(socubuf);
return false;
}
CURL *hnd = curl_easy_init();
ret = setupContext(hnd, URL.c_str());
if (ret != 0) {
socExit();
free(result_buf);
free(socubuf);
result_buf = nullptr;
result_sz = 0;
result_written = 0;
return false;
}
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 = nullptr;
result_sz = 0;
result_written = 0;
return false;
}
if (nlohmann::json::accept(result_buf)) {
nlohmann::json parsedAPI = nlohmann::json::parse(result_buf);
if (parsedAPI.contains("storeInfo") && parsedAPI.contains("storeContent")) {
/* Ensure, version == 3. */
if (parsedAPI["storeInfo"].contains("version") && parsedAPI["storeInfo"]["version"].is_number()) {
if (parsedAPI["storeInfo"]["version"] == 3) {
if (currentRev > -1) {
if (parsedAPI["storeInfo"].contains("revision") && parsedAPI["storeInfo"]["revision"].is_number()) {
const int rev = parsedAPI["storeInfo"]["revision"];
if (rev > currentRev) {
Msg::DisplayMsg(Lang::get("UPDATING_UNISTORE"));
if (parsedAPI["storeInfo"].contains("file") && parsedAPI["storeInfo"]["file"].is_string()) {
fl = parsedAPI["storeInfo"]["file"];
/* Make sure it's not "/", otherwise it breaks. */
if (!(fl.find("/") != std::string::npos)) {
FILE *out = fopen((std::string(_STORE_PATH) + fl).c_str(), "w");
fwrite(result_buf, sizeof(char), result_written, out);
fclose(out);
socExit();
free(result_buf);
free(socubuf);
result_buf = nullptr;
result_sz = 0;
result_written = 0;
return true;
} else {
Msg::waitMsg(Lang::get("FILE_SLASH"));
}
}
}
}
} else {
if (parsedAPI["storeInfo"].contains("file") && parsedAPI["storeInfo"]["file"].is_string()) {
fl = parsedAPI["storeInfo"]["file"];
/* Make sure it's not "/", otherwise it breaks. */
if (!(fl.find("/") != std::string::npos)) {
FILE *out = fopen((std::string(_STORE_PATH) + fl).c_str(), "w");
fwrite(result_buf, sizeof(char), result_written, out);
fclose(out);
socExit();
free(result_buf);
free(socubuf);
result_buf = nullptr;
result_sz = 0;
result_written = 0;
return true;
} else {
Msg::waitMsg(Lang::get("FILE_SLASH"));
}
}
}
} else if (parsedAPI["storeInfo"]["version"] < 3) {
Msg::waitMsg(Lang::get("UNISTORE_TOO_OLD"));
} else if (parsedAPI["storeInfo"]["version"] > 3) {
Msg::waitMsg(Lang::get("UNISTORE_TOO_NEW"));
}
}
} else {
Msg::waitMsg(Lang::get("UNISTORE_INVALID_ERROR"));
}
}
socExit();
free(result_buf);
free(socubuf);
result_buf = nullptr;
result_sz = 0;
result_written = 0;
return false;
}
/*
Download a SpriteSheet.
const std::string &URL: Const Reference to the SpriteSheet URL.
const std::string &file: Const Reference to the filepath.
*/
bool DownloadSpriteSheet(const std::string &URL, const std::string &file) {
if (file.find("/") != std::string::npos) return false;
Result ret = 0;
void *socubuf = memalign(0x1000, 0x100000);
if (!socubuf) return false;
ret = socInit((u32 *)socubuf, 0x100000);
if (R_FAILED(ret)) {
free(socubuf);
return false;
}
CURL *hnd = curl_easy_init();
ret = setupContext(hnd, URL.c_str());
if (ret != 0) {
socExit();
free(result_buf);
free(socubuf);
result_buf = nullptr;
result_sz = 0;
result_written = 0;
return false;
}
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 = nullptr;
result_sz = 0;
result_written = 0;
return false;
}
C2D_SpriteSheet sheet = C2D_SpriteSheetLoadFromMem(result_buf, result_written);
if (sheet) {
if (C2D_SpriteSheetCount(sheet) > 0) {
FILE *out = fopen((std::string(_STORE_PATH) + file).c_str(), "w");
fwrite(result_buf, sizeof(char), result_written, out);
fclose(out);
socExit();
free(result_buf);
free(socubuf);
result_buf = nullptr;
result_sz = 0;
result_written = 0;
C2D_SpriteSheetFree(sheet);
return true;
}
}
socExit();
free(result_buf);
free(socubuf);
result_buf = nullptr;
result_sz = 0;
result_written = 0;
return false;
}
/*
Checks for U-U updates.
*/
bool IsUUUpdateAvailable() {
if (!checkWifiStatus()) return false;
Msg::DisplayMsg(Lang::get("CHECK_UU_UPDATES"));
Result ret = 0;
void *socubuf = memalign(0x1000, 0x100000);
if (!socubuf) return false;
ret = socInit((u32 *)socubuf, 0x100000);
if (R_FAILED(ret)) {
free(socubuf);
return false;
}
CURL *hnd = curl_easy_init();
ret = setupContext(hnd, "https://api.github.com/repos/Universal-Team/Universal-Updater/releases/latest");
if (ret != 0) {
socExit();
free(result_buf);
free(socubuf);
result_buf = nullptr;
result_sz = 0;
result_written = 0;
return false;
}
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 = nullptr;
result_sz = 0;
result_written = 0;
return false;
}
if (nlohmann::json::accept(result_buf)) {
nlohmann::json parsedAPI = nlohmann::json::parse(result_buf);
if (parsedAPI.contains("tag_name") && parsedAPI["tag_name"].is_string()) {
const std::string tag = parsedAPI["tag_name"];
socExit();
free(result_buf);
free(socubuf);
result_buf = nullptr;
result_sz = 0;
result_written = 0;
return strcasecmp(StringUtils::lower_case(tag).c_str(), StringUtils::lower_case(C_V).c_str()) > 0;
}
}
socExit();
free(result_buf);
free(socubuf);
result_buf = nullptr;
result_sz = 0;
result_written = 0;
return false;
}
extern bool is3DSX, exiting;
extern std::string _3dsxPath;
/*
Execute U-U update action.
*/
void UpdateAction() {
if (ScriptUtils::downloadRelease("Universal-Team/Universal-Updater", (is3DSX ? "Universal-Updater.3dsx" : "Universal-Updater.cia"),
(is3DSX ? _3dsxPath : "sdmc:/Universal-Updater.cia"),
false, Lang::get("DONLOADING_UNIVERSAL_UPDATER")) == 0) {
if (is3DSX) {
Msg::waitMsg(Lang::get("UPDATE_DONE"));
exiting = true;
return;
}
ScriptUtils::installFile("sdmc:/Universal-Updater.cia", false, Lang::get("INSTALL_UNIVERSAL_UPDATER"));
ScriptUtils::removeFile("sdmc:/Universal-Updater.cia", Lang::get("DELETE_UNNEEDED_FILE"));
Msg::waitMsg(Lang::get("UPDATE_DONE"));
exiting = true;
}
}
+98 -104
View File
@@ -1,105 +1,99 @@
/*
* This file is part of Universal-Updater
* Copyright (C) 2019-2020 Universal-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#include "extract.hpp"
#include "logging.hpp"
#include <archive.h>
#include <archive_entry.h>
#include <regex>
int filesExtracted = 0;
std::string extractingFile = "";
// That are our File Progressbar variable.
u64 extractSize = 0, writeOffset = 0;
Result extractArchive(std::string archivePath, std::string wantedFile, std::string outputPath) {
extractSize = 0, writeOffset = 0, filesExtracted = 0;
archive *a = archive_read_new();
archive_entry *entry;
int flags;
/* Select which attributes we want to restore. */
flags = ARCHIVE_EXTRACT_TIME;
flags |= ARCHIVE_EXTRACT_PERM;
flags |= ARCHIVE_EXTRACT_ACL;
flags |= ARCHIVE_EXTRACT_FFLAGS;
a = archive_read_new();
archive_read_support_format_all(a);
if (archive_read_open_filename(a, archivePath.c_str(), 0x4000) != ARCHIVE_OK) {
return EXTRACT_ERROR_OPENFILE;
}
while(archive_read_next_header(a, &entry) == ARCHIVE_OK) {
if (archive_entry_size(entry) > 0) { // Ignore folders
std::smatch match;
std::string entryName(archive_entry_pathname(entry));
if (std::regex_search(entryName, match, std::regex(wantedFile))) {
extractingFile = outputPath + match.suffix().str();
// make directories
int substrPos = 1;
while(extractingFile.find("/", substrPos)) {
mkdir(extractingFile.substr(0, substrPos).c_str(), 0777);
substrPos = extractingFile.find("/", substrPos) + 1;
}
uint sizeLeft = archive_entry_size(entry);
extractSize = sizeLeft;
writeOffset = 0;
FILE *file = fopen(extractingFile.c_str(), "wb");
if (!file) {
return EXTRACT_ERROR_WRITEFILE;
}
u8 *buf = new u8[0x30000];
if (buf == nullptr) {
return EXTRACT_ERROR_ALLOC;
}
while(sizeLeft > 0) {
u64 toRead = std::min(0x30000u, sizeLeft);
ssize_t size = archive_read_data(a, buf, toRead);
fwrite(buf, 1, size, file);
sizeLeft -= size;
writeOffset += size;
}
filesExtracted++;
fclose(file);
delete[] buf;
}
}
}
archive_read_close(a);
archive_read_free(a);
return EXTRACT_ERROR_NONE;
/*
* This file is part of Universal-Updater
* Copyright (C) 2019-2020 Universal-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#include "extract.hpp"
#include "scriptUtils.hpp"
#include <archive.h>
#include <archive_entry.h>
#include <regex>
int filesExtracted = 0;
std::string extractingFile = "";
/* That are our File Progressbar variable. */
u64 extractSize = 0, writeOffset = 0;
Result extractArchive(const std::string &archivePath, const std::string &wantedFile, const std::string &outputPath) {
extractSize = 0, writeOffset = 0, filesExtracted = 0;
archive *a = archive_read_new();
archive_entry *entry;
int flags;
/* Select which attributes we want to restore. */
flags = ARCHIVE_EXTRACT_TIME;
flags |= ARCHIVE_EXTRACT_PERM;
flags |= ARCHIVE_EXTRACT_ACL;
flags |= ARCHIVE_EXTRACT_FFLAGS;
a = archive_read_new();
archive_read_support_format_all(a);
if (archive_read_open_filename(a, archivePath.c_str(), 0x4000) != ARCHIVE_OK) return EXTRACT_ERROR_OPENFILE;
while(archive_read_next_header(a, &entry) == ARCHIVE_OK) {
if (archive_entry_size(entry) > 0) { /* Ignore folders. */
std::smatch match;
std::string entryName(archive_entry_pathname(entry));
if (std::regex_search(entryName, match, std::regex(wantedFile))) {
extractingFile = outputPath + match.suffix().str();
/* make directories. */
int substrPos = 1;
while(extractingFile.find("/", substrPos)) {
mkdir(extractingFile.substr(0, substrPos).c_str(), 0777);
substrPos = extractingFile.find("/", substrPos) + 1;
}
uint sizeLeft = archive_entry_size(entry);
extractSize = sizeLeft;
writeOffset = 0;
FILE *file = fopen(extractingFile.c_str(), "wb");
if (!file) return EXTRACT_ERROR_WRITEFILE;
u8 *buf = new u8[0x30000];
if (!buf) return EXTRACT_ERROR_ALLOC;
while(sizeLeft > 0) {
u64 toRead = std::min(0x30000u, sizeLeft);
ssize_t size = archive_read_data(a, buf, toRead);
fwrite(buf, 1, size, file);
sizeLeft -= size;
writeOffset += size;
}
filesExtracted++;
fclose(file);
delete[] buf;
}
}
}
archive_read_close(a);
archive_read_free(a);
return EXTRACT_ERROR_NONE;
}
+281 -453
View File
@@ -1,454 +1,282 @@
/*
* This file is part of Universal-Updater
* Copyright (C) 2019-2020 Universal-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#include "common.hpp"
#include "config.hpp"
#include "fileBrowse.hpp"
#include "gfx.hpp"
#include "gui.hpp"
#include "screenCommon.hpp"
#include "structs.hpp"
#include <3ds.h>
#include <cstring>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <vector>
#include <algorithm>
#include <functional>
#include <array>
#include <iostream>
int file_count = 0;
extern std::unique_ptr<Config> config;
extern uint selectedFile;
extern int keyRepeatDelay;
extern bool dirChanged;
std::vector<DirEntry> dirContents;
extern bool touching(touchPosition touch, Structs::ButtonPos button);
extern touchPosition touch;
const std::vector<Structs::ButtonPos> buttonPositions = {
{295, 0, 25, 25}, // Arrow Up.
{295, 215, 25, 25}, // Arrow Down.
{15, 220, 50, 15}, // Open.
{80, 220, 50, 15}, // Select.
{145, 220, 50, 15}, // Refresh.
{210, 220, 50, 15}, // Back.
{0, 0, 25, 25} // ViewMode Change.
};
off_t getFileSize(const char *fileName) {
FILE* fp = fopen(fileName, "rb");
off_t fsize = 0;
if (fp) {
fseek(fp, 0, SEEK_END);
fsize = ftell(fp); // Get source file's size
fseek(fp, 0, SEEK_SET);
}
fclose(fp);
return fsize;
}
bool nameEndsWith(const std::string& name, const std::vector<std::string> extensionList) {
if (name.substr(0, 2) == "._") return false;
if (name.size() == 0) return false;
if (extensionList.size() == 0) return true;
for(int i = 0; i < (int)extensionList.size(); i++) {
const std::string ext = extensionList.at(i);
if (strcasecmp(name.c_str() + name.size() - ext.size(), ext.c_str()) == 0) return true;
}
return false;
}
bool dirEntryPredicate(const DirEntry& lhs, const DirEntry& rhs) {
if (!lhs.isDirectory && rhs.isDirectory) {
return false;
}
if (lhs.isDirectory && !rhs.isDirectory) {
return true;
}
return strcasecmp(lhs.name.c_str(), rhs.name.c_str()) < 0;
}
void getDirectoryContents(std::vector<DirEntry>& dirContents, const std::vector<std::string> extensionList) {
struct stat st;
dirContents.clear();
DIR *pdir = opendir(".");
if (pdir == NULL) {
Msg::DisplayMsg("Unable to open the directory.");
for(int i = 0; i < 120; i++) gspWaitForVBlank();
} else {
while(true) {
DirEntry dirEntry;
struct dirent* pent = readdir(pdir);
if (pent == NULL) break;
stat(pent->d_name, &st);
dirEntry.name = pent->d_name;
dirEntry.isDirectory = (st.st_mode & S_IFDIR) ? true : false;
if (dirEntry.name.compare(".") != 0 && (dirEntry.isDirectory || nameEndsWith(dirEntry.name, extensionList))) {
dirContents.push_back(dirEntry);
}
}
closedir(pdir);
}
sort(dirContents.begin(), dirContents.end(), dirEntryPredicate);
}
void getDirectoryContents(std::vector<DirEntry>& dirContents) {
getDirectoryContents(dirContents, {});
}
std::vector<std::string> getContents(const std::string &name, const std::vector<std::string> &extensionList) {
std::vector<std::string> dirContents;
DIR* pdir = opendir(name.c_str());
struct dirent *pent;
while ((pent = readdir(pdir)) != NULL) {
if (nameEndsWith(pent->d_name, extensionList))
dirContents.push_back(pent->d_name);
}
closedir(pdir);
return dirContents;
}
// Directory exist?
bool returnIfExist(const std::string &path, const std::vector<std::string> &extensionList) {
dirContents.clear();
chdir(path.c_str());
std::vector<DirEntry> dirContentsTemp;
getDirectoryContents(dirContentsTemp, extensionList);
for(uint i = 0; i < dirContentsTemp.size(); i++) {
dirContents.push_back(dirContentsTemp[i]);
}
if (dirContents.size() == 0) {
return false;
}
return true;
}
// returns a Path or file to 'std::string'.
// selectText is the Text which is displayed on the bottom bar of the top screen.
// selectionMode is how you select it. 1 -> Path, 2 -> File.
std::string selectFilePath(std::string selectText, std::string initialPath, const std::vector<std::string> &extensionList, int selectionMode) {
uint selectedFile = 0;
std::string selectedPath = "";
int keyRepeatDelay = 4;
bool dirChanged = true;
bool fastMode = false;
uint screenPos = 0;
uint screenPosList = 0;
std::vector<DirEntry> dirContents;
std::string dirs;
// Initial dir change.
dirContents.clear();
chdir(initialPath.c_str());
std::vector<DirEntry> dirContentsTemp;
getDirectoryContents(dirContentsTemp, extensionList);
for(uint i = 0; i < dirContentsTemp.size(); i++) {
dirContents.push_back(dirContentsTemp[i]);
}
selectedFile = 0;
while (1) {
Gui::clearTextBufs();
C3D_FrameBegin(C3D_FRAME_SYNCDRAW);
C2D_TargetClear(Top, BLACK);
C2D_TargetClear(Bottom, BLACK);
GFX::DrawTop();
char path[PATH_MAX];
getcwd(path, PATH_MAX);
Gui::DrawString((400-(Gui::GetStringWidth(0.60f, path)))/2, config->useBars() ? 0 : 2, 0.60f, config->textColor(), path, 390);
Gui::DrawStringCentered(0, config->useBars() ? 220 : 218, 0.60f, config->textColor(), selectText, 390);
GFX::DrawBottom();
if (config->viewMode() == 0) {
for(int i = 0; i < ENTRIES_PER_SCREEN && i < (int)dirContents.size(); i++) {
Gui::Draw_Rect(0, 40+(i*57), 320, 45, config->unselectedColor());
dirs = dirContents[screenPos + i].name;
if (screenPos + i == selectedFile) {
Gui::drawAnimatedSelector(0, 40+(i*57), 320, 45, .060, TRANSPARENT, config->selectedColor());
}
Gui::DrawStringCentered(0, 50+(i*57), 0.7f, config->textColor(), dirs, 320);
}
} else if (config->viewMode() == 1) {
for(int i = 0; i < ENTRIES_PER_LIST && i < (int)dirContents.size(); i++) {
Gui::Draw_Rect(0, (i+1)*27, 320, 25, config->unselectedColor());
dirs = dirContents[screenPosList + i].name;
if (screenPosList + i == selectedFile) {
Gui::drawAnimatedSelector(0, (i+1)*27, 320, 25, .060, TRANSPARENT, config->selectedColor());
}
Gui::DrawStringCentered(0, ((i+1)*27)+1, 0.7f, config->textColor(), dirs, 320);
}
}
Gui::DrawStringCentered(0, config->useBars() ? 0 : 2, 0.45f, config->textColor(), Lang::get("FILEBROWSE_MSG"), 260);
GFX::DrawArrow(295, -1);
GFX::DrawArrow(315, 240, 180.0);
GFX::DrawSpriteBlend(sprites_view_idx, buttonPositions[6].x, buttonPositions[6].y);
Gui::Draw_Rect(buttonPositions[2].x, buttonPositions[2].y, buttonPositions[2].w, buttonPositions[2].h, C2D_Color32(0, 0, 0, 190));
Gui::Draw_Rect(buttonPositions[3].x, buttonPositions[3].y, buttonPositions[3].w, buttonPositions[3].h, C2D_Color32(0, 0, 0, 190));
Gui::Draw_Rect(buttonPositions[4].x, buttonPositions[4].y, buttonPositions[4].w, buttonPositions[4].h, C2D_Color32(0, 0, 0, 190));
Gui::Draw_Rect(buttonPositions[5].x, buttonPositions[5].y, buttonPositions[5].w, buttonPositions[5].h, C2D_Color32(0, 0, 0, 190));
Gui::DrawStringCentered(-120, 222, 0.4, config->textColor(), Lang::get("OPEN"), 40);
Gui::DrawStringCentered(-55, 222, 0.4, config->textColor(), Lang::get("SELECT"), 40);
Gui::DrawStringCentered(10, 222, 0.4, config->textColor(), Lang::get("REFRESH"), 40);
Gui::DrawStringCentered(75, 222, 0.4, config->textColor(), Lang::get("BACK"), 40);
C3D_FrameEnd(0);
// The input part.
hidScanInput();
hidTouchRead(&touch);
if (keyRepeatDelay) keyRepeatDelay--;
if (dirChanged) {
dirContents.clear();
std::vector<DirEntry> dirContentsTemp;
getDirectoryContents(dirContentsTemp, extensionList);
for(uint i = 0; i < dirContentsTemp.size(); i++) {
dirContents.push_back(dirContentsTemp[i]);
}
dirChanged = false;
}
if ((hidKeysDown() & KEY_SELECT) || (hidKeysDown() & KEY_TOUCH && touching(touch, buttonPositions[4]))) {
dirChanged = true;
}
if ((hidKeysDown() & KEY_A) || (hidKeysDown() & KEY_TOUCH && touching(touch, buttonPositions[2]))) {
if (dirContents.size() != 0) {
if (dirContents[selectedFile].isDirectory) {
chdir(dirContents[selectedFile].name.c_str());
selectedFile = 0;
dirChanged = true;
}
}
}
if (hidKeysDown() & KEY_TOUCH && touching(touch, buttonPositions[0])) {
if (selectedFile > 0) selectedFile--;
}
if (hidKeysDown() & KEY_TOUCH && touching(touch, buttonPositions[1])) {
if (selectedFile < dirContents.size()-1) selectedFile++;
}
if (hidKeysHeld() & KEY_UP) {
if (selectedFile > 0 && !keyRepeatDelay) {
selectedFile--;
if (fastMode == true) {
keyRepeatDelay = 3;
} else if (fastMode == false){
keyRepeatDelay = 6;
}
}
}
if (hidKeysHeld() & KEY_DOWN && !keyRepeatDelay) {
if (selectedFile < dirContents.size()-1) {
selectedFile++;
if (fastMode == true) {
keyRepeatDelay = 3;
} else if (fastMode == false){
keyRepeatDelay = 6;
}
}
}
if ((hidKeysDown() & KEY_B) || (hidKeysDown() & KEY_TOUCH && touching(touch, buttonPositions[5]))) {
char path[PATH_MAX];
getcwd(path, PATH_MAX);
if (strcmp(path, "sdmc:/") == 0 || strcmp(path, "/") == 0) {
return "";
} else {
chdir("..");
selectedFile = 0;
dirChanged = true;
}
}
if ((hidKeysDown() & KEY_X) || (hidKeysDown() & KEY_TOUCH && touching(touch, buttonPositions[3]))) {
char path[PATH_MAX];
getcwd(path, PATH_MAX);
selectedPath = path;
if (selectionMode == 2) {
selectedPath += dirContents[selectedFile].name;
}
return selectedPath;
}
if (hidKeysDown() & KEY_R) {
fastMode = true;
}
if (hidKeysDown() & KEY_L) {
fastMode = false;
}
// Switch ViewMode.
if ((hidKeysDown() & KEY_Y) || (hidKeysDown() & KEY_TOUCH && touching(touch, buttonPositions[6]))) {
if (config->viewMode() == 0) {
config->viewMode(1);
} else {
config->viewMode(0);
}
}
if (config->viewMode() == 0) {
if (selectedFile < screenPos) {
screenPos = selectedFile;
} else if (selectedFile > screenPos + ENTRIES_PER_SCREEN - 1) {
screenPos = selectedFile - ENTRIES_PER_SCREEN + 1;
}
} else if (config->viewMode() == 1) {
if (selectedFile < screenPosList) {
screenPosList = selectedFile;
} else if (selectedFile > screenPosList + ENTRIES_PER_LIST - 1) {
screenPosList = selectedFile - ENTRIES_PER_LIST + 1;
}
}
}
}
#define copyBufSize 0x8000
u32 copyBuf[copyBufSize];
void dirCopy(DirEntry* entry, int i, const char *destinationPath, const char *sourcePath) {
std::vector<DirEntry> dirContents;
dirContents.clear();
if (entry->isDirectory) chdir((sourcePath + ("/" + entry->name)).c_str());
getDirectoryContents(dirContents);
if (((int)dirContents.size()) == 1) mkdir((destinationPath + ("/" + entry->name)).c_str(), 0777);
if (((int)dirContents.size()) != 1) fcopy((sourcePath + ("/" + entry->name)).c_str(), (destinationPath + ("/" + entry->name)).c_str());
}
int fcopy(const char *sourcePath, const char *destinationPath) {
DIR *isDir = opendir(sourcePath);
if (isDir != NULL) {
closedir(isDir);
// Source path is a directory
chdir(sourcePath);
std::vector<DirEntry> dirContents;
getDirectoryContents(dirContents);
DirEntry* entry = &dirContents.at(1);
mkdir(destinationPath, 0777);
for(int i = 1; i < ((int)dirContents.size()); i++) {
chdir(sourcePath);
entry = &dirContents.at(i);
dirCopy(entry, i, destinationPath, sourcePath);
}
chdir(destinationPath);
chdir("..");
return 1;
} else {
closedir(isDir);
// Source path is a file
FILE* sourceFile = fopen(sourcePath, "rb");
off_t fsize = 0;
if (sourceFile) {
fseek(sourceFile, 0, SEEK_END);
fsize = ftell(sourceFile); // Get source file's size
fseek(sourceFile, 0, SEEK_SET);
} else {
fclose(sourceFile);
return -1;
}
FILE* destinationFile = fopen(destinationPath, "wb");
//if (destinationFile) {
fseek(destinationFile, 0, SEEK_SET);
/*} else {
fclose(sourceFile);
fclose(destinationFile);
return -1;
}*/
off_t offset = 0;
int numr;
while(1) {
scanKeys();
if (keysHeld() & KEY_B) {
// Cancel copying
fclose(sourceFile);
fclose(destinationFile);
return -1;
break;
}
printf("\x1b[16;0H");
printf("Progress:\n");
printf("%i/%i Bytes ", (int)offset, (int)fsize);
// Copy file to destination path
numr = fread(copyBuf, 2, copyBufSize, sourceFile);
fwrite(copyBuf, 2, numr, destinationFile);
offset += copyBufSize;
if (offset > fsize) {
fclose(sourceFile);
fclose(destinationFile);
printf("\x1b[17;0H");
printf("%i/%i Bytes ", (int)fsize, (int)fsize);
for(int i = 0; i < 30; i++) gspWaitForVBlank();
return 1;
break;
}
}
return -1;
}
/*
* This file is part of Universal-Updater
* Copyright (C) 2019-2020 Universal-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#include "fileBrowse.hpp"
#include "json.hpp"
#include "structs.hpp"
#include <3ds.h>
#include <cstring>
#include <functional>
#include <unistd.h>
extern bool touching(touchPosition touch, Structs::ButtonPos button);
extern touchPosition touch;
bool nameEndsWith(const std::string &name, const std::vector<std::string> &extensionList) {
if (name.substr(0, 2) == "._") return false;
if (name.size() == 0) return false;
if (extensionList.size() == 0) return true;
for(int i = 0; i < (int)extensionList.size(); i++) {
const std::string ext = extensionList.at(i);
if (strcasecmp(name.c_str() + name.size() - ext.size(), ext.c_str()) == 0) return true;
}
return false;
}
bool dirEntryPredicate(const DirEntry &lhs, const DirEntry &rhs) {
if (!lhs.isDirectory && rhs.isDirectory) return false;
if (lhs.isDirectory && !rhs.isDirectory) return true;
return strcasecmp(lhs.name.c_str(), rhs.name.c_str()) < 0;
}
void getDirectoryContents(std::vector<DirEntry> &dirContents, const std::vector<std::string> &extensionList) {
struct stat st;
dirContents.clear();
DIR *pdir = opendir(".");
if (pdir != nullptr) {
while(true) {
DirEntry dirEntry;
struct dirent *pent = readdir(pdir);
if (pent == NULL) break;
stat(pent->d_name, &st);
dirEntry.name = pent->d_name;
dirEntry.isDirectory = (st.st_mode & S_IFDIR) ? true : false;
if (dirEntry.name.compare(".") != 0 && (dirEntry.isDirectory || nameEndsWith(dirEntry.name, extensionList))) {
dirContents.push_back(dirEntry);
}
}
closedir(pdir);
}
sort(dirContents.begin(), dirContents.end(), dirEntryPredicate);
}
void getDirectoryContents(std::vector<DirEntry> &dirContents) {
getDirectoryContents(dirContents, {});
}
std::vector<std::string> getContents(const std::string &name, const std::vector<std::string> &extensionList) {
std::vector<std::string> dirContents;
DIR* pdir = opendir(name.c_str());
struct dirent *pent;
while ((pent = readdir(pdir)) != NULL) {
if (nameEndsWith(pent->d_name, extensionList)) dirContents.push_back(pent->d_name);
}
closedir(pdir);
return dirContents;
}
/*
Return UniStore info.
const std::string &file: Const Reference to the path of the file.
const std::string &fieName: Const Reference to the filename, without path.
*/
UniStoreInfo GetInfo(const std::string &file, const std::string &fileName) {
UniStoreInfo Temp = { "", "", "", fileName, "", -1, -1, -1 }; // Title, Author, URL, FileName, Desc, Version, Revision, Entries.
nlohmann::json JSON = nullptr;
FILE *temp = fopen(file.c_str(), "r");
JSON = nlohmann::json::parse(temp, nullptr, false);
fclose(temp);
if (!JSON.contains("storeInfo")) return Temp; // storeInfo does not exist.
if (JSON["storeInfo"].contains("title") && JSON["storeInfo"]["title"].is_string()) {
Temp.Title = JSON["storeInfo"]["title"];
}
if (JSON["storeInfo"].contains("author") && JSON["storeInfo"]["author"].is_string()) {
Temp.Author = JSON["storeInfo"]["author"];
}
if (JSON["storeInfo"].contains("url") && JSON["storeInfo"]["url"].is_string()) {
Temp.URL = JSON["storeInfo"]["url"];
}
if (JSON["storeInfo"].contains("description") && JSON["storeInfo"]["description"].is_string()) {
Temp.Description = JSON["storeInfo"]["description"];
}
if (JSON["storeInfo"].contains("version") && JSON["storeInfo"]["version"].is_number()) {
Temp.Version = JSON["storeInfo"]["version"];
}
if (JSON["storeInfo"].contains("revision") && JSON["storeInfo"]["revision"].is_number()) {
Temp.Revision = JSON["storeInfo"]["revision"];
}
if (JSON.contains("storeContent")) Temp.StoreSize = JSON["storeContent"].size();
return Temp;
}
/*
Return UniStore info vector.
const std::string &path: Const Reference to the path, where to check.
*/
std::vector<UniStoreInfo> GetUniStoreInfo(const std::string &path) {
std::vector<UniStoreInfo> info;
std::vector<DirEntry> dirContents;
chdir(path.c_str());
getDirectoryContents(dirContents, { "unistore" });
for(uint i = 0; i < dirContents.size(); i++) {
/* Make sure to ONLY push .unistores, and no folders. Avoids crashes in that case too. */
if ((path + dirContents[i].name).find(".unistore") != std::string::npos) {
info.push_back( GetInfo(path + dirContents[i].name, dirContents[i].name) );
}
}
return info;
}
#define copyBufSize 0x8000
u32 copyBuf[copyBufSize];
/*
Copy a directory.
DirEntry *entry: Pointer to a DirEntry.
const char *destinationPath: Pointer to the destination path.
const char *sourcePath: Pointer to the source path.
*/
void dirCopy(DirEntry *entry, const char *destinationPath, const char *sourcePath) {
std::vector<DirEntry> dirContents;
dirContents.clear();
if (entry->isDirectory) chdir((sourcePath + ("/" + entry->name)).c_str());
getDirectoryContents(dirContents);
if (((int)dirContents.size()) == 1) mkdir((destinationPath + ("/" + entry->name)).c_str(), 0777);
if (((int)dirContents.size()) != 1) fcopy((sourcePath + ("/" + entry->name)).c_str(), (destinationPath + ("/" + entry->name)).c_str());
}
/*
The copy operation.
const char *destinationPath: Pointer to the destination path.
const char *sourcePath: Pointer to the source path.
*/
int fcopy(const char *sourcePath, const char *destinationPath) {
DIR *isDir = opendir(sourcePath);
if (isDir != NULL) {
closedir(isDir);
/* Source path is a directory. */
chdir(sourcePath);
std::vector<DirEntry> dirContents;
getDirectoryContents(dirContents);
DirEntry *entry = &dirContents.at(1);
mkdir(destinationPath, 0777);
for(int i = 1; i < ((int)dirContents.size()); i++) {
chdir(sourcePath);
entry = &dirContents.at(i);
dirCopy(entry, destinationPath, sourcePath);
}
chdir(destinationPath);
chdir("..");
return 1;
} else {
closedir(isDir);
/* Source path is a file. */
FILE *sourceFile = fopen(sourcePath, "rb");
off_t fsize = 0;
if (sourceFile) {
fseek(sourceFile, 0, SEEK_END);
fsize = ftell(sourceFile); // Get source file's size.
fseek(sourceFile, 0, SEEK_SET);
} else {
fclose(sourceFile);
return -1;
}
FILE* destinationFile = fopen(destinationPath, "wb");
//if (destinationFile) {
fseek(destinationFile, 0, SEEK_SET);
/*} else {
fclose(sourceFile);
fclose(destinationFile);
return -1;
}*/
off_t offset = 0;
int numr;
while(1) {
scanKeys();
if (keysHeld() & KEY_B) {
/* Cancel copying. */
fclose(sourceFile);
fclose(destinationFile);
return -1;
break;
}
printf("\x1b[16;0H");
printf("Progress:\n");
printf("%i/%i Bytes ", (int)offset, (int)fsize);
/* Copy file to destination path. */
numr = fread(copyBuf, 2, copyBufSize, sourceFile);
fwrite(copyBuf, 2, numr, destinationFile);
offset += copyBufSize;
if (offset > fsize) {
fclose(sourceFile);
fclose(destinationFile);
printf("\x1b[17;0H");
printf("%i/%i Bytes ", (int)fsize, (int)fsize);
for(int i = 0; i < 30; i++) gspWaitForVBlank();
return 1;
break;
}
}
return -1;
}
}
-101
View File
@@ -1,101 +0,0 @@
#include "files.h"
FS_Path getPathInfo(const char * path, FS_ArchiveID * archive) {
*archive = ARCHIVE_SDMC;
FS_Path filePath = {0};
unsigned int prefixlen = 0;
if (!strncmp(path, "sdmc:/", 6)) {
prefixlen = 5;
} else if (*path != '/') {
//if the path is local (doesnt start with a slash), it needs to be appended to the working dir to be valid
char * actualPath = NULL;
asprintf(&actualPath, "%s%s", WORKING_DIR, path);
filePath = fsMakePath(PATH_ASCII, actualPath);
free(actualPath);
}
//if the filePath wasnt set above, set it
if (filePath.size == 0) {
filePath = fsMakePath(PATH_ASCII, path+prefixlen);
}
return filePath;
}
Result makeDirs(const char * path) {
Result ret = 0;
FS_ArchiveID archiveID;
FS_Path filePath = getPathInfo(path, &archiveID);
FS_Archive archive;
ret = FSUSER_OpenArchive(&archive, archiveID, fsMakePath(PATH_EMPTY, ""));
for (char * slashpos = strchr(path+1, '/'); slashpos != NULL; slashpos = strchr(slashpos+1, '/')) {
char bak = *(slashpos);
*(slashpos) = '\0';
Handle dirHandle;
ret = FSUSER_OpenDirectory(&dirHandle, archive, filePath);
if (R_SUCCEEDED(ret)) FSDIR_Close(dirHandle);
else ret = FSUSER_CreateDirectory(archive, filePath, FS_ATTRIBUTE_DIRECTORY);
*(slashpos) = bak;
}
FSUSER_CloseArchive(archive);
return ret;
}
Result openFile(Handle* fileHandle, const char * path, bool write) {
FS_ArchiveID archive;
FS_Path filePath = getPathInfo(path, &archive);
u32 flags = (write ? (FS_OPEN_CREATE | FS_OPEN_WRITE) : FS_OPEN_READ);
Result ret = 0;
ret = makeDirs(strdup(path));
ret = FSUSER_OpenFileDirectly(fileHandle, archive, fsMakePath(PATH_EMPTY, ""), filePath, flags, 0);
if (write) ret = FSFILE_SetSize(*fileHandle, 0); //truncate the file to remove previous contents before writing
return ret;
}
Result deleteFile(const char * path) {
FS_ArchiveID archiveID;
FS_Path filePath = getPathInfo(path, &archiveID);
FS_Archive archive;
Result ret = FSUSER_OpenArchive(&archive, archiveID, fsMakePath(PATH_EMPTY, ""));
if (R_FAILED(ret)) return ret;
ret = FSUSER_DeleteFile(archive, filePath);
FSUSER_CloseArchive(archive);
return ret;
}
Result removeDir(const char *path) {
FS_ArchiveID archiveID;
FS_Path filePath = getPathInfo(path, &archiveID);
FS_Archive archive;
Result ret = FSUSER_OpenArchive(&archive, archiveID, fsMakePath(PATH_EMPTY, ""));
if (R_FAILED(ret)) return ret;
ret = FSUSER_DeleteDirectory(archive, filePath);
FSUSER_CloseArchive(archive);
return ret;
}
Result removeDirRecursive(const char *path) {
FS_ArchiveID archiveID;
FS_Path filePath = getPathInfo(path, &archiveID);
FS_Archive archive;
Result ret = FSUSER_OpenArchive(&archive, archiveID, fsMakePath(PATH_EMPTY, ""));
if (R_FAILED(ret)) return ret;
ret = FSUSER_DeleteDirectoryRecursively(archive, filePath);
FSUSER_CloseArchive(archive);
return ret;
}
+130
View File
@@ -0,0 +1,130 @@
/*
* This file is part of Universal-Updater
* Copyright (C) 2019-2020 Universal-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#include "files.hpp"
FS_Path getPathInfo(const char *path, FS_ArchiveID *archive) {
*archive = ARCHIVE_SDMC;
FS_Path filePath = { PATH_INVALID, 0, nullptr };
unsigned int prefixlen = 0;
if (!strncmp(path, "sdmc:/", 6)) {
prefixlen = 5;
} else if (*path != '/') {
/*
if the path is local (doesnt start with a slash),
it needs to be appended to the working dir to be valid.
*/
char *actualPath = NULL;
asprintf(&actualPath, "%s%s", "/", path);
filePath = fsMakePath(PATH_ASCII, actualPath);
free(actualPath);
}
/* if the filePath wasnt set above, set it. */
if (filePath.size == 0) filePath = fsMakePath(PATH_ASCII, path + prefixlen);
return filePath;
}
Result makeDirs(const char *path) {
Result ret = 0;
FS_ArchiveID archiveID;
FS_Path filePath = getPathInfo(path, &archiveID);
FS_Archive archive;
ret = FSUSER_OpenArchive(&archive, archiveID, fsMakePath(PATH_EMPTY, ""));
for (char *slashpos = strchr(path + 1, '/'); slashpos != NULL; slashpos = strchr(slashpos + 1, '/')) {
char bak = *(slashpos);
*(slashpos) = '\0';
Handle dirHandle;
ret = FSUSER_OpenDirectory(&dirHandle, archive, filePath);
if (R_SUCCEEDED(ret)) FSDIR_Close(dirHandle);
else ret = FSUSER_CreateDirectory(archive, filePath, FS_ATTRIBUTE_DIRECTORY);
*(slashpos) = bak;
}
FSUSER_CloseArchive(archive);
return ret;
}
Result openFile(Handle *fileHandle, const char *path, const bool &write) {
FS_ArchiveID archive;
FS_Path filePath = getPathInfo(path, &archive);
u32 flags = (write ? (FS_OPEN_CREATE | FS_OPEN_WRITE) : FS_OPEN_READ);
Result ret = 0;
ret = makeDirs(strdup(path));
ret = FSUSER_OpenFileDirectly(fileHandle, archive, fsMakePath(PATH_EMPTY, ""), filePath, flags, 0);
if (write) ret = FSFILE_SetSize(*fileHandle, 0); // truncate the file to remove previous contents before writing.
return ret;
}
Result deleteFile(const char *path) {
FS_ArchiveID archiveID;
FS_Path filePath = getPathInfo(path, &archiveID);
FS_Archive archive;
Result ret = FSUSER_OpenArchive(&archive, archiveID, fsMakePath(PATH_EMPTY, ""));
if (R_FAILED(ret)) return ret;
ret = FSUSER_DeleteFile(archive, filePath);
FSUSER_CloseArchive(archive);
return ret;
}
Result removeDir(const char *path) {
FS_ArchiveID archiveID;
FS_Path filePath = getPathInfo(path, &archiveID);
FS_Archive archive;
Result ret = FSUSER_OpenArchive(&archive, archiveID, fsMakePath(PATH_EMPTY, ""));
if (R_FAILED(ret)) return ret;
ret = FSUSER_DeleteDirectory(archive, filePath);
FSUSER_CloseArchive(archive);
return ret;
}
Result removeDirRecursive(const char *path) {
FS_ArchiveID archiveID;
FS_Path filePath = getPathInfo(path, &archiveID);
FS_Archive archive;
Result ret = FSUSER_OpenArchive(&archive, archiveID, fsMakePath(PATH_EMPTY, ""));
if (R_FAILED(ret)) return ret;
ret = FSUSER_DeleteDirectoryRecursively(archive, filePath);
FSUSER_CloseArchive(archive);
return ret;
}
@@ -1,45 +1,55 @@
/*
* This file is part of Universal-Updater
* Copyright (C) 2019-2020 Universal-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#include "formatting.hpp"
// adapted from GM9i's byte parsing.
std::string formatBytes(int bytes) {
char out[32];
if (bytes == 1) {
snprintf(out, sizeof(out), "%d Byte", bytes);
} else if (bytes < 1024) {
snprintf(out, sizeof(out), "%d Bytes", bytes);
} else if (bytes < 1024 * 1024) {
snprintf(out, sizeof(out), "%.1f KB", (float)bytes / 1024);
} else if (bytes < 1024 * 1024 * 1024) {
snprintf(out, sizeof(out), "%.1f MB", (float)bytes / 1024 / 1024);
} else {
snprintf(out, sizeof(out), "%.1f GB", (float)bytes / 1024 / 1024 / 1024);
}
return out;
/*
* This file is part of Universal-Updater
* Copyright (C) 2019-2020 Universal-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#include "lang.hpp"
#include <stdio.h>
#include <unistd.h>
static nlohmann::json appJson;
std::string Lang::get(const std::string &key) {
if (!appJson.contains(key)) return "";
return appJson.at(key).get_ref<const std::string&>();
}
void Lang::load(const std::string &lang) {
FILE *values;
/* Check if exist. */
if (access(("romfs:/lang/" + lang + "/app.json").c_str(), F_OK) == 0) {
values = fopen(std::string(("romfs:/lang/" + lang + "/app.json")).c_str(), "rt");
appJson = nlohmann::json::parse(values, nullptr, false);
fclose(values);
return;
} else {
values = fopen(("romfs:/lang/en/app.json"), "rt");
appJson = nlohmann::json::parse(values, nullptr, false);
fclose(values);
return;
}
}
-233
View File
@@ -1,233 +0,0 @@
/*
* This file is part of Universal-Updater
* Copyright (C) 2019-2020 Universal-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#include "cia.hpp"
#include "download.hpp"
#include "extract.hpp"
#include "fileBrowse.hpp"
#include "gui.hpp"
#include "msg.hpp"
#include "scriptHelper.hpp"
#include "thread.hpp"
#include <fstream>
#include <regex>
#include <unistd.h>
extern bool showProgressBar;
extern ProgressBar progressbarType;
extern char progressBarMsg[128];
extern int filesExtracted;
extern void downloadFailed();
// Get String of the Script.
std::string ScriptHelper::getString(nlohmann::json json, const std::string &key, const std::string &key2) {
if (!json.contains(key)) return "MISSING: " + key;
if (!json.at(key).is_object()) return "NOT OBJECT: " + key;
if (!json.at(key).contains(key2)) return "MISSING: " + key + "." + key2;
if (!json.at(key).at(key2).is_string()) return "NOT STRING: " + key + "." + key2;
return json.at(key).at(key2).get_ref<const std::string&>();
}
// Get int of the Script.
int ScriptHelper::getNum(nlohmann::json json, const std::string &key, const std::string &key2) {
if (!json.contains(key)) return 0;
if (!json.at(key).is_object()) return 0;
if (!json.at(key).contains(key2)) return 0;
if (!json.at(key).at(key2).is_number()) return 0;
return json.at(key).at(key2).get_ref<const int64_t&>();
}
// 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());
out = std::regex_replace(out, std::regex("%ARCHIVE_DEFAULT%"), config->archivepath().c_str());
Result ret = NONE;
if (downloadFromRelease("https://github.com/" + repo, file, out, message, includePrereleases, showVersions) != 0) {
showProgressBar = false;
downloadFailed();
ret = FAILED_DOWNLOAD;
return ret;
}
showProgressBar = false;
return ret;
}
// 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());
out = std::regex_replace(out, std::regex("%ARCHIVE_DEFAULT%"), config->archivepath().c_str());
Result ret = NONE;
snprintf(progressBarMsg, sizeof(progressBarMsg), message.c_str());
showProgressBar = true;
progressbarType = ProgressBar::Downloading;
Threads::create((ThreadFunc)displayProgressBar);
if (downloadToFile(file, out) != 0) {
showProgressBar = false;
downloadFailed();
ret = FAILED_DOWNLOAD;
return ret;
}
showProgressBar = false;
return ret;
}
// Remove a File.
Result ScriptHelper::removeFile(std::string file, std::string message) {
std::string out;
out = std::regex_replace(file, std::regex("%ARCHIVE_DEFAULT%"), config->archivepath().c_str());
Result ret = NONE;
if (access(out.c_str(), F_OK) != 0 ) {
return DELETE_ERROR;
}
Msg::DisplayMsg(message);
deleteFile(out.c_str());
return ret;
}
// Install a file.
void ScriptHelper::installFile(std::string file, bool updatingSelf, std::string message) {
snprintf(progressBarMsg, sizeof(progressBarMsg), message.c_str());
showProgressBar = true;
progressbarType = ProgressBar::Installing;
Threads::create((ThreadFunc)displayProgressBar);
installCia(file.c_str(), updatingSelf);
showProgressBar = false;
}
// Extract Files.
void ScriptHelper::extractFile(std::string file, std::string input, std::string output, std::string message) {
std::string out, in;
in = std::regex_replace(file, std::regex("%ARCHIVE_DEFAULT%"), config->archivepath().c_str());
out = std::regex_replace(output, std::regex("%ARCHIVE_DEFAULT%"), config->archivepath().c_str());
snprintf(progressBarMsg, sizeof(progressBarMsg), message.c_str());
showProgressBar = true;
filesExtracted = 0;
progressbarType = ProgressBar::Extracting;
Threads::create((ThreadFunc)displayProgressBar);
extractArchive(in, input, out);
showProgressBar = false;
}
// Create an empty file.
Result ScriptHelper::createFile(const char * path) {
std::ofstream ofstream;
ofstream.open(path, std::ofstream::out | std::ofstream::app);
ofstream.close();
return 0;
}
// Display a Message for a specific amount of time.
void ScriptHelper::displayTimeMsg(std::string message, int seconds) {
Msg::DisplayMsg(message);
for (int i = 0; i < 60*seconds; i++) {
gspWaitForVBlank();
}
}
bool ScriptHelper::checkIfValid(std::string scriptFile, int mode) {
FILE* file = fopen(scriptFile.c_str(), "rt");
if (!file) {
printf("File not found\n");
return false;
}
nlohmann::json json = nlohmann::json::parse(file, nullptr, false);
fclose(file);
if (mode == 0) {
if (!json.contains("info")) return false;
} else if (mode == 1) {
if (!json.contains("storeInfo")) return false;
}
return true;
}
void ScriptHelper::bootTitle(const std::string TitleID, bool isNAND, std::string message) {
std::string MSG = Lang::get("BOOT_TITLE") + "\n\n";
if (isNAND) MSG += Lang::get("MEDIATYPE_NAND") + "\n" + TitleID;
else MSG += Lang::get("MEDIATYPE_SD") + "\n" + TitleID;
u64 ID = std::stoull(TitleID, 0, 16);
if (Msg::promptMsg(MSG)) {
Msg::DisplayMsg(message);
CIA_LaunchTitle(ID, isNAND ? MEDIATYPE_NAND : MEDIATYPE_SD);
}
}
Result ScriptHelper::prompt(std::string message) {
Result ret = NONE;
if (!Msg::promptMsg(message)) {
ret = SCRIPT_CANCELED;
}
return ret;
}
Result ScriptHelper::copyFile(std::string source, std::string destination, std::string message) {
Result ret = NONE;
if (access(source.c_str(), F_OK) != 0) {
return COPY_ERROR;
}
Msg::DisplayMsg(message);
// If destination does not exist, create dirs.
if (access(destination.c_str(), F_OK) != 0) {
makeDirs(destination.c_str());
}
fcopy(source.c_str(), destination.c_str());
return ret;
}
Result ScriptHelper::renameFile(std::string oldName, std::string newName, std::string message) {
Result ret = NONE;
if (access(oldName.c_str(), F_OK) != 0) {
return MOVE_ERROR;
}
Msg::DisplayMsg(message);
// TODO: Kinda avoid that?
makeDirs(newName.c_str());
rename(oldName.c_str(), newName.c_str());
return ret;
}
+458
View File
@@ -0,0 +1,458 @@
/*
* This file is part of Universal-Updater
* Copyright (C) 2019-2020 Universal-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#include "animation.hpp"
#include "cia.hpp"
#include "download.hpp"
#include "extract.hpp"
#include "fileBrowse.hpp"
#include "files.hpp"
#include "scriptUtils.hpp"
#include <regex>
#include <unistd.h>
extern bool showProgressBar;
extern ProgressBar progressbarType;
extern char progressBarMsg[128];
extern int filesExtracted;
extern void downloadFailed();
static Thread thread;
bool ScriptUtils::matchPattern(const std::string &pattern, const std::string &tested) {
std::regex patternRegex(pattern);
return regex_match(tested, patternRegex);
}
/*
Remove a File.
*/
Result ScriptUtils::removeFile(const std::string &file, const std::string &message) {
std::string out;
out = std::regex_replace(file, std::regex("%ARCHIVE_DEFAULT%"), config->archPath());
Result ret = NONE;
if (access(out.c_str(), F_OK) != 0) return DELETE_ERROR;
Msg::DisplayMsg(message);
deleteFile(out.c_str());
return ret;
}
/*
Boot a title.
*/
void ScriptUtils::bootTitle(const std::string &TitleID, const bool &isNAND, const std::string &message) {
std::string MSG = Lang::get("BOOT_TITLE") + "\n\n";
if (isNAND) MSG += Lang::get("MEDIATYPE_NAND") + "\n" + TitleID;
else MSG += Lang::get("MEDIATYPE_SD") + "\n" + TitleID;
const u64 ID = std::stoull(TitleID, 0, 16);
if (Msg::promptMsg(MSG)) {
Msg::DisplayMsg(message);
CIA_LaunchTitle(ID, isNAND ? MEDIATYPE_NAND : MEDIATYPE_SD);
}
}
/*
Prompt message.
*/
Result ScriptUtils::prompt(const std::string &message) {
Result ret = NONE;
if (!Msg::promptMsg(message)) ret = SCRIPT_CANCELED;
return ret;
}
/*
Copy.
*/
Result ScriptUtils::copyFile(const std::string &source, const std::string &destination, const std::string &message) {
Result ret = NONE;
if (access(source.c_str(), F_OK) != 0) return COPY_ERROR;
Msg::DisplayMsg(message);
/* If destination does not exist, create dirs. */
if (access(destination.c_str(), F_OK) != 0) makeDirs(destination.c_str());
fcopy(source.c_str(), destination.c_str());
return ret;
}
/*
Rename / Move a file.
*/
Result ScriptUtils::renameFile(const std::string &oldName, const std::string &newName, const std::string &message) {
Result ret = NONE;
if (access(oldName.c_str(), F_OK) != 0) return MOVE_ERROR;
Msg::DisplayMsg(message);
/* TODO: Kinda avoid that? */
makeDirs(newName.c_str());
rename(oldName.c_str(), newName.c_str());
return ret;
}
/*
Download from GitHub Release.
*/
Result ScriptUtils::downloadRelease(const std::string &repo, const std::string &file, const std::string &output, const bool &includePrereleases, const std::string &message) {
std::string out;
out = std::regex_replace(output, std::regex("%3DSX%"), config->_3dsxPath());
out = std::regex_replace(out, std::regex("%NDS%"), config->ndsPath());
out = std::regex_replace(out, std::regex("%ARCHIVE_DEFAULT%"), config->archPath());
Result ret = NONE;
snprintf(progressBarMsg, sizeof(progressBarMsg), message.c_str());
showProgressBar = true;
progressbarType = ProgressBar::Downloading;
s32 prio = 0;
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
thread = threadCreate((ThreadFunc)displayProgressBar, NULL, 64 * 1024, prio - 1, -2, false);
if (downloadFromRelease("https://github.com/" + repo, file, out, includePrereleases) != 0) {
showProgressBar = false;
downloadFailed();
ret = FAILED_DOWNLOAD;
threadJoin(thread, U64_MAX);
threadFree(thread);
return ret;
}
showProgressBar = false;
threadJoin(thread, U64_MAX);
threadFree(thread);
return ret;
}
/*
Download a file.
*/
Result ScriptUtils::downloadFile(const std::string &file, const std::string &output, const std::string &message) {
std::string out;
out = std::regex_replace(output, std::regex("%3DSX%"), config->_3dsxPath());
out = std::regex_replace(out, std::regex("%NDS%"), config->ndsPath());
out = std::regex_replace(out, std::regex("%ARCHIVE_DEFAULT%"), config->archPath());
Result ret = NONE;
snprintf(progressBarMsg, sizeof(progressBarMsg), message.c_str());
showProgressBar = true;
progressbarType = ProgressBar::Downloading;
s32 prio = 0;
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
thread = threadCreate((ThreadFunc)displayProgressBar, NULL, 64 * 1024, prio - 1, -2, false);
if (downloadToFile(file, out) != 0) {
showProgressBar = false;
downloadFailed();
ret = FAILED_DOWNLOAD;
threadJoin(thread, U64_MAX);
threadFree(thread);
return ret;
}
showProgressBar = false;
threadJoin(thread, U64_MAX);
threadFree(thread);
return ret;
}
/*
Install CIA files.
*/
void ScriptUtils::installFile(const std::string &file, const bool &updatingSelf, const std::string &message) {
snprintf(progressBarMsg, sizeof(progressBarMsg), message.c_str());
showProgressBar = true;
progressbarType = ProgressBar::Installing;
s32 prio = 0;
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
thread = threadCreate((ThreadFunc)displayProgressBar, NULL, 64 * 1024, prio - 1, -2, false);
installCia(file.c_str(), updatingSelf);
showProgressBar = false;
threadJoin(thread, U64_MAX);
threadFree(thread);
}
/*
Extract files.
*/
void ScriptUtils::extractFile(const std::string &file, const std::string &input, const std::string &output, const std::string &message) {
std::string out, in;
in = std::regex_replace(file, std::regex("%ARCHIVE_DEFAULT%"), config->archPath());
out = std::regex_replace(output, std::regex("%ARCHIVE_DEFAULT%"), config->archPath());
snprintf(progressBarMsg, sizeof(progressBarMsg), message.c_str());
showProgressBar = true;
filesExtracted = 0;
progressbarType = ProgressBar::Extracting;
s32 prio = 0;
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
thread = threadCreate((ThreadFunc)displayProgressBar, NULL, 64 * 1024, prio - 1, -2, false);
extractArchive(in, input, out);
showProgressBar = false;
threadJoin(thread, U64_MAX);
threadFree(thread);
}
/*
Execute | run the script.
*/
Result ScriptUtils::runFunctions(const nlohmann::json &storeJson, const int &selection, const std::string &entry) {
Result ret = NONE; // No Error as of yet.
if (!storeJson.contains("storeContent")) { Msg::waitMsg(Lang::get("SYNTAX_ERROR")); return SYNTAX_ERROR; };
if ((int)storeJson["storeContent"].size() < selection) { Msg::waitMsg(Lang::get("SYNTAX_ERROR")); return SYNTAX_ERROR; };
if (!storeJson["storeContent"][selection].contains(entry)) { Msg::waitMsg(Lang::get("SYNTAX_ERROR")); return SYNTAX_ERROR; };
for(int i = 0; i < (int)storeJson["storeContent"][selection][entry].size(); i++) {
if (ret == NONE) {
std::string type = "";
if (storeJson["storeContent"][selection][entry][i].contains("type") && storeJson["storeContent"][selection][entry][i]["type"].is_string()) {
type = storeJson["storeContent"][selection][entry][i]["type"];
} else {
ret = SYNTAX_ERROR;
}
if (type == "deleteFile") {
bool missing = false;
std::string file = "", message = "";
if (storeJson["storeContent"][selection][entry][i].contains("file") && storeJson["storeContent"][selection][entry][i]["file"].is_string()) {
file = storeJson["storeContent"][selection][entry][i]["file"];
}
else missing = true;
if (storeJson["storeContent"][selection][entry][i].contains("message") && storeJson["storeContent"][selection][entry][i]["message"].is_string()) {
message = storeJson["storeContent"][selection][entry][i]["message"];
}
if (!missing) ret = ScriptUtils::removeFile(file, message);
else ret = SYNTAX_ERROR;
} else if (type == "downloadFile") {
bool missing = false;
std::string file = "", output = "", message = "";
if (storeJson["storeContent"][selection][entry][i].contains("file") && storeJson["storeContent"][selection][entry][i]["file"].is_string()) {
file = storeJson["storeContent"][selection][entry][i]["file"];
}
else missing = true;
if (storeJson["storeContent"][selection][entry][i].contains("output") && storeJson["storeContent"][selection][entry][i]["output"].is_string()) {
output = storeJson["storeContent"][selection][entry][i]["output"];
}
else missing = true;
if (storeJson["storeContent"][selection][entry][i].contains("message") && storeJson["storeContent"][selection][entry][i]["message"].is_string()) {
message = storeJson["storeContent"][selection][entry][i]["message"];
}
if (!missing) ret = ScriptUtils::downloadFile(file, output, message);
else ret = SYNTAX_ERROR;
} else if (type == "downloadRelease") {
bool missing = false, includePrereleases = false;
std::string repo = "", file = "", output = "", message = "";
if (storeJson["storeContent"][selection][entry][i].contains("repo") && storeJson["storeContent"][selection][entry][i]["repo"].is_string()) {
repo = storeJson["storeContent"][selection][entry][i]["repo"];
}
else missing = true;
if (storeJson["storeContent"][selection][entry][i].contains("file") && storeJson["storeContent"][selection][entry][i]["file"].is_string()) {
file = storeJson["storeContent"][selection][entry][i]["file"];
}
else missing = true;
if (storeJson["storeContent"][selection][entry][i].contains("output") && storeJson["storeContent"][selection][entry][i]["output"].is_string()) {
output = storeJson["storeContent"][selection][entry][i]["output"];
}
else missing = true;
if (storeJson["storeContent"][selection][entry][i].contains("includePrereleases") && storeJson["storeContent"][selection][entry][i]["includePrereleases"].is_boolean())
includePrereleases = storeJson["storeContent"][selection][entry][i]["includePrereleases"];
if (storeJson["storeContent"][selection][entry][i].contains("message") && storeJson["storeContent"][selection][entry][i]["message"].is_string()) {
message = storeJson["storeContent"][selection][entry][i]["message"];
}
if (!missing) ret = ScriptUtils::downloadRelease(repo, file, output, includePrereleases, message);
else ret = SYNTAX_ERROR;
} else if (type == "extractFile") {
bool missing = false;
std::string file = "", input = "", output = "", message = "";
if (storeJson["storeContent"][selection][entry][i].contains("file") && storeJson["storeContent"][selection][entry][i]["file"].is_string()) {
file = storeJson["storeContent"][selection][entry][i]["file"];
}
else missing = true;
if (storeJson["storeContent"][selection][entry][i].contains("input") && storeJson["storeContent"][selection][entry][i]["input"].is_string()) {
input = storeJson["storeContent"][selection][entry][i]["input"];
}
else missing = true;
if (storeJson["storeContent"][selection][entry][i].contains("output") && storeJson["storeContent"][selection][entry][i]["output"].is_string()) {
output = storeJson["storeContent"][selection][entry][i]["output"];
}
else missing = true;
if (storeJson["storeContent"][selection][entry][i].contains("message") && storeJson["storeContent"][selection][entry][i]["message"].is_string()) {
message = storeJson["storeContent"][selection][entry][i]["message"];
}
if (!missing) ScriptUtils::extractFile(file, input, output, message);
else ret = SYNTAX_ERROR;
} else if (type == "installCia") {
bool missing = false, updateSelf = false;
std::string file = "", message = "";
if (storeJson["storeContent"][selection][entry][i].contains("file") && storeJson["storeContent"][selection][entry][i]["file"].is_string()) {
file = storeJson["storeContent"][selection][entry][i]["file"];
}
else missing = true;
if (storeJson["storeContent"][selection][entry][i].contains("updateSelf") && storeJson["storeContent"][selection][entry][i]["updateSelf"].is_boolean()) {
updateSelf = storeJson["storeContent"][selection][entry][i]["updateSelf"];
}
if (storeJson["storeContent"][selection][entry][i].contains("message") && storeJson["storeContent"][selection][entry][i]["message"].is_string()) {
message = storeJson["storeContent"][selection][entry][i]["message"];
}
if (!missing) ScriptUtils::installFile(file, updateSelf, message);
else ret = SYNTAX_ERROR;
} else if (type == "mkdir") {
bool missing = false;
std::string directory = "", message = "";
if (storeJson["storeContent"][selection][entry][i].contains("directory") && storeJson["storeContent"][selection][entry][i]["directory"].is_string()) {
directory = storeJson["storeContent"][selection][entry][i]["directory"];
}
else missing = true;
if (!missing) makeDirs(directory.c_str());
else ret = SYNTAX_ERROR;
} else if (type == "rmdir") {
bool missing = false;
std::string directory = "", message = "", promptmsg = "";
if (storeJson["storeContent"][selection][entry][i].contains("directory") && storeJson["storeContent"][selection][entry][i]["directory"].is_string()) {
directory = storeJson["storeContent"][selection][entry][i]["directory"];
}
else missing = true;
promptmsg = Lang::get("DELETE_PROMPT") + "\n" + directory;
if (!missing && directory != "") {
if (access(directory.c_str(), F_OK) != 0) ret = DELETE_ERROR;
else {
if (Msg::promptMsg(promptmsg)) removeDirRecursive(directory.c_str());
}
}
else ret = SYNTAX_ERROR;
} else if (type == "promptMessage") {
std::string Message = "";
if (storeJson["storeContent"][selection][entry][i].contains("message") && storeJson["storeContent"][selection][entry][i]["message"].is_string()) {
Message = storeJson["storeContent"][selection][entry][i]["message"];
}
ret = ScriptUtils::prompt(Message);
if (ret == SCRIPT_CANCELED) {
ret = NONE;
i++; // Skip.
}
} else if (type == "copy") {
std::string Message = "", source = "", destination = "";
bool missing = false;
if (storeJson["storeContent"][selection][entry][i].contains("source") && storeJson["storeContent"][selection][entry][i]["source"].is_string()) {
source = storeJson["storeContent"][selection][entry][i]["source"];
}
else missing = true;
if (storeJson["storeContent"][selection][entry][i].contains("destination") && storeJson["storeContent"][selection][entry][i]["destination"].is_string()) {
destination = storeJson["storeContent"][selection][entry][i]["destination"];
}
else missing = true;
if (storeJson["storeContent"][selection][entry][i].contains("message") && storeJson["storeContent"][selection][entry][i]["message"].is_string()) {
Message = storeJson["storeContent"][selection][entry][i]["message"];
}
if (!missing) ret = ScriptUtils::copyFile(source, destination, Message);
else ret = SYNTAX_ERROR;
} else if (type == "move") {
std::string Message = "", oldFile = "", newFile = "";
bool missing = false;
if (storeJson["storeContent"][selection][entry][i].contains("old") && storeJson["storeContent"][selection][entry][i]["old"].is_string()) {
oldFile = storeJson["storeContent"][selection][entry][i]["old"];
}
else missing = true;
if (storeJson["storeContent"][selection][entry][i].contains("new") && storeJson["storeContent"][selection][entry][i]["new"].is_string()) {
newFile = storeJson["storeContent"][selection][entry][i]["new"];
}
else missing = true;
if (storeJson["storeContent"][selection][entry][i].contains("message") && storeJson["storeContent"][selection][entry][i]["message"].is_string()) {
Message = storeJson["storeContent"][selection][entry][i]["message"];
}
if (!missing) ret = ScriptUtils::renameFile(oldFile, newFile, Message);
else ret = SYNTAX_ERROR;
}
}
}
if (ret == NONE) doneMsg();
else if (ret == FAILED_DOWNLOAD) Msg::waitMsg(Lang::get("DOWNLOAD_ERROR"));
else if (ret == SYNTAX_ERROR) Msg::waitMsg(Lang::get("SYNTAX_ERROR"));
else if (ret == COPY_ERROR) Msg::waitMsg(Lang::get("COPY_ERROR"));
else if (ret == MOVE_ERROR) Msg::waitMsg(Lang::get("MOVE_ERROR"));
else if (ret == DELETE_ERROR) Msg::waitMsg(Lang::get("DELETE_ERROR"));
return ret;
}
-125
View File
@@ -1,125 +0,0 @@
#include "sound.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
using std::string;
// Reference: http://yannesposito.com/Scratch/en/blog/2010-10-14-Fun-with-wav/
typedef struct _WavHeader {
char magic[4]; // "RIFF"
u32 totallength; // Total file length, minus 8.
char wavefmt[8]; // Should be "WAVEfmt "
u32 format; // 16 for PCM format
u16 pcm; // 1 for PCM format
u16 channels; // Channels
u32 frequency; // Sampling frequency
u32 bytes_per_second;
u16 bytes_by_capture;
u16 bits_per_sample;
char data[4]; // "data"
u32 bytes_in_data;
} WavHeader;
static_assert(sizeof(WavHeader) == 44, "WavHeader size is not 44 bytes.");
sound::sound(const string& path, int channel, bool toloop) {
ndspSetOutputMode(NDSP_OUTPUT_STEREO);
ndspSetOutputCount(2); // Num of buffers
// Reading wav file
FILE* fp = fopen(path.c_str(), "rb");
if (!fp) {
printf("Could not open the WAV file: %s\n", path.c_str());
return;
}
WavHeader wavHeader;
size_t read = fread(&wavHeader, 1, sizeof(wavHeader), fp);
if (read != sizeof(wavHeader)) {
// Short read.
printf("WAV file header is too short: %s\n", path.c_str());
fclose(fp);
return;
}
// Verify the header.
static const char RIFF_magic[4] = {'R','I','F','F'};
if (memcmp(wavHeader.magic, RIFF_magic, sizeof(wavHeader.magic)) != 0) {
// Incorrect magic number.
printf("Wrong file format.\n");
fclose(fp);
return;
}
if (wavHeader.totallength == 0 ||
(wavHeader.channels != 1 && wavHeader.channels != 2) ||
(wavHeader.bits_per_sample != 8 && wavHeader.bits_per_sample != 16)) {
// Unsupported WAV file.
printf("Corrupted wav file.\n");
fclose(fp);
return;
}
// Get the file size.
fseek(fp, 0, SEEK_END);
dataSize = ftell(fp) - sizeof(wavHeader);
// Allocating and reading samples
data = static_cast<u8*>(linearAlloc(dataSize));
fseek(fp, 44, SEEK_SET);
fread(data, 1, dataSize, fp);
fclose(fp);
dataSize /= 2; // FIXME: 16-bit or stereo?
// Find the right format
u16 ndspFormat;
if (wavHeader.bits_per_sample == 8) {
ndspFormat = (wavHeader.channels == 1) ?
NDSP_FORMAT_MONO_PCM8 :
NDSP_FORMAT_STEREO_PCM8;
} else {
ndspFormat = (wavHeader.channels == 1) ?
NDSP_FORMAT_MONO_PCM16 :
NDSP_FORMAT_STEREO_PCM16;
}
ndspChnReset(channel);
ndspChnSetInterp(channel, NDSP_INTERP_NONE);
ndspChnSetRate(channel, float(wavHeader.frequency));
ndspChnSetFormat(channel, ndspFormat);
// Create and play a wav buffer
memset(&waveBuf, 0, sizeof(waveBuf));
waveBuf.data_vaddr = reinterpret_cast<u32*>(data);
waveBuf.nsamples = dataSize / (wavHeader.bits_per_sample >> 3);
waveBuf.looping = toloop;
waveBuf.status = NDSP_WBUF_FREE;
chnl = channel;
}
sound::~sound() {
waveBuf.data_vaddr = 0;
waveBuf.nsamples = 0;
waveBuf.looping = false;
waveBuf.status = 0;
ndspChnWaveBufClear(chnl);
if (data) {
linearFree(data);
}
}
void sound::play() {
if (!data) return;
DSP_FlushDataCache(data, dataSize);
ndspChnWaveBufAdd(chnl, &waveBuf);
}
void sound::stop() {
if (!data) return;
ndspChnWaveBufClear(chnl);
}
-292
View File
@@ -1,292 +0,0 @@
/*
* This file is part of Universal-Updater
* Copyright (C) 2019-2020 Universal-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#include "store.hpp"
#include <unistd.h>
Store::Store(nlohmann::json &JS, std::string JSONName) {
this->storeJson = JS;
this->updateFile = JSONName;
if (access("sdmc:/3ds/Universal-Updater/updates.json", F_OK) != 0) {
// We'd create the file here.
FILE *file = fopen("sdmc:/3ds/Universal-Updater/updates.json", "w");
this->updateJSON = nlohmann::json::parse("{}"); // So we have a valid JSON at the end.
fwrite(this->updateJSON.dump(1, '\t').c_str(), 1, this->updateJSON.dump(1, '\t').size(), file);
fclose(file);
FILE *file2 = fopen("sdmc:/3ds/Universal-Updater/updates.json", "r");
this->updateJSON = nlohmann::json::parse(file2, nullptr, false);
fclose(file2);
} else {
FILE *file = fopen("sdmc:/3ds/Universal-Updater/updates.json", "r");
this->updateJSON = nlohmann::json::parse(file, nullptr, false);
fclose(file);
}
for (int i = 0; i < (int)this->storeJson.at("storeContent").size(); i++) {
this->unsortedStore.push_back(this->getData(i));
}
this->sortedStore = this->unsortedStore; // Put that to sorted store as well.
// If Categories available, push them to our vector.
if (this->storeJson["storeInfo"].contains("categories")) {
this->availableCategories = this->storeJson["storeInfo"]["categories"].get<std::vector<std::string>>();
}
// If Authors available, push them to our vector.
if (this->storeJson["storeInfo"].contains("authors")) {
this->availableAuthors = this->storeJson["storeInfo"]["authors"].get<std::vector<std::string>>();
}
// If Systems available, push them to our vector.
if (this->storeJson["storeInfo"].contains("consoles")) {
this->availableSystems = this->storeJson["storeInfo"]["consoles"].get<std::vector<std::string>>();
}
}
bool Store::updateAvailable(int index) {
if (index > (int)this->storeJson.at("storeContent").size()) return false; // out of scope.
if (this->storeJson["storeContent"][index]["info"].contains("last_updated")) {
const std::string updateEntry = this->storeJson["storeContent"][index]["info"]["last_updated"];
const std::string entry = this->storeJson["storeContent"][index]["info"]["title"];
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;
} else {
return false; // Since we do not have this entry there yet.
}
} else { // Our update json don't have that yet.. so display available.
return false;
}
} else { // Since the Store doesn't have that feature.
return false;
}
return false;
}
// Here we write that to our file.
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;
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;
}
// Here we get the data of the UniStore!
UniStoreV2Struct Store::getData(const int index) {
UniStoreV2Struct temp = {"", "", "", "", "", "" ,"", -1, 0, false};
if (index > (int)this->storeJson["storeContent"].size()) return temp; // Empty.
// Here we check.
// Title.
if (this->storeJson["storeContent"][index]["info"].contains("title")) {
temp.title = this->storeJson["storeContent"][index]["info"]["title"];
}
// Author.
if (this->storeJson["storeContent"][index]["info"].contains("author")) {
temp.author = this->storeJson["storeContent"][index]["info"]["author"];
}
// Description.
if (this->storeJson["storeContent"][index]["info"].contains("description")) {
temp.description = this->storeJson["storeContent"][index]["info"]["description"];
}
// Version.
if (this->storeJson["storeContent"][index]["info"].contains("version")) {
temp.version = this->storeJson["storeContent"][index]["info"]["version"];
}
if (this->storeJson["storeContent"][index]["info"].contains("category")) {
temp.category = this->storeJson["storeContent"][index]["info"]["category"];
}
// Console.
if (this->storeJson["storeContent"][index]["info"].contains("console")) {
temp.console = this->storeJson["storeContent"][index]["info"]["console"];
}
// Last updated.
if (this->storeJson["storeContent"][index]["info"].contains("last_updated")) {
temp.last_updated = this->storeJson["storeContent"][index]["info"]["last_updated"];
}
// Icon index.
if (this->storeJson["storeContent"][index]["info"].contains("icon_index")) {
temp.icon_index = this->storeJson["storeContent"][index]["info"]["icon_index"];
}
// Update available(?).
temp.updateAvailable = this->updateAvailable(index);
// JSON index.
temp.JSONIndex = index;
return temp;
}
int Store::searchForCategory(const std::string searchResult) {
std::vector<UniStoreV2Struct> temp;
for (int i = 0; i < (int)this->sortedStore.size(); i++) {
if (this->sortedStore[i].category == searchResult) {
temp.push_back({this->sortedStore[i]});
}
}
if (temp.size() != 0) {
this->sortedStore = temp;
}
return (int)temp.size();
}
int Store::searchForConsole(const std::string searchResult) {
std::vector<UniStoreV2Struct> temp;
for (int i = 0; i < (int)this->sortedStore.size(); i++) {
if (this->sortedStore[i].console == searchResult) {
temp.push_back({this->sortedStore[i]});
}
}
if (temp.size() != 0) {
this->sortedStore = temp;
}
return (int)temp.size();
}
int Store::searchForAuthor(const std::string searchResult) {
std::vector<UniStoreV2Struct> temp;
for (int i = 0; i < (int)this->sortedStore.size(); i++) {
if (this->sortedStore[i].author == searchResult) {
temp.push_back({this->sortedStore[i]});
}
}
if (temp.size() != 0) {
this->sortedStore = temp;
}
return (int)temp.size();
}
int Store::searchForEntries(const std::string searchResult) {
std::vector<UniStoreV2Struct> temp;
for (int i = 0; i < (int)this->sortedStore.size(); i++) {
if (this->sortedStore[i].title.find(searchResult) != std::string::npos) {
temp.push_back({this->sortedStore[i]});
}
}
if (temp.size() != 0) {
this->sortedStore = temp;
}
return (int)temp.size();
}
// Title.
bool compareTitleDescending(const UniStoreV2Struct& a, const UniStoreV2Struct& b) {
return strcasecmp(a.title.c_str(), b.title.c_str()) > 0;
}
bool compareTitleAscending(const UniStoreV2Struct& a, const UniStoreV2Struct& b) {
return strcasecmp(b.title.c_str(), a.title.c_str()) > 0;
}
// Author.
bool compareAuthorDescending(const UniStoreV2Struct& a, const UniStoreV2Struct& b) {
return strcasecmp(a.author.c_str(), b.author.c_str()) > 0;
}
bool compareAuthorAscending(const UniStoreV2Struct& a, const UniStoreV2Struct& b) {
return strcasecmp(b.author.c_str(), a.author.c_str()) > 0;
}
// Last updated.
bool compareUpdateDescending(const UniStoreV2Struct& a, const UniStoreV2Struct& b) {
return strcasecmp(a.last_updated.c_str(), b.last_updated.c_str()) > 0;
}
bool compareUpdateAscending(const UniStoreV2Struct& a, const UniStoreV2Struct& b) {
return strcasecmp(b.last_updated.c_str(), a.last_updated.c_str()) > 0;
}
void Store::sorting(bool Ascending, SortType sorttype) {
this->ascending = Ascending;
this->sorttype = sorttype;
switch(this->sorttype) {
case SortType::TITLE:
Ascending ? std::sort(this->sortedStore.begin(), this->sortedStore.end(), compareTitleAscending) : std::sort(this->sortedStore.begin(), this->sortedStore.end(), compareAuthorAscending);
break;
case SortType::AUTHOR:
Ascending ? std::sort(this->sortedStore.begin(), this->sortedStore.end(), compareTitleAscending) : std::sort(this->sortedStore.begin(), this->sortedStore.end(), compareAuthorDescending);
break;
case SortType::LAST_UPDATED:
Ascending ? std::sort(this->sortedStore.begin(), this->sortedStore.end(), compareUpdateAscending) : std::sort(this->sortedStore.begin(), this->sortedStore.end(), compareUpdateDescending);
break;
}
}
// Some return stuff with checks!
std::string Store::returnTitle(const int index) {
if (index > (int)this->sortedStore.size()) return "?"; // Out of scope.
return this->sortedStore[index].title;
}
std::string Store::returnAuthor(const int index) {
if (index > (int)this->sortedStore.size()) return "?"; // Out of scope.
return this->sortedStore[index].author;
}
std::string Store::returnDescription(const int index) {
if (index > (int)this->sortedStore.size()) return "?"; // Out of scope.
return this->sortedStore[index].description;
}
int Store::returnIconIndex(const int index) {
if (index > (int)this->sortedStore.size()) return -1; // Out of scope.
return this->sortedStore[index].icon_index;
}
int Store::returnJSONIndex(const int index) {
if (index > (int)this->sortedStore.size()) return -1; // Out of scope.
return this->sortedStore[index].JSONIndex;
}
int Store::getSize() { return (int)this->sortedStore.size(); }
+102 -11
View File
@@ -1,16 +1,107 @@
/*
* This file is part of Universal-Updater
* Copyright (C) 2019-2020 Universal-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#include "common.hpp"
#include "stringutils.hpp"
bool matchPattern(std::string pattern, std::string tested) {
std::regex patternRegex(pattern);
return regex_match(tested, patternRegex);
/*
To lowercase conversion.
const std::string &str: The string which should be converted.
*/
std::string StringUtils::lower_case(const std::string &str) {
std::string lower;
transform(str.begin(), str.end(), std::back_inserter(lower), tolower); // Transform the string to lowercase.
return lower;
}
std::string StringUtils::format(const std::string& fmt_str, ...) {
va_list ap;
char* fp = NULL;
va_start(ap, fmt_str);
vasprintf(&fp, fmt_str.c_str(), ap);
va_end(ap);
std::unique_ptr<char, decltype(free)*> formatted(fp, free);
return std::string(formatted.get());
/*
Fetch strings from a vector and return it as a single string.
std::vector<std::string> fetch: The vector.
*/
std::string StringUtils::FetchStringsFromVector(const std::vector<std::string> &fetch) {
std::string temp;
if (fetch.size() < 1) return ""; // Smaller than 1 --> Return empty.
for (int i = 0; i < (int)fetch.size(); i++) {
if (i != (int)fetch.size() - 1) {
temp += fetch[i] + ", ";
} else {
temp += fetch[i];
}
}
return temp;
}
/*
adapted from GM9i's byte parsing.
*/
std::string StringUtils::formatBytes(const int bytes) {
char out[32];
if (bytes == 1) snprintf(out, sizeof(out), "%d Byte", bytes);
else if (bytes < 1024) snprintf(out, sizeof(out), "%d Bytes", bytes);
else if (bytes < 1024 * 1024) snprintf(out, sizeof(out), "%.1f KB", (float)bytes / 1024);
else if (bytes < 1024 * 1024 * 1024) snprintf(out, sizeof(out), "%.1f MB", (float)bytes / 1024 / 1024);
else snprintf(out, sizeof(out), "%.1f GB", (float)bytes / 1024 / 1024 / 1024);
return out;
}
/*
Return a vector of all marks.
*/
std::vector<std::string> StringUtils::GetMarks(int marks) {
std::vector<std::string> out;
if (marks & favoriteMarks::STAR) out.push_back( "" );
if (marks & favoriteMarks::HEART) out.push_back( "" );
if (marks & favoriteMarks::DIAMOND) out.push_back( "" );
if (marks & favoriteMarks::CLUBS) out.push_back( "" );
if (marks & favoriteMarks::SPADE) out.push_back( "" );
return out;
}
/*
Return a string of all marks.
*/
std::string StringUtils::GetMarkString(int marks) {
std::string out;
if (marks & favoriteMarks::STAR) out += "";
if (marks & favoriteMarks::HEART) out += "";
if (marks & favoriteMarks::DIAMOND) out += "";
if (marks & favoriteMarks::CLUBS) out += "";
if (marks & favoriteMarks::SPADE) out += "";
return out;
}
-21
View File
@@ -1,21 +0,0 @@
#include "thread.hpp"
#include <3ds.h>
#include <stdio.h>
#include <string.h>
static std::vector<Thread> threads;
void Threads::create(ThreadFunc entrypoint) {
s32 prio = 0;
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
Thread thread = threadCreate((ThreadFunc)entrypoint, NULL, 64 * 1024, prio - 1, -2, false);
threads.push_back(thread);
}
void Threads::destroy(void) {
for (u32 i = 0; i < threads.size(); i++) {
threadJoin(threads.at(i), U64_MAX);
threadFree(threads.at(i));
}
}