2019-10-18 19:01:51 +00:00
|
|
|
#include <filesystem>
|
2019-10-22 22:14:37 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
2019-10-25 01:33:41 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <unistd.h>
|
2019-10-26 05:58:32 +00:00
|
|
|
#include <curl/curl.h>
|
2019-11-15 16:32:38 +00:00
|
|
|
#include <regex>
|
2019-10-18 19:01:51 +00:00
|
|
|
#include "switch.h"
|
2019-10-25 01:33:41 +00:00
|
|
|
#include "util/util.hpp"
|
2019-10-18 19:01:51 +00:00
|
|
|
#include "nx/ipc/tin_ipc.h"
|
2019-10-26 04:38:12 +00:00
|
|
|
#include "util/INIReader.h"
|
2019-10-26 05:58:32 +00:00
|
|
|
#include "util/config.hpp"
|
2019-11-14 01:01:35 +00:00
|
|
|
#include "util/curl.hpp"
|
2019-10-18 19:01:51 +00:00
|
|
|
|
2019-10-26 05:58:32 +00:00
|
|
|
namespace inst::util {
|
2019-10-18 19:01:51 +00:00
|
|
|
void initApp () {
|
2019-11-03 21:56:58 +00:00
|
|
|
// Dilate
|
|
|
|
if (std::filesystem::exists("sdmc:/license.dat")) fatalThrow(0);
|
2019-10-18 19:01:51 +00:00
|
|
|
if (!std::filesystem::exists("sdmc:/switch")) std::filesystem::create_directory("sdmc:/switch");
|
2019-10-26 05:58:32 +00:00
|
|
|
if (!std::filesystem::exists(inst::config::appDir)) std::filesystem::create_directory(inst::config::appDir);
|
|
|
|
if (std::filesystem::exists(inst::config::configPath)) inst::config::parseConfig();
|
|
|
|
else inst::config::setConfig();
|
2019-10-18 19:01:51 +00:00
|
|
|
|
|
|
|
socketInitializeDefault();
|
|
|
|
#ifdef __DEBUG__
|
|
|
|
nxlinkStdio();
|
|
|
|
#endif
|
2019-11-03 21:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void deinitApp () {
|
|
|
|
socketExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void initInstallServices() {
|
2019-10-18 19:01:51 +00:00
|
|
|
ncmInitialize();
|
|
|
|
nsInitialize();
|
|
|
|
nsextInitialize();
|
|
|
|
esInitialize();
|
2019-11-03 18:18:42 +00:00
|
|
|
splCryptoInitialize();
|
|
|
|
splInitialize();
|
2019-10-18 19:01:51 +00:00
|
|
|
}
|
2019-10-22 22:14:37 +00:00
|
|
|
|
2019-11-03 21:16:52 +00:00
|
|
|
void deinitInstallServices() {
|
2019-10-18 19:01:51 +00:00
|
|
|
ncmExit();
|
|
|
|
nsExit();
|
|
|
|
nsextExit();
|
|
|
|
esExit();
|
2019-11-03 18:18:42 +00:00
|
|
|
splCryptoExit();
|
|
|
|
splExit();
|
2019-10-18 19:01:51 +00:00
|
|
|
}
|
2019-10-22 22:14:37 +00:00
|
|
|
|
|
|
|
std::vector<std::filesystem::path> getDirectoryFiles(const std::string & dir, const std::vector<std::string> & extensions) {
|
|
|
|
std::vector<std::filesystem::path> files;
|
|
|
|
for(auto & p: std::filesystem::directory_iterator(dir))
|
|
|
|
{
|
|
|
|
if (std::filesystem::is_regular_file(p))
|
|
|
|
{
|
2019-11-12 21:40:08 +00:00
|
|
|
std::string ourExtension = p.path().extension().string();
|
|
|
|
std::transform(ourExtension.begin(), ourExtension.end(), ourExtension.begin(), ::tolower);
|
|
|
|
if (extensions.empty() || std::find(extensions.begin(), extensions.end(), ourExtension) != extensions.end())
|
2019-10-22 22:14:37 +00:00
|
|
|
{
|
|
|
|
files.push_back(p.path());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::sort(files.begin(), files.end());
|
|
|
|
std::reverse(files.begin(), files.end());
|
|
|
|
return files;
|
|
|
|
}
|
2019-10-25 01:33:41 +00:00
|
|
|
|
|
|
|
bool removeDirectory(std::string dir) {
|
|
|
|
try {
|
|
|
|
for(auto & p: std::filesystem::recursive_directory_iterator(dir))
|
|
|
|
{
|
|
|
|
if (std::filesystem::is_regular_file(p))
|
|
|
|
{
|
|
|
|
std::filesystem::remove(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rmdir(dir.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (std::filesystem::filesystem_error & e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool copyFile(std::string inFile, std::string outFile) {
|
|
|
|
char ch;
|
|
|
|
std::ifstream f1(inFile);
|
|
|
|
std::ofstream f2(outFile);
|
|
|
|
|
|
|
|
if(!f1 || !f2) return false;
|
|
|
|
|
|
|
|
while(f1 && f1.get(ch)) f2.put(ch);
|
|
|
|
return true;
|
|
|
|
}
|
2019-10-26 05:58:32 +00:00
|
|
|
|
|
|
|
std::string formatUrlString(std::string ourString) {
|
|
|
|
std::stringstream ourStream(ourString);
|
|
|
|
std::string segment;
|
|
|
|
std::vector<std::string> seglist;
|
|
|
|
|
|
|
|
while(std::getline(ourStream, segment, '/')) {
|
|
|
|
seglist.push_back(segment);
|
|
|
|
}
|
|
|
|
|
|
|
|
CURL *curl = curl_easy_init();
|
|
|
|
int outlength;
|
|
|
|
std::string finalString = curl_easy_unescape(curl, seglist[seglist.size() - 1].c_str(), seglist[seglist.size() - 1].length(), &outlength);
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
|
|
return finalString;
|
|
|
|
}
|
2019-10-28 23:47:49 +00:00
|
|
|
|
|
|
|
std::string shortenString(std::string ourString, int ourLength, bool isFile) {
|
|
|
|
std::filesystem::path ourStringAsAPath = ourString;
|
2019-11-08 17:05:36 +00:00
|
|
|
std::string ourExtension = ourStringAsAPath.extension().string();
|
|
|
|
if (ourString.size() - ourExtension.size() > (unsigned long)ourLength) {
|
|
|
|
if(isFile) return (std::string)ourString.substr(0,ourLength) + "(...)" + ourExtension;
|
2019-10-28 23:47:49 +00:00
|
|
|
else return (std::string)ourString.substr(0,ourLength) + "...";
|
|
|
|
} else return ourString;
|
|
|
|
}
|
2019-10-30 01:58:53 +00:00
|
|
|
|
|
|
|
std::string readTextFromFile(std::string ourFile) {
|
|
|
|
if (std::filesystem::exists(ourFile)) {
|
|
|
|
FILE * file = fopen(ourFile.c_str(), "r");
|
|
|
|
char line[1024];
|
|
|
|
fgets(line, 1024, file);
|
|
|
|
std::string url = line;
|
|
|
|
fflush(file);
|
|
|
|
fclose(file);
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2019-11-14 01:01:35 +00:00
|
|
|
|
|
|
|
std::string softwareKeyboard(std::string guideText, std::string initialText, int LenMax) {
|
|
|
|
Result rc=0;
|
|
|
|
SwkbdConfig kbd;
|
|
|
|
char tmpoutstr[LenMax + 1] = {0};
|
|
|
|
rc = swkbdCreate(&kbd, 0);
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
swkbdConfigMakePresetDefault(&kbd);
|
|
|
|
swkbdConfigSetGuideText(&kbd, guideText.c_str());
|
|
|
|
swkbdConfigSetInitialText(&kbd, initialText.c_str());
|
|
|
|
swkbdConfigSetStringLenMax(&kbd, LenMax);
|
|
|
|
rc = swkbdShow(&kbd, tmpoutstr, sizeof(tmpoutstr));
|
|
|
|
swkbdClose(&kbd);
|
|
|
|
if (R_SUCCEEDED(rc) && tmpoutstr[0] != 0) return(((std::string)(tmpoutstr)));
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getDriveFileName(std::string fileId) {
|
2019-11-15 16:32:38 +00:00
|
|
|
std::string htmlData = inst::curl::downloadToBuffer("https://drive.google.com/file/d/" + fileId + "/view");
|
|
|
|
if (htmlData.size() > 0) {
|
|
|
|
std::smatch ourMatches;
|
|
|
|
std::regex ourRegex("<title>\\s*(.+?)\\s*</title>");
|
|
|
|
std::regex_search(htmlData, ourMatches, ourRegex);
|
|
|
|
if (ourMatches.size() > 1) {
|
|
|
|
if (ourMatches[1].str() == "Google Drive -- Page Not Found") return "";
|
|
|
|
return ourMatches[1].str().substr(0, ourMatches[1].str().size() - 15);
|
|
|
|
}
|
2019-11-14 01:01:35 +00:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2019-10-18 19:01:51 +00:00
|
|
|
}
|