From 63090ae2e485ba8b5e4b2217c043dbc04df661fb Mon Sep 17 00:00:00 2001 From: GameDragon2k Date: Sun, 22 Oct 2017 13:02:16 -0400 Subject: [PATCH] Simple http request function to download badges. --- cheevos/badges.c | 53 +++++++++++++++++++++++++++++++++++++++++++++-- cheevos/badges.h | 1 + cheevos/cheevos.c | 47 +++++++++++++++++++++++++++++------------ 3 files changed, 86 insertions(+), 15 deletions(-) diff --git a/cheevos/badges.c b/cheevos/badges.c index 4dca01fef6..d143084660 100644 --- a/cheevos/badges.c +++ b/cheevos/badges.c @@ -1,9 +1,58 @@ -#include "badges.h" -#include "../menu/menu_driver.h" +#include +#include + #include "../verbosity.h" +#include "../network/net_http_special.h" + +#include "badges.h" badges_ctx_t badges_ctx; +bool download_badge(const char* filename) +{ + char fullpath[PATH_MAX_LENGTH]; + + strcpy(fullpath, "badges/"); + strcat(fullpath, filename); + + if(path_file_exists(fullpath)) + { + return true; + } + else + { + size_t mysize = 1024 * 100; + size_t *size; + size = &mysize; + + const char **buffer = malloc(sizeof(*buffer) * mysize); + char url[PATH_MAX_LENGTH]; + strcpy(url, "http://i.retroachievements.org/Badge/"); + strcat(url, filename); + + retro_time_t *timeout; + retro_time_t timesecs = 10000000; //10 seconds + timeout = ×ecs; + + if(net_http_get(buffer, size, url, timeout) != NET_HTTP_GET_OK) + { + printf("[CHEEVOS]: Download to %s failed.\n", fullpath); + return false; + } + + if (!filestream_write_file(fullpath, *buffer, *size)) + { + printf("[CHEEVOS]: Write to %s failed.\n", fullpath); + return false; + } + else + { + printf("[CHEEVOS]: %s downloaded.\n", fullpath); + return true; + } + } +} + void set_badge_menu_texture(badges_ctx_t * badges, int i) { const char * locked_suffix = (badges->badge_locked[i] == true) ? "_lock.png" : ".png"; diff --git a/cheevos/badges.h b/cheevos/badges.h index b5b9011122..da0e4d6787 100644 --- a/cheevos/badges.h +++ b/cheevos/badges.h @@ -19,6 +19,7 @@ typedef struct void set_badge_menu_texture(badges_ctx_t * badges, int i); extern void set_badge_info (badges_ctx_t *badge_struct, int id, const char *badge_id, bool active); extern menu_texture_item get_badge_texture(int id); +extern bool download_badge(const char* filename); extern badges_ctx_t badges_ctx; diff --git a/cheevos/cheevos.c b/cheevos/cheevos.c index bce5770895..4edf5e87f7 100644 --- a/cheevos/cheevos.c +++ b/cheevos/cheevos.c @@ -2573,11 +2573,12 @@ static int cheevos_iterate(coro_t* coro) FILL_MD5 = -7, GET_GAMEID = -8, GET_CHEEVOS = -9, - LOGIN = -10, - HTTP_GET = -11, - DEACTIVATE = -12, - PLAYING = -13, - DELAY = -14 + GET_BADGES = -10, + LOGIN = -11, + HTTP_GET = -12, + DEACTIVATE = -13, + PLAYING = -14, + DELAY = -15 }; static const uint32_t genesis_exts[] = @@ -2817,6 +2818,7 @@ static int cheevos_iterate(coro_t* coro) if ((void*)CHEEVOS_VAR_JSON) free((void*)CHEEVOS_VAR_JSON); + cheevos_loaded = true; /* @@ -3153,6 +3155,33 @@ static int cheevos_iterate(coro_t* coro) RARCH_LOG("[CHEEVOS]: got achievements for game id %u.\n", CHEEVOS_VAR_GAMEID); CORO_RET(); + /************************************************************************** + * Info Gets the achievements from Retro Achievements + * Inputs CHEEVOS_VAR_GAMEID + * Outputs CHEEVOS_VAR_JSON + *************************************************************************/ + CORO_SUB(GET_BADGES) + + char badge_filename[16]; + cheevo_t *cheevo = cheevos_locals.core.cheevos; + const cheevo_t *end = cheevos_locals.core.cheevos + + cheevos_locals.core.count; + + for (unsigned i = 0; cheevo < end ; cheevo++) + { + strcpy(badge_filename, cheevo->badge); + strcat(badge_filename, ".png"); + if (!download_badge(badge_filename)) + break; + + strcpy(badge_filename, cheevo->badge); + strcat(badge_filename, "_lock.png"); + if (!download_badge(badge_filename)) + break; + } + + CORO_RET(); + /************************************************************************** * Info Logs in the user at Retro Achievements *************************************************************************/ @@ -3340,12 +3369,8 @@ static int cheevos_iterate(coro_t* coro) else RARCH_ERR("[CHEEVOS]: error deactivating unlocked achievements in softcore mode.\n"); -<<<<<<< HEAD if ((void*)CHEEVOS_VAR_JSON) free((void*)CHEEVOS_VAR_JSON); -======= - free((void*)CHEEVOS_VAR_JSON); ->>>>>>> Initial test for badge icon } else RARCH_ERR("[CHEEVOS]: error retrieving list of unlocked achievements in softcore mode.\n"); @@ -3373,12 +3398,8 @@ static int cheevos_iterate(coro_t* coro) else RARCH_ERR("[CHEEVOS]: error deactivating unlocked achievements in hardcore mode.\n"); -<<<<<<< HEAD if ((void*)CHEEVOS_VAR_JSON) free((void*)CHEEVOS_VAR_JSON); -======= - free((void*)CHEEVOS_VAR_JSON); ->>>>>>> Initial test for badge icon } else RARCH_ERR("[CHEEVOS]: error retrieving list of unlocked achievements in hardcore mode.\n");