Add option to automatically check for updates

This commit is contained in:
Huntereb 2019-12-18 20:33:27 -05:00
parent 3608a661c8
commit 4d686e0c2a
8 changed files with 64 additions and 33 deletions

View file

@ -17,7 +17,8 @@ namespace inst::ui {
void onInput(u64 Down, u64 Up, u64 Held, pu::ui::Touch Pos);
Image::Ref awooImage;
private:
bool appletThreadFinished;
bool appletFinished;
bool updateFinished;
TextBlock::Ref butText;
Rectangle::Ref topRect;
Rectangle::Ref botRect;

View file

@ -9,6 +9,7 @@ namespace inst::ui {
optionsPage();
PU_SMART_CTOR(optionsPage)
void onInput(u64 Down, u64 Up, u64 Held, pu::ui::Touch Pos);
static void checkForUpdate(std::vector<std::string> updateInfo);
private:
TextBlock::Ref butText;
Rectangle::Ref topRect;
@ -20,6 +21,5 @@ namespace inst::ui {
pu::ui::elm::Menu::Ref menu;
void setMenuText();
std::string getMenuOptionIcon(bool ourBool);
void checkForUpdate();
};
}

View file

@ -1,5 +1,7 @@
#pragma once
#include <vector>
namespace inst::config {
static const std::string appDir = "sdmc:/switch/Awoo-Installer";
static const std::string configPath = appDir + "/config.ini";
@ -7,10 +9,12 @@ namespace inst::config {
extern std::string sigPatchesUrl;
extern std::string gAuthKey;
extern std::vector<std::string> updateInfo;
extern bool ignoreReqVers;
extern bool validateNCAs;
extern bool overClock;
extern bool deletePrompt;
extern bool autoUpdate;
extern bool gayMode;
extern bool usbAck;

View file

@ -1,7 +1,9 @@
#include <thread>
#include "switch.h"
#include "util/error.hpp"
#include "ui/MainApplication.hpp"
#include "util/util.hpp"
#include "util/config.hpp"
using namespace pu::ui::render;
int main(int argc, char* argv[])
@ -11,8 +13,11 @@ int main(int argc, char* argv[])
auto renderer = Renderer::New(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMECONTROLLER,
RendererInitOptions::RendererNoSound, RendererHardwareFlags);
auto main = inst::ui::MainApplication::New(renderer);
std::thread updateThread;
if (inst::config::autoUpdate && inst::util::getIPAddress() != "1.0.0.127") updateThread = std::thread(inst::util::checkForAppUpdate);
main->Prepare();
main->ShowWithFadeIn();
updateThread.join();
} catch (std::exception& e) {
LOG_DEBUG("An error occurred:\n%s", e.what());
}

View file

@ -11,18 +11,24 @@
namespace inst::ui {
extern MainApplication *mainApp;
bool appletThreadFinished = false;
bool appletFinished = false;
bool updateFinished = false;
void warnAboutAppletMode() {
void mainMenuThread() {
bool menuLoaded = mainApp->IsShown();
if (!appletThreadFinished && menuLoaded && appletGetAppletType() == AppletType_LibraryApplet) {
inst::ui::appletThreadFinished = true;
if (!appletFinished && menuLoaded && appletGetAppletType() == AppletType_LibraryApplet) {
inst::ui::appletFinished = true;
tin::data::NUM_BUFFER_SEGMENTS = 2;
mainApp->CreateShowDialog("Applet Mode not supported", "You may experience issues using Awoo Installer in Applet Mode. If you do\nhave problems, please switch to running Awoo Installer over an installed\ntitle or forwarder!", {"OK"}, true);
} else if (!appletThreadFinished && menuLoaded) {
inst::ui::appletThreadFinished = true;
} else if (!appletFinished && menuLoaded) {
inst::ui::appletFinished = true;
tin::data::NUM_BUFFER_SEGMENTS = 4;
}
if (!updateFinished && !inst::config::autoUpdate) updateFinished = true;
if (!updateFinished && menuLoaded && inst::config::updateInfo.size()) {
updateFinished = true;
optionsPage::checkForUpdate(inst::config::updateInfo);
}
}
MainPage::MainPage() : Layout::Layout() {
@ -76,7 +82,7 @@ namespace inst::ui {
this->Add(this->eggImage);
this->awooImage->SetVisible(!inst::config::gayMode);
this->eggImage->SetVisible(false);
this->AddThread(warnAboutAppletMode);
this->AddThread(mainMenuThread);
}
void MainPage::installMenuItem_Click() {

View file

@ -42,27 +42,17 @@ namespace inst::ui {
this->Add(this->menu);
}
void optionsPage::checkForUpdate() {
std::vector<std::string> downloadUrl = inst::util::checkForAppUpdate();
if (inst::util::getIPAddress() == "1.0.0.127") {
inst::ui::mainApp->CreateShowDialog("Network connection not available", "Check that airplane mode is disabled and you're connected to a local network.", {"OK"}, true);
return;
}
if (!downloadUrl.size()) {
mainApp->CreateShowDialog("No updates found", "You are on the latest version of Awoo Installer!", {"OK"}, false);
return;
}
else {
if (!mainApp->CreateShowDialog("Update available", "Awoo Installer " + downloadUrl[0] + " is available now! Ready to update?", {"Update", "Cancel"}, false)) {
void optionsPage::checkForUpdate(std::vector<std::string> updateInfo) {
if (!mainApp->CreateShowDialog("Update available", "Awoo Installer " + updateInfo[0] + " is available now! Ready to update?", {"Update", "Cancel"}, false)) {
inst::ui::loadInstallScreen();
inst::ui::setTopInstInfoText("Updating to Awoo Installer " + downloadUrl[0]);
inst::ui::setTopInstInfoText("Updating to Awoo Installer " + updateInfo[0]);
inst::ui::setInstBarPerc(0);
inst::ui::setInstInfoText("Downloading Awoo Installer " + downloadUrl[0]);
inst::ui::setInstInfoText("Downloading Awoo Installer " + updateInfo[0]);
try {
romfsExit();
std::string curName = inst::config::appDir + "/Awoo-Installer.nro";
std::string downloadName = inst::config::appDir + "/temp_download";
inst::curl::downloadFile(downloadUrl[1], downloadName.c_str(), 0, true);
inst::curl::downloadFile(updateInfo[1], downloadName.c_str(), 0, true);
if (std::filesystem::exists(curName)) std::filesystem::remove(curName);
std::filesystem::rename(downloadName, curName);
mainApp->CreateShowDialog("Update complete!", "The software will now be closed.", {"OK"}, false);
@ -72,7 +62,6 @@ namespace inst::ui {
mainApp->FadeOut();
mainApp->Close();
}
}
return;
}
@ -99,6 +88,10 @@ namespace inst::ui {
deletePromptOption->SetColor(COLOR("#FFFFFFFF"));
deletePromptOption->SetIcon(this->getMenuOptionIcon(inst::config::deletePrompt));
this->menu->AddItem(deletePromptOption);
auto autoUpdateOption = pu::ui::elm::MenuItem::New("Check for updates automatically");
autoUpdateOption->SetColor(COLOR("#FFFFFFFF"));
autoUpdateOption->SetIcon(this->getMenuOptionIcon(inst::config::autoUpdate));
this->menu->AddItem(autoUpdateOption);
auto gayModeOption = pu::ui::elm::MenuItem::New("Remove anime");
gayModeOption->SetColor(COLOR("#FFFFFFFF"));
gayModeOption->SetIcon(this->getMenuOptionIcon(inst::config::gayMode));
@ -120,6 +113,7 @@ namespace inst::ui {
}
if ((Down & KEY_A) || (Up & KEY_TOUCH)) {
std::string keyboardResult;
std::vector<std::string> downloadUrl;
switch (this->menu->GetSelectedIndex()) {
case 0:
inst::config::ignoreReqVers = !inst::config::ignoreReqVers;
@ -144,6 +138,11 @@ namespace inst::ui {
this->setMenuText();
break;
case 4:
inst::config::autoUpdate = !inst::config::autoUpdate;
inst::config::setConfig();
this->setMenuText();
break;
case 5:
if (inst::config::gayMode) {
inst::config::gayMode = false;
mainApp->mainPage->awooImage->SetVisible(true);
@ -157,7 +156,7 @@ namespace inst::ui {
inst::config::setConfig();
this->setMenuText();
break;
case 5:
case 6:
keyboardResult = inst::util::softwareKeyboard("Enter the URL to obtain Signature Patches from", inst::config::sigPatchesUrl.c_str(), 500);
if (keyboardResult.size() > 0) {
inst::config::sigPatchesUrl = keyboardResult;
@ -165,10 +164,19 @@ namespace inst::ui {
this->setMenuText();
}
break;
case 6:
this->checkForUpdate();
break;
case 7:
if (inst::util::getIPAddress() == "1.0.0.127") {
inst::ui::mainApp->CreateShowDialog("Network connection not available", "Check that airplane mode is disabled and you're connected to a local network.", {"OK"}, true);
break;
}
downloadUrl = inst::util::checkForAppUpdate();
if (!downloadUrl.size()) {
mainApp->CreateShowDialog("No updates found", "You are on the latest version of Awoo Installer!", {"OK"}, false);
break;
}
this->checkForUpdate(downloadUrl);
break;
case 8:
inst::ui::mainApp->CreateShowDialog("Thanks to the following people!", "- HookedBehemoth for A LOT of contributions\n- Adubbz and other contributors for Tinfoil\n- XorTroll for Plutonium and Goldleaf\n- blawar (wife beater) and nicoboss for NSZ support\n- The kind folks at the AtlasNX Discuck (or at least some of them)\n- The also kind folks at the RetroNX Discuck (of no direct involvement)\n- namako8982 for the Momiji art\n- TheXzoron for being a baka", {"Close"}, true);
break;
default:

View file

@ -5,13 +5,15 @@
#include "util/config.hpp"
namespace inst::config {
static const char* configBase = "[settings]\nignoreReqVers=%d\nvalidateNCAs=%d\noverClock=%d\ndeletePrompt=%d\ngayMode=%d\nsigPatchesUrl=%s\ngAuthKey=%s\nusbAck=%d";
static const char* configBase = "[settings]\nignoreReqVers=%d\nvalidateNCAs=%d\noverClock=%d\ndeletePrompt=%d\nautoUpdate=%d\ngayMode=%d\nsigPatchesUrl=%s\ngAuthKey=%s\nusbAck=%d";
std::string sigPatchesUrl;
std::string gAuthKey;
std::vector<std::string> updateInfo;
bool ignoreReqVers;
bool validateNCAs;
bool overClock;
bool deletePrompt;
bool autoUpdate;
bool gayMode;
bool usbAck;
@ -21,6 +23,7 @@ namespace inst::config {
validateNCAs = reader.GetBoolean("settings", "validateNCAs", true);
overClock = reader.GetBoolean("settings", "overClock", false);
deletePrompt = reader.GetBoolean("settings", "deletePrompt", true);
autoUpdate = reader.GetBoolean("settings", "autoUpdate", true);
gayMode = reader.GetBoolean("settings", "gayMode", false);
sigPatchesUrl = reader.GetString("settings", "sigPatchesUrl", "https://github.com/Huntereb/Awoo-Installer/releases/download/SignaturePatches/patches.zip");
gAuthKey = reader.GetString("settings", "gAuthKey", {0x41,0x49,0x7a,0x61,0x53,0x79,0x42,0x4d,0x71,0x76,0x34,0x64,0x58,0x6e,0x54,0x4a,0x4f,0x47,0x51,0x74,0x5a,0x5a,0x53,0x33,0x43,0x42,0x6a,0x76,0x66,0x37,0x34,0x38,0x51,0x76,0x78,0x53,0x7a,0x46,0x30});
@ -30,8 +33,8 @@ namespace inst::config {
void setConfig() {
std::filesystem::remove(inst::config::configPath);
char data[106 + sigPatchesUrl.size() + gAuthKey.size()];
sprintf(data, configBase, ignoreReqVers, validateNCAs, overClock, deletePrompt, gayMode, sigPatchesUrl.c_str(), gAuthKey.c_str(), usbAck);
char data[119 + sigPatchesUrl.size() + gAuthKey.size()];
sprintf(data, configBase, ignoreReqVers, validateNCAs, overClock, deletePrompt, autoUpdate, gayMode, sigPatchesUrl.c_str(), gAuthKey.c_str(), usbAck);
FILE * configFile = fopen(inst::config::configPath.c_str(), "w");
fwrite(data, sizeof(char), strlen(data), configFile);
fflush(configFile);

View file

@ -302,7 +302,11 @@ namespace inst::util {
std::string jsonData = inst::curl::downloadToBuffer("https://api.github.com/repos/Huntereb/Awoo-Installer/releases/latest", 0, 0, 1000L);
if (jsonData.size() == 0) return {};
nlohmann::json ourJson = nlohmann::json::parse(jsonData);
if (ourJson["tag_name"].get<std::string>() != inst::config::appVersion) return {ourJson["tag_name"].get<std::string>(), ourJson["assets"][0]["browser_download_url"].get<std::string>()};
if (ourJson["tag_name"].get<std::string>() != inst::config::appVersion) {
std::vector<std::string> ourUpdateInfo = {ourJson["tag_name"].get<std::string>(), ourJson["assets"][0]["browser_download_url"].get<std::string>()};
inst::config::updateInfo = ourUpdateInfo;
return ourUpdateInfo;
}
} catch (...) {}
return {};
}