From bccfccb559529d74c5c1d0efb7bea5cb60a79688 Mon Sep 17 00:00:00 2001 From: Slluxx Date: Sun, 23 Oct 2022 17:28:01 +0200 Subject: [PATCH] Added option to download images --- source/amiibo.hpp | 20 ++++++++++----- source/main.cpp | 62 +++++++++++++++++++++++++++++++++++------------ 2 files changed, 61 insertions(+), 21 deletions(-) diff --git a/source/amiibo.hpp b/source/amiibo.hpp index 2a48260..5674656 100644 --- a/source/amiibo.hpp +++ b/source/amiibo.hpp @@ -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]; @@ -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(), 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; } diff --git a/source/main.cpp b/source/main.cpp index 68e0edb..7e20d97 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -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); @@ -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()) { @@ -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) @@ -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 { @@ -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); }