From 8399d043f567ac1107954ff9f44fc6f6a5237a67 Mon Sep 17 00:00:00 2001 From: max Date: Wed, 4 May 2022 00:39:01 +0200 Subject: [PATCH] Update RequiredAttributeValidator Fix required attribute validator by adding flags when getting the fields. --- CHANGELOG.md | 4 +- .../Validators/RequiredAttributeValidator.cs | 39 +++++++------------ package.json | 2 +- 3 files changed, 17 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e2cb1f3..779347a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,9 @@ # Change Log: +## 0.1.4 +- Fix attribute validator..? ## 0.1.3 - Fix attribute validator..? -- ## 0.1.2 +## 0.1.2 - ... ## 0.1.1 - First prototype of Attribute validator. diff --git a/Editor/Validators/RequiredAttributeValidator.cs b/Editor/Validators/RequiredAttributeValidator.cs index 8d73d43..c8b27a9 100644 --- a/Editor/Validators/RequiredAttributeValidator.cs +++ b/Editor/Validators/RequiredAttributeValidator.cs @@ -10,6 +10,8 @@ namespace Validator.Editor { public string MenuName => "Attributes/RequiredAttributeAssetValidator"; + private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; + public Report Validate() { Report report = new Report(nameof(RequiredAttributeAssetValidator)); @@ -20,36 +22,19 @@ namespace Validator.Editor { EditorUtility.DisplayProgressBar("RequiredAttributeAssetValidator", "RequiredAttribute...", (float)i / objects.Count); - FieldInfo[] fields = objects[i].GetType().GetFields(); + IEnumerable<(FieldInfo FieldInfo, RequiredAttribute Attribute)> fieldsWithRequiredAttribute = from fi in objects[i].GetType().GetFields(flags) + let attr = fi.GetCustomAttributes(typeof(RequiredAttribute), true) + where attr.Length == 1 + select (FieldInfo: fi, Attribute: attr.First() as RequiredAttribute); - for (int j = 0; j < fields.Length; j++) + foreach ((FieldInfo FieldInfo, RequiredAttribute Attribute) field in fieldsWithRequiredAttribute) { - object[] attr = fields[j].GetCustomAttributes(typeof(RequiredAttribute), true); - - if(attr.Length > 0 && attr[0] is RequiredAttribute requiredAttribute) + object o = field.FieldInfo.GetValue(objects[i]); + if (o == null || o.Equals(null)) { - object o = fields[j].GetValue(objects[i]); - - if (o == null || o.Equals(null)) - { - report.Log(objects[i], requiredAttribute.WarningType, requiredAttribute.Category, $"{fields[j].Name} is null", $"Assign {fields[j].FieldType}"); - } + report.Log(objects[i], field.Attribute.WarningType, field.Attribute.Category, $"{field.FieldInfo.Name} is null", $"Assign {field.FieldInfo.FieldType}"); } } - - //IEnumerable<(FieldInfo FieldInfo, RequiredAttribute Attribute)> fieldsWithRequiredAttribute = from fi in objects[i].GetType().GetFields() - // let attr = fi.GetCustomAttributes(typeof(RequiredAttribute), true) - // where attr.Length == 1 - // select (FieldInfo: fi, Attribute: attr.First() as RequiredAttribute); - - //foreach ((FieldInfo FieldInfo, RequiredAttribute Attribute) field in fieldsWithRequiredAttribute) - //{ - // object o = field.FieldInfo.GetValue(objects[i]); - // if (o == null || o.Equals(null)) - // { - // report.Log(objects[i], field.Attribute.WarningType, field.Attribute.Category, $"{field.FieldInfo.Name} is null", $"Assign {field.FieldInfo.FieldType}"); - // } - //} } EditorUtility.ClearProgressBar(); @@ -61,6 +46,8 @@ namespace Validator.Editor { public string MenuName => "Attributes/RequiredAttributeSceneValidator"; + private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; + public Report Validate() { Report report = new Report(nameof(RequiredAttributeSceneValidator)); @@ -70,7 +57,7 @@ namespace Validator.Editor for (int i = 0; i < objects.Count; i++) { EditorUtility.DisplayProgressBar("RequiredAttributeSceneValidator", "RequiredAttribute...", (float)i / objects.Count); - IEnumerable<(FieldInfo FieldInfo, RequiredAttribute Attribute)> fieldsWithRequiredAttribute = from fi in objects[i].GetType().GetFields() + IEnumerable<(FieldInfo FieldInfo, RequiredAttribute Attribute)> fieldsWithRequiredAttribute = from fi in objects[i].GetType().GetFields(flags) let attr = fi.GetCustomAttributes(typeof(RequiredAttribute), true) where attr.Length == 1 select (FieldInfo: fi, Attribute: attr.First() as RequiredAttribute); diff --git a/package.json b/package.json index 9bbb6f7..8895f03 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.vertexcolor.validator", "displayName": "Validator", - "version": "0.1.3", + "version": "0.1.4", "unity": "2019.3", "description": "Unity project validator framework.", "category": "Tool",