Skip to content

Commit

Permalink
Fixed crash when no extra levels
Browse files Browse the repository at this point in the history
  • Loading branch information
dlvoy committed Jan 4, 2024
1 parent 460dd03 commit ce0280e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 30 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

# 1.0.1 - 2024-01-04

## Fixed

- Crash on game continue when no extra levels were added to SD Card

# 1.0.0 - 2024-01-04

## Added
Expand Down
2 changes: 1 addition & 1 deletion application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ App(
fap_icon_assets="images",
fap_file_assets="assets",
fap_author="Dominik Dzienia",
fap_version="1.0.0",
fap_version="1.0.1",
)
68 changes: 39 additions & 29 deletions load.c
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,11 @@ void delete_progress(LevelScore* scores) {

void init_level_list(LevelList* ls, int capacity) {
ls->count = capacity;
ls->ids = malloc(sizeof(FuriString*) * capacity);
if(capacity > 0) {
ls->ids = malloc(sizeof(FuriString*) * capacity);
} else {
ls->ids = NULL;
}
}

//-----------------------------------------------------------------------------
Expand All @@ -744,6 +748,7 @@ void free_level_list(LevelList* ls) {
furi_string_free(ls->ids[i]);
}
free(ls->ids);
ls->ids = NULL;
}
}

Expand Down Expand Up @@ -773,37 +778,42 @@ void list_extra_levels(Storage* storage, LevelList* levelList) {
furi_string_free(levelId);
}
} while(fileFound);
init_level_list(levelList, levelCount);

storage_dir_close(file);
storage_file_free(file);
file = storage_file_alloc(storage);
storage_dir_open(file, MY_APP_DATA_PATH("extra_levels"));

do {
memset(buf, 0, bufSize);
fileFound = storage_dir_read(file, &fileinfo, buf, bufSize);
if(fileFound) {
levelId = furi_string_alloc_set(buf);
if(furi_string_end_with_str(levelId, ".vxl")) {
furi_string_left(levelId, furi_string_size(levelId) - 4);
memset(buf2, 0, bufSize);
level_set_id_to_error_path(levelId, bufSize, buf2);
if(!storage_common_exists(storage, buf2)) {
FURI_LOG_D(TAG, "EXTRA LEVEL FILE \"%s\"", furi_string_get_cstr(levelId));
levelList->ids[levelNo] = furi_string_alloc_set(levelId);
levelNo++;
} else {
FURI_LOG_W(
TAG,
"CANNOT LOAD LEVEL \"%s\" - error file exists at %s",
furi_string_get_cstr(levelId),
buf2);
if(levelCount > 0) {
init_level_list(levelList, levelCount);

storage_dir_close(file);
storage_file_free(file);

file = storage_file_alloc(storage);
storage_dir_open(file, MY_APP_DATA_PATH("extra_levels"));

do {
memset(buf, 0, bufSize);
fileFound = storage_dir_read(file, &fileinfo, buf, bufSize);
if(fileFound) {
levelId = furi_string_alloc_set(buf);
if(furi_string_end_with_str(levelId, ".vxl")) {
furi_string_left(levelId, furi_string_size(levelId) - 4);
memset(buf2, 0, bufSize);
level_set_id_to_error_path(levelId, bufSize, buf2);
if(!storage_common_exists(storage, buf2)) {
FURI_LOG_D(
TAG, "EXTRA LEVEL FILE \"%s\"", furi_string_get_cstr(levelId));
levelList->ids[levelNo] = furi_string_alloc_set(levelId);
levelNo++;
} else {
FURI_LOG_W(
TAG,
"CANNOT LOAD LEVEL \"%s\" - error file exists at %s",
furi_string_get_cstr(levelId),
buf2);
}
}
furi_string_free(levelId);
}
furi_string_free(levelId);
}
} while(fileFound);
} while(fileFound);
}
}

storage_dir_close(file);
Expand Down

0 comments on commit ce0280e

Please sign in to comment.