diff --git a/src/menu/menu_fileinfo.c b/src/menu/menu_fileinfo.c index fe507621..7c1af8fd 100644 --- a/src/menu/menu_fileinfo.c +++ b/src/menu/menu_fileinfo.c @@ -33,6 +33,17 @@ static char *get_file_type(FILINFO current_fileinfo) { } } +void menu_fileinfo_draw_n64_rom_info(surface_t *disp) { + // rom_header_t temp_header = file_read_rom_header(tmp_buffer); + + // sprintf(buff, "ROM checksum: %llu\n", temp_header.checksum); + // sprintf(buff, "ROM title: %s\n", temp_header.title); + // sprintf(buff, "ROM media type: %c\n", temp_header.metadata.media_type); + // sprintf(buff, "ROM unique id: %.2s\n", (char*)&(temp_header.metadata.unique_identifier)); + // sprintf(buff, "ROM dest market: %c\n", temp_header.metadata.destination_market); + // sprintf(buff, "ROM version: %hhu\n", temp_header.version); +} + void menu_fileinfo_draw_header(surface_t *disp) { graphics_draw_text(disp, (disp->width / 2) - 64, vertical_start_position, "FILE INFORMATION"); // centre = numchars * font_horizontal_pixels / 2 @@ -86,6 +97,8 @@ void menu_fileinfo(FILINFO current_fileinfo) { graphics_draw_text(disp, horizontal_start_position, vertical_position, "Type:"); graphics_draw_text(disp, horizontal_indent, vertical_position += font_vertical_pixels, get_file_type(current_fileinfo)); + //menu_fileinfo_draw_n64_rom_info(disp); + menu_fileinfo_draw_footer(disp); display_show(disp); diff --git a/src/menu/menu_main.c b/src/menu/menu_main.c index 0a224c9f..16756a2e 100644 --- a/src/menu/menu_main.c +++ b/src/menu/menu_main.c @@ -22,6 +22,40 @@ static int items_in_dir = 1; static FILINFO current_fileinfo; + +void load_n64_rom() { + + console_clear(); + + char tmp_buffer[280]; + sprintf(tmp_buffer, "sd:/%s", current_fileinfo.fname); + + rom_header_t temp_header = file_read_rom_header(tmp_buffer); + + printf("ROM title: %s\n\n", temp_header.title); + + printf("ROM media type: %c\n", temp_header.metadata.media_type); + printf("ROM unique id: %.2s\n", (char*)&(temp_header.metadata.unique_identifier)); + printf("uid as int: %d\n\n", temp_header.metadata.unique_identifier); + + printf("ROM destination market: %c\n\n", temp_header.metadata.destination_market); + + printf("ROM version: %hhu\n", temp_header.version); + printf("ROM checksum: %llu\n\n", temp_header.checksum); + + + uint8_t save_type = rom_db_match_save_type(temp_header); + + printf("save type: %d\n", save_type); + sprintf(tmp_buffer, "%s.%llu.sav", current_fileinfo.fname, temp_header.checksum); + wait_ms(5000); // wait used for debugging. Can be removed later. + + assertf(flashcart_load_save(tmp_buffer, (flashcart_save_type_t)save_type, true) == FLASHCART_OK, "ROM load save error"); + + assertf(flashcart_load_rom(current_fileinfo.fname) == FLASHCART_OK, "ROM load error"); + +} + // FIXME: use newlib rather than fatfs to do this! FRESULT scan_file_path (char *path) { @@ -158,27 +192,8 @@ void menu_main_init (settings_t *settings) { if (str_endswith(current_fileinfo.fname, ".z64") || str_endswith(current_fileinfo.fname, ".n64") || str_endswith(current_fileinfo.fname, ".v64") || str_endswith(current_fileinfo.fname, ".rom")) { printf("Loading N64 ROM type...\n"); printf("%s\n", current_fileinfo.fname); - char tmp_buffer[280]; - sprintf(tmp_buffer, "sd:/%s", current_fileinfo.fname); - rom_header_t temp_header = file_read_rom_header(tmp_buffer); - printf("ROM checksum: %llu\n", temp_header.checksum); - printf("ROM title: %s\n", temp_header.title); - printf("ROM media type: %c\n", temp_header.metadata.media_type); - printf("ROM unique id: %.2s\n", (char*)&(temp_header.metadata.unique_identifier)); - printf("ROM dest market: %c\n", temp_header.metadata.destination_market); - printf("ROM version: %hhu\n", temp_header.version); - - printf("uid as int: %d\n", temp_header.metadata.unique_identifier); - uint8_t save_type = rom_db_match_save_type(temp_header); - - printf("save type: %d\n", save_type); - sprintf(tmp_buffer, "%s.%llu.sav", current_fileinfo.fname, temp_header.checksum); - wait_ms(5000); // wait used for debugging. Can be removed later. - - assertf(flashcart_load_save(tmp_buffer, (flashcart_save_type_t)save_type, true) == FLASHCART_OK, "ROM load save error"); - - assertf(flashcart_load_rom(current_fileinfo.fname) == FLASHCART_OK, "ROM load error"); + load_n64_rom(); break; //required! } @@ -210,6 +225,10 @@ void menu_main_init (settings_t *settings) { menu_fileinfo(current_fileinfo); menu_main_refresh(current_dir, disp); } + if (joypad.c[0].L) { + //load the new File list GUI + //menu_file_scroll(current_fileinfo); + } } // TODO: write menu state to SD card diff --git a/src/menu/settings.c b/src/menu/settings.c index aabc0306..f7e6ec80 100644 --- a/src/menu/settings.c +++ b/src/menu/settings.c @@ -12,44 +12,38 @@ #include "settings.h" -void settings_load_from_file (settings_t *settings) { - FILE *fp = NULL; +void settings_load_from_file(settings_t *settings) { + FILE *fp = fopen(SC64_SETTINGS_FILEPATH, "r"); + if (!fp) { + printf("Error loading config file %s\n", SC64_SETTINGS_FILEPATH); + // Generate a default config file. + printf("Creating a new one!\n"); + wait_ms(10000); + settings_load_default_state(settings); + return; + } + char error_buffer[256]; - - printf("Loading settings file %s\n", SC64_SETTINGS_FILEPATH); - wait_ms(1000); - - fp = fopen(SC64_SETTINGS_FILEPATH, "r"); - if (!fp) { - printf("Error loading config file %s\n", SC64_SETTINGS_FILEPATH); - // generate a default config file. - printf("Creating a new one!"); - wait_ms(10000); - //assertf(!fp, "Couldn't open toml config file: %s", SC64_SETTINGS_FILEPATH); - settings_save_default_state(); - } - toml_table_t *conf = toml_parse_file(fp, error_buffer, sizeof(error_buffer)); - if (!conf) { - printf("Error parsing config: %s\n", error_buffer); - wait_ms(10000); - printf("Attempt a repair!"); - settings_validate(); - printf("Creating a new one!"); - settings_save_default_state(); - //assertf(!conf, "Couldn't parse toml config: %s", error_buffer); // TODO: work out why and handle appropriately. - } + fclose(fp); - //fclose(fp); - assertf(!fclose(fp), "Couldn't close toml config file"); // TODO: work out why and handle appropriately. - fp = NULL; + if (!conf) { + printf("Error parsing config: %s\n", error_buffer); + wait_ms(10000); + printf("Attempt a repair!\n"); + settings_validate(); + printf("Creating a new one!\n"); + settings_load_default_state(settings); + //assertf(!conf, "Couldn't parse toml config: %s", error_buffer); // TODO: work out why and handle appropriately. + return; + } // Handle last_rom toml_table_t *last_rom = toml_table_in(conf, "last_rom"); if (!last_rom) { - printf("Missing '[last_rom]' header in config\n"); + printf("Missing '[last_rom]' header in config\n"); wait_ms(10000); - } + } else { toml_datum_t rom_path = toml_string_in(last_rom, "rom_path"); if (!rom_path.ok) { @@ -67,7 +61,7 @@ void settings_load_from_file (settings_t *settings) { wait_ms(10000); } else { - printf("Found save path: %s\n", save_path.u.s ); + printf("Found save path: %s\n", save_path.u.s); settings->last_rom.save_path = save_path.u.s; } @@ -78,7 +72,7 @@ void settings_load_from_file (settings_t *settings) { } else { assertf((int)tmp_save_type.u.i < __FLASHCART_SAVE_TYPE_END, "Invalid save type in config file"); - printf("Found save type: %d\n", (int)tmp_save_type.u.i ); + printf("Found save type: %d\n", (int)tmp_save_type.u.i); settings->last_rom.save_type = (int)tmp_save_type.u.i; } } @@ -131,18 +125,26 @@ void settings_load_from_file (settings_t *settings) { } -void settings_save(void) { - // TODO: if there is a failure, validate or write the default state. - FILE *fp = NULL; - printf("Saving settings file %s\n", SC64_SETTINGS_FILEPATH); - wait_ms(1000); - fp = fopen(SC64_SETTINGS_FILEPATH, "w"); +void settings_save(const char* filename, const settings_t* settings) { + // FILE* fp = fopen(filename, "wb"); + // if (!fp) { + // perror("Failed to open file for writing"); + // return 1; + // } - // TODO: convert and save the state to TOML - fclose(fp); - assertf(!fclose(fp), "Couldn't close toml settings file"); // TODO: work out why and handle appropriately. - fp = NULL; + // // Populate settings data... + + + // if (result != 0) { + // fprintf(stderr, "Failed to write TOML data to file\n"); + // } + + + // fclose(fp); + + // return result; + return; } void settings_load_default_state(settings_t *settings) { @@ -162,15 +164,17 @@ void settings_load_default_state(settings_t *settings) { settings->boot_params.reset_type = BOOT_RESET_TYPE_NMI; settings->boot_params.detect_tv_type = true; settings->boot_params.detect_cic_seed = true; + + // Initialize other default settings... //#endif } -void settings_save_default_state(void) { +void settings_reset(void) { } void settings_validate(void) { + // Validate settings against a file schema... // TODO: should validate against a file schema. // Must take into account that the settings will change in future, so should be backwards compatible. } - diff --git a/src/menu/settings.h b/src/menu/settings.h index 7bb8496f..1573c612 100644 --- a/src/menu/settings.h +++ b/src/menu/settings.h @@ -34,10 +34,9 @@ typedef struct { } settings_t; void settings_load_from_file (settings_t *settings); -void settings_save (void); +void settings_save(const char* filename, const settings_t* settings); void settings_reset (void); void settings_load_default_state(settings_t *settings); -void settings_save_default_state(void); void settings_validate (void); #endif