Blender  V3.3
library.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2001-2002 NaN Holding BV. All rights reserved. */
3 
10 #include "CLG_log.h"
11 
12 #include "MEM_guardedalloc.h"
13 
14 /* all types are needed here, in order to do memory operations */
15 #include "DNA_ID.h"
16 
17 #include "BLI_utildefines.h"
18 
19 #include "BLI_blenlib.h"
20 
21 #include "BLT_translation.h"
22 
23 #include "BKE_bpath.h"
24 #include "BKE_idtype.h"
25 #include "BKE_lib_id.h"
26 #include "BKE_lib_query.h"
27 #include "BKE_library.h"
28 #include "BKE_main.h"
29 #include "BKE_main_namemap.h"
30 #include "BKE_packedFile.h"
31 
32 /* Unused currently. */
33 // static CLG_LogRef LOG = {.identifier = "bke.library"};
34 
35 struct BlendWriter;
36 struct BlendDataReader;
37 
39 {
40  if (lib->runtime.name_map) {
41  BKE_main_namemap_destroy(&lib->runtime.name_map);
42  }
43 }
44 
45 static void library_free_data(ID *id)
46 {
47  Library *library = (Library *)id;
49  if (library->packedfile) {
50  BKE_packedfile_free(library->packedfile);
51  }
52 }
53 
55 {
56  Library *lib = (Library *)id;
58 }
59 
60 static void library_foreach_path(ID *id, BPathForeachPathData *bpath_data)
61 {
62  Library *lib = (Library *)id;
63 
64  /* FIXME: Find if we should respect #BKE_BPATH_FOREACH_PATH_SKIP_PACKED here, and if not, explain
65  * why. */
66  if (lib->packedfile !=
67  NULL /*&& (bpath_data->flag & BKE_BPATH_FOREACH_PATH_SKIP_PACKED) != 0 */) {
68  return;
69  }
70 
71  if (BKE_bpath_foreach_path_fixed_process(bpath_data, lib->filepath)) {
72  BKE_library_filepath_set(bpath_data->bmain, lib, lib->filepath);
73  }
74 }
75 
76 static void library_blend_read_data(struct BlendDataReader *UNUSED(reader), ID *id)
77 {
78  Library *lib = (Library *)id;
79  lib->runtime.name_map = NULL;
80 }
81 
83  .id_code = ID_LI,
84  .id_filter = FILTER_ID_LI,
85  .main_listbase_index = INDEX_ID_LI,
86  .struct_size = sizeof(Library),
87  .name = "Library",
88  .name_plural = "libraries",
89  .translation_context = BLT_I18NCONTEXT_ID_LIBRARY,
91  .asset_type_info = NULL,
92 
93  .init_data = NULL,
94  .copy_data = NULL,
95  .free_data = library_free_data,
96  .make_local = NULL,
97  .foreach_id = library_foreach_id,
98  .foreach_cache = NULL,
99  .foreach_path = library_foreach_path,
100  .owner_get = NULL,
101 
102  .blend_write = NULL,
103  .blend_read_data = library_blend_read_data,
104  .blend_read_lib = NULL,
105  .blend_read_expand = NULL,
106 
107  .blend_read_undo_preserve = NULL,
108 
109  .lib_override_apply_post = NULL,
110 };
111 
112 void BKE_library_filepath_set(Main *bmain, Library *lib, const char *filepath)
113 {
114  /* in some cases this is used to update the absolute path from the
115  * relative */
116  if (lib->filepath != filepath) {
117  BLI_strncpy(lib->filepath, filepath, sizeof(lib->filepath));
118  }
119 
120  BLI_strncpy(lib->filepath_abs, filepath, sizeof(lib->filepath_abs));
121 
122  /* Not essential but set `filepath_abs` is an absolute copy of value which
123  * is more useful if its kept in sync. */
124  if (BLI_path_is_rel(lib->filepath_abs)) {
125  /* NOTE(campbell): the file may be unsaved, in this case, setting the
126  * `filepath_abs` on an indirectly linked path is not allowed from the
127  * outliner, and its not really supported but allow from here for now
128  * since making local could cause this to be directly linked.
129  */
130  /* Never make paths relative to parent lib - reading code (blenloader) always set *all*
131  * `lib->filepath` relative to current main, not to their parent for indirectly linked ones. */
132  const char *blendfile_path = BKE_main_blendfile_path(bmain);
133  BLI_path_abs(lib->filepath_abs, blendfile_path);
134  }
135 }
bool BKE_bpath_foreach_path_fixed_process(struct BPathForeachPathData *bpath_data, char *path)
Definition: bpath.c:121
@ IDTYPE_FLAGS_NO_ANIMDATA
Definition: BKE_idtype.h:41
@ IDTYPE_FLAGS_NO_COPY
Definition: BKE_idtype.h:30
@ IDTYPE_FLAGS_NO_LIBLINKING
Definition: BKE_idtype.h:32
#define BKE_LIB_FOREACHID_PROCESS_IDSUPER(_data, _id_super, _cb_flag)
@ IDWALK_CB_NEVER_SELF
Definition: BKE_lib_query.h:35
const char * BKE_main_blendfile_path(const struct Main *bmain) ATTR_NONNULL()
void BKE_main_namemap_destroy(struct UniqueName_Map **r_name_map) ATTR_NONNULL()
void BKE_packedfile_free(struct PackedFile *pf)
Definition: packedFile.c:140
bool BLI_path_is_rel(const char *path) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
Definition: path_util.c:347
bool BLI_path_abs(char *path, const char *basepath) ATTR_NONNULL()
Definition: path_util.c:897
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
#define UNUSED(x)
#define BLT_I18NCONTEXT_ID_LIBRARY
ID and Library types, which are fundamental for sdna.
struct Library Library
@ INDEX_ID_LI
Definition: DNA_ID.h:980
#define FILTER_ID_LI
Definition: DNA_ID.h:937
@ ID_LI
Definition: DNA_ID_enums.h:46
Read Guarded memory(de)allocation.
static void init_data(ModifierData *md)
DRWShaderLibrary * lib
void BKE_library_filepath_set(Main *bmain, Library *lib, const char *filepath)
Definition: library.c:112
static void library_runtime_reset(Library *lib)
Definition: library.c:38
static void library_blend_read_data(struct BlendDataReader *UNUSED(reader), ID *id)
Definition: library.c:76
static void library_foreach_path(ID *id, BPathForeachPathData *bpath_data)
Definition: library.c:60
static void library_foreach_id(ID *id, LibraryForeachIDData *data)
Definition: library.c:54
IDTypeInfo IDType_ID_LI
Definition: library.c:82
static void library_free_data(ID *id)
Definition: library.c:45
struct Main * bmain
Definition: BKE_bpath.h:75
short id_code
Definition: BKE_idtype.h:114
Definition: DNA_ID.h:368
Definition: BKE_main.h:121
static FT_Library library