Fix crash when deleting/renaming asset library while it's visible

Storing the asset library reference by name wasn't a good idea, I thought it
would work with a careful fallback, but it's easier to just use the index
instead. So change to using indices, make sure fallback methods work reliable
and make sure the file list is updated when asset libraries are removed.

I added a new notifier type for the latter, I prefer not using file notifiers
in asset-library/preferences code. We have more than enough values for
notifiers left.
This commit is contained in:
Julian Eisel 2020-12-15 16:58:30 +01:00
parent 7dc8db7cd1
commit 990406e1ff
7 changed files with 49 additions and 31 deletions

View File

@ -61,6 +61,7 @@
#include "BKE_lib_id.h"
#include "BKE_main.h"
#include "BKE_main_idmap.h"
#include "BKE_preferences.h"
#include "BLO_readfile.h"
#include "DNA_asset_types.h"
@ -1050,7 +1051,11 @@ static bool filelist_compare_asset_libraries(const FileSelectAssetLibraryUID *li
return false;
}
if (library_a->type == FILE_ASSET_LIBRARY_CUSTOM) {
return STREQ(library_a->custom_library_identifier, library_b->custom_library_identifier);
/* Don't only check the index, also check that it's valid. */
bUserAssetLibrary *library_ptr_a = BKE_preferences_asset_library_find_from_index(
&U, library_a->custom_library_index);
return (library_ptr_a != NULL) &&
(library_a->custom_library_index == library_b->custom_library_index);
}
return true;

View File

