2020-09-20 01:21:28 +01:00
|
|
|
#include "extract.hpp"
|
2021-02-10 16:28:47 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
|
|
|
#include <filesystem>
|
|
|
|
#include <fstream>
|
|
|
|
#include <set>
|
|
|
|
#include <unzipper.h>
|
|
|
|
#include "progress_event.hpp"
|
2021-03-16 14:56:46 +00:00
|
|
|
#include "utils.hpp"
|
|
|
|
#include "download.hpp"
|
|
|
|
#include "fs.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 {
|
|
|
|
bool caselessCompare (const std::string& a, const std::string& b){
|
|
|
|
return strcasecmp(a.c_str(), b.c_str()) < 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 21:58:40 +01:00
|
|
|
void extract(const char * filename, const char* workingPath, int overwriteInis){
|
2020-09-20 01:21:28 +01:00
|
|
|
ProgressEvent::instance().reset();
|
2020-09-21 07:53:59 +01:00
|
|
|
ProgressEvent::instance().setStep(1);
|
2020-09-20 01:21:28 +01:00
|
|
|
chdir(workingPath);
|
2021-03-16 14:56:46 +00:00
|
|
|
std::set<std::string> ignoreList = fs::readLineByLine(FILES_IGNORE);
|
2020-12-28 23:24:35 +00:00
|
|
|
std::set<std::string>::iterator it;
|
2020-09-20 01:21:28 +01:00
|
|
|
zipper::Unzipper unzipper(filename);
|
|
|
|
std::vector<zipper::ZipEntry> entries = unzipper.entries();
|
2020-12-28 23:24:35 +00:00
|
|
|
int k = -1;
|
|
|
|
bool isIgnored;
|
2020-09-21 07:53:59 +01:00
|
|
|
ProgressEvent::instance().setTotalSteps(entries.size() + 1);
|
2020-09-20 01:21:28 +01:00
|
|
|
for (int i = 0; i < (int) entries.size(); i++){
|
2020-12-28 23:24:35 +00:00
|
|
|
isIgnored = false;
|
|
|
|
it = ignoreList.begin();
|
2020-12-12 15:41:56 +00:00
|
|
|
if(overwriteInis == 0 && entries[i].name.substr(entries[i].name.length() - 4) == ".ini"){
|
2020-12-28 23:24:35 +00:00
|
|
|
if(!std::filesystem::exists("/" + entries[i].name)){
|
|
|
|
unzipper.extractEntry(entries[i].name);
|
|
|
|
}
|
|
|
|
continue;
|
2020-12-12 15:41:56 +00:00
|
|
|
}
|
2020-12-28 23:24:35 +00:00
|
|
|
while (it != ignoreList.end() ){
|
|
|
|
k = ("/" + entries[i].name).find((*it));
|
|
|
|
if(k == 0 || k == 1){
|
|
|
|
isIgnored = true;
|
|
|
|
if(!std::filesystem::exists("/" + entries[i].name)){
|
|
|
|
unzipper.extractEntry(entries[i].name);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
if(!isIgnored){
|
2021-04-13 11:07:51 +01:00
|
|
|
if(entries[i].name == "sept/payload.bin" || entries[i].name == "atmosphere/fusee-secondary.bin" || entries[i].name == "atmosphere/stratosphere.romfs"){
|
2020-12-12 15:41:56 +00:00
|
|
|
unzipper.extractEntry(entries[i].name, CONFIG_PATH_UNZIP);
|
|
|
|
}
|
2021-03-16 14:56:46 +00:00
|
|
|
else {
|
2020-12-12 15:41:56 +00:00
|
|
|
unzipper.extractEntry(entries[i].name);
|
2021-03-16 14:56:46 +00:00
|
|
|
if(entries[i].name.substr(0, 13) == "hekate_ctcaer") {
|
|
|
|
fs::copyFile(("/" + entries[i].name).c_str(), UPDATE_BIN_PATH);
|
2021-01-18 16:24:36 +00:00
|
|
|
}
|
2020-09-21 19:36:46 +01:00
|
|
|
}
|
2020-09-20 21:58:40 +01:00
|
|
|
}
|
|
|
|
|
2020-09-20 01:21:28 +01:00
|
|
|
ProgressEvent::instance().setStep(i);
|
|
|
|
}
|
|
|
|
unzipper.close();
|
2020-09-21 07:53:59 +01:00
|
|
|
ProgressEvent::instance().setStep(ProgressEvent::instance().getMax());
|
2020-09-20 01:21:28 +01:00
|
|
|
}
|
|
|
|
|
2020-09-21 19:36:46 +01:00
|
|
|
void extract(const char * filename, const char* workingPath, const char* toExclude){
|
|
|
|
ProgressEvent::instance().reset();
|
|
|
|
ProgressEvent::instance().setStep(1);
|
|
|
|
chdir(workingPath);
|
2021-03-16 14:56:46 +00:00
|
|
|
std::set<std::string> ignoreList = fs::readLineByLine(FILES_IGNORE);
|
2020-12-28 23:24:35 +00:00
|
|
|
ignoreList.insert(toExclude);
|
|
|
|
std::set<std::string>::iterator it;
|
2020-09-21 19:36:46 +01:00
|
|
|
zipper::Unzipper unzipper(filename);
|
|
|
|
std::vector<zipper::ZipEntry> entries = unzipper.entries();
|
2020-12-28 23:24:35 +00:00
|
|
|
int k = -1;
|
|
|
|
bool isIgnored;
|
2020-09-21 19:36:46 +01:00
|
|
|
ProgressEvent::instance().setTotalSteps(entries.size() + 1);
|
|
|
|
for (int i = 0; i < (int) entries.size(); i++){
|
2020-12-28 23:24:35 +00:00
|
|
|
isIgnored = false;
|
|
|
|
it = ignoreList.begin();
|
|
|
|
while (it != ignoreList.end()){
|
|
|
|
k = ("/" + entries[i].name).find((*it));
|
|
|
|
if(k == 0 || k == 1){
|
|
|
|
isIgnored = true;
|
|
|
|
if(!std::filesystem::exists("/" + entries[i].name)){
|
|
|
|
unzipper.extractEntry(entries[i].name);
|
|
|
|
}
|
|
|
|
break;
|
2020-09-21 19:36:46 +01:00
|
|
|
}
|
2020-12-28 23:24:35 +00:00
|
|
|
it++;
|
2020-09-21 19:36:46 +01:00
|
|
|
}
|
2020-12-28 23:24:35 +00:00
|
|
|
if(!isIgnored) {
|
2020-09-21 19:36:46 +01:00
|
|
|
unzipper.extractEntry(entries[i].name);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgressEvent::instance().setStep(i);
|
|
|
|
}
|
|
|
|
unzipper.close();
|
|
|
|
ProgressEvent::instance().setStep(ProgressEvent::instance().getMax());
|
|
|
|
}
|
2020-09-20 01:21:28 +01:00
|
|
|
|
2021-02-22 19:51:29 +00:00
|
|
|
std::vector<std::string> getInstalledTitlesNs(){
|
|
|
|
std::vector<std::string> titles;
|
2020-09-20 01:21:28 +01:00
|
|
|
|
2021-03-27 00:33:20 +00:00
|
|
|
NsApplicationRecord *records = new NsApplicationRecord[MaxTitleCount]();
|
|
|
|
NsApplicationControlData *controlData = NULL;
|
|
|
|
NacpLanguageEntry* langEntry = NULL;
|
2020-09-20 01:21:28 +01:00
|
|
|
|
2021-03-27 00:33:20 +00:00
|
|
|
s32 recordCount = 0;
|
|
|
|
u64 controlSize = 0;
|
2020-09-20 01:21:28 +01:00
|
|
|
|
2021-03-27 00:33:20 +00:00
|
|
|
if (R_SUCCEEDED(nsListApplicationRecord(records, MaxTitleCount, 0, &recordCount))){
|
|
|
|
for (s32 i = 0; i < recordCount; i++){
|
|
|
|
controlSize = 0;
|
|
|
|
controlData = (NsApplicationControlData*)malloc(sizeof(NsApplicationControlData));
|
|
|
|
if(controlData != NULL)
|
|
|
|
memset(controlData, 0, sizeof(NsApplicationControlData));
|
2020-09-20 01:21:28 +01:00
|
|
|
|
2021-03-27 00:33:20 +00:00
|
|
|
if(R_FAILED(nsGetApplicationControlData(NsApplicationControlSource_Storage, records[i].application_id, controlData, sizeof(NsApplicationControlData), &controlSize))) continue;
|
|
|
|
|
|
|
|
if(controlSize < sizeof(controlData->nacp)) continue;
|
|
|
|
|
|
|
|
if(R_FAILED(nacpGetLanguageEntry(&controlData->nacp, &langEntry))) continue;
|
|
|
|
|
|
|
|
titles.push_back(util::formatApplicationId(records[i].application_id));
|
2020-09-20 01:21:28 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-27 00:34:57 +00:00
|
|
|
free(controlData);
|
2021-03-27 00:33:20 +00:00
|
|
|
delete[] records;
|
2020-09-20 01:21:28 +01:00
|
|
|
std::sort(titles.begin(), titles.end());
|
|
|
|
return titles;
|
|
|
|
}
|
|
|
|
|
2021-02-22 19:51:29 +00:00
|
|
|
std::vector<std::string> excludeTitles(const char* path, std::vector<std::string> listedTitles){
|
|
|
|
std::vector<std::string> titles;
|
2020-09-20 01:21:28 +01:00
|
|
|
std::ifstream file(path);
|
|
|
|
int total = 0;
|
|
|
|
std::string name;
|
|
|
|
|
|
|
|
if (file.is_open()) {
|
|
|
|
std::string line;
|
|
|
|
while (std::getline(file, line)) {
|
|
|
|
std::transform(line.begin(), line.end(), line.begin(), ::toupper);
|
2021-02-22 19:51:29 +00:00
|
|
|
for(size_t i = 0; i < listedTitles.size(); i++) {
|
|
|
|
if(line == listedTitles[i]) {
|
|
|
|
titles.push_back(line);
|
2020-09-20 01:21:28 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(titles.begin(), titles.end());
|
|
|
|
|
2021-02-22 19:51:29 +00:00
|
|
|
std::vector<std::string> diff;
|
2020-09-20 01:21:28 +01:00
|
|
|
std::set_difference(listedTitles.begin(), listedTitles.end(), titles.begin(), titles.end(),
|
|
|
|
std::inserter(diff, diff.begin()));
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
2021-02-22 19:51:29 +00:00
|
|
|
void extractCheats(const char * zipPath, std::vector<std::string> titles, CFW cfw, bool credits){
|
2020-09-20 01:21:28 +01:00
|
|
|
//TODO: REWRITE WITH SETS INSTEAD OF VECTORS
|
|
|
|
ProgressEvent::instance().reset();
|
|
|
|
zipper::Unzipper unzipper(zipPath);
|
|
|
|
std::vector<zipper::ZipEntry> entries = unzipper.entries();
|
|
|
|
std::set<std::string> extractedTitles;
|
|
|
|
int offset = 0;
|
|
|
|
switch(cfw){
|
2021-03-16 02:04:21 +00:00
|
|
|
case CFW::ams:
|
2020-09-20 01:21:28 +01:00
|
|
|
offset = std::string(CONTENTS_PATH).length();
|
2020-09-21 19:36:46 +01:00
|
|
|
std::filesystem::create_directory(AMS_PATH);
|
|
|
|
std::filesystem::create_directory(AMS_CONTENTS);
|
2020-09-20 01:21:28 +01:00
|
|
|
chdir(AMS_PATH);
|
|
|
|
break;
|
2021-03-16 02:04:21 +00:00
|
|
|
case CFW::rnx:
|
2020-09-20 01:21:28 +01:00
|
|
|
offset = std::string(CONTENTS_PATH).length();
|
2020-09-21 19:36:46 +01:00
|
|
|
std::filesystem::create_directory(REINX_PATH);
|
|
|
|
std::filesystem::create_directory(REINX_CONTENTS);
|
2020-09-20 01:21:28 +01:00
|
|
|
chdir(REINX_PATH);
|
|
|
|
break;
|
2021-03-16 02:04:21 +00:00
|
|
|
case CFW::sxos:
|
2020-09-20 01:21:28 +01:00
|
|
|
offset = std::string(TITLES_PATH).length();
|
2020-09-21 19:36:46 +01:00
|
|
|
std::filesystem::create_directory(SXOS_PATH);
|
|
|
|
std::filesystem::create_directory(SXOS_TITLES);
|
2020-09-20 01:21:28 +01:00
|
|
|
chdir(SXOS_PATH);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> entriesNames;
|
|
|
|
std::vector<int> parentIndexes;
|
|
|
|
for (size_t i = 1; i < entries.size(); i++){
|
|
|
|
entriesNames.push_back(entries[i].name);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(entriesNames.begin(), entriesNames.end(), caselessCompare);
|
|
|
|
|
|
|
|
std::vector<std::string> parents;
|
|
|
|
std::vector<std::vector<std::string>> children;
|
|
|
|
std::vector<std::string> tempChildren;
|
|
|
|
|
|
|
|
|
|
|
|
size_t k = 0;
|
|
|
|
while(k < entriesNames.size()){
|
|
|
|
if(entriesNames[k].length() == (size_t) (offset + 17)){
|
|
|
|
parents.push_back(entriesNames[k]);
|
|
|
|
k++;
|
|
|
|
while(entriesNames[k].length() != (size_t) (offset + 17) && k < entriesNames.size()){
|
|
|
|
if(credits == false){
|
|
|
|
if(strcasecmp(entriesNames[k].substr(offset + 16, 7).c_str(), "/cheats") == 0){
|
|
|
|
tempChildren.push_back(entriesNames[k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
tempChildren.push_back(entriesNames[k]);
|
|
|
|
}
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
children.push_back(tempChildren);
|
|
|
|
tempChildren.clear ();
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t lastL = 0;
|
|
|
|
std::string id;
|
|
|
|
ProgressEvent::instance().setTotalSteps(titles.size());
|
|
|
|
for(size_t j = 0; j < titles.size(); j++){
|
|
|
|
for(size_t l = lastL; l < parents.size(); l++){
|
2021-02-22 19:51:29 +00:00
|
|
|
if(strcasecmp((titles[j]).c_str(), parents[l].substr(offset, 16).c_str()) == 0){
|
2020-09-20 01:21:28 +01:00
|
|
|
unzipper.extractEntry(parents[l]);
|
|
|
|
for(auto& e : children[l]){
|
|
|
|
unzipper.extractEntry(e);
|
|
|
|
extractedTitles.insert(id);
|
|
|
|
ProgressEvent::instance().setStep(j);
|
|
|
|
id = e.substr(offset, 16);
|
|
|
|
std::transform(id.begin(), id.end(), id.begin(), ::toupper);
|
|
|
|
}
|
|
|
|
lastL = l;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unzipper.close();
|
2020-09-26 15:55:24 +01:00
|
|
|
writeTitlesToFile(extractedTitles, UPDATED_TITLES_PATH);
|
2021-03-16 02:04:21 +00:00
|
|
|
auto cheatsVerVec = download::downloadFile(CHEATS_URL_VERSION);
|
2021-03-11 18:05:45 +00:00
|
|
|
std::string cheatsVer(cheatsVerVec.begin(), cheatsVerVec.end());
|
2021-03-16 02:04:21 +00:00
|
|
|
util::saveVersion(cheatsVer, CHEATS_VERSION);
|
2020-09-20 01:21:28 +01:00
|
|
|
ProgressEvent::instance().setStep(ProgressEvent::instance().getMax());
|
|
|
|
}
|
|
|
|
|
2021-03-10 16:23:02 +00:00
|
|
|
void extractAllCheats(const char * zipPath, CFW cfw){
|
|
|
|
ProgressEvent::instance().reset();
|
|
|
|
zipper::Unzipper unzipper(zipPath);
|
|
|
|
std::vector<zipper::ZipEntry> entries = unzipper.entries();
|
|
|
|
int offset = 0;
|
|
|
|
switch(cfw){
|
2021-03-16 02:04:21 +00:00
|
|
|
case CFW::ams:
|
2021-03-10 16:23:02 +00:00
|
|
|
offset = std::string(CONTENTS_PATH).length() + 17 + 7;
|
|
|
|
std::filesystem::create_directory(AMS_PATH);
|
|
|
|
std::filesystem::create_directory(AMS_CONTENTS);
|
|
|
|
chdir(AMS_PATH);
|
|
|
|
break;
|
2021-03-16 02:04:21 +00:00
|
|
|
case CFW::rnx:
|
2021-03-10 16:23:02 +00:00
|
|
|
offset = std::string(CONTENTS_PATH).length() + 17 + 7;
|
|
|
|
std::filesystem::create_directory(REINX_PATH);
|
|
|
|
std::filesystem::create_directory(REINX_CONTENTS);
|
|
|
|
chdir(REINX_PATH);
|
|
|
|
break;
|
2021-03-16 02:04:21 +00:00
|
|
|
case CFW::sxos:
|
2021-03-10 16:23:02 +00:00
|
|
|
offset = std::string(TITLES_PATH).length() + 17 + 7;
|
|
|
|
std::filesystem::create_directory(SXOS_PATH);
|
|
|
|
std::filesystem::create_directory(SXOS_TITLES);
|
|
|
|
chdir(SXOS_PATH);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ProgressEvent::instance().setTotalSteps(entries.size());
|
|
|
|
for(size_t j = 0; j < entries.size(); j++){
|
|
|
|
if(((int) entries[j].name.size() == offset + 16 + 4) && (isBID(entries[j].name.substr(offset, 16)))) {
|
|
|
|
unzipper.extractEntry(entries[j].name);
|
|
|
|
}
|
|
|
|
ProgressEvent::instance().setStep(j);
|
|
|
|
}
|
|
|
|
unzipper.close();
|
2021-03-16 02:04:21 +00:00
|
|
|
auto cheatsVerVec = download::downloadFile(CHEATS_URL_VERSION);
|
2021-03-11 18:05:45 +00:00
|
|
|
std::string cheatsVer(cheatsVerVec.begin(), cheatsVerVec.end());
|
2021-03-16 02:04:21 +00:00
|
|
|
util::saveVersion(cheatsVer, CHEATS_VERSION);
|
2021-03-10 16:23:02 +00:00
|
|
|
ProgressEvent::instance().setStep(ProgressEvent::instance().getMax());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isBID(std::string bid) {
|
|
|
|
for(char const &c : bid){
|
|
|
|
if(!isxdigit(c)) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-26 15:55:24 +01:00
|
|
|
void writeTitlesToFile(std::set<std::string> titles, const char* path){
|
2020-09-20 01:21:28 +01:00
|
|
|
std::ofstream updatedTitlesFile;
|
|
|
|
std::set<std::string>::iterator it = titles.begin();
|
2020-09-26 15:55:24 +01:00
|
|
|
updatedTitlesFile.open(path, std::ofstream::out | std::ofstream::trunc);
|
2020-09-20 01:21:28 +01:00
|
|
|
if(updatedTitlesFile.is_open()) {
|
|
|
|
while (it != titles.end()){
|
|
|
|
updatedTitlesFile << (*it) << std::endl;
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
updatedTitlesFile.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeCheats(CFW cfw){
|
|
|
|
std::string path;
|
|
|
|
switch(cfw){
|
2021-03-16 02:04:21 +00:00
|
|
|
case CFW::ams:
|
2020-09-20 01:21:28 +01:00
|
|
|
path = std::string(AMS_PATH) + std::string(CONTENTS_PATH);
|
|
|
|
break;
|
2021-03-16 02:04:21 +00:00
|
|
|
case CFW::rnx:
|
2020-09-20 01:21:28 +01:00
|
|
|
path = std::string(REINX_PATH) + std::string(CONTENTS_PATH);
|
|
|
|
break;
|
2021-03-16 02:04:21 +00:00
|
|
|
case CFW::sxos:
|
2020-09-20 01:21:28 +01:00
|
|
|
path = std::string(SXOS_PATH) + std::string(TITLES_PATH);
|
|
|
|
break;
|
|
|
|
}
|
2021-03-14 15:24:56 +00:00
|
|
|
ProgressEvent::instance().reset();
|
|
|
|
ProgressEvent::instance().setTotalSteps(std::distance(std::filesystem::directory_iterator(path), std::filesystem::directory_iterator()));
|
|
|
|
int c = 0;
|
2020-09-20 01:21:28 +01:00
|
|
|
for (const auto & entry : std::filesystem::directory_iterator(path)){
|
|
|
|
std::string cheatsPath = entry.path().string() + "/cheats";
|
|
|
|
if(std::filesystem::exists(cheatsPath)){
|
|
|
|
for (const auto & cheat : std::filesystem::directory_iterator(cheatsPath)){
|
|
|
|
std::filesystem::remove(cheat);
|
|
|
|
}
|
|
|
|
rmdir(cheatsPath.c_str());
|
|
|
|
if(std::filesystem::is_empty(entry)){
|
|
|
|
rmdir(entry.path().string().c_str());
|
|
|
|
}
|
|
|
|
}
|
2021-03-14 15:24:56 +00:00
|
|
|
ProgressEvent::instance().setStep(c++);
|
2020-09-20 01:21:28 +01:00
|
|
|
}
|
|
|
|
std::filesystem::remove(UPDATED_TITLES_PATH);
|
2020-09-29 16:05:13 +01:00
|
|
|
std::filesystem::remove(CHEATS_VERSION);
|
2020-09-20 01:21:28 +01:00
|
|
|
ProgressEvent::instance().setStep(ProgressEvent::instance().getMax());
|
2021-03-16 02:04:21 +00:00
|
|
|
}
|
|
|
|
|
2020-09-20 01:21:28 +01:00
|
|
|
}
|