2020-08-29 21:15:54 +10:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2020-09-18 18:38:11 +10:00
|
|
|
|
using UnityEngine;
|
2020-10-08 06:15:42 +11:00
|
|
|
|
using Explorer.UI.Shared;
|
2020-08-29 21:15:54 +10:00
|
|
|
|
|
2020-10-08 06:15:42 +11:00
|
|
|
|
namespace Explorer.CacheObject
|
2020-08-29 21:15:54 +10:00
|
|
|
|
{
|
2020-10-08 06:15:42 +11:00
|
|
|
|
public class CacheMethod : CacheMember
|
2020-08-29 21:15:54 +10:00
|
|
|
|
{
|
|
|
|
|
private CacheObjectBase m_cachedReturnValue;
|
|
|
|
|
|
2020-09-18 18:03:17 +10:00
|
|
|
|
public override bool HasParameters => base.HasParameters || GenericArgs.Length > 0;
|
|
|
|
|
|
2020-10-08 06:15:42 +11:00
|
|
|
|
public override bool IsStatic => (MemInfo as MethodInfo).IsStatic;
|
|
|
|
|
|
2020-09-18 18:03:17 +10:00
|
|
|
|
public Type[] GenericArgs { get; private set; }
|
2020-09-19 01:27:33 +10:00
|
|
|
|
public Type[][] GenericConstraints { get; private set; }
|
2020-09-18 18:03:17 +10:00
|
|
|
|
|
|
|
|
|
public string[] GenericArgInput = new string[0];
|
|
|
|
|
|
2020-10-08 06:15:42 +11:00
|
|
|
|
public override void InitMember(MemberInfo member, object declaringInstance)
|
2020-09-18 18:03:17 +10:00
|
|
|
|
{
|
2020-10-08 06:15:42 +11:00
|
|
|
|
base.InitMember(member, declaringInstance);
|
|
|
|
|
|
|
|
|
|
var mi = MemInfo as MethodInfo;
|
2020-09-18 18:03:17 +10:00
|
|
|
|
GenericArgs = mi.GetGenericArguments();
|
|
|
|
|
|
2020-09-19 01:27:33 +10:00
|
|
|
|
GenericConstraints = GenericArgs.Select(x => x.GetGenericParameterConstraints())
|
2020-09-18 18:03:17 +10:00
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
GenericArgInput = new string[GenericArgs.Length];
|
|
|
|
|
|
2020-10-08 06:15:42 +11:00
|
|
|
|
m_arguments = mi.GetParameters();
|
|
|
|
|
m_argumentInput = new string[m_arguments.Length];
|
|
|
|
|
|
|
|
|
|
base.Init(null, mi.ReturnType);
|
2020-09-18 18:03:17 +10:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 21:15:54 +10:00
|
|
|
|
public override void UpdateValue()
|
|
|
|
|
{
|
2020-10-08 06:15:42 +11:00
|
|
|
|
// CacheMethod cannot UpdateValue directly. Need to Evaluate.
|
2020-09-07 17:05:37 +10:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-09 19:15:47 +10:00
|
|
|
|
public void Evaluate()
|
2020-09-07 17:05:37 +10:00
|
|
|
|
{
|
2020-09-18 23:10:46 +10:00
|
|
|
|
MethodInfo mi;
|
2020-09-18 18:03:17 +10:00
|
|
|
|
if (GenericArgs.Length > 0)
|
|
|
|
|
{
|
2020-09-18 23:10:46 +10:00
|
|
|
|
mi = MakeGenericMethodFromInput();
|
|
|
|
|
if (mi == null) return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mi = MemInfo as MethodInfo;
|
2020-09-18 18:03:17 +10:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-18 23:10:46 +10:00
|
|
|
|
object ret = null;
|
|
|
|
|
|
|
|
|
|
try
|
2020-09-07 17:05:37 +10:00
|
|
|
|
{
|
2020-09-18 23:10:46 +10:00
|
|
|
|
ret = mi.Invoke(mi.IsStatic ? null : DeclaringInstance, ParseArguments());
|
2020-09-07 17:05:37 +10:00
|
|
|
|
m_evaluated = true;
|
2020-09-18 23:10:46 +10:00
|
|
|
|
m_isEvaluating = false;
|
2020-09-07 17:05:37 +10:00
|
|
|
|
}
|
2020-09-18 23:10:46 +10:00
|
|
|
|
catch (Exception e)
|
2020-09-07 17:05:37 +10:00
|
|
|
|
{
|
2020-09-27 22:04:23 +10:00
|
|
|
|
ExplorerCore.LogWarning($"Exception evaluating: {e.GetType()}, {e.Message}");
|
2020-09-18 23:10:46 +10:00
|
|
|
|
ReflectionException = ReflectionHelpers.ExceptionToString(e);
|
2020-09-07 17:05:37 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ret != null)
|
|
|
|
|
{
|
2020-10-08 06:15:42 +11:00
|
|
|
|
//m_cachedReturnValue = CacheFactory.GetTypeAndCacheObject(ret);
|
|
|
|
|
m_cachedReturnValue = CacheFactory.GetCacheObject(ret, IValue.ValueType);
|
2020-09-07 17:05:37 +10:00
|
|
|
|
m_cachedReturnValue.UpdateValue();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m_cachedReturnValue = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-18 23:10:46 +10:00
|
|
|
|
private MethodInfo MakeGenericMethodFromInput()
|
|
|
|
|
{
|
|
|
|
|
var mi = MemInfo as MethodInfo;
|
|
|
|
|
|
|
|
|
|
var list = new List<Type>();
|
|
|
|
|
for (int i = 0; i < GenericArgs.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var input = GenericArgInput[i];
|
|
|
|
|
if (ReflectionHelpers.GetTypeByName(input) is Type t)
|
|
|
|
|
{
|
2020-09-19 01:27:33 +10:00
|
|
|
|
if (GenericConstraints[i].Length == 0)
|
2020-09-18 23:10:46 +10:00
|
|
|
|
{
|
|
|
|
|
list.Add(t);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-09-19 01:27:33 +10:00
|
|
|
|
foreach (var constraint in GenericConstraints[i].Where(x => x != null))
|
2020-09-18 23:10:46 +10:00
|
|
|
|
{
|
2020-10-08 06:15:42 +11:00
|
|
|
|
if (!constraint.IsAssignableFrom(t))
|
|
|
|
|
{
|
2020-09-27 22:04:23 +10:00
|
|
|
|
ExplorerCore.LogWarning($"Generic argument #{i}, '{input}' is not assignable from the constraint '{constraint}'!");
|
2020-09-19 01:27:33 +10:00
|
|
|
|
return null;
|
2020-10-08 06:15:42 +11:00
|
|
|
|
}
|
2020-09-18 23:10:46 +10:00
|
|
|
|
}
|
2020-09-19 01:27:33 +10:00
|
|
|
|
|
|
|
|
|
list.Add(t);
|
2020-09-18 23:10:46 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-09-27 22:04:23 +10:00
|
|
|
|
ExplorerCore.LogWarning($"Generic argument #{i}, could not get any type by the name of '{input}'!" +
|
2020-09-18 23:10:46 +10:00
|
|
|
|
$" Make sure you use the full name, including the NameSpace.");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// make into a generic with type list
|
|
|
|
|
mi = mi.MakeGenericMethod(list.ToArray());
|
|
|
|
|
|
|
|
|
|
return mi;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 17:05:37 +10:00
|
|
|
|
// ==== GUI DRAW ====
|
2020-08-30 01:08:48 +10:00
|
|
|
|
|
2020-10-08 06:15:42 +11:00
|
|
|
|
//public override void Draw(Rect window, float width)
|
|
|
|
|
//{
|
|
|
|
|
// base.Draw(window, width);
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
public void DrawValue(Rect window, float width)
|
2020-08-29 21:15:54 +10:00
|
|
|
|
{
|
2020-10-08 06:15:42 +11:00
|
|
|
|
string typeLabel = $"<color={Syntax.Class_Instance}>{IValue.ValueType.FullName}</color>";
|
2020-09-16 20:03:57 +10:00
|
|
|
|
|
2020-08-29 21:15:54 +10:00
|
|
|
|
if (m_evaluated)
|
|
|
|
|
{
|
|
|
|
|
if (m_cachedReturnValue != null)
|
|
|
|
|
{
|
2020-10-08 06:15:42 +11:00
|
|
|
|
m_cachedReturnValue.IValue.DrawValue(window, width);
|
2020-08-29 21:15:54 +10:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-09-27 22:04:23 +10:00
|
|
|
|
GUILayout.Label($"null ({typeLabel})", new GUILayoutOption[0]);
|
2020-08-29 21:15:54 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-09-27 22:04:23 +10:00
|
|
|
|
GUILayout.Label($"<color=grey><i>Not yet evaluated</i></color> ({typeLabel})", new GUILayoutOption[0]);
|
2020-08-29 21:15:54 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-08 06:15:42 +11:00
|
|
|
|
|
|
|
|
|
public void DrawGenericArgsInput()
|
|
|
|
|
{
|
|
|
|
|
GUILayout.Label($"<b><color=orange>Generic Arguments:</color></b>", new GUILayoutOption[0]);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < this.GenericArgs.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
string types = "";
|
|
|
|
|
if (this.GenericConstraints[i].Length > 0)
|
|
|
|
|
{
|
|
|
|
|
foreach (var constraint in this.GenericConstraints[i])
|
|
|
|
|
{
|
|
|
|
|
if (types != "") types += ", ";
|
|
|
|
|
|
|
|
|
|
string type;
|
|
|
|
|
|
|
|
|
|
if (constraint == null)
|
|
|
|
|
type = "Any";
|
|
|
|
|
else
|
|
|
|
|
type = constraint.ToString();
|
|
|
|
|
|
|
|
|
|
types += $"<color={Syntax.Class_Instance}>{type}</color>";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
types = $"<color={Syntax.Class_Instance}>Any</color>";
|
|
|
|
|
}
|
|
|
|
|
var input = this.GenericArgInput[i];
|
|
|
|
|
|
|
|
|
|
GUIUnstrip.BeginHorizontal(new GUILayoutOption[0]);
|
|
|
|
|
|
|
|
|
|
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
|
|
|
|
|
GUILayout.Label(
|
|
|
|
|
$"<color={Syntax.StructGreen}>{this.GenericArgs[i].Name}</color>",
|
|
|
|
|
new GUILayoutOption[] { GUILayout.Width(15) }
|
|
|
|
|
);
|
|
|
|
|
this.GenericArgInput[i] = GUIUnstrip.TextField(input, new GUILayoutOption[] { GUILayout.Width(150) });
|
|
|
|
|
GUI.skin.label.alignment = TextAnchor.MiddleLeft;
|
|
|
|
|
GUILayout.Label(types, new GUILayoutOption[0]);
|
|
|
|
|
|
|
|
|
|
GUILayout.EndHorizontal();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-29 21:15:54 +10:00
|
|
|
|
}
|
|
|
|
|
}
|