Checkpoint/switch/source/util.cpp

189 lines
6.3 KiB
C++
Raw Normal View History

2018-05-09 11:25:01 +02:00
/*
2019-05-06 22:51:09 +02:00
* This file is part of Checkpoint
* Copyright (C) 2017-2019 Bernardo Giordano, FlagBrew
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
2018-05-09 11:25:01 +02:00
#include "util.hpp"
void servicesExit(void)
{
2019-07-24 21:02:27 +02:00
socketExit();
2019-06-16 13:50:34 +02:00
SDLH_Exit();
2018-05-09 11:25:01 +02:00
}
Result servicesInit(void)
{
2019-07-24 19:35:27 +02:00
Logger::getInstance().log(Logger::INFO, "Starting Checkpoint loading...");
2019-07-15 23:36:00 +02:00
int appletType = (int)appletGetAppletType();
if (appletType != AppletType_Application) {
Logger::getInstance().log(Logger::ERROR, "Please run Checkpoint under Atmosphére title takeover. AppletType is %d.", appletType);
return -1;
}
2019-07-14 21:46:09 +02:00
2019-07-04 23:09:46 +02:00
Result socinit = 0;
if ((socinit = socketInitializeDefault()) == 0) {
2019-05-06 22:51:09 +02:00
// nxlinkStdio();
2018-12-16 21:00:15 +01:00
}
2019-07-15 23:36:00 +02:00
else {
Logger::getInstance().log(Logger::INFO, "Unable to initialize socket. Result code 0x%08lX.", socinit);
2019-07-15 23:36:00 +02:00
}
2019-07-05 21:08:15 +02:00
g_shouldExitNetworkLoop = R_FAILED(socinit);
2018-05-09 11:25:01 +02:00
Result res = 0;
2019-07-24 21:02:27 +02:00
2018-07-14 17:05:30 +02:00
romfsInit();
2019-07-24 21:02:27 +02:00
ATEXIT(romfsExit);
2019-05-01 12:23:03 +02:00
io::createDirectory("sdmc:/switch");
io::createDirectory("sdmc:/switch/Checkpoint");
io::createDirectory("sdmc:/switch/Checkpoint/saves");
io::createDirectory("sdmc:/switch/Checkpoint/cheats");
2018-05-09 11:25:01 +02:00
2019-05-06 22:51:09 +02:00
if (R_FAILED(res = plInitialize())) {
Logger::getInstance().log(Logger::ERROR, "plInitialize failed. Result code 0x%08lX.", res);
2018-05-15 21:53:51 +02:00
return res;
}
2019-07-24 21:02:27 +02:00
ATEXIT(plExit);
2018-05-15 21:53:51 +02:00
2019-05-06 22:51:09 +02:00
if (R_FAILED(res = Account::init())) {
Logger::getInstance().log(Logger::ERROR, "Account::init failed. Result code 0x%08lX.", res);
2018-05-09 11:25:01 +02:00
return res;
}
2019-07-24 21:02:27 +02:00
ATEXIT(Account::exit);
2018-05-09 11:25:01 +02:00
2019-05-06 22:51:09 +02:00
if (R_FAILED(res = nsInitialize())) {
Logger::getInstance().log(Logger::ERROR, "nsInitialize failed. Result code 0x%08lX.", res);
2018-05-09 11:25:01 +02:00
return res;
2019-05-06 22:51:09 +02:00
}
2019-07-24 21:02:27 +02:00
ATEXIT(nsExit);
2019-05-06 22:51:09 +02:00
2019-06-16 13:50:34 +02:00
if (!SDLH_Init()) {
Logger::getInstance().log(Logger::ERROR, "SDLH_Init failed. Result code 0x%08lX.", res);
return -1;
}
2019-07-24 21:02:27 +02:00
ATEXIT(freeIcons);
2018-05-14 15:26:25 +02:00
2019-05-06 22:51:09 +02:00
if (R_SUCCEEDED(res = hidsysInitialize())) {
2019-05-04 14:35:16 +02:00
g_notificationLedAvailable = true;
2019-07-24 21:02:27 +02:00
ATEXIT(hidsysExit);
2019-05-06 22:51:09 +02:00
}
2019-07-15 23:36:00 +02:00
else {
Logger::getInstance().log(Logger::INFO, "Notification led not available. Result code 0x%08lX.", res);
2019-07-15 23:36:00 +02:00
}
2019-05-04 14:35:16 +02:00
2018-09-16 16:34:12 +02:00
Configuration::getInstance();
2019-07-24 21:02:27 +02:00
if (R_SUCCEEDED(socinit)) {
2019-07-04 09:44:29 +02:00
if (R_SUCCEEDED(res = ftp_init())) {
2019-07-24 21:02:27 +02:00
ATEXIT(ftp_exit);
2019-07-04 09:44:29 +02:00
g_ftpAvailable = true;
2019-07-24 19:35:27 +02:00
Logger::getInstance().log(Logger::INFO, "FTP Server successfully loaded.");
2019-07-15 23:36:00 +02:00
}
else {
Logger::getInstance().log(Logger::INFO, "FTP Server failed to load. Result code 0x%08lX.", res);
2019-07-04 09:44:29 +02:00
}
}
2019-07-26 22:49:36 +02:00
if (R_SUCCEEDED(res = pdmqryInitialize())) {
ATEXIT(pdmqryExit);
} else {
Logger::getInstance().log(Logger::WARN, "pdmqryInitialize failed with result 0x%08lX.", res);
}
2019-07-24 19:35:27 +02:00
Logger::getInstance().log(Logger::INFO, "Checkpoint loading completed!");
return 0;
}
2018-05-14 15:26:25 +02:00
std::u16string StringUtils::UTF8toUTF16(const char* src)
{
char16_t tmp[256] = {0};
2019-05-06 22:51:09 +02:00
utf8_to_utf16((uint16_t*)tmp, (uint8_t*)src, 256);
return std::u16string(tmp);
}
2018-05-14 15:26:25 +02:00
// https://stackoverflow.com/questions/14094621/change-all-accented-letters-to-normal-letters-in-c
std::string StringUtils::removeAccents(std::string str)
{
std::u16string src = UTF8toUTF16(str.c_str());
const std::u16string illegal = UTF8toUTF16("ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ");
2019-05-06 22:51:09 +02:00
const std::u16string fixed = UTF8toUTF16("AAAAAAECEEEEIIIIDNOOOOOx0UUUUYPsaaaaaaeceeeeiiiiOnooooo/0uuuuypy");
for (size_t i = 0, sz = src.length(); i < sz; i++) {
size_t index = illegal.find(src[i]);
2019-05-06 22:51:09 +02:00
if (index != std::string::npos) {
src[i] = fixed[index];
}
}
return UTF16toUTF8(src);
}
std::string StringUtils::removeNotAscii(std::string str)
{
2019-05-06 22:51:09 +02:00
for (size_t i = 0, sz = str.length(); i < sz; i++) {
if (!isascii(str[i])) {
str[i] = ' ';
}
2018-07-16 15:59:45 +02:00
}
2019-05-06 22:51:09 +02:00
return str;
2019-05-04 14:35:16 +02:00
}
HidsysNotificationLedPattern blinkLedPattern(u8 times)
{
HidsysNotificationLedPattern pattern;
memset(&pattern, 0, sizeof(pattern));
2019-05-06 22:51:09 +02:00
pattern.baseMiniCycleDuration = 0x1; // 12.5ms.
pattern.totalMiniCycles = 0x2; // 2 mini cycles.
pattern.totalFullCycles = times; // Repeat n times.
pattern.startIntensity = 0x0; // 0%.
2019-05-04 14:35:16 +02:00
2019-05-06 22:51:09 +02:00
pattern.miniCycles[0].ledIntensity = 0xF; // 100%.
pattern.miniCycles[0].transitionSteps = 0xF; // 15 steps. Total 187.5ms.
pattern.miniCycles[0].finalStepDuration = 0x0; // Forced 12.5ms.
pattern.miniCycles[1].ledIntensity = 0x0; // 0%.
pattern.miniCycles[1].transitionSteps = 0xF; // 15 steps. Total 187.5ms.
pattern.miniCycles[1].finalStepDuration = 0x0; // Forced 12.5ms.
2019-05-04 14:35:16 +02:00
return pattern;
}
void blinkLed(u8 times)
{
2019-05-06 22:51:09 +02:00
if (g_notificationLedAvailable) {
2019-05-04 14:35:16 +02:00
size_t n;
u64 uniquePadIds[2];
HidsysNotificationLedPattern pattern = blinkLedPattern(times);
memset(uniquePadIds, 0, sizeof(uniquePadIds));
Result res = hidsysGetUniquePadsFromNpad(hidGetHandheldMode() ? CONTROLLER_HANDHELD : CONTROLLER_PLAYER_1, uniquePadIds, 2, &n);
2019-05-06 22:51:09 +02:00
if (R_SUCCEEDED(res)) {
for (size_t i = 0; i < n; i++) {
2019-05-04 14:35:16 +02:00
hidsysSetNotificationLedPattern(&pattern, uniquePadIds[i]);
}
}
}
2018-05-09 11:25:01 +02:00
}