2020-09-20 01:21:28 +01:00
|
|
|
#include "utils.hpp"
|
2021-03-16 14:56:46 +00:00
|
|
|
#include "fs.hpp"
|
2021-02-10 16:28:47 +00:00
|
|
|
#include "current_cfw.hpp"
|
|
|
|
#include "download.hpp"
|
|
|
|
#include "extract.hpp"
|
|
|
|
#include "progress_event.hpp"
|
|
|
|
#include "main_frame.hpp"
|
2021-05-28 14:51:30 +01:00
|
|
|
#include "reboot_payload.h"
|
|
|
|
#include "unistd.h"
|
2021-06-27 23:46:00 +01:00
|
|
|
#include "progress_event.hpp"
|
2021-06-05 18:10:51 +01:00
|
|
|
#include <switch.h>
|
2021-02-10 16:28:47 +00:00
|
|
|
#include <filesystem>
|
|
|
|
#include <fstream>
|
|
|
|
|
2020-10-05 23:53:12 +01:00
|
|
|
namespace i18n = brls::i18n;
|
|
|
|
using namespace i18n::literals;
|
2020-09-20 01:21:28 +01:00
|
|
|
|
2021-03-16 02:04:21 +00:00
|
|
|
namespace util {
|
2020-09-20 01:21:28 +01:00
|
|
|
|
2021-05-22 17:10:34 +01:00
|
|
|
bool isArchive(const std::string& path){
|
2020-09-20 01:21:28 +01:00
|
|
|
std::fstream file;
|
|
|
|
std::string fileContent;
|
|
|
|
if(std::filesystem::exists(path)){
|
|
|
|
file.open(path, std::fstream::in);
|
|
|
|
file >> fileContent;
|
|
|
|
}
|
|
|
|
return fileContent.find("DOCTYPE") == std::string::npos;
|
|
|
|
}
|
|
|
|
|
2021-07-17 13:29:55 +01:00
|
|
|
void downloadArchive(const std::string& url, archiveType type) {
|
2021-06-27 23:46:00 +01:00
|
|
|
long status_code;
|
2021-07-17 13:29:55 +01:00
|
|
|
downloadArchive(url, type, status_code);
|
2021-06-27 23:46:00 +01:00
|
|
|
}
|
|
|
|
|
2021-07-17 13:29:55 +01:00
|
|
|
void downloadArchive(const std::string& url, archiveType type, long& status_code) {
|
2021-03-16 14:56:46 +00:00
|
|
|
fs::createTree(DOWNLOAD_PATH);
|
2020-09-20 01:21:28 +01:00
|
|
|
switch(type){
|
2021-03-16 02:04:21 +00:00
|
|
|
case archiveType::sigpatches:
|
2021-06-27 23:46:00 +01:00
|
|
|
status_code = download::downloadFile(url, SIGPATCHES_FILENAME, OFF);
|
2020-09-20 01:21:28 +01:00
|
|
|
break;
|
2021-03-16 02:04:21 +00:00
|
|
|
case archiveType::cheats:
|
2021-06-27 23:46:00 +01:00
|
|
|
status_code = download::downloadFile(url, CHEATS_FILENAME, OFF);
|
2020-09-20 01:21:28 +01:00
|
|
|
break;
|
2021-03-16 02:04:21 +00:00
|
|
|
case archiveType::fw:
|
2021-07-29 15:20:40 +01:00
|
|
|
status_code = download::downloadFile(url, FIRMWARE_FILENAME, OFF);
|
2020-09-20 01:21:28 +01:00
|
|
|
break;
|
2021-03-16 02:04:21 +00:00
|
|
|
case archiveType::app:
|
2021-06-27 23:46:00 +01:00
|
|
|
status_code = download::downloadFile(url, APP_FILENAME, OFF);
|
2020-09-20 01:21:28 +01:00
|
|
|
break;
|
2021-03-16 02:04:21 +00:00
|
|
|
case archiveType::cfw:
|
2021-06-27 23:46:00 +01:00
|
|
|
status_code = download::downloadFile(url, CFW_FILENAME, OFF);
|
2020-09-20 01:21:28 +01:00
|
|
|
break;
|
2021-03-16 02:04:21 +00:00
|
|
|
case archiveType::ams_cfw:
|
2021-06-27 23:46:00 +01:00
|
|
|
status_code = download::downloadFile(url, AMS_FILENAME, OFF);
|
2020-09-20 01:21:28 +01:00
|
|
|
}
|
2021-06-27 23:46:00 +01:00
|
|
|
ProgressEvent::instance().setStatusCode(status_code);
|
2020-09-20 01:21:28 +01:00
|
|
|
}
|
|
|
|
|
2021-07-17 13:29:55 +01:00
|
|
|
int showDialogBox(const std::string& text, const std::string& opt){
|
2021-02-15 14:19:49 +00:00
|
|
|
int dialogResult = -1;
|
2020-09-22 22:12:05 +01:00
|
|
|
int result = -1;
|
|
|
|
brls::Dialog* dialog = new brls::Dialog(text);
|
2021-02-15 14:19:49 +00:00
|
|
|
brls::GenericEvent::Callback callback = [dialog, &dialogResult](brls::View* view) {
|
2020-09-22 22:12:05 +01:00
|
|
|
dialogResult = 0;
|
|
|
|
dialog->close();
|
|
|
|
};
|
|
|
|
dialog->addButton(opt, callback);
|
|
|
|
dialog->setCancelable(false);
|
|
|
|
dialog->open();
|
|
|
|
while(result == -1){
|
|
|
|
usleep(1);
|
|
|
|
result = dialogResult;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-07-17 13:29:55 +01:00
|
|
|
int showDialogBox(const std::string& text, const std::string& opt1, const std::string& opt2){
|
2021-02-15 14:19:49 +00:00
|
|
|
int dialogResult = -1;
|
2020-09-21 19:36:46 +01:00
|
|
|
int result = -1;
|
|
|
|
brls::Dialog* dialog = new brls::Dialog(text);
|
2021-02-15 14:19:49 +00:00
|
|
|
brls::GenericEvent::Callback callback1 = [dialog, &dialogResult](brls::View* view) {
|
2020-09-20 21:58:40 +01:00
|
|
|
dialogResult = 0;
|
|
|
|
dialog->close();
|
|
|
|
};
|
2021-02-15 14:19:49 +00:00
|
|
|
brls::GenericEvent::Callback callback2 = [dialog, &dialogResult](brls::View* view) {
|
2020-09-20 21:58:40 +01:00
|
|
|
dialogResult = 1;
|
|
|
|
dialog->close();
|
|
|
|
};
|
2020-09-21 19:36:46 +01:00
|
|
|
dialog->addButton(opt1, callback1);
|
|
|
|
dialog->addButton(opt2, callback2);
|
2020-09-20 21:58:40 +01:00
|
|
|
dialog->setCancelable(false);
|
2020-09-21 19:36:46 +01:00
|
|
|
dialog->open();
|
|
|
|
while(result == -1){
|
|
|
|
usleep(1);
|
|
|
|
result = dialogResult;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2020-09-20 21:58:40 +01:00
|
|
|
|
2021-07-17 13:29:55 +01:00
|
|
|
void extractArchive(archiveType type, const std::string& tag){
|
2020-09-21 19:36:46 +01:00
|
|
|
int overwriteInis = 0;
|
2021-02-22 19:51:29 +00:00
|
|
|
std::vector<std::string> titles;
|
2020-10-07 01:08:39 +01:00
|
|
|
std::string nroPath ="sdmc:" + std::string(APP_PATH);
|
|
|
|
chdir(ROOT_PATH);
|
2020-09-20 01:21:28 +01:00
|
|
|
switch(type){
|
2021-03-16 02:04:21 +00:00
|
|
|
case archiveType::sigpatches:
|
2020-09-21 07:53:59 +01:00
|
|
|
if(isArchive(SIGPATCHES_FILENAME)) {
|
2021-06-27 23:46:00 +01:00
|
|
|
/* if(std::filesystem::exists(HEKATE_IPL_PATH)){
|
2021-03-10 20:54:17 +00:00
|
|
|
overwriteInis = showDialogBox("menus/utils/overwrite"_i18n + std::string(HEKATE_IPL_PATH) +"?", "menus/common/no"_i18n, "menus/common/yes"_i18n);
|
2020-09-21 07:53:59 +01:00
|
|
|
if(overwriteInis == 0){
|
2021-03-16 02:04:21 +00:00
|
|
|
extract::extract(SIGPATCHES_FILENAME, ROOT_PATH, HEKATE_IPL_PATH);
|
2020-09-21 07:53:59 +01:00
|
|
|
}
|
|
|
|
else{
|
2021-03-16 02:04:21 +00:00
|
|
|
extract::extract(SIGPATCHES_FILENAME);
|
2020-09-21 07:53:59 +01:00
|
|
|
}
|
2021-06-27 23:46:00 +01:00
|
|
|
} */
|
|
|
|
//else{
|
2021-03-16 02:04:21 +00:00
|
|
|
extract::extract(SIGPATCHES_FILENAME);
|
2021-06-27 23:46:00 +01:00
|
|
|
//}
|
2020-09-21 07:53:59 +01:00
|
|
|
}
|
2020-09-22 22:12:05 +01:00
|
|
|
else{
|
2021-03-01 18:19:17 +00:00
|
|
|
brls::Application::crash("menus/utils/wrong_type_sigpatches"_i18n);
|
2020-09-22 22:12:05 +01:00
|
|
|
}
|
2020-09-20 01:21:28 +01:00
|
|
|
break;
|
2021-03-16 02:04:21 +00:00
|
|
|
case archiveType::cheats:
|
|
|
|
titles = extract::getInstalledTitlesNs();
|
|
|
|
titles = extract::excludeTitles(CHEATS_EXCLUDE, titles);
|
|
|
|
extract::extractCheats(CHEATS_FILENAME, titles, CurrentCfw::running_cfw);
|
2020-09-20 01:21:28 +01:00
|
|
|
break;
|
2021-03-16 02:04:21 +00:00
|
|
|
case archiveType::fw:
|
2020-09-22 22:12:05 +01:00
|
|
|
if(std::filesystem::file_size(FIRMWARE_FILENAME) < 200000){
|
2021-03-01 18:19:17 +00:00
|
|
|
brls::Application::crash("menus/utils/wrong_type_sigpatches_downloaded"_i18n);
|
2020-09-22 22:12:05 +01:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
if (std::filesystem::exists(FIRMWARE_PATH)) std::filesystem::remove_all(FIRMWARE_PATH);
|
2021-03-16 14:56:46 +00:00
|
|
|
fs::createTree(FIRMWARE_PATH);
|
2021-03-16 02:04:21 +00:00
|
|
|
extract::extract(FIRMWARE_FILENAME, FIRMWARE_PATH);
|
2020-09-22 22:12:05 +01:00
|
|
|
}
|
2020-09-20 01:21:28 +01:00
|
|
|
break;
|
2021-03-16 02:04:21 +00:00
|
|
|
case archiveType::app:
|
|
|
|
extract::extract(APP_FILENAME, CONFIG_PATH);
|
2021-03-16 14:56:46 +00:00
|
|
|
fs::copyFile(ROMFS_FORWARDER, FORWARDER_PATH);
|
2021-01-28 19:26:41 +00:00
|
|
|
envSetNextLoad(FORWARDER_PATH, ("\"" + std::string(FORWARDER_PATH) + "\"").c_str());
|
2020-10-07 01:08:39 +01:00
|
|
|
romfsExit();
|
|
|
|
brls::Application::quit();
|
2020-09-20 01:21:28 +01:00
|
|
|
break;
|
2021-03-16 02:04:21 +00:00
|
|
|
case archiveType::cfw:
|
2020-09-22 22:12:05 +01:00
|
|
|
if(isArchive(CFW_FILENAME)){
|
2021-03-10 20:54:17 +00:00
|
|
|
overwriteInis = showDialogBox("menus/utils/overwrite_inis"_i18n, "menus/common/no"_i18n, "menus/common/yes"_i18n);
|
2021-03-16 02:04:21 +00:00
|
|
|
extract::extract(CFW_FILENAME, ROOT_PATH, overwriteInis);
|
2020-09-22 22:12:05 +01:00
|
|
|
}
|
|
|
|
else{
|
2021-03-01 18:19:17 +00:00
|
|
|
brls::Application::crash("menus/utils/wrong_type_cfw"_i18n);
|
2020-09-22 22:12:05 +01:00
|
|
|
}
|
2020-09-20 01:21:28 +01:00
|
|
|
break;
|
2021-03-16 02:04:21 +00:00
|
|
|
case archiveType::ams_cfw:
|
2020-12-12 15:41:56 +00:00
|
|
|
if(isArchive(AMS_FILENAME)){
|
2021-03-10 20:54:17 +00:00
|
|
|
overwriteInis = showDialogBox("menus/utils/overwrite_inis"_i18n, "menus/common/no"_i18n, "menus/common/yes"_i18n);
|
2021-02-15 18:31:06 +00:00
|
|
|
usleep(800000);
|
2021-03-10 20:54:17 +00:00
|
|
|
int deleteContents = showDialogBox("menus/ams_update/delete_sysmodules_flags"_i18n, "menus/common/no"_i18n, "menus/common/yes"_i18n);
|
2021-02-15 18:31:06 +00:00
|
|
|
if(deleteContents == 1)
|
2021-03-02 14:05:06 +00:00
|
|
|
removeSysmodulesFlags(AMS_CONTENTS);
|
2021-03-16 02:04:21 +00:00
|
|
|
extract::extract(AMS_FILENAME, ROOT_PATH, overwriteInis);
|
2020-12-12 15:41:56 +00:00
|
|
|
}
|
2021-02-15 14:19:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-05-22 17:10:34 +01:00
|
|
|
if(type == archiveType::ams_cfw || type == archiveType::cfw)
|
|
|
|
fs::copyFiles(COPY_FILES_TXT);
|
2020-09-20 01:21:28 +01:00
|
|
|
}
|
|
|
|
|
2021-03-16 02:04:21 +00:00
|
|
|
std::string formatListItemTitle(const std::string &str, size_t maxScore) {
|
2020-09-20 01:21:28 +01:00
|
|
|
size_t score = 0;
|
2021-03-16 14:56:46 +00:00
|
|
|
for (size_t i = 0; i < str.length(); i++) {
|
2020-09-20 01:21:28 +01:00
|
|
|
score += std::isupper(str[i]) ? 4 : 3;
|
2021-03-16 14:56:46 +00:00
|
|
|
if(score > maxScore) {
|
2020-09-20 01:21:28 +01:00
|
|
|
return str.substr(0, i-1) + "\u2026";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string formatApplicationId(u64 ApplicationId){
|
2021-03-14 15:24:56 +00:00
|
|
|
return fmt::format("{:016X}", ApplicationId);
|
2020-09-20 01:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> fetchPayloads(){
|
|
|
|
std::vector<std::string> payloadPaths;
|
|
|
|
payloadPaths.push_back(ROOT_PATH);
|
|
|
|
if(std::filesystem::exists(PAYLOAD_PATH)) payloadPaths.push_back(PAYLOAD_PATH);
|
|
|
|
if(std::filesystem::exists(AMS_PATH)) payloadPaths.push_back(AMS_PATH);
|
|
|
|
if(std::filesystem::exists(REINX_PATH)) payloadPaths.push_back(REINX_PATH);
|
2020-12-12 15:41:56 +00:00
|
|
|
if(std::filesystem::exists(BOOTLOADER_PATH)) payloadPaths.push_back(BOOTLOADER_PATH);
|
2020-09-20 01:21:28 +01:00
|
|
|
if(std::filesystem::exists(BOOTLOADER_PL_PATH)) payloadPaths.push_back(BOOTLOADER_PL_PATH);
|
|
|
|
if(std::filesystem::exists(SXOS_PATH)) payloadPaths.push_back(SXOS_PATH);
|
|
|
|
std::vector<std::string> res;
|
2021-03-16 14:56:46 +00:00
|
|
|
for (const auto& path : payloadPaths){
|
|
|
|
for (const auto& entry : std::filesystem::directory_iterator(path)){
|
2021-01-17 17:58:20 +00:00
|
|
|
if(entry.path().extension().string() == ".bin"){
|
|
|
|
if(entry.path().string() != FUSEE_SECONDARY && entry.path().string() != FUSEE_MTC)
|
|
|
|
res.push_back(entry.path().string().c_str());
|
|
|
|
}
|
2020-09-20 01:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-03-16 14:56:46 +00:00
|
|
|
void shutDown(bool reboot){
|
2021-06-17 21:40:54 +01:00
|
|
|
spsmInitialize();
|
|
|
|
spsmShutdown(reboot);
|
2020-09-20 21:58:40 +01:00
|
|
|
}
|
|
|
|
|
2021-05-28 14:51:30 +01:00
|
|
|
void rebootToPayload(const std::string& path) {
|
2021-06-17 21:40:54 +01:00
|
|
|
reboot_to_payload(path.c_str(), CurrentCfw::running_cfw != CFW::ams);
|
2021-05-28 14:51:30 +01:00
|
|
|
}
|
|
|
|
|
2021-06-27 23:46:00 +01:00
|
|
|
std::string getLatestTag(const std::string& url) {
|
|
|
|
nlohmann::ordered_json tag;
|
|
|
|
download::getRequest(url, tag, {"accept: application/vnd.github.v3+json"});
|
2021-02-26 18:21:49 +00:00
|
|
|
if(tag.find("tag_name") != tag.end())
|
|
|
|
return tag["tag_name"];
|
|
|
|
else
|
2020-09-20 21:58:40 +01:00
|
|
|
return "";
|
2020-09-23 12:21:05 +01:00
|
|
|
}
|
|
|
|
|
2021-06-27 23:46:00 +01:00
|
|
|
std::string downloadFileToString(const std::string& url) {
|
|
|
|
std::vector<uint8_t> bytes;
|
|
|
|
download::downloadFile(url, bytes);
|
|
|
|
std::string str(bytes.begin(), bytes.end());
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2021-07-17 13:29:55 +01:00
|
|
|
void saveVersion(const std::string& version, const std::string& path){
|
2021-05-28 14:51:30 +01:00
|
|
|
std::ofstream newVersion(path);
|
2020-09-29 15:41:43 +01:00
|
|
|
newVersion << version << std::endl;
|
|
|
|
}
|
|
|
|
|
2021-05-22 17:10:34 +01:00
|
|
|
std::string readVersion(const std::string& path){
|
2020-09-29 15:41:43 +01:00
|
|
|
std::fstream versionFile;
|
|
|
|
std::string version = "0";
|
|
|
|
if(std::filesystem::exists(path)){
|
|
|
|
versionFile.open(path, std::fstream::in);
|
|
|
|
versionFile >> version;
|
|
|
|
versionFile.close();
|
|
|
|
}
|
|
|
|
return version;
|
|
|
|
}
|
2021-02-15 00:02:34 +00:00
|
|
|
|
2021-02-17 18:00:12 +00:00
|
|
|
bool isErista() {
|
2021-06-17 21:40:54 +01:00
|
|
|
SetSysProductModel model;
|
|
|
|
setsysGetProductModel(&model);
|
|
|
|
return (model == SetSysProductModel_Nx || model == SetSysProductModel_Copper);
|
|
|
|
}
|
2021-03-02 14:05:06 +00:00
|
|
|
|
2021-05-22 17:10:34 +01:00
|
|
|
void removeSysmodulesFlags(const std::string& directory) {
|
2021-03-02 14:05:06 +00:00
|
|
|
for (const auto & e : std::filesystem::recursive_directory_iterator(directory)) {
|
|
|
|
if(e.path().string().find("boot2.flag") != std::string::npos) {
|
|
|
|
std::filesystem::remove(e.path());
|
|
|
|
}
|
|
|
|
}
|
2021-03-14 15:24:56 +00:00
|
|
|
}
|
|
|
|
|
2021-06-27 23:46:00 +01:00
|
|
|
std::string lowerCase(const std::string& str) {
|
|
|
|
std::string res = str;
|
|
|
|
std::for_each(res.begin(), res.end(), [](char & c){
|
|
|
|
c = std::tolower(c);
|
|
|
|
});
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-06-28 15:58:04 +01:00
|
|
|
std::string upperCase(const std::string& str) {
|
|
|
|
std::string res = str;
|
|
|
|
std::for_each(res.begin(), res.end(), [](char & c){
|
|
|
|
c = std::toupper(c);
|
|
|
|
});
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-06-27 23:46:00 +01:00
|
|
|
std::string getErrorMessage(long status_code) {
|
|
|
|
std::string res;
|
|
|
|
switch(status_code) {
|
|
|
|
case 500:
|
|
|
|
res = fmt::format("{0:}: Internal Server Error", status_code);
|
|
|
|
break;
|
|
|
|
case 503:
|
|
|
|
res = fmt::format("{0:}: Service Temporarily Unavailable", status_code);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
res = fmt::format("error: {0:}", status_code);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isApplet() {
|
|
|
|
AppletType at = appletGetAppletType();
|
|
|
|
return at != AppletType_Application && at != AppletType_SystemApplication;
|
|
|
|
}
|
|
|
|
|
2021-06-28 15:58:04 +01:00
|
|
|
std::set<std::string> getExistingCheatsTids() {
|
2021-07-15 18:56:59 +01:00
|
|
|
std::string path = getContentsPath();
|
2021-06-28 15:58:04 +01:00
|
|
|
std::set<std::string> res;
|
2021-07-15 18:56:59 +01:00
|
|
|
for(const auto& entry : std::filesystem::directory_iterator(path)) {
|
|
|
|
std::string cheatsPath = entry.path().string() + "/cheats";
|
|
|
|
if(std::filesystem::exists(cheatsPath)){
|
|
|
|
res.insert(util::upperCase(cheatsPath.substr(cheatsPath.length() - 7 - 16, 16)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getContentsPath() {
|
|
|
|
std::string path;
|
2021-06-28 15:58:04 +01:00
|
|
|
switch(CurrentCfw::running_cfw){
|
|
|
|
case CFW::ams:
|
|
|
|
path = std::string(AMS_PATH) + std::string(CONTENTS_PATH);
|
|
|
|
break;
|
|
|
|
case CFW::rnx:
|
|
|
|
path = std::string(REINX_PATH) + std::string(CONTENTS_PATH);
|
|
|
|
break;
|
|
|
|
case CFW::sxos:
|
|
|
|
path = std::string(SXOS_PATH) + std::string(TITLES_PATH);
|
|
|
|
break;
|
|
|
|
}
|
2021-07-15 18:56:59 +01:00
|
|
|
return path;
|
2021-06-28 15:58:04 +01:00
|
|
|
}
|
|
|
|
|
2021-07-21 14:38:43 +01:00
|
|
|
bool getBoolValue(const nlohmann::json& jsonFile, const std::string& key) {
|
|
|
|
/* try { return jsonFile.at(key); }
|
|
|
|
catch (nlohmann::json::out_of_range& e) { return false; } */
|
|
|
|
return (jsonFile.find(key) != jsonFile.end()) ? jsonFile.at(key).get<bool>() : false;
|
|
|
|
}
|
|
|
|
|
2021-03-02 14:05:06 +00:00
|
|
|
}
|