[Bf-blender-cvs] [4b06420e650] master: Cleanup: Packedfile don't repeat yourself.

Jeroen Bakker noreply at git.blender.org
Mon Sep 13 14:15:52 CEST 2021


Commit: 4b06420e65040c642d2b0a7a1c9bf7515d3cec0c
Author: Jeroen Bakker
Date:   Mon Sep 13 14:13:58 2021 +0200
Branches: master
https://developer.blender.org/rB4b06420e65040c642d2b0a7a1c9bf7515d3cec0c

Cleanup: Packedfile don't repeat yourself.

Introduced `BKE_packedfile_unpack` that is called from the specialized
implementation for Image, Sound, Font, Volume etc. This is in
preparation for T91252.

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

M	source/blender/blenkernel/BKE_packedFile.h
M	source/blender/blenkernel/intern/packedFile.c

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

diff --git a/source/blender/blenkernel/BKE_packedFile.h b/source/blender/blenkernel/BKE_packedFile.h
index c45a0bc857d..9b66c6003d7 100644
--- a/source/blender/blenkernel/BKE_packedFile.h
+++ b/source/blender/blenkernel/BKE_packedFile.h
@@ -74,6 +74,12 @@ char *BKE_packedfile_unpack_to_file(struct ReportList *reports,
                                     const char *local_name,
                                     struct PackedFile *pf,
                                     enum ePF_FileStatus how);
+char *BKE_packedfile_unpack(struct Main *bmain,
+                            struct ReportList *reports,
+                            struct ID *id,
+                            const char *orig_file_name,
+                            struct PackedFile *pf,
+                            enum ePF_FileStatus how);
 int BKE_packedfile_unpack_vfont(struct Main *bmain,
                                 struct ReportList *reports,
                                 struct VFont *vfont,
diff --git a/source/blender/blenkernel/intern/packedFile.c b/source/blender/blenkernel/intern/packedFile.c
index 78e7e11c248..baff1bb47cc 100644
--- a/source/blender/blenkernel/intern/packedFile.c
+++ b/source/blender/blenkernel/intern/packedFile.c
@@ -576,26 +576,42 @@ static void unpack_generate_paths(const char *name,
   }
 }
 
