Skip to content

Commit 2a309c9

Browse files
authored
Merge pull request #35 from DerpyNewbie/bug/fix-newbie-inject-non-serialized
Fix Inject throwing NRE on a non-serialized NewbieInject field
2 parents 648c1a1 + 9e834c6 commit 2a309c9

File tree

1 file changed

+52
-16
lines changed

1 file changed

+52
-16
lines changed

Packages/dev.derpynewbie.common/Editor/NewbieInjectProcessor.cs

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static void Inject(Scene scene, bool showProgress = true, bool printResul
3838

3939
if (showProgress && EditorUtility.DisplayCancelableProgressBar(
4040
"Injecting Reference",
41-
$"{injectFields.Count}/{ProcessedFieldCount} Injecting field `{field.Module.Name}:{field.Name}:{field.FieldType.Name}({injectOption.Scope.ToString()})` for `{GetHierarchyName(component)}`",
41+
$"{injectFields.Count}/{ProcessedFieldCount} Injecting field `{GetFieldName(field)}({injectOption.Scope.ToString()})` for `{GetHierarchyName(component)}`",
4242
injectFields.Count / (float)ProcessedFieldCount))
4343
{
4444
EditorUtility.ClearProgressBar();
@@ -62,7 +62,7 @@ public static void Inject(Scene scene, bool showProgress = true, bool printResul
6262
foundComponentsDict.Add(field, injectingComponents);
6363

6464
Log(
65-
$"Found Scene scoped component `{field.FieldType.FullName}` at `{injectingComponents.Select(GetHierarchyName).Join()}`");
65+
$"Found Scene scoped component `{GetFieldName(field)}` at `{injectingComponents.Select(GetHierarchyName).Join()}`");
6666
}
6767

6868
break;
@@ -80,7 +80,7 @@ public static void Inject(Scene scene, bool showProgress = true, bool printResul
8080
injectingComponents = go.GetComponents(GetComponentType(field.FieldType)).ToList();
8181
goSearchCache.Add(go, injectingComponents);
8282
Log(
83-
$"Found Self scoped component `{GetHierarchyName(go)}:{field.FieldType.FullName}` at `{injectingComponents.Select(GetHierarchyName).Join()}`");
83+
$"Found Self scoped component `{GetHierarchyName(go)}:{GetFieldName(field)}` at `{injectingComponents.Select(GetHierarchyName).Join()}`");
8484
}
8585

8686
break;
@@ -100,7 +100,7 @@ public static void Inject(Scene scene, bool showProgress = true, bool printResul
100100
goSearchCache.Add(go, injectingComponents);
101101

102102
Log(
103-
$"Found Children scoped component `{GetHierarchyName(go)}:{field.FieldType.FullName}` at `{injectingComponents.Select(GetHierarchyName).Join()}`");
103+
$"Found Children scoped component `{GetHierarchyName(go)}:{GetFieldName(field)}` at `{injectingComponents.Select(GetHierarchyName).Join()}`");
104104
}
105105

106106
break;
@@ -119,7 +119,7 @@ public static void Inject(Scene scene, bool showProgress = true, bool printResul
119119
.GetComponentsInParent(GetComponentType(field.FieldType)).ToList();
120120
goSearchCache.Add(go, injectingComponents);
121121
Log(
122-
$"Found Parents scoped component `{GetHierarchyName(go)}:{field.FieldType.FullName}` at `{injectingComponents.Select(GetHierarchyName).Join()}`");
122+
$"Found Parents scoped component `{GetHierarchyName(go)}:{GetFieldName(field)}` at `{injectingComponents.Select(GetHierarchyName).Join()}`");
123123
}
124124

