mine_sweeper_storage.c 5.0 KB

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