Blender V4.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
10#include <cmath>
11#include <cstddef>
12#include <cstdio>
13#include <cstdlib>
14#include <cstring>
15
16#include "MEM_guardedalloc.h"
17
18#include "BLI_ghash.h"
19#include "BLI_linklist.h"
20#include "BLI_path_utils.hh" /* Only for assertions. */
21#include "BLI_string.h"
22#include "BLI_utildefines.h"
23
24#include "DNA_genfile.h"
25#include "DNA_sdna_types.h"
26
27#include "BKE_asset.hh"
28#include "BKE_idtype.hh"
29#include "BKE_main.hh"
30#include "BKE_preview_image.hh"
31
32#include "BLO_blend_defs.hh"
33#include "BLO_readfile.hh"
34
35#include "readfile.hh"
36
37#include "BLI_sys_types.h" /* Needed for `intptr_t`. */
38
39#ifdef WIN32
40# include "BLI_winstuff.h"
41#endif
42
43/* Access routines used by file-selector. */
44
46{
47 if (datablock_info->free_asset_data) {
48 BKE_asset_metadata_free(&datablock_info->asset_data);
49 datablock_info->free_asset_data = false;
50 }
51}
52
54{
55 BLI_linklist_free(datablock_infos, [](void *link) {
56 BLODataBlockInfo *datablock_info = static_cast<BLODataBlockInfo *>(link);
57 BLO_datablock_info_free(datablock_info);
58 MEM_freeN(datablock_info);
59 });
60}
61
62BlendHandle *BLO_blendhandle_from_file(const char *filepath, BlendFileReadReport *reports)
63{
64 BlendHandle *bh;
65
66 bh = (BlendHandle *)blo_filedata_from_file(filepath, reports);
67
68 return bh;
69}
70
71BlendHandle *BLO_blendhandle_from_memory(const void *mem,
72 int memsize,
73 BlendFileReadReport *reports)
74{
75 BlendHandle *bh;
76
77 bh = (BlendHandle *)blo_filedata_from_memory(mem, memsize, reports);
78
79 return bh;
80}
81
83 int ofblocktype,
84 const bool use_assets_only,
85 int *r_tot_names)
86{
87 FileData *fd = (FileData *)bh;
88 LinkNode *names = nullptr;
89 BHead *bhead;
90 int tot = 0;
91
92 for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
93 if (bhead->code == ofblocktype) {
94 const char *idname = blo_bhead_id_name(fd, bhead);
95 if (use_assets_only && blo_bhead_id_asset_data_address(fd, bhead) == nullptr) {
96 continue;
97 }
98
99 BLI_linklist_prepend(&names, BLI_strdup(idname + 2));
100 tot++;
101 }
102 else if (bhead->code == BLO_CODE_ENDB) {
103 break;
104 }
105 }
106
107 *r_tot_names = tot;
108 return names;
109}
110
112 int ofblocktype,
113 const bool use_assets_only,
114 int *r_tot_info_items)
115{
116 FileData *fd = (FileData *)bh;
117 LinkNode *infos = nullptr;
118 BHead *bhead;
119 int tot = 0;
120
121 const int sdna_nr_preview_image = DNA_struct_find_with_alias(fd->filesdna, "PreviewImage");
122
123 for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
124 if (bhead->code == BLO_CODE_ENDB) {
125 break;
126 }
127 if (bhead->code == ofblocktype) {
128 BHead *id_bhead = bhead;
129
130 const char *name = blo_bhead_id_name(fd, bhead) + 2;
131 AssetMetaData *asset_meta_data = blo_bhead_id_asset_data_address(fd, bhead);
132
133 const bool is_asset = asset_meta_data != nullptr;
134 const bool skip_datablock = use_assets_only && !is_asset;
135 if (skip_datablock) {
136 continue;
137 }
138 BLODataBlockInfo *info = static_cast<BLODataBlockInfo *>(
139 MEM_mallocN(sizeof(*info), __func__));
140
141 /* Lastly, read asset data from the following blocks. */
142 if (asset_meta_data) {
143 bhead = blo_read_asset_data_block(fd, bhead, &asset_meta_data);
144 /* blo_read_asset_data_block() reads all DATA heads and already advances bhead to the
145 * next non-DATA one. Go back, so the loop doesn't skip the non-DATA head. */
146 bhead = blo_bhead_prev(fd, bhead);
147 }
148
149 STRNCPY(info->name, name);
150 info->asset_data = asset_meta_data;
151 info->free_asset_data = true;
152
153 bool has_preview = false;
154 /* See if we can find a preview in the data of this ID. */
155 for (BHead *data_bhead = blo_bhead_next(fd, id_bhead); data_bhead->code == BLO_CODE_DATA;
156 data_bhead = blo_bhead_next(fd, data_bhead))
157 {
158 if (data_bhead->SDNAnr == sdna_nr_preview_image) {
159 has_preview = true;
160 break;
161 }
162 }
163 info->no_preview_found = !has_preview;
164
165 BLI_linklist_prepend(&infos, info);
166 tot++;
167 }
168 }
169
170 *r_tot_info_items = tot;
171 return infos;
172}
173
189 BHead *bhead,
190 PreviewImage *result,
191 const PreviewImage *preview_from_file)
192{
193 for (int preview_index = 0; preview_index < NUM_ICON_SIZES; preview_index++) {
194 if (preview_from_file->rect[preview_index] && preview_from_file->w[preview_index] &&
195 preview_from_file->h[preview_index])
196 {
197 bhead = blo_bhead_next(fd, bhead);
198 BLI_assert((preview_from_file->w[preview_index] * preview_from_file->h[preview_index] *
199 sizeof(uint)) == bhead->len);
200 result->rect[preview_index] = static_cast<uint *>(
201 BLO_library_read_struct(fd, bhead, "PreviewImage Icon Rect"));
202 }
203 else {
204 /* This should not be needed, but can happen in 'broken' .blend files,
205 * better handle this gracefully than crashing. */
206 BLI_assert(preview_from_file->rect[preview_index] == nullptr &&
207 preview_from_file->w[preview_index] == 0 &&
208 preview_from_file->h[preview_index] == 0);
209 result->rect[preview_index] = nullptr;
210 result->w[preview_index] = result->h[preview_index] = 0;
211 }
212 BKE_previewimg_finish(result, preview_index);
213 }
214
215 return bhead;
216}
217
219 int ofblocktype,
220 const char *name)
221{
222 FileData *fd = (FileData *)bh;
223 bool looking = false;
224 const int sdna_preview_image = DNA_struct_find_with_alias(fd->filesdna, "PreviewImage");
225
226 for (BHead *bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
227 if (bhead->code == BLO_CODE_DATA) {
228 if (looking && bhead->SDNAnr == sdna_preview_image) {
229 PreviewImage *preview_from_file = static_cast<PreviewImage *>(
230 BLO_library_read_struct(fd, bhead, "PreviewImage"));
231
232 if (preview_from_file == nullptr) {
233 break;
234 }
235
236 PreviewImage *result = static_cast<PreviewImage *>(MEM_dupallocN(preview_from_file));
237 result->runtime = MEM_new<blender::bke::PreviewImageRuntime>(__func__);
238 bhead = blo_blendhandle_read_preview_rects(fd, bhead, result, preview_from_file);
239 MEM_freeN(preview_from_file);
240 return result;
241 }
242 }
243 else if (looking || bhead->code == BLO_CODE_ENDB) {
244 /* We were looking for a preview image, but didn't find any belonging to block. So it doesn't
245 * exist. */
246 break;
247 }
248 else if (bhead->code == ofblocktype) {
249 const char *idname = blo_bhead_id_name(fd, bhead);
250 if (STREQ(&idname[2], name)) {
251 looking = true;
252 }
253 }
254 }
255
256 return nullptr;
257}
258
260{
261 FileData *fd = (FileData *)bh;
262 GSet *gathered = BLI_gset_ptr_new("linkable_groups gh");
263 LinkNode *names = nullptr;
264 BHead *bhead;
265
266 for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
267 if (bhead->code == BLO_CODE_ENDB) {
268 break;
269 }
270 if (BKE_idtype_idcode_is_valid(bhead->code)) {
272 const char *str = BKE_idtype_idcode_to_name(bhead->code);
273
274 if (BLI_gset_add(gathered, (void *)str)) {
276 }
277 }
278 }
279 }
280
281 BLI_gset_free(gathered, nullptr);
282
283 return names;
284}
285
286void BLO_blendhandle_close(BlendHandle *bh)
287{
288 FileData *fd = (FileData *)bh;
289
291}
292
293void BLO_read_invalidate_message(BlendHandle *bh, Main *bmain, const char *message)
294{
295 FileData *fd = reinterpret_cast<FileData *>(bh);
296
297 blo_readfile_invalidate(fd, bmain, message);
298}
299
300/**********/
301
302BlendFileData *BLO_read_from_file(const char *filepath,
303 eBLOReadSkip skip_flags,
304 BlendFileReadReport *reports)
305{
306 BLI_assert(!BLI_path_is_rel(filepath));
308
309 BlendFileData *bfd = nullptr;
310 FileData *fd;
311
312 fd = blo_filedata_from_file(filepath, reports);
313 if (fd) {
314 fd->skip_flags = skip_flags;
315 bfd = blo_read_file_internal(fd, filepath);
317 }
318
319 return bfd;
320}
321
323 int memsize,
324 eBLOReadSkip skip_flags,
325 ReportList *reports)
326{
327 BlendFileData *bfd = nullptr;
328 FileData *fd;
329 BlendFileReadReport bf_reports{};
330 bf_reports.reports = reports;
331
332 fd = blo_filedata_from_memory(mem, memsize, &bf_reports);
333 if (fd) {
334 fd->skip_flags = skip_flags;
335 bfd = blo_read_file_internal(fd, "");
337 }
338
339 return bfd;
340}
341
343 const char *filepath,
344 MemFile *memfile,
346 ReportList *reports)
347{
348 BlendFileData *bfd = nullptr;
349 FileData *fd;
350 ListBase old_mainlist;
351 BlendFileReadReport bf_reports{};
352 bf_reports.reports = reports;
353
354 fd = blo_filedata_from_memfile(memfile, params, &bf_reports);
355 if (fd) {
356 fd->skip_flags = eBLOReadSkip(params->skip_flags);
357 STRNCPY(fd->relabase, filepath);
358
359 /* Build old ID map for all old IDs. */
360 blo_make_old_idmap_from_main(fd, oldmain);
361
362 /* Separate linked data from old main. */
363 blo_split_main(&old_mainlist, oldmain);
364 fd->old_mainlist = &old_mainlist;
365
366 /* Removed packed data from this trick - it's internal data that needs saves. */
367
368 /* Store all existing ID caches pointers into a mapping, to allow restoring them into newly
369 * read IDs whenever possible.
370 *
371 * Note that this is only required for local data, since linked data are always re-used
372 * 'as-is'. */
373 blo_cache_storage_init(fd, oldmain);
374
375 bfd = blo_read_file_internal(fd, filepath);
376
377 /* Ensure relinked caches are not freed together with their old IDs. */
379
380 /* Still in-use libraries have already been moved from oldmain to new mainlist,
381 * but oldmain itself shall *never* be 'transferred' to new mainlist! */
382 BLI_assert(old_mainlist.first == oldmain);
383
384 /* That way, libraries (aka mains) we did not reuse in new undone/redone state
385 * will be cleared together with `oldmain`. */
386 blo_join_main(&old_mainlist);
387
389 }
390
391 return bfd;
392}
393
395{
396 if (bfd->main) {
397 BKE_main_free(bfd->main);
398 }
399
400 if (bfd->user) {
401 MEM_freeN(bfd->user);
402 }
403
404 MEM_freeN(bfd);
405}
406
408 BlendfileLinkAppendContext *lapp_context,
409 BlendFileReadReport *reports)
410{
411 do_versions_after_setup(new_bmain, lapp_context, reports);
412}
void BKE_asset_metadata_free(AssetMetaData **asset_data)
Definition asset.cc:38
const char * BKE_idtype_idcode_to_name(short idcode)
Definition idtype.cc:168
bool BKE_idtype_idcode_is_linkable(short idcode)
Definition idtype.cc:201
bool BKE_idtype_idcode_is_valid(short idcode)
Definition idtype.cc:196
void BKE_main_free(Main *bmain)
Definition main.cc:175
void BKE_previewimg_finish(PreviewImage *prv, int size)
#define BLI_assert(a)
Definition BLI_assert.h:50
struct GSet GSet
Definition BLI_ghash.h:341
GSet * BLI_gset_ptr_new(const char *info)
void BLI_gset_free(GSet *gs, GSetKeyFreeFP keyfreefp)
Definition BLI_ghash.c:1034
bool BLI_gset_add(GSet *gs, void *key)
Definition BLI_ghash.c:966
bool BLI_path_is_abs_from_cwd(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
Definition string.c:40
#define STRNCPY(dst, src)
Definition BLI_string.h:593
unsigned int uint
#define STREQ(a, b)
Compatibility-like things for windows.
defines for blend-file codes.
@ BLO_CODE_ENDB
@ BLO_CODE_DATA
external readfile function prototypes.
eBLOReadSkip
void * BLO_library_read_struct(FileData *fd, BHead *bh, const char *blockname)
Definition readfile.cc:4513
@ NUM_ICON_SIZES
blenloader genfile private function prototypes
int DNA_struct_find_with_alias(const struct SDNA *sdna, const char *str)
Read Guarded memory(de)allocation.
#define str(s)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void *(* MEM_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
void *(* MEM_dupallocN)(const void *vmemh)
Definition mallocn.cc:39
LinkNode * BLO_blendhandle_get_datablock_info(BlendHandle *bh, int ofblocktype, const bool use_assets_only, int *r_tot_info_items)
void BLO_datablock_info_linklist_free(LinkNode *datablock_infos)
void BLO_datablock_info_free(BLODataBlockInfo *datablock_info)
void BLO_read_invalidate_message(BlendHandle *bh, Main *bmain, const char *message)
PreviewImage * BLO_blendhandle_get_preview_for_id(BlendHandle *bh, int ofblocktype, const char *name)
BlendHandle * BLO_blendhandle_from_file(const char *filepath, BlendFileReadReport *reports)
BlendHandle * BLO_blendhandle_from_memory(const void *mem, int memsize, BlendFileReadReport *reports)
BlendFileData * BLO_read_from_memfile(Main *oldmain, const char *filepath, MemFile *memfile, const BlendFileReadParams *params, ReportList *reports)
BlendFileData * BLO_read_from_memory(const void *mem, int memsize, eBLOReadSkip skip_flags, ReportList *reports)
BlendFileData * BLO_read_from_file(const char *filepath, eBLOReadSkip skip_flags, BlendFileReadReport *reports)
static BHead * blo_blendhandle_read_preview_rects(FileData *fd, BHead *bhead, PreviewImage *result, const PreviewImage *preview_from_file)
LinkNode * BLO_blendhandle_get_datablock_names(BlendHandle *bh, int ofblocktype, const bool use_assets_only, int *r_tot_names)
void BLO_read_do_version_after_setup(Main *new_bmain, BlendfileLinkAppendContext *lapp_context, BlendFileReadReport *reports)
LinkNode * BLO_blendhandle_get_linkable_groups(BlendHandle *bh)
void BLO_blendhandle_close(BlendHandle *bh)
void BLO_blendfiledata_free(BlendFileData *bfd)
FileData * blo_filedata_from_memory(const void *mem, int memsize, BlendFileReadReport *reports)
Definition readfile.cc:1312
BHead * blo_bhead_next(FileData *fd, BHead *thisblock)
Definition readfile.cc:867
BHead * blo_bhead_first(FileData *fd)
Definition readfile.cc:839
AssetMetaData * blo_bhead_id_asset_data_address(const FileData *fd, const BHead *bhead)
Definition readfile.cc:939
BlendFileData * blo_read_file_internal(FileData *fd, const char *filepath)
Definition readfile.cc:3531
FileData * blo_filedata_from_memfile(MemFile *memfile, const BlendFileReadParams *params, BlendFileReadReport *reports)
Definition readfile.cc:1342
const char * blo_bhead_id_name(const FileData *fd, const BHead *bhead)
Definition readfile.cc:934
FileData * blo_filedata_from_file(const char *filepath, BlendFileReadReport *reports)
Definition readfile.cc:1282
void blo_split_main(ListBase *mainlist, Main *main)
Definition readfile.cc:397
void blo_cache_storage_init(FileData *fd, Main *bmain)
Definition readfile.cc:1628
void blo_make_old_idmap_from_main(FileData *fd, Main *bmain)
Definition readfile.cc:1533
BHead * blo_read_asset_data_block(FileData *fd, BHead *bhead, AssetMetaData **r_asset_data)
Definition readfile.cc:3012
void blo_filedata_free(FileData *fd)
Definition readfile.cc:1359
BHead * blo_bhead_prev(FileData *, BHead *thisblock)
Definition readfile.cc:859
void blo_readfile_invalidate(FileData *fd, Main *bmain, const char *message)
Definition readfile.cc:590
void blo_cache_storage_old_bmain_clear(FileData *fd, Main *bmain_old)
Definition readfile.cc:1665
void blo_join_main(ListBase *mainlist)
Definition readfile.cc:350
void do_versions_after_setup(Main *new_bmain, BlendfileLinkAppendContext *lapp_context, BlendFileReadReport *reports)
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...
AssetMetaData * asset_data
UserDef * user
eBLOReadSkip skip_flags
Definition readfile.hh:103
ListBase * old_mainlist
Definition readfile.hh:133
char relabase[FILE_MAX]
Definition readfile.hh:84
SDNA * filesdna
Definition readfile.hh:87
void * first
PreviewImageRuntimeHandle * runtime
Definition DNA_ID.h:609
unsigned int h[2]
Definition DNA_ID.h:604
unsigned int * rect[2]
Definition DNA_ID.h:607
unsigned int w[2]
Definition DNA_ID.h:603