mine_sweeper_storage.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. uint32_t w = app->settings_info.board_width, h = app->settings_info.board_height,
  42. d = app->settings_info.difficulty, f = app->feedback_enabled;
  43. flipper_format_write_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_WIDTH, &w, 1);
  44. flipper_format_write_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_HEIGHT, &h, 1);
  45. flipper_format_write_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_DIFFICULTY, &d, 1);
  46. flipper_format_write_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_FEEDBACK, &f, 1);
  47. if(!flipper_format_rewind(fff_file)) {
  48. FURI_LOG_E(TAG, "Rewind error");
  49. mine_sweeper_close_config_file(fff_file);
  50. mine_sweeper_close_storage();
  51. return;
  52. }
  53. mine_sweeper_close_config_file(fff_file);
  54. mine_sweeper_close_storage();
  55. return;
  56. }
  57. bool mine_sweeper_read_settings(void* context) {
  58. MineSweeperApp* app = context;
  59. Storage* storage = mine_sweeper_open_storage();
  60. FlipperFormat* fff_file = flipper_format_file_alloc(storage);
  61. if(storage_common_stat(storage, MINESWEEPER_SETTINGS_SAVE_PATH, NULL) != FSE_OK) {
  62. mine_sweeper_close_config_file(fff_file);
  63. mine_sweeper_close_storage();
  64. return false;
  65. }
  66. uint32_t file_version;
  67. FuriString* temp_str = furi_string_alloc();
  68. if(!flipper_format_file_open_existing(fff_file, MINESWEEPER_SETTINGS_SAVE_PATH)) {
  69. FURI_LOG_E(TAG, "Cannot open file %s", MINESWEEPER_SETTINGS_SAVE_PATH);
  70. mine_sweeper_close_config_file(fff_file);
  71. mine_sweeper_close_storage();
  72. return false;
  73. }
  74. if(!flipper_format_read_header(fff_file, temp_str, &file_version)) {
  75. FURI_LOG_E(TAG, "Missing Header Data");
  76. mine_sweeper_close_config_file(fff_file);
  77. mine_sweeper_close_storage();
  78. return false;
  79. }
  80. furi_string_free(temp_str);
  81. if(file_version < MINESWEEPER_SETTINGS_FILE_VERSION) {
  82. FURI_LOG_I(TAG, "old config version, will be removed.");
  83. mine_sweeper_close_config_file(fff_file);
  84. mine_sweeper_close_storage();
  85. return false;
  86. }
  87. uint32_t w = 7, h = 16, d = 0, f = 1;
  88. flipper_format_read_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_WIDTH, &w, 1);
  89. flipper_format_read_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_HEIGHT, &h, 1);
  90. flipper_format_read_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_DIFFICULTY, &d, 1);
  91. flipper_format_read_uint32(fff_file, MINESWEEPER_SETTINGS_KEY_FEEDBACK, &f, 1);
  92. if(w > 32) {
  93. w = 32;
  94. }
  95. if(w < 16) {
  96. w = 16;
  97. }
  98. if(h > 32) {
  99. h = 32;
  100. }
  101. if(h < 7) {
  102. h = 7;
  103. }
  104. if(d > 2) {
  105. d = 2;
  106. }
  107. if(f > 1) {
  108. f = 1;
  109. }
  110. app->settings_info.board_width = (uint8_t)w;
  111. app->settings_info.board_height = (uint8_t)h;
  112. app->settings_info.difficulty = (uint8_t)d;
  113. app->feedback_enabled = (uint8_t)f;
  114. flipper_format_rewind(fff_file);
  115. mine_sweeper_close_config_file(fff_file);
  116. mine_sweeper_close_storage();
  117. return true;
  118. }