Blender  V3.3
addon.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include <stddef.h>
8 #include <stdlib.h>
9 
10 #include "RNA_types.h"
11 
12 #include "BLI_ghash.h"
13 #include "BLI_listbase.h"
14 #include "BLI_string.h"
15 #include "BLI_utildefines.h"
16 
17 #include "BKE_addon.h" /* own include */
18 #include "BKE_idprop.h"
19 
20 #include "DNA_listBase.h"
21 #include "DNA_userdef_types.h"
22 
23 #include "MEM_guardedalloc.h"
24 
25 #include "CLG_log.h"
26 
27 static CLG_LogRef LOG = {"bke.addon"};
28 
29 /* -------------------------------------------------------------------- */
34 {
35  bAddon *addon = MEM_callocN(sizeof(bAddon), "bAddon");
36  return addon;
37 }
38 
39 bAddon *BKE_addon_find(ListBase *addon_list, const char *module)
40 {
41  return BLI_findstring(addon_list, module, offsetof(bAddon, module));
42 }
43 
44 bAddon *BKE_addon_ensure(ListBase *addon_list, const char *module)
45 {
46  bAddon *addon = BKE_addon_find(addon_list, module);
47  if (addon == NULL) {
48  addon = BKE_addon_new();
49  BLI_strncpy(addon->module, module, sizeof(addon->module));
50  BLI_addtail(addon_list, addon);
51  }
52  return addon;
53 }
54 
55 bool BKE_addon_remove_safe(ListBase *addon_list, const char *module)
56 {
57  bAddon *addon = BLI_findstring(addon_list, module, offsetof(bAddon, module));
58  if (addon) {
59  BLI_remlink(addon_list, addon);
60  BKE_addon_free(addon);
61  return true;
62  }
63  return false;
64 }
65 
66 void BKE_addon_free(bAddon *addon)
67 {
68  if (addon->prop) {
69  IDP_FreeProperty(addon->prop);
70  }
71  MEM_freeN(addon);
72 }
73 
76 /* -------------------------------------------------------------------- */
81 
82 bAddonPrefType *BKE_addon_pref_type_find(const char *idname, bool quiet)
83 {
84  if (idname[0]) {
85  bAddonPrefType *apt;
86 
88  if (apt) {
89  return apt;
90  }
91 
92  if (!quiet) {
93  CLOG_WARN(&LOG, "search for unknown addon-pref '%s'", idname);
94  }
95  }
96  else {
97  if (!quiet) {
98  CLOG_WARN(&LOG, "search for empty addon-pref");
99  }
100  }
101 
102  return NULL;
103 }
104 
106 {
107  BLI_ghash_insert(global_addonpreftype_hash, apt->idname, apt);
108 }
109 
111 {
113 }
114 
116 {
119 }
120 
122 {
125 }
126 
struct bAddonPrefType bAddonPrefType
Definition: BKE_addon.h:25
void IDP_FreeProperty(struct IDProperty *prop)
Definition: idprop.c:1093
#define BLI_assert(a)
Definition: BLI_assert.h:46
GHash * BLI_ghash_str_new(const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:734
bool BLI_ghash_remove(GHash *gh, const void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:790
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition: BLI_ghash.c:710
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:863
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:80
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:100
void * BLI_findstring(const struct ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
#define CLOG_WARN(clg_ref,...)
Definition: CLG_log.h:189
These structs are the foundation for all linked lists in the library system.
Read Guarded memory(de)allocation.
bAddonPrefType * BKE_addon_pref_type_find(const char *idname, bool quiet)
Definition: addon.c:82
void BKE_addon_pref_type_free(void)
Definition: addon.c:121
void BKE_addon_free(bAddon *addon)
Definition: addon.c:66
void BKE_addon_pref_type_add(bAddonPrefType *apt)
Definition: addon.c:105
bAddon * BKE_addon_find(ListBase *addon_list, const char *module)
Definition: addon.c:39
bAddon * BKE_addon_ensure(ListBase *addon_list, const char *module)
Definition: addon.c:44
void BKE_addon_pref_type_init(void)
Definition: addon.c:115
bAddon * BKE_addon_new(void)
Definition: addon.c:33
void BKE_addon_pref_type_remove(const bAddonPrefType *apt)
Definition: addon.c:110
bool BKE_addon_remove_safe(ListBase *addon_list, const char *module)
Definition: addon.c:55
static CLG_LogRef LOG
Definition: addon.c:27
static GHash * global_addonpreftype_hash
Definition: addon.c:80
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
static struct PyModuleDef module
Definition: python.cpp:972
IDProperty * prop
char module[64]