2020-08-22 00:16:05 +10:00
|
|
|
|
using System;
|
2020-08-24 01:42:19 +10:00
|
|
|
|
using System.Reflection;
|
2020-08-22 00:16:05 +10:00
|
|
|
|
using MelonLoader;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace Explorer
|
|
|
|
|
{
|
|
|
|
|
public class CachePrimitive : CacheObject
|
|
|
|
|
{
|
2020-08-24 01:42:19 +10:00
|
|
|
|
public enum PrimitiveTypes
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
|
|
|
|
Bool,
|
|
|
|
|
Double,
|
|
|
|
|
Float,
|
|
|
|
|
Int,
|
|
|
|
|
String
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 01:42:19 +10:00
|
|
|
|
private string m_valueToString;
|
2020-08-22 00:16:05 +10:00
|
|
|
|
|
2020-08-24 01:42:19 +10:00
|
|
|
|
public PrimitiveTypes PrimitiveType;
|
|
|
|
|
|
|
|
|
|
public MethodInfo ParseMethod
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-08-24 01:42:19 +10:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (m_parseMethod == null)
|
|
|
|
|
{
|
|
|
|
|
Type t = null;
|
|
|
|
|
switch (PrimitiveType)
|
|
|
|
|
{
|
|
|
|
|
case PrimitiveTypes.Bool:
|
|
|
|
|
t = typeof(bool); break;
|
|
|
|
|
case PrimitiveTypes.Double:
|
|
|
|
|
t = typeof(double); break;
|
|
|
|
|
case PrimitiveTypes.Float:
|
|
|
|
|
t = typeof(float); break;
|
|
|
|
|
case PrimitiveTypes.Int:
|
|
|
|
|
t = typeof(int); break;
|
|
|
|
|
case PrimitiveTypes.String:
|
|
|
|
|
t = typeof(string); break;
|
|
|
|
|
}
|
|
|
|
|
m_parseMethod = t.GetMethod("Parse", new Type[] { typeof(string) });
|
|
|
|
|
}
|
|
|
|
|
return m_parseMethod;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private MethodInfo m_parseMethod;
|
2020-08-22 17:17:11 +10:00
|
|
|
|
|
2020-08-24 01:42:19 +10:00
|
|
|
|
public override void Init()
|
|
|
|
|
{
|
|
|
|
|
if (Value == null)
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-08-24 01:42:19 +10:00
|
|
|
|
// this must mean it is a string? no other primitive type should be nullable
|
|
|
|
|
PrimitiveType = PrimitiveTypes.String;
|
|
|
|
|
return;
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
2020-08-24 01:42:19 +10:00
|
|
|
|
|
|
|
|
|
m_valueToString = Value.ToString();
|
|
|
|
|
var type = Value.GetType();
|
|
|
|
|
|
|
|
|
|
if (type == typeof(bool))
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-08-24 01:42:19 +10:00
|
|
|
|
PrimitiveType = PrimitiveTypes.Bool;
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
2020-08-24 01:42:19 +10:00
|
|
|
|
else if (type == typeof(double))
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-08-24 01:42:19 +10:00
|
|
|
|
PrimitiveType = PrimitiveTypes.Double;
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
2020-08-24 01:42:19 +10:00
|
|
|
|
else if (type == typeof(float))
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-08-24 01:42:19 +10:00
|
|
|
|
PrimitiveType = PrimitiveTypes.Float;
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
2020-08-24 01:42:19 +10:00
|
|
|
|
else if (type == typeof(int) || type == typeof(long) || type == typeof(uint) || type == typeof(ulong) || type == typeof(IntPtr))
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-08-24 01:42:19 +10:00
|
|
|
|
PrimitiveType = PrimitiveTypes.Int;
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
2020-08-24 01:42:19 +10:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (type != typeof(string))
|
|
|
|
|
{
|
|
|
|
|
MelonLogger.Log("Unsupported primitive: " + type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PrimitiveType = PrimitiveTypes.String;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void UpdateValue()
|
|
|
|
|
{
|
|
|
|
|
base.UpdateValue();
|
|
|
|
|
|
|
|
|
|
m_valueToString = Value?.ToString();
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void DrawValue(Rect window, float width)
|
|
|
|
|
{
|
2020-08-24 01:42:19 +10:00
|
|
|
|
if (PrimitiveType == PrimitiveTypes.Bool)
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-08-24 01:42:19 +10:00
|
|
|
|
var b = (bool)Value;
|
2020-08-22 00:16:05 +10:00
|
|
|
|
var color = "<color=" + (b ? "lime>" : "red>");
|
2020-08-24 01:42:19 +10:00
|
|
|
|
b = GUILayout.Toggle(b, color + b.ToString() + "</color>", null);
|
2020-08-22 00:16:05 +10:00
|
|
|
|
|
|
|
|
|
if (b != (bool)Value)
|
|
|
|
|
{
|
2020-08-24 01:42:19 +10:00
|
|
|
|
SetValue(m_valueToString);
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-08-24 01:42:19 +10:00
|
|
|
|
GUILayout.Label("<color=yellow><i>" + PrimitiveType + "</i></color>", new GUILayoutOption[] { GUILayout.Width(50) });
|
|
|
|
|
|
|
|
|
|
var _width = window.width - 200;
|
|
|
|
|
if (m_valueToString.Length > 37)
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-08-24 01:42:19 +10:00
|
|
|
|
m_valueToString = GUILayout.TextArea(m_valueToString, new GUILayoutOption[] { GUILayout.MaxWidth(_width) });
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-08-24 01:42:19 +10:00
|
|
|
|
m_valueToString = GUILayout.TextField(m_valueToString, new GUILayoutOption[] { GUILayout.MaxWidth(_width) });
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
2020-08-24 01:42:19 +10:00
|
|
|
|
|
|
|
|
|
if (CanWrite)
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
|
|
|
|
if (GUILayout.Button("<color=#00FF00>Apply</color>", new GUILayoutOption[] { GUILayout.Width(60) }))
|
|
|
|
|
{
|
2020-08-24 01:42:19 +10:00
|
|
|
|
SetValue(m_valueToString);
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 01:42:19 +10:00
|
|
|
|
public void SetValue(string value)
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
|
|
|
|
if (MemberInfo == null)
|
|
|
|
|
{
|
|
|
|
|
MelonLogger.Log("Trying to SetValue but the MemberInfo is null!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 01:42:19 +10:00
|
|
|
|
if (PrimitiveType == PrimitiveTypes.String)
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-08-24 01:42:19 +10:00
|
|
|
|
Value = value;
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
2020-08-24 01:42:19 +10:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var val = ParseMethod.Invoke(null, new object[] { value });
|
|
|
|
|
Value = val;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
MelonLogger.Log("Exception parsing value: " + e.GetType() + ", " + e.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetValue();
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|