[Bf-blender-cvs] [39c9164ea18] master: File/Asset Browser: Get full asset path from asset representation

Julian Eisel noreply at git.blender.org
Wed Nov 30 19:44:53 CET 2022


Commit: 39c9164ea1839decfa4f6b8930237864e5be6509
Author: Julian Eisel
Date:   Wed Nov 30 19:34:11 2022 +0100
Branches: master
https://developer.blender.org/rB39c9164ea1839decfa4f6b8930237864e5be6509

File/Asset Browser: Get full asset path from asset representation

No user visible changes expected.

Add a function to query the full path for a file, so that asset files
can get the path via the asset representation and its new asset
identifier. This is designed to be a reliable way to locate an asset,
and using it is yet another step to rely less on the problematic file
browser code.
Also, previous code would build the full path manually in a few places,
which is good to deduplicate anyway.

===================================================================

M	source/blender/editors/space_file/file_ops.c
M	source/blender/editors/space_file/filelist.cc
M	source/blender/editors/space_file/filelist.h

===================================================================

diff --git a/source/blender/editors/space_file/file_ops.c b/source/blender/editors/space_file/file_ops.c
index 6d7365fa136..e9a7080ff35 100644
--- a/source/blender/editors/space_file/file_ops.c
+++ b/source/blender/editors/space_file/file_ops.c
@@ -2859,12 +2859,12 @@ static bool file_delete_poll(bContext *C)
   return false;
 }
 
