From d66b1c4e1ec7402ea5ecbf0f3a6341a82618abd8 Mon Sep 17 00:00:00 2001 From: flb Date: Tue, 16 Mar 2021 03:04:21 +0100 Subject: [PATCH] refactor to be more in line with C++ conventions --- aiosu-forwarder/source/main.cpp | 4 +- include/JC_color_swapper.hpp | 14 -- include/PC_color_swapper.hpp | 16 -- include/color_swapper.hpp | 33 +++ include/constants.hpp | 146 +++++------ include/current_cfw.hpp | 11 +- include/download.hpp | 20 +- include/exclude_page.hpp | 8 +- include/extract.hpp | 45 ++-- include/net_page.hpp | 2 +- include/utils.hpp | 11 +- rcm/bootloader/libs/fatfs/ffconf.h | 2 +- source/JC_color_swapper.cpp | 212 ---------------- source/JC_page.cpp | 8 +- source/PC_color_swapper.cpp | 202 ---------------- source/PC_page.cpp | 8 +- source/ams_tab.cpp | 14 +- source/app_page.cpp | 21 +- source/cheats_page.cpp | 16 +- source/color_swapper.cpp | 373 +++++++++++++++++++++++++++++ source/current_cfw.cpp | 96 ++++---- source/download.cpp | 154 ++++++------ source/download_cheats_page.cpp | 32 +-- source/download_payload_page.cpp | 6 +- source/exclude_page.cpp | 32 ++- source/extract.cpp | 50 ++-- source/hide_tabs_page.cpp | 2 +- source/list_download_tab.cpp | 41 ++-- source/main.cpp | 6 +- source/main_frame.cpp | 14 +- source/net_page.cpp | 30 +-- source/payload_page.cpp | 12 +- source/reboot_payload.c | 1 - source/tools_tab.cpp | 12 +- source/utils.cpp | 87 ++++--- source/warning_page.cpp | 2 +- 36 files changed, 861 insertions(+), 882 deletions(-) delete mode 100644 include/JC_color_swapper.hpp delete mode 100644 include/PC_color_swapper.hpp create mode 100644 include/color_swapper.hpp delete mode 100644 source/JC_color_swapper.cpp delete mode 100644 source/PC_color_swapper.cpp create mode 100644 source/color_swapper.cpp diff --git a/aiosu-forwarder/source/main.cpp b/aiosu-forwarder/source/main.cpp index 9fedc17..94eb7b5 100644 --- a/aiosu-forwarder/source/main.cpp +++ b/aiosu-forwarder/source/main.cpp @@ -11,7 +11,7 @@ #define CONFIG_SWITCH "/config/aio-switch-updater/switch/" #define HIDDEN_FILE "/config/aio-switch-updater/.aio-switch-updater" -int removeDir(const char* path) +int util::removeDir(const char* path) { Result ret = 0; FsFileSystem *fs = fsdevGetDeviceFileSystem("sdmc"); @@ -36,7 +36,7 @@ int main(int argc, char* argv[]) std::filesystem::create_directory(PATH); std::filesystem::remove(FULL_PATH); std::filesystem::rename(CONFIG_PATH, FULL_PATH); - removeDir(CONFIG_SWITCH); + util::removeDir(CONFIG_SWITCH); } std::filesystem::remove(FORWARDER_PATH); diff --git a/include/JC_color_swapper.hpp b/include/JC_color_swapper.hpp deleted file mode 100644 index c81e721..0000000 --- a/include/JC_color_swapper.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include "json.hpp" - -int hexToBGR(std::string hex); -std::string BGRToHex(int v); -bool isHexaAnd3Bytes(std::string str); -int setColor(std::vector colors); -int backupToJSON(nlohmann::json &profiles, const char* path); -void writeJSONToFile(nlohmann::json &profiles, const char* path); -std::tuple, std::vector>> getProfiles(const char* path); -void changeJCColor(std::vector values); -nlohmann::json backupProfile(); -void backupJCColor(const char* path); \ No newline at end of file diff --git a/include/PC_color_swapper.hpp b/include/PC_color_swapper.hpp deleted file mode 100644 index 257d953..0000000 --- a/include/PC_color_swapper.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include "json.hpp" - -namespace pc{ -int hexToBGR(std::string hex); -std::string BGRToHex(int v); -bool isHexaAnd3Bytes(std::string str); -int setColor(std::vector colors); -int backupToJSON(nlohmann::json &profiles, const char* path); -void writeJSONToFile(nlohmann::json &profiles, const char* path); -std::tuple, std::vector>> getProfiles(const char* path); -void changePCColor(std::vector values); -nlohmann::json backupProfile(); -void backupPCColor(const char* path); -} \ No newline at end of file diff --git a/include/color_swapper.hpp b/include/color_swapper.hpp new file mode 100644 index 0000000..71fcde8 --- /dev/null +++ b/include/color_swapper.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include "json.hpp" + +namespace JC { + + int setColor(std::vector colors); + int backupToJSON(nlohmann::json &profiles, const char* path); + std::tuple, std::vector>> getProfiles(const char* path); + void changeJCColor(std::vector values); + nlohmann::json backupProfile(); + void backupJCColor(const char* path); + +} + +namespace PC { + + int setColor(std::vector colors); + int backupToJSON(nlohmann::json &profiles, const char* path); + std::tuple, std::vector>> getProfiles(const char* path); + void changePCColor(std::vector values); + nlohmann::json backupProfile(); + void backupPCColor(const char* path); + +} + +namespace ColorSwapper { + + int hexToBGR(std::string hex); + std::string BGRToHex(int v); + bool isHexaAnd3Bytes(std::string str); + +} \ No newline at end of file diff --git a/include/constants.hpp b/include/constants.hpp index 156373a..f81b2ba 100644 --- a/include/constants.hpp +++ b/include/constants.hpp @@ -1,95 +1,95 @@ #pragma once -#define ROOT_PATH "/" -#define APP_PATH "/switch/aio-switch-updater/" -#define NRO_PATH "/switch/aio-switch-updater/aio-switch-updater.nro" -#define DOWNLOAD_PATH "/config/aio-switch-updater/" -#define CONFIG_PATH "/config/aio-switch-updater/" -#define CONFIG_PATH_UNZIP "config\\aio-switch-updater" +constexpr const char ROOT_PATH[]= "/"; +constexpr const char APP_PATH[] = "/switch/aio-switch-updater/"; +constexpr const char NRO_PATH[] = "/switch/aio-switch-updater/aio-switch-updater.nro"; +constexpr const char DOWNLOAD_PATH[] = "/config/aio-switch-updater/"; +constexpr const char CONFIG_PATH[] = "/config/aio-switch-updater/"; +constexpr const char CONFIG_PATH_UNZIP[] = "config\\aio-switch-updater"; -#define RCM_PAYLOAD_PATH "romfs:/aio_rcm.bin" +constexpr const char RCM_PAYLOAD_PATH[] = "romfs:/aio_rcm.bin"; -#define APP_URL "https://github.com/HamletDuFromage/aio-switch-updater/releases/latest/download/aio-switch-updater.zip" -#define TAGS_INFO "https://api.github.com/repos/HamletDuFromage/aio-switch-updater/releases/latest" -#define APP_FILENAME "/config/aio-switch-updater/app.zip" +constexpr const char APP_URL[] = "https://github.com/HamletDuFromage/aio-switch-updater/releases/latest/download/aio-switch-updater.zip"; +constexpr const char TAGS_INFO[] = "https://api.github.com/repos/HamletDuFromage/aio-switch-updater/releases/latest"; +constexpr const char APP_FILENAME[] = "/config/aio-switch-updater/app.zip"; -#define SIGPATCHES_URL "https://raw.githubusercontent.com/HamletDuFromage/nx-links/master/sigpatches.json" -#define SIGPATCHES_FILENAME "/config/aio-switch-updater/sigpatches.zip" -#define HEKATE_IPL_PATH "/bootloader/hekate_ipl.ini" +constexpr const char SIGPATCHES_URL[] = "https://raw.githubusercontent.com/HamletDuFromage/nx-links/master/sigpatches.json"; +constexpr const char SIGPATCHES_FILENAME[] ="/config/aio-switch-updater/sigpatches.zip"; +constexpr const char HEKATE_IPL_PATH[] = "/bootloader/hekate_ipl.ini"; -#define FIRMWARE_URL "https://raw.githubusercontent.com/HamletDuFromage/nx-links/master/firmwares.json" -#define FIRMWARE_FILENAME "/config/aio-switch-updater/firmware.zip" -#define FIRMWARE_PATH "/firmware/" +constexpr const char FIRMWARE_URL[] = "https://raw.githubusercontent.com/HamletDuFromage/nx-links/master/firmwares.json"; +constexpr const char FIRMWARE_FILENAME[] = "/config/aio-switch-updater/firmware.zip"; +constexpr const char FIRMWARE_PATH[] = "/firmware/"; -#define CFW_URL "https://raw.githubusercontent.com/HamletDuFromage/nx-links/master/cfw.json" -#define CFW_FILENAME "/config/aio-switch-updater/cfw.zip" +constexpr const char CFW_URL[] = "https://raw.githubusercontent.com/HamletDuFromage/nx-links/master/cfw.json"; +constexpr const char CFW_FILENAME[] = "/config/aio-switch-updater/cfw.zip"; -#define AMS_URL "https://raw.githubusercontent.com/HamletDuFromage/nx-links/master/ams.json" -#define SXOS_URL "https://raw.githubusercontent.com/HamletDuFromage/nx-links/master/sxos.json" -#define AMS_FILENAME "/config/aio-switch-updater/ams.zip" +constexpr const char AMS_URL[] = "https://raw.githubusercontent.com/HamletDuFromage/nx-links/master/ams.json"; +constexpr const char SXOS_URL[] = "https://raw.githubusercontent.com/HamletDuFromage/nx-links/master/sxos.json"; +constexpr const char AMS_FILENAME[] = "/config/aio-switch-updater/ams.zip"; -#define HEKATE_URL "https://raw.githubusercontent.com/HamletDuFromage/nx-links/master/hekate.json" +constexpr const char HEKATE_URL[] = "https://raw.githubusercontent.com/HamletDuFromage/nx-links/master/hekate.json"; -#define PAYLOAD_URL "https://raw.githubusercontent.com/HamletDuFromage/nx-links/master/payloads.json" +constexpr const char PAYLOAD_URL[] = "https://raw.githubusercontent.com/HamletDuFromage/nx-links/master/payloads.json"; -#define CHEATS_RELEASE_URL "https://github.com/HamletDuFromage/switch-cheats-db/releases/tag/v1.0" -#define CHEATS_URL_TITLES "https://github.com/HamletDuFromage/switch-cheats-db/releases/download/v1.0/titles.zip" -#define CHEATS_URL_CONTENTS "https://github.com/HamletDuFromage/switch-cheats-db/releases/download/v1.0/contents.zip" -#define CHEATS_URL_VERSION "https://github.com/HamletDuFromage/switch-cheats-db/releases/download/v1.0/VERSION" -#define LOOKUP_TABLE_URL "https://raw.githubusercontent.com/HamletDuFromage/switch-cheats-db/master/versions.json" -#define LOOKUP_TABLE_CBOR "https://github.com/HamletDuFromage/switch-cheats-db/raw/master/versions.cbor" -#define CHEATSLIPS_CHEATS_URL "https://www.cheatslips.com/api/v1/cheats/" -#define CHEATSLIPS_TOKEN_URL "https://www.cheatslips.com/api/v1/token" -#define TOKEN_PATH "/config/aio-switch-updater/token.json" -#define CHEATS_FILENAME "/config/aio-switch-updater/cheats.zip" -#define CHEATS_EXCLUDE "/config/aio-switch-updater/exclude.txt" -#define FILES_IGNORE "/config/aio-switch-updater/preserve.txt" -#define INTERNET_JSON "/config/aio-switch-updater/internet.json" -#define UPDATED_TITLES_PATH "/config/aio-switch-updater/updated.dat" -#define CHEATS_VERSION "/config/aio-switch-updater/cheats_version.dat" -#define AMS_CONTENTS "/atmosphere/contents/" -#define REINX_CONTENTS "/ReiNX/contents/" -#define SXOS_TITLES "/sxos/titles/" -#define AMS_PATH "/atmosphere/" -#define SXOS_PATH "/sxos/" -#define REINX_PATH "/ReiNX/" -#define CONTENTS_PATH "contents/" -#define TITLES_PATH "titles/" +constexpr const char CHEATS_RELEASE_URL[] = "https://github.com/HamletDuFromage/switch-cheats-db/releases/tag/v1.0"; +constexpr const char CHEATS_URL_TITLES[] = "https://github.com/HamletDuFromage/switch-cheats-db/releases/download/v1.0/titles.zip"; +constexpr const char CHEATS_URL_CONTENTS[] ="https://github.com/HamletDuFromage/switch-cheats-db/releases/download/v1.0/contents.zip"; +constexpr const char CHEATS_URL_VERSION[] = "https://github.com/HamletDuFromage/switch-cheats-db/releases/download/v1.0/VERSION"; +constexpr const char LOOKUP_TABLE_URL[] = "https://raw.githubusercontent.com/HamletDuFromage/switch-cheats-db/master/versions.json"; +constexpr const char LOOKUP_TABLE_CBOR[] = "https://github.com/HamletDuFromage/switch-cheats-db/raw/master/versions.cbor"; +constexpr const char CHEATSLIPS_CHEATS_URL[] ="https://www.cheatslips.com/api/v1/cheats/"; +constexpr const char CHEATSLIPS_TOKEN_URL[] ="https://www.cheatslips.com/api/v1/token"; +constexpr const char TOKEN_PATH[] = "/config/aio-switch-updater/token.json"; +constexpr const char CHEATS_FILENAME[] = "/config/aio-switch-updater/cheats.zip"; +constexpr const char CHEATS_EXCLUDE[] = "/config/aio-switch-updater/exclude.txt"; +constexpr const char FILES_IGNORE[] = "/config/aio-switch-updater/preserve.txt"; +constexpr const char INTERNET_JSON[] = "/config/aio-switch-updater/internet.json"; +constexpr const char UPDATED_TITLES_PATH[] ="/config/aio-switch-updater/updated.dat"; +constexpr const char CHEATS_VERSION[] = "/config/aio-switch-updater/cheats_version.dat"; +constexpr const char AMS_CONTENTS[] = "/atmosphere/contents/"; +constexpr const char REINX_CONTENTS[] = "/ReiNX/contents/"; +constexpr const char SXOS_TITLES[] = "/sxos/titles/"; +constexpr const char AMS_PATH[] = "/atmosphere/"; +constexpr const char SXOS_PATH[] = "/sxos/"; +constexpr const char REINX_PATH[] = "/ReiNX/"; +constexpr const char CONTENTS_PATH[] = "contents/"; +constexpr const char TITLES_PATH[] = "titles/"; -#define COLOR_PROFILES_PATH "/config/aio-switch-updater/jc_profiles.json" -#define PC_COLOR_PATH "/config/aio-switch-updater/pc_profiles.json" +constexpr const char COLOR_PROFILES_PATH[] ="/config/aio-switch-updater/jc_profiles.json"; +constexpr const char PC_COLOR_PATH[] = "/config/aio-switch-updater/pc_profiles.json"; -#define PAYLOAD_PATH "/payloads/" -#define BOOTLOADER_PATH "/bootloader/" -#define BOOTLOADER_PL_PATH "/bootloader/payloads/" -#define UPDATE_BIN_PATH "/bootloader/update.bin" -#define REBOOT_PAYLOAD_PATH "/atmosphere/reboot_payload.bin" -#define FUSEE_SECONDARY "/atmosphere/fusee-secondary.bin" -#define FUSEE_MTC "/atmosphere/fusee-mtc.bin" +constexpr const char PAYLOAD_PATH[] = "/payloads/"; +constexpr const char BOOTLOADER_PATH[] = "/bootloader/"; +constexpr const char BOOTLOADER_PL_PATH[] = "/bootloader/payloads/"; +constexpr const char UPDATE_BIN_PATH[] = "/bootloader/update.bin"; +constexpr const char REBOOT_PAYLOAD_PATH[] ="/atmosphere/reboot_payload.bin"; +constexpr const char FUSEE_SECONDARY[] = "/atmosphere/fusee-secondary.bin"; +constexpr const char FUSEE_MTC[] = "/atmosphere/fusee-mtc.bin"; -#define AMS_ZIP_PATH "/config/aio-switch-updater/ams.zip" -#define APP_ZIP_PATH "/config/aio-switch-updater/app.zip" -#define CFW_ZIP_PATH "/config/aio-switch-updater/cfw.zip" -#define FW_ZIP_PATH "/config/aio-switch-updater/firmware.zip" -#define CHEATS_ZIP_PATH "/config/aio-switch-updater/cheats.zip" -#define SIGPATCHES_ZIP_PATH "/config/aio-switch-updater/sigpatches.zip" -#define AMS_DIRECTORY_PATH "/config/aio-switch-updater/atmosphere/" -#define SEPT_DIRECTORY_PATH "/config/aio-switch-updater/sept/" -#define FW_DIRECTORY_PATH "/firmware/" +constexpr const char AMS_ZIP_PATH[] = "/config/aio-switch-updater/ams.zip"; +constexpr const char APP_ZIP_PATH[] = "/config/aio-switch-updater/app.zip"; +constexpr const char CFW_ZIP_PATH[] = "/config/aio-switch-updater/cfw.zip"; +constexpr const char FW_ZIP_PATH[] = "/config/aio-switch-updater/firmware.zip"; +constexpr const char CHEATS_ZIP_PATH[] = "/config/aio-switch-updater/cheats.zip"; +constexpr const char SIGPATCHES_ZIP_PATH[] ="/config/aio-switch-updater/sigpatches.zip"; +constexpr const char AMS_DIRECTORY_PATH[] = "/config/aio-switch-updater/atmosphere/"; +constexpr const char SEPT_DIRECTORY_PATH[] ="/config/aio-switch-updater/sept/"; +constexpr const char FW_DIRECTORY_PATH[] = "/firmware/"; -#define HIDE_TABS_JSON "/config/aio-switch-updater/hide_tabs.json" -#define COPY_FILES_JSON "/config/aio-switch-updater/copy_files.json" -#define LANGUAGE_JSON "/config/aio-switch-updater/language.json" +constexpr const char HIDE_TABS_JSON[] = "/config/aio-switch-updater/hide_tabs.json"; +constexpr const char COPY_FILES_JSON[] = "/config/aio-switch-updater/copy_files.json"; +constexpr const char LANGUAGE_JSON[] = "/config/aio-switch-updater/language.json"; -#define ROMFS_FORWARDER "romfs:/aiosu-forwarder.nro" -#define FORWARDER_PATH "/config/aio-switch-updater/aiosu-forwarder.nro" +constexpr const char ROMFS_FORWARDER[] = "romfs:/aiosu-forwarder.nro"; +constexpr const char FORWARDER_PATH[] = "/config/aio-switch-updater/aiosu-forwarder.nro"; -#define HIDDEN_AIO_FILE "/config/aio-switch-updater/.aio-switch-updater" +constexpr const char HIDDEN_AIO_FILE[] = "/config/aio-switch-updater/.aio-switch-updater"; -#define LISTITEM_HEIGHT 50 +constexpr const int LISTITEM_HEIGHT = 50; -enum archiveType{ +enum class archiveType{ sigpatches, cheats, fw, @@ -98,7 +98,7 @@ enum archiveType{ ams_cfw, }; -enum CFW{ +enum class CFW{ rnx, sxos, ams, diff --git a/include/current_cfw.hpp b/include/current_cfw.hpp index 8f83120..d6b916c 100644 --- a/include/current_cfw.hpp +++ b/include/current_cfw.hpp @@ -3,7 +3,10 @@ #include "constants.hpp" #include -bool isServiceRunning(const char *serviceName); -CFW getCFW(); -std::string getAmsInfo(); -const extern CFW running_cfw; \ No newline at end of file +namespace CurrentCfw { + + CFW getCFW(); + std::string getAmsInfo(); + const extern CFW running_cfw; + +} \ No newline at end of file diff --git a/include/download.hpp b/include/download.hpp index 67bcd90..e572ec3 100644 --- a/include/download.hpp +++ b/include/download.hpp @@ -1,13 +1,17 @@ #pragma once -#define ON 1 -#define OFF 0 +constexpr int ON = 1; +constexpr int OFF = 0; #include "json.hpp" -std::vector downloadFile(const char *url, const char *output = "", int api = OFF); -std::vector> getLinks(const char *url); -std::string fetchTitle(const char *url); -std::string downloadPage(const char* url, std::vector headers = {}, std::string body = ""); -std::vector downloadPageBinary(const char* url, std::vector headers = {}, std::string body = ""); -nlohmann::ordered_json getRequest(std::string url, std::vector headers = {}, std::string body = ""); \ No newline at end of file +namespace download { + + std::vector downloadFile(const char *url, const char *output = "", int api = OFF); + std::vector> getLinks(const char *url); + std::string fetchTitle(const char *url); + std::string downloadPage(const char* url, std::vector headers = {}, std::string body = ""); + std::vector downloadPageBinary(const char* url, std::vector headers = {}, std::string body = ""); + nlohmann::ordered_json getRequest(std::string url, std::vector headers = {}, std::string body = ""); + +} \ No newline at end of file diff --git a/include/exclude_page.hpp b/include/exclude_page.hpp index 70e6fae..8b88bc1 100644 --- a/include/exclude_page.hpp +++ b/include/exclude_page.hpp @@ -1,10 +1,8 @@ #pragma once #include -#include #include - -typedef struct app App; +#include "utils.hpp" class ExcludePage : public brls::AppletFrame { @@ -12,9 +10,9 @@ class ExcludePage : public brls::AppletFrame brls::List* list; brls::Label* label; brls::ListItem* save; - std::vector apps; + std::vector apps; std::set titles; - std::tuple, std::vector> items; + std::set> items; public: ExcludePage(); diff --git a/include/extract.hpp b/include/extract.hpp index 1296b27..6f0f26f 100644 --- a/include/extract.hpp +++ b/include/extract.hpp @@ -6,27 +6,30 @@ #include #include -static constexpr u32 MaxTitleCount = 64000; +namespace extract { -typedef struct Title { - std::string id; - std::string name; - bool operator ==(const Title&x) const { - return id == x.id; - } + static constexpr u32 MaxTitleCount = 64000; - bool operator <(const Title&x) const { - return id < x.id; - } -} Title; + typedef struct Title { + std::string id; + std::string name; + bool operator ==(const Title&x) const { + return id == x.id; + } -void extract(const char* filename, const char* workingPath = ROOT_PATH, int overwriteInis = 1); -void extract(const char * filename, const char* workingPath, const char* toExclude); -std::string formatApplicationId(u64 ApplicationId); -std::vector getInstalledTitlesNs(); -std::vector excludeTitles(const char* path, std::vector listedTitles); -void writeTitlesToFile(std::set titles, const char* path); -void extractCheats(const char * zipPath, std::vector titles, CFW cfw, bool credits = false); -void extractAllCheats(const char * zipPath, CFW cfw); -void removeCheats(CFW cfw); -bool isBID(std::string bid); \ No newline at end of file + bool operator <(const Title&x) const { + return id < x.id; + } + } Title; + + void extract(const char* filename, const char* workingPath = ROOT_PATH, int overwriteInis = 1); + void extract(const char * filename, const char* workingPath, const char* toExclude); + std::vector getInstalledTitlesNs(); + std::vector excludeTitles(const char* path, std::vector listedTitles); + void writeTitlesToFile(std::set titles, const char* path); + void extractCheats(const char * zipPath, std::vector titles, CFW cfw, bool credits = false); + void extractAllCheats(const char * zipPath, CFW cfw); + void removeCheats(CFW cfw); + bool isBID(std::string bid); + +} \ No newline at end of file diff --git a/include/net_page.hpp b/include/net_page.hpp index 898818b..59f9993 100644 --- a/include/net_page.hpp +++ b/include/net_page.hpp @@ -2,7 +2,7 @@ #include -#define AF_INET 2 +constexpr int AF_INET = 2; class NetPage : public brls::AppletFrame { diff --git a/include/utils.hpp b/include/utils.hpp index 1d14345..34879f5 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -7,6 +7,8 @@ #include #include "json.hpp" +namespace util { + typedef char NsApplicationName[0x201]; typedef uint8_t NsApplicationIcon[0x20000]; @@ -18,14 +20,12 @@ struct app brls::ListItem* listItem; }; -std::vector htmlProcess(std::string s, std::regex rgx); void createTree(std::string path); void clearConsole(); bool isArchive(const char * path); void downloadArchive(std::string url, archiveType type); void extractArchive(archiveType type, std::string tag = "0"); -void progressTest(std::string url, archiveType type); -std::string formatListItemTitle(const std::string str, size_t maxScore = 140); +std::string formatListItemTitle(const std::string &str, size_t maxScore = 140); std::string formatApplicationId(u64 ApplicationId); std::set readLineByLine(const char * path); std::vector fetchPayloads(); @@ -41,4 +41,7 @@ std::string copyFiles(const char* path); int removeDir(const char* path); bool isErista(); void removeSysmodulesFlags(const char * directory); -nlohmann::json parseJsonFile(const char* path); \ No newline at end of file +nlohmann::json parseJsonFile(const char* path); +void writeJsonToFile(nlohmann::json &profiles, const char* path); + +} \ No newline at end of file diff --git a/rcm/bootloader/libs/fatfs/ffconf.h b/rcm/bootloader/libs/fatfs/ffconf.h index ee536ef..60bf77a 100644 --- a/rcm/bootloader/libs/fatfs/ffconf.h +++ b/rcm/bootloader/libs/fatfs/ffconf.h @@ -100,7 +100,7 @@ / 936 - Simplified Chinese (DBCS) / 949 - Korean (DBCS) / 950 - Traditional Chinese (DBCS) -/ 0 - Include all code pages above and configured by f_setcp() +/ 0 - Include all code pages above and configured by f_setutil::cp() */ diff --git a/source/JC_color_swapper.cpp b/source/JC_color_swapper.cpp deleted file mode 100644 index 3ccf8fb..0000000 --- a/source/JC_color_swapper.cpp +++ /dev/null @@ -1,212 +0,0 @@ -#include "JC_color_swapper.hpp" -#include -#include -#include -#include -#include -#include - -#include "constants.hpp" -#include "progress_event.hpp" -#include "utils.hpp" - -using json = nlohmann::json; - -int hexToBGR(std::string hex){ - std::string R = hex.substr(0, 2); - std::string G = hex.substr(2, 2); - std::string B = hex.substr(4, 2); - return std::stoi(B + G + R, 0, 16); -} - -std::string BGRToHex(int v){ - std::stringstream ss; - v = ((v & 0xFF) << 16) + (v & 0xFF00) + (v >> 16) + 256; - ss << std::setfill('0') << std::setw(6) << std::right << std::hex << v; - return ss.str(); -} - -bool isHexaAnd3Bytes(std::string str){ - if(str.size()!=6) return false; - for(char const &c : str){ - if(!isxdigit(c)) return false; - } - return true; -} - -int setColor(std::vector colors){ - Result pads, ljc, rjc; - int res = 0; - s32 nbEntries; - HidsysUniquePadId UniquePadIds[2] = {}; - - pads = hidsysGetUniquePadsFromNpad(HidNpadIdType_Handheld, UniquePadIds, 2,&nbEntries); - if(R_SUCCEEDED(pads)){ - ljc = hiddbgUpdateControllerColor(colors[0], colors[1], UniquePadIds[0]); - if (R_FAILED(ljc)) res +=1; - rjc = hiddbgUpdateControllerColor(colors[2], colors[3], UniquePadIds[1]); - if (R_FAILED(rjc)) res +=2; - } - else{ - res +=4; - } - return res; -} - -int backupToJSON(json &profiles, const char* path){ - HidNpadControllerColor color_left; - HidNpadControllerColor color_right; - Result res = hidGetNpadControllerColorSplit(HidNpadIdType_Handheld, &color_left, &color_right); - std::vector oldBackups; - if (R_SUCCEEDED(res)) { - int i = 0; - for (const auto& x : profiles.items()){ - if(x.value()["name"] == "_backup") { - oldBackups.push_back(i); - } - i++; - } - for (auto &k : oldBackups){ - profiles.erase(profiles.begin() + k); - } - json newBackup = json::object({ - {"name", "_backup"}, - {"L_JC", BGRToHex(color_left.main)}, - {"L_BTN", BGRToHex(color_left.sub)}, - {"R_JC", BGRToHex(color_right.main)}, - {"R_BTN", BGRToHex(color_right.sub)} - }); - profiles.push_back(newBackup); - writeJSONToFile(profiles, path); - return 0; - } - else{ - return -1; - } -} - -json backupProfile(){ - json newBackup; - HidNpadControllerColor color_left; - HidNpadControllerColor color_right; - Result res = hidGetNpadControllerColorSplit(HidNpadIdType_Handheld, &color_left, &color_right); - if (R_SUCCEEDED(res)) { - newBackup = json::object({ - {"name", "_backup"}, - {"L_JC", BGRToHex(color_left.main)}, - {"L_BTN", BGRToHex(color_left.sub)}, - {"R_JC", BGRToHex(color_right.main)}, - {"R_BTN", BGRToHex(color_right.sub)} - }); - } - return newBackup; - -} - -void writeJSONToFile(json &profiles, const char* path){ - std::fstream newProfiles; - newProfiles.open(path, std::fstream::out | std::fstream::trunc); - newProfiles << std::setw(4) << profiles << std::endl; - newProfiles.close(); -} - -std::tuple, std::vector>> getProfiles(const char* path){ - std::vector names; - std::vector> colorValues; - bool properData; - std::fstream profilesFile; - json profilesJson; - if(std::filesystem::exists(path)){ - profilesFile.open(path, std::fstream::in); - profilesFile >> profilesJson; - profilesFile.close(); - } - if(profilesJson.empty()){ - profilesJson = {{ - {"L_BTN", "0A1E0A"}, - {"L_JC", "82FF96"}, - {"R_BTN", "0A1E28"}, - {"R_JC", "96F5F5"}, - {"name", "Animal Crossing: New Horizons"} - }}; - writeJSONToFile(profilesJson, path); - } - for (const auto& x : profilesJson.items()){ - std::string name = x.value()["name"]; - std::vector values = { - std::string(x.value()["L_JC"]), - std::string(x.value()["L_BTN"]), - std::string(x.value()["R_JC"]), - std::string(x.value()["R_BTN"]) - }; - properData = true; - for(auto& str : values){ - if(!isHexaAnd3Bytes(str)){ - properData = false; - } - } - if(properData){ - if(name == "") name = "Unamed"; - names.push_back(name); - colorValues.push_back({ - hexToBGR(values[0]), - hexToBGR(values[1]), - hexToBGR(values[2]), - hexToBGR(values[3]) - }); - } - } - return std::make_tuple(names, colorValues); -} - -void changeJCColor(std::vector values){ - hiddbgInitialize(); - hidsysInitialize(); - ProgressEvent::instance().reset(); - ProgressEvent::instance().setStep(1); - int res = setColor(values); - if(res != 0){ - showDialogBox("Could not change the Joy-Cons color. Make sure they're docked and try again.\nError :" + std::to_string(res), "Ok"); - } - hiddbgExit(); - hidsysExit(); - ProgressEvent::instance().setStep(ProgressEvent::instance().getMax()); -} - -void backupJCColor(const char* path){ - hiddbgInitialize(); - hidsysInitialize(); - ProgressEvent::instance().reset(); - ProgressEvent::instance().setStep(1); - json backup; - json profiles; - std::fstream profilesFile; - if(std::filesystem::exists(path)){ - profilesFile.open(path, std::fstream::in); - profilesFile >> profiles; - profilesFile.close(); - } - - std::vector oldBackups; - int i = 0; - for (const auto& x : profiles.items()){ - if(x.value()["name"] == "_backup") { - oldBackups.push_back(i); - } - i++; - } - for (auto &k : oldBackups){ - profiles.erase(profiles.begin() + k); - } - - while(backup.empty()){ - backup = backupProfile(); - } - - profiles.push_back(backup); - //backup.push_back(profiles); - writeJSONToFile(profiles, path); - hiddbgExit(); - hidsysExit(); - ProgressEvent::instance().setStep(ProgressEvent::instance().getMax()); -} \ No newline at end of file diff --git a/source/JC_page.cpp b/source/JC_page.cpp index 560d8f4..0cc64b0 100644 --- a/source/JC_page.cpp +++ b/source/JC_page.cpp @@ -1,5 +1,5 @@ #include "JC_page.hpp" -#include "JC_color_swapper.hpp" +#include "color_swapper.hpp" #include "confirm_page.hpp" #include "worker_page.hpp" #include "constants.hpp" @@ -21,7 +21,7 @@ JCPage::JCPage() : AppletFrame(true, true) stagedFrame->setTitle("menus/joy_con/label"_i18n); stagedFrame->addStage( new WorkerPage(stagedFrame, "menus/joy_con/backing_up"_i18n, - [](){backupJCColor(COLOR_PROFILES_PATH);}) + [](){JC::backupJCColor(COLOR_PROFILES_PATH);}) ); stagedFrame->addStage( new ConfirmPage(stagedFrame, "menus/common/all_done"_i18n, true) @@ -32,7 +32,7 @@ JCPage::JCPage() : AppletFrame(true, true) list->addView(new brls::ListItemGroupSpacing(true)); - auto profiles = getProfiles(COLOR_PROFILES_PATH); + auto profiles = JC::getProfiles(COLOR_PROFILES_PATH); std::vector names = std::get<0>(profiles); int nbProfiles = names.size(); for (int i = nbProfiles - 1; i >= 0; i--){ @@ -44,7 +44,7 @@ JCPage::JCPage() : AppletFrame(true, true) stagedFrame->setTitle("menus/joy_con/label"_i18n); stagedFrame->addStage( new WorkerPage(stagedFrame, "menus/joy_con/changing"_i18n, - [value](){changeJCColor(value);}) + [value](){JC::changeJCColor(value);}) ); stagedFrame->addStage( new ConfirmPage(stagedFrame, "menus/joy_con/all_done"_i18n, true) diff --git a/source/PC_color_swapper.cpp b/source/PC_color_swapper.cpp deleted file mode 100644 index ce158f5..0000000 --- a/source/PC_color_swapper.cpp +++ /dev/null @@ -1,202 +0,0 @@ -#include "PC_color_swapper.hpp" -#include -#include -#include -#include -#include -#include - -#include -#include "constants.hpp" -#include "progress_event.hpp" -#include "utils.hpp" - -using json = nlohmann::json; - -namespace pc{ -int hexToBGR(std::string hex){ - std::string R = hex.substr(0, 2); - std::string G = hex.substr(2, 2); - std::string B = hex.substr(4, 2); - return std::stoi(B + G + R, 0, 16); -} - -std::string BGRToHex(int v){ - std::stringstream ss; - v = ((v & 0xFF) << 16) + (v & 0xFF00) + (v >> 16) + 256; - ss << std::setfill('0') << std::setw(6) << std::right << std::hex << v; - return ss.str(); -} - -bool isHexaAnd3Bytes(std::string str){ - if(str.size()!=6) return false; - for(char const &c : str){ - if(!isxdigit(c)) return false; - } - return true; -} - -int setColor(std::vector colors){ - Result pads, pc; - int res = 0; - s32 nbEntries; - HidsysUniquePadId UniquePadIds[1] = {}; - - pads = hidsysGetUniquePadsFromNpad(HidNpadIdType_No1, UniquePadIds, 1 ,&nbEntries); - if(R_SUCCEEDED(pads)){ - pc = hiddbgUpdateControllerColor(colors[0], colors[1], UniquePadIds[0]); - if (R_FAILED(pc)) res +=1; - } - else{ - res +=4; - } - return res; -} - -int backupToJSON(json &profiles, const char* path){ - HidNpadControllerColor color; - Result res = hidGetNpadControllerColorSingle(HidNpadIdType_No1, &color); - std::vector oldBackups; - if (R_SUCCEEDED(res)) { - int i = 0; - for (const auto& x : profiles.items()){ - if(x.value()["name"] == "_backup") { - oldBackups.push_back(i); - } - i++; - } - for (auto &k : oldBackups){ - profiles.erase(profiles.begin() + k); - } - json newBackup = json::object({ - {"name", "_backup"}, - {"BODY", BGRToHex(color.main)}, - {"BTN", BGRToHex(color.sub)} - }); - profiles.push_back(newBackup); - writeJSONToFile(profiles, path); - return 0; - } - else{ - return -1; - } -} - -json backupProfile(){ - json newBackup; - HidNpadControllerColor color; - Result res = hidGetNpadControllerColorSingle(HidNpadIdType_No1, &color); - if (R_SUCCEEDED(res)) { - newBackup = json::object({ - {"name", "_backup"}, - {"BODY", BGRToHex(color.main)}, - {"BTN", BGRToHex(color.sub)} - }); - } - return newBackup; - -} - -void writeJSONToFile(json &profiles, const char* path){ - std::fstream newProfiles; - newProfiles.open(path, std::fstream::out | std::fstream::trunc); - newProfiles << std::setw(4) << profiles << std::endl; - newProfiles.close(); -} - -std::tuple, std::vector>> getProfiles(const char* path){ - std::vector names; - std::vector> colorValues; - bool properData; - std::fstream profilesFile; - json profilesJson; - if(std::filesystem::exists(path)){ - profilesFile.open(path, std::fstream::in); - profilesFile >> profilesJson; - profilesFile.close(); - } - if(profilesJson.empty()){ - profilesJson = {{ - {"BTN", "2d2d2d"}, - {"BODY", "e6e6e6"}, - {"name", "Default black"} - }}; - writeJSONToFile(profilesJson, path); - } - for (const auto& x : profilesJson.items()){ - std::string name = x.value()["name"]; - std::vector values = { - std::string(x.value()["BODY"]), - std::string(x.value()["BTN"]) - }; - properData = true; - for(auto& str : values){ - if(!isHexaAnd3Bytes(str)){ - properData = false; - } - } - if(properData){ - if(name == "") name = "Unamed"; - names.push_back(name); - colorValues.push_back({ - hexToBGR(values[0]), - hexToBGR(values[1]) - }); - } - } - return std::make_tuple(names, colorValues); -} - -void changePCColor(std::vector values){ - hiddbgInitialize(); - hidsysInitialize(); - ProgressEvent::instance().reset(); - ProgressEvent::instance().setStep(1); - int res = setColor(values); - if(res != 0){ - showDialogBox("Could not change the Pro-Con color. Make they're connected to P1. This feature may not work on unoffical controllers. \nError :" + std::to_string(res), "Ok"); - } - hiddbgExit(); - hidsysExit(); - ProgressEvent::instance().setStep(ProgressEvent::instance().getMax()); -} - -void backupPCColor(const char* path){ - hiddbgInitialize(); - hidsysInitialize(); - ProgressEvent::instance().reset(); - ProgressEvent::instance().setStep(1); - json backup; - json profiles; - std::fstream profilesFile; - if(std::filesystem::exists(path)){ - profilesFile.open(path, std::fstream::in); - profilesFile >> profiles; - profilesFile.close(); - } - - std::vector oldBackups; - int i = 0; - for (const auto& x : profiles.items()){ - if(x.value()["name"] == "_backup") { - oldBackups.push_back(i); - } - i++; - } - for (auto &k : oldBackups){ - profiles.erase(profiles.begin() + k); - } - - while(backup.empty()){ - backup = backupProfile(); - } - - profiles.push_back(backup); - //backup.push_back(profiles); - writeJSONToFile(profiles, path); - hiddbgExit(); - hidsysExit(); - ProgressEvent::instance().setStep(ProgressEvent::instance().getMax()); -} - -} \ No newline at end of file diff --git a/source/PC_page.cpp b/source/PC_page.cpp index e1f2098..6ecf8a4 100644 --- a/source/PC_page.cpp +++ b/source/PC_page.cpp @@ -1,5 +1,5 @@ #include "PC_page.hpp" -#include "PC_color_swapper.hpp" +#include "color_swapper.hpp" #include "confirm_page.hpp" #include "worker_page.hpp" #include "constants.hpp" @@ -20,7 +20,7 @@ PCPage::PCPage() : AppletFrame(true, true) stagedFrame->setTitle("menus/pro_con/label"_i18n); stagedFrame->addStage( new WorkerPage(stagedFrame, "menus/pro_con/backing_up"_i18n, - [](){pc::backupPCColor(PC_COLOR_PATH);}) + [](){PC::backupPCColor(PC_COLOR_PATH);}) ); stagedFrame->addStage( new ConfirmPage(stagedFrame, "menus/common/all_done"_i18n, true) @@ -31,7 +31,7 @@ PCPage::PCPage() : AppletFrame(true, true) list->addView(new brls::ListItemGroupSpacing(true)); - auto profiles = pc::getProfiles(PC_COLOR_PATH); + auto profiles = PC::getProfiles(PC_COLOR_PATH); std::vector names = std::get<0>(profiles); int nbProfiles = names.size(); for (int i = nbProfiles - 1; i >= 0; i--){ @@ -43,7 +43,7 @@ PCPage::PCPage() : AppletFrame(true, true) stagedFrame->setTitle("menus/pro_con/label"_i18n); stagedFrame->addStage( new WorkerPage(stagedFrame, "menus/pro_con/changing"_i18n, - [value](){pc::changePCColor(value);}) + [value](){PC::changePCColor(value);}) ); stagedFrame->addStage( new ConfirmPage(stagedFrame, "menus/pro_con/all_done"_i18n, true) diff --git a/source/ams_tab.cpp b/source/ams_tab.cpp index 6d7e192..f75d8e2 100644 --- a/source/ams_tab.cpp +++ b/source/ams_tab.cpp @@ -16,14 +16,14 @@ AmsTab::AmsTab() : { std::vector> links; std::string operation("menus/main/getting"_i18n); - this->description = new brls::Label(brls::LabelStyle::DESCRIPTION, "menus/main/ams_text"_i18n + (running_cfw == ams ? "\n" + "menus/ams_update/current_ams"_i18n + getAmsInfo() : ""), true); + this->description = new brls::Label(brls::LabelStyle::DESCRIPTION, "menus/main/ams_text"_i18n + (CurrentCfw::running_cfw == CFW::ams ? "\n" + "menus/ams_update/current_ams"_i18n + CurrentCfw::getAmsInfo() : ""), true); this->addView(description); operation += "menus/main/ams"_i18n; - links = getLinks(AMS_URL); + links = download::getLinks(AMS_URL); int nbLinks = links.size(); if(nbLinks){ - auto hekate_link = getLinks(HEKATE_URL); + auto hekate_link = download::getLinks(HEKATE_URL); std::string hekate_url = hekate_link[0].second; std::string text_hekate = "menus/common/download"_i18n + hekate_link[0].first; @@ -39,19 +39,19 @@ AmsTab::AmsTab() : new ConfirmPage(stagedFrame, text) ); stagedFrame->addStage( - new WorkerPage(stagedFrame, "menus/common/downloading"_i18n, [url](){downloadArchive(url, ams_cfw);}) + new WorkerPage(stagedFrame, "menus/common/downloading"_i18n, [url](){util::downloadArchive(url, archiveType::ams_cfw);}) ); stagedFrame->addStage( - new WorkerPage(stagedFrame, "menus/common/extracting"_i18n, [](){extractArchive(ams_cfw);}) + new WorkerPage(stagedFrame, "menus/common/extracting"_i18n, [](){util::extractArchive(archiveType::ams_cfw);}) ); stagedFrame->addStage( new DialoguePage(stagedFrame, text_hekate) ); stagedFrame->addStage( - new WorkerPage(stagedFrame, "menus/common/downloading"_i18n, [hekate_url](){downloadArchive(hekate_url, cfw);}) + new WorkerPage(stagedFrame, "menus/common/downloading"_i18n, [hekate_url](){util::downloadArchive(hekate_url, archiveType::cfw);}) ); stagedFrame->addStage( - new WorkerPage(stagedFrame, "menus/common/extracting"_i18n, [](){extractArchive(cfw);}) + new WorkerPage(stagedFrame, "menus/common/extracting"_i18n, [](){util::extractArchive(archiveType::cfw);}) ); stagedFrame->addStage( new ConfirmPage(stagedFrame, "menus/ams_update/reboot_rcm"_i18n, false, true) diff --git a/source/app_page.cpp b/source/app_page.cpp index 19a0e0e..34aad83 100644 --- a/source/app_page.cpp +++ b/source/app_page.cpp @@ -31,7 +31,7 @@ AppPage::AppPage(const bool cheatSlips) : AppletFrame(true, true) int recordCount = 0; size_t controlSize = 0; - titles = readLineByLine(UPDATED_TITLES_PATH); + titles = util::readLineByLine(UPDATED_TITLES_PATH); if(!titles.empty() || cheatSlips){ while (true) @@ -50,12 +50,12 @@ AppPage::AppPage(const bool cheatSlips) : AppletFrame(true, true) i++; continue; } - if(!cheatSlips && titles.find(formatApplicationId(tid)) == titles.end()) { + if(!cheatSlips && titles.find(util::formatApplicationId(tid)) == titles.end()) { i++; continue; } - listItem = new brls::ListItem(std::string(langEntry->name), "", formatApplicationId(tid)); + listItem = new brls::ListItem(std::string(langEntry->name), "", util::formatApplicationId(tid)); listItem->setThumbnail(controlData.icon, sizeof(controlData.icon)); if(cheatSlips){ listItem->getClickEvent()->subscribe([&, tid](brls::View* view) { @@ -69,31 +69,30 @@ AppPage::AppPage(const bool cheatSlips) : AppletFrame(true, true) } std::string text("menus/cheats/downloading"_i18n); std::string url = ""; - switch(running_cfw){ - case ams: + switch(CurrentCfw::running_cfw){ + case CFW::ams: url += CHEATS_URL_CONTENTS; break; - case rnx: + case CFW::rnx: url += CHEATS_URL_CONTENTS; break; - case sxos: + case CFW::sxos: url += CHEATS_URL_CONTENTS; break; } text += url; download = new brls::ListItem("menus/cheats/dl_latest"_i18n); - archiveType type = cheats; - download->getClickEvent()->subscribe([&, url, text, type](brls::View* view) { + download->getClickEvent()->subscribe([&, url, text](brls::View* view) { brls::StagedAppletFrame* stagedFrame = new brls::StagedAppletFrame(); stagedFrame->setTitle("menus/cheats/getting_cheats"_i18n); stagedFrame->addStage( new ConfirmPage(stagedFrame, text) ); stagedFrame->addStage( - new WorkerPage(stagedFrame, "menus/common/downloading"_i18n, [url, type](){downloadArchive(url, type);}) + new WorkerPage(stagedFrame, "menus/common/downloading"_i18n, [url](){util::downloadArchive(url, archiveType::cheats);}) ); stagedFrame->addStage( - new WorkerPage(stagedFrame, "menus/common/extracting"_i18n, [type](){extractArchive(type);}) + new WorkerPage(stagedFrame, "menus/common/extracting"_i18n, [](){util::extractArchive(archiveType::cheats);}) ); stagedFrame->addStage( new ConfirmPage(stagedFrame, "menus/common/all_done"_i18n, true) diff --git a/source/cheats_page.cpp b/source/cheats_page.cpp index cd85d2d..5a05278 100644 --- a/source/cheats_page.cpp +++ b/source/cheats_page.cpp @@ -34,7 +34,7 @@ CheatsPage::CheatsPage() : AppletFrame(true, true) stagedFrame = new brls::StagedAppletFrame(); stagedFrame->setTitle("menus/cheats/delete_all"_i18n); stagedFrame->addStage( - new WorkerPage(stagedFrame, "menus/cheats/deleting"_i18n, [](){removeCheats(running_cfw);}) + new WorkerPage(stagedFrame, "menus/cheats/deleting"_i18n, [](){extract::removeCheats(CurrentCfw::running_cfw);}) ); stagedFrame->addStage( new ConfirmPage(stagedFrame, "menus/common/all_done"_i18n, true) @@ -43,20 +43,20 @@ CheatsPage::CheatsPage() : AppletFrame(true, true) }); list->addView(deleteCheats); - auto cheatsVerVec = downloadFile(CHEATS_URL_VERSION); + auto cheatsVerVec = download::downloadFile(CHEATS_URL_VERSION); std::string cheatsVer(cheatsVerVec.begin(), cheatsVerVec.end()); if(cheatsVer != "") { dlAll = new brls::ListItem("menus/cheats/dl_all"_i18n); dlAll->getClickEvent()->subscribe([&, cheatsVer](brls::View* view) { std::string url; - switch(running_cfw){ - case sxos: + switch(CurrentCfw::running_cfw){ + case CFW::sxos: url = CHEATS_URL_TITLES; break; - case ams: + case CFW::ams: url = CHEATS_URL_CONTENTS; break; - case rnx: + case CFW::rnx: url = CHEATS_URL_CONTENTS; break; } @@ -67,10 +67,10 @@ CheatsPage::CheatsPage() : AppletFrame(true, true) new ConfirmPage(stagedFrame, text) ); stagedFrame->addStage( - new WorkerPage(stagedFrame, "menus/common/downloading"_i18n, [url](){downloadArchive(url, cheats);}) + new WorkerPage(stagedFrame, "menus/common/downloading"_i18n, [url](){util::downloadArchive(url, archiveType::cheats);}) ); stagedFrame->addStage( - new WorkerPage(stagedFrame, "menus/common/extracting"_i18n, [](){extractAllCheats(CHEATS_ZIP_PATH, running_cfw);}) + new WorkerPage(stagedFrame, "menus/common/extracting"_i18n, [](){extract::extractAllCheats(CHEATS_ZIP_PATH, CurrentCfw::running_cfw);}) ); stagedFrame->addStage( new ConfirmPage(stagedFrame, "menus/common/all_done"_i18n, true) diff --git a/source/color_swapper.cpp b/source/color_swapper.cpp new file mode 100644 index 0000000..a425351 --- /dev/null +++ b/source/color_swapper.cpp @@ -0,0 +1,373 @@ +#include "color_swapper.hpp" +#include +#include +#include +#include +#include +#include + +#include "constants.hpp" +#include "progress_event.hpp" +#include "utils.hpp" + +using json = nlohmann::json; + +namespace ColorSwapper { + + int hexToBGR(std::string hex){ + std::string R = hex.substr(0, 2); + std::string G = hex.substr(2, 2); + std::string B = hex.substr(4, 2); + return std::stoi(B + G + R, 0, 16); + } + + std::string BGRToHex(int v){ + std::stringstream ss; + v = ((v & 0xFF) << 16) + (v & 0xFF00) + (v >> 16) + 256; + ss << std::setfill('0') << std::setw(6) << std::right << std::hex << v; + return ss.str(); + } + + bool isHexaAnd3Bytes(std::string str){ + if(str.size()!=6) return false; + for(char const &c : str){ + if(!isxdigit(c)) return false; + } + return true; + } + +} + +namespace JC { + + int setColor(std::vector colors){ + Result pads, ljc, rjc; + int res = 0; + s32 nbEntries; + HidsysUniquePadId UniquePadIds[2] = {}; + + pads = hidsysGetUniquePadsFromNpad(HidNpadIdType_Handheld, UniquePadIds, 2,&nbEntries); + if(R_SUCCEEDED(pads)){ + ljc = hiddbgUpdateControllerColor(colors[0], colors[1], UniquePadIds[0]); + if (R_FAILED(ljc)) res +=1; + rjc = hiddbgUpdateControllerColor(colors[2], colors[3], UniquePadIds[1]); + if (R_FAILED(rjc)) res +=2; + } + else{ + res +=4; + } + return res; + } + + int backupToJSON(json &profiles, const char* path){ + HidNpadControllerColor color_left; + HidNpadControllerColor color_right; + Result res = hidGetNpadControllerColorSplit(HidNpadIdType_Handheld, &color_left, &color_right); + std::vector oldBackups; + if (R_SUCCEEDED(res)) { + int i = 0; + for (const auto& x : profiles.items()){ + if(x.value()["name"] == "_backup") { + oldBackups.push_back(i); + } + i++; + } + for (auto &k : oldBackups){ + profiles.erase(profiles.begin() + k); + } + json newBackup = json::object({ + {"name", "_backup"}, + {"L_JC", ColorSwapper::BGRToHex(color_left.main)}, + {"L_BTN", ColorSwapper::BGRToHex(color_left.sub)}, + {"R_JC", ColorSwapper::BGRToHex(color_right.main)}, + {"R_BTN", ColorSwapper::BGRToHex(color_right.sub)} + }); + profiles.push_back(newBackup); + util::writeJsonToFile(profiles, path); + return 0; + } + else{ + return -1; + } + } + + json backupProfile(){ + json newBackup; + HidNpadControllerColor color_left; + HidNpadControllerColor color_right; + Result res = hidGetNpadControllerColorSplit(HidNpadIdType_Handheld, &color_left, &color_right); + if (R_SUCCEEDED(res)) { + newBackup = json::object({ + {"name", "_backup"}, + {"L_JC", ColorSwapper::BGRToHex(color_left.main)}, + {"L_BTN", ColorSwapper::BGRToHex(color_left.sub)}, + {"R_JC", ColorSwapper::BGRToHex(color_right.main)}, + {"R_BTN", ColorSwapper::BGRToHex(color_right.sub)} + }); + } + return newBackup; + + } + + std::tuple, std::vector>> getProfiles(const char* path){ + std::vector names; + std::vector> colorValues; + bool properData; + std::fstream profilesFile; + json profilesJson; + if(std::filesystem::exists(path)){ + profilesFile.open(path, std::fstream::in); + profilesFile >> profilesJson; + profilesFile.close(); + } + if(profilesJson.empty()){ + profilesJson = {{ + {"L_BTN", "0A1E0A"}, + {"L_JC", "82FF96"}, + {"R_BTN", "0A1E28"}, + {"R_JC", "96F5F5"}, + {"name", "Animal Crossing: New Horizons"} + }}; + util::writeJsonToFile(profilesJson, path); + } + for (const auto& x : profilesJson.items()){ + std::string name = x.value()["name"]; + std::vector values = { + std::string(x.value()["L_JC"]), + std::string(x.value()["L_BTN"]), + std::string(x.value()["R_JC"]), + std::string(x.value()["R_BTN"]) + }; + properData = true; + for(auto& str : values){ + if(!ColorSwapper::isHexaAnd3Bytes(str)){ + properData = false; + } + } + if(properData){ + if(name == "") name = "Unamed"; + names.push_back(name); + colorValues.push_back({ + ColorSwapper::hexToBGR(values[0]), + ColorSwapper::hexToBGR(values[1]), + ColorSwapper::hexToBGR(values[2]), + ColorSwapper::hexToBGR(values[3]) + }); + } + } + return std::make_tuple(names, colorValues); + } + + void changeJCColor(std::vector values){ + hiddbgInitialize(); + hidsysInitialize(); + ProgressEvent::instance().reset(); + ProgressEvent::instance().setStep(1); + int res = setColor(values); + if(res != 0){ + util::showDialogBox("Could not change the Joy-Cons color. Make sure they're docked and try again.\nError :" + std::to_string(res), "Ok"); + } + hiddbgExit(); + hidsysExit(); + ProgressEvent::instance().setStep(ProgressEvent::instance().getMax()); + } + + void backupJCColor(const char* path){ + hiddbgInitialize(); + hidsysInitialize(); + ProgressEvent::instance().reset(); + ProgressEvent::instance().setStep(1); + json backup; + json profiles; + std::fstream profilesFile; + if(std::filesystem::exists(path)){ + profilesFile.open(path, std::fstream::in); + profilesFile >> profiles; + profilesFile.close(); + } + + std::vector oldBackups; + int i = 0; + for (const auto& x : profiles.items()){ + if(x.value()["name"] == "_backup") { + oldBackups.push_back(i); + } + i++; + } + for (auto &k : oldBackups){ + profiles.erase(profiles.begin() + k); + } + + while(backup.empty()){ + backup = backupProfile(); + } + + profiles.push_back(backup); + //backup.push_back(profiles); + util::writeJsonToFile(profiles, path); + hiddbgExit(); + hidsysExit(); + ProgressEvent::instance().setStep(ProgressEvent::instance().getMax()); + } + +} + +namespace PC { + + int setColor(std::vector colors){ + Result pads, pc; + int res = 0; + s32 nbEntries; + HidsysUniquePadId UniquePadIds[1] = {}; + + pads = hidsysGetUniquePadsFromNpad(HidNpadIdType_No1, UniquePadIds, 1 ,&nbEntries); + if(R_SUCCEEDED(pads)){ + pc = hiddbgUpdateControllerColor(colors[0], colors[1], UniquePadIds[0]); + if (R_FAILED(pc)) res +=1; + } + else{ + res +=4; + } + return res; + } + + int backupToJSON(json &profiles, const char* path){ + HidNpadControllerColor color; + Result res = hidGetNpadControllerColorSingle(HidNpadIdType_No1, &color); + std::vector oldBackups; + if (R_SUCCEEDED(res)) { + int i = 0; + for (const auto& x : profiles.items()){ + if(x.value()["name"] == "_backup") { + oldBackups.push_back(i); + } + i++; + } + for (auto &k : oldBackups){ + profiles.erase(profiles.begin() + k); + } + json newBackup = json::object({ + {"name", "_backup"}, + {"BODY", ColorSwapper::BGRToHex(color.main)}, + {"BTN", ColorSwapper::BGRToHex(color.sub)} + }); + profiles.push_back(newBackup); + util::writeJsonToFile(profiles, path); + return 0; + } + else{ + return -1; + } + } + + json backupProfile(){ + json newBackup; + HidNpadControllerColor color; + Result res = hidGetNpadControllerColorSingle(HidNpadIdType_No1, &color); + if (R_SUCCEEDED(res)) { + newBackup = json::object({ + {"name", "_backup"}, + {"BODY", ColorSwapper::BGRToHex(color.main)}, + {"BTN", ColorSwapper::BGRToHex(color.sub)} + }); + } + return newBackup; + + } + + std::tuple, std::vector>> getProfiles(const char* path){ + std::vector names; + std::vector> colorValues; + bool properData; + std::fstream profilesFile; + json profilesJson; + if(std::filesystem::exists(path)){ + profilesFile.open(path, std::fstream::in); + profilesFile >> profilesJson; + profilesFile.close(); + } + if(profilesJson.empty()){ + profilesJson = {{ + {"BTN", "2d2d2d"}, + {"BODY", "e6e6e6"}, + {"name", "Default black"} + }}; + util::writeJsonToFile(profilesJson, path); + } + for (const auto& x : profilesJson.items()){ + std::string name = x.value()["name"]; + std::vector values = { + std::string(x.value()["BODY"]), + std::string(x.value()["BTN"]) + }; + properData = true; + for(auto& str : values){ + if(!ColorSwapper::isHexaAnd3Bytes(str)){ + properData = false; + } + } + if(properData){ + if(name == "") name = "Unamed"; + names.push_back(name); + colorValues.push_back({ + ColorSwapper::hexToBGR(values[0]), + ColorSwapper::hexToBGR(values[1]) + }); + } + } + return std::make_tuple(names, colorValues); + } + + void changePCColor(std::vector values){ + hiddbgInitialize(); + hidsysInitialize(); + ProgressEvent::instance().reset(); + ProgressEvent::instance().setStep(1); + int res = setColor(values); + if(res != 0){ + util::showDialogBox("Could not change the Pro-Con color. Make they're connected to P1. This feature may not work on unoffical controllers. \nError :" + std::to_string(res), "Ok"); + } + hiddbgExit(); + hidsysExit(); + ProgressEvent::instance().setStep(ProgressEvent::instance().getMax()); + } + + void backupPCColor(const char* path){ + hiddbgInitialize(); + hidsysInitialize(); + ProgressEvent::instance().reset(); + ProgressEvent::instance().setStep(1); + json backup; + json profiles; + std::fstream profilesFile; + if(std::filesystem::exists(path)){ + profilesFile.open(path, std::fstream::in); + profilesFile >> profiles; + profilesFile.close(); + } + + std::vector oldBackups; + int i = 0; + for (const auto& x : profiles.items()){ + if(x.value()["name"] == "_backup") { + oldBackups.push_back(i); + } + i++; + } + for (auto &k : oldBackups){ + profiles.erase(profiles.begin() + k); + } + + while(backup.empty()){ + backup = backupProfile(); + } + + profiles.push_back(backup); + //backup.push_back(profiles); + util::writeJsonToFile(profiles, path); + hiddbgExit(); + hidsysExit(); + ProgressEvent::instance().setStep(ProgressEvent::instance().getMax()); + } + +} \ No newline at end of file diff --git a/source/current_cfw.cpp b/source/current_cfw.cpp index 8f51743..e2bdd57 100644 --- a/source/current_cfw.cpp +++ b/source/current_cfw.cpp @@ -1,52 +1,60 @@ #include "current_cfw.hpp" #include -bool isServiceRunning(const char *serviceName) { - Handle handle; - SmServiceName service_name = smEncodeName(serviceName); - bool running = R_FAILED(smRegisterService(&handle, service_name, false, 1)); - svcCloseHandle(handle); - if (!running) - smUnregisterService(service_name); +namespace CurrentCfw { - return running; -} + namespace { -Result smAtmosphereHasService(bool *out, SmServiceName name) { - u8 tmp = 0; - Result rc = serviceDispatchInOut(smGetServiceSession(), 65100, name, tmp); - if (R_SUCCEEDED(rc) && out) - *out = tmp; - return rc; -} + bool isServiceRunning(const char *serviceName) { + Handle handle; + SmServiceName service_name = smEncodeName(serviceName); + bool running = R_FAILED(smRegisterService(&handle, service_name, false, 1)); + svcCloseHandle(handle); + if (!running) + smUnregisterService(service_name); -CFW getCFW(){ - bool res = false; - if(R_SUCCEEDED(smAtmosphereHasService(&res, smEncodeName("rnx")))) { - if(res) - return rnx; - smAtmosphereHasService(&res, smEncodeName("tx")); - if(res) - return sxos; - } - else { // use old method just in case - if(isServiceRunning("rnx")) return rnx; - if(isServiceRunning("tx")) return sxos; - } - return ams; -} - -std::string getAmsInfo() { - u64 version; - std::string res; - if(R_SUCCEEDED(splGetConfig((SplConfigItem) 65000, &version))){ - res += std::to_string((version >> 56) & ((1 << 8) - 1)) + "." + - std::to_string((version >> 48) & ((1 << 8) - 1)) + "." + - std::to_string((version >> 40) & ((1 << 8) - 1)); - if(R_SUCCEEDED(splGetConfig((SplConfigItem) 65007, &version))) - res += version ? "|E" : "|S"; - return res; + return running; } - else - return "Couldn't retrieve AMS version"; + + Result smAtmosphereHasService(bool *out, SmServiceName name) { + u8 tmp = 0; + Result rc = serviceDispatchInOut(smGetServiceSession(), 65100, name, tmp); + if (R_SUCCEEDED(rc) && out) + *out = tmp; + return rc; + } + + } + + CFW getCFW(){ + bool res = false; + if(R_SUCCEEDED(smAtmosphereHasService(&res, smEncodeName("rnx")))) { + if(res) + return CFW::rnx; + smAtmosphereHasService(&res, smEncodeName("tx")); + if(res) + return CFW::sxos; + } + else { // use old method just in case + if(isServiceRunning("rnx")) return CFW::rnx; + if(isServiceRunning("tx")) return CFW::sxos; + } + return CFW::ams; + } + + std::string getAmsInfo() { + u64 version; + std::string res; + if(R_SUCCEEDED(splGetConfig((SplConfigItem) 65000, &version))){ + res += std::to_string((version >> 56) & ((1 << 8) - 1)) + "." + + std::to_string((version >> 48) & ((1 << 8) - 1)) + "." + + std::to_string((version >> 40) & ((1 << 8) - 1)); + if(R_SUCCEEDED(splGetConfig((SplConfigItem) 65007, &version))) + res += version ? "|E" : "|S"; + return res; + } + else + return "Couldn't retrieve AMS version"; + } + } \ No newline at end of file diff --git a/source/download.cpp b/source/download.cpp index 3945f95..1a6dbf4 100644 --- a/source/download.cpp +++ b/source/download.cpp @@ -12,64 +12,94 @@ #include "progress_event.hpp" -#define API_AGENT "HamletDuFromage" -#define _1MiB 0x100000 +constexpr const char API_AGENT[] = "HamletDuFromage"; +constexpr int _1MiB = 0x100000; using json = nlohmann::json; -std::chrono::_V2::steady_clock::time_point time_old; -double dlold; +namespace download { -typedef struct -{ - char *memory; - size_t size; -} MemoryStruct_t; + namespace { -typedef struct -{ - u_int8_t *data; - size_t data_size; - u_int64_t offset; - FILE *out; -} ntwrk_struct_t; + std::chrono::_V2::steady_clock::time_point time_old; + double dlold; -static size_t WriteMemoryCallback(void *contents, size_t size, size_t num_files, void *userp) -{ - ntwrk_struct_t *data_struct = (ntwrk_struct_t *)userp; - size_t realsize = size * num_files; + typedef struct + { + char *memory; + size_t size; + } MemoryStruct_t; + + typedef struct + { + u_int8_t *data; + size_t data_size; + u_int64_t offset; + FILE *out; + } ntwrk_struct_t; + + static size_t WriteMemoryCallback(void *contents, size_t size, size_t num_files, void *userp) + { + ntwrk_struct_t *data_struct = (ntwrk_struct_t *)userp; + size_t realsize = size * num_files; + + if (realsize + data_struct->offset >= data_struct->data_size) + { + fwrite(data_struct->data, data_struct->offset, 1, data_struct->out); + data_struct->offset = 0; + } + + memcpy(&data_struct->data[data_struct->offset], contents, realsize); + data_struct->offset += realsize; + data_struct->data[data_struct->offset] = 0; + return realsize; + } + + int download_progress(void *p, double dltotal, double dlnow, double ultotal, double ulnow) + { + if (dltotal <= 0.0) return 0; + + double fractionDownloaded = dlnow / dltotal; + int counter = (int) (fractionDownloaded * ProgressEvent::instance().getMax()); //20 is the number of increments + ProgressEvent::instance().setStep(std::min(ProgressEvent::instance().getMax() - 1, counter)); + ProgressEvent::instance().setNow(dlnow); + ProgressEvent::instance().setTotalCount(dltotal); + auto time_now = std::chrono::steady_clock::now(); + double elasped_time = ((std::chrono::duration) (time_now - time_old)).count(); + if(elasped_time > 1.2f) { + ProgressEvent::instance().setSpeed((dlnow - dlold) / elasped_time); + dlold = dlnow; + time_old = time_now; + } + return 0; + } + + struct MemoryStruct { + char *memory; + size_t size; + }; + + static size_t WriteMemoryCallback2(void *contents, size_t size, size_t nmemb, void *userp) + { + size_t realsize = size * nmemb; + struct MemoryStruct *mem = (struct MemoryStruct *)userp; + + char *ptr = static_cast(realloc(mem->memory, mem->size + realsize + 1)); + if(ptr == NULL) { + /* out of memory! */ + return 0; + } + + mem->memory = ptr; + memcpy(&(mem->memory[mem->size]), contents, realsize); + mem->size += realsize; + mem->memory[mem->size] = 0; + + return realsize; + } - if (realsize + data_struct->offset >= data_struct->data_size) - { - fwrite(data_struct->data, data_struct->offset, 1, data_struct->out); - data_struct->offset = 0; } - memcpy(&data_struct->data[data_struct->offset], contents, realsize); - data_struct->offset += realsize; - data_struct->data[data_struct->offset] = 0; - return realsize; -} - -int download_progress(void *p, double dltotal, double dlnow, double ultotal, double ulnow) -{ - if (dltotal <= 0.0) return 0; - - double fractionDownloaded = dlnow / dltotal; - int counter = (int) (fractionDownloaded * ProgressEvent::instance().getMax()); //20 is the number of increments - ProgressEvent::instance().setStep(std::min(ProgressEvent::instance().getMax() - 1, counter)); - ProgressEvent::instance().setNow(dlnow); - ProgressEvent::instance().setTotalCount(dltotal); - auto time_now = std::chrono::steady_clock::now(); - double elasped_time = ((std::chrono::duration) (time_now - time_old)).count(); - if(elasped_time > 1.2f) { - ProgressEvent::instance().setSpeed((dlnow - dlold) / elasped_time); - dlold = dlnow; - time_old = time_now; - } - return 0; -} - std::vector downloadFile(const char *url, const char *output, int api) { ProgressEvent::instance().reset(); @@ -124,30 +154,6 @@ std::vector downloadFile(const char *url, const char *output, int } -struct MemoryStruct { - char *memory; - size_t size; -}; - -static size_t WriteMemoryCallback2(void *contents, size_t size, size_t nmemb, void *userp) -{ - size_t realsize = size * nmemb; - struct MemoryStruct *mem = (struct MemoryStruct *)userp; - - char *ptr = static_cast(realloc(mem->memory, mem->size + realsize + 1)); - if(ptr == NULL) { - /* out of memory! */ - return 0; - } - - mem->memory = ptr; - memcpy(&(mem->memory[mem->size]), contents, realsize); - mem->size += realsize; - mem->memory[mem->size] = 0; - - return realsize; -} - std::string fetchTitle(const char *url){ CURL *curl_handle; struct MemoryStruct chunk; @@ -273,4 +279,6 @@ std::vector> getLinks(const char *url) { res.push_back(std::make_pair(it.key(), it.value())); } return res; +} + } \ No newline at end of file diff --git a/source/download_cheats_page.cpp b/source/download_cheats_page.cpp index 777e235..536249b 100644 --- a/source/download_cheats_page.cpp +++ b/source/download_cheats_page.cpp @@ -16,7 +16,7 @@ DownloadCheatsPage::DownloadCheatsPage(uint64_t tid) : AppletFrame(true, true) this->setTitle("menus/cheats/menu"_i18n); std::string bid = ""; - if(running_cfw == ams) + if(CurrentCfw::running_cfw == CFW::ams) bid = GetBuilID(tid); if(bid == "") bid = GetBuilIDFromFile(tid); @@ -31,7 +31,7 @@ DownloadCheatsPage::DownloadCheatsPage(uint64_t tid) : AppletFrame(true, true) if(bid != "") { std::vector headers = {"accept: application/json"}; - json cheatsInfo = getRequest((CHEATSLIPS_CHEATS_URL + formatApplicationId(tid) + "/" + bid).c_str(), headers); + json cheatsInfo = download::getRequest((CHEATSLIPS_CHEATS_URL + util::formatApplicationId(tid) + "/" + bid).c_str(), headers); if(cheatsInfo.find("cheats") != cheatsInfo.end()) { for (const auto& p : cheatsInfo["cheats"].items()) { json cheat = p.value(); @@ -75,7 +75,7 @@ DownloadCheatsPage::DownloadCheatsPage(uint64_t tid) : AppletFrame(true, true) if(token.find("token") != token.end()) { headers.push_back("X-API-TOKEN: " + token["token"].get()); } - json cheatsInfo = getRequest(("https://www.cheatslips.com/api/v1/cheats/" + formatApplicationId(tid) + "/" + bid).c_str(), headers); + json cheatsInfo = download::getRequest(("https://www.cheatslips.com/api/v1/cheats/" + util::formatApplicationId(tid) + "/" + bid).c_str(), headers); if(cheatsInfo.find("cheats") != cheatsInfo.end()) { for (const auto& p : cheatsInfo["cheats"].items()) { if(std::find(ids.begin(), ids.end(), p.value()["id"]) != ids.end()) { @@ -178,14 +178,14 @@ std::string DownloadCheatsPage::GetBuilIDFromFile(uint64_t tid) { json lookupTable; try { - lookupTable = json::parse(std::string(json::from_cbor(downloadFile(LOOKUP_TABLE_CBOR)))); + lookupTable = json::parse(std::string(json::from_cbor(download::downloadFile(LOOKUP_TABLE_CBOR)))); } catch (json::parse_error& e) { //std::cout << "message: " << e.what() << '\n' << "exception id: " << e.id << '\n' << "byte position of error: " << e.byte << std::endl; } - std::string tidstr = formatApplicationId(tid); + std::string tidstr = util::formatApplicationId(tid); std::string versionstr = std::to_string(version); if(lookupTable.find(tidstr) != lookupTable.end()) { json buildIDs = lookupTable[tidstr]; @@ -212,20 +212,20 @@ std::string DownloadCheatsPage::GetCheatsTitle(json cheat) { void DownloadCheatsPage::WriteCheats(uint64_t tid, std::string bid, std::string cheatContent) { std::string path; - std::string tidstr = formatApplicationId(tid); - switch(running_cfw){ - case ams: + std::string tidstr = util::formatApplicationId(tid); + switch(CurrentCfw::running_cfw){ + case CFW::ams: path = std::string(AMS_PATH) + std::string(CONTENTS_PATH); break; - case rnx: + case CFW::rnx: path = std::string(REINX_PATH) + std::string(CONTENTS_PATH); break; - case sxos: + case CFW::sxos: path = std::string(SXOS_PATH) + std::string(TITLES_PATH); break; } path += tidstr + "/cheats/"; - createTree(path); + util::createTree(path); path += bid + ".txt"; std::ofstream cheatFile; cheatFile.open(path, std::ios::app); @@ -237,18 +237,18 @@ void DownloadCheatsPage::WriteCheats(uint64_t tid, std::string bid, std::string void DownloadCheatsPage::DeleteCheats(uint64_t tid, std::string bid) { std::string path; - switch(running_cfw){ - case ams: + switch(CurrentCfw::running_cfw){ + case CFW::ams: path = std::string(AMS_PATH) + std::string(CONTENTS_PATH); break; - case rnx: + case CFW::rnx: path = std::string(REINX_PATH) + std::string(CONTENTS_PATH); break; - case sxos: + case CFW::sxos: path = std::string(SXOS_PATH) + std::string(TITLES_PATH); break; } - std::filesystem::remove(path + formatApplicationId(tid) + "/cheats/" + bid + ".txt"); + std::filesystem::remove(path + util::formatApplicationId(tid) + "/cheats/" + bid + ".txt"); } void DownloadCheatsPage::ShowCheatsContent(nlohmann::ordered_json titles) { diff --git a/source/download_payload_page.cpp b/source/download_payload_page.cpp index 7f83c9b..f037f36 100644 --- a/source/download_payload_page.cpp +++ b/source/download_payload_page.cpp @@ -17,7 +17,7 @@ DownloadPayloadPage::DownloadPayloadPage() : AppletFrame(true, true) ); list->addView(label); - auto links = getLinks(PAYLOAD_URL); + auto links = download::getLinks(PAYLOAD_URL); int nbLinks = links.size(); if(nbLinks){ for (int i = 0; igetClickEvent()->subscribe([&, text, url, path](brls::View* view) { - createTree(BOOTLOADER_PL_PATH); + util::createTree(BOOTLOADER_PL_PATH); brls::StagedAppletFrame* stagedFrame = new brls::StagedAppletFrame(); stagedFrame->setTitle("menus/getting_paylaod"_i18n); stagedFrame->addStage( new ConfirmPage(stagedFrame, text) ); stagedFrame->addStage( - new WorkerPage(stagedFrame, "menus/common/downloading"_i18n, [url, path](){downloadFile(url.c_str(), path.c_str(), OFF);}) + new WorkerPage(stagedFrame, "menus/common/downloading"_i18n, [url, path](){download::downloadFile(url.c_str(), path.c_str(), OFF);}) ); stagedFrame->addStage( new ConfirmPage(stagedFrame, "menus/common/downloadingload_all_done"_i18n, true) diff --git a/source/exclude_page.cpp b/source/exclude_page.cpp index 911d03d..e155bc7 100644 --- a/source/exclude_page.cpp +++ b/source/exclude_page.cpp @@ -1,11 +1,11 @@ #include "exclude_page.hpp" #include -#include "utils.hpp" -#include "extract.hpp" #include #include #include - +#include "extract.hpp" +#include "utils.hpp" + namespace i18n = brls::i18n; using namespace i18n::literals; ExcludePage::ExcludePage() : AppletFrame(true, true) @@ -29,9 +29,7 @@ ExcludePage::ExcludePage() : AppletFrame(true, true) int recordCount = 0; size_t controlSize = 0; - titles = readLineByLine(CHEATS_EXCLUDE); - - //std::tuple, std::vector> items; + titles = util::readLineByLine(CHEATS_EXCLUDE); while (true) { @@ -51,7 +49,7 @@ ExcludePage::ExcludePage() : AppletFrame(true, true) i++; continue; } - App* app = (App*) malloc(sizeof(App)); + util::app* app = (util::app*) malloc(sizeof(util::app)); app->tid = tid; memset(app->name, 0, sizeof(app->name)); @@ -59,28 +57,26 @@ ExcludePage::ExcludePage() : AppletFrame(true, true) memcpy(app->icon, controlData.icon, sizeof(app->icon)); - // Add the ListItem brls::ToggleListItem *listItem; - if(titles.find(formatApplicationId(tid)) != titles.end()) - listItem = new brls::ToggleListItem(formatListItemTitle(std::string(app->name)), 0); + if(titles.find(util::formatApplicationId(tid)) != titles.end()) + listItem = new brls::ToggleListItem(util::formatListItemTitle(std::string(app->name)), 0); else - listItem = new brls::ToggleListItem(formatListItemTitle(std::string(app->name)), 1); + listItem = new brls::ToggleListItem(util::formatListItemTitle(std::string(app->name)), 1); listItem->setThumbnail(app->icon, sizeof(app->icon)); - std::get<0>(items).push_back(listItem); - std::get<1>(items).push_back(formatApplicationId(app->tid)); + items.insert(std::make_pair(listItem, util::formatApplicationId(app->tid))); list->addView(listItem); i++; } list->registerAction("menus/cheats/exclude_titles_save"_i18n, brls::Key::B, [this] { - std::set exclude{}; - for(int i = 0; i < (int) std::get<1>(items).size(); i++){ - if(!std::get<0>(items)[i]->getToggleState()){ - exclude.insert(std::get<1>(items)[i]); + std::set exclude; + for (const auto& item : items) { + if(!item.first->getToggleState()) { + exclude.insert(item.second); } } - writeTitlesToFile(exclude, CHEATS_EXCLUDE); + extract::writeTitlesToFile(exclude, CHEATS_EXCLUDE); brls::Application::popView(); return true; }); diff --git a/source/extract.cpp b/source/extract.cpp index 6a1a66b..04aca03 100644 --- a/source/extract.cpp +++ b/source/extract.cpp @@ -15,11 +15,20 @@ namespace i18n = brls::i18n; using namespace i18n::literals; + +namespace extract { + + namespace { + bool caselessCompare (const std::string& a, const std::string& b){ + return strcasecmp(a.c_str(), b.c_str()) < 0; + } + } + void extract(const char * filename, const char* workingPath, int overwriteInis){ ProgressEvent::instance().reset(); ProgressEvent::instance().setStep(1); chdir(workingPath); - std::set ignoreList = readLineByLine(FILES_IGNORE); + std::set ignoreList = util::readLineByLine(FILES_IGNORE); std::set::iterator it; zipper::Unzipper unzipper(filename); std::vector entries = unzipper.entries(); @@ -53,7 +62,7 @@ void extract(const char * filename, const char* workingPath, int overwriteInis){ else if(entries[i].name.substr(0, 13) == "hekate_ctcaer"){ unzipper.extractEntry(entries[i].name); int c = 0; - while(R_FAILED(CopyFile(("/" + entries[i].name).c_str(), UPDATE_BIN_PATH)) && c < 10){ + while(R_FAILED(util::CopyFile(("/" + entries[i].name).c_str(), UPDATE_BIN_PATH)) && c < 10){ c++; } } @@ -72,7 +81,7 @@ void extract(const char * filename, const char* workingPath, const char* toExclu ProgressEvent::instance().reset(); ProgressEvent::instance().setStep(1); chdir(workingPath); - std::set ignoreList = readLineByLine(FILES_IGNORE); + std::set ignoreList = util::readLineByLine(FILES_IGNORE); ignoreList.insert(toExclude); std::set::iterator it; zipper::Unzipper unzipper(filename); @@ -138,7 +147,7 @@ std::vector getInstalledTitlesNs(){ rc = nacpGetLanguageEntry(&buf->nacp, &langentry); if (R_SUCCEEDED(rc)) { - titles.push_back(formatApplicationId(recs[i].application_id)); + titles.push_back(util::formatApplicationId(recs[i].application_id)); } } } @@ -176,10 +185,6 @@ std::vector excludeTitles(const char* path, std::vector titles, CFW cfw, bool credits){ //TODO: REWRITE WITH SETS INSTEAD OF VECTORS ProgressEvent::instance().reset(); @@ -188,19 +193,19 @@ void extractCheats(const char * zipPath, std::vector titles, CFW cf std::set extractedTitles; int offset = 0; switch(cfw){ - case ams: + case CFW::ams: offset = std::string(CONTENTS_PATH).length(); std::filesystem::create_directory(AMS_PATH); std::filesystem::create_directory(AMS_CONTENTS); chdir(AMS_PATH); break; - case rnx: + case CFW::rnx: offset = std::string(CONTENTS_PATH).length(); std::filesystem::create_directory(REINX_PATH); std::filesystem::create_directory(REINX_CONTENTS); chdir(REINX_PATH); break; - case sxos: + case CFW::sxos: offset = std::string(TITLES_PATH).length(); std::filesystem::create_directory(SXOS_PATH); std::filesystem::create_directory(SXOS_TITLES); @@ -214,7 +219,6 @@ void extractCheats(const char * zipPath, std::vector titles, CFW cf entriesNames.push_back(entries[i].name); } - std::sort(entriesNames.begin(), entriesNames.end(), caselessCompare); std::vector parents; @@ -267,9 +271,9 @@ void extractCheats(const char * zipPath, std::vector titles, CFW cf } unzipper.close(); writeTitlesToFile(extractedTitles, UPDATED_TITLES_PATH); - auto cheatsVerVec = downloadFile(CHEATS_URL_VERSION); + auto cheatsVerVec = download::downloadFile(CHEATS_URL_VERSION); std::string cheatsVer(cheatsVerVec.begin(), cheatsVerVec.end()); - saveVersion(cheatsVer, CHEATS_VERSION); + util::saveVersion(cheatsVer, CHEATS_VERSION); ProgressEvent::instance().setStep(ProgressEvent::instance().getMax()); } @@ -279,19 +283,19 @@ void extractAllCheats(const char * zipPath, CFW cfw){ std::vector entries = unzipper.entries(); int offset = 0; switch(cfw){ - case ams: + case CFW::ams: offset = std::string(CONTENTS_PATH).length() + 17 + 7; std::filesystem::create_directory(AMS_PATH); std::filesystem::create_directory(AMS_CONTENTS); chdir(AMS_PATH); break; - case rnx: + case CFW::rnx: offset = std::string(CONTENTS_PATH).length() + 17 + 7; std::filesystem::create_directory(REINX_PATH); std::filesystem::create_directory(REINX_CONTENTS); chdir(REINX_PATH); break; - case sxos: + case CFW::sxos: offset = std::string(TITLES_PATH).length() + 17 + 7; std::filesystem::create_directory(SXOS_PATH); std::filesystem::create_directory(SXOS_TITLES); @@ -306,9 +310,9 @@ void extractAllCheats(const char * zipPath, CFW cfw){ ProgressEvent::instance().setStep(j); } unzipper.close(); - auto cheatsVerVec = downloadFile(CHEATS_URL_VERSION); + auto cheatsVerVec = download::downloadFile(CHEATS_URL_VERSION); std::string cheatsVer(cheatsVerVec.begin(), cheatsVerVec.end()); - saveVersion(cheatsVer, CHEATS_VERSION); + util::saveVersion(cheatsVer, CHEATS_VERSION); ProgressEvent::instance().setStep(ProgressEvent::instance().getMax()); } @@ -335,13 +339,13 @@ void writeTitlesToFile(std::set titles, const char* path){ void removeCheats(CFW cfw){ std::string path; switch(cfw){ - case ams: + case CFW::ams: path = std::string(AMS_PATH) + std::string(CONTENTS_PATH); break; - case rnx: + case CFW::rnx: path = std::string(REINX_PATH) + std::string(CONTENTS_PATH); break; - case sxos: + case CFW::sxos: path = std::string(SXOS_PATH) + std::string(TITLES_PATH); break; } @@ -364,4 +368,6 @@ void removeCheats(CFW cfw){ std::filesystem::remove(UPDATED_TITLES_PATH); std::filesystem::remove(CHEATS_VERSION); ProgressEvent::instance().setStep(ProgressEvent::instance().getMax()); +} + } \ No newline at end of file diff --git a/source/hide_tabs_page.cpp b/source/hide_tabs_page.cpp index 35ba306..ad63e2e 100644 --- a/source/hide_tabs_page.cpp +++ b/source/hide_tabs_page.cpp @@ -18,7 +18,7 @@ HideTabsPage::HideTabsPage() : AppletFrame(true, true) { ); list->addView(label); - json hideStatus = parseJsonFile(HIDE_TABS_JSON); + json hideStatus = util::parseJsonFile(HIDE_TABS_JSON); bool status = false; if(hideStatus.find("about") != hideStatus.end()) { diff --git a/source/list_download_tab.cpp b/source/list_download_tab.cpp index 08f6a0a..9903e1d 100644 --- a/source/list_download_tab.cpp +++ b/source/list_download_tab.cpp @@ -25,52 +25,53 @@ ListDownloadTab::ListDownloadTab(const archiveType type) : this->description = new brls::Label(brls::LabelStyle::DESCRIPTION, "", true); switch(type){ - case sigpatches: - links = getLinks(SIGPATCHES_URL); + case archiveType::ams_cfw: + case archiveType::sigpatches: + links = download::getLinks(SIGPATCHES_URL); operation += "menus/main/sigpatches"_i18n; this->description->setText( "menus/main/sigpatches_text"_i18n ); break; - case fw: - links = getLinks(FIRMWARE_URL); + case archiveType::fw: + links = download::getLinks(FIRMWARE_URL); operation += "menus/main/firmware"_i18n; SetSysFirmwareVersion ver; if (R_SUCCEEDED(setsysGetFirmwareVersion(&ver))) firmwareText += ver.display_version; else firmwareText += "menus/main/not_found"_i18n; this->description->setText(firmwareText); break; - case app: + case archiveType::app: links.push_back(std::make_pair("menus/main/latest_cheats"_i18n, APP_URL)); operation += "menus/main/app"_i18n; break; - case cfw: - links = getLinks(CFW_URL); - sxoslinks = getLinks(SXOS_URL); + case archiveType::cfw: + links = download::getLinks(CFW_URL); + sxoslinks = download::getLinks(SXOS_URL); links.insert(links.end(), sxoslinks.begin(), sxoslinks.end()); operation += "menus/main/cfw"_i18n; this->description->setText( "menus/main/cfw_text"_i18n ); break; - case cheats: - auto cheatsVerVec = downloadFile(CHEATS_URL_VERSION); + case archiveType::cheats: + auto cheatsVerVec = download::downloadFile(CHEATS_URL_VERSION); std::string cheatsVer(cheatsVerVec.begin(), cheatsVerVec.end()); if(cheatsVer != ""){ - switch(running_cfw){ - case sxos: + switch(CurrentCfw::running_cfw){ + case CFW::sxos: links.push_back(std::make_pair("menus/main/get_cheats"_i18n + cheatsVer + ")", CHEATS_URL_TITLES)); break; - case ams: + case CFW::ams: links.push_back(std::make_pair("menus/main/get_cheats"_i18n + cheatsVer + ")", CHEATS_URL_CONTENTS)); break; - case rnx: + case CFW::rnx: links.push_back(std::make_pair("menus/main/get_cheats"_i18n + cheatsVer + ")", CHEATS_URL_CONTENTS)); break; } } operation += "menus/main/cheats"_i18n; - currentCheatsVer += readVersion(CHEATS_VERSION); + currentCheatsVer += util::readVersion(CHEATS_VERSION); this->description->setText(currentCheatsVer); break; } @@ -91,14 +92,14 @@ ListDownloadTab::ListDownloadTab(const archiveType type) : new ConfirmPage(stagedFrame, text) ); stagedFrame->addStage( - new WorkerPage(stagedFrame, "menus/common/downloading"_i18n, [url, type](){downloadArchive(url, type);}) + new WorkerPage(stagedFrame, "menus/common/downloading"_i18n, [url, type](){util::downloadArchive(url, type);}) ); stagedFrame->addStage( - new WorkerPage(stagedFrame, "menus/common/extracting"_i18n, [type](){extractArchive(type);}) + new WorkerPage(stagedFrame, "menus/common/extracting"_i18n, [type](){util::extractArchive(type);}) ); stagedFrame->addStage( new ConfirmPage(stagedFrame, - (type == sigpatches) ? + (type == archiveType::sigpatches) ? "menus/common/all_done"_i18n + "\n" + "menus/sigpatches/reboot"_i18n : "menus/common/all_done"_i18n, true) @@ -119,7 +120,7 @@ ListDownloadTab::ListDownloadTab(const archiveType type) : this->addView(notFound); } - if(type == cheats){ + if(type == archiveType::cheats){ cheatSlipLabel = new brls::Label( brls::LabelStyle::DESCRIPTION, "menus/cheats/cheatslips_label"_i18n, @@ -155,7 +156,7 @@ ListDownloadTab::ListDownloadTab(const archiveType type) : } std::string body = "{\"email\":\"" + std::string(usr) + "\",\"password\":\"" + std::string(pwd) + "\"}"; - nlohmann::json token = getRequest(CHEATSLIPS_TOKEN_URL, + nlohmann::json token = download::getRequest(CHEATSLIPS_TOKEN_URL, {"Accept: application/json", "Content-Type: application/json", "charset: utf-8"}, diff --git a/source/main.cpp b/source/main.cpp index c748a21..afebf38 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -17,7 +17,7 @@ using namespace i18n::literals; //TimeServiceType __nx_time_service_type = TimeServiceType_System; -const CFW running_cfw = getCFW(); +const CFW CurrentCfw::running_cfw = CurrentCfw::getCFW(); int main(int argc, char* argv[]) { @@ -28,7 +28,7 @@ int main(int argc, char* argv[]) return EXIT_FAILURE; } - nlohmann::json languageFile = parseJsonFile(LANGUAGE_JSON); + nlohmann::json languageFile = util::parseJsonFile(LANGUAGE_JSON); if(languageFile.find("language") != languageFile.end()) i18n::loadTranslations(languageFile["language"]); else @@ -48,7 +48,7 @@ int main(int argc, char* argv[]) splInitialize(); romfsInit(); - createTree(CONFIG_PATH); + util::createTree(CONFIG_PATH); brls::Logger::setLogLevel(brls::LogLevel::DEBUG); brls::Logger::debug("Start"); diff --git a/source/main_frame.cpp b/source/main_frame.cpp index a954bd4..75e841f 100644 --- a/source/main_frame.cpp +++ b/source/main_frame.cpp @@ -16,15 +16,15 @@ MainFrame::MainFrame() : TabFrame() this->setIcon("romfs:/gui_icon.png"); this->setTitle(std::string(APP_TITLE)); - std::string tag = getLatestTag(TAGS_INFO); + std::string tag = util::getLatestTag(TAGS_INFO); if(!tag.empty() && tag != APP_VERSION) this->setFooterText("v" + std::string(APP_VERSION) + "menus/main/new_update"_i18n); else this->setFooterText("v" + std::string(APP_VERSION)); - json hideStatus = parseJsonFile(HIDE_TABS_JSON); + json hideStatus = util::parseJsonFile(HIDE_TABS_JSON); - bool erista = isErista(); + bool erista = util::isErista(); if(hideStatus.find("about") == hideStatus.end() || !hideStatus["about"]) this->addTab("menus/main/about"_i18n, new AboutTab()); @@ -33,16 +33,16 @@ MainFrame::MainFrame() : TabFrame() this->addTab("menus/main/update_ams"_i18n, new AmsTab()); if(hideStatus.find("cfw") == hideStatus.end() || !hideStatus["cfw"]) - this->addTab("menus/main/update_cfw"_i18n, new ListDownloadTab(cfw)); + this->addTab("menus/main/update_cfw"_i18n, new ListDownloadTab(archiveType::cfw)); if(hideStatus.find("sigpatches") == hideStatus.end() || !hideStatus["sigpatches"]) - this->addTab("menus/main/update_sigpatches"_i18n, new ListDownloadTab(sigpatches)); + this->addTab("menus/main/update_sigpatches"_i18n, new ListDownloadTab(archiveType::sigpatches)); if(hideStatus.find("firmwares") == hideStatus.end() || !hideStatus["firmwares"]) - this->addTab("menus/main/download_firmware"_i18n, new ListDownloadTab(fw)); + this->addTab("menus/main/download_firmware"_i18n, new ListDownloadTab(archiveType::fw)); if(hideStatus.find("cheats") == hideStatus.end() || !hideStatus["cheats"]) - this->addTab("menus/main/download_cheats"_i18n, new ListDownloadTab(cheats)); + this->addTab("menus/main/download_cheats"_i18n, new ListDownloadTab(archiveType::cheats)); if(hideStatus.find("tools") == hideStatus.end() || !hideStatus["tools"]) this->addTab("menus/main/tools"_i18n, new ToolsTab(tag, erista)); diff --git a/source/net_page.cpp b/source/net_page.cpp index dbdcaad..d44754c 100644 --- a/source/net_page.cpp +++ b/source/net_page.cpp @@ -1,13 +1,14 @@ #include "net_page.hpp" #include #include -#include -#include "constants.hpp" -#include "main_frame.hpp" #include #include #include #include +#include "constants.hpp" +#include "main_frame.hpp" +#include "json.hpp" +#include "utils.hpp" namespace i18n = brls::i18n; using namespace i18n::literals; @@ -65,25 +66,21 @@ NetPage::NetPage() : AppletFrame(true, true) //dns_auto if(uuid){ - std::fstream profilesFile; - json profiles; - if(std::filesystem::exists(INTERNET_JSON)){ - profilesFile.open(INTERNET_JSON, std::fstream::in); - profilesFile >> profiles; - profilesFile.close(); - profiles.push_back(json::object({ - {"name", "90DNS (Europe)"}, - {"dns1", "163.172.141.219"}, - {"dns2", "207.246.121.77"} - })); - } - else{ + json profiles = util::parseJsonFile(INTERNET_JSON); + if(profiles.empty()) { profiles = {{ {"name", "90DNS (Europe)"}, {"dns1", "163.172.141.219"}, {"dns2", "207.246.121.77"} }}; } + else { + profiles.push_back(json::object({ + {"name", "90DNS (Europe)"}, + {"dns1", "163.172.141.219"}, + {"dns2", "207.246.121.77"} + })); + } profiles.push_back(json::object({ {"name", "90DNS (USA)"}, @@ -130,7 +127,6 @@ NetPage::NetPage() : AppletFrame(true, true) if(values.find("ip_addr") != values.end()){ if(inet_pton(AF_INET, std::string(values["ip_addr"]).c_str(), buf)){ profile.ip_setting_data.ip_address_setting.is_automatic = u8(0); - //nifmSetNetworkProfile(&profile, &profile.uuid); stringToIp(std::string(values["ip_addr"]), profile.ip_setting_data.ip_address_setting.current_addr.addr); } } diff --git a/source/payload_page.cpp b/source/payload_page.cpp index d1e3736..e77148f 100644 --- a/source/payload_page.cpp +++ b/source/payload_page.cpp @@ -16,7 +16,7 @@ PayloadPage::PayloadPage() : AppletFrame(true, true) true ); list->addView(label); - std::vector payloads = fetchPayloads(); + std::vector payloads = util::fetchPayloads(); int nbPayloads = payloads.size(); for (int i = 0; i < nbPayloads; i++){ std::string payload = payloads[i]; @@ -25,10 +25,10 @@ PayloadPage::PayloadPage() : AppletFrame(true, true) reboot_to_payload(payload.c_str()); brls::Application::popView(); }); - if(running_cfw == ams){ + if(CurrentCfw::running_cfw == CFW::ams){ listItem->registerAction("menus/payloads/set_reboot_payload"_i18n, brls::Key::X, [this, payload] { std::string res1; - if(R_SUCCEEDED(CopyFile(payload.c_str(), REBOOT_PAYLOAD_PATH))){ + if(R_SUCCEEDED(util::CopyFile(payload.c_str(), REBOOT_PAYLOAD_PATH))){ res1 += "menus/payloads/copy_success"_i18n + payload + "menus/payloads/to"_i18n + std::string(REBOOT_PAYLOAD_PATH) + "'."; } @@ -47,7 +47,7 @@ PayloadPage::PayloadPage() : AppletFrame(true, true) } listItem->registerAction("menus/payloads/set_reboot_payload_up"_i18n, brls::Key::Y, [this, payload] { std::string res2; - if(R_SUCCEEDED(CopyFile(payload.c_str(), UPDATE_BIN_PATH))){ + if(R_SUCCEEDED(util::CopyFile(payload.c_str(), UPDATE_BIN_PATH))){ res2 += "menus/payloads/copy_success"_i18n + payload + "menus/payloads/to"_i18n + std::string(UPDATE_BIN_PATH) + "'."; } else{ @@ -68,14 +68,14 @@ PayloadPage::PayloadPage() : AppletFrame(true, true) shutDown = new brls::ListItem("menus/common/shut_down"_i18n); shutDown->getClickEvent()->subscribe([](brls::View* view) { - shut_down(false); + util::shut_down(false); brls::Application::popView(); }); list->addView(shutDown); reboot = new brls::ListItem("menus/payloads/reboot"_i18n); reboot->getClickEvent()->subscribe([](brls::View* view) { - shut_down(true); + util::shut_down(true); brls::Application::popView(); }); list->addView(reboot); diff --git a/source/reboot_payload.c b/source/reboot_payload.c index a3a59df..ab9acd4 100644 --- a/source/reboot_payload.c +++ b/source/reboot_payload.c @@ -1,5 +1,4 @@ #include "reboot_payload.h" -#include "constants.hpp" #define IRAM_PAYLOAD_MAX_SIZE 0x2F000 #define IRAM_PAYLOAD_BASE 0x40010000 diff --git a/source/tools_tab.cpp b/source/tools_tab.cpp index c33ea3c..66ea54e 100644 --- a/source/tools_tab.cpp +++ b/source/tools_tab.cpp @@ -31,10 +31,10 @@ ToolsTab::ToolsTab(std::string tag, bool erista) : brls::List() new ConfirmPage(stagedFrame, text) ); stagedFrame->addStage( - new WorkerPage(stagedFrame, "menus/common/downloading"_i18n, [](){downloadArchive(APP_URL, app);}) + new WorkerPage(stagedFrame, "menus/common/downloading"_i18n, [](){util::downloadArchive(APP_URL, archiveType::app);}) ); stagedFrame->addStage( - new WorkerPage(stagedFrame, "menus/common/extracting"_i18n, [tag](){extractArchive(app, tag);}) + new WorkerPage(stagedFrame, "menus/common/extracting"_i18n, [tag](){util::extractArchive(archiveType::app, tag);}) ); stagedFrame->addStage( new ConfirmPage(stagedFrame, "menus/common/all_done"_i18n, true) @@ -156,7 +156,7 @@ ToolsTab::ToolsTab(std::string tag, bool erista) : brls::List() chdir("/"); std::string error = ""; if(std::filesystem::exists(COPY_FILES_JSON)){ - error = copyFiles(COPY_FILES_JSON); + error = util::copyFiles(COPY_FILES_JSON); } else{ error = "menus/tools/batch_copy_config_not_found"_i18n; @@ -180,9 +180,9 @@ ToolsTab::ToolsTab(std::string tag, bool erista) : brls::List() std::filesystem::remove(FW_ZIP_PATH); std::filesystem::remove(CHEATS_ZIP_PATH); std::filesystem::remove(SIGPATCHES_ZIP_PATH); - removeDir(AMS_DIRECTORY_PATH); - removeDir(SEPT_DIRECTORY_PATH); - removeDir(FW_DIRECTORY_PATH); + util::removeDir(AMS_DIRECTORY_PATH); + util::removeDir(SEPT_DIRECTORY_PATH); + util::removeDir(FW_DIRECTORY_PATH); brls::Dialog* dialog = new brls::Dialog("menus/common/all_done"_i18n); brls::GenericEvent::Callback callback = [dialog](brls::View* view) { dialog->close(); diff --git a/source/utils.cpp b/source/utils.cpp index 3497ee3..3789554 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -11,16 +11,7 @@ namespace i18n = brls::i18n; using namespace i18n::literals; -std::vector htmlProcess(std::string s, std::regex rgx){ - //std::vector rgx = {std::regex(">(?:(?!>).)*?"), std::regex(">(?:(?!>).)*?")}; - std::vector res; - std::smatch match; - while (std::regex_search(s, match, rgx)){ - res.push_back(match[0].str()); - s = match.suffix(); - } - return res; -} +namespace util { void createTree(std::string path){ std::string delimiter = "/"; @@ -50,29 +41,29 @@ void downloadArchive(std::string url, archiveType type){ createTree(DOWNLOAD_PATH); AppletType at; switch(type){ - case sigpatches: - downloadFile(url.c_str(), SIGPATCHES_FILENAME, OFF); + case archiveType::sigpatches: + download::downloadFile(url.c_str(), SIGPATCHES_FILENAME, OFF); break; - case cheats: - downloadFile(url.c_str(), CHEATS_FILENAME, OFF); + case archiveType::cheats: + download::downloadFile(url.c_str(), CHEATS_FILENAME, OFF); break; - case fw: + case archiveType::fw: at = appletGetAppletType(); if (at == AppletType_Application || at == AppletType_SystemApplication) { - downloadFile(url.c_str(), FIRMWARE_FILENAME, OFF); + download::downloadFile(url.c_str(), FIRMWARE_FILENAME, OFF); } else{ brls::Application::crash("menus/utils/fw_warning"_i18n); } break; - case app: - downloadFile(url.c_str(), APP_FILENAME, OFF); + case archiveType::app: + download::downloadFile(url.c_str(), APP_FILENAME, OFF); break; - case cfw: - downloadFile(url.c_str(), CFW_FILENAME, OFF); + case archiveType::cfw: + download::downloadFile(url.c_str(), CFW_FILENAME, OFF); break; - case ams_cfw: - downloadFile(url.c_str(), AMS_FILENAME, OFF); + case archiveType::ams_cfw: + download::downloadFile(url.c_str(), AMS_FILENAME, OFF); } } @@ -124,66 +115,66 @@ void extractArchive(archiveType type, std::string tag){ std::string nroPath ="sdmc:" + std::string(APP_PATH); chdir(ROOT_PATH); switch(type){ - case sigpatches: + case archiveType::sigpatches: if(isArchive(SIGPATCHES_FILENAME)) { /* std::string backup(HEKATE_IPL_PATH); backup += ".old"; */ if(std::filesystem::exists(HEKATE_IPL_PATH)){ overwriteInis = showDialogBox("menus/utils/overwrite"_i18n + std::string(HEKATE_IPL_PATH) +"?", "menus/common/no"_i18n, "menus/common/yes"_i18n); if(overwriteInis == 0){ - extract(SIGPATCHES_FILENAME, ROOT_PATH, HEKATE_IPL_PATH); + extract::extract(SIGPATCHES_FILENAME, ROOT_PATH, HEKATE_IPL_PATH); } else{ - extract(SIGPATCHES_FILENAME); + extract::extract(SIGPATCHES_FILENAME); } } else{ - extract(SIGPATCHES_FILENAME); + extract::extract(SIGPATCHES_FILENAME); } } else{ brls::Application::crash("menus/utils/wrong_type_sigpatches"_i18n); } break; - case cheats: - titles = getInstalledTitlesNs(); - titles = excludeTitles(CHEATS_EXCLUDE, titles); - extractCheats(CHEATS_FILENAME, titles, running_cfw); + case archiveType::cheats: + titles = extract::getInstalledTitlesNs(); + titles = extract::excludeTitles(CHEATS_EXCLUDE, titles); + extract::extractCheats(CHEATS_FILENAME, titles, CurrentCfw::running_cfw); break; - case fw: + case archiveType::fw: if(std::filesystem::file_size(FIRMWARE_FILENAME) < 200000){ brls::Application::crash("menus/utils/wrong_type_sigpatches_downloaded"_i18n); } else{ if (std::filesystem::exists(FIRMWARE_PATH)) std::filesystem::remove_all(FIRMWARE_PATH); createTree(FIRMWARE_PATH); - extract(FIRMWARE_FILENAME, FIRMWARE_PATH); + extract::extract(FIRMWARE_FILENAME, FIRMWARE_PATH); } break; - case app: - extract(APP_FILENAME, CONFIG_PATH); + case archiveType::app: + extract::extract(APP_FILENAME, CONFIG_PATH); cp(ROMFS_FORWARDER, FORWARDER_PATH); envSetNextLoad(FORWARDER_PATH, ("\"" + std::string(FORWARDER_PATH) + "\"").c_str()); romfsExit(); brls::Application::quit(); break; - case cfw: + case archiveType::cfw: if(isArchive(CFW_FILENAME)){ overwriteInis = showDialogBox("menus/utils/overwrite_inis"_i18n, "menus/common/no"_i18n, "menus/common/yes"_i18n); - extract(CFW_FILENAME, ROOT_PATH, overwriteInis); + extract::extract(CFW_FILENAME, ROOT_PATH, overwriteInis); } else{ brls::Application::crash("menus/utils/wrong_type_cfw"_i18n); } break; - case ams_cfw: + case archiveType::ams_cfw: if(isArchive(AMS_FILENAME)){ overwriteInis = showDialogBox("menus/utils/overwrite_inis"_i18n, "menus/common/no"_i18n, "menus/common/yes"_i18n); usleep(800000); int deleteContents = showDialogBox("menus/ams_update/delete_sysmodules_flags"_i18n, "menus/common/no"_i18n, "menus/common/yes"_i18n); if(deleteContents == 1) removeSysmodulesFlags(AMS_CONTENTS); - extract(AMS_FILENAME, ROOT_PATH, overwriteInis); + extract::extract(AMS_FILENAME, ROOT_PATH, overwriteInis); } break; } @@ -191,14 +182,7 @@ void extractArchive(archiveType type, std::string tag){ copyFiles(COPY_FILES_JSON); } -void progressTest(std::string url, archiveType type){ - ProgressEvent::instance().reset(); - ProgressEvent::instance().setTotalSteps(2); - ProgressEvent::instance().setStep(1); - ProgressEvent::instance().setStep(2); -} - -std::string formatListItemTitle(const std::string str, size_t maxScore) { +std::string formatListItemTitle(const std::string &str, size_t maxScore) { size_t score = 0; for (size_t i = 0; i < str.length(); i++) { @@ -260,7 +244,7 @@ void shut_down(bool reboot){ } std::string getLatestTag(const char *url){ - nlohmann::json tag = getRequest(url, {"accept: application/vnd.github.v3+json"}); + nlohmann::json tag = download::getRequest(url, {"accept: application/vnd.github.v3+json"}); if(tag.find("tag_name") != tag.end()) return tag["tag_name"]; else @@ -451,4 +435,13 @@ nlohmann::json parseJsonFile(const char* path) { if(nlohmann::json::accept(fileContent)) return nlohmann::json::parse(fileContent); else return nlohmann::json::object(); +} + +void writeJsonToFile(nlohmann::json &profiles, const char* path) { + std::fstream newProfiles; + newProfiles.open(path, std::fstream::out | std::fstream::trunc); + newProfiles << std::setw(4) << profiles << std::endl; + newProfiles.close(); +} + } \ No newline at end of file diff --git a/source/warning_page.cpp b/source/warning_page.cpp index 3d90acc..39155e4 100644 --- a/source/warning_page.cpp +++ b/source/warning_page.cpp @@ -9,7 +9,7 @@ namespace i18n = brls::i18n; using namespace i18n::literals; WarningPage::WarningPage(std::string text) { - createTree(CONFIG_PATH); + util::createTree(CONFIG_PATH); std::ofstream(HIDDEN_AIO_FILE); this->button = (new brls::Button(brls::ButtonStyle::PRIMARY))->setLabel("menus/common/continue"_i18n); this->button->setParent(this);