Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit db91968

Browse files
committed
Cleanup and improve syntax highlighting
* Static class members are now displayed in Italics and in a darker color, making them easier to distinguish. * Cleaned up some issues related to syntax highlighting and refactored it into a global class. * Methods and properties no longer display their arguments as part of the member name, they are only displayed when "Evaluate" is pressed.
1 parent 5d58993 commit db91968

File tree

8 files changed

+134
-61
lines changed

8 files changed

+134
-61
lines changed

src/CachedObjects/CacheObjectBase.cs

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,11 @@ public void Draw(Rect window, float labelWidth = 215f)
400400
var input = m_argumentInput[i];
401401
var type = m_arguments[i].ParameterType.Name;
402402

403-
var label = "<color=#2df7b2>" + type + "</color> <color=#a6e9e9>" + name + "</color>";
403+
var label = $"<color={UIStyles.Syntax.Class_Instance}>{type}</color> ";
404+
label += $"<color={UIStyles.Syntax.Local}>{name}</color>";
404405
if (m_arguments[i].HasDefaultValue)
405406
{
406-
label = $"<i>[{label} = {m_arguments[i].DefaultValue}]</i>";
407+
label = $"<i>[{label} = {m_arguments[i].DefaultValue ?? "null"}]</i>";
407408
}
408409

409410
GUILayout.BeginHorizontal(null);
@@ -469,7 +470,7 @@ public void Draw(Rect window, float labelWidth = 215f)
469470
}
470471
else if ((HasParameters || this is CacheMethod) && !m_evaluated)
471472
{
472-
GUILayout.Label($"<color=grey><i>Not yet evaluated</i></color> (<color=#2df7b2>{ValueTypeName}</color>)", null);
473+
GUILayout.Label($"<color=grey><i>Not yet evaluated</i></color> (<color={UIStyles.Syntax.Class_Instance}>{ValueTypeName}</color>)", null);
473474
}
474475
else if (Value == null && !(this is CacheMethod))
475476
{
@@ -484,31 +485,62 @@ public void Draw(Rect window, float labelWidth = 215f)
484485
private string GetRichTextName()
485486
{
486487
string memberColor = "";
487-
switch (MemInfo.MemberType)
488-
{
489-
case MemberTypes.Field:
490-
memberColor = "#c266ff"; break;
491-
case MemberTypes.Property:
492-
memberColor = "#72a6a6"; break;
493-
case MemberTypes.Method:
494-
memberColor = "#ff8000"; break;
495-
};
496-
497-
m_richTextName = $"<color=#2df7b2>{MemInfo.DeclaringType.Name}</color>.<color={memberColor}>{MemInfo.Name}</color>";
488+
bool isStatic = false;
498489

499-
if (m_arguments.Length > 0 || this is CacheMethod)
490+
if (MemInfo is FieldInfo fi)
500491
{
501-
m_richTextName += "(";
502-
var _params = "";
503-
foreach (var param in m_arguments)
492+
if (fi.IsStatic)
504493
{
505-
if (_params != "") _params += ", ";
506-
507-
_params += $"<color=#2df7b2>{param.ParameterType.Name}</color> <color=#a6e9e9>{param.Name}</color>";
494+
isStatic = true;
495+
memberColor = UIStyles.Syntax.Field_Static;
508496
}
509-
m_richTextName += _params;
510-
m_richTextName += ")";
497+
else
498+
memberColor = UIStyles.Syntax.Field_Instance;
499+
}
500+
else if (MemInfo is MethodInfo mi)
501+
{
502+
if (mi.IsStatic)
503+
{
504+
isStatic = true;
505+
memberColor = UIStyles.Syntax.Method_Static;
506+
}
507+
else
508+
memberColor = UIStyles.Syntax.Method_Instance;
511509
}
510+
else if (MemInfo is PropertyInfo pi)
511+
{
512+
if (pi.GetAccessors()[0].IsStatic)
513+
{
514+
isStatic = true;
515+
memberColor = UIStyles.Syntax.Prop_Static;
516+
}
517+
else
518+
memberColor = UIStyles.Syntax.Prop_Instance;
519+
}
520+
521+
string classColor = MemInfo.DeclaringType.IsAbstract && MemInfo.DeclaringType.IsSealed
522+
? UIStyles.Syntax.Class_Static
523+
: UIStyles.Syntax.Class_Instance;
524+
525+
m_richTextName = $"<color={classColor}>{MemInfo.DeclaringType.Name}</color>.";
526+
if (isStatic) m_richTextName += "<i>";
527+
m_richTextName += $"<color={memberColor}>{MemInfo.Name}</color>";
528+
if (isStatic) m_richTextName += "</i>";
529+
530+
//if (m_arguments.Length > 0 || this is CacheMethod)
531+
//{
532+
// m_richTextName += "(";
533+
// var args = "";
534+
// foreach (var param in m_arguments)
535+
// {
536+
// if (args != "") args += ", ";
537+
538+
// args += $"<color={classColor}>{param.ParameterType.Name}</color> ";
539+
// args += $"<color={UIStyles.Syntax.Local}>{param.Name}</color>";
540+
// }
541+
// m_richTextName += args;
542+
// m_richTextName += ")";
543+
//}
512544

513545
return m_richTextName;
514546
}

src/CachedObjects/Other/CacheMethod.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public void Evaluate()
7070

7171
public override void DrawValue(Rect window, float width)
7272
{
73+
string typeLabel = $"<color={UIStyles.Syntax.Class_Instance}>{ValueTypeName}</color>";
74+
7375
if (m_evaluated)
7476
{
7577
if (m_cachedReturnValue != null)
@@ -78,12 +80,12 @@ public override void DrawValue(Rect window, float width)
7880
}
7981
else
8082
{
81-
GUILayout.Label($"null (<color=#2df7b2>{ValueTypeName}</color>)", null);
83+
GUILayout.Label($"null ({typeLabel})", null);
8284
}
8385
}
8486
else
8587
{
86-
GUILayout.Label($"<color=grey><i>Not yet evaluated</i></color> (<color=#2df7b2>{ValueTypeName}</color>)", null);
88+
GUILayout.Label($"<color=grey><i>Not yet evaluated</i></color> ({typeLabel})", null);
8789
}
8890
}
8991
}

