Skip to content

Commit

Permalink
Reflection fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
App24 committed May 18, 2023
1 parent 5d10c49 commit 807b81c
Show file tree
Hide file tree
Showing 41 changed files with 104 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using RicUtils.ScriptableObjects;
using RicUtils.Utilities;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;

Expand Down Expand Up @@ -75,7 +77,7 @@ public override void OnInspectorGUI()
{
items.RemoveAt(j);
}
availableScriptableObjectType.GetMethod("SetItems", System.Reflection.BindingFlags.NonPublic).Invoke(availableScriptableObject, new object[] { items });
availableScriptableObjectType.GetRecursiveMethod("SetItems", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(availableScriptableObject, new object[] { items });

EditorUtility.SetDirty(availableScriptableObject);
AssetDatabase.SaveAssets();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RicUtils;
using RicUtils.ScriptableObjects;

#ROOTNAMESPACEBEGIN#
public class #SCRIPTNAME# : AvailableScriptableObject<#SCRIPTABLEOBJECT#>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RicUtils;
using RicUtils.Editor;
using RicUtils.Editor.Windows;
using UnityEditor;

#ROOTNAMESPACEBEGIN#
public class #SCRIPTNAME# : GenericEditorWindow<#SCRIPTABLEOBJECT#, #AVAILABLESCRIPTABLEOBJECT#>
{
[MenuItem("Window/RicUtil Windows/#SCRIPTNAME#")]
public static #SCRIPTNAME# ShowWindow()
{
return GetWindow<#SCRIPTNAME#>();
Expand All @@ -21,5 +23,10 @@ public class #SCRIPTNAME# : GenericEditorWindow<#SCRIPTABLEOBJECT#, #AVAILABLESC
{

}

protected override void LoadScriptableObject(#SCRIPTABLEOBJECT# so, bool isNull)
{

}
}
#ROOTNAMESPACEEND#
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RicUtils;
using RicUtils.ScriptableObjects;

#ROOTNAMESPACEBEGIN#
public class #SCRIPTNAME# : GenericScriptableObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void CreateAvailableScripts()
{
available = ScriptableObject.CreateInstance(keyValuePair.AvailableScriptableObjectType);
var items = (IList)System.Activator.CreateInstance(typeof(List<>).MakeGenericType(keyValuePair.CustomScriptableObjectType));
keyValuePair.AvailableScriptableObjectType.GetMethod("SetItems", System.Reflection.BindingFlags.NonPublic).Invoke(available, new object[] { items });
keyValuePair.AvailableScriptableObjectType.GetRecursiveMethod("SetItems", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Invoke(available, new object[] { items });

AssetDatabase.CreateAsset(available, path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static AssetDeleteResult OnWillDeleteAsset(string AssetPath, RemoveAssetO
items.Add(item);
}
items.Remove(temp);
availableScriptableObjectType.GetMethod("SetItems", BindingFlags.NonPublic).Invoke(availableScriptableObject, new object[] { items });
availableScriptableObjectType.GetRecursiveMethod("SetItems", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(availableScriptableObject, new object[] { items });

EditorUtility.SetDirty(availableScriptableObject);

Expand Down
5 changes: 3 additions & 2 deletions Assets/RicUtils/Editor/Utilities/ToolUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using RicUtils.Editor.Settings;
using RicUtils.ScriptableObjects;
using RicUtils.Utilities;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
Expand Down Expand Up @@ -81,8 +82,8 @@ private static bool OpenScriptableObjectFile(GenericScriptableObject so)
}
var temp = showWindow.Invoke(null, null);
var data = System.Convert.ChangeType(temp, keyValuePair.EditorType);
keyValuePair.EditorType.GetField("scriptableObject").SetValue(data, so);
keyValuePair.EditorType.GetMethod("LoadScriptableObject").Invoke(data, new object[] { so, so == null });
keyValuePair.EditorType.GetRecursiveField("scriptableObject", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).SetValue(data, so);
keyValuePair.EditorType.GetRecursiveMethod("LoadScriptableObjectInternal", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(data, new object[] { so });
return true;
}
}
Expand Down
11 changes: 6 additions & 5 deletions Assets/RicUtils/Editor/Windows/GenericEditorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private void OnGUI()
{
EditorGUIHelper.DrawObjectField(ref scriptableObject, "Scriptable Object", () =>
{
LoadScriptableObjectInternal(scriptableObject, scriptableObject == null);
LoadScriptableObjectInternal(scriptableObject);
});

DrawIDInput(ref spawnableId);
Expand All @@ -42,14 +42,14 @@ protected virtual void OnEnable()
serializedObject = new SerializedObject(this);
}

protected void DrawIDInput(ref string id)
private void DrawIDInput(ref string id)
{
GUI.enabled = scriptableObject == null;
EditorGUIHelper.DrawStringInput(ref id, "ID");
GUI.enabled = true;
}

protected void DrawSaveDeleteButtons(bool checkExists = true)
private void DrawSaveDeleteButtons(bool checkExists = true)
{
EditorGUIHelper.DrawSeparator();
List<CompleteCriteria> criteria = new List<CompleteCriteria>(GetInbuiltCompleteCriteria());
Expand Down Expand Up @@ -118,7 +118,7 @@ protected void DrawSaveDeleteButtons(bool checkExists = true)
AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(scriptableObject));

scriptableObject = null;
LoadScriptableObjectInternal(scriptableObject, true);
LoadScriptableObjectInternal(scriptableObject);
}
EditorGUI.EndDisabledGroup();
}
Expand Down Expand Up @@ -151,8 +151,9 @@ private void SaveAsset(T asset, string saveName)
AssetDatabase.CreateAsset(asset, $"{SavePath}/{saveName}.asset");
}

private void LoadScriptableObjectInternal(T so, bool isNull)
private void LoadScriptableObjectInternal(T so)
{
bool isNull = so == null;
if (isNull)
{
spawnableId = "";
Expand Down
8 changes: 8 additions & 0 deletions Assets/RicUtils/Runtime/Scripts.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
60 changes: 60 additions & 0 deletions Assets/RicUtils/Runtime/Scripts/Utilities/ReflectionUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;

namespace RicUtils.Utilities
{
public static class ReflectionUtilities
{
public static IEnumerable<MethodInfo> GetRecursiveMethods(this Type type, BindingFlags bindingFlags)
{
IEnumerable<MethodInfo> methods = type.GetMethods(bindingFlags);

if (type.BaseType != null)
{
methods = methods.Concat(type.BaseType.GetRecursiveMethods(bindingFlags));
}

return methods;
}

public static MethodInfo GetRecursiveMethod(this Type type, string name, BindingFlags bindingFlags)
{
var method = type.GetMethod(name, bindingFlags);

if (method == null && type.BaseType != null)
{
method = type.BaseType.GetRecursiveMethod(name, bindingFlags);
}

return method;
}

public static IEnumerable<FieldInfo> GetRecursiveFields(this Type type, BindingFlags bindingFlags)
{
IEnumerable<FieldInfo> methods = type.GetFields(bindingFlags);

if (type.BaseType != null)
{
methods = methods.Concat(type.BaseType.GetRecursiveFields(bindingFlags));
}

return methods;
}

public static FieldInfo GetRecursiveField(this Type type, string name, BindingFlags bindingFlags)
{
var method = type.GetField(name, bindingFlags);

if (method == null && type.BaseType != null)
{
method = type.BaseType.GetRecursiveField(name, bindingFlags);
}

return method;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 807b81c

Please sign in to comment.