mine_sweeper_storage.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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(TAG, "Config file %s is not found. Will create new.", MINESWEEPER_SETTINGS_SAVE_PATH);
  24. if(storage_common_stat(storage, CONFIG_FILE_DIRECTORY_PATH, NULL) == FSE_NOT_EXIST) {
  25. FURI_LOG_I(
  26. TAG,
  27. "Directory %s doesn't exist. Will create new.",
  28. CONFIG_FILE_DIRECTORY_PATH);
  29. if(!storage_simply_mkdir(storage, CONFIG_FILE_DIRECTORY_PATH)) {
  30. FURI_LOG_E(TAG, "Error creating directory %s", CONFIG_FILE_DIRECTORY_PATH);
  31. }
  32. }
  33. }
  34. if(!flipper_format_file_open_new(fff_file, MINESWEEPER_SETTINGS_SAVE_PATH)) {
  35. FURI_LOG_E(TAG, "Error creating new file %s", MINESWEEPER_SETTINGS_SAVE_PATH);
  36. mine_sweeper_close_storage();
  37. return;
  38. }
  39. // Store Settings
  40. flipper_format_write_header_cstr(
  41. fff_file, MINESWEEPER_SETTINGS_HEADER, MINESWEEPER_SETTINGS_FILE_VERSION);
  42. uint32_t w = app->settings_info.board_width,
  43. h = app->settings_info.board_height,
  44. d = app->settings_info.difficulty,
  45. f = app->feedback_enabled;
  46. flipper_format_write_uint32(
  47. fff_file, MINESWEEPER_SETTINGS_KEY_WIDTH, &w, 1);
  48. flipper_format_write_uint32(
  49. fff_file, MINESWEEPER_SETTINGS_KEY_HEIGHT, &h, 1);
  50. flipper_format_write_uint32(
  51. fff_file, MINESWEEPER_SETTINGS_KEY_DIFFICULTY, &d, 1);
  52. flipper_format_write_uint32(
  53. fff_file, MINESWEEPER_SETTINGS_KEY_FEEDBACK, &f, 1);
  54. if(!flipper_format_rewind(fff_file)) {
  55. FURI_LOG_E(TAG, "Rewind error");
  56. mine_sweeper_close_config_file(fff_file);
  57. mine_sweeper_close_storage();
  58. return;
  59. }
  60. mine_sweeper_close_config_file(fff_file);
  61. mine_sweeper_close_storage();
  62. return;
  63. }
  64. bool mine_sweeper_read_settings(void* context) {
  65. MineSweeperApp* app = context;
  66. Storage* storage = mine_sweeper_open_storage();
  67. FlipperFormat* fff_file = flipper_format_file_alloc(storage);
  68. if(storage_common_stat(storage, MINESWEEPER_SETTINGS_SAVE_PATH, NULL) != FSE_OK) {
  69. mine_sweeper_close_config_file(fff_file);
  70. mine_sweeper_close_storage();
  71. return false;
  72. }
  73. uint32_t file_version;
  74. FuriString* temp_str = furi_string_alloc();
  75. if (!flipper_format_file_open_existing(fff_file, MINESWEEPER_SETTINGS_SAVE_PATH)) {
  76. FURI_LOG_E(TAG, "Cannot open file %s", MINESWEEPER_SETTINGS_SAVE_PATH);
  77. mine_sweeper_close_config_file(fff_file);
  78. mine_sweeper_close_storage();
  79. return false;
  80. }
  81. if(!flipper_format_read_header(fff_file, temp_str, &file_version)) {
  82. FURI_LOG_E(TAG, "Missing Header Data");
  83. mine_sweeper_close_config_file(fff_file);
  84. mine_sweeper_close_storage();
  85. return false;
  86. }
  87. furi_string_free(temp_str);
  88. if(file_version < MINESWEEPER_SETTINGS_FILE_VERSION) {
  89. FURI_LOG_I(TAG, "old config version, will be removed.");
  90. mine_sweeper_close_config_file(fff_file);
  91. mine_sweeper_close_storage();
  92. return false;
  93. }
  94. uint32_t w = 7, h = 16, d = 0, f = 1;
  95. flipper_format_read_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_WIDTH, &w, 1);
  96. flipper_format_read_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_HEIGHT, &h, 1);
  97. flipper_format_read_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_DIFFICULTY, &d, 1);
  98. flipper_format_read_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_FEEDBACK, &f, 1);
  99. if (w > 32) {w = 32;}
  100. if (w < 16 ) {w = 16;}
  101. if (h > 32 ) {h = 32;}
  102. if (h < 7 ) {h = 7;}
  103. if (d > 2 ) {d = 2;}
  104. if (f > 1) {f = 1;}
  105. app->settings_info.board_width = (uint8_t) w;
  106. app->settings_info.board_height = (uint8_t) h;
  107. app->settings_info.difficulty = (uint8_t) d;
  108. app->feedback_enabled = (uint8_t) f;
  109. flipper_format_rewind(fff_file);
  110. mine_sweeper_close_config_file(fff_file);
  111. mine_sweeper_close_storage();
  112. return true;
  113. }