Blender  V2.93
asset.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <cstring>
22 
23 #include "DNA_ID.h"
24 #include "DNA_asset_types.h"
25 #include "DNA_defaults.h"
26 
27 #include "BLI_listbase.h"
28 #include "BLI_string.h"
29 #include "BLI_string_utils.h"
30 #include "BLI_utildefines.h"
31 
32 #include "BKE_asset.h"
33 #include "BKE_icons.h"
34 #include "BKE_idprop.h"
35 
36 #include "BLO_read_write.h"
37 
38 #include "MEM_guardedalloc.h"
39 
41 {
42  AssetMetaData *asset_data = (AssetMetaData *)MEM_callocN(sizeof(*asset_data), __func__);
43  memcpy(asset_data, DNA_struct_default_get(AssetMetaData), sizeof(*asset_data));
44  return asset_data;
45 }
46 
48 {
49  if ((*asset_data)->properties) {
50  IDP_FreeProperty((*asset_data)->properties);
51  }
52  MEM_SAFE_FREE((*asset_data)->description);
53  BLI_freelistN(&(*asset_data)->tags);
54 
55  MEM_SAFE_FREE(*asset_data);
56 }
57 
58 static AssetTag *asset_metadata_tag_add(AssetMetaData *asset_data, const char *const name)
59 {
60  AssetTag *tag = (AssetTag *)MEM_callocN(sizeof(*tag), __func__);
61  BLI_strncpy(tag->name, name, sizeof(tag->name));
62 
63  BLI_addtail(&asset_data->tags, tag);
64  asset_data->tot_tags++;
65  /* Invariant! */
66  BLI_assert(BLI_listbase_count(&asset_data->tags) == asset_data->tot_tags);
67 
68  return tag;
69 }
70 
71 AssetTag *BKE_asset_metadata_tag_add(AssetMetaData *asset_data, const char *name)
72 {
73  AssetTag *tag = asset_metadata_tag_add(asset_data, name);
74  BLI_uniquename(&asset_data->tags, tag, name, '.', offsetof(AssetTag, name), sizeof(tag->name));
75  return tag;
76 }
77 
82  const char *name)
83 {
84  struct AssetTagEnsureResult result = {nullptr};
85  if (!name[0]) {
86  return result;
87  }
88 
89  AssetTag *tag = (AssetTag *)BLI_findstring(&asset_data->tags, name, offsetof(AssetTag, name));
90 
91  if (tag) {
92  result.tag = tag;
93  result.is_new = false;
94  return result;
95  }
96 
97  tag = asset_metadata_tag_add(asset_data, name);
98 
99  result.tag = tag;
100  result.is_new = true;
101  return result;
102 }
103 
105 {
106  BLI_assert(BLI_findindex(&asset_data->tags, tag) >= 0);
107  BLI_freelinkN(&asset_data->tags, tag);
108  asset_data->tot_tags--;
109  /* Invariant! */
110  BLI_assert(BLI_listbase_count(&asset_data->tags) == asset_data->tot_tags);
111 }
112 
113 /* Queries -------------------------------------------- */
114 
116  const ID *id)
117 {
118  return BKE_previewimg_id_get(id);
119 }
120 
121 /* .blend file API -------------------------------------------- */
122 
124 {
125  BLO_write_struct(writer, AssetMetaData, asset_data);
126 
127  if (asset_data->properties) {
128  IDP_BlendWrite(writer, asset_data->properties);
129  }
130 
131  if (asset_data->description) {
132  BLO_write_string(writer, asset_data->description);
133  }
134  LISTBASE_FOREACH (AssetTag *, tag, &asset_data->tags) {
135  BLO_write_struct(writer, AssetTag, tag);
136  }
137 }
138 
140 {
141  /* asset_data itself has been read already. */
142 
143  if (asset_data->properties) {
144  BLO_read_data_address(reader, &asset_data->properties);
145  IDP_BlendDataRead(reader, &asset_data->properties);
146  }
147 
148  BLO_read_data_address(reader, &asset_data->description);
149  BLO_read_list(reader, &asset_data->tags);
150  BLI_assert(BLI_listbase_count(&asset_data->tags) == asset_data->tot_tags);
151 }
struct PreviewImage * BKE_previewimg_id_get(const struct ID *id)
void IDP_BlendWrite(struct BlendWriter *writer, const struct IDProperty *prop)
#define IDP_BlendDataRead(reader, prop)
Definition: BKE_idprop.h:208
void IDP_FreeProperty(struct IDProperty *prop)
Definition: idprop.c:1040
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
void BLI_freelinkN(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:281
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:547
void * BLI_findstring(const struct ListBase *listbase, const char *id, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
int BLI_findindex(const struct ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
int BLI_listbase_count(const struct ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
bool BLI_uniquename(struct ListBase *list, void *vlink, const char *defname, char delim, int name_offset, size_t len)
Definition: string_utils.c:381
#define UNUSED(x)
#define BLO_read_data_address(reader, ptr_p)
#define BLO_write_struct(writer, struct_name, data_ptr)
void BLO_read_list(BlendDataReader *reader, struct ListBase *list)
Definition: readfile.c:5654
void BLO_write_string(BlendWriter *writer, const char *data_ptr)
Definition: writefile.c:1401
ID and Library types, which are fundamental for sdna.
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:44
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
AssetTag * BKE_asset_metadata_tag_add(AssetMetaData *asset_data, const char *name)
Definition: asset.cc:71
static AssetTag * asset_metadata_tag_add(AssetMetaData *asset_data, const char *const name)
Definition: asset.cc:58
void BKE_asset_metadata_read(BlendDataReader *reader, AssetMetaData *asset_data)
Definition: asset.cc:139
void BKE_asset_metadata_free(AssetMetaData **asset_data)
Definition: asset.cc:47
struct AssetTagEnsureResult BKE_asset_metadata_tag_ensure(AssetMetaData *asset_data, const char *name)
Definition: asset.cc:81
void BKE_asset_metadata_write(BlendWriter *writer, AssetMetaData *asset_data)
Definition: asset.cc:123
PreviewImage * BKE_asset_metadata_preview_get_from_id(const AssetMetaData *UNUSED(asset_data), const ID *id)
Definition: asset.cc:115
AssetMetaData * BKE_asset_metadata_create(void)
Definition: asset.cc:40
void BKE_asset_metadata_tag_remove(AssetMetaData *asset_data, AssetTag *tag)
Definition: asset.cc:104
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...
struct IDProperty * properties
struct AssetTag * tag
Definition: BKE_asset.h:38
User defined tag. Currently only used by assets, could be used more often at some point....
char name[64]
Definition: DNA_ID.h:273