Blender V5.3
readblenentry.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
9
10#include <cstddef>
11#include <cstdlib>
12#include <cstring>
13
14#include "MEM_guardedalloc.h"
15
16#include "BLI_ghash.hh"
17#include "BLI_linklist.hh"
18#include "BLI_path_utils.hh" /* Only for assertions. */
19#include "BLI_set.hh"
20#include "BLI_string.hh"
21#include "BLI_utildefines.hh"
22
23#include "DNA_genfile.h"
24
25#include "BKE_asset.hh"
26#include "BKE_idtype.hh"
27#include "BKE_main.hh"
28#include "BKE_preview_image.hh"
29
30#include "BLO_readfile.hh"
31
32#include "readfile.hh"
33
34#include "BLI_sys_types.hh" /* Needed for `intptr_t`. */
35
36namespace blender {
37
38#ifdef WIN32
39# include "BLI_winstuff.hh"
40#endif
41
42/* Access routines used by file-selector. */
43
45{
46 if (datablock_info->free_asset_data) {
47 BKE_asset_metadata_free(&datablock_info->asset_data);
48 datablock_info->free_asset_data = false;
49 }
50}
51
53{
54 BLI_linklist_free(datablock_infos, [](void *link) {
55 BLODataBlockInfo *datablock_info = static_cast<BLODataBlockInfo *>(link);
56 BLO_datablock_info_free(datablock_info);
57 MEM_delete(datablock_info);
58 });
59}
60
61BlendHandle *BLO_blendhandle_from_file(const char *filepath, BlendFileReadReport *reports)
62{
63 BlendHandle *bh;
64
65 bh = reinterpret_cast<BlendHandle *>(blo_filedata_from_file(filepath, reports));
66
67 return bh;
68}
69
70BlendHandle *BLO_blendhandle_from_memory(const void *mem,
71 int memsize,
72 BlendFileReadReport *reports)
73{
74 BlendHandle *bh;
75
76 bh = reinterpret_cast<BlendHandle *>(blo_filedata_from_memory(mem, memsize, reports));
77
78 return bh;
79}
80
81int3 BLO_blendhandle_get_version(const BlendHandle *bh)
82{
83 const FileData *fd = reinterpret_cast<const FileData *>(bh);
84 return int3(fd->fileversion / 100, fd->fileversion % 100, fd->filesubversion);
85}
86
87/* Return `false` if the block should be skipped because it is either an invalid block, or it does
88 * not meet to required conditions. */
90 BHead *bhead,
91 bool use_assets_only,
92 const char *&r_idname,
93 short &r_idflag,
94 AssetMetaData *&r_asset_meta_data,
95 BLODataBlockInfo::Library *r_library_data)
96{
97 r_idname = blo_bhead_id_name(fd, bhead);
98 if (!r_idname || r_idname[0] == '\0') {
99 return false;
100 }
101 r_idflag = blo_bhead_id_flag(fd, bhead);
102 if (r_library_data) {
103 r_library_data->filepath = blo_bhead_library_filepath(fd, bhead);
104 if (!r_library_data->filepath) {
105 return false;
106 }
107 r_library_data->flag = blo_bhead_library_flag(fd, bhead);
108 }
109 /* Do not list (and therefore allow direct linking of) packed data.
110 * While supporting this is conceptually possible, it would require significant changes in
111 * the UI (file browser) and UX (link operation) to convey this concept and handle it
112 * correctly. */
113 if (r_idflag & ID_FLAG_LINKED_AND_PACKED) {
114 return false;
115 }
116 r_asset_meta_data = blo_bhead_id_asset_data_address(fd, bhead);
117 if (use_assets_only && r_asset_meta_data == nullptr) {
118 return false;
119 }
120 return true;
121}
122
124 int ofblocktype,
125 const bool use_assets_only,
126 int *r_tot_names)
127{
128 FileData *fd = reinterpret_cast<FileData *>(bh);
129 LinkNode *names = nullptr;
130 BHead *bhead;
131 int tot = 0;
132
133 for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
134 if (bhead->code == ofblocktype) {
135 const char *idname;
136 short idflag;
137 AssetMetaData *asset_meta_data;
139 fd, bhead, use_assets_only, idname, idflag, asset_meta_data, nullptr))
140 {
141 continue;
142 }
143
144 BLI_linklist_prepend(&names, BLI_strdup(idname + 2));
145 tot++;
146 }
147 else if (bhead->code == BLO_CODE_ENDB) {
148 break;
149 }
150 }
151
152 *r_tot_names = tot;
153 return names;
154}
155
157 int ofblocktype,
158 const bool use_assets_only,
159 int *r_tot_info_items)
160{
161 FileData *fd = reinterpret_cast<FileData *>(bh);
162 LinkNode *infos = nullptr;
163 BHead *bhead;
164 int tot = 0;
165
166 const bool is_library = (ofblocktype == ID_LI);
167
168 const int sdna_nr_preview_image = DNA_struct_find_with_alias(fd->filesdna.get(), "PreviewImage");
169
170 for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
171 if (bhead->code == BLO_CODE_ENDB) {
172 break;
173 }
174 if (bhead->code == ofblocktype) {
175 BHead *id_bhead = bhead;
176
177 const char *idname;
178 short idflag;
179 AssetMetaData *asset_meta_data;
180 BLODataBlockInfo::Library library_data;
182 id_bhead,
183 use_assets_only,
184 idname,
185 idflag,
186 asset_meta_data,
187 is_library ? &library_data : nullptr))
188 {
189 continue;
190 }
191
192 const char *name = idname + 2;
193 BLODataBlockInfo *info = MEM_new<BLODataBlockInfo>(__func__);
194
195 if (is_library) {
196 info->library_data = library_data;
197 }
198
199 /* Lastly, read asset data from the following blocks. */
200 if (asset_meta_data) {
201 bhead = blo_read_asset_data_block(fd, bhead, &asset_meta_data);
202 /* blo_read_asset_data_block() reads all DATA heads and already advances bhead to the
203 * next non-DATA one. Go back, so the loop doesn't skip the non-DATA head. */
204 if (bhead) {
205 bhead = blo_bhead_prev(fd, bhead);
206 }
207 /* Asset data is missing in corrupt files. */
208 if (use_assets_only && asset_meta_data == nullptr) {
209 MEM_delete(info);
210 continue;
211 }
212 }
213
214 STRNCPY(info->name, name);
215 info->asset_data = asset_meta_data;
216 info->free_asset_data = true;
217
218 bool has_preview = false;
219 /* See if we can find a preview in the data of this ID. */
220 for (BHead *data_bhead = blo_bhead_next(fd, id_bhead);
221 data_bhead && data_bhead->code == BLO_CODE_DATA;
222 data_bhead = blo_bhead_next(fd, data_bhead))
223 {
224 if (data_bhead->SDNAnr == sdna_nr_preview_image) {
225 has_preview = true;
226 break;
227 }
228 }
229 info->no_preview_found = !has_preview;
230
231 BLI_linklist_prepend(&infos, info);
232 tot++;
233 }
234 }
235
236 *r_tot_info_items = tot;
237 return infos;
238}
239
255 BHead *bhead,
257 const PreviewImage *preview_from_file)
258{
259 for (int preview_index = 0; preview_index < NUM_ICON_SIZES; preview_index++) {
260 if (preview_from_file->rect[preview_index] && preview_from_file->w[preview_index] &&
261 preview_from_file->h[preview_index])
262 {
263 bhead = blo_bhead_next(fd, bhead);
264
265 const int64_t pixels_num = int64_t(preview_from_file->w[preview_index]) *
266 preview_from_file->h[preview_index];
267 const int64_t pixel_size = sizeof(uint);
268 if (bhead && bhead->SDNAnr == SDNA_RAW_DATA_STRUCT_INDEX && bhead->len % pixel_size == 0 &&
269 bhead->len / pixel_size == pixels_num)
270 {
271 result->rect[preview_index] = static_cast<uint *>(
272 BLO_library_read_struct(fd, bhead, "PreviewImage Icon Rect"));
273 }
274 else {
275 result->rect[preview_index] = nullptr;
276 }
277 if (result->rect[preview_index] == nullptr) {
278 result->w[preview_index] = result->h[preview_index] = 0;
279 }
280 }
281 else {
282 /* This should not be needed, but can happen in 'broken' .blend files,
283 * better handle this gracefully than crashing. */
284 result->rect[preview_index] = nullptr;
285 result->w[preview_index] = result->h[preview_index] = 0;
286 }
287 result->flag[preview_index] &= ~PRV_RENDERING;
288 }
289
290 return bhead;
291}
292
294 int ofblocktype,
295 const char *name)
296{
297 FileData *fd = reinterpret_cast<FileData *>(bh);
298 bool looking = false;
299 const int sdna_preview_image = DNA_struct_find_with_alias(fd->filesdna.get(), "PreviewImage");
300
301 for (BHead *bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
302 if (bhead->code == BLO_CODE_DATA) {
303 if (looking && bhead->SDNAnr == sdna_preview_image) {
304 PreviewImage *preview_from_file = static_cast<PreviewImage *>(
305 BLO_library_read_struct(fd, bhead, "PreviewImage"));
306
307 if (preview_from_file == nullptr) {
308 break;
309 }
310
311 PreviewImage *result = MEM_dupalloc(preview_from_file);
312 result->runtime = MEM_new<bke::PreviewImageRuntime>(__func__);
313 bhead = blo_blendhandle_read_preview_rects(fd, bhead, result, preview_from_file);
314 MEM_delete(preview_from_file);
315 return result;
316 }
317 }
318 else if (looking || bhead->code == BLO_CODE_ENDB) {
319 /* We were looking for a preview image, but didn't find any belonging to block. So it doesn't
320 * exist. */
321 break;
322 }
323 else if (bhead->code == ofblocktype) {
324 const char *idname = blo_bhead_id_name(fd, bhead);
325 if (idname && STREQ(&idname[2], name)) {
326 looking = true;
327 }
328 }
329 }
330
331 return nullptr;
332}
333
335{
336 FileData *fd = reinterpret_cast<FileData *>(bh);
337 Set<const char *> gathered;
338 LinkNode *names = nullptr;
339 BHead *bhead;
340
341 for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
342 if (bhead->code == BLO_CODE_ENDB) {
343 break;
344 }
345 if (BKE_idtype_idcode_is_valid(bhead->code)) {
347 const char *str = BKE_idtype_idcode_to_name(bhead->code);
348
349 if (gathered.add(str)) {
351 }
352 }
353 }
354 }
355
356 return names;
357}
358
359void BLO_blendhandle_close(BlendHandle *bh)
360{
361 FileData *fd = reinterpret_cast<FileData *>(bh);
362
364}
365
366void BLO_read_invalidate_message(BlendHandle *bh, Main *bmain, const char *message)
367{
368 FileData *fd = reinterpret_cast<FileData *>(bh);
369
370 blo_readfile_invalidate(fd, bmain, message);
371}
372
373/**********/
374
375BlendFileData *BLO_read_from_file(const char *filepath,
376 eBLOReadSkip skip_flags,
377 BlendFileReadReport *reports)
378{
379 BLI_assert(!BLI_path_is_rel(filepath));
381
382 BlendFileData *bfd = nullptr;
383 FileData *fd;
384
385 fd = blo_filedata_from_file(filepath, reports);
386 if (fd) {
387 fd->skip_flags = skip_flags;
388 bfd = blo_read_file_internal(fd, filepath);
390 }
391
392 return bfd;
393}
394
396 int memsize,
397 eBLOReadSkip skip_flags,
398 ReportList *reports)
399{
400 BlendFileData *bfd = nullptr;
401 FileData *fd;
402 BlendFileReadReport bf_reports{};
403 bf_reports.reports = reports;
404
405 fd = blo_filedata_from_memory(mem, memsize, &bf_reports);
406 if (fd) {
407 fd->skip_flags = skip_flags;
408 bfd = blo_read_file_internal(fd, "");
410 }
411
412 return bfd;
413}
414
416 const char *filepath,
417 MemFile *memfile,
418 const BlendFileReadParams *params,
419 ReportList *reports)
420{
421 BlendFileData *bfd = nullptr;
422 FileData *fd;
423 BlendFileReadReport bf_reports{};
424 bf_reports.reports = reports;
425
426 fd = blo_filedata_from_memfile(memfile, params, &bf_reports);
427 if (fd) {
428 fd->skip_flags = eBLOReadSkip(params->skip_flags);
429 STRNCPY(fd->relabase, filepath);
430
431 /* Build old ID map for all old IDs. */
432 blo_make_old_idmap_from_main(fd, oldmain);
433
434 /* Separate linked data from old main.
435 * WARNING: Do not split out packed IDs here, as these are handled similarly as local IDs in
436 * undo context. */
437 blo_split_main(oldmain, false);
438 fd->old_bmain = oldmain;
439
440 /* Removed packed data from this trick - it's internal data that needs saves. */
441
442 /* Store all existing ID caches pointers into a mapping, to allow restoring them into newly
443 * read IDs whenever possible.
444 *
445 * Note that this is only required for local data, since linked data are always re-used
446 * 'as-is'. */
447 blo_cache_storage_init(fd, oldmain);
448
449 bfd = blo_read_file_internal(fd, filepath);
450
451 /* Ensure relinked caches are not freed together with their old IDs. */
453
454 /* Still in-use libraries have already been moved from oldmain to new main
455 * (fd->bmain->split_mains), but oldmain itself shall *never* be 'transferred' to the new
456 * split_mains!
457 */
458 BLI_assert(oldmain->split_mains && (*oldmain->split_mains)[0] == oldmain);
459
460 /* That way, libraries (aka mains) we did not reuse in new undone/redone state
461 * will be cleared together with `oldmain`. */
462 blo_join_main(oldmain);
463
465 }
466
467 return bfd;
468}
469
471{
472 if (bfd->main) {
473 BKE_main_free(bfd->main);
474 }
475
476 if (bfd->user) {
477 MEM_delete(bfd->user);
478 }
479
480 MEM_delete(bfd);
481}
482
484 BlendfileLinkAppendContext *lapp_context,
485 BlendFileReadReport *reports)
486{
487 do_versions_after_setup(new_bmain, lapp_context, reports);
488}
489
490} // namespace blender
Compatibility-like things for windows.
external readfile function prototypes.
blenloader genfile private function prototypes
#define SDNA_RAW_DATA_STRUCT_INDEX
Read Blender memory allocation and freeing.
#define STREQ(a, b)
bool add(const Key &key)
Definition BLI_set.hh:248
#define str(s)
BMesh Iterators.
int3 BLO_blendhandle_get_version(const BlendHandle *bh)
const char * BKE_idtype_idcode_to_name(short idcode)
Definition idtype.cc:261
void blo_readfile_invalidate(FileData *fd, Main *bmain, const char *message)
Definition readfile.cc:589
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
BlendHandle * BLO_blendhandle_from_file(const char *filepath, BlendFileReadReport *reports)
LinkNode * BLO_blendhandle_get_datablock_info(BlendHandle *bh, int ofblocktype, bool use_assets_only, int *r_tot_info_items)
void BLO_datablock_info_linklist_free(LinkNode *datablock_infos)
__int64 int64_t
Definition BKE_text.h:90
char * STRNCPY(char(&dst)[N], const char *src)
@ NUM_ICON_SIZES
FileData * blo_filedata_from_memfile(MemFile *memfile, const BlendFileReadParams *params, BlendFileReadReport *reports)
Definition readfile.cc:1387
unsigned int uint
Definition BKE_text.h:55
bool BKE_idtype_idcode_is_valid(short idcode)
Definition idtype.cc:302
void BLO_blendhandle_close(BlendHandle *bh) ATTR_NONNULL(1)
void * BLO_library_read_struct(FileData *fd, BHead *bh, const char *blockname)
Definition readfile.cc:5957
void BLO_blendfiledata_free(BlendFileData *bfd)
bool BKE_idtype_idcode_is_linkable(short idcode)
Definition idtype.cc:307
void BKE_asset_metadata_free(AssetMetaData **asset_data)
Definition asset.cc:36
void blo_cache_storage_init(FileData *fd, Main *bmain)
Definition readfile.cc:1706
static bool blendhandle_load_id_data_and_validate(FileData *fd, BHead *bhead, bool use_assets_only, const char *&r_idname, short &r_idflag, AssetMetaData *&r_asset_meta_data, BLODataBlockInfo::Library *r_library_data)
AssetMetaData * blo_bhead_id_asset_data_address(const FileData *fd, const BHead *bhead)
Definition readfile.cc:850
FileData * blo_filedata_from_file(const char *filepath, BlendFileReadReport *reports)
Definition readfile.cc:1336
void BLO_read_do_version_after_setup(Main *new_bmain, BlendfileLinkAppendContext *lapp_context, BlendFileReadReport *reports)
void void BLI_linklist_prepend(LinkNode **listp, void *ptr) ATTR_NONNULL(1)
void blo_cache_storage_old_bmain_clear(FileData *fd, Main *bmain_old)
Definition readfile.cc:1742
BlendFileData * BLO_read_from_file(const char *filepath, eBLOReadSkip skip_flags, BlendFileReadReport *reports)
BlendHandle * BLO_blendhandle_from_memory(const void *mem, int memsize, BlendFileReadReport *reports)
const char * blo_bhead_library_filepath(const FileData *fd, const BHead *bhead)
Definition readfile.cc:868
static BHead * blo_blendhandle_read_preview_rects(FileData *fd, BHead *bhead, PreviewImage *result, const PreviewImage *preview_from_file)
void blo_split_main(Main *bmain, const bool do_split_packed_ids)
Definition readfile.cc:449
VecBase< int32_t, 3 > int3
const char * blo_bhead_id_name(FileData *fd, const BHead *bhead)
Definition readfile.cc:828
LibraryFlag blo_bhead_library_flag(const FileData *fd, const BHead *bhead)
Definition readfile.cc:874
bool BLI_path_is_abs_from_cwd(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void do_versions_after_setup(Main *new_bmain, BlendfileLinkAppendContext *lapp_context, BlendFileReadReport *reports)
BHead * blo_read_asset_data_block(FileData *fd, BHead *bhead, AssetMetaData **r_asset_data)
Definition readfile.cc:3773
BHead * blo_bhead_prev(FileData *, BHead *thisblock)
Definition readfile.cc:724
void BLO_datablock_info_free(BLODataBlockInfo *datablock_info)
BlendFileData * blo_read_file_internal(FileData *fd, const char *filepath)
Definition readfile.cc:4412
void BKE_main_free(Main *bmain)
Definition main.cc:202
int DNA_struct_find_with_alias(const struct SDNA *sdna, StringRef str)
void BLI_linklist_free(LinkNode *list, LinkNodeFreeFP freefunc)
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
short blo_bhead_id_flag(const FileData *fd, const BHead *bhead)
Definition readfile.cc:843
LinkNode * BLO_blendhandle_get_datablock_names(BlendHandle *bh, int ofblocktype, bool use_assets_only, int *r_tot_names)
LinkNode * BLO_blendhandle_get_linkable_groups(BlendHandle *bh)
FileData * blo_filedata_from_memory(const void *mem, const int memsize, BlendFileReadReport *reports)
Definition readfile.cc:1366
BlendFileData * BLO_read_from_memory(const void *mem, int memsize, eBLOReadSkip skip_flags, ReportList *reports)
@ ID_FLAG_LINKED_AND_PACKED
Definition DNA_ID.h:806
@ PRV_RENDERING
Definition DNA_ID.h:622
void blo_make_old_idmap_from_main(FileData *fd, Main *bmain)
Definition readfile.cc:1604
BlendFileData * BLO_read_from_memfile(Main *oldmain, const char *filepath, MemFile *memfile, const BlendFileReadParams *params, ReportList *reports)
PreviewImage * BLO_blendhandle_get_preview_for_id(BlendHandle *bh, int ofblocktype, const char *name)
BHead * blo_bhead_next(FileData *fd, BHead *thisblock)
Definition readfile.cc:732
BHead * blo_bhead_first(FileData *fd)
Definition readfile.cc:704
void blo_join_main(Main *bmain)
Definition readfile.cc:393
void blo_filedata_free(FileData *fd)
Definition readfile.cc:1404
void BLO_read_invalidate_message(BlendHandle *bh, Main *bmain, const char *message)
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...
BLODataBlockInfo::Library library_data
char relabase[FILE_MAX]
Definition readfile.hh:108
eBLOReadSkip skip_flags
Definition readfile.hh:144
std::unique_ptr< SDNA > filesdna
Definition readfile.hh:111
std::shared_ptr< VectorSet< Main * > > split_mains
Definition BKE_main.hh:254
unsigned int w[2]
Definition DNA_ID.h:645
unsigned int * rect[2]
Definition DNA_ID.h:649
unsigned int h[2]
Definition DNA_ID.h:646