N64FlashcartMenu/src/menu/settings.c

181 lines
5.5 KiB
C
Raw Normal View History

2023-02-24 15:24:59 +01:00
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <libdragon.h>
#include "flashcart/flashcart.h"
#include "libs/toml/toml.h"
#include "boot/boot.h"
#include "settings.h"
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");
2023-02-24 15:24:59 +01:00
wait_ms(10000);
settings_load_default_state(settings);
return;
}
2023-02-24 15:24:59 +01:00
char error_buffer[256];
2023-02-24 15:24:59 +01:00
toml_table_t *conf = toml_parse_file(fp, error_buffer, sizeof(error_buffer));
fclose(fp);
2023-02-24 15:24:59 +01:00
if (!conf) {
printf("Error parsing config: %s\n", error_buffer);
2023-02-24 15:24:59 +01:00
wait_ms(10000);
printf("Attempt a repair!\n");
2023-02-24 15:24:59 +01:00
settings_validate();
printf("Creating a new one!\n");
settings_load_default_state(settings);
2023-02-24 15:24:59 +01:00
//assertf(!conf, "Couldn't parse toml config: %s", error_buffer); // TODO: work out why and handle appropriately.
return;
}
2023-02-24 15:24:59 +01:00
2023-02-25 03:24:19 +01:00
// Handle last_rom
2023-02-24 15:24:59 +01:00
toml_table_t *last_rom = toml_table_in(conf, "last_rom");
if (!last_rom) {
printf("Missing '[last_rom]' header in config\n");
2023-02-24 15:24:59 +01:00
wait_ms(10000);
}
2023-02-24 15:24:59 +01:00
else {
2023-02-25 03:24:19 +01:00
toml_datum_t rom_path = toml_string_in(last_rom, "rom_path");
if (!rom_path.ok) {
printf("Couldn't read 'rom_path' value in config\n");
wait_ms(10000);
}
else {
printf("Found rom path: %s\n", rom_path.u.s);
settings->last_rom.rom_path = rom_path.u.s;
}
toml_datum_t save_path = toml_string_in(last_rom, "save_path");
if (!save_path.ok) {
printf("Couldn't read 'save_path' value in config\n");
wait_ms(10000);
}
else {
printf("Found save path: %s\n", save_path.u.s);
2023-02-25 03:24:19 +01:00
settings->last_rom.save_path = save_path.u.s;
}
toml_datum_t tmp_save_type = toml_int_in(last_rom, "save_type");
if (!tmp_save_type.ok) {
printf("Couldn't read 'save_type' value in config\n");
wait_ms(10000);
}
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);
2023-02-25 03:24:19 +01:00
settings->last_rom.save_type = (int)tmp_save_type.u.i;
}
2023-02-24 15:24:59 +01:00
}
2023-02-25 03:24:19 +01:00
// Handle last_state
toml_table_t* last_state = toml_table_in(conf, "last_state");
if (!last_state) {
printf("Missing '[last_state]' header in config\n");
2023-02-24 15:24:59 +01:00
wait_ms(10000);
}
else {
2023-02-25 03:24:19 +01:00
// toml_datum_t current_directory = toml_string_in(last_state, "current_directory");
// if (!current_directory.ok) {
// printf("Couldn't read 'current_directory' value in config\n");
// printf("Defaulting to root directory.\n");
// settings->last_state.current_directory = "sd://";
// wait_ms(5000);
// }
// else {
// printf("Found current directory: %s\n", current_directory.u.s );
// settings->last_state.current_directory = current_directory.u.s;
// }
// toml_datum_t tmp_auto_load_last_rom = toml_bool_in(last_state, "auto_load");
// if (!tmp_auto_load_last_rom.ok) {
// printf("Couldn't read 'auto_load' value in config\n");
// printf("Defaulting to false.\n");
// wait_ms(5000);
// settings->last_state.auto_load_last_rom = false;
// }
// else {
// printf("Found autoload: %s\n", tmp_auto_load_last_rom.u.b ? "true" : "false");
// settings->last_state.auto_load_last_rom = tmp_auto_load_last_rom.u.b;
// }
2023-02-24 15:24:59 +01:00
}
2023-02-25 03:24:19 +01:00
// Handle boot_params
toml_table_t* boot_params = toml_table_in(conf, "boot_params");
if (!boot_params) {
printf("Missing '[boot_params]' header in config\n");
2023-02-24 15:24:59 +01:00
wait_ms(10000);
}
else {
2023-02-25 03:36:41 +01:00
2023-02-24 15:24:59 +01:00
}
toml_free(conf);
}
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;
// }
2023-02-24 15:24:59 +01:00
// // Populate settings data...
// if (result != 0) {
// fprintf(stderr, "Failed to write TOML data to file\n");
// }
// fclose(fp);
// return result;
return;
2023-02-24 15:24:59 +01:00
}
void settings_load_default_state(settings_t *settings) {
// Happens on init
// TODO: should also happen as a last resort
// Mainly if the file does not exist, or is corrupted beyond repair.
//#ifdef SETTINGS_SCHEMA_VERSION 1
settings->last_rom.rom_path = "";
settings->last_rom.save_path = "";
settings->last_rom.save_type = FLASHCART_SAVE_TYPE_NONE;
settings->last_rom.save_writeback = false;
New browser view using rdpq acceleration + MP3 player (#10) <!--- Provide a general summary of your changes in the Title above --> ## Description This change replaces deprecated graphics libdragon api with rdpq hardware accelerated drawing calls. New view has been added: MP3 player <!--- Describe your changes in detail --> ## Motivation and Context Use newest and supported features of libdragon api <!--- What does this sample do? What problem does it solve? --> <!--- If it fixes/closes/resolves an open issue, please link to the issue here --> ## How Has This Been Tested? On hardware with SC64 flashcart <!-- (if applicable) --> <!--- Please describe in detail how you tested your sample/changes. --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> ## Screenshots ![Screenshot 2023-07-08 23-57-56](https://github.com/Polprzewodnikowy/N64FlashcartMenu/assets/3756990/3f791246-5f70-43d1-8c54-aeac62513ff3) ![Screenshot 2023-07-08 23-58-16](https://github.com/Polprzewodnikowy/N64FlashcartMenu/assets/3756990/fdf436bf-6201-4b43-bebc-70be993ebc50) ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Improvement (non-breaking change that adds a new feature) - [ ] Bug fix (fixes an issue) - [x] Breaking change (breaking change) - [ ] Config and build (change in the configuration and build system, has no impact on code or features) ## Checklist: <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [x] My code follows the code style of this project. - [x] My change requires a change to the documentation. - [ ] I have updated the documentation accordingly. - [ ] I have added tests to cover my changes. - [ ] All new and existing tests passed. <!--- It would be nice if you could sign off your contribution by replacing the name with your GitHub user name and GitHub email contact. --> Signed-off-by: GITHUB_USER <GITHUB_USER_EMAIL>
2023-07-09 00:01:41 +02:00
settings->last_state.directory = ""; // This must not include the trailing slash on dirs!
2023-02-24 15:24:59 +01:00
settings->last_state.auto_load_last_rom = false;
settings->boot_params.device_type = BOOT_DEVICE_TYPE_ROM;
settings->boot_params.reset_type = BOOT_RESET_TYPE_NMI;
2023-07-02 21:52:58 +02:00
settings->boot_params.tv_type = BOOT_TV_TYPE_PASSTHROUGH;
2023-02-24 15:24:59 +01:00
settings->boot_params.detect_cic_seed = true;
// Initialize other default settings...
2023-02-24 15:24:59 +01:00
//#endif
}
void settings_reset(void) {
2023-02-24 15:24:59 +01:00
}
void settings_validate(void) {
// Validate settings against a file schema...
2023-02-24 15:24:59 +01:00
// TODO: should validate against a file schema.
// Must take into account that the settings will change in future, so should be backwards compatible.
}