Blender V5.3
packedFile.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cstddef>
11#include <cstdio>
12#include <fcntl.h>
13#include <sys/stat.h>
14
15#ifndef WIN32
16# include <unistd.h>
17#else
18# include <io.h>
19#endif
20#include "MEM_guardedalloc.h"
21#include <cstring>
22
24
25#include "DNA_ID.h"
26#include "DNA_image_types.h"
27#include "DNA_modifier_types.h"
29#include "DNA_sound_types.h"
30#include "DNA_vfont_types.h"
31#include "DNA_volume_types.h"
32
33#include "BLI_listbase.hh"
34#include "BLI_path_utils.hh"
35#include "BLI_string.hh"
36#include "BLI_utildefines.hh"
37
40#include "BKE_image.hh"
41#include "BKE_image_format.hh"
42#include "BKE_library.hh"
43#include "BKE_main.hh"
44#include "BKE_packedFile.hh"
45#include "BKE_report.hh"
46#include "BKE_sound.hh"
47#include "BKE_vfont.hh"
48#include "BKE_volume.hh"
49
50#include "DEG_depsgraph.hh"
51
52#include "IMB_imbuf.hh"
53
54#include "BLO_read_write.hh"
55
56#include "CLG_log.h"
57
58namespace blender {
59
60static CLG_LogRef LOG = {"lib.packedfile"};
61
62int BKE_packedfile_seek(PackedFile *pf, int offset, int whence)
63{
64 int oldseek = -1, seek = 0;
65
66 if (pf) {
67 oldseek = pf->seek;
68 switch (whence) {
69 case SEEK_CUR:
70 seek = oldseek + offset;
71 break;
72 case SEEK_END:
73 seek = pf->size + offset;
74 break;
75 case SEEK_SET:
76 seek = offset;
77 break;
78 default:
79 oldseek = -1;
80 break;
81 }
82 if (seek < 0) {
83 seek = 0;
84 }
85 else if (seek > pf->size) {
86 seek = pf->size;
87 }
88 pf->seek = seek;
89 }
90
91 return oldseek;
92}
93
95{
96 BKE_packedfile_seek(pf, 0, SEEK_SET);
97}
98
99int BKE_packedfile_read(PackedFile *pf, void *data, int size)
100{
101 if ((pf != nullptr) && (size >= 0) && (data != nullptr)) {
102 if (size + pf->seek > pf->size) {
103 size = pf->size - pf->seek;
104 }
105
106 if (size > 0) {
107 memcpy(data, (static_cast<const char *>(pf->data)) + pf->seek, size);
108 }
109 else {
110 size = 0;
111 }
112
113 pf->seek += size;
114 }
115 else {
116 size = -1;
117 }
118
119 return size;
120}
121
123{
124 Image *ima;
125 VFont *vf;
126 bSound *sound;
127 Volume *volume;
128
129 PackedFileCount count;
130
131 /* let's check if there are packed files... */
132 for (ima = bmain->images.first(); ima; ima = static_cast<Image *>(ima->id.next)) {
133 if (BKE_image_has_packedfile(ima) && !ID_IS_LINKED(ima)) {
134 count.individual_files++;
135 }
136 }
137
138 for (vf = bmain->fonts.first(); vf; vf = static_cast<VFont *>(vf->id.next)) {
139 if (vf->packedfile && !ID_IS_LINKED(vf)) {
140 count.individual_files++;
141 }
142 }
143
144 for (sound = bmain->sounds.first(); sound; sound = static_cast<bSound *>(sound->id.next)) {
145 if (sound->packedfile && !ID_IS_LINKED(sound)) {
146 count.individual_files++;
147 }
148 }
149
150 for (volume = bmain->volumes.first(); volume; volume = static_cast<Volume *>(volume->id.next)) {
151 if (volume->packedfile && !ID_IS_LINKED(volume)) {
152 count.individual_files++;
153 }
154 }
155
156 for (Object &object : bmain->objects) {
157 if (ID_IS_LINKED(&object)) {
158 continue;
159 }
160 for (ModifierData &md : object.modifiers) {
161 if (md.type == eModifierType_Nodes) {
162 NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(&md);
163 for (const NodesModifierBake &bake : Span{nmd->bakes, nmd->bakes_num}) {
164 if (bake.packed) {
165 count.bakes++;
166 }
167 }
168 }
169 }
170 }
171
172 return count;
173}
174
176{
177 if (pf) {
178 BLI_assert(pf->data != nullptr);
179 BLI_assert(pf->sharing_info != nullptr);
180
181 pf->sharing_info->remove_user_and_delete_if_last();
182 MEM_delete(pf);
183 }
184 else {
185 printf("%s: Trying to free a nullptr pointer\n", __func__);
186 }
187}
188
190{
191 BLI_assert(pf_src != nullptr);
192 BLI_assert(pf_src->data != nullptr);
193
194 PackedFile *pf_dst;
195
196 pf_dst = MEM_dupalloc(pf_src);
197 pf_dst->sharing_info->add_user();
198
199 return pf_dst;
200}
201
203 int memlen,
204 const ImplicitSharingInfo *sharing_info)
205{
206 BLI_assert(mem != nullptr);
207 if (!sharing_info) {
208 /* Assume we are the only owner of that memory currently. */
209 sharing_info = implicit_sharing::info_for_mem_free(static_cast<const std::byte *>(mem));
210 }
211
212 PackedFile *pf = MEM_new<PackedFile>("PackedFile");
213 pf->data = mem;
214 pf->size = memlen;
215 pf->sharing_info = sharing_info;
216
217 return pf;
218}
219
220PackedFile *BKE_packedfile_new(ReportList *reports, const char *filepath_rel, const char *basepath)
221{
222 char filepath[FILE_MAX];
223
224 /* render result has no filepath and can be ignored
225 * any other files with no name can be ignored too */
226 if (filepath_rel[0] == '\0') {
227 return nullptr;
228 }
229
230 // XXX waitcursor(1);
231
232 /* convert relative filenames to absolute filenames */
233
234 STRNCPY(filepath, filepath_rel);
235 BLI_path_abs(filepath, basepath);
236
237 /* open the file
238 * and create a PackedFile structure */
239
240 const int file = BLI_open(filepath, O_BINARY | O_RDONLY, 0);
241 if (file == -1) {
242 BKE_reportf(reports, RPT_ERROR, "Unable to pack file, source path '%s' not found", filepath);
243 return nullptr;
244 }
245
246 PackedFile *pf = nullptr;
247 const size_t file_size = BLI_file_descriptor_size(file);
248 if (file_size == size_t(-1)) {
249 BKE_reportf(reports, RPT_ERROR, "Unable to access the size of, source path '%s'", filepath);
250 }
251 else if (file_size > PACKED_FILE_MAX_SIZE) {
252 BKE_reportf(reports, RPT_ERROR, "Unable to pack files over 2gb, source path '%s'", filepath);
253 }
254 else {
255 /* #MEM_new_uninitialized complains about `MEM_new_uninitialized(0, "...")`,
256 * a single allocation is harmless and doesn't cause any complications. */
257 void *data = MEM_new_uninitialized(std::max(file_size, size_t(1)), "packFile");
258 if (BLI_read(file, data, file_size) == file_size) {
260 }
261 else {
263 }
264 }
265
266 close(file);
267
268 // XXX waitcursor(0);
269
270 return pf;
271}
272
273void BKE_packedfile_pack_all(Main *bmain, ReportList *reports, bool verbose)
274{
275 Image *ima;
276 VFont *vfont;
277 bSound *sound;
278 Volume *volume;
279 int tot = 0;
280
281 for (ima = bmain->images.first(); ima; ima = static_cast<Image *>(ima->id.next)) {
282 if (BKE_image_has_packedfile(ima) == false && ID_IS_EDITABLE(ima)) {
283 if (ELEM(ima->source, IMA_SRC_FILE, IMA_SRC_TILED)) {
284 BKE_image_packfiles(reports, ima, ID_BLEND_PATH(bmain, &ima->id));
285 tot++;
286 }
287 else if (ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE) && verbose) {
288 BKE_reportf(reports,
290 "Image '%s' skipped, packing movies or image sequences not supported",
291 ima->id.name + 2);
292 }
293 }
294 }
295
296 for (vfont = bmain->fonts.first(); vfont; vfont = static_cast<VFont *>(vfont->id.next)) {
297 if (vfont->packedfile == nullptr && ID_IS_EDITABLE(vfont) &&
298 BKE_vfont_is_builtin(vfont) == false)
299 {
301 reports, vfont->filepath, BKE_main_blendfile_path(bmain));
302 tot++;
303 }
304 }
305
306 for (sound = bmain->sounds.first(); sound; sound = static_cast<bSound *>(sound->id.next)) {
307 if (sound->packedfile == nullptr && ID_IS_EDITABLE(sound)) {
309 reports, sound->filepath, BKE_main_blendfile_path(bmain));
310 tot++;
311 }
312 }
313
314 for (volume = bmain->volumes.first(); volume; volume = static_cast<Volume *>(volume->id.next)) {
315 if (volume->packedfile == nullptr && ID_IS_EDITABLE(volume)) {
317 reports, volume->filepath, BKE_main_blendfile_path(bmain));
318 tot++;
319 }
320 }
321
322 for (Object &object : bmain->objects) {
323 if (ID_IS_LINKED(&object)) {
324 continue;
325 }
326 for (ModifierData &md : object.modifiers) {
327 if (md.type == eModifierType_Nodes) {
328 NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(&md);
329 for (NodesModifierBake &bake : MutableSpan{nmd->bakes, nmd->bakes_num}) {
330 bke::bake::pack_geometry_nodes_bake(*bmain, reports, object, *nmd, bake);
331 }
332 }
333 }
334 }
335
336 if (tot > 0) {
337 BKE_reportf(reports, RPT_INFO, "Packed %d file(s)", tot);
338 }
339 else if (verbose) {
340 BKE_report(reports, RPT_INFO, "No new files have been packed");
341 }
342}
343
345 const char *ref_file_name,
346 const char *filepath_rel,
347 PackedFile *pf)
348{
349 int file, number;
350 int ret_value = RET_OK;
351 bool remove_tmp = false;
352 char filepath[FILE_MAX];
353 char filepath_temp[FILE_MAX];
354 // void *data;
355
356 STRNCPY(filepath, filepath_rel);
357 BLI_path_abs(filepath, ref_file_name);
358
359 if (BLI_exists(filepath)) {
360 for (number = 1; number <= 999; number++) {
361 SNPRINTF(filepath_temp, "%s.%03d_", filepath, number);
362 if (!BLI_exists(filepath_temp)) {
363 if (BLI_copy(filepath, filepath_temp) == RET_OK) {
364 remove_tmp = true;
365 }
366 break;
367 }
368 }
369 }
370
372
373 file = BLI_open(filepath, O_BINARY + O_WRONLY + O_CREAT + O_TRUNC, 0666);
374 if (file == -1) {
375 BKE_reportf(reports, RPT_ERROR, "Error creating file '%s'", filepath);
376 ret_value = RET_ERROR;
377 }
378 else {
379 if (write(file, pf->data, pf->size) != pf->size) {
380 BKE_reportf(reports, RPT_ERROR, "Error writing file '%s'", filepath);
381 ret_value = RET_ERROR;
382 }
383 else {
384 BKE_reportf(reports, RPT_INFO, "Saved packed file to: %s", filepath);
385 }
386
387 close(file);
388 }
389
390 if (remove_tmp) {
391 if (ret_value == RET_ERROR) {
392 if (BLI_rename_overwrite(filepath_temp, filepath) != 0) {
393 BKE_reportf(reports,
394 RPT_ERROR,
395 "Error restoring temp file (check files '%s' '%s')",
396 filepath_temp,
397 filepath);
398 }
399 }
400 else {
401 if (BLI_delete(filepath_temp, false, false) != 0) {
402 BKE_reportf(reports, RPT_ERROR, "Error deleting '%s' (ignored)", filepath_temp);
403 }
404 }
405 }
406
407 return ret_value;
408}
409
410enum ePF_FileCompare BKE_packedfile_compare_to_file(const char *ref_file_name,
411 const char *filepath_rel,
412 const PackedFile *pf)
413{
414 BLI_stat_t st;
415 enum ePF_FileCompare ret_val;
416 char buf[4096];
417 char filepath[FILE_MAX];
418
419 STRNCPY(filepath, filepath_rel);
420 BLI_path_abs(filepath, ref_file_name);
421
422 if (BLI_stat(filepath, &st) == -1) {
423 ret_val = PF_CMP_NOFILE;
424 }
425 else if (st.st_size != pf->size) {
426 ret_val = PF_CMP_DIFFERS;
427 }
428 else {
429 /* we'll have to compare the two... */
430
431 const int file = BLI_open(filepath, O_BINARY | O_RDONLY, 0);
432 if (file == -1) {
433 ret_val = PF_CMP_NOFILE;
434 }
435 else {
436 ret_val = PF_CMP_EQUAL;
437
438 for (int i = 0; i < pf->size; i += sizeof(buf)) {
439 int len = pf->size - i;
440 len = std::min<ulong>(len, sizeof(buf));
441
442 if (BLI_read(file, buf, len) != len) {
443 /* read error ... */
444 ret_val = PF_CMP_DIFFERS;
445 break;
446 }
447
448 if (memcmp(buf, (static_cast<const char *>(pf->data)) + i, len) != 0) {
449 ret_val = PF_CMP_DIFFERS;
450 break;
451 }
452 }
453
454 close(file);
455 }
456 }
457
458 return ret_val;
459}
460
462 const char *ref_file_name,
463 const char *abs_name,
464 const char *local_name,
465 PackedFile *pf,
466 enum ePF_FileStatus how)
467{
468 char *newname = nullptr;
469 const char *temp = nullptr;
470
471 if (pf != nullptr) {
472 switch (how) {
473 case PF_KEEP:
474 break;
475 case PF_REMOVE:
476 temp = abs_name;
477 break;
478 case PF_USE_LOCAL: {
479 char temp_abs[FILE_MAX];
480
481 STRNCPY(temp_abs, local_name);
482 BLI_path_abs(temp_abs, ref_file_name);
483
484 /* if file exists use it */
485 if (BLI_exists(temp_abs)) {
486 temp = local_name;
487 break;
488 }
489 /* else create it */
491 }
492 case PF_WRITE_LOCAL:
493 if (BKE_packedfile_write_to_file(reports, ref_file_name, local_name, pf) == RET_OK) {
494 temp = local_name;
495 }
496 break;
497 case PF_USE_ORIGINAL: {
498 char temp_abs[FILE_MAX];
499
500 STRNCPY(temp_abs, abs_name);
501 BLI_path_abs(temp_abs, ref_file_name);
502
503 /* if file exists use it */
504 if (BLI_exists(temp_abs)) {
505 BKE_reportf(reports, RPT_INFO, "Use existing file (instead of packed): %s", abs_name);
506 temp = abs_name;
507 break;
508 }
509 /* else create it */
511 }
513 if (BKE_packedfile_write_to_file(reports, ref_file_name, abs_name, pf) == RET_OK) {
514 temp = abs_name;
515 }
516 break;
517 default:
518 printf("%s: unknown return_value %d\n", __func__, how);
519 break;
520 }
521
522 if (temp) {
523 newname = BLI_strdup(temp);
524 }
525 }
526
527 return newname;
528}
529
530static void unpack_generate_paths(const char *filepath,
531 ID *id,
532 char *r_abspath,
533 size_t abspath_maxncpy,
534 char *r_relpath,
535 size_t relpath_maxncpy)
536{
537 const short id_type = GS(id->name);
538 char temp_filename[FILE_MAX];
539 char temp_dirname[FILE_MAXDIR];
540
542 filepath, temp_dirname, sizeof(temp_dirname), temp_filename, sizeof(temp_filename));
543
544 if (temp_filename[0] == '\0') {
545 /* NOTE: we generally do not have any real way to re-create extension out of data. */
546 const size_t len = STRNCPY_RLEN(temp_filename, id->name + 2);
547 printf("%s\n", temp_filename);
548
549 /* For images ensure that the temporary filename contains tile number information as well as
550 * a file extension based on the file magic. */
551 if (id_type == ID_IM) {
552 Image *ima = id_cast<Image *>(id);
553 ImagePackedFile *imapf = ima->packedfiles.last();
554 if (imapf != nullptr && imapf->packedfile != nullptr) {
555 const PackedFile *pf = imapf->packedfile;
556 enum eImbFileType ftype = eImbFileType(
557 IMB_test_image_type_from_memory(static_cast<const uchar *>(pf->data), pf->size));
558 if (ima->source == IMA_SRC_TILED) {
559 char tile_number[6];
560 SNPRINTF(tile_number, ".%d", imapf->tile_number);
561 BLI_strncpy(temp_filename + len, tile_number, sizeof(temp_filename) - len);
562 }
563 if (ftype != IMB_FTYPE_NONE) {
564 const int imtype = BKE_ftype_to_imtype(ftype, nullptr);
565 BKE_image_path_ext_from_imtype_ensure(temp_filename, sizeof(temp_filename), imtype);
566 }
567 }
568 }
569
570 BLI_path_make_safe_filename(temp_filename);
571 printf("%s\n", temp_filename);
572 }
573
574 if (temp_dirname[0] == '\0') {
575 /* Fall back to relative dir. */
576 STRNCPY(temp_dirname, "//");
577 }
578
579 {
580 const char *dir_name = nullptr;
581 switch (id_type) {
582 case ID_VF:
583 dir_name = "fonts";
584 break;
585 case ID_SO:
586 dir_name = "sounds";
587 break;
588 case ID_IM:
589 dir_name = "textures";
590 break;
591 case ID_VO:
592 dir_name = "volumes";
593 break;
594 default:
595 break;
596 }
597 if (dir_name) {
598 BLI_path_join(r_relpath, relpath_maxncpy, "//", dir_name, temp_filename);
599 }
600 }
601
602 {
603 size_t len = BLI_strncpy_rlen(r_abspath, temp_dirname, abspath_maxncpy);
604 BLI_strncpy(r_abspath + len, temp_filename, abspath_maxncpy - len);
605 }
606}
607
609 ReportList *reports,
610 ID *id,
611 const char *orig_file_path,
612 PackedFile *pf,
613 enum ePF_FileStatus how)
614{
615 char localname[FILE_MAX], absname[FILE_MAX];
616 char *new_name = nullptr;
617
618 if (id != nullptr) {
620 orig_file_path, id, absname, sizeof(absname), localname, sizeof(localname));
622 reports, BKE_main_blendfile_path(bmain), absname, localname, pf, how);
623 }
624
625 return new_name;
626}
627
629 ReportList *reports,
630 VFont *vfont,
631 enum ePF_FileStatus how)
632{
633 int ret_value = RET_ERROR;
634 if (vfont) {
635 char *new_file_path = BKE_packedfile_unpack(
636 bmain, reports, id_cast<ID *>(vfont), vfont->filepath, vfont->packedfile, how);
637
638 if (new_file_path != nullptr) {
639 ret_value = RET_OK;
641 vfont->packedfile = nullptr;
642 STRNCPY(vfont->filepath, new_file_path);
643 MEM_delete(new_file_path);
644 }
645 }
646
647 return ret_value;
648}
649
651 ReportList *reports,
652 bSound *sound,
653 enum ePF_FileStatus how)
654{
655 int ret_value = RET_ERROR;
656
657 if (sound != nullptr) {
658 char *new_file_path = BKE_packedfile_unpack(
659 bmain, reports, id_cast<ID *>(sound), sound->filepath, sound->packedfile, how);
660 if (new_file_path != nullptr) {
661 STRNCPY(sound->filepath, new_file_path);
662 MEM_delete(new_file_path);
663
665 sound->packedfile = nullptr;
666
667 BKE_sound_load(bmain, sound);
668
669 ret_value = RET_OK;
670 }
671 }
672
673 return ret_value;
674}
675
677 ReportList *reports,
678 Image *ima,
679 enum ePF_FileStatus how)
680{
681 int ret_value = RET_ERROR;
682
683 if (ima != nullptr) {
684 while (ima->packedfiles.last()) {
685 ImagePackedFile *imapf = ima->packedfiles.last();
686 char *new_file_path = BKE_packedfile_unpack(
687 bmain, reports, id_cast<ID *>(ima), imapf->filepath, imapf->packedfile, how);
688
689 if (new_file_path != nullptr) {
690 ImageView *iv;
691
692 ret_value = ret_value == RET_ERROR ? RET_ERROR : RET_OK;
694 imapf->packedfile = nullptr;
695
696 /* update the new corresponding view filepath */
697 iv = static_cast<ImageView *>(
698 BLI_findstring(&ima->views, imapf->filepath, offsetof(ImageView, filepath)));
699 if (iv) {
700 STRNCPY(iv->filepath, new_file_path);
701 }
702
703 /* keep the new name in the image for non-pack specific reasons */
704 if (how != PF_REMOVE) {
705 STRNCPY(ima->filepath, new_file_path);
706 if (ima->source == IMA_SRC_TILED) {
707 /* Ensure that the Image filepath is kept in a tokenized format. */
708 BKE_image_ensure_tile_token(ima->filepath, sizeof(ima->filepath));
709 }
710 }
711 MEM_delete(new_file_path);
712 }
713 else {
714 ret_value = RET_ERROR;
715 }
716
717 BLI_remlink(&ima->packedfiles, imapf);
718 MEM_delete(imapf);
719 }
720 }
721
722 if (ret_value == RET_OK) {
723 BKE_image_signal(bmain, ima, nullptr, IMA_SIGNAL_RELOAD);
724 }
725
726 return ret_value;
727}
728
730 ReportList *reports,
731 Volume *volume,
732 enum ePF_FileStatus how)
733{
734 int ret_value = RET_ERROR;
735
736 if (volume != nullptr) {
737 char *new_file_path = BKE_packedfile_unpack(
738 bmain, reports, id_cast<ID *>(volume), volume->filepath, volume->packedfile, how);
739 if (new_file_path != nullptr) {
740 STRNCPY(volume->filepath, new_file_path);
741 MEM_delete(new_file_path);
742
744 volume->packedfile = nullptr;
745
746 BKE_volume_unload(volume);
747
748 ret_value = RET_OK;
749 }
750 }
751
752 return ret_value;
753}
754
756{
757 Library *lib;
758 char *newname;
759 int ret_value = RET_ERROR;
760
761 for (lib = bmain->libraries.first(); lib; lib = static_cast<Library *>(lib->id.next)) {
762 if (lib->packedfile && lib->filepath[0]) {
763
764 newname = BKE_packedfile_unpack_to_file(reports,
766 lib->runtime->filepath_abs,
767 lib->runtime->filepath_abs,
768 lib->packedfile,
770 if (newname != nullptr) {
771 ret_value = RET_OK;
772
773 printf("Unpacked .blend library: %s\n", newname);
774
776 lib->packedfile = nullptr;
777
778 MEM_delete(newname);
779 }
780 }
781 }
782
783 return ret_value;
784}
785
787{
788 Library *lib;
789
790 /* Only allow libraries with relative paths (to avoid issues when unpacking, a limitation that we
791 * might want to lift since the "relativeness" does not really ensure sanity significantly more).
792 */
793 for (lib = bmain->libraries.first(); lib; lib = static_cast<Library *>(lib->id.next)) {
794 /* Exception to the above: essential assets have an absolute path and should not prevent to
795 * operator from continuing. */
797 continue;
798 }
799
800 if (!BLI_path_is_rel(lib->filepath)) {
801 break;
802 }
803 }
804
805 if (lib) {
806 BKE_reportf(reports, RPT_ERROR, "Cannot pack absolute file: '%s'", lib->filepath);
807 return;
808 }
809
810 for (lib = bmain->libraries.first(); lib; lib = static_cast<Library *>(lib->id.next)) {
811 /* Do not really pack essential assets though (see above). */
813 continue;
814 }
815 if (lib->packedfile == nullptr) {
817 }
818 }
819}
820
822{
823 Image *ima;
824 VFont *vf;
825 bSound *sound;
826 Volume *volume;
827
828 for (ima = bmain->images.first(); ima; ima = static_cast<Image *>(ima->id.next)) {
829 if (BKE_image_has_packedfile(ima) && !ID_IS_LINKED(ima)) {
830 BKE_packedfile_unpack_image(bmain, reports, ima, how);
831 }
832 }
833
834 for (vf = bmain->fonts.first(); vf; vf = static_cast<VFont *>(vf->id.next)) {
835 if (vf->packedfile && !ID_IS_LINKED(vf)) {
836 BKE_packedfile_unpack_vfont(bmain, reports, vf, how);
837 }
838 }
839
840 for (sound = bmain->sounds.first(); sound; sound = static_cast<bSound *>(sound->id.next)) {
841 if (sound->packedfile && !ID_IS_LINKED(sound)) {
842 BKE_packedfile_unpack_sound(bmain, reports, sound, how);
843 }
844 }
845
846 for (volume = bmain->volumes.first(); volume; volume = static_cast<Volume *>(volume->id.next)) {
847 if (volume->packedfile && !ID_IS_LINKED(volume)) {
848 BKE_packedfile_unpack_volume(bmain, reports, volume, how);
849 }
850 }
851
852 for (Object &object : bmain->objects) {
853 if (ID_IS_LINKED(&object)) {
854 continue;
855 }
856 for (ModifierData &md : object.modifiers) {
857 if (md.type == eModifierType_Nodes) {
858 NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(&md);
859 for (NodesModifierBake &bake : MutableSpan{nmd->bakes, nmd->bakes_num}) {
860 bke::bake::unpack_geometry_nodes_bake(*bmain, reports, object, *nmd, bake, how);
861 }
862 }
863 }
864 }
865}
866
868{
869 switch (GS(id->name)) {
870 case ID_IM: {
871 const Image *ima = id_cast<const Image *>(id);
872 return BKE_image_has_packedfile(ima);
873 }
874 case ID_VF: {
875 const VFont *vf = id_cast<const VFont *>(id);
876 return vf->packedfile != nullptr;
877 }
878 case ID_SO: {
879 const bSound *snd = id_cast<const bSound *>(id);
880 return snd->packedfile != nullptr;
881 }
882 case ID_VO: {
883 const Volume *volume = id_cast<const Volume *>(id);
884 return volume->packedfile != nullptr;
885 }
886 case ID_LI: {
887 const Library *li = id_cast<const Library *>(id);
888 return li->packedfile != nullptr;
889 }
890 default:
891 break;
892 }
893 return false;
894}
895
896void BKE_packedfile_id_unpack(Main *bmain, ID *id, ReportList *reports, enum ePF_FileStatus how)
897{
898 /* Only unpack when datablock is editable. */
899 if (!ID_IS_EDITABLE(id)) {
900 return;
901 }
902
903 switch (GS(id->name)) {
904 case ID_IM: {
905 Image *ima = id_cast<Image *>(id);
906 if (BKE_image_has_packedfile(ima)) {
907 BKE_packedfile_unpack_image(bmain, reports, ima, how);
908 }
909 break;
910 }
911 case ID_VF: {
912 VFont *vf = id_cast<VFont *>(id);
913 if (vf->packedfile) {
914 BKE_packedfile_unpack_vfont(bmain, reports, vf, how);
915 }
916 break;
917 }
918 case ID_SO: {
919 bSound *snd = id_cast<bSound *>(id);
920 if (snd->packedfile) {
921 BKE_packedfile_unpack_sound(bmain, reports, snd, how);
922 }
923 break;
924 }
925 case ID_VO: {
926 Volume *volume = id_cast<Volume *>(id);
927 if (volume->packedfile) {
928 BKE_packedfile_unpack_volume(bmain, reports, volume, how);
929 }
930 break;
931 }
932 case ID_LI: {
933 Library *li = id_cast<Library *>(id);
934 BKE_reportf(reports, RPT_ERROR, "Cannot unpack individual Library file, '%s'", li->filepath);
935 break;
936 }
937 default:
938 break;
939 }
940}
941
943{
944 if (pf == nullptr) {
945 return;
946 }
947 writer->write_shared(
948 pf->data, pf->size, pf->sharing_info, [&]() { writer->write_raw(pf->size, pf->data); });
949 writer->write_struct(pf);
950}
951
953{
954 BLO_read_struct(reader, PackedFile, pf_p);
955 PackedFile *pf = *pf_p;
956 if (pf == nullptr) {
957 return;
958 }
959 /* NOTE: this is endianness-sensitive. */
960 /* NOTE: there is no way to handle endianness switch here. */
961 pf->sharing_info = BLO_read_shared(reader, &pf->data, [&]() {
962 BLO_read_array_and_validate_size(
963 reader, reinterpret_cast<std::byte **>(const_cast<void **>(&pf->data)), &pf->size);
964 /* Do not create an implicit sharing if read data pointer is `nullptr`. */
965 return pf->data ?
966 implicit_sharing::info_for_mem_free(static_cast<const std::byte *>(pf->data)) :
967 nullptr;
968 });
969 if (pf->data == nullptr) {
970 /* We cannot allow a #PackedFile with a nullptr data field,
971 * the whole code assumes this is not possible. See #70315. */
972 CLOG_WARN(&LOG,
973 "%s: nullptr packedfile data (source: '%s'), cleaning up...",
974 __func__,
975 filepath.c_str());
976 BLI_assert(pf->sharing_info == nullptr);
977 MEM_SAFE_DELETE(*pf_p);
978 }
979}
980
981} // namespace blender
#define IMA_SIGNAL_RELOAD
Definition BKE_image.hh:178
#define RET_OK
#define RET_ERROR
#define ATTR_FALLTHROUGH
Volume data-block.
#define O_BINARY
#define FILE_MAX
#define BLI_path_join(...)
#define FILE_MAXDIR
#define STRNCPY_RLEN(dst, src)
#define SNPRINTF(dst, format,...)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
ID and Library types, which are fundamental for SDNA.
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define ID_BLEND_PATH(_bmain, _id)
Definition DNA_ID.h:688
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
Read Blender memory allocation and freeing.
#define ELEM(...)
constexpr const char * c_str() const
#define offsetof(t, d)
#define pf(_x, _i)
Prefetch 64.
Definition gim_memory.h:48
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
StringRefNull essentials_directory_path()
UnpackGeometryNodesBakeResult unpack_geometry_nodes_bake(Main &bmain, ReportList *reports, Object &object, NodesModifierData &nmd, NodesModifierBake &bake, ePF_FileStatus how)
PackGeometryNodesBakeResult pack_geometry_nodes_bake(Main &bmain, ReportList *reports, Object &object, NodesModifierData &nmd, NodesModifierBake &bake)
const ImplicitSharingInfo * info_for_mem_free(T *data)
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_packedfile_blend_write(BlendWriter *writer, const PackedFile *pf)
int BKE_packedfile_unpack_all_libraries(Main *bmain, ReportList *reports)
ePF_FileCompare BKE_packedfile_compare_to_file(const char *ref_file_name, const char *filepath_rel, const PackedFile *pf)
const ImplicitSharingInfo * BLO_read_shared(BlendDataReader *reader, T **data_ptr, FunctionRef< const ImplicitSharingInfo *()> read_fn)
char * STRNCPY(char(&dst)[N], const char *src)
@ RPT_WARNING
Definition BKE_report.hh:40
int BKE_packedfile_read(PackedFile *pf, void *data, int size)
Definition packedFile.cc:99
PackedFile * BKE_packedfile_duplicate(const PackedFile *pf_src)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
bool BKE_vfont_is_builtin(const VFont *vfont)
Definition vfont.cc:278
void BKE_image_signal(Main *bmain, Image *ima, ImageUser *iuser, int signal)
bool BKE_packedfile_id_check(const ID *id)
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
int64_t BLI_read(int fd, void *buf, size_t nbytes)
Definition fileops_c.cc:98
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
void BKE_packedfile_free(PackedFile *pf)
int BKE_packedfile_seek(PackedFile *pf, int offset, int whence)
Definition packedFile.cc:62
void BKE_packedfile_rewind(PackedFile *pf)
Definition packedFile.cc:94
PackedFile * BKE_packedfile_new(ReportList *reports, const char *filepath_rel, const char *basepath)
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BKE_packedfile_id_unpack(Main *bmain, ID *id, ReportList *reports, enum ePF_FileStatus how)
void MEM_delete_void(void *vmemh)
int BKE_packedfile_unpack_vfont(Main *bmain, ReportList *reports, VFont *vfont, enum ePF_FileStatus how)
PackedFileCount BKE_packedfile_count_all(Main *bmain)
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
void BKE_packedfile_pack_all_libraries(Main *bmain, ReportList *reports)
void BKE_packedfile_unpack_all(Main *bmain, ReportList *reports, enum ePF_FileStatus how)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
unsigned char uchar
Definition BKE_text.h:58
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
int BKE_packedfile_unpack_image(Main *bmain, ReportList *reports, Image *ima, enum ePF_FileStatus how)
BMesh const char void * data
char * BKE_packedfile_unpack_to_file(ReportList *reports, const char *ref_file_name, const char *abs_name, const char *local_name, PackedFile *pf, enum ePF_FileStatus how)
int BLI_stat(const char *path, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
void BKE_image_packfiles(ReportList *reports, Image *ima, const char *basepath)
char char size_t BLI_strncpy_rlen(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
int BKE_packedfile_unpack_volume(Main *bmain, ReportList *reports, Volume *volume, enum ePF_FileStatus how)
void BKE_volume_unload(Volume *volume)
int BLI_rename_overwrite(const char *from, const char *to) ATTR_NONNULL()
Definition fileops_c.cc:541
void BKE_packedfile_pack_all(Main *bmain, ReportList *reports, bool verbose)
void BKE_image_ensure_tile_token(char *filepath, size_t filepath_maxncpy)
static void unpack_generate_paths(const char *filepath, ID *id, char *r_abspath, size_t abspath_maxncpy, char *r_relpath, size_t relpath_maxncpy)
int BLI_copy(const char *path_src, const char *path_dst) ATTR_NONNULL()
char BKE_ftype_to_imtype(eImbFileType ftype, const ImbFormatOptions *options)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
eImbFileType IMB_test_image_type_from_memory(const unsigned char *buf, size_t buf_size)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
char * BKE_packedfile_unpack(Main *bmain, ReportList *reports, ID *id, const char *orig_file_path, PackedFile *pf, enum ePF_FileStatus how)
bool BLI_path_contains(const char *container_path, const char *containee_path) ATTR_NONNULL(1
constexpr int64_t PACKED_FILE_MAX_SIZE
ID_Type GS(const char *name)
Definition DNA_ID.h:410
void BKE_sound_load(Main *bmain, bSound *sound)
void BLI_path_split_dir_file(const char *filepath, char *dir, size_t dir_maxncpy, char *file, size_t file_maxncpy) ATTR_NONNULL(1
size_t BLI_file_descriptor_size(int file) ATTR_WARN_UNUSED_RESULT
Definition storage.cc:219
static CLG_LogRef LOG
Definition logging.cpp:12
void BKE_packedfile_blend_read(BlendDataReader *reader, PackedFile **pf_p, StringRefNull filepath)
bool BLI_path_make_safe_filename(char *filename) ATTR_NONNULL(1)
struct stat BLI_stat_t
bool BKE_image_has_packedfile(const Image *image)
PackedFile * BKE_packedfile_new_from_memory(const void *mem, int memlen, const ImplicitSharingInfo *sharing_info=nullptr)
int BKE_image_path_ext_from_imtype_ensure(char *filepath, size_t filepath_maxncpy, char imtype)
bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:454
int BKE_packedfile_unpack_sound(Main *bmain, ReportList *reports, bSound *sound, enum ePF_FileStatus how)
int BKE_packedfile_write_to_file(ReportList *reports, const char *ref_file_name, const char *filepath_rel, PackedFile *pf)
void write_shared(const void *data, size_t approximate_size_in_bytes, const ImplicitSharingInfo *sharing_info, FunctionRef< void()> write_fn)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
void * next
Definition DNA_ID.h:418
struct PackedFile * packedfile
struct PackedFile * packedfile
Definition DNA_ID.h:584
char filepath[1024]
Definition DNA_ID.h:562
bke::library::LibraryRuntime * runtime
Definition DNA_ID.h:591
ListBaseT< VFont > fonts
Definition BKE_main.hh:401
ListBaseT< Library > libraries
Definition BKE_main.hh:387
ListBaseT< Object > objects
Definition BKE_main.hh:388
ListBaseT< bSound > sounds
Definition BKE_main.hh:405
ListBaseT< Image > images
Definition BKE_main.hh:394
ListBaseT< Volume > volumes
Definition BKE_main.hh:430
const ImplicitSharingInfoHandle * sharing_info
struct PackedFile * packedfile
char filepath[1024]
struct PackedFile * packedfile
char filepath[1024]
struct PackedFile * packedfile