-static bool file_delete_single(const FileSelectParams *params,
+static bool file_delete_single(const struct FileList *files,
                                FileDirEntry *file,
                                const char **r_error_message)
 {
-  char str[FILE_MAX];
-  BLI_path_join(str, sizeof(str), params->dir, file->relpath);
+  char str[FILE_MAX_LIBEXTRA];
+  filelist_file_get_full_path(files, file, str);
   if (BLI_delete_soft(str, r_error_message) != 0 || BLI_exists(str)) {
     return false;
   }
@@ -2876,7 +2876,6 @@ static int file_delete_exec(bContext *C, wmOperator *op)
 {
   wmWindowManager *wm = CTX_wm_manager(C);
   SpaceFile *sfile = CTX_wm_space_file(C);
-  FileSelectParams *params = ED_fileselect_get_active_params(sfile);
   int numfiles = filelist_files_ensure(sfile->files);
 
   const char *error_message = NULL;
@@ -2885,7 +2884,7 @@ static int file_delete_exec(bContext *C, wmOperator *op)
   for (int i = 0; i < numfiles; i++) {
     if (filelist_entry_select_index_get(sfile->files, i, CHECK_ALL)) {
       FileDirEntry *file = filelist_file(sfile->files, i);
-      if (!file_delete_single(params, file, &error_message)) {
+      if (!file_delete_single(sfile->files, file, &error_message)) {
         report_error = true;
       }
     }
diff --git a/source/blender/editors/space_file/filelist.cc b/source/blender/editors/space_file/filelist.cc
index b97f1e27a1a..beb1387b26e 100644
--- a/source/blender/editors/space_file/filelist.cc
+++ b/source/blender/editors/space_file/filelist.cc
@@ -1132,6 +1132,18 @@ void filelist_free_icons(void)
   }
 }
 
+void filelist_file_get_full_path(const FileList *filelist, const FileDirEntry *file, char *r_path)
+{
+  if (file->asset) {
+    const std::string asset_path = AS_asset_representation_full_path_get(file->asset);
+    BLI_strncpy(r_path, asset_path.c_str(), FILE_MAX_LIBEXTRA);
+    return;
+  }
+
+  const char *root = filelist_dir(filelist);
+  BLI_path_join(r_path, FILE_MAX_LIBEXTRA, root, file->relpath);
+}
+
 static FileDirEntry *filelist_geticon_get_file(struct FileList *filelist, const int index)
 {
   BLI_assert(G.background == false);
@@ -1176,8 +1188,8 @@ ImBuf *filelist_geticon_image(struct FileList *filelist, const int index)
   return filelist_geticon_image_ex(file);
 }
 
-static int filelist_geticon_ex(const FileDirEntry *file,
-                               const char *root,
+static int filelist_geticon_ex(const FileList *filelist,
+                               const FileDirEntry *file,
                                const bool is_main,
                                const bool ignore_libdir)
 {
@@ -1215,8 +1227,8 @@ static int filelist_geticon_ex(const FileDirEntry *file,
       if (file->redirection_path) {
         target = file->redirection_path;
       }
-      else if (root) {
-        BLI_path_join(fullpath, sizeof(fullpath), root, file->relpath);
+      else if (filelist) {
+        filelist_file_get_full_path(filelist, file, fullpath);
         BLI_path_slash_ensure(fullpath, sizeof(fullpath));
       }
       for (; tfsm; tfsm = tfsm->next) {
@@ -1296,13 +1308,13 @@ int filelist_geticon(struct FileList *filelist, const int index, const bool is_m
 {
   FileDirEntry *file = filelist_geticon_get_file(filelist, index);
 
-  return filelist_geticon_ex(file, filelist->filelist.root, is_main, false);
+  return filelist_geticon_ex(filelist, file, is_main, false);
 }
 
 int ED_file_icon(const FileDirEntry *file)
 {
   return file->preview_icon_id ? file->preview_icon_id :
-                                 filelist_geticon_ex(file, nullptr, false, false);
+                                 filelist_geticon_ex(nullptr, file, false, false);
 }
 
 static bool filelist_intern_entry_is_main_file(const FileListInternEntry *intern_entry)
@@ -1629,8 +1641,7 @@ static void filelist_cache_previews_push(FileList *filelist, FileDirEntry *entry
       BLI_strncpy(preview->filepath, entry->redirection_path, FILE_MAXDIR);
     }
     else {
-      BLI_path_join(
-          preview->filepath, sizeof(preview->filepath), filelist->filelist.root, entry->relpath);
+      filelist_file_get_full_path(filelist, entry, preview->filepath);
     }
     // printf("%s: %d - %s\n", __func__, preview->index, preview->filepath);
 
@@ -1953,7 +1964,7 @@ static const char *fileentry_uiname(const char *root, FileListInternEntry *entry
   return BLI_strdup(name);
 }
 
-const char *filelist_dir(struct FileList *filelist)
+const char *filelist_dir(const FileList *filelist)
 {
   return filelist->filelist.root;
 }
diff --git a/source/blender/editors/space_file/filelist.h b/source/blender/editors/space_file/filelist.h
index 89831483fdf..e81a8926eaf 100644
--- a/source/blender/editors/space_file/filelist.h
+++ b/source/blender/editors/space_file/filelist.h
@@ -71,6 +71,9 @@ void filelist_setlibrary(struct FileList *filelist,
 
 void filelist_init_icons(void);
 void filelist_free_icons(void);
+void filelist_file_get_full_path(const struct FileList *filelist,
+                                 const FileDirEntry *file,
+                                 char r_path[/*FILE_MAX_LIBEXTRA*/]);
 struct ImBuf *filelist_getimage(struct FileList *filelist, int index);
 struct ImBuf *filelist_file_getimage(const FileDirEntry *file);
 struct ImBuf *filelist_geticon_image_ex(const FileDirEntry *file);
@@ -91,7 +94,7 @@ void filelist_clear_ex(struct FileList *filelist,
 void filelist_clear_from_reset_tag(struct FileList *filelist);
 void filelist_free(struct FileList *filelist);
 
-const char *filelist_dir(struct FileList *filelist);
+const char *filelist_dir(const struct FileList *filelist);
 bool filelist_is_dir(struct FileList *filelist, const char *path);
 /**
  * May modify in place given r_dir, which is expected to be FILE_MAX_LIBEXTRA length.



More information about the Bf-blender-cvs mailing list