Skip to content

Commit ee6543a

Browse files
committed
Implemented argument checks on event listeners custom inspectors
1 parent 3a007a5 commit ee6543a

8 files changed

Lines changed: 124 additions & 39 deletions

CustomScriptTemplates/SOEventListenerTemplate.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using SOBaseEvents;
12
<CUSTOM_NAMESPACE_LIST>
23
<NAMESPACE_DECLARATION_START>
34
public class <SCRIPT_NAME> : EventListener<<SO_EVENT_NAME><ARGUMENT_TYPE_LIST>>

CustomScriptTemplates/SOEventTemplate.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using SOBaseEvents;
12
using UnityEngine;
23
<CUSTOM_NAMESPACE_LIST>
34
<NAMESPACE_DECLARATION_START>

Editor/EditorUI/BaseEventListenerCustomInspector.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using UnityEditor;
22
using System;
3+
using System.Linq;
34
using UnityEngine;
45

56
namespace Editor.EditorUI
@@ -9,10 +10,15 @@ public abstract class BaseEventListenerCustomInspector : UnityEditor.Editor
910
protected const string EventSOVariableName = "_baseEvent";
1011

1112
protected SerializedProperty _baseEventProp;
13+
protected Type[] _tArgs;
14+
protected string _types;
15+
16+
protected abstract bool ValidateEventType(System.Object obj);
1217

1318
protected virtual void OnEnable()
1419
{
1520
_baseEventProp = serializedObject.FindProperty(EventSOVariableName);
21+
_types = string.Empty;
1622
}
1723

1824
protected void DrawEventInspector(string label, string tooltip, Func<UnityEngine.Object, bool> validationLogic)
@@ -38,7 +44,45 @@ protected void DrawEventInspector(string label, string tooltip, Func<UnityEngine
3844
DrawPropertiesExcluding(serializedObject, "m_Script", EventSOVariableName);
3945
serializedObject.ApplyModifiedProperties();
4046
}
47+
48+
protected void GetArgTypes(Type type)
49+
{
50+
if (type != null)
51+
{
52+
var allArgs = type.GetGenericArguments();
53+
54+
//Skipping the first one. Not sure if this is always applicable
55+
_tArgs = new Type[allArgs.Length - 1];
56+
for (int i = 1; i< allArgs.Length; i++)
57+
{
58+
_tArgs[i - 1] = allArgs[i];
59+
_types += _tArgs[i-1].Name;
60+
if (i < allArgs.Length - 1)
61+
{
62+
_types +=", ";
63+
}
64+
}
65+
}
66+
}
67+
68+
protected bool ValidateArgCountAndTypes(Type @interface)
69+
{
70+
var args = @interface.GetGenericArguments();
71+
72+
if (args.Length == _tArgs.Length)
73+
{
74+
for (int i = 0; i < args.Length; i++)
75+
{
76+
if (args[i] != _tArgs[i])
77+
{
78+
return false;
79+
}
80+
}
81+
82+
return true;
83+
}
4184

42-
protected abstract bool ValidationLogic(System.Object obj);
85+
return false;
86+
}
4387
}
4488
}

Editor/EditorUI/EventListener1ArgCustomInspector.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ namespace Editor.EditorUI
88
[CanEditMultipleObjects]
99
public class EventListener1ArgCustomInspector : BaseEventListenerCustomInspector
1010
{
11-
Type _tArg;
11+
Type[] _tArgs;
12+
private string _types;
1213

1314
private void OnEnable()
1415
{
@@ -20,23 +21,35 @@ private void OnEnable()
2021
type = type.BaseType;
2122
}
2223

23-
if (type != null)
24-
{
25-
_tArg = type.GetGenericArguments()[1];
26-
}
24+
GetArgTypes(type);
2725
}
2826

2927
public override void OnInspectorGUI()
3028
{
3129
DrawEventInspector("Event Asset",
32-
$"Must implement ISOEventRegistry<{(_tArg != null ? _tArg.Name : "T")}>",
33-
obj => ValidationLogic(obj));
30+
$"Must implement ISOEventRegistry<{(_tArgs != null ? _types : "T")}>",
31+
obj => ValidateEventType(obj));
3432
}
35-
36-
protected override bool ValidationLogic(Object obj)
33+
34+
protected override bool ValidateEventType(Object obj)
3735
{
38-
var registryInterface = typeof(ISOEventRegistry<>).MakeGenericType(_tArg);
39-
return obj is ISOEventBase && registryInterface.IsAssignableFrom(obj.GetType());
36+
if (!(obj is ISOEventBase))
37+
{
38+
return false;
39+
}
40+
41+
var objType = obj.GetType();
42+
var argsValidated = false;
43+
44+
foreach (var iface in objType.GetInterfaces())
45+
{
46+
if (iface.IsGenericType && iface.GetGenericTypeDefinition() == typeof(ISOEventRegistry<>))
47+
{
48+
argsValidated |= ValidateArgCountAndTypes(iface);
49+
}
50+
}
51+
52+
return argsValidated;
4053
}
4154
}
4255
}

