Skip to content

Commit

Permalink
Added option to download images
Browse files Browse the repository at this point in the history
  • Loading branch information
Slluxx committed Oct 23, 2022
1 parent 34aa471 commit bccfccb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 21 deletions.
20 changes: 14 additions & 6 deletions source/amiibo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Amiibo
amiiboJsonDatabase = json::parse(ifs);
}

bool generateAmiibo(int number, int maxAmiibos)
bool generateAmiibo(int number, int maxAmiibos, bool withImage)
{

json amiibo = amiiboJsonDatabase["amiibo"][number];
Expand Down Expand Up @@ -123,22 +123,30 @@ class Amiibo
output2 << amiiboData.dump(2);
output2.close();

std::string text = "[" + std::to_string(number) + "/" + std::to_string(maxAmiibos) + "] generated: " + amiiboSeries + " - " + amiiboName + "\n";

if (withImage){
int ret = Util::download_file(amiibo["image"].get<std::string>(), amiiboPathFull + "amiibo.png");
if (ret != 0)
{
printf("Failed to download image. Error code: %d\n", ret);
}
}

std::string text = "[" + std::to_string(number + 1) + "/" + std::to_string(maxAmiibos) + "] generated: " + amiiboSeries + " - " + amiiboName + "\n";
printf(text.c_str());
consoleUpdate(NULL);
return true;
}

bool generateAllAmibos()
bool generateAllAmibos(bool withImage = false)
{
for (long unsigned int i = 0; i < amiiboJsonDatabase["amiibo"].size(); i++)
{
generateAmiibo(i, amiiboJsonDatabase["amiibo"].size() - 1);
generateAmiibo(i, amiiboJsonDatabase["amiibo"].size(), withImage);
}

printf("\nAmiibos generated, you can exit now. (+)\n");
printf("\nAmiibos generated.\n\n");
consoleUpdate(NULL);

return true;
}

Expand Down
62 changes: 47 additions & 15 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,31 @@
#include "amiibo.hpp"
#include "util.hpp"

void print_menu()
{
printf("Press + to exit.\n");
// printf("Press - to delete all currently generated amiibo.\n");
printf("Press Y to download/update amiibo database (needs internet).\n");
printf("Press X to generate all amiibos.\n");
printf("Press A to generate all amiibos with images (very slow, needs internet).\n\n");
consoleUpdate(NULL);
}

void clickedGenerateAmiibo(bool dbExists, bool withImage = false)
{
if (!dbExists)
{
printf("Amiibo database not found. Please download it first (Y).\n\n");
}
else
{
printf("Generating all amiibos...\n");
Amiibo amiibo = Amiibo();
amiibo.generateAllAmibos(withImage);
print_menu();
}
}

int main(int argc, char *argv[])
{
consoleInit(NULL);
Expand All @@ -19,14 +44,14 @@ int main(int argc, char *argv[])
bool amiiboFileExists = Util::check_file_exist("sdmc:/emuiibo/amiibos.json");
if (!amiiboFileExists)
{
printf("Amiibo database not found. Press Y to download it.\n\n");
printf("Amiibo database not found.\n\n");
}
else
{
printf("Amiibo database found.\nPress Y to update, X to generate all amiibos or + to exit.\n\n");
printf("Amiibo database found.\n\n");
}

consoleUpdate(NULL);
print_menu();

while (appletMainLoop())
{
Expand All @@ -36,6 +61,19 @@ int main(int argc, char *argv[])
if (kDown & HidNpadButton_Plus)
break;

/*
// I dont know why but this doesnt work for some reason. Crashes the application.
if (kDown & HidNpadButton_Minus)
{
printf("Deleting all generated amiibos....\n");
if (Util::check_folder_exist("sdmc:/emuiibo/amiibo/"))
{
Util::delete_folder_with_content("sdmc:/emuiibo/amiibo");
}
printf("All generated amiibo deleted.\n\n");
}
*/

if (kDown & HidNpadButton_Y)
{
if (amiiboFileExists)
Expand All @@ -47,9 +85,8 @@ int main(int argc, char *argv[])
int ret = Util::download_file("https://www.amiiboapi.com/api/amiibo/", "sdmc:/emuiibo/amiibos.json");
if (ret == 0)
{
printf("Database downloaded.\n");
printf("You can now press X to generate all amiibos, Y to update again or + to exit.\n\n");
amiiboFileExists = true;
printf("Database downloaded. You can now generate amiibos.\n\n");
}
else
{
Expand All @@ -60,17 +97,12 @@ int main(int argc, char *argv[])

if (kDown & HidNpadButton_X)
{
if (!amiiboFileExists)
{
printf("Amiibo database not found. Please download it first.\n\n");
}
else
{
clickedGenerateAmiibo(amiiboFileExists, false);
}

printf("Generating all amiibos...\n");
Amiibo amiibo = Amiibo();
amiibo.generateAllAmibos();
}
if (kDown & HidNpadButton_A)
{
clickedGenerateAmiibo(amiiboFileExists, true);
}
consoleUpdate(NULL);
}
Expand Down

0 comments on commit bccfccb

Please sign in to comment.