1
0
Fork 0
mirror of https://github.com/HamletDuFromage/aio-switch-updater.git synced 2024-11-08 11:31:43 +00:00
AIO-switch-updater/source/utils.cpp

272 lines
9.1 KiB
C++
Raw Normal View History

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 <switch.h>
#include "download.hpp"
#include "extract.hpp"
#include "progress_event.hpp"
#include "main_frame.hpp"
#include <filesystem>
#include <fstream>
namespace i18n = brls::i18n;
using namespace i18n::literals;
2020-09-20 01:21:28 +01:00
namespace util {
2020-09-20 01:21:28 +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;
file.close();
}
return fileContent.find("DOCTYPE") == std::string::npos;
}
void downloadArchive(std::string url, archiveType type){
2021-03-16 14:56:46 +00:00
fs::createTree(DOWNLOAD_PATH);
AppletType at;
2020-09-20 01:21:28 +01:00
switch(type){
case archiveType::sigpatches:
download::downloadFile(url, SIGPATCHES_FILENAME, OFF);
2020-09-20 01:21:28 +01:00
break;
case archiveType::cheats:
download::downloadFile(url, CHEATS_FILENAME, OFF);
2020-09-20 01:21:28 +01:00
break;
case archiveType::fw:
at = appletGetAppletType();
if (at == AppletType_Application || at == AppletType_SystemApplication) {
download::downloadFile(url, FIRMWARE_FILENAME, OFF);
}
else{
2021-03-01 18:19:17 +00:00
brls::Application::crash("menus/utils/fw_warning"_i18n);
}
2020-09-20 01:21:28 +01:00
break;
case archiveType::app:
download::downloadFile(url, APP_FILENAME, OFF);
2020-09-20 01:21:28 +01:00
break;
case archiveType::cfw:
download::downloadFile(url, CFW_FILENAME, OFF);
2020-09-20 01:21:28 +01:00
break;
case archiveType::ams_cfw:
download::downloadFile(url, AMS_FILENAME, OFF);
2020-09-20 01:21:28 +01:00
}
}
int showDialogBox(std::string text, std::string opt){
int dialogResult = -1;
int result = -1;
brls::Dialog* dialog = new brls::Dialog(text);
brls::GenericEvent::Callback callback = [dialog, &dialogResult](brls::View* view) {
dialogResult = 0;
dialog->close();
};
dialog->addButton(opt, callback);
dialog->setCancelable(false);
dialog->open();
while(result == -1){
usleep(1);
result = dialogResult;
}
return result;
}
int showDialogBox(std::string text, std::string opt1, std::string opt2){
int dialogResult = -1;
int result = -1;
brls::Dialog* dialog = new brls::Dialog(text);
brls::GenericEvent::Callback callback1 = [dialog, &dialogResult](brls::View* view) {
dialogResult = 0;
dialog->close();
};
brls::GenericEvent::Callback callback2 = [dialog, &dialogResult](brls::View* view) {
dialogResult = 1;
dialog->close();
};
dialog->addButton(opt1, callback1);
dialog->addButton(opt2, callback2);
dialog->setCancelable(false);
dialog->open();
while(result == -1){
usleep(1);
result = dialogResult;
}
return result;
}
2020-10-07 01:08:39 +01:00
void extractArchive(archiveType type, std::string tag){
int overwriteInis = 0;
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){
case archiveType::sigpatches:
2020-09-21 07:53:59 +01:00
if(isArchive(SIGPATCHES_FILENAME)) {
/* std::string backup(HEKATE_IPL_PATH);
backup += ".old"; */
2020-09-21 07:53:59 +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){
extract::extract(SIGPATCHES_FILENAME, ROOT_PATH, HEKATE_IPL_PATH);
2020-09-21 07:53:59 +01:00
}
else{
extract::extract(SIGPATCHES_FILENAME);
2020-09-21 07:53:59 +01:00
}
}
else{
extract::extract(SIGPATCHES_FILENAME);
2020-09-21 07:53:59 +01:00
}
}
else{
2021-03-01 18:19:17 +00:00
brls::Application::crash("menus/utils/wrong_type_sigpatches"_i18n);
}
2020-09-20 01:21:28 +01:00
break;
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;
case archiveType::fw:
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);
}
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);
extract::extract(FIRMWARE_FILENAME, FIRMWARE_PATH);
}
2020-09-20 01:21:28 +01:00
break;
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;
case archiveType::cfw:
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);
extract::extract(CFW_FILENAME, ROOT_PATH, overwriteInis);
}
else{
2021-03-01 18:19:17 +00:00
brls::Application::crash("menus/utils/wrong_type_cfw"_i18n);
}
2020-09-20 01:21:28 +01:00
break;
case archiveType::ams_cfw:
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);
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);
if(deleteContents == 1)
removeSysmodulesFlags(AMS_CONTENTS);
extract::extract(AMS_FILENAME, ROOT_PATH, overwriteInis);
}
break;
}
if(type == archiveType::ams_cfw || type == archiveType::cfw)
fs::copyFiles(COPY_FILES_TXT);
2020-09-20 01:21:28 +01: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){
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);
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)){
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){
2020-09-20 01:21:28 +01:00
bpcInitialize();
if(reboot) bpcRebootSystem();
else bpcShutdownSystem();
bpcExit();
}
std::string getLatestTag(const std::string& url){
nlohmann::json tag = download::getRequest(url, {"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
return "";
}
void saveVersion(std::string version, const std::string& path){
std::fstream newVersion;
newVersion.open(path, std::fstream::out | std::fstream::trunc);
newVersion << version << std::endl;
newVersion.close();
}
std::string readVersion(const std::string& path){
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() {
u64 hwType;
Result rc = splGetConfig(SplConfigItem_HardwareType, &hwType);
if(R_FAILED(rc))
return true;
switch (hwType)
{
case 0:
case 1:
return true;
case 2:
case 3:
case 4:
case 5:
return false;
default:
return true;
}
};
void removeSysmodulesFlags(const std::string& directory) {
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());
}
}
}
}