From a59b8f4056dd5fd9bb64293e40b819db6f196c5f Mon Sep 17 00:00:00 2001 From: Steveice10 Date: Wed, 26 Jul 2017 18:48:34 -0700 Subject: [PATCH] Fix potential crash when removing a TitleDB entry to replace with a newer version, add loading indicator to TitleDB list. --- source/ui/section/task/listtitledb.c | 26 ++++++++++++++++++++++++-- source/ui/section/task/task.h | 1 + source/ui/section/titledb.c | 11 ++++++++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/source/ui/section/task/listtitledb.c b/source/ui/section/task/listtitledb.c index b7785af..b526d26 100644 --- a/source/ui/section/task/listtitledb.c +++ b/source/ui/section/task/listtitledb.c @@ -68,6 +68,9 @@ static void task_populate_titledb_thread(void* arg) { json_value* json = json_parse(text, textSize); if(json != NULL) { if(json->type == json_array) { + linked_list titles; + linked_list_init(&titles); + for(u32 i = 0; i < json->u.array.length && R_SUCCEEDED(res); i++) { svcWaitSynchronization(task_get_pause_event(), U64_MAX); if(task_is_quit_all() || svcWaitSynchronization(data->cancelEvent, 0) == 0) { @@ -123,7 +126,7 @@ static void task_populate_titledb_thread(void* arg) { task_populate_titledb_update_status(item); linked_list_iter iter; - linked_list_iterate(data->items, &iter); + linked_list_iterate(&titles, &iter); bool add = true; while(linked_list_iter_has_next(&iter)) { @@ -143,7 +146,7 @@ static void task_populate_titledb_thread(void* arg) { } if(add) { - linked_list_add_sorted(data->items, item, NULL, task_populate_titledb_compare); + linked_list_add_sorted(&titles, item, NULL, task_populate_titledb_compare); } else { task_free_titledb(item); } @@ -157,6 +160,21 @@ static void task_populate_titledb_thread(void* arg) { } } } + + linked_list_iter iter; + linked_list_iterate(&titles, &iter); + + while(linked_list_iter_has_next(&iter)) { + list_item* item = linked_list_iter_next(&iter); + + if(R_SUCCEEDED(res)) { + linked_list_add(data->items, item); + } else { + task_free_titledb(item); + } + } + + linked_list_destroy(&titles); } else { res = R_FBI_BAD_DATA; } @@ -170,6 +188,8 @@ static void task_populate_titledb_thread(void* arg) { res = R_FBI_OUT_OF_MEMORY; } + data->itemsListed = true; + if(R_SUCCEEDED(res)) { linked_list_iter iter; linked_list_iterate(data->items, &iter); @@ -242,6 +262,7 @@ Result task_populate_titledb(populate_titledb_data* data) { task_clear_titledb(data->items); + data->itemsListed = false; data->finished = false; data->result = 0; data->cancelEvent = 0; @@ -254,6 +275,7 @@ Result task_populate_titledb(populate_titledb_data* data) { } if(R_FAILED(res)) { + data->itemsListed = true; data->finished = true; if(data->cancelEvent != 0) { diff --git a/source/ui/section/task/task.h b/source/ui/section/task/task.h index c440570..aa9a528 100644 --- a/source/ui/section/task/task.h +++ b/source/ui/section/task/task.h @@ -217,6 +217,7 @@ typedef struct populate_titles_data_s { typedef struct populate_titledb_data_s { linked_list* items; + volatile bool itemsListed; volatile bool finished; Result result; Handle cancelEvent; diff --git a/source/ui/section/titledb.c b/source/ui/section/titledb.c index 424d6cf..4947e3a 100644 --- a/source/ui/section/titledb.c +++ b/source/ui/section/titledb.c @@ -80,7 +80,16 @@ static void titledb_action_open(linked_list* items, list_item* selected) { } static void titledb_draw_top(ui_view* view, void* data, float x1, float y1, float x2, float y2, list_item* selected) { - if(selected != NULL && selected->data != NULL) { + titledb_data* listData = (titledb_data*) data; + + if(!listData->populateData.itemsListed) { + static const char* text = "Loading title list, please wait...\nNOTE: Cancelling may take up to 15 seconds."; + + float textWidth; + float textHeight; + screen_get_string_size(&textWidth, &textHeight, text, 0.5f, 0.5f); + screen_draw_string(text, x1 + (x2 - x1 - textWidth) / 2, y1 + (y2 - y1 - textHeight) / 2, 0.5f, 0.5f, COLOR_TEXT, true); + } else if(selected != NULL && selected->data != NULL) { ui_draw_titledb_info(view, selected->data, x1, y1, x2, y2); } }