Blender  V2.93
addon.c
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 <stddef.h>
22 #include <stdlib.h>
23 
24 #include "RNA_types.h"
25 
26 #include "BLI_ghash.h"
27 #include "BLI_listbase.h"
28 #include "BLI_string.h"
29 #include "BLI_utildefines.h"
30 
31 #include "BKE_addon.h" /* own include */
32 #include "BKE_idprop.h"
33 
34 #include "DNA_listBase.h"
35 #include "DNA_userdef_types.h"
36 
37 #include "MEM_guardedalloc.h"
38 
39 #include "CLG_log.h"
40 
41 static CLG_LogRef LOG = {"bke.addon"};
42 
43 /* -------------------------------------------------------------------- */
48 {
49  bAddon *addon = MEM_callocN(sizeof(bAddon), "bAddon");
50  return addon;
51 }
52 
53 bAddon *BKE_addon_find(ListBase *addon_list, const char *module)
54 {
55  return BLI_findstring(addon_list, module, offsetof(bAddon, module));
56 }
57 
58 bAddon *BKE_addon_ensure(ListBase *addon_list, const char *module)
59 {
60  bAddon *addon = BKE_addon_find(addon_list, module);
61  if (addon == NULL) {
62  addon = BKE_addon_new();
63  BLI_strncpy(addon->module, module, sizeof(addon->module));
64  BLI_addtail(addon_list, addon);
65  }
66  return addon;
67 }
68 
69 bool BKE_addon_remove_safe(ListBase *addon_list, const char *module)
70 {
71  bAddon *addon = BLI_findstring(addon_list, module, offsetof(bAddon, module));
72  if (addon) {
73  BLI_remlink(addon_list, addon);
74  BKE_addon_free(addon);
75  return true;
76  }
77  return false;
78 }
79 
80 void BKE_addon_free(bAddon *addon)
81 {
82  if (addon->prop) {
83  IDP_FreeProperty(addon->prop);
84  }
85  MEM_freeN(addon);
86 }
87 
90 /* -------------------------------------------------------------------- */
95 
96 bAddonPrefType *BKE_addon_pref_type_find(const char *idname, bool quiet)
97 {
98  if (idname[0]) {
99  bAddonPrefType *apt;
100 
102  if (apt) {
103  return apt;
104  }
105 
106  if (!quiet) {
107  CLOG_WARN(&LOG, "search for unknown addon-pref '%s'", idname);
108  }
109  }
110  else {
111  if (!quiet) {
112  CLOG_WARN(&LOG, "search for empty addon-pref");
113  }
114  }
115 
116  return NULL;
117 }
118 
120 {
121  BLI_ghash_insert(global_addonpreftype_hash, apt->idname, apt);
122 }
123 
125 {
127 }
128 
130 {
133 }
134 
136 {
139 }
140 
struct bAddonPrefType bAddonPrefType
Definition: BKE_addon.h:39
void IDP_FreeProperty(struct IDProperty *prop)
Definition: idprop.c:1040
#define BLI_assert(a)
Definition: BLI_assert.h:58
GHash * BLI_ghash_str_new(const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
bool BLI_ghash_remove(GHash *gh, const void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:900
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition: BLI_ghash.c:756
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:1008
void * BLI_ghash_lookup(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:803
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
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:133
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
#define CLOG_WARN(clg_ref,...)
Definition: CLG_log.h:203
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:96
void BKE_addon_pref_type_free(void)
Definition: addon.c:135
void BKE_addon_free(bAddon *addon)
Definition: addon.c:80
void BKE_addon_pref_type_add(bAddonPrefType *apt)
Definition: addon.c:119
bAddon * BKE_addon_find(ListBase *addon_list, const char *module)
Definition: addon.c:53
bAddon * BKE_addon_ensure(ListBase *addon_list, const char *module)
Definition: addon.c:58
void BKE_addon_pref_type_init(void)
Definition: addon.c:129
bAddon * BKE_addon_new(void)
Definition: addon.c:47
void BKE_addon_pref_type_remove(const bAddonPrefType *apt)
Definition: addon.c:124
bool BKE_addon_remove_safe(ListBase *addon_list, const char *module)
Definition: addon.c:69
static CLG_LogRef LOG
Definition: addon.c:41
static GHash * global_addonpreftype_hash
Definition: addon.c:94
static struct PyModuleDef module
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
IDProperty * prop
char module[64]