125125
break;
@@ -128,6 +128,13 @@ public static void Inject(Scene scene, bool showProgress = true, bool printResul
128128

129129
var serializedObject = new SerializedObject(component);
130130
var serializedProperty = serializedObject.FindProperty(field.Name);
131+
if (serializedProperty == null)
132+
{
133+
Debug.LogWarning(
134+
$"Field `{GetFieldName(field)}` is marked with NewbieInject attribute, but is not found in component `{GetHierarchyName(component)}`.");
135+
continue;
136+
}
137+
131138
if (field.FieldType.IsArray)
132139
{
133140
serializedProperty.ClearArray();
@@ -154,13 +161,17 @@ public static void Inject(Scene scene, bool showProgress = true, bool printResul
154161

155162
if (printResult)
156163
{
157-
var sb = new StringBuilder("Scene Injection Result:" +
158-
"\n====== Scene Search ======");
164+
var sb = new StringBuilder("Scene Injection Result:");
165+
166+
sb.Append(
167+
$"\n{ProcessedFieldCount} fields affected, {ComponentUpdateCount} component updates in {stopwatch.ElapsedMilliseconds} ms.");
168+
169+
sb.Append("\n\n====== Scene Search ======");
159170

160171
foreach (var pair in foundComponentsDict)
161172
sb.Append("\n")
162-
.Append(pair.Key.FieldType.FullName).Append(", ")
163-
.Append(pair.Value.Select(GetHierarchyName).Join());
173+
.Append(GetFieldName(pair.Key)).Append(", ")
174+
.Append('{').Append(pair.Value.Select(GetHierarchyName).Join()).Append('}');
164175

165176
sb.Append("\n\n====== GameObject Search ======");
166177

@@ -169,18 +180,15 @@ public static void Inject(Scene scene, bool showProgress = true, bool printResul
169180
foreach (var goSearch in pair.Value)
170181
{
171182
sb.Append("\n")
172-
.Append(pair.Key.FieldType.FullName)
173-
.Append(":")
183+
.Append(GetFieldName(pair.Key))
184+
.Append(':')
174185
.Append(pair.Key.GetCustomAttribute<NewbieInject>().Scope.ToString())
175186
.Append(", ")
176-
.Append("(").Append(GetHierarchyName(goSearch.Key)).Append("), ")
177-
.Append(goSearch.Value.Select(GetHierarchyName).Join());
187+
.Append('{').Append(GetHierarchyName(goSearch.Key)).Append("}, ")
188+
.Append('{').Append(goSearch.Value.Select(GetHierarchyName).Join()).Append('}');
178189
}
179190
}
180191

181-
sb.Append(
182-
$"\n\n{ProcessedFieldCount} fields affected, {ComponentUpdateCount} component updates in {stopwatch.ElapsedMilliseconds} ms.");
183-
184192
Log(sb.ToString());
185193
}
186194
}
@@ -199,6 +207,13 @@ public static void Clear(Scene scene)
199207
{
200208
var serializedObject = new SerializedObject(component);
201209
var serializedProperty = serializedObject.FindProperty(field.Name);
210+
if (serializedProperty == null)
211+
{
212+
Debug.LogWarning(
213+
$"Field `{field.Module.Name}:{field.Name}:{field.FieldType.Name}` is marked with NewbieInject attribute, but is not found in component `{GetHierarchyName(component)}`.");
214+
continue;
215+
}
216+
202217
if (field.FieldType.IsArray)
203218
{
204219
serializedProperty.ClearArray();
@@ -227,7 +242,21 @@ public static List<FieldInfo> GetInjectFields()
227242
foreach (var type in asm.GetTypes())
228243
foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
229244
if (field.IsDefined(typeof(NewbieInject), true))
245+
{
246+
if (!field.IsDefined(typeof(SerializeField), true) && field.IsPrivate)
247+
{
248+
Debug.LogWarning(
249+
$"Field `{GetFieldName(field)}` is marked with NewbieInject attribute, but is not marked with SerializeField attribute. This may cause unexpected behaviour.");
250+
}
251+
252+
if (field.IsDefined(typeof(NonSerializedAttribute), true))
253+
{
254+
Debug.LogWarning(
255+
$"Field `{GetFieldName(field)}` is marked with NewbieInject attribute, but is marked with NonSerialized attribute. This may cause unexpected behaviour.");
256+
}
257+
230258
result.Add(field);
259+
}
231260

232261
return result;
233262
}
@@ -269,6 +298,13 @@ public static string GetHierarchyName(Transform t)
269298
return sb.ToString();
270299
}
271300

301+
public static string GetFieldName(FieldInfo field)
302+
{
303+
return field.DeclaringType != null
304+
? $"{field.FieldType.Name} {field.DeclaringType.FullName}#{field.Name}"
305+
: $"{field.FieldType.Name} {field.Name}";
306+
}
307+
272308
public static void DoPrePlayInject(PlayModeStateChange change)
273309
{
274310
if (!NewbieInjectConfig.InjectOnPlay)

0 commit comments

Comments
 (0)