2020-08-29 21:15:54 +10:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using MelonLoader;
|
2020-09-18 18:38:11 +10:00
|
|
|
|
using UnityEngine;
|
2020-08-29 21:15:54 +10:00
|
|
|
|
|
|
|
|
|
namespace Explorer
|
|
|
|
|
{
|
|
|
|
|
public class CacheMethod : CacheObjectBase
|
|
|
|
|
{
|
|
|
|
|
private CacheObjectBase m_cachedReturnValue;
|
|
|
|
|
|
2020-09-18 18:03:17 +10:00
|
|
|
|
public override bool HasParameters => base.HasParameters || GenericArgs.Length > 0;
|
|
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
|
|
public override void Init()
|
|
|
|
|
{
|
|
|
|
|
var mi = (MemInfo as MethodInfo);
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
|
|
ValueType = mi.ReturnType;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 21:15:54 +10:00
|
|
|
|
public override void UpdateValue()
|
|
|
|
|
{
|
2020-08-31 18:23:19 +10:00
|
|
|
|
//base.UpdateValue();
|
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-18 23:10:46 +10:00
|
|
|
|
MelonLogger.LogWarning($"Exception evaluating: {e.GetType()}, {e.Message}");
|
|
|
|
|
ReflectionException = ReflectionHelpers.ExceptionToString(e);
|
2020-09-07 17:05:37 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ret != null)
|
|
|
|
|
{
|
|
|
|
|
m_cachedReturnValue = GetCacheObject(ret);
|
|
|
|
|
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-09-19 01:27:33 +10:00
|
|
|
|
if (!constraint.IsAssignableFrom(t))
|
|
|
|
|
{
|
|
|
|
|
MelonLogger.LogWarning($"Generic argument #{i}, '{input}' is not assignable from the constraint '{constraint}'!");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
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
|
|
|
|
|
{
|
|
|
|
|
MelonLogger.LogWarning($"Generic argument #{i}, could not get any type by the name of '{input}'!" +
|
|
|
|
|
$" 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-08-30 07:01:13 +10:00
|
|
|
|
public override void DrawValue(Rect window, float width)
|
2020-08-29 21:15:54 +10:00
|
|
|
|
{
|
2020-09-19 01:44:38 +10:00
|
|
|
|
string typeLabel = $"<color={UIStyles.Syntax.Class_Instance}>{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-09-07 17:05:37 +10:00
|
|
|
|
m_cachedReturnValue.DrawValue(window, width);
|
2020-08-29 21:15:54 +10:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-09-16 20:03:57 +10:00
|
|
|
|
GUILayout.Label($"null ({typeLabel})", null);
|
2020-08-29 21:15:54 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-09-16 20:03:57 +10:00
|
|
|
|
GUILayout.Label($"<color=grey><i>Not yet evaluated</i></color> ({typeLabel})", null);
|
2020-08-29 21:15:54 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|