From 7776c360486e4b08e66c27f8a547cbdf92cfec95 Mon Sep 17 00:00:00 2001 From: JuanJakobo Date: Tue, 1 Nov 2022 15:42:46 +0100 Subject: [PATCH 1/3] Snake Plugin: Formating clang-format --- .../snake_game/helpers/snake_file_handler.c | 56 ++++++++++--------- applications/plugins/snake_game/snake_game.c | 14 +++-- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/applications/plugins/snake_game/helpers/snake_file_handler.c b/applications/plugins/snake_game/helpers/snake_file_handler.c index 79405cbf4..3a1ea7455 100644 --- a/applications/plugins/snake_game/helpers/snake_file_handler.c +++ b/applications/plugins/snake_game/helpers/snake_file_handler.c @@ -4,7 +4,7 @@ #include static void snake_game_close_file(FlipperFormat* file) { - if(file == NULL){ + if(file == NULL) { furi_record_close(RECORD_STORAGE); return; } @@ -28,7 +28,8 @@ static FlipperFormat* snake_game_open_file() { return NULL; } - flipper_format_write_header_cstr(file, SNAKE_GAME_FILE_HEADER, SNAKE_GAME_FILE_ACTUAL_VERSION); + flipper_format_write_header_cstr( + file, SNAKE_GAME_FILE_HEADER, SNAKE_GAME_FILE_ACTUAL_VERSION); flipper_format_rewind(file); } return file; @@ -37,7 +38,7 @@ static FlipperFormat* snake_game_open_file() { void snake_game_save_score_to_file(int16_t highscore) { FlipperFormat* file = snake_game_open_file(); uint32_t temp = highscore; - if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_HIGHSCORE,&temp, 1)){ + if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &temp, 1)) { snake_game_close_file(file); return; } @@ -48,41 +49,45 @@ void snake_game_save_game_to_file(SnakeState* const snake_state) { FlipperFormat* file = snake_game_open_file(); uint32_t temp = snake_state->len; - if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_KEY_LEN,&temp, 1)){ + if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_KEY_LEN, &temp, 1)) { snake_game_close_file(file); return; } uint16_t array_size = snake_state->len * 2; uint32_t temp_array[array_size]; - for(int16_t i = 0, a = 0; a < array_size && i < snake_state->len; i++){ + for(int16_t i = 0, a = 0; a < array_size && i < snake_state->len; i++) { temp_array[a++] = snake_state->points[i].x; temp_array[a++] = snake_state->points[i].y; } - if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_KEY_POINTS, temp_array, array_size)){ - snake_game_close_file(file); - return; + if(!flipper_format_insert_or_update_uint32( + file, SNAKE_GAME_CONFIG_KEY_POINTS, temp_array, array_size)) { + snake_game_close_file(file); + return; } temp = snake_state->currentMovement; - if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT,&temp, 1)){ - snake_game_close_file(file); - return; + if(!flipper_format_insert_or_update_uint32( + file, SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT, &temp, 1)) { + snake_game_close_file(file); + return; } temp = snake_state->nextMovement; - if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT,&temp, 1)){ - snake_game_close_file(file); - return; + if(!flipper_format_insert_or_update_uint32( + file, SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT, &temp, 1)) { + snake_game_close_file(file); + return; } array_size = 2; uint32_t temp_point_array[array_size]; temp_point_array[0] = snake_state->fruit.x; temp_point_array[1] = snake_state->fruit.y; - if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS, temp_point_array, array_size)){ - snake_game_close_file(file); - return; + if(!flipper_format_insert_or_update_uint32( + file, SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS, temp_point_array, array_size)) { + snake_game_close_file(file); + return; } snake_game_close_file(file); @@ -100,12 +105,12 @@ bool snake_game_init_game_from_file(SnakeState* const snake_state) { } furi_string_free(file_type); - uint32_t temp; - snake_state->highscore = (flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &temp, 1)) ? temp : 0; + snake_state->highscore = + (flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &temp, 1)) ? temp : 0; flipper_format_rewind(file); - if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_LEN, &temp, 1)){ + if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_LEN, &temp, 1)) { snake_game_close_file(file); return false; } @@ -114,25 +119,25 @@ bool snake_game_init_game_from_file(SnakeState* const snake_state) { uint16_t array_size = snake_state->len * 2; uint32_t temp_array[array_size]; - if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_POINTS, temp_array, array_size)){ + if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_POINTS, temp_array, array_size)) { snake_game_close_file(file); return false; } - for(int16_t i = 0, a = 0; a < array_size && i < snake_state->len; i++){ + for(int16_t i = 0, a = 0; a < array_size && i < snake_state->len; i++) { snake_state->points[i].x = temp_array[a++]; snake_state->points[i].y = temp_array[a++]; } flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_POINTS); - if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT, &temp, 1)){ + if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT, &temp, 1)) { snake_game_close_file(file); return false; } snake_state->currentMovement = temp; flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT); - if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT, &temp, 1)){ + if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT, &temp, 1)) { snake_game_close_file(file); return false; } @@ -141,7 +146,8 @@ bool snake_game_init_game_from_file(SnakeState* const snake_state) { array_size = 2; uint32_t temp_point_array[array_size]; - if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS, temp_point_array, array_size)){ + if(!flipper_format_read_uint32( + file, SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS, temp_point_array, array_size)) { snake_game_close_file(file); return false; } diff --git a/applications/plugins/snake_game/snake_game.c b/applications/plugins/snake_game/snake_game.c index 5658c1ac1..623ccea10 100644 --- a/applications/plugins/snake_game/snake_game.c +++ b/applications/plugins/snake_game/snake_game.c @@ -224,14 +224,13 @@ static void snake_game_move_snake(SnakeState* const snake_state, Point const nex static void snake_game_game_over(SnakeState* const snake_state, NotificationApp* notification) { snake_state->state = GameStateGameOver; - snake_state->len = snake_state->len -7; - if(snake_state->len > snake_state->highscore){ + snake_state->len = snake_state->len - 7; + if(snake_state->len > snake_state->highscore) { snake_state->isNewHighscore = true; snake_state->highscore = snake_state->len; } notification_message_block(notification, &sequence_fail); - } static void @@ -294,8 +293,9 @@ int32_t snake_game_app(void* p) { SnakeState* snake_state = malloc(sizeof(SnakeState)); snake_state->isNewHighscore = false; snake_state->highscore = 0; - if(!snake_game_init_game_from_file(snake_state)) + if(!snake_game_init_game_from_file(snake_state)) { snake_game_init_game(snake_state); + } ValueMutex state_mutex; if(!init_mutex(&state_mutex, snake_state, sizeof(SnakeState))) { @@ -350,8 +350,9 @@ int32_t snake_game_app(void* p) { } break; case InputKeyBack: - if(snake_state->state == GameStateLife) + if(snake_state->state == GameStateLife) { snake_game_save_game_to_file(snake_state); + } processing = false; break; } @@ -367,8 +368,9 @@ int32_t snake_game_app(void* p) { release_mutex(&state_mutex, snake_state); } - if(snake_state->isNewHighscore) + if(snake_state->isNewHighscore) { snake_game_save_score_to_file(snake_state->highscore); + } // Wait for all notifications to be played and return backlight to normal state notification_message_block(notification, &sequence_display_backlight_enforce_auto); From 86c81086989aa0c251b986967f5acf401eae05b2 Mon Sep 17 00:00:00 2001 From: JuanJakobo Date: Mon, 14 Nov 2022 09:59:33 +0100 Subject: [PATCH 2/3] Snake Plugin: move storage to /ext/apps_data/snake_game --- .../plugins/snake_game/helpers/snake_file_handler.c | 11 +++++++++++ .../plugins/snake_game/helpers/snake_file_handler.h | 4 +++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/applications/plugins/snake_game/helpers/snake_file_handler.c b/applications/plugins/snake_game/helpers/snake_file_handler.c index 3a1ea7455..a60463c17 100644 --- a/applications/plugins/snake_game/helpers/snake_file_handler.c +++ b/applications/plugins/snake_game/helpers/snake_file_handler.c @@ -23,6 +23,17 @@ static FlipperFormat* snake_game_open_file() { return NULL; } } else { + if(storage_common_stat(storage, APPS_DATA, NULL) == FSE_NOT_EXIST) { + if(!storage_simply_mkdir(storage, APPS_DATA)) { + return NULL; + } + } + if(storage_common_stat(storage, SNAKE_GAME_FILE_DIR_PATH, NULL) == FSE_NOT_EXIST) { + if(!storage_simply_mkdir(storage, SNAKE_GAME_FILE_DIR_PATH)) { + return NULL; + } + } + if(!flipper_format_file_open_new(file, SNAKE_GAME_FILE_PATH)) { snake_game_close_file(file); return NULL; diff --git a/applications/plugins/snake_game/helpers/snake_file_handler.h b/applications/plugins/snake_game/helpers/snake_file_handler.h index 25cda4506..586d9df94 100644 --- a/applications/plugins/snake_game/helpers/snake_file_handler.h +++ b/applications/plugins/snake_game/helpers/snake_file_handler.h @@ -4,7 +4,9 @@ #include #include -#define SNAKE_GAME_FILE_PATH "/ext/apps/games/.snake" +#define APPS_DATA EXT_PATH("apps_data") +#define SNAKE_GAME_FILE_DIR_PATH APPS_DATA "/snake_game" +#define SNAKE_GAME_FILE_PATH SNAKE_GAME_FILE_DIR_PATH "/.snake" #define SNAKE_GAME_FILE_HEADER "Flipper Snake plugin run file" #define SNAKE_GAME_FILE_ACTUAL_VERSION 1 From 46170549ca9dc21767ef1442ec92b7abebd7e07b Mon Sep 17 00:00:00 2001 From: JuanJakobo Date: Mon, 14 Nov 2022 10:02:59 +0100 Subject: [PATCH 3/3] Snake Plugin: Check if File could be opened, if not return --- .../snake_game/helpers/snake_file_handler.c | 207 +++++++++--------- .../snake_game/helpers/snake_file_handler.h | 4 +- 2 files changed, 110 insertions(+), 101 deletions(-) diff --git a/applications/plugins/snake_game/helpers/snake_file_handler.c b/applications/plugins/snake_game/helpers/snake_file_handler.c index a60463c17..569bd8738 100644 --- a/applications/plugins/snake_game/helpers/snake_file_handler.c +++ b/applications/plugins/snake_game/helpers/snake_file_handler.c @@ -48,125 +48,134 @@ static FlipperFormat* snake_game_open_file() { void snake_game_save_score_to_file(int16_t highscore) { FlipperFormat* file = snake_game_open_file(); - uint32_t temp = highscore; - if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &temp, 1)) { + if(file != NULL) { + uint32_t temp = highscore; + if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &temp, 1)) { + snake_game_close_file(file); + return; + } snake_game_close_file(file); - return; } - snake_game_close_file(file); } void snake_game_save_game_to_file(SnakeState* const snake_state) { FlipperFormat* file = snake_game_open_file(); - uint32_t temp = snake_state->len; - if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_KEY_LEN, &temp, 1)) { - snake_game_close_file(file); - return; - } + if(file != NULL) { + uint32_t temp = snake_state->len; + if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_KEY_LEN, &temp, 1)) { + snake_game_close_file(file); + return; + } - uint16_t array_size = snake_state->len * 2; - uint32_t temp_array[array_size]; - for(int16_t i = 0, a = 0; a < array_size && i < snake_state->len; i++) { - temp_array[a++] = snake_state->points[i].x; - temp_array[a++] = snake_state->points[i].y; - } - if(!flipper_format_insert_or_update_uint32( - file, SNAKE_GAME_CONFIG_KEY_POINTS, temp_array, array_size)) { - snake_game_close_file(file); - return; - } + uint16_t array_size = snake_state->len * 2; + uint32_t temp_array[array_size]; + for(int16_t i = 0, a = 0; a < array_size && i < snake_state->len; i++) { + temp_array[a++] = snake_state->points[i].x; + temp_array[a++] = snake_state->points[i].y; + } + if(!flipper_format_insert_or_update_uint32( + file, SNAKE_GAME_CONFIG_KEY_POINTS, temp_array, array_size)) { + snake_game_close_file(file); + return; + } - temp = snake_state->currentMovement; - if(!flipper_format_insert_or_update_uint32( - file, SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT, &temp, 1)) { - snake_game_close_file(file); - return; - } + temp = snake_state->currentMovement; + if(!flipper_format_insert_or_update_uint32( + file, SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT, &temp, 1)) { + snake_game_close_file(file); + return; + } - temp = snake_state->nextMovement; - if(!flipper_format_insert_or_update_uint32( - file, SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT, &temp, 1)) { - snake_game_close_file(file); - return; - } + temp = snake_state->nextMovement; + if(!flipper_format_insert_or_update_uint32( + file, SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT, &temp, 1)) { + snake_game_close_file(file); + return; + } - array_size = 2; - uint32_t temp_point_array[array_size]; - temp_point_array[0] = snake_state->fruit.x; - temp_point_array[1] = snake_state->fruit.y; - if(!flipper_format_insert_or_update_uint32( - file, SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS, temp_point_array, array_size)) { - snake_game_close_file(file); - return; - } + array_size = 2; + uint32_t temp_point_array[array_size]; + temp_point_array[0] = snake_state->fruit.x; + temp_point_array[1] = snake_state->fruit.y; + if(!flipper_format_insert_or_update_uint32( + file, SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS, temp_point_array, array_size)) { + snake_game_close_file(file); + return; + } - snake_game_close_file(file); + snake_game_close_file(file); + } } bool snake_game_init_game_from_file(SnakeState* const snake_state) { FlipperFormat* file = snake_game_open_file(); - FuriString* file_type = furi_string_alloc(); - uint32_t version = 1; - if(!flipper_format_read_header(file, file_type, &version)) { + if(file != NULL) { + FuriString* file_type = furi_string_alloc(); + uint32_t version = 1; + if(!flipper_format_read_header(file, file_type, &version)) { + furi_string_free(file_type); + snake_game_close_file(file); + return false; + } furi_string_free(file_type); + + uint32_t temp; + snake_state->highscore = + (flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &temp, 1)) ? temp : 0; + flipper_format_rewind(file); + + if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_LEN, &temp, 1)) { + snake_game_close_file(file); + return false; + } + snake_state->len = temp; + flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_LEN); + + uint16_t array_size = snake_state->len * 2; + uint32_t temp_array[array_size]; + if(!flipper_format_read_uint32( + file, SNAKE_GAME_CONFIG_KEY_POINTS, temp_array, array_size)) { + snake_game_close_file(file); + return false; + } + + for(int16_t i = 0, a = 0; a < array_size && i < snake_state->len; i++) { + snake_state->points[i].x = temp_array[a++]; + snake_state->points[i].y = temp_array[a++]; + } + flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_POINTS); + + if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT, &temp, 1)) { + snake_game_close_file(file); + return false; + } + snake_state->currentMovement = temp; + flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT); + + if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT, &temp, 1)) { + snake_game_close_file(file); + return false; + } + snake_state->nextMovement = temp; + flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT); + + array_size = 2; + uint32_t temp_point_array[array_size]; + if(!flipper_format_read_uint32( + file, SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS, temp_point_array, array_size)) { + snake_game_close_file(file); + return false; + } + snake_state->fruit.x = temp_point_array[0]; + snake_state->fruit.y = temp_point_array[1]; + flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS); + snake_game_close_file(file); - return false; - } - furi_string_free(file_type); - uint32_t temp; - snake_state->highscore = - (flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &temp, 1)) ? temp : 0; - flipper_format_rewind(file); - - if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_LEN, &temp, 1)) { - snake_game_close_file(file); - return false; - } - snake_state->len = temp; - flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_LEN); - - uint16_t array_size = snake_state->len * 2; - uint32_t temp_array[array_size]; - if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_POINTS, temp_array, array_size)) { - snake_game_close_file(file); - return false; + return true; } - for(int16_t i = 0, a = 0; a < array_size && i < snake_state->len; i++) { - snake_state->points[i].x = temp_array[a++]; - snake_state->points[i].y = temp_array[a++]; - } - flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_POINTS); - - if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT, &temp, 1)) { - snake_game_close_file(file); - return false; - } - snake_state->currentMovement = temp; - flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT); - - if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT, &temp, 1)) { - snake_game_close_file(file); - return false; - } - snake_state->nextMovement = temp; - flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT); - - array_size = 2; - uint32_t temp_point_array[array_size]; - if(!flipper_format_read_uint32( - file, SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS, temp_point_array, array_size)) { - snake_game_close_file(file); - return false; - } - snake_state->fruit.x = temp_point_array[0]; - snake_state->fruit.y = temp_point_array[1]; - flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS); - - snake_game_close_file(file); - - return true; + return false; } diff --git a/applications/plugins/snake_game/helpers/snake_file_handler.h b/applications/plugins/snake_game/helpers/snake_file_handler.h index 586d9df94..178d5d8e0 100644 --- a/applications/plugins/snake_game/helpers/snake_file_handler.h +++ b/applications/plugins/snake_game/helpers/snake_file_handler.h @@ -4,8 +4,8 @@ #include #include -#define APPS_DATA EXT_PATH("apps_data") -#define SNAKE_GAME_FILE_DIR_PATH APPS_DATA "/snake_game" +#define APPS_DATA EXT_PATH("apps_data") +#define SNAKE_GAME_FILE_DIR_PATH APPS_DATA "/snake_game" #define SNAKE_GAME_FILE_PATH SNAKE_GAME_FILE_DIR_PATH "/.snake" #define SNAKE_GAME_FILE_HEADER "Flipper Snake plugin run file"