RNA: report error on struct naming collision

Fixes T52463, error instead of crash.
This commit is contained in:
Campbell Barton 2017-08-23 14:59:14 +10:00
parent 4761dea573
commit 1e60ac3394
9 changed files with 102 additions and 25 deletions

@ -783,6 +783,8 @@ const struct ListBase *RNA_struct_type_functions(StructRNA *srna);
char *RNA_struct_name_get_alloc(PointerRNA *ptr, char *fixedbuf, int fixedlen, int *r_len); char *RNA_struct_name_get_alloc(PointerRNA *ptr, char *fixedbuf, int fixedlen, int *r_len);
bool RNA_struct_available_or_report(struct ReportList *reports, const char *identifier);
/* Properties /* Properties
* *
* Access to struct properties. All this works with RNA pointers rather than * Access to struct properties. All this works with RNA pointers rather than

@ -273,6 +273,10 @@ StructRNA *rna_PropertyGroup_register(Main *UNUSED(bmain), ReportList *reports,
return NULL; return NULL;
} }
if (!RNA_struct_available_or_report(reports, identifier)) {
return NULL;
}
return RNA_def_struct_ptr(&BLENDER_RNA, identifier, &RNA_PropertyGroup); /* XXX */ return RNA_def_struct_ptr(&BLENDER_RNA, identifier, &RNA_PropertyGroup); /* XXX */
} }

@ -814,6 +814,38 @@ char *RNA_struct_name_get_alloc(PointerRNA *ptr, char *fixedbuf, int fixedlen, i
return NULL; return NULL;
} }
bool RNA_struct_available_or_report(ReportList *reports, const char *identifier)
{
const StructRNA *srna_exists = RNA_struct_find(identifier);
if (UNLIKELY(srna_exists != NULL)) {
/* Use comprehensive string construction since this is such a rare occurrence
* and information here may cut down time troubleshooting. */
DynStr *dynstr = BLI_dynstr_new();
BLI_dynstr_appendf(dynstr, "Type identifier '%s' is already in use: '", identifier);
BLI_dynstr_append(dynstr, srna_exists->identifier);
int i = 0;
if (srna_exists->base) {
for (const StructRNA *base = srna_exists->base; base; base = base->base) {
BLI_dynstr_append(dynstr, "(");
BLI_dynstr_append(dynstr, base->identifier);
i += 1;
}
while (i--) {
BLI_dynstr_append(dynstr, ")");
}
}
BLI_dynstr_append(dynstr, "'.");
char *result = BLI_dynstr_get_cstring(dynstr);
BLI_dynstr_free(dynstr);
BKE_report(reports, RPT_ERROR, result);
MEM_freeN(result);
return false;
}
else {
return true;
}
}
/* Property Information */ /* Property Information */
const char *RNA_property_identifier(PropertyRNA *prop) const char *RNA_property_identifier(PropertyRNA *prop)

