diff options
author | Jeroen Bakker <jeroen@blender.org> | 2021-09-13 15:13:58 +0300 |
---|---|---|
committer | Jeroen Bakker <jeroen@blender.org> | 2021-09-13 15:13:58 +0300 |
commit | 4b06420e65040c642d2b0a7a1c9bf7515d3cec0c (patch) | |
tree | 6c8ac7246c72a1f37d023b7c5eafcec4d1e23959 /source/blender/blenkernel/intern | |
parent | 603ae580ce557eb62cb323302dfcc0be265e7ea7 (diff) |
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.
Diffstat (limited to 'source/blender/blenkernel/intern')
-rw-r--r-- | source/blender/blenkernel/intern/packedFile.c | 81 |
1 files changed, 42 insertions, 39 deletions
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; |