This commit is contained in:
RogueMaster
2022-10-25 00:20:29 -04:00
parent 9691650616
commit a21df918d6
3 changed files with 65 additions and 46 deletions

View File

@@ -4,7 +4,7 @@
#include <flipper_format/flipper_format.h>
static void snake_game_close_file(FlipperFormat* file) {
if(file == NULL){
if(file == NULL) {
furi_record_close(RECORD_STORAGE);
return;
}
@@ -27,7 +27,8 @@ static FlipperFormat* snake_game_open_file(Storage* storage) {
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;
@@ -39,19 +40,19 @@ int16_t snake_game_save_score_to_file(int32_t len) {
len = len - 7;
int32_t temp;
if(!flipper_format_read_int32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &temp, 1)){
if(!flipper_format_insert_or_update_int32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &len, 1)){
if(!flipper_format_read_int32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &temp, 1)) {
if(!flipper_format_insert_or_update_int32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &len, 1)) {
snake_game_close_file(file);
return -1;
}
}else{
if(len > temp){
} else {
if(len > temp) {
flipper_format_rewind(file);
if(!flipper_format_insert_or_update_int32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &len, 1)){
if(!flipper_format_insert_or_update_int32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &len, 1)) {
snake_game_close_file(file);
return -1;
}
}else{
} else {
len = temp;
}
}
@@ -64,41 +65,45 @@ void snake_game_save_game_to_file(SnakeState* const snake_state) {
FlipperFormat* file = snake_game_open_file(storage);
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);
@@ -118,7 +123,7 @@ bool snake_game_init_game_from_file(SnakeState* const snake_state) {
furi_string_free(file_type);
uint32_t temp;
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;
}
@@ -127,35 +132,35 @@ 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;
}
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)){
if(!flipper_format_read_uint32(
file, SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS, temp_point_array, array_size)) {
snake_game_close_file(file);
return false;
}

View File

@@ -233,7 +233,6 @@ static void
return;
}
snake_state->currentMovement = snake_game_get_turn_snake(snake_state);
Point next_step = snake_game_get_next_step(snake_state);
@@ -289,8 +288,7 @@ int32_t snake_game_app(void* p) {
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(SnakeEvent));
SnakeState* snake_state = malloc(sizeof(SnakeState));
if(!snake_game_init_game_from_file(snake_state))
snake_game_init_game(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))) {

View File

@@ -23,7 +23,7 @@
static void render_callback(Canvas* const canvas, void* ctx) {
PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25);
if (plugin_state != NULL && !plugin_state->changing_scene) {
if(plugin_state != NULL && !plugin_state->changing_scene) {
totp_scene_director_render(canvas, plugin_state);
}
@@ -45,29 +45,43 @@ static bool totp_state_init(PluginState* const plugin_state) {
totp_scene_director_init_scenes(plugin_state);
if (plugin_state->crypto_verify_data == NULL) {
if(plugin_state->crypto_verify_data == NULL) {
DialogMessage* message = dialog_message_alloc();
dialog_message_set_buttons(message, "No", NULL, "Yes");
dialog_message_set_text(message, "Would you like to setup PIN?", SCREEN_WIDTH_CENTER, SCREEN_HEIGHT_CENTER, AlignCenter, AlignCenter);
dialog_message_set_text(
message,
"Would you like to setup PIN?",
SCREEN_WIDTH_CENTER,
SCREEN_HEIGHT_CENTER,
AlignCenter,
AlignCenter);
DialogMessageButton dialog_result = dialog_message_show(plugin_state->dialogs, message);
dialog_message_free(message);
if (dialog_result == DialogMessageButtonRight) {
if(dialog_result == DialogMessageButtonRight) {
totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL);
} else {
totp_crypto_seed_iv(plugin_state, NULL, 0);
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
}
} else if (plugin_state->pin_set) {
} else if(plugin_state->pin_set) {
totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL);
} else {
totp_crypto_seed_iv(plugin_state, NULL, 0);
if (totp_crypto_verify_key(plugin_state)) {
if(totp_crypto_verify_key(plugin_state)) {
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
} else {
FURI_LOG_E(LOGGING_TAG, "Digital signature verification failed. Looks like conf file was created on another flipper and can't be used on any other");
FURI_LOG_E(
LOGGING_TAG,
"Digital signature verification failed. Looks like conf file was created on another flipper and can't be used on any other");
DialogMessage* message = dialog_message_alloc();
dialog_message_set_buttons(message, "Exit", NULL, NULL);
dialog_message_set_text(message, "Digital signature verification failed", SCREEN_WIDTH_CENTER, SCREEN_HEIGHT_CENTER, AlignCenter, AlignCenter);
dialog_message_set_text(
message,
"Digital signature verification failed",
SCREEN_WIDTH_CENTER,
SCREEN_HEIGHT_CENTER,
AlignCenter,
AlignCenter);
dialog_message_show(plugin_state->dialogs, message);
dialog_message_free(message);
return false;
@@ -88,7 +102,7 @@ static void plugin_state_free(PluginState* plugin_state) {
ListNode* node = plugin_state->tokens_list;
ListNode* tmp;
while (node != NULL) {
while(node != NULL) {
tmp = node->next;
TokenInfo* tokenInfo = node->data;
token_info_free(tokenInfo);
@@ -96,7 +110,7 @@ static void plugin_state_free(PluginState* plugin_state) {
node = tmp;
}
if (plugin_state->crypto_verify_data != NULL) {
if(plugin_state->crypto_verify_data != NULL) {
free(plugin_state->crypto_verify_data);
}
free(plugin_state);
@@ -106,7 +120,7 @@ int32_t totp_app() {
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
PluginState* plugin_state = malloc(sizeof(PluginState));
if (!totp_state_init(plugin_state)) {
if(!totp_state_init(plugin_state)) {
FURI_LOG_E(LOGGING_TAG, "App state initialization failed\r\n");
plugin_state_free(plugin_state);
return 254;
@@ -131,18 +145,20 @@ int32_t totp_app() {
bool processing = true;
uint32_t last_user_interaction_time = furi_get_tick();
while(processing) {
if (plugin_state->changing_scene) continue;
if(plugin_state->changing_scene) continue;
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
PluginState* plugin_state = acquire_mutex_block(&state_mutex);
if(event_status == FuriStatusOk) {
if (event.type == EventTypeKey) {
if(event.type == EventTypeKey) {
last_user_interaction_time = furi_get_tick();
}
processing = totp_scene_director_handle_event(&event, plugin_state);
} else if (plugin_state->pin_set && plugin_state->current_scene != TotpSceneAuthentication && furi_get_tick() - last_user_interaction_time > IDLE_TIMEOUT) {
} else if(
plugin_state->pin_set && plugin_state->current_scene != TotpSceneAuthentication &&
furi_get_tick() - last_user_interaction_time > IDLE_TIMEOUT) {
totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL);
}