1
0
Fork 0
mirror of https://github.com/HamletDuFromage/aio-switch-updater.git synced 2024-11-25 02:52:05 +00:00
AIO-switch-updater/source/extract.cpp

355 lines
13 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 <dirent.h>
#include <minizip/unzip.h>
2021-09-11 14:48:13 +01:00
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;
constexpr size_t WRITE_BUFFER_SIZE = 0x10000;
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
s64 getUncompressedSize(const std::string& archivePath)
{
s64 size = 0;
unzFile zfile = unzOpen(archivePath.c_str());
unz_global_info gi;
unzGetGlobalInfo(zfile, &gi);
for (uLong i = 0; i < gi.number_entry; ++i) {
unz_file_info fi;
unzOpenCurrentFile(zfile);
unzGetCurrentFileInfo(zfile, &fi, NULL, 0, NULL, 0, NULL, 0);
size += fi.uncompressed_size;
unzCloseCurrentFile(zfile);
unzGoToNextFile(zfile);
}
unzClose(zfile);
return size; // in B
}
2022-09-06 19:45:26 +01:00
void ensureAvailableStorage(const std::string& archivePath)
2021-09-11 14:48:13 +01:00
{
s64 uncompressedSize = getUncompressedSize(archivePath);
2021-06-03 17:47:35 +01:00
s64 freeStorage;
2021-09-11 14:48:13 +01:00
if (R_SUCCEEDED(fs::getFreeStorageSD(freeStorage))) {
brls::Logger::info("Uncompressed size of archive {}: {}. Available: {}", archivePath, uncompressedSize, freeStorage);
2021-09-11 14:48:13 +01:00
if (uncompressedSize * 1.1 > freeStorage) {
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();
}
}
}
void extractEntry(std::string filename, unzFile& zfile, bool forceCreateTree = false)
{
if (filename.back() == '/') {
fs::createTree(filename);
return;
}
if (forceCreateTree) {
fs::createTree(filename);
}
void* buf = malloc(WRITE_BUFFER_SIZE);
FILE* outfile;
outfile = fopen(filename.c_str(), "wb");
for (int j = unzReadCurrentFile(zfile, buf, WRITE_BUFFER_SIZE); j > 0; j = unzReadCurrentFile(zfile, buf, WRITE_BUFFER_SIZE)) {
fwrite(buf, 1, j, outfile);
}
free(buf);
fclose(outfile);
2021-06-03 17:47:35 +01:00
}
2021-09-11 14:48:13 +01:00
} // namespace
2022-11-04 01:24:45 +00:00
void extract(const std::string& archivePath, const std::string& workingPath, bool preserveInis, std::function<void()> func)
2021-09-11 14:48:13 +01:00
{
2022-09-06 19:45:26 +01:00
ensureAvailableStorage(archivePath);
unzFile zfile = unzOpen(archivePath.c_str());
unz_global_info gi;
unzGetGlobalInfo(zfile, &gi);
ProgressEvent::instance().setTotalSteps(gi.number_entry);
ProgressEvent::instance().setStep(0);
2021-09-11 14:48:13 +01:00
std::set<std::string> ignoreList = fs::readLineByLine(FILES_IGNORE);
std::string appPath = util::getAppPath();
for (uLong i = 0; i < gi.number_entry; ++i) {
char szFilename[0x301] = "";
unzOpenCurrentFile(zfile);
unzGetCurrentFileInfo(zfile, NULL, szFilename, sizeof(szFilename), NULL, 0, NULL, 0);
2022-09-06 19:45:26 +01:00
std::string filename = workingPath + szFilename;
if (ProgressEvent::instance().getInterupt()) {
unzCloseCurrentFile(zfile);
break;
}
2022-09-06 19:45:26 +01:00
if (appPath != filename) {
2022-11-04 01:24:45 +00:00
if ((preserveInis == true && filename.substr(filename.length() - 4) == ".ini") || std::find_if(ignoreList.begin(), ignoreList.end(), [&filename](std::string ignored) {
u8 res = filename.find(ignored);
return (res == 0 || res == 1); }) != ignoreList.end()) {
2022-09-06 19:45:26 +01:00
if (!std::filesystem::exists(filename)) {
extractEntry(filename, zfile);
}
}
else {
2022-09-06 19:45:26 +01:00
if ((filename == "/atmosphere/package3") || (filename == "/atmosphere/stratosphere.romfs")) {
2022-08-10 13:27:04 +01:00
extractEntry(filename + ".aio", zfile);
}
else {
extractEntry(filename, zfile);
if (filename.substr(0, 14) == "/hekate_ctcaer") {
2022-09-06 19:45:26 +01:00
fs::copyFile(filename, UPDATE_BIN_PATH);
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().setStep(i);
unzCloseCurrentFile(zfile);
unzGoToNextFile(zfile);
}
unzClose(zfile);
2021-09-11 14:48:13 +01:00
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& archivePath, const std::vector<std::string>& titles, CFW cfw, const std::string& version, bool extractAll)
{
2022-09-06 19:45:26 +01:00
ensureAvailableStorage(archivePath);
unzFile zfile = unzOpen(archivePath.c_str());
unz_global_info gi;
unzGetGlobalInfo(zfile, &gi);
ProgressEvent::instance().setTotalSteps(gi.number_entry);
ProgressEvent::instance().setStep(0);
int offset = computeOffset(cfw);
2021-09-11 14:48:13 +01:00
for (uLong i = 0; i < gi.number_entry; ++i) {
char szFilename[0x301] = "";
unzOpenCurrentFile(zfile);
unzGetCurrentFileInfo(zfile, NULL, szFilename, sizeof(szFilename), NULL, 0, NULL, 0);
std::string filename = szFilename;
if (ProgressEvent::instance().getInterupt()) {
unzCloseCurrentFile(zfile);
break;
}
if ((int)filename.size() > offset + 16 + 7 && caselessCompare(filename.substr(offset + 16, 7), "/cheats")) {
if (extractAll) {
extractEntry(filename, zfile);
}
else {
if (std::find_if(titles.begin(), titles.end(), [&filename, offset](std::string title) {
return caselessCompare((title.substr(0, 13)), filename.substr(offset, 13));
}) != titles.end()) {
extractEntry(filename, zfile);
}
}
2021-09-11 14:48:13 +01:00
}
ProgressEvent::instance().setStep(i);
unzCloseCurrentFile(zfile);
unzGoToNextFile(zfile);
2020-09-20 01:21:28 +01:00
}
unzClose(zfile);
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& archivePath, CFW cfw, const std::string& version)
2021-09-11 14:48:13 +01:00
{
extractCheats(archivePath, {}, 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;
}
} // namespace extract