1
0
Fork 0
mirror of https://github.com/HamletDuFromage/aio-switch-updater.git synced 2024-11-09 12:01:44 +00:00
AIO-switch-updater/source/utils.cpp

394 lines
13 KiB
C++
Raw Normal View History

2020-09-20 01:21:28 +01:00
#include "utils.hpp"
2021-09-11 14:48:13 +01:00
#include <switch.h>
2022-05-02 00:04:05 +01:00
#include <chrono>
2021-09-11 14:48:13 +01:00
#include <filesystem>
#include <fstream>
2022-05-02 00:04:05 +01:00
#include <thread>
2021-09-11 14:48:13 +01:00
2021-02-10 16:28:47 +00:00
#include "current_cfw.hpp"
#include "download.hpp"
#include "extract.hpp"
2021-09-11 14:48:13 +01:00
#include "fs.hpp"
2021-02-10 16:28:47 +00:00
#include "main_frame.hpp"
2021-09-11 14:48:13 +01:00
#include "progress_event.hpp"
2021-05-28 14:51:30 +01:00
#include "reboot_payload.h"
#include "unistd.h"
2021-02-10 16:28:47 +00:00
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
2021-09-11 14:48:13 +01:00
bool isArchive(const std::string& path)
{
if (std::filesystem::exists(path)) {
2021-10-19 13:07:14 +01:00
std::ifstream is(path, std::ifstream::binary);
char zip_signature[4] = {0x50, 0x4B, 0x03, 0x04}; // zip signature header PK\3\4
char signature[4];
is.read(signature, 4);
if (is.good() && std::equal(std::begin(signature), std::end(signature), std::begin(zip_signature), std::end(zip_signature))) {
return true;
}
2021-09-11 14:48:13 +01:00
}
2021-10-19 13:07:14 +01:00
return false;
2020-09-20 01:21:28 +01:00
}
void downloadArchive(const std::string& url, contentType type)
2021-09-11 14:48:13 +01:00
{
long status_code;
downloadArchive(url, type, status_code);
}
void downloadArchive(const std::string& url, contentType type, long& status_code)
2021-09-11 14:48:13 +01:00
{
fs::createTree(DOWNLOAD_PATH);
switch (type) {
case contentType::sigpatches:
2021-09-11 14:48:13 +01:00
status_code = download::downloadFile(url, SIGPATCHES_FILENAME, OFF);
break;
case contentType::cheats:
2021-09-11 14:48:13 +01:00
status_code = download::downloadFile(url, CHEATS_FILENAME, OFF);
break;
case contentType::fw:
2021-09-11 14:48:13 +01:00
status_code = download::downloadFile(url, FIRMWARE_FILENAME, OFF);
break;
case contentType::app:
2021-09-11 14:48:13 +01:00
status_code = download::downloadFile(url, APP_FILENAME, OFF);
break;
case contentType::bootloaders:
2021-09-11 14:48:13 +01:00
status_code = download::downloadFile(url, CFW_FILENAME, OFF);
break;
case contentType::ams_cfw:
2021-09-11 14:48:13 +01:00
status_code = download::downloadFile(url, AMS_FILENAME, OFF);
break;
default:
break;
2021-09-11 14:48:13 +01:00
}
ProgressEvent::instance().setStatusCode(status_code);
2020-09-20 01:21:28 +01:00
}
2022-03-30 21:23:15 +01:00
void showDialogBoxInfo(const std::string& text)
{
brls::Dialog* dialog;
dialog = new brls::Dialog(text);
brls::GenericEvent::Callback callback = [dialog](brls::View* view) {
dialog->close();
};
dialog->addButton("menus/common/ok"_i18n, callback);
dialog->setCancelable(true);
dialog->open();
}
int showDialogBoxBlocking(const std::string& text, const std::string& opt)
2021-09-11 14:48:13 +01:00
{
int result = -1;
brls::Dialog* dialog = new brls::Dialog(text);
2022-05-02 00:04:05 +01:00
brls::GenericEvent::Callback callback = [dialog, &result](brls::View* view) {
result = 0;
2021-09-11 14:48:13 +01:00
dialog->close();
};
dialog->addButton(opt, callback);
dialog->setCancelable(false);
dialog->open();
while (result == -1) {
2022-05-02 00:04:05 +01:00
std::this_thread::sleep_for(std::chrono::microseconds(10));
2021-09-11 14:48:13 +01:00
}
2022-05-02 00:04:05 +01:00
std::this_thread::sleep_for(std::chrono::microseconds(800000));
2021-09-11 14:48:13 +01:00
return result;
}
2022-03-30 21:23:15 +01:00
int showDialogBoxBlocking(const std::string& text, const std::string& opt1, const std::string& opt2)
2021-09-11 14:48:13 +01:00
{
int result = -1;
brls::Dialog* dialog = new brls::Dialog(text);
2022-05-02 00:04:05 +01:00
brls::GenericEvent::Callback callback1 = [dialog, &result](brls::View* view) {
result = 0;
2021-09-11 14:48:13 +01:00
dialog->close();
};
2022-05-02 00:04:05 +01:00
brls::GenericEvent::Callback callback2 = [dialog, &result](brls::View* view) {
result = 1;
2021-09-11 14:48:13 +01:00
dialog->close();
};
dialog->addButton(opt1, callback1);
dialog->addButton(opt2, callback2);
dialog->setCancelable(false);
dialog->open();
while (result == -1) {
2022-05-02 00:04:05 +01:00
std::this_thread::sleep_for(std::chrono::microseconds(10));
2021-09-11 14:48:13 +01:00
}
2022-05-02 00:04:05 +01:00
std::this_thread::sleep_for(std::chrono::microseconds(800000));
2021-09-11 14:48:13 +01:00
return result;
}
2021-10-19 13:07:14 +01:00
void crashIfNotArchive(contentType type)
{
std::string filename;
switch (type) {
case contentType::sigpatches:
filename = SIGPATCHES_FILENAME;
break;
case contentType::cheats:
filename = CHEATS_FILENAME;
break;
case contentType::fw:
filename = FIRMWARE_FILENAME;
break;
case contentType::app:
filename = APP_FILENAME;
break;
case contentType::bootloaders:
filename = CFW_FILENAME;
break;
case contentType::ams_cfw:
filename = AMS_FILENAME;
break;
default:
return;
}
if (!isArchive(filename)) {
brls::Application::crash("menus/utils/not_an_archive"_i18n);
}
}
void extractArchive(contentType type, const std::string& version)
2021-09-11 14:48:13 +01:00
{
chdir(ROOT_PATH);
2021-10-19 13:07:14 +01:00
crashIfNotArchive(type);
2021-09-11 14:48:13 +01:00
switch (type) {
case contentType::sigpatches:
2021-10-19 13:07:14 +01:00
extract::extract(SIGPATCHES_FILENAME);
2021-09-11 14:48:13 +01:00
break;
case contentType::cheats: {
2021-09-11 14:48:13 +01:00
std::vector<std::string> titles = extract::getInstalledTitlesNs();
titles = extract::excludeTitles(CHEATS_EXCLUDE, titles);
extract::extractCheats(CHEATS_FILENAME, titles, CurrentCfw::running_cfw, version);
2021-09-11 14:48:13 +01:00
break;
}
case contentType::fw:
2021-10-19 13:07:14 +01:00
if (std::filesystem::exists(FIRMWARE_PATH)) std::filesystem::remove_all(FIRMWARE_PATH);
fs::createTree(FIRMWARE_PATH);
extract::extract(FIRMWARE_FILENAME, FIRMWARE_PATH);
2021-09-11 14:48:13 +01:00
break;
case contentType::app:
2021-09-11 14:48:13 +01:00
extract::extract(APP_FILENAME, CONFIG_PATH);
fs::copyFile(ROMFS_FORWARDER, FORWARDER_PATH);
envSetNextLoad(FORWARDER_PATH, fmt::format("\"{}\"", FORWARDER_PATH).c_str());
2021-09-11 14:48:13 +01:00
romfsExit();
brls::Application::quit();
break;
2021-10-19 13:07:14 +01:00
case contentType::bootloaders: {
2022-03-30 21:23:15 +01:00
int overwriteInis = showDialogBoxBlocking("menus/utils/overwrite_inis"_i18n, "menus/common/no"_i18n, "menus/common/yes"_i18n);
2021-10-19 13:07:14 +01:00
extract::extract(CFW_FILENAME, ROOT_PATH, overwriteInis);
2021-09-11 14:48:13 +01:00
break;
2021-10-19 13:07:14 +01:00
}
case contentType::ams_cfw: {
2022-03-30 21:23:15 +01:00
int overwriteInis = showDialogBoxBlocking("menus/utils/overwrite_inis"_i18n, "menus/common/no"_i18n, "menus/common/yes"_i18n);
int deleteContents = showDialogBoxBlocking("menus/ams_update/delete_sysmodules_flags"_i18n, "menus/common/no"_i18n, "menus/common/yes"_i18n);
2021-10-19 13:07:14 +01:00
if (deleteContents == 1)
removeSysmodulesFlags(AMS_CONTENTS);
extract::extract(AMS_FILENAME, ROOT_PATH, overwriteInis);
2021-09-11 14:48:13 +01:00
break;
2021-10-19 13:07:14 +01:00
}
default:
break;
}
if (type == contentType::ams_cfw || type == contentType::bootloaders)
2021-09-11 14:48:13 +01:00
fs::copyFiles(COPY_FILES_TXT);
}
2020-09-20 01:21:28 +01:00
2021-09-11 14:48:13 +01:00
std::string formatListItemTitle(const std::string& str, size_t maxScore)
{
size_t score = 0;
for (size_t i = 0; i < str.length(); i++) {
score += std::isupper(str[i]) ? 4 : 3;
if (score > maxScore) {
return str.substr(0, i - 1) + "\u2026";
}
2020-09-20 01:21:28 +01:00
}
2021-09-11 14:48:13 +01:00
return str;
2020-09-20 01:21:28 +01:00
}
2021-09-11 14:48:13 +01:00
std::string formatApplicationId(u64 ApplicationId)
{
return fmt::format("{:016X}", ApplicationId);
}
2020-09-20 01:21:28 +01:00
2021-09-11 14:48:13 +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);
if (std::filesystem::exists(BOOTLOADER_PL_PATH)) payloadPaths.push_back(BOOTLOADER_PL_PATH);
if (std::filesystem::exists(SXOS_PATH)) payloadPaths.push_back(SXOS_PATH);
if (std::filesystem::exists(ROMFS_PATH)) payloadPaths.push_back(ROMFS_PATH);
2021-09-11 14:48:13 +01:00
std::vector<std::string> res;
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
}
2021-09-11 14:48:13 +01:00
return res;
2020-09-20 01:21:28 +01:00
}
2021-09-11 14:48:13 +01:00
void shutDown(bool reboot)
{
spsmInitialize();
spsmShutdown(reboot);
}
2021-09-11 14:48:13 +01:00
void rebootToPayload(const std::string& path)
{
reboot_to_payload(path.c_str(), CurrentCfw::running_cfw != CFW::ams);
}
2021-05-28 14:51:30 +01:00
2021-09-11 14:48:13 +01:00
std::string getLatestTag(const std::string& url)
{
nlohmann::ordered_json tag;
download::getRequest(url, tag, {"accept: application/vnd.github.v3+json"});
if (tag.find("tag_name") != tag.end())
return tag["tag_name"];
else
return "";
}
2021-09-11 14:48:13 +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;
}
std::string getCheatsVersion()
2021-09-11 14:48:13 +01:00
{
std::string res = util::downloadFileToString(CHEATS_URL_VERSION);
if (res == "" && isArchive(CHEATS_ZIP_PATH)) {
res = "offline";
}
return res;
2021-09-11 14:48:13 +01:00
}
void saveToFile(const std::string& text, const std::string& path)
2021-09-11 14:48:13 +01:00
{
std::ofstream file(path);
file << text << std::endl;
}
std::string readFile(const std::string& path)
{
std::string text = "";
std::ifstream file(path);
if (file.good()) {
file >> text;
2021-09-11 14:48:13 +01:00
}
return text;
}
2021-02-15 00:02:34 +00:00
2021-09-11 14:48:13 +01:00
bool isErista()
{
SetSysProductModel model;
setsysGetProductModel(&model);
return (model == SetSysProductModel_Nx || model == SetSysProductModel_Copper);
}
2021-09-11 14:48:13 +01:00
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());
2021-09-11 14:48:13 +01:00
}
}
}
2021-09-11 14:48:13 +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-09-11 14:48:13 +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-09-11 14:48:13 +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;
}
2021-09-11 14:48:13 +01:00
bool isApplet()
{
AppletType at = appletGetAppletType();
return at != AppletType_Application && at != AppletType_SystemApplication;
}
2021-09-11 14:48:13 +01:00
std::set<std::string> getExistingCheatsTids()
{
std::string path = getContentsPath();
std::set<std::string> res;
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)));
}
}
2021-09-11 14:48:13 +01:00
return res;
}
2021-09-11 14:48:13 +01:00
std::string getContentsPath()
{
std::string path;
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;
}
return path;
}
2021-09-11 14:48:13 +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; } */
2021-09-11 14:48:13 +01:00
return (jsonFile.find(key) != jsonFile.end()) ? jsonFile.at(key).get<bool>() : false;
}
2021-09-15 16:50:34 +01:00
const nlohmann::ordered_json getValueFromKey(const nlohmann::ordered_json& jsonFile, const std::string& key)
{
return (jsonFile.find(key) != jsonFile.end()) ? jsonFile.at(key) : nlohmann::ordered_json::object();
}
2021-09-11 14:48:13 +01:00
} // namespace util