Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:Siccity/xNode.git
Browse files Browse the repository at this point in the history
  • Loading branch information
Siccity committed Feb 17, 2019
2 parents 71defcb + 57d3a03 commit 9875f8d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
10 changes: 3 additions & 7 deletions Scripts/Editor/NodeEditorGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,10 @@ private void DrawNodes() {
selectionCache = new List<UnityEngine.Object>(Selection.objects);
}

//Active node is hashed before and after node GUI to detect changes
int nodeHash = 0;
System.Reflection.MethodInfo onValidate = null;
if (Selection.activeObject != null && Selection.activeObject is XNode.Node) {
onValidate = Selection.activeObject.GetType().GetMethod("OnValidate");
if (onValidate != null) nodeHash = Selection.activeObject.GetHashCode();
if (onValidate != null) EditorGUI.BeginChangeCheck();
}

BeginZoomed(position, zoom, topPadding);
Expand Down Expand Up @@ -383,12 +381,10 @@ private void DrawNodes() {
if (e.type != EventType.Layout && currentActivity == NodeActivity.DragGrid) Selection.objects = preSelection.ToArray();
EndZoomed(position, zoom, topPadding);

//If a change in hash is detected in the selected node, call OnValidate method.
//If a change in is detected in the selected node, call OnValidate method.
//This is done through reflection because OnValidate is only relevant in editor,
//and thus, the code should not be included in build.
if (nodeHash != 0) {
if (onValidate != null && nodeHash != Selection.activeObject.GetHashCode()) onValidate.Invoke(Selection.activeObject, null);
}
if (onValidate != null && EditorGUI.EndChangeCheck()) onValidate.Invoke(Selection.activeObject, null);
}

private bool ShouldBeCulled(XNode.Node node) {
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Editor/NodeEditorGUILayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public static void PropertyField(SerializedProperty property, GUIContent label,

private static System.Type GetType(SerializedProperty property) {
System.Type parentType = property.serializedObject.targetObject.GetType();
System.Reflection.FieldInfo fi = parentType.GetField(property.propertyPath);
System.Reflection.FieldInfo fi = NodeEditorWindow.GetFieldInfo(property.serializedObject.targetObject.GetType(), property.name);
return fi.FieldType;
}

Expand Down
9 changes: 9 additions & 0 deletions Scripts/Editor/NodeEditorReflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ public static Dictionary<Type, int> GetNodeWidth() {
return widths;
}

/// <summary> Get FieldInfo of a field, including those that are private and/or inherited </summary>
public static FieldInfo GetFieldInfo(Type type, string fieldName) {
// If we can't find field in the first run, it's probably a private field in a base class.
FieldInfo field = type.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
// Search base classes for private fields only. Public fields are found above
while (field == null && (type = type.BaseType) != typeof(XNode.Node)) field = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
return field;
}

/// <summary> Get all classes deriving from baseType via reflection </summary>
public static Type[] GetDerivedTypes(Type baseType) {
List<System.Type> types = new List<System.Type>();
Expand Down
4 changes: 1 addition & 3 deletions Scripts/Editor/NodeEditorUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public static bool GetAttrib<T>(object[] attribs, out T attribOut) where T : Att

public static bool GetAttrib<T>(Type classType, string fieldName, out T attribOut) where T : Attribute {
// If we can't find field in the first run, it's probably a private field in a base class.
FieldInfo field = classType.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
// Search base classes for private fields only. Public fields are found above
while (field == null && (classType = classType.BaseType) != typeof(XNode.Node)) field = classType.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
FieldInfo field = NodeEditorWindow.GetFieldInfo(classType, fieldName);
// This shouldn't happen. Ever.
if (field == null) {
Debug.LogWarning("Field " + fieldName + " couldnt be found");
Expand Down
4 changes: 0 additions & 4 deletions Scripts/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,6 @@ public void ClearConnections() {
foreach (NodePort port in Ports) port.ClearConnections();
}

public override int GetHashCode() {
return JsonUtility.ToJson(this).GetHashCode();
}

#region Attributes
/// <summary> Mark a serializable field as an input port. You can access this through <see cref="GetInputPort(string)"/> </summary>
[AttributeUsage(AttributeTargets.Field, AllowMultiple = true)]
Expand Down

0 comments on commit 9875f8d

Please sign in to comment.