Skip to content

Commit 504d664

Browse files
committed
Added enum command to list values of a specified enum type.
1 parent 32f0bfe commit 504d664

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

Runtime/DevConsoleMono.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ internal sealed class DevConsoleMono : MonoBehaviour
5050
private const float MinConsoleHeight = 200;
5151
private const float MaxConsoleHeight = 900;
5252
private const int CommandHistoryLength = 10;
53+
private const int MaxCachedEnumTypes = 6;
5354
private const InputKey DefaultToggleKey =
5455
#if USE_NEW_INPUT_SYSTEM
5556
InputKey.Backquote;
@@ -141,6 +142,7 @@ internal sealed class DevConsoleMono : MonoBehaviour
141142
private string[] _commandSuggestions = null;
142143
private int _commandSuggestionIndex = 0;
143144
private bool _ignoreInputChange = false;
145+
private readonly List<Type> _cacheEnumTypes = new List<Type>(MaxCachedEnumTypes);
144146

145147
#endregion
146148

@@ -212,6 +214,7 @@ internal void DisableConsole()
212214
_dynamicTransform.anchoredPosition = _initPosition;
213215
_dynamicTransform.sizeDelta = _initSize;
214216
_commandHistory.Clear();
217+
_cacheEnumTypes.Clear();
215218
ClearConsole();
216219
Application.logMessageReceived -= OnLogMessageReceived;
217220
//Application.logMessageReceivedThreaded -= OnLogMessageReceived;
@@ -832,6 +835,69 @@ private void InitBuiltInCommands()
832835
}
833836
));
834837

838+
AddCommand(Command.Create<string>(
839+
"enum",
840+
"",
841+
"Display information about a specified enum",
842+
Parameter.Create("enumName", "Name of the enum to get information about (case-sensitive)"),
843+
s =>
844+
{
845+
// Check if the enum type was cached
846+
Type enumType = _cacheEnumTypes.FirstOrDefault(t => t.Name.Equals(s));
847+
848+
if (enumType == null)
849+
{
850+
// Search all loaded assemblies for the enum
851+
Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
852+
853+
foreach (Assembly assembly in loadedAssemblies)
854+
{
855+
enumType = assembly.GetTypes()
856+
.SelectMany(t => t.GetMembers())
857+
.Union(assembly.GetTypes())
858+
.FirstOrDefault(t => t.ReflectedType != null && t.ReflectedType.IsEnum && t.ReflectedType.Name.Equals(s))
859+
?.ReflectedType;
860+
861+
if (enumType != null)
862+
{
863+
// Cache the type
864+
_cacheEnumTypes.Add(enumType);
865+
if (_commandHistory.Count > MaxCachedEnumTypes)
866+
{
867+
_commandHistory.RemoveAt(0);
868+
}
869+
break;
870+
}
871+
}
872+
}
873+
874+
if (enumType == null)
875+
{
876+
LogError($"Could not find enum type with the specified name: \"{s}\"");
877+
return;
878+
}
879+
880+
LogSeperator($"{enumType.Name} ({enumType.GetEnumUnderlyingType().Name}){(enumType.GetCustomAttribute(typeof(FlagsAttribute)) == null ? "" : " [Flags]")}");
881+
882+
FieldInfo[] values = enumType.GetFields();
883+
string formattedValues = string.Empty;
884+
bool first = true;
885+
for (int i = 0; i < values.Length; i++)
886+
{
887+
if (values[i].Name.Equals("value__"))
888+
{
889+
continue;
890+
}
891+
892+
formattedValues += $"{(first ? "" : "\n")}{values[i].Name} = {values[i].GetRawConstantValue()}";
893+
first = false;
894+
}
895+
Log(formattedValues);
896+
897+
LogSeperator();
898+
}
899+
));
900+
835901
AddCommand(Command.Create(
836902
"commands",
837903
"",

Runtime/Parameter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ internal Parameter SetType(Type type)
9090
if (type.GetEnumNames().Length > MaxEnumNames)
9191
{
9292
// Recommend using the help_enum command
93-
enumHelpText = $"use <b>help_enum {type.Name}</b> to see options";
93+
enumHelpText = $"use <b>enum {type.Name}</b> to see options";
9494
}
9595
else
9696
{

0 commit comments

Comments
 (0)