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

Commit f203ae3

Browse files
committed
1.7.5
* Added support for Enums with [Flags] attribute (can set each flag individually) * Added support for easier bitwise operations on ints (or any primitive assignable to int), and viewing the int as binary. This is intended for things like `Camera.cullingMask`, etc. * Fixed an issue with Enums that contain duplicate values, for example `CameraClearFlags` (has duplicate values for 2).
1 parent 2006a9e commit f203ae3

File tree

8 files changed

+322
-30
lines changed

8 files changed

+322
-30
lines changed

src/CachedObjects/CacheObjectBase.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,6 @@ private static CacheObjectBase GetCacheObjectImpl(object obj, MemberInfo memberI
121121
return null;
122122
}
123123

124-
// This is pretty ugly, could probably make a cleaner implementation.
125-
// However, the only cleaner ways I can think of are slower and probably not worth it.
126-
127-
// Note: the order is somewhat important.
128-
129124
if (mi != null)
130125
{
131126
holder = new CacheMethod();
@@ -140,7 +135,14 @@ private static CacheObjectBase GetCacheObjectImpl(object obj, MemberInfo memberI
140135
}
141136
else if (valueType.IsEnum)
142137
{
143-
holder = new CacheEnum();
138+
if (valueType.GetCustomAttributes(typeof(FlagsAttribute), false) is object[] attributes && attributes.Length > 0)
139+
{
140+
holder = new CacheEnumFlags();
141+
}
142+
else
143+
{
144+
holder = new CacheEnum();
145+
}
144146
}
145147
else if (valueType == typeof(Vector2) || valueType == typeof(Vector3) || valueType == typeof(Vector4))
146148
{

src/CachedObjects/Struct/CacheEnum.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,18 @@ public override void Init()
2323

2424
if (ValueType != null)
2525
{
26-
EnumNames = Enum.GetNames(ValueType);
26+
// using GetValues not GetNames, to catch instances of weird enums (eg CameraClearFlags)
27+
var values = Enum.GetValues(ValueType);
28+
29+
var list = new List<string>();
30+
foreach (var value in values)
31+
{
32+
var v = value.ToString();
33+
if (list.Contains(v)) continue;
34+
list.Add(v);
35+
}
36+
37+
EnumNames = list.ToArray();
2738
}
2839
else
2940
{
@@ -37,28 +48,28 @@ public override void DrawValue(Rect window, float width)
3748
{
3849
if (GUILayout.Button("<", new GUILayoutOption[] { GUILayout.Width(25) }))
3950
{
40-
SetEnum(ref Value, -1);
51+
SetEnum(-1);
4152
SetValue();
4253
}
4354
if (GUILayout.Button(">", new GUILayoutOption[] { GUILayout.Width(25) }))
4455
{
45-
SetEnum(ref Value, 1);
56+
SetEnum(1);
4657
SetValue();
4758
}
4859
}
4960

5061
GUILayout.Label(Value.ToString() + "<color=#2df7b2><i> (" + ValueType + ")</i></color>", null);
5162
}
5263

53-
public void SetEnum(ref object value, int change)
64+
public void SetEnum(int change)
5465
{
5566
var names = EnumNames.ToList();
5667

57-
int newindex = names.IndexOf(value.ToString()) + change;
68+
int newindex = names.IndexOf(Value.ToString()) + change;
5869

59-
if ((change < 0 && newindex >= 0) || (change > 0 && newindex < names.Count))
70+
if (newindex >= 0 && newindex < names.Count)
6071
{
61-
value = Enum.Parse(ValueType, names[newindex]);
72+
Value = Enum.Parse(ValueType, EnumNames[newindex]);
6273
}
6374
}
6475
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using MelonLoader;
7+
using UnityEngine;
8+
9+
namespace Explorer
10+
{
11+
public class CacheEnumFlags : CacheObjectBase, IExpandHeight
12+
{
13+
public string[] EnumNames = new string[0];
14+
public bool[] m_enabledFlags = new bool[0];
15+
16+
public bool IsExpanded { get; set; }
17+
public float WhiteSpace { get; set; } = 215f;
18+
19+
public override void Init()
20+
{
21+
base.Init();
22+
23+
if (ValueType == null && Value != null)
24+
{
25+
ValueType = Value.GetType();
26+
}
27+
28+
if (ValueType != null)
29+
{
30+
EnumNames = Enum.GetNames(ValueType);
31+
32+
m_enabledFlags = new bool[EnumNames.Length];
33+
34+
UpdateValue();
35+
}
36+
else
37+
{
38+
ReflectionException = "Unknown, could not get Enum names.";
39+
}
40+
}
41+
42+
public void SetFlagsFromInput()
43+
{
44+
string val = "";
45+
for (int i = 0; i < EnumNames.Length; i++)
46+
{
47+
if (m_enabledFlags[i])
48+
{
49+
if (val != "") val += ", ";
50+
val += EnumNames[i];
51+
}
52+
}
53+
Value = Enum.Parse(ValueType, val);
54+
SetValue();
55+
}
56+
57+
public override void UpdateValue()
58+
{
59+
base.UpdateValue();
60+
61+
try
62+
{
63+
var enabledNames = Value.ToString().Split(',').Select(it => it.Trim());
64+
65+
for (int i = 0; i < EnumNames.Length; i++)
66+
{
67+
m_enabledFlags[i] = enabledNames.Contains(EnumNames[i]);
68+
}
69+
}
70+
catch (Exception e)
71+
{
72+
MelonLogger.Log(e.ToString());
73+
}
74+
}
75+
76+
77+
public override void DrawValue(Rect window, float width)
78+
{
79+
if (CanWrite)
80+
{
81+
if (!IsExpanded)
82+
{
83+
if (GUILayout.Button("v", new GUILayoutOption[] { GUILayout.Width(25) }))
84+
{
85+
IsExpanded = true;
86+
}
87+
}
88+
else
89+
{
90+
if (GUILayout.Button("^", new GUILayoutOption[] { GUILayout.Width(25) }))
91+
{
92+
IsExpanded = false;
93+
}
94+
}
95+
}
96+
97+
GUILayout.Label(Value.ToString() + "<color=#2df7b2><i> (" + ValueType + ")</i></color>", null);
98+
99+
if (IsExpanded)
100+
{
101+
GUILayout.EndHorizontal();
102+
103+
var whitespace = CalcWhitespace(window);
104+
105+
for (int i = 0; i < EnumNames.Length; i++)
106+
{
107+
GUILayout.BeginHorizontal(null);
108+
GUIUnstrip.Space(whitespace);
109+
110+
m_enabledFlags[i] = GUILayout.Toggle(m_enabledFlags[i], EnumNames[i], null);
111+
112+
GUILayout.EndHorizontal();
113+
}
114+
115+
GUILayout.BeginHorizontal(null);
116+
GUIUnstrip.Space(whitespace);
117+
if (GUILayout.Button("<color=lime>Apply</color>", new GUILayoutOption[] { GUILayout.Width(155) }))
118+
{
119+
SetFlagsFromInput();
120+
}
121+
GUILayout.EndHorizontal();
122+
123+
GUILayout.BeginHorizontal(null);
124+
}
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)