static inline bool LatestVersion(int *major, int *minor, int *current_line) { u32 http_status = 0; u8* outbuf = NULL; u32 filesize; int line = *current_line; PrintFormat(DEFAULT_SIZE, BLACK, MENU_POS_X, MENU_POS_Y + 20*line, Downloads[DOWNLOAD_VERSION].text); UpdateScreen(); line++; if(!http_request(Downloads[DOWNLOAD_VERSION].url, Downloads[DOWNLOAD_VERSION].max_size)) { PrintFormat(DEFAULT_SIZE, BLACK, MENU_POS_X, MENU_POS_Y + 20*line, "Failed to retrieve version"); UpdateScreen(); *current_line = line; return false; } http_get_result(&http_status, &outbuf, &filesize); if (((int)*outbuf & 0xF0000000) == 0xF0000000) { if (outbuf != NULL) free(outbuf); *current_line = line; return false; } sscanf((char*)outbuf, " #ifndef %*s #define %*s #define NIN_MAJOR_VERSION %i #define NIN_MINOR_VERSION %i", major, minor); gprintf("major = %i, minor = %i\r\n", *major, *minor); if (outbuf != NULL) free(outbuf); if ((*major <= NIN_MAJOR_VERSION) && (*minor <= NIN_MINOR_VERSION)) { bool still_download = true; PrintFormat(DEFAULT_SIZE, BLACK, MENU_POS_X, MENU_POS_Y + 20*line, "You already have the latest version"); line++; PrintFormat(DEFAULT_SIZE, BLACK, MENU_POS_X, MENU_POS_Y + 20*line, "Download anyway? (A: Yes, B: No)"); line++; UpdateScreen(); while(true) { DrawBuffer(); FPAD_Update(); if (FPAD_Cancel(0)) { gprintf("Cancelling download\n"); still_download = false; break; } if (FPAD_OK(0)) { gprintf("okay\n"); break; } GRRLIB_Render(); } if(!still_download) { *current_line = line; return false; } } *current_line = line; return (*major + *minor) > 0; }
int get_nus_object(u32 titleid1, u32 titleid2, char *content, u8 **outbuf, u32 *outlen) { static char buf[128]; int retval; u32 http_status; snprintf(buf, 128, "http://nus.cdn.shop.wii.com/ccs/download/%08x%08x/%s", titleid1, titleid2, content); retval = http_request(buf, 1 << 31); if (!retval) { printf("Error making http request\n"); return -1; } retval = http_get_result(&http_status, outbuf, outlen); if (((int)*outbuf & 0xF0000000) == 0xF0000000) { return (int) *outbuf; } return 0; }
static s32 Download(DOWNLOADS download_number) { ClearScreen(); int line = 1; int ret, major = 0, minor = 0; u32 http_status = 0; u8* outbuf = NULL; u32 filesize; char filepath[MAXPATHLEN]; FILE *file; bool dir_argument_exists = strlen(launch_dir); snprintf(filepath, sizeof(filepath), "%s%s", dir_argument_exists ? launch_dir : "/apps/Nintendont/", Downloads[download_number].filename); PrintFormat(DEFAULT_SIZE, BLACK, MENU_POS_X, MENU_POS_Y + 20*line, Downloads[download_number].text); UpdateScreen(); line++; gprintf("Downloading %s to %s\r\n", Downloads[download_number].url, filepath); ret = net_init(); if(ret < 0) { gprintf("Failed to init network\r\n"); goto end; } gprintf("Network Initialized\r\n"); PrintFormat(DEFAULT_SIZE, BLACK, MENU_POS_X, MENU_POS_Y + 20*line, "Network Initialized"); UpdateScreen(); ssl_init(); //only once needed line++; if (download_number == DOWNLOAD_NINTENDONT) { ret = LatestVersion(&major, &minor, &line); if (!ret) { PrintFormat(DEFAULT_SIZE, BLACK, MENU_POS_X, MENU_POS_Y + 20*line, "Download Cancelled"); UpdateScreen(); if (outbuf != NULL) free(outbuf); net_deinit(); sleep(4); return 0; } PrintFormat(DEFAULT_SIZE, BLACK, MENU_POS_X, MENU_POS_Y + 20*line, "Downloading Nintendont v%i.%i", major, minor); UpdateScreen(); line++; } int i; for (i = 0; i <= 10; i++) { ret = http_request(Downloads[download_number].url, Downloads[download_number].max_size); if (ret) break; if (i == 10) { gprintf("Error making http request\r\n"); ret = -1; goto end; } } ret = http_get_result(&http_status, &outbuf, &filesize); if(((int)*outbuf & 0xF0000000) == 0xF0000000) { ret = -2; goto end; } PrintFormat(DEFAULT_SIZE, BLACK, MENU_POS_X, MENU_POS_Y + 20*line, "Download Complete"); UpdateScreen(); line++; if (!dir_argument_exists) { gprintf("Creating new directory\r\n"); mkdir("/apps", S_IWRITE|S_IREAD); mkdir("/apps/Nintendont", S_IWRITE|S_IREAD); } file = fopen(filepath, "wb"); if(!file) { gprintf("File Error\r\n"); ret = -3; goto end; } else { fwrite(outbuf, filesize, 1, file); fclose(file); if (download_number == DOWNLOAD_CONTROLLERS) ret = UnzipControllers(filepath); if (ret == 1) { PrintFormat(DEFAULT_SIZE, BLACK, MENU_POS_X, MENU_POS_Y + 20*line, "Update Complete"); UpdateScreen(); line++; } } end: if (ret != 1) PrintFormat(DEFAULT_SIZE, MAROON, MENU_POS_X, MENU_POS_Y + 20*line, "Update Error: %i", ret); else PrintFormat(DEFAULT_SIZE, BLACK, MENU_POS_X, MENU_POS_Y + 20*line, "Restart Nintendont to complete update"); UpdateScreen(); if (outbuf != NULL) free(outbuf); net_deinit(); sleep(4); return ret; }