src/CachedObjects/Other/CacheOther.cs

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,55 +11,72 @@ namespace Explorer
1111
{
1212
public class CacheOther : CacheObjectBase
1313
{
14+
public string ButtonLabel => m_btnLabel ?? GetButtonLabel();
15+
private string m_btnLabel;
16+
17+
public MethodInfo ToStringMethod => m_toStringMethod ?? GetToStringMethod();
1418
private MethodInfo m_toStringMethod;
1519

16-
public MethodInfo ToStringMethod
20+
public override void DrawValue(Rect window, float width)
1721
{
18-
get
22+
GUI.skin.button.alignment = TextAnchor.MiddleLeft;
23+
if (GUILayout.Button(ButtonLabel, new GUILayoutOption[] { GUILayout.Width(width - 15) }))
1924
{
20-
if (m_toStringMethod == null)
21-
{
22-
try
23-
{
24-
m_toStringMethod = ReflectionHelpers.GetActualType(Value).GetMethod("ToString", new Type[0])
25-
?? typeof(object).GetMethod("ToString", new Type[0]);
26-
27-
// test invoke
28-
m_toStringMethod.Invoke(Value, null);
29-
}
30-
catch
31-
{
32-
m_toStringMethod = typeof(object).GetMethod("ToString", new Type[0]);
33-
}
34-
}
35-
return m_toStringMethod;
25+
WindowManager.InspectObject(Value, out bool _);
3626
}
27+
GUI.skin.button.alignment = TextAnchor.MiddleCenter;
3728
}
3829

39-
public override void DrawValue(Rect window, float width)
30+
private MethodInfo GetToStringMethod()
4031
{
41-
string label = (string)ToStringMethod?.Invoke(Value, null) ?? Value.ToString();
42-
43-
if (!label.Contains(ValueTypeName))
32+
try
4433
{
45-
label += $" (<color=#2df7b2>{ValueTypeName}</color>)";
34+
m_toStringMethod = ReflectionHelpers.GetActualType(Value).GetMethod("ToString", new Type[0])
35+
?? typeof(object).GetMethod("ToString", new Type[0]);
36+
37+
// test invoke
38+
m_toStringMethod.Invoke(Value, null);
4639
}
47-
else
40+
catch
4841
{
49-
label = label.Replace(ValueTypeName, $"<color=#2df7b2>{ValueTypeName}</color>");
42+
m_toStringMethod = typeof(object).GetMethod("ToString", new Type[0]);
5043
}
44+
return m_toStringMethod;
45+
}
46+
47+
private string GetButtonLabel()
48+
{
49+
string label = (string)ToStringMethod?.Invoke(Value, null) ?? Value.ToString();
5150

52-
if (Value is UnityEngine.Object unityObj && !label.Contains(unityObj.name))
51+
var classColor = ValueType.IsAbstract && ValueType.IsSealed
52+
? UIStyles.Syntax.Class_Static
53+
: UIStyles.Syntax.Class_Instance;
54+
55+
if (Value is UnityEngine.Object)
5356
{
54-
label = unityObj.name + " | " + label;
57+
int typeStart = label.LastIndexOf("("); // get where the '(Type)' starts
58+
var newLabel = label.Substring(0, typeStart + 1); // get just the name and first '('
59+
newLabel += $"<color={classColor}>"; // add color tag
60+
newLabel += label.Substring(typeStart + 1); // add the TypeName back in
61+
newLabel = newLabel.Substring(0, newLabel.Length - 1); // remove the ending ')'
62+
newLabel += "</color>)"; // close color tag and put the ')' back.
63+
label = newLabel;
5564
}
56-
57-
GUI.skin.button.alignment = TextAnchor.MiddleLeft;
58-
if (GUILayout.Button(label, new GUILayoutOption[] { GUILayout.Width(width - 15) }))
65+
else
5966
{
60-
WindowManager.InspectObject(Value, out bool _);
67+
string classLabel = $"<color={classColor}>{ValueTypeName}</color>";
68+
69+
if (!label.Contains(ValueTypeName))
70+
{
71+
label += $" ({classLabel})";
72+
}
73+
else
74+
{
75+
label = label.Replace(ValueTypeName, $"<color={classColor}>{ValueTypeName}</color>");
76+
}
6177
}
62-
GUI.skin.button.alignment = TextAnchor.MiddleCenter;
78+
79+
return m_btnLabel = label;
6380
}
6481
}
6582
}