@ -119,6 +119,7 @@ static void fileselect_ensure_updated_asset_params(SpaceFile *sfile)
"FileAssetSelectParams");
asset_params->base_params.details_flags = U_default.file_space_data.details_flags;
asset_params->asset_library.type = FILE_ASSET_LIBRARY_LOCAL;
asset_params->asset_library.custom_library_index = -1;
}
FileSelectParams *base_params = &asset_params->base_params;
@ -419,8 +420,10 @@ static void fileselect_refresh_asset_params(FileAssetSelectParams *asset_params)
/* Ensure valid repo, or fall-back to local one. */
if (library->type == FILE_ASSET_LIBRARY_CUSTOM) {
user_library = BKE_preferences_asset_library_find_from_name(
&U, library->custom_library_identifier);
BLI_assert(library->custom_library_index >= 0);
user_library = BKE_preferences_asset_library_find_from_index(&U,
library->custom_library_index);
if (!user_library) {
library->type = FILE_ASSET_LIBRARY_LOCAL;
}

View File

@ -412,6 +412,11 @@ static void file_listener(wmWindow *UNUSED(win),
ED_area_tag_refresh(area);
}
break;
case ND_SPACE_ASSET_PARAMS:
if (sfile->browse_mode == FILE_BROWSE_MODE_ASSETS) {
ED_area_tag_refresh(area);
}
break;
}
break;
case NC_ASSET: {

View File

@ -170,6 +170,8 @@ static int preferences_asset_library_remove_exec(bContext *UNUSED(C), wmOperator
if (library) {
BKE_preferences_asset_library_remove(&U, library);
U.runtime.is_dirty = true;
/* Trigger refresh for the Asset Browser. */
WM_main_add_notifier(NC_SPACE | ND_SPACE_ASSET_PARAMS, NULL);
}
return OPERATOR_FINISHED;
}

View File

@ -673,12 +673,13 @@ typedef enum eSpaceSeq_OverlayType {
*/
typedef struct FileSelectAssetLibraryUID {
short type;
char _pad[6];
char _pad[2];
/**
* If showing a custom asset library (#FILE_ASSET_LIBRARY_CUSTOM), this name has to be set to
* define which. Can be empty otherwise.
* If showing a custom asset library (#FILE_ASSET_LIBRARY_CUSTOM), this is the index of the
* #bUserAssetLibrary within #UserDef.asset_libraries.
* Should be ignored otherwise (but better set to -1 then, for sanity and debugging).
*/
char custom_library_identifier[64]; /* MAX_NAME */
int custom_library_index;
} FileSelectAssetLibraryUID;
/* Config and Input for File Selector */

View File

@ -2482,11 +2482,10 @@ static int rna_FileAssetSelectParams_asset_library_get(PointerRNA *ptr)
/* Note that the path isn't checked for validity here. If an invalid library path is used, the
* Asset Browser can give a nice hint on what's wrong. */
const bUserAssetLibrary *user_library = BKE_preferences_asset_library_find_from_name(
&U, params->asset_library.custom_library_identifier);
const int index = BKE_preferences_asset_library_get_index(&U, user_library);
if (index > -1) {
return FILE_ASSET_LIBRARY_CUSTOM + index;
const bUserAssetLibrary *user_library = BKE_preferences_asset_library_find_from_index(
&U, params->asset_library.custom_library_index);
if (user_library) {
return FILE_ASSET_LIBRARY_CUSTOM + params->asset_library.custom_library_index;
}
BLI_assert(0);
@ -2500,7 +2499,7 @@ static void rna_FileAssetSelectParams_asset_library_set(PointerRNA *ptr, int val
/* Simple case: Predefined repo, just set the value. */
if (value < FILE_ASSET_LIBRARY_CUSTOM) {
params->asset_library.type = value;
params->asset_library.custom_library_identifier[0] = '\0';
params->asset_library.custom_library_index = -1;
BLI_assert(ELEM(value, FILE_ASSET_LIBRARY_LOCAL));
return;
}
@ -2511,10 +2510,12 @@ static void rna_FileAssetSelectParams_asset_library_set(PointerRNA *ptr, int val
/* Note that the path isn't checked for validity here. If an invalid library path is used, the
* Asset Browser can give a nice hint on what's wrong. */
const bool is_valid = (user_library->name[0] && user_library->path[0]);
if (user_library && is_valid) {
BLI_strncpy(params->asset_library.custom_library_identifier,
user_library->name,
sizeof(params->asset_library.custom_library_identifier));
if (!user_library) {
params->asset_library.type = FILE_ASSET_LIBRARY_LOCAL;
params->asset_library.custom_library_index = -1;
}
else if (user_library && is_valid) {
params->asset_library.custom_library_index = value - FILE_ASSET_LIBRARY_CUSTOM;
params->asset_library.type = FILE_ASSET_LIBRARY_CUSTOM;
}
}

View File

@ -410,20 +410,21 @@ typedef struct wmNotifier {
#define ND_SPACE_IMAGE (4 << 16)
#define ND_SPACE_FILE_PARAMS (5 << 16)
#define ND_SPACE_FILE_LIST (6 << 16)
#define ND_SPACE_NODE (7 << 16)
#define ND_SPACE_OUTLINER (8 << 16)
#define ND_SPACE_VIEW3D (9 << 16)
#define ND_SPACE_PROPERTIES (10 << 16)
#define ND_SPACE_TEXT (11 << 16)
#define ND_SPACE_TIME (12 << 16)
#define ND_SPACE_GRAPH (13 << 16)
#define ND_SPACE_DOPESHEET (14 << 16)
#define ND_SPACE_NLA (15 << 16)
#define ND_SPACE_SEQUENCER (16 << 16)
#define ND_SPACE_NODE_VIEW (17 << 16)
#define ND_SPACE_CHANGED (18 << 16) /*sent to a new editor type after it's replaced an old one*/
#define ND_SPACE_CLIP (19 << 16)
#define ND_SPACE_FILE_PREVIEW (20 << 16)
#define ND_SPACE_ASSET_PARAMS (7 << 16)
#define ND_SPACE_NODE (8 << 16)
#define ND_SPACE_OUTLINER (9 << 16)
#define ND_SPACE_VIEW3D (10 << 16)
#define ND_SPACE_PROPERTIES (11 << 16)
#define ND_SPACE_TEXT (12 << 16)
#define ND_SPACE_TIME (13 << 16)
#define ND_SPACE_GRAPH (14 << 16)
#define ND_SPACE_DOPESHEET (15 << 16)
#define ND_SPACE_NLA (16 << 16)
#define ND_SPACE_SEQUENCER (17 << 16)
#define ND_SPACE_NODE_VIEW (18 << 16)
#define ND_SPACE_CHANGED (19 << 16) /*sent to a new editor type after it's replaced an old one*/
#define ND_SPACE_CLIP (20 << 16)
#define ND_SPACE_FILE_PREVIEW (21 << 16)
/* subtype, 256 entries too */
#define NOTE_SUBTYPE 0x0000FF00