Skip to content

Commit

Permalink
Improved JSON validation
Browse files Browse the repository at this point in the history
  • Loading branch information
CompSciOrBust committed Oct 31, 2019
1 parent 5223a53 commit 3b4ceab
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ INCLUDES := include
#ROMFS := romfs
APP_TITLE := Amiigo
APP_AUTHOR := CompSciOrBust
APP_VERSION := 1.4.1
APP_VERSION := 1.4.2

#---------------------------------------------------------------------------------
# options for code generation
Expand Down
30 changes: 20 additions & 10 deletions source/CreatorUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ void CreatorUI::DrawHeader()

void CreatorUI::GetDataFromAPI(string FilterTerm)
{
bool CouldNotGetJSON = false;
//Make the Amiigo config dir
mkdir("sdmc:/config/amiigo/", 0);
//Check we have a connection before trying to access the network
Expand All @@ -315,20 +316,29 @@ void CreatorUI::GetDataFromAPI(string FilterTerm)
//Get data from the api
string APIURI = "https://www.amiiboapi.com/api/amiibo" + FilterTerm;
AmiiboAPIString = RetrieveContent(APIURI, "application/json").c_str();
//Quick hack to make sure we don't overwrite a valid file with an invalid one
//Should fatal if invalid
JData = json::parse(AmiiboAPIString);
//Save the data to the SD card in case the user wants to use it offline
ofstream DataFileWriter("sdmc:/config/amiigo/API.json");
DataFileWriter << AmiiboAPIString;
DataFileWriter.close();
//Check if valid json
if(json::accept(AmiiboAPIString))
{
//Save the data to the SD card in case the user wants to use it offline
ofstream DataFileWriter("sdmc:/config/amiigo/API.json");
DataFileWriter << AmiiboAPIString;
DataFileWriter.close();
}
else //JSON is invalid so could not get JSON
{
CouldNotGetJSON = true;
}
}
else
else //No connection so could not get JSON
{
CouldNotGetJSON = true;
}
//We couldn't get data from the Amiibo API so load the data from the SD card
if(CouldNotGetJSON)
{
//No connection so load the data from the SD card
ifstream DataFileReader("sdmc:/config/amiigo/API.json");
getline(DataFileReader, AmiiboAPIString);
DataFileReader.close();
DataFileReader.close();
}
//Parse and use the JSON data
JData = json::parse(AmiiboAPIString);
Expand Down
22 changes: 22 additions & 0 deletions source/UpdaterUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ void UpdaterUI::DrawUI()
else
{
UpdateText = "Waiting for connection.";
if(BPressed)
{
*WindowState = 0;
}
}
}
break;
Expand Down Expand Up @@ -107,6 +111,16 @@ void UpdaterUI::DrawUI()
*IsDone = 1;
}
break;
//Somethign went wrong. User should not normally end up here.
case 999:
{
UpdateText = "Error! Is GitHub rate limiting you?";
if(BPressed)
{
*WindowState = 0;
}
}
break;
}
DrawText(UpdateText);
}
Expand All @@ -115,7 +129,15 @@ bool UpdaterUI::CheckForNewVersion()
{
//Get data from GitHub API
string Data = RetrieveContent("https://api.github.com/repos/CompSciOrBust/Amiigo/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(GitAPIData.count("0") == 0)
{
//User is probably rate limited.
UpdateState = 999;
return false;
}
LatestID = GitAPIData[0]["tag_name"].get<std::string>();
//Check if we're running the latest version
return (LatestID != APP_VERSION);
Expand Down

0 comments on commit 3b4ceab

Please sign in to comment.