mine_sweeper_storage.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "mine_sweeper_storage.h"
  2. static Storage* mine_sweeper_open_storage() {
  3. return furi_record_open(RECORD_STORAGE);
  4. }
  5. static void mine_sweeper_close_storage() {
  6. furi_record_close(RECORD_STORAGE);
  7. }
  8. static void mine_sweeper_close_config_file(FlipperFormat* file) {
  9. if(file == NULL) return;
  10. flipper_format_file_close(file);
  11. flipper_format_free(file);
  12. }
  13. void mine_sweeper_save_settings(void* context) {
  14. MineSweeperApp* app = context;
  15. Storage* storage = mine_sweeper_open_storage();
  16. FlipperFormat* fff_file = flipper_format_file_alloc(storage);
  17. // Overwrite wont work, so delete first
  18. if(storage_file_exists(storage, MINESWEEPER_SETTINGS_SAVE_PATH)) {
  19. storage_simply_remove(storage, MINESWEEPER_SETTINGS_SAVE_PATH);
  20. }
  21. // Open File, create if not exists
  22. if(!storage_common_stat(storage, MINESWEEPER_SETTINGS_SAVE_PATH, NULL) == FSE_OK) {
  23. FURI_LOG_I(
  24. TAG, "Config file %s is not found. Will create new.", MINESWEEPER_SETTINGS_SAVE_PATH);
  25. if(storage_common_stat(storage, CONFIG_FILE_DIRECTORY_PATH, NULL) == FSE_NOT_EXIST) {
  26. FURI_LOG_I(
  27. TAG, "Directory %s doesn't exist. Will create new.", CONFIG_FILE_DIRECTORY_PATH);
  28. if(!storage_simply_mkdir(storage, CONFIG_FILE_DIRECTORY_PATH)) {
  29. FURI_LOG_E(TAG, "Error creating directory %s", CONFIG_FILE_DIRECTORY_PATH);
  30. }
  31. }
  32. }
  33. if(!flipper_format_file_open_new(fff_file, MINESWEEPER_SETTINGS_SAVE_PATH)) {
  34. FURI_LOG_E(TAG, "Error creating new file %s", MINESWEEPER_SETTINGS_SAVE_PATH);
  35. mine_sweeper_close_storage();
  36. return;
  37. }
  38. // Store Settings
  39. flipper_format_write_header_cstr(
  40. fff_file, MINESWEEPER_SETTINGS_HEADER, MINESWEEPER_SETTINGS_FILE_VERSION);
  41. flipper_format_write_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_HAPTIC, &app->haptic, 1);
  42. flipper_format_write_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_SPEAKER, &app->speaker, 1);
  43. flipper_format_write_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_LED, &app->led, 1);
  44. uint32_t w = app->settings_info.board_width, h = app->settings_info.board_height,
  45. d = app->settings_info.difficulty;
  46. flipper_format_write_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_WIDTH, &w, 1);
  47. flipper_format_write_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_HEIGHT, &h, 1);
  48. flipper_format_write_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_DIFFICULTY, &d, 1);
  49. if(!flipper_format_rewind(fff_file)) {
  50. FURI_LOG_E(TAG, "Rewind error");
  51. mine_sweeper_close_config_file(fff_file);
  52. mine_sweeper_close_storage();
  53. return;
  54. }
  55. mine_sweeper_close_config_file(fff_file);
  56. mine_sweeper_close_storage();
  57. return;
  58. }
  59. bool mine_sweeper_read_settings(void* context) {
  60. MineSweeperApp* app = context;
  61. Storage* storage = mine_sweeper_open_storage();
  62. FlipperFormat* fff_file = flipper_format_file_alloc(storage);
  63. if(storage_common_stat(storage, MINESWEEPER_SETTINGS_SAVE_PATH, NULL) != FSE_OK) {
  64. mine_sweeper_close_config_file(fff_file);
  65. mine_sweeper_close_storage();
  66. return false;
  67. }
  68. uint32_t file_version;
  69. FuriString* temp_str = furi_string_alloc();
  70. if(!flipper_format_file_open_existing(fff_file, MINESWEEPER_SETTINGS_SAVE_PATH)) {
  71. FURI_LOG_E(TAG, "Cannot open file %s", MINESWEEPER_SETTINGS_SAVE_PATH);
  72. mine_sweeper_close_config_file(fff_file);
  73. mine_sweeper_close_storage();
  74. return false;
  75. }
  76. if(!flipper_format_read_header(fff_file, temp_str, &file_version)) {
  77. FURI_LOG_E(TAG, "Missing Header Data");
  78. mine_sweeper_close_config_file(fff_file);
  79. mine_sweeper_close_storage();
  80. return false;
  81. }
  82. furi_string_free(temp_str);
  83. if(file_version < MINESWEEPER_SETTINGS_FILE_VERSION) {
  84. FURI_LOG_I(TAG, "old config version, will be removed.");
  85. mine_sweeper_close_config_file(fff_file);
  86. mine_sweeper_close_storage();
  87. return false;
  88. }
  89. uint32_t w = 7, h = 16, d = 0;
  90. flipper_format_read_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_WIDTH, &w, 1);
  91. flipper_format_read_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_HEIGHT, &h, 1);
  92. flipper_format_read_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_DIFFICULTY, &d, 1);
  93. if(w > 146) {
  94. w = 146;
  95. }
  96. if(w < 16) {
  97. w = 16;
  98. }
  99. if(h > 64) {
  100. h = 64;
  101. }
  102. if(h < 7) {
  103. h = 7;
  104. }
  105. if(d > 2) {
  106. d = 2;
  107. }
  108. app->settings_info.board_width = (uint8_t)w;
  109. app->settings_info.board_height = (uint8_t)h;
  110. app->settings_info.difficulty = (uint8_t)d;
  111. flipper_format_read_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_HAPTIC, &app->haptic, 1);
  112. flipper_format_read_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_SPEAKER, &app->speaker, 1);
  113. flipper_format_read_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_LED, &app->led, 1);
  114. flipper_format_rewind(fff_file);
  115. mine_sweeper_close_config_file(fff_file);
  116. mine_sweeper_close_storage();
  117. return true;
  118. }