Skip to content

Commit

Permalink
properly sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
HamletDuFromage committed May 1, 2022
1 parent 74a9d89 commit 1b008ba
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ DATA := data
INCLUDES := include lib/zipper/include /lib/borealis/library/include/borealis/extern/nlohmann
APP_TITLE := All-in-One Switch Updater
APP_AUTHOR := HamletDuFromage
APP_VERSION := 2.18.0
APP_VERSION := 2.18.1
TARGET := $(notdir $(CURDIR))

ROMFS := resources
Expand Down
3 changes: 3 additions & 0 deletions source/changelog_page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ ChangelogPage::ChangelogPage() : AppletFrame(true, true)
verTitles.push_back("v2.18.0");
changes.push_back("\uE016 Add mega.nz support (https://github.com/aedalzotto).\n\uE016 Add Korean localization (https://github.com/DDinghoya).\n\uE016 Improve Spanish localization (https://github.com/Armi-Heavy).");

verTitles.push_back("v2.18.1");
changes.push_back("\uE016 Fix some pop-up related bugs.");


for (int i = verTitles.size() - 1; i >= 0; i--) {
listItem = new brls::ListItem(verTitles[i]);
Expand Down
79 changes: 36 additions & 43 deletions source/download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

#include <curl/curl.h>
#include <math.h>
#include <mbedtls/base64.h>
#include <switch.h>
#include <time.h>
#include <mbedtls/base64.h>

#include <algorithm>
#include <chrono>
#include <regex>
#include <string>
#include <thread>

#include "fs.hpp"
#include "progress_event.hpp"
Expand Down Expand Up @@ -58,11 +59,11 @@ namespace download {
data_struct->offset = 0;
}

if(data_struct->aes)
if (data_struct->aes)
aes128CtrCrypt(data_struct->aes, &data_struct->data[data_struct->offset], contents, realsize);
else
memcpy(&data_struct->data[data_struct->offset], contents, realsize);

data_struct->offset += realsize;
data_struct->data[data_struct->offset] = 0;
return realsize;
Expand Down Expand Up @@ -113,7 +114,7 @@ namespace download {
}

bool checkSize(CURL* curl, const std::string& url)
{
{
curl_off_t dl;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, API_AGENT);
Expand All @@ -136,7 +137,7 @@ namespace download {
{
auto len = url.length();

if(len < 52)
if (len < 52)
throw std::invalid_argument("Invalid URL.");

bool old_link = url.find("#!") < len;
Expand All @@ -150,7 +151,7 @@ namespace download {
/* Finally crop the url */
std::string id = url.substr(init_pos, end_pos - init_pos);

if(id.length() != 8)
if (id.length() != 8)
throw std::invalid_argument("Invalid URL ID.");

return id;
Expand All @@ -160,13 +161,11 @@ namespace download {
{
std::string id = mega_id(url);

json request = json::array({
{
{"a", "g"},
{"g", 1},
{"p", id},
}
});
json request = json::array({{
{"a", "g"},
{"g", 1},
{"p", id},
}});

std::string body = request.dump();
std::string output;
Expand All @@ -181,16 +180,14 @@ namespace download {
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(
curl,
CURLOPT_WRITEFUNCTION,
+[](void *buffer, size_t size, size_t nmemb, void *userp) -> size_t
{
curl,
CURLOPT_WRITEFUNCTION,
+[](void* buffer, size_t size, size_t nmemb, void* userp) -> size_t {
std::string* output = reinterpret_cast<std::string*>(userp);
size_t actual_size = size * nmemb;
output->append(reinterpret_cast<char*>(buffer), actual_size);
return actual_size;
}
);
});

curl_easy_perform(curl);

Expand All @@ -200,9 +197,9 @@ namespace download {

s64 freeStorage;
s64 fileSize = response[0]["s"];
if(R_SUCCEEDED(fs::getFreeStorageSD(freeStorage)) && fileSize * 1.1 > freeStorage)
if (R_SUCCEEDED(fs::getFreeStorageSD(freeStorage)) && fileSize * 1.1 > freeStorage)
return "";

return response[0]["g"];
}

Expand All @@ -211,7 +208,7 @@ namespace download {
/* Check if old link format */
auto len = url.length();

if(len < 52)
if (len < 52)
throw std::invalid_argument("Invalid URL.");

bool old_link = url.find("#!") < len;
Expand All @@ -228,23 +225,22 @@ namespace download {

/* Add padding */
auto key_len = key.length();
unsigned pad = 4 - key_len%4;
unsigned pad = 4 - key_len % 4;
key.append(pad, '=');

/* The encoded key should have 44 characters to produce a 32 byte node key */
if(key.length() != 44)
if (key.length() != 44)
throw std::invalid_argument("Invalid URL key.");

std::string decoded(key.size()*3/4, 0);
std::string decoded(key.size() * 3 / 4, 0);
size_t olen = 0;

mbedtls_base64_decode(
reinterpret_cast<unsigned char*>(decoded.data()),
decoded.size(),
&olen,
reinterpret_cast<const unsigned char*>(key.c_str()),
key.size()
);
key.size());

/**
* The encoded base64 is (usually?) 43 characters long. With padding it goes
Expand All @@ -253,34 +249,31 @@ namespace download {
* valid character, it should produce a 32 byte node key.
*/
decoded.resize(olen);
if(decoded.size() != 32)
if (decoded.size() != 32)
throw std::invalid_argument("Invalid node key.");

return decoded;
}

std::string mega_key(const std::string &node_key)
std::string mega_key(const std::string& node_key)
{
std::string key(16, 0);

reinterpret_cast<uint64_t*>(key.data())[0] =
reinterpret_cast<const uint64_t*>(node_key.data())[0] ^
reinterpret_cast<const uint64_t*>(node_key.data())[2]
;
reinterpret_cast<uint64_t*>(key.data())[1] =
reinterpret_cast<const uint64_t*>(node_key.data())[1] ^
reinterpret_cast<const uint64_t*>(node_key.data())[3]
;
reinterpret_cast<uint64_t*>(key.data())[0] =
reinterpret_cast<const uint64_t*>(node_key.data())[0] ^
reinterpret_cast<const uint64_t*>(node_key.data())[2];
reinterpret_cast<uint64_t*>(key.data())[1] =
reinterpret_cast<const uint64_t*>(node_key.data())[1] ^
reinterpret_cast<const uint64_t*>(node_key.data())[3];

return key;
}

std::string mega_iv(const std::string &node_key)
std::string mega_iv(const std::string& node_key)
{
std::string iv(16, 0);
reinterpret_cast<uint64_t*>(iv.data())[0] =
reinterpret_cast<const uint64_t*>(node_key.data())[2]
;
reinterpret_cast<uint64_t*>(iv.data())[0] =
reinterpret_cast<const uint64_t*>(node_key.data())[2];

return iv;
}
Expand Down Expand Up @@ -341,7 +334,7 @@ namespace download {
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status_code);

if (fp && chunk.offset && can_download)
if (fp && chunk.offset && can_download)
fwrite(chunk.data, 1, chunk.offset, fp);

curl_easy_cleanup(curl);
Expand All @@ -353,7 +346,7 @@ namespace download {
fclose(chunk.out);
if (!can_download) {
brls::Application::crash("menus/errors/insufficient_storage"_i18n);
usleep(2000000);
std::this_thread::sleep_for(std::chrono::microseconds(2000000));
brls::Application::quit();
res = {};
}
Expand Down
3 changes: 2 additions & 1 deletion source/extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <set>
#include <sstream>
#include <string>
#include <thread>
#include <vector>

#include "current_cfw.hpp"
Expand Down Expand Up @@ -44,7 +45,7 @@ namespace extract {
if (uncompressedSize * 1.1 > freeStorage) {
unzipper.close();
brls::Application::crash("menus/errors/insufficient_storage"_i18n);
usleep(2000000);
std::this_thread::sleep_for(std::chrono::microseconds(2000000));
brls::Application::quit();
}
}
Expand Down
3 changes: 2 additions & 1 deletion source/net_page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <thread>
#include <json.hpp>

#include "constants.hpp"
Expand Down Expand Up @@ -166,7 +167,7 @@ NetPage::NetPage() : AppletFrame(true, true)
nifmSetNetworkProfile(&profile, &profile.uuid);
nifmSetWirelessCommunicationEnabled(true);
nifmExit();
usleep(2500000); //Wait to avoid crashes when leaving too fast
std::this_thread::sleep_for(std::chrono::microseconds(2500000)); //Wait to avoid crashes when leaving too fast
dialog->close();
};
brls::GenericEvent::Callback callbackNo = [dialog](brls::View* view) {
Expand Down
25 changes: 12 additions & 13 deletions source/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

#include <switch.h>

#include <chrono>
#include <filesystem>
#include <fstream>
#include <thread>

#include "current_cfw.hpp"
#include "download.hpp"
Expand Down Expand Up @@ -81,44 +83,42 @@ namespace util {

int showDialogBoxBlocking(const std::string& text, const std::string& opt)
{
int dialogResult = -1;
int result = -1;
brls::Dialog* dialog = new brls::Dialog(text);
brls::GenericEvent::Callback callback = [dialog, &dialogResult](brls::View* view) {
dialogResult = 0;
brls::GenericEvent::Callback callback = [dialog, &result](brls::View* view) {
result = 0;
dialog->close();
};
dialog->addButton(opt, callback);
dialog->setCancelable(false);
dialog->open();
while (result == -1) {
usleep(1);
result = dialogResult;
std::this_thread::sleep_for(std::chrono::microseconds(10));
}
std::this_thread::sleep_for(std::chrono::microseconds(800000));
return result;
}

int showDialogBoxBlocking(const std::string& text, const std::string& opt1, const std::string& opt2)
{
int dialogResult = -1;
int result = -1;
brls::Dialog* dialog = new brls::Dialog(text);
brls::GenericEvent::Callback callback1 = [dialog, &dialogResult](brls::View* view) {
dialogResult = 0;
brls::GenericEvent::Callback callback1 = [dialog, &result](brls::View* view) {
result = 0;
dialog->close();
};
brls::GenericEvent::Callback callback2 = [dialog, &dialogResult](brls::View* view) {
dialogResult = 1;
brls::GenericEvent::Callback callback2 = [dialog, &result](brls::View* view) {
result = 1;
dialog->close();
};
dialog->addButton(opt1, callback1);
dialog->addButton(opt2, callback2);
dialog->setCancelable(false);
dialog->open();
while (result == -1) {
usleep(1);
result = dialogResult;
std::this_thread::sleep_for(std::chrono::microseconds(10));
}
std::this_thread::sleep_for(std::chrono::microseconds(800000));
return result;
}

Expand Down Expand Up @@ -185,7 +185,6 @@ namespace util {
}
case contentType::ams_cfw: {
int overwriteInis = showDialogBoxBlocking("menus/utils/overwrite_inis"_i18n, "menus/common/no"_i18n, "menus/common/yes"_i18n);
usleep(1000000);
int deleteContents = showDialogBoxBlocking("menus/ams_update/delete_sysmodules_flags"_i18n, "menus/common/no"_i18n, "menus/common/yes"_i18n);
if (deleteContents == 1)
removeSysmodulesFlags(AMS_CONTENTS);
Expand Down

0 comments on commit 1b008ba

Please sign in to comment.