src/CppExplorer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Explorer
1313
public class CppExplorer : MelonMod
1414
{
1515
public const string NAME = "CppExplorer";
16-
public const string VERSION = "1.7.31";
16+
public const string VERSION = "1.7.4";
1717
public const string AUTHOR = "Sinai";
1818
public const string GUID = "com.sinai.cppexplorer";
1919

src/Menu/MainMenu/Pages/ConsolePage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public override void Init()
4343

4444
try
4545
{
46-
MethodInput = @"// This is a basic C# REPL console.
46+
MethodInput = @"// This is a basic C# console.
4747
// Some common using directives are added by default, you can add more below.
4848
// If you want to return some output, MelonLogger.Log() it.
4949
@@ -123,7 +123,7 @@ public object Evaluate(string str, bool suppressWarning = false)
123123

124124
public override void DrawWindow()
125125
{
126-
GUILayout.Label("<b><size=15><color=cyan>C# REPL Console</color></size></b>", null);
126+
GUILayout.Label("<b><size=15><color=cyan>C# Console</color></size></b>", null);
127127

128128
GUI.skin.label.alignment = TextAnchor.UpperLeft;
129129

src/Menu/UIStyles.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ namespace Explorer
1111
{
1212
public class UIStyles
1313
{
14+
public class Syntax
15+
{
16+
public const string Field_Static = "#8d8dc6";
17+
public const string Field_Instance = "#c266ff";
18+
19+
public const string Method_Static = "#b55b02";
20+
public const string Method_Instance = "#ff8000";
21+
22+
public const string Prop_Static = "#588075";
23+
public const string Prop_Instance = "#55a38e";
24+
25+
public const string Class_Static = "#3a8d71";
26+
public const string Class_Instance = "#2df7b2";
27+
28+
public const string Local = "#a6e9e9";
29+
}
30+
1431
public static Color LightGreen = new Color(Color.green.r - 0.3f, Color.green.g - 0.3f, Color.green.b - 0.3f);
1532

1633
public static GUISkin WindowSkin

src/Menu/Windows/GameObjectWindow.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ private void ComponentList(Rect m_rect)
363363
GUILayout.EndHorizontal();
364364

365365
GUILayout.BeginHorizontal(null);
366-
m_addComponentInput = GUILayout.TextField(m_addComponentInput, new GUILayoutOption[] { GUILayout.Width(130) });
366+
var width = m_rect.width / 2 - 115f;
367+
m_addComponentInput = GUILayout.TextField(m_addComponentInput, new GUILayoutOption[] { GUILayout.Width(width) });
367368
if (GUILayout.Button("Add Comp", null))
368369
{
369370
if (ReflectionHelpers.GetTypeByName(m_addComponentInput) is Type compType)

src/Tests/TestClass.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public TestClass()
2222
ILHashSetTest.Add("3");
2323
}
2424

25+
public static int StaticProperty => 5;
26+
public static int StaticField = 5;
27+
public int NonStaticField;
28+
2529
// test a non-generic dictionary
2630

2731
public Hashtable TestNonGenericDict()

0 commit comments

Comments
 (0)