Editor/EditorUI/EventListener2ArgCustomInspector.cs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ namespace Editor.EditorUI
88
[CanEditMultipleObjects]
99
public class EventListener2ArgCustomInspector : BaseEventListenerCustomInspector
1010
{
11-
Type _tArg;
11+
Type[] _tArgs;
12+
private string _types;
1213

1314
private void OnEnable()
1415
{
@@ -20,23 +21,35 @@ private void OnEnable()
2021
type = type.BaseType;
2122
}
2223

23-
if (type != null)
24-
{
25-
_tArg = type.GetGenericArguments()[1];
26-
}
24+
GetArgTypes(type);
2725
}
2826

2927
public override void OnInspectorGUI()
3028
{
3129
DrawEventInspector("Event Asset",
32-
$"Must implement ISOEventRegistry<{(_tArg != null ? _tArg.Name : "T")}>",
33-
obj => ValidationLogic(obj));
30+
$"Must implement ISOEventRegistry<{(_tArgs != null ? _types : "T")}>",
31+
obj => ValidateEventType(obj));
3432
}
3533

36-
protected override bool ValidationLogic(Object obj)
34+
protected override bool ValidateEventType(Object obj)
3735
{
38-
var registryInterface = typeof(ISOEventRegistry<,>).MakeGenericType(_tArg);
39-
return obj is ISOEventBase && registryInterface.IsAssignableFrom(obj.GetType());
36+
if (!(obj is ISOEventBase))
37+
{
38+
return false;
39+
}
40+
41+
var objType = obj.GetType();
42+
var argsValidated = false;
43+
44+
foreach (var iface in objType.GetInterfaces())
45+
{
46+
if (iface.IsGenericType && iface.GetGenericTypeDefinition() == typeof(ISOEventRegistry<,>))
47+
{
48+
argsValidated |= ValidateArgCountAndTypes(iface);
49+
}
50+
}
51+
52+
return argsValidated;
4053
}
4154
}
4255
}
Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using UnityEditor;
22
using System;
3+
using System.Linq;
34
using SOBaseEvents;
45

56
namespace Editor.EditorUI
@@ -8,35 +9,47 @@ namespace Editor.EditorUI
89
[CanEditMultipleObjects]
910
public class EventListener3ArgCustomInspector : BaseEventListenerCustomInspector
1011
{
11-
Type _tArg;
12-
1312
private void OnEnable()
1413
{
1514
base.OnEnable();
15+
1616
var type = target.GetType();
1717

18-
while (type != null && (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(EventListener<,,,>)))
18+
while (type != null
19+
&& (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(EventListener<,,,>)))
1920
{
2021
type = type.BaseType;
2122
}
22-
23-
if (type != null)
24-
{
25-
_tArg = type.GetGenericArguments()[1];
26-
}
23+
24+
GetArgTypes(type);
2725
}
28-
26+
2927
public override void OnInspectorGUI()
3028
{
3129
DrawEventInspector("Event Asset",
32-
$"Must implement ISOEventRegistry<{(_tArg != null ? _tArg.Name : "T")}>",
33-
obj => ValidationLogic(obj));
30+
$"Must implement ISOEventRegistry<{(_tArgs != null ? _types : "T")}>",
31+
obj => ValidateEventType(obj));
3432
}
3533

36-
protected override bool ValidationLogic(Object obj)
34+
protected override bool ValidateEventType(Object obj)
3735
{
38-
var registryInterface = typeof(ISOEventRegistry<,,>).MakeGenericType(_tArg);
39-
return obj is ISOEventBase && registryInterface.IsAssignableFrom(obj.GetType());
36+
if (!(obj is ISOEventBase))
37+
{
38+
return false;
39+
}
40+
41+
var objType = obj.GetType();
42+
var argsValidated = false;
43+
44+
foreach (var iface in objType.GetInterfaces())
45+
{
46+
if (iface.IsGenericType && iface.GetGenericTypeDefinition() == typeof(ISOEventRegistry<,,>))
47+
{
48+
argsValidated |= ValidateArgCountAndTypes(iface);
49+
}
50+
}
51+
52+
return argsValidated;
4053
}
4154
}
4255
}

Editor/EditorUI/EventListenerCustomInspector.cs renamed to Editor/EditorUI/EventListenerNoArgsCustomInspector.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ namespace Editor.EditorUI
66
{
77
[CustomEditor(typeof(EventListener<>), true)]
88
[CanEditMultipleObjects]
9-
public class EventListenerCustomInspector : BaseEventListenerCustomInspector
9+
public class EventListenerNoArgsCustomInspector : BaseEventListenerCustomInspector
1010
{
1111
public override void OnInspectorGUI()
1212
{
1313
DrawEventInspector("Event Asset (Void)",
1414
"Must implement ISOEventRegistry",
15-
obj => ValidationLogic(obj));
15+
obj => ValidateEventType(obj));
1616
}
1717

18-
protected override bool ValidationLogic(Object obj)
18+
protected override bool ValidateEventType(Object obj)
1919
{
2020
return obj is ISOEventBase && obj is ISOEventRegistry;
2121
}

Editor/EditorUI/EventListenerCustomInspector.cs.meta renamed to Editor/EditorUI/EventListenerNoArgsCustomInspector.cs.meta

File renamed without changes.

0 commit comments

Comments
 (0)