-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added updater code and improved zip extraction
- Loading branch information
1 parent
641bd7e
commit 19fcec6
Showing
9 changed files
with
304 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
[submodule "SimpleIniParser"] | ||
path = SimpleIniParser | ||
url = https://github.com/AtlasNX/SimpleIniParser | ||
url = https://github.com/AtlasNX/SimpleIniParser | ||
[submodule "json"] | ||
path = json | ||
url = https://github.com/nlohmann/json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include <switch.h> | ||
#include <curl/curl.h> | ||
#include <string> | ||
|
||
std::size_t CurlStrWrite(const char* in, std::size_t size, std::size_t num, std::string* out); | ||
std::size_t CurlFileWrite(const char* in, std::size_t size, std::size_t num, FILE* out); | ||
std::string RetrieveContent(std::string URL, std::string MIMEType); | ||
void RetrieveToFile(std::string, std::string); | ||
std::string FormatURL(std::string TextToFormat); | ||
bool HasConnection(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <Utils.h> | ||
#include <nlohmann/json.hpp> | ||
using json = nlohmann::json; | ||
|
||
class UpdaterUI : public UIWindow | ||
{ | ||
private: | ||
//vars | ||
int State = 0; | ||
bool NewVersion = false; | ||
std::string LatestID; | ||
json GitAPIData; | ||
//functions | ||
bool CheckForNewVersion(); | ||
public: | ||
//vars | ||
std::string NroPath = "sdmc:/switch/N-Xplorer.nro"; | ||
MenuUI *Menu; | ||
//functions | ||
void DrawUI(); | ||
void GetInput(); | ||
UpdaterUI(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
//Ripped straight from Amiigo | ||
#include <switch.h> | ||
#include <curl/curl.h> | ||
#include <string> | ||
#include <iostream> | ||
#include <vector> | ||
#include <fstream> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <chrono> | ||
#include <thread> | ||
#include "Utils.h" | ||
|
||
extern int destroyer; | ||
//Stolen from Goldleaf | ||
//Thank you XOR | ||
std::size_t CurlStrWrite(const char* in, std::size_t size, std::size_t num, std::string* out) | ||
{ | ||
const size_t totalBytes(size * num); | ||
out->append(in, totalBytes); | ||
return totalBytes; | ||
} | ||
|
||
std::size_t CurlFileWrite(const char* in, std::size_t size, std::size_t num, FILE* out) | ||
{ | ||
fwrite(in, size, num, out); | ||
return (size * num); | ||
} | ||
|
||
std::string RetrieveContent(std::string URL, std::string MIMEType) | ||
{ | ||
std::string cnt; | ||
CURL *curl = curl_easy_init(); | ||
if(!MIMEType.empty()) | ||
{ | ||
curl_slist *headerdata = NULL; | ||
headerdata = curl_slist_append(headerdata, ("Content-Type: " + MIMEType).c_str()); | ||
headerdata = curl_slist_append(headerdata, ("Accept: " + MIMEType).c_str()); | ||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerdata); | ||
} | ||
curl_easy_setopt(curl, CURLOPT_URL, URL.c_str()); | ||
curl_easy_setopt(curl, CURLOPT_USERAGENT, "N-Xplorer"); | ||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); | ||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); | ||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); | ||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlStrWrite); | ||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &cnt); | ||
curl_easy_perform(curl); | ||
curl_easy_cleanup(curl); | ||
return cnt; | ||
} | ||
|
||
void RetrieveToFile(std::string URL, std::string Path) | ||
{ | ||
FILE *f = fopen(Path.c_str(), "wb"); | ||
if(f) | ||
{ | ||
CURL *curl = curl_easy_init(); | ||
curl_easy_setopt(curl, CURLOPT_URL, URL.c_str()); | ||
curl_easy_setopt(curl, CURLOPT_USERAGENT, "N-Xplorer"); | ||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); | ||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); | ||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); | ||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFileWrite); | ||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, f); | ||
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); | ||
curl_easy_perform(curl); | ||
curl_easy_cleanup(curl); | ||
} | ||
fclose(f); | ||
} | ||
|
||
//I made this so even though it's only one two calls it's probably janky. | ||
std::string FormatURL(std::string TextToFormat) | ||
{ | ||
CURL *curl = curl_easy_init(); | ||
return curl_easy_escape(curl, TextToFormat.c_str(), 0); | ||
} | ||
|
||
//More stuff from Xortroll Industries | ||
bool HasConnection() | ||
{ | ||
u32 strg = 0; | ||
nifmInitialize(NifmServiceType_User); | ||
nifmGetInternetConnectionStatus(NULL, &strg, NULL); | ||
return (strg > 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
//Based on Amiigo's updater | ||
#include <SDL.h> | ||
#include <Networking.h> | ||
#include <Utils.h> | ||
#include <ExplorerUI.h> | ||
#include <nlohmann/json.hpp> | ||
using json = nlohmann::json; | ||
|
||
class UpdaterUI : public UIWindow | ||
{ | ||
private: | ||
//vars | ||
int State = 0; | ||
bool NewVersion = false; | ||
std::string LatestID; | ||
json GitAPIData; | ||
//functions | ||
bool CheckForNewVersion(); | ||
public: | ||
//vars | ||
std::string NroPath = "sdmc:/switch/N-Xplorer.nro"; | ||
MenuUI *Menu; | ||
//functions | ||
void DrawUI(); | ||
void GetInput(); | ||
UpdaterUI(); | ||
}; | ||
|
||
UpdaterUI::UpdaterUI() | ||
{ | ||
nifmInitialize(NifmServiceType_User); //Init nifm for connection stuff | ||
} | ||
|
||
void UpdaterUI::DrawUI() | ||
{ | ||
Menu->DrawLongOpMessage(); | ||
} | ||
|
||
void UpdaterUI::GetInput() | ||
{ | ||
//scan input | ||
bool BPressed = false; | ||
while (SDL_PollEvent(Event)) | ||
{ | ||
switch (Event->type) | ||
{ | ||
//Joycon button down | ||
case SDL_JOYBUTTONDOWN: | ||
{ | ||
if (Event->jbutton.which == 0) | ||
{ | ||
if(Event->jbutton.button == 1) | ||
{ | ||
BPressed = true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
//Update the message and do logic | ||
switch(State) | ||
{ | ||
//Check for connection and update | ||
case 0: | ||
{ | ||
if(!HasConnection()) | ||
{ | ||
Menu->LongOpMessage = "Waiting for connection."; | ||
if(BPressed) | ||
{ | ||
*WindowState = 6; | ||
} | ||
} | ||
else | ||
{ | ||
Menu->LongOpMessage = "Checking for new version..."; | ||
NewVersion = CheckForNewVersion(); | ||
State++; | ||
} | ||
} | ||
break; | ||
//Download new zip | ||
case 1: | ||
{ | ||
if(NewVersion) | ||
{ | ||
Menu->LongOpMessage = "Downloading " + LatestID + "."; | ||
string UpdateFileURL = "https://github.com/CompSciOrBust/N-Xplorer/releases/download/" + LatestID + "/N-Xplorer.zip"; | ||
RetrieveToFile(UpdateFileURL, "sdmc:/config/N-Xplorer/update.zip"); | ||
UnzipFile("sdmc:/config/N-Xplorer/update.zip", "sdmc:/"); | ||
if(NroPath != "sdmc:/switch/N-Xplorer.nro") | ||
{ | ||
remove(NroPath.c_str()); | ||
rename("sdmc:/switch/N-Xplorer.nro", NroPath.c_str()); | ||
*IsDone = 1; | ||
} | ||
} | ||
else | ||
{ | ||
Menu->LongOpMessage = "Already on the latest version. Press B to exit."; | ||
if(BPressed) | ||
{ | ||
*WindowState = 6; | ||
NewVersion = false; | ||
State = 0; | ||
} | ||
} | ||
} | ||
break; | ||
//Error | ||
case 999: | ||
{ | ||
Menu->LongOpMessage = "Error! Is GitHub rate limiting you? Press B to exit"; | ||
if(BPressed) | ||
{ | ||
*WindowState = 6; | ||
} | ||
} | ||
break; | ||
|
||
} | ||
} | ||
|
||
bool UpdaterUI::CheckForNewVersion() | ||
{ | ||
//Get data from GitHub API | ||
std::string Data = RetrieveContent("https://api.github.com/repos/CompSciOrBust/N-Xplorer/releases", "application/json"); | ||
//Get the release tag string from the data | ||
GitAPIData = json::parse(Data); | ||
//Check if GitAPI gave us a release tag otherwise we'll crash | ||
if(Data.length() < 300) | ||
{ | ||
State = 999; | ||
return false; | ||
} | ||
LatestID = GitAPIData[0]["tag_name"].get<std::string>(); | ||
//Check if we're running the latest version | ||
return (LatestID != VERSION); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters