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/extract.cpp

301 lines
12 KiB
C++
Raw Normal View History

2020-09-20 01:21:28 +01:00
#include "extract.hpp"
2021-09-11 14:48:13 +01:00
#include <unzipper.h>
2021-02-10 16:28:47 +00:00
#include <algorithm>
#include <filesystem>
#include <fstream>
2021-09-11 14:48:13 +01:00
#include <iomanip>
#include <iterator>
#include <ranges>
2021-02-10 16:28:47 +00:00
#include <set>
2021-09-11 14:48:13 +01:00
#include <sstream>
#include <string>
2022-05-02 00:04:05 +01:00
#include <thread>
2021-09-11 14:48:13 +01:00
#include <vector>
2021-10-26 14:48:24 +01:00
#include "current_cfw.hpp"
#include "download.hpp"
2021-09-11 14:48:13 +01:00
#include "fs.hpp"
#include "main_frame.hpp"
#include "progress_event.hpp"
#include "utils.hpp"
namespace i18n = brls::i18n;
using namespace i18n::literals;
namespace extract {
namespace {
2021-09-11 14:48:13 +01:00
bool caselessCompare(const std::string& a, const std::string& b)
{
return strcasecmp(a.c_str(), b.c_str()) == 0;
}
2021-06-03 17:47:35 +01:00
2021-09-11 14:48:13 +01:00
void preWork(zipper::Unzipper& unzipper, const std::string& workingPath, std::vector<zipper::ZipEntry>& entries)
{
2021-06-03 17:47:35 +01:00
chdir(workingPath.c_str());
entries = unzipper.entries();
s64 uncompressedSize = 0;
s64 freeStorage;
2021-09-11 14:48:13 +01:00
for (const auto& entry : entries)
2021-06-03 17:47:35 +01:00
uncompressedSize += entry.uncompressedSize;
2021-09-11 14:48:13 +01:00
if (R_SUCCEEDED(fs::getFreeStorageSD(freeStorage))) {
if (uncompressedSize * 1.1 > freeStorage) {
2021-06-03 17:47:35 +01:00
unzipper.close();
brls::Application::crash("menus/errors/insufficient_storage"_i18n);
2022-05-02 00:04:05 +01:00
std::this_thread::sleep_for(std::chrono::microseconds(2000000));
2021-06-03 17:47:35 +01:00
brls::Application::quit();
}
}
ProgressEvent::instance().setTotalSteps(entries.size() + 1);
ProgressEvent::instance().setStep(0);
2021-06-03 17:47:35 +01:00
}
2021-09-11 14:48:13 +01:00
} // namespace
void extract(const std::string& filename, const std::string& workingPath, int overwriteInis, std::function<void()> func)
2021-09-11 14:48:13 +01:00
{
zipper::Unzipper unzipper(filename);
std::vector<zipper::ZipEntry> entries;
2021-09-11 14:48:13 +01:00
preWork(unzipper, workingPath, entries);
std::set<std::string> ignoreList = fs::readLineByLine(FILES_IGNORE);
2021-09-11 14:48:13 +01:00
for (const auto& entry : entries) {
if (ProgressEvent::instance().getInterupt()) {
break;
}
2021-09-11 14:48:13 +01:00
if ((overwriteInis == 0 && entry.name.substr(entry.name.length() - 4) == ".ini") || find_if(ignoreList.begin(), ignoreList.end(), [&entry](std::string ignored) {
u8 res = ("/" + entry.name).find(ignored);
2021-09-11 14:48:13 +01:00
return (res == 0 || res == 1); }) != ignoreList.end()) {
if (!std::filesystem::exists("/" + entry.name)) {
unzipper.extractEntry(entry.name);
}
}
else if (entry.name == "atmosphere/stratosphere.romfs" || entry.name == "atmosphere/package3") {
2021-09-11 14:48:13 +01:00
std::ofstream readonlyFile(entry.name + ".aio");
unzipper.extractEntryToStream(entry.name, readonlyFile);
}
2021-09-11 14:48:13 +01:00
else {
unzipper.extractEntry(entry.name);
if (entry.name.substr(0, 13) == "hekate_ctcaer") {
fs::copyFile("/" + entry.name, UPDATE_BIN_PATH);
2022-03-30 21:23:15 +01:00
if (CurrentCfw::running_cfw == CFW::ams && util::showDialogBoxBlocking(fmt::format("menus/utils/set_hekate_reboot_payload"_i18n, UPDATE_BIN_PATH, REBOOT_PAYLOAD_PATH), "menus/common/yes"_i18n, "menus/common/no"_i18n) == 0) {
fs::copyFile(UPDATE_BIN_PATH, REBOOT_PAYLOAD_PATH);
}
2021-09-11 14:48:13 +01:00
}
}
ProgressEvent::instance().incrementStep(1);
}
2021-09-11 14:48:13 +01:00
unzipper.close();
ProgressEvent::instance().setStep(ProgressEvent::instance().getMax());
2020-09-20 01:21:28 +01:00
}
2021-09-11 14:48:13 +01:00
std::vector<std::string> getInstalledTitlesNs()
{
std::vector<std::string> titles;
2020-09-20 01:21:28 +01:00
2021-09-11 14:48:13 +01:00
NsApplicationRecord* records = new NsApplicationRecord[MaxTitleCount]();
NsApplicationControlData* controlData = NULL;
2020-09-20 01:21:28 +01:00
2021-09-11 14:48:13 +01:00
s32 recordCount = 0;
u64 controlSize = 0;
2020-09-20 01:21:28 +01:00
2021-09-11 14:48:13 +01:00
if (R_SUCCEEDED(nsListApplicationRecord(records, MaxTitleCount, 0, &recordCount))) {
for (s32 i = 0; i < recordCount; i++) {
controlSize = 0;
free(controlData);
controlData = (NsApplicationControlData*)malloc(sizeof(NsApplicationControlData));
if (controlData == NULL) {
break;
}
else {
memset(controlData, 0, sizeof(NsApplicationControlData));
}
2020-09-20 01:21:28 +01:00
2021-09-11 14:48:13 +01:00
if (R_FAILED(nsGetApplicationControlData(NsApplicationControlSource_Storage, records[i].application_id, controlData, sizeof(NsApplicationControlData), &controlSize))) continue;
2021-03-27 00:33:20 +00:00
2021-09-11 14:48:13 +01:00
if (controlSize < sizeof(controlData->nacp)) {
continue;
}
2021-03-27 00:33:20 +00:00
2021-09-11 14:48:13 +01:00
titles.push_back(util::formatApplicationId(records[i].application_id));
}
free(controlData);
2020-09-20 01:21:28 +01:00
}
2021-09-11 14:48:13 +01:00
delete[] records;
std::sort(titles.begin(), titles.end());
return titles;
2020-09-20 01:21:28 +01:00
}
2021-09-11 14:48:13 +01:00
std::vector<std::string> excludeTitles(const std::string& path, const std::vector<std::string>& listedTitles)
{
std::vector<std::string> titles;
std::ifstream file(path);
std::string name;
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
std::transform(line.begin(), line.end(), line.begin(), ::toupper);
for (size_t i = 0; i < listedTitles.size(); i++) {
if (line == listedTitles[i]) {
titles.push_back(line);
break;
}
2020-09-20 01:21:28 +01:00
}
}
}
2021-09-11 14:48:13 +01:00
std::sort(titles.begin(), titles.end());
2020-09-20 01:21:28 +01:00
2021-09-11 14:48:13 +01:00
std::vector<std::string> diff;
std::set_difference(listedTitles.begin(), listedTitles.end(), titles.begin(), titles.end(),
std::inserter(diff, diff.begin()));
return diff;
2020-09-20 01:21:28 +01:00
}
int computeOffset(CFW cfw)
2021-09-11 14:48:13 +01:00
{
switch (cfw) {
case CFW::ams:
std::filesystem::create_directory(AMS_PATH);
std::filesystem::create_directory(AMS_CONTENTS);
chdir(AMS_PATH);
return std::string(CONTENTS_PATH).length();
2021-09-11 14:48:13 +01:00
break;
case CFW::rnx:
std::filesystem::create_directory(REINX_PATH);
std::filesystem::create_directory(REINX_CONTENTS);
chdir(REINX_PATH);
return std::string(CONTENTS_PATH).length();
2021-09-11 14:48:13 +01:00
break;
case CFW::sxos:
std::filesystem::create_directory(SXOS_PATH);
std::filesystem::create_directory(SXOS_TITLES);
chdir(SXOS_PATH);
return std::string(TITLES_PATH).length();
2021-09-11 14:48:13 +01:00
break;
}
return 0;
}
2021-09-11 14:48:13 +01:00
void extractCheats(const std::string& zipPath, const std::vector<std::string>& titles, CFW cfw, const std::string& version, bool extractAll)
{
zipper::Unzipper unzipper(zipPath);
std::vector<zipper::ZipEntry> entries = unzipper.entries();
int offset = computeOffset(cfw);
2021-09-11 14:48:13 +01:00
if (!extractAll) {
ProgressEvent::instance().setTotalSteps(titles.size() + 1);
for (const auto& title : titles) {
if (ProgressEvent::instance().getInterupt()) {
break;
}
auto matches = entries | std::views::filter([&title, offset](zipper::ZipEntry entry) {
if ((int)entry.name.size() > offset + 16 + 7) {
return caselessCompare((title.substr(0, 13)), entry.name.substr(offset, 13)) && caselessCompare(entry.name.substr(offset + 16, 7), "/cheats");
}
else {
return false;
}
});
for (const auto& match : matches) {
unzipper.extractEntry(match.name);
}
ProgressEvent::instance().incrementStep(1);
}
}
else {
ProgressEvent::instance().setTotalSteps(entries.size() + 1);
for (const auto& entry : entries) {
if (ProgressEvent::instance().getInterupt()) {
break;
}
if ((int)entry.name.size() > offset + 16 + 7 && caselessCompare(entry.name.substr(offset + 16, 7), "/cheats")) {
unzipper.extractEntry(entry.name);
}
ProgressEvent::instance().incrementStep(1);
2021-09-11 14:48:13 +01:00
}
2020-09-20 01:21:28 +01:00
}
2021-09-11 14:48:13 +01:00
unzipper.close();
if (version != "offline" && version != "") {
util::saveToFile(version, CHEATS_VERSION);
}
2021-09-11 14:48:13 +01:00
ProgressEvent::instance().setStep(ProgressEvent::instance().getMax());
2020-09-20 01:21:28 +01:00
}
void extractAllCheats(const std::string& zipPath, CFW cfw, const std::string& version)
2021-09-11 14:48:13 +01:00
{
extractCheats(zipPath, {}, cfw, version, true);
2020-09-20 01:21:28 +01:00
}
2021-09-11 14:48:13 +01:00
bool isBID(const std::string& bid)
{
for (char const& c : bid) {
if (!isxdigit(c)) return false;
}
2021-09-11 14:48:13 +01:00
return true;
}
2021-09-11 14:48:13 +01:00
void writeTitlesToFile(const std::set<std::string>& titles, const std::string& path)
{
std::ofstream updatedTitlesFile;
std::set<std::string>::iterator it = titles.begin();
updatedTitlesFile.open(path, std::ofstream::out | std::ofstream::trunc);
if (updatedTitlesFile.is_open()) {
while (it != titles.end()) {
updatedTitlesFile << (*it) << std::endl;
it++;
}
updatedTitlesFile.close();
2020-09-20 01:21:28 +01:00
}
}
2021-09-11 14:48:13 +01:00
void removeCheats()
{
std::string path = util::getContentsPath();
ProgressEvent::instance().setTotalSteps(std::distance(std::filesystem::directory_iterator(path), std::filesystem::directory_iterator()) + 1);
2021-09-11 14:48:13 +01:00
for (const auto& entry : std::filesystem::directory_iterator(path)) {
if (ProgressEvent::instance().getInterupt()) {
break;
}
removeCheatsDirectory(entry.path().string());
2021-09-11 14:48:13 +01:00
ProgressEvent::instance().incrementStep(1);
2020-09-20 01:21:28 +01:00
}
2021-09-11 14:48:13 +01:00
std::filesystem::remove(CHEATS_VERSION);
ProgressEvent::instance().setStep(ProgressEvent::instance().getMax());
2020-09-20 01:21:28 +01:00
}
2021-10-14 20:50:18 +01:00
void removeOrphanedCheats()
{
auto path = util::getContentsPath();
std::vector<std::string> titles = getInstalledTitlesNs();
ProgressEvent::instance().setTotalSteps(std::distance(std::filesystem::directory_iterator(path), std::filesystem::directory_iterator()) + 1);
for (const auto& entry : std::filesystem::directory_iterator(path)) {
if (ProgressEvent::instance().getInterupt()) {
break;
}
if (std::find_if(titles.begin(), titles.end(), [&entry](std::string title) {
return caselessCompare(entry.path().filename(), title);
}) == titles.end()) {
removeCheatsDirectory(entry.path().string());
}
ProgressEvent::instance().incrementStep(1);
}
std::filesystem::remove(CHEATS_VERSION);
ProgressEvent::instance().setStep(ProgressEvent::instance().getMax());
}
bool removeCheatsDirectory(const std::string& entry)
{
bool res = true;
std::string cheatsPath = fmt::format("{}/cheats", entry);
if (std::filesystem::exists(cheatsPath)) res &= fs::removeDir(cheatsPath);
if (std::filesystem::is_empty(entry)) res &= fs::removeDir(entry);
return res;
}
2021-09-11 14:48:13 +01:00
} // namespace extract