2020-08-22 00:16:05 +10:00
|
|
|
|
using System;
|
2020-08-29 21:15:54 +10:00
|
|
|
|
using System.Collections.Generic;
|
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
|
|
|
|
|
{
|
2020-08-29 21:15:54 +10:00
|
|
|
|
public class CachePrimitive : CacheObjectBase
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-09-05 23:10:50 +10:00
|
|
|
|
public enum Types
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
|
|
|
|
Bool,
|
|
|
|
|
Double,
|
|
|
|
|
Float,
|
|
|
|
|
Int,
|
2020-08-29 21:15:54 +10:00
|
|
|
|
String,
|
|
|
|
|
Char
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 01:42:19 +10:00
|
|
|
|
private string m_valueToString;
|
2020-08-22 00:16:05 +10:00
|
|
|
|
|
2020-09-05 23:10:50 +10:00
|
|
|
|
public Types PrimitiveType;
|
2020-08-24 01:42:19 +10:00
|
|
|
|
|
2020-09-05 23:10:50 +10:00
|
|
|
|
public MethodInfo ParseMethod => m_parseMethod ?? (m_parseMethod = Value.GetType().GetMethod("Parse", new Type[] { typeof(string) }));
|
2020-08-24 01:42:19 +10:00
|
|
|
|
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-29 21:15:54 +10:00
|
|
|
|
// this must mean it is a string. No other primitive type should be nullable.
|
2020-09-05 23:10:50 +10:00
|
|
|
|
PrimitiveType = Types.String;
|
2020-08-24 01:42:19 +10:00
|
|
|
|
return;
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
2020-08-24 01:42:19 +10:00
|
|
|
|
|
|
|
|
|
m_valueToString = Value.ToString();
|
|
|
|
|
|
2020-09-05 23:10:50 +10:00
|
|
|
|
var type = Value.GetType();
|
2020-08-24 01:42:19 +10:00
|
|
|
|
if (type == typeof(bool))
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-09-05 23:10:50 +10:00
|
|
|
|
PrimitiveType = Types.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-09-05 23:10:50 +10:00
|
|
|
|
PrimitiveType = Types.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-09-05 23:10:50 +10:00
|
|
|
|
PrimitiveType = Types.Float;
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
2020-09-05 23:10:50 +10:00
|
|
|
|
else if (type == typeof(char))
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-09-05 23:10:50 +10:00
|
|
|
|
PrimitiveType = Types.Char;
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
2020-09-05 23:10:50 +10:00
|
|
|
|
else if (typeof(int).IsAssignableFrom(type))
|
2020-08-29 21:15:54 +10:00
|
|
|
|
{
|
2020-09-05 23:10:50 +10:00
|
|
|
|
PrimitiveType = Types.Int;
|
2020-08-29 21:15:54 +10:00
|
|
|
|
}
|
2020-08-24 01:42:19 +10:00
|
|
|
|
else
|
|
|
|
|
{
|
2020-09-05 23:10:50 +10:00
|
|
|
|
PrimitiveType = Types.String;
|
2020-08-24 01:42:19 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-09-05 23:10:50 +10:00
|
|
|
|
if (PrimitiveType == Types.Bool)
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-08-24 01:42:19 +10:00
|
|
|
|
var b = (bool)Value;
|
2020-09-05 23:10:50 +10:00
|
|
|
|
var label = $"<color={(b ? "lime" : "red")}>{b}</color>";
|
2020-08-22 00:16:05 +10:00
|
|
|
|
|
2020-08-29 21:15:54 +10:00
|
|
|
|
if (CanWrite)
|
|
|
|
|
{
|
|
|
|
|
b = GUILayout.Toggle(b, label, null);
|
|
|
|
|
if (b != (bool)Value)
|
|
|
|
|
{
|
2020-08-31 23:28:44 +10:00
|
|
|
|
SetValueFromInput(b.ToString());
|
2020-08-29 21:15:54 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-08-29 21:15:54 +10:00
|
|
|
|
GUILayout.Label(label, null);
|
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) });
|
|
|
|
|
|
2020-08-28 00:45:34 +10:00
|
|
|
|
int dynSize = 25 + (m_valueToString.Length * 15);
|
|
|
|
|
var maxwidth = window.width - 300f;
|
|
|
|
|
if (CanWrite) maxwidth -= 60;
|
|
|
|
|
|
|
|
|
|
if (dynSize > maxwidth)
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-08-28 00:45:34 +10:00
|
|
|
|
m_valueToString = GUILayout.TextArea(m_valueToString, new GUILayoutOption[] { GUILayout.MaxWidth(maxwidth) });
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-08-28 00:45:34 +10:00
|
|
|
|
m_valueToString = GUILayout.TextField(m_valueToString, new GUILayoutOption[] { GUILayout.MaxWidth(dynSize) });
|
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-31 23:28:44 +10:00
|
|
|
|
SetValueFromInput(m_valueToString);
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-28 00:45:34 +10:00
|
|
|
|
|
|
|
|
|
GUILayout.Space(5);
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-05 23:10:50 +10:00
|
|
|
|
public void SetValueFromInput(string valueString)
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-09-01 18:03:44 +10:00
|
|
|
|
if (MemInfo == null)
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
|
|
|
|
MelonLogger.Log("Trying to SetValue but the MemberInfo is null!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-05 23:10:50 +10:00
|
|
|
|
if (PrimitiveType == Types.String)
|
2020-08-22 00:16:05 +10:00
|
|
|
|
{
|
2020-09-05 23:10:50 +10:00
|
|
|
|
Value = valueString;
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
2020-08-24 01:42:19 +10:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-09-05 23:10:50 +10:00
|
|
|
|
Value = ParseMethod.Invoke(null, new object[] { valueString });
|
2020-08-24 01:42:19 +10:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
MelonLogger.Log("Exception parsing value: " + e.GetType() + ", " + e.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetValue();
|
2020-08-22 00:16:05 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|