@ -270,9 +270,13 @@ static StructRNA *rna_KeyingSetInfo_register(Main *bmain, ReportList *reports, v
/* check if we have registered this info before, and remove it */ /* check if we have registered this info before, and remove it */
ksi = ANIM_keyingset_info_find_name(dummyksi.idname); ksi = ANIM_keyingset_info_find_name(dummyksi.idname);
if (ksi && ksi->ext.srna) if (ksi && ksi->ext.srna) {
rna_KeyingSetInfo_unregister(bmain, ksi->ext.srna); rna_KeyingSetInfo_unregister(bmain, ksi->ext.srna);
}
if (!RNA_struct_available_or_report(reports, identifier)) {
return NULL;
}
/* create a new KeyingSetInfo type */ /* create a new KeyingSetInfo type */
ksi = MEM_callocN(sizeof(KeyingSetInfo), "python keying set info"); ksi = MEM_callocN(sizeof(KeyingSetInfo), "python keying set info");
memcpy(ksi, &dummyksi, sizeof(KeyingSetInfo)); memcpy(ksi, &dummyksi, sizeof(KeyingSetInfo));

@ -630,9 +630,13 @@ static StructRNA *rna_NodeTree_register(
/* check if we have registered this tree type before, and remove it */ /* check if we have registered this tree type before, and remove it */
nt = ntreeTypeFind(dummynt.idname); nt = ntreeTypeFind(dummynt.idname);
if (nt) if (nt) {
rna_NodeTree_unregister(bmain, nt->ext.srna); rna_NodeTree_unregister(bmain, nt->ext.srna);
}
if (!RNA_struct_available_or_report(reports, identifier)) {
return NULL;
}
/* create a new node tree type */ /* create a new node tree type */
nt = MEM_callocN(sizeof(bNodeTreeType), "node tree type"); nt = MEM_callocN(sizeof(bNodeTreeType), "node tree type");
memcpy(nt, &dummynt, sizeof(dummynt)); memcpy(nt, &dummynt, sizeof(dummynt));
@ -1389,11 +1393,18 @@ static bNodeType *rna_Node_register_base(Main *bmain, ReportList *reports, Struc
identifier, (int)sizeof(dummynt.idname)); identifier, (int)sizeof(dummynt.idname));
return NULL; return NULL;
} }
if (!RNA_struct_available_or_report(reports, identifier)) {
return NULL;
}
/* check if we have registered this node type before, and remove it */ /* check if we have registered this node type before, and remove it */
nt = nodeTypeFind(dummynt.idname); nt = nodeTypeFind(dummynt.idname);
if (nt) if (nt) {
rna_Node_unregister(bmain, nt->ext.srna); rna_Node_unregister(bmain, nt->ext.srna);
}
if (!RNA_struct_available_or_report(reports, identifier)) {
return NULL;
}
/* create a new node type */ /* create a new node type */
nt = MEM_callocN(sizeof(bNodeType), "node type"); nt = MEM_callocN(sizeof(bNodeType), "node type");

@ -321,6 +321,9 @@ static StructRNA *rna_RenderEngine_register(Main *bmain, ReportList *reports, vo
break; break;
} }
} }
if (!RNA_struct_available_or_report(reports, identifier)) {
return NULL;
}
/* create a new engine type */ /* create a new engine type */
et = MEM_callocN(sizeof(RenderEngineType), "python render engine"); et = MEM_callocN(sizeof(RenderEngineType), "python render engine");