+char *BKE_packedfile_unpack(Main *bmain,
+                            ReportList *reports,
+                            ID *id,
+                            const char *orig_file_path,
+                            PackedFile *pf,
+                            enum ePF_FileStatus how)
+{
+  char localname[FILE_MAX], absname[FILE_MAX];
+  char *new_name = NULL;
+
+  if (id != NULL) {
+    unpack_generate_paths(
+        orig_file_path, id, absname, localname, sizeof(absname), sizeof(localname));
+    new_name = BKE_packedfile_unpack_to_file(
+        reports, BKE_main_blendfile_path(bmain), absname, localname, pf, how);
+  }
+
+  return new_name;
+}
+
 int BKE_packedfile_unpack_vfont(Main *bmain,
                                 ReportList *reports,
                                 VFont *vfont,
                                 enum ePF_FileStatus how)
 {
-  char localname[FILE_MAX], absname[FILE_MAX];
-  char *newname;
   int ret_value = RET_ERROR;
+  if (vfont) {
+    char *new_file_path = BKE_packedfile_unpack(
+        bmain, reports, (ID *)vfont, vfont->filepath, vfont->packedfile, how);
 
-  if (vfont != NULL) {
-    unpack_generate_paths(
-        vfont->filepath, (ID *)vfont, absname, localname, sizeof(absname), sizeof(localname));
-    newname = BKE_packedfile_unpack_to_file(
-        reports, BKE_main_blendfile_path(bmain), absname, localname, vfont->packedfile, how);
-    if (newname != NULL) {
+    if (new_file_path != NULL) {
       ret_value = RET_OK;
       BKE_packedfile_free(vfont->packedfile);
       vfont->packedfile = NULL;
-      BLI_strncpy(vfont->filepath, newname, sizeof(vfont->filepath));
-      MEM_freeN(newname);
+      BLI_strncpy(vfont->filepath, new_file_path, sizeof(vfont->filepath));
+      MEM_freeN(new_file_path);
     }
   }
 
@@ -607,18 +623,14 @@ int BKE_packedfile_unpack_sound(Main *bmain,
                                 bSound *sound,
                                 enum ePF_FileStatus how)
 {
-  char localname[FILE_MAX], absname[FILE_MAX];
-  char *newname;
   int ret_value = RET_ERROR;
 
   if (sound != NULL) {
-    unpack_generate_paths(
-        sound->filepath, (ID *)sound, absname, localname, sizeof(absname), sizeof(localname));
-    newname = BKE_packedfile_unpack_to_file(
-        reports, BKE_main_blendfile_path(bmain), absname, localname, sound->packedfile, how);
-    if (newname != NULL) {
-      BLI_strncpy(sound->filepath, newname, sizeof(sound->filepath));
-      MEM_freeN(newname);
+    char *new_file_path = BKE_packedfile_unpack(
+        bmain, reports, (ID *)sound, sound->filepath, sound->packedfile, how);
+    if (new_file_path != NULL) {
+      BLI_strncpy(sound->filepath, new_file_path, sizeof(sound->filepath));
+      MEM_freeN(new_file_path);
 
       BKE_packedfile_free(sound->packedfile);
       sound->packedfile = NULL;
@@ -641,16 +653,11 @@ int BKE_packedfile_unpack_image(Main *bmain,
 
   if (ima != NULL) {
     while (ima->packedfiles.last) {
-      char localname[FILE_MAX], absname[FILE_MAX];
-      char *newname;
       ImagePackedFile *imapf = ima->packedfiles.last;
+      char *new_file_path = BKE_packedfile_unpack(
+          bmain, reports, (ID *)ima, imapf->filepath, imapf->packedfile, how);
 
-      unpack_generate_paths(
-          imapf->filepath, (ID *)ima, absname, localname, sizeof(absname), sizeof(localname));
-      newname = BKE_packedfile_unpack_to_file(
-          reports, BKE_main_blendfile_path(bmain), absname, localname, imapf->packedfile, how);
-
-      if (newname != NULL) {
+      if (new_file_path != NULL) {
         ImageView *iv;
 
         ret_value = ret_value == RET_ERROR ? RET_ERROR : RET_OK;
@@ -660,14 +667,14 @@ int BKE_packedfile_unpack_image(Main *bmain,
         /* update the new corresponding view filepath */
         iv = BLI_findstring(&ima->views, imapf->filepath, offsetof(ImageView, filepath));
         if (iv) {
-          BLI_strncpy(iv->filepath, newname, sizeof(imapf->filepath));
+          BLI_strncpy(iv->filepath, new_file_path, sizeof(imapf->filepath));
         }
 
         /* keep the new name in the image for non-pack specific reasons */
         if (how != PF_REMOVE) {
-          BLI_strncpy(ima->filepath, newname, sizeof(imapf->filepath));
+          BLI_strncpy(ima->filepath, new_file_path, sizeof(imapf->filepath));
         }
-        MEM_freeN(newname);
+        MEM_freeN(new_file_path);
       }
       else {
         ret_value = RET_ERROR;
@@ -690,18 +697,14 @@ int BKE_packedfile_unpack_volume(Main *bmain,
                                  Volume *volume,
                                  enum ePF_FileStatus how)
 {
-  char localname[FILE_MAX], absname[FILE_MAX];
-  char *newfilepath;
   int ret_value = RET_ERROR;
 
   if (volume != NULL) {
-    unpack_generate_paths(
-        volume->filepath, (ID *)volume, absname, localname, sizeof(absname), sizeof(localname));
-    newfilepath = BKE_packedfile_unpack_to_file(
-        reports, BKE_main_blendfile_path(bmain), absname, localname, volume->packedfile, how);
-    if (newfilepath != NULL) {
-      BLI_strncpy(volume->filepath, newfilepath, sizeof(volume->filepath));
-      MEM_freeN(newfilepath);
+    char *new_file_path = BKE_packedfile_unpack(
+        bmain, reports, (ID *)volume, volume->filepath, volume->packedfile, how);
+    if (new_file_path != NULL) {
+      BLI_strncpy(volume->filepath, new_file_path, sizeof(volume->filepath));
+      MEM_freeN(new_file_path);
 
       BKE_packedfile_free(volume->packedfile);
       volume->packedfile = NULL;



More information about the Bf-blender-cvs mailing list