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>
|
2021-09-25 01:14:38 +01:00
|
|
|
#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"
|
2021-11-19 13:28:10 +00:00
|
|
|
#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"
|
2021-03-10 16:23:02 +00:00
|
|
|
|
2020-10-05 23:53:12 +01:00
|
|
|
namespace i18n = brls::i18n;
|
|
|
|
using namespace i18n::literals;
|
2021-03-16 02:04:21 +00:00
|
|
|
|
|
|
|
namespace extract {
|
|
|
|
|
|
|
|
namespace {
|
2021-09-11 14:48:13 +01:00
|
|
|
bool caselessCompare(const std::string& a, const std::string& b)
|
|
|
|
{
|
2021-09-27 20:56:41 +01:00
|
|
|
return strcasecmp(a.c_str(), b.c_str()) == 0;
|
2021-03-16 02:04:21 +00:00
|
|
|
}
|
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();
|
2021-06-27 23:46:00 +01:00
|
|
|
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);
|
2021-10-21 22:12:29 +01:00
|
|
|
ProgressEvent::instance().setStep(0);
|
2021-06-03 17:47:35 +01:00
|
|
|
}
|
2021-09-11 14:48:13 +01:00
|
|
|
} // namespace
|
2021-03-16 02:04:21 +00:00
|
|
|
|
2021-10-21 22:12:29 +01:00
|
|
|
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-22 20:01:45 +01:00
|
|
|
|
2021-09-11 14:48:13 +01:00
|
|
|
preWork(unzipper, workingPath, entries);
|
|
|
|
std::set<std::string> ignoreList = fs::readLineByLine(FILES_IGNORE);
|
2021-09-22 20:01:45 +01:00
|
|
|
|
2021-09-11 14:48:13 +01:00
|
|
|
for (const auto& entry : entries) {
|
2021-09-22 20:01:45 +01:00
|
|
|
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) {
|
2021-05-29 13:48:11 +01:00
|
|
|
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);
|
|
|
|
}
|
2020-12-28 23:24:35 +00:00
|
|
|
}
|
2021-10-21 22:12:29 +01:00
|
|
|
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);
|
2020-09-21 19:36:46 +01:00
|
|
|
}
|
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) {
|
2021-10-21 22:12:29 +01:00
|
|
|
fs::copyFile(UPDATE_BIN_PATH, REBOOT_PAYLOAD_PATH);
|
|
|
|
}
|
2021-09-11 14:48:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ProgressEvent::instance().incrementStep(1);
|
2020-09-20 21:58:40 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-09-25 01:14:38 +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);
|
2021-09-25 01:14:38 +01:00
|
|
|
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);
|
2021-09-25 01:14:38 +01:00
|
|
|
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);
|
2021-09-25 01:14:38 +01:00
|
|
|
return std::string(TITLES_PATH).length();
|
2021-09-11 14:48:13 +01:00
|
|
|
break;
|
|
|
|
}
|
2021-09-25 01:14:38 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2021-09-11 14:48:13 +01:00
|
|
|
|
2021-11-19 13:28:10 +00:00
|
|
|
void extractCheats(const std::string& zipPath, const std::vector<std::string>& titles, CFW cfw, const std::string& version, bool extractAll)
|
2021-09-25 01:14:38 +01:00
|
|
|
{
|
|
|
|
zipper::Unzipper unzipper(zipPath);
|
|
|
|
std::vector<zipper::ZipEntry> entries = unzipper.entries();
|
|
|
|
int offset = computeOffset(cfw);
|
2021-09-11 14:48:13 +01:00
|
|
|
|
2021-11-19 13:28:10 +00: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);
|
2021-09-22 20:01:45 +01:00
|
|
|
}
|
2021-11-19 13:28:10 +00:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2021-09-25 01:14:38 +01:00
|
|
|
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();
|
2021-11-19 13:28:10 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-19 13:28:10 +00:00
|
|
|
void extractAllCheats(const std::string& zipPath, CFW cfw, const std::string& version)
|
2021-09-11 14:48:13 +01:00
|
|
|
{
|
2021-11-19 13:28:10 +00: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-03-10 16:23:02 +00:00
|
|
|
}
|
2021-09-11 14:48:13 +01:00
|
|
|
return true;
|
2021-03-10 16:23:02 +00:00
|
|
|
}
|
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();
|
2021-09-27 20:56:41 +01:00
|
|
|
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)) {
|
2021-09-25 16:45:08 +01:00
|
|
|
if (ProgressEvent::instance().getInterupt()) {
|
|
|
|
break;
|
|
|
|
}
|
2021-09-21 14:07:47 +01:00
|
|
|
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-03-16 02:04:21 +00: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());
|
|
|
|
}
|
|
|
|
|
2021-09-21 14:07:47 +01:00
|
|
|
bool removeCheatsDirectory(const std::string& entry)
|
|
|
|
{
|
|
|
|
bool res = true;
|
|
|
|
std::string cheatsPath = fmt::format("{}/cheats", entry);
|
2021-09-27 20:56:41 +01:00
|
|
|
if (std::filesystem::exists(cheatsPath)) res &= fs::removeDir(cheatsPath);
|
|
|
|
if (std::filesystem::is_empty(entry)) res &= fs::removeDir(entry);
|
|
|
|
return res;
|
2021-09-21 14:07:47 +01:00
|
|
|
}
|
|
|
|
|
2021-09-11 14:48:13 +01:00
|
|
|
} // namespace extract
|