@ -229,6 +229,9 @@ static StructRNA *rna_Panel_register(Main *bmain, ReportList *reports, void *dat
break; break;
} }
} }
if (!RNA_struct_available_or_report(reports, identifier)) {
return NULL;
}
/* create a new panel type */ /* create a new panel type */
pt = MEM_callocN(sizeof(PanelType), "python buttons panel"); pt = MEM_callocN(sizeof(PanelType), "python buttons panel");
@ -488,8 +491,12 @@ static StructRNA *rna_UIList_register(Main *bmain, ReportList *reports, void *da
/* check if we have registered this uilist type before, and remove it */ /* check if we have registered this uilist type before, and remove it */
ult = WM_uilisttype_find(dummyult.idname, true); ult = WM_uilisttype_find(dummyult.idname, true);
if (ult && ult->ext.srna) if (ult && ult->ext.srna) {
rna_UIList_unregister(bmain, ult->ext.srna); rna_UIList_unregister(bmain, ult->ext.srna);
}
if (!RNA_struct_available_or_report(reports, identifier)) {
return NULL;
}
/* create a new menu type */ /* create a new menu type */
ult = MEM_callocN(sizeof(uiListType) + over_alloc, "python uilist"); ult = MEM_callocN(sizeof(uiListType) + over_alloc, "python uilist");
@ -592,6 +599,9 @@ static StructRNA *rna_Header_register(Main *bmain, ReportList *reports, void *da
break; break;
} }
} }
if (!RNA_struct_available_or_report(reports, identifier)) {
return NULL;
}
/* create a new header type */ /* create a new header type */
ht = MEM_callocN(sizeof(HeaderType), "python buttons header"); ht = MEM_callocN(sizeof(HeaderType), "python buttons header");
@ -712,8 +722,12 @@ static StructRNA *rna_Menu_register(Main *bmain, ReportList *reports, void *data
/* check if we have registered this menu type before, and remove it */ /* check if we have registered this menu type before, and remove it */
mt = WM_menutype_find(dummymt.idname, true); mt = WM_menutype_find(dummymt.idname, true);
if (mt && mt->ext.srna) if (mt && mt->ext.srna) {
rna_Menu_unregister(bmain, mt->ext.srna); rna_Menu_unregister(bmain, mt->ext.srna);
}
if (!RNA_struct_available_or_report(reports, identifier)) {
return NULL;
}
/* create a new menu type */ /* create a new menu type */
if (_menu_descr[0]) { if (_menu_descr[0]) {

@ -638,10 +638,11 @@ static StructRNA *rna_AddonPref_register(Main *bmain, ReportList *reports, void
/* check if we have registered this header type before, and remove it */ /* check if we have registered this header type before, and remove it */
apt = BKE_addon_pref_type_find(dummyaddon.module, true); apt = BKE_addon_pref_type_find(dummyaddon.module, true);
if (apt) { if (apt && apt->ext.srna) {
if (apt->ext.srna) { rna_AddonPref_unregister(bmain, apt->ext.srna);
rna_AddonPref_unregister(bmain, apt->ext.srna); }
} if (!RNA_struct_available_or_report(reports, identifier)) {
return NULL;
} }
/* create a new header type */ /* create a new header type */

@ -1147,6 +1147,16 @@ static StructRNA *rna_Operator_register(
if (validate(&dummyotr, data, have_function) != 0) if (validate(&dummyotr, data, have_function) != 0)
return NULL; return NULL;
/* check if we have registered this operator type before, and remove it */
{
wmOperatorType *ot = WM_operatortype_find(dummyot.idname, true);
if (ot && ot->ext.srna)
rna_Operator_unregister(bmain, ot->ext.srna);
}
if (!RNA_struct_available_or_report(reports, identifier)) {
return NULL;
}
{ /* convert foo.bar to FOO_OT_bar { /* convert foo.bar to FOO_OT_bar
* allocate the description and the idname in 1 go */ * allocate the description and the idname in 1 go */
@ -1214,13 +1224,6 @@ static StructRNA *rna_Operator_register(
} }
} }
/* check if we have registered this operator type before, and remove it */
{
wmOperatorType *ot = WM_operatortype_find(dummyot.idname, true);
if (ot && ot->ext.srna)
rna_Operator_unregister(bmain, ot->ext.srna);
}
/* XXX, this doubles up with the operator name [#29666] /* XXX, this doubles up with the operator name [#29666]
* for now just remove from dir(bpy.types) */ * for now just remove from dir(bpy.types) */
@ -1323,6 +1326,16 @@ static StructRNA *rna_MacroOperator_register(
return NULL; return NULL;
} }
/* check if we have registered this operator type before, and remove it */
{
wmOperatorType *ot = WM_operatortype_find(dummyot.idname, true);
if (ot && ot->ext.srna)
rna_Operator_unregister(bmain, ot->ext.srna);
}
if (!RNA_struct_available_or_report(reports, identifier)) {
return NULL;
}
{ /* convert foo.bar to FOO_OT_bar { /* convert foo.bar to FOO_OT_bar
* allocate the description and the idname in 1 go */ * allocate the description and the idname in 1 go */
const uint idname_len = strlen(temp_buffers.idname) + 4; const uint idname_len = strlen(temp_buffers.idname) + 4;
@ -1349,13 +1362,6 @@ static StructRNA *rna_MacroOperator_register(
dummyot.undo_group = ch; dummyot.undo_group = ch;
} }
/* check if we have registered this operator type before, and remove it */
{
wmOperatorType *ot = WM_operatortype_find(dummyot.idname, true);
if (ot && ot->ext.srna)
rna_Operator_unregister(bmain, ot->ext.srna);
}
/* XXX, this doubles up with the operator name [#29666] /* XXX, this doubles up with the operator name [#29666]
* for now just remove from dir(bpy.types) */ * for now just remove from dir(bpy.types) */