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