Skip to content

Commit a18325b

Browse files
committed
enum command parameter now supports namespaces to distinguish between Enums with same names.
1 parent 115d01e commit a18325b

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88
- Added LogException methods.
99
- Changed the size of the input field so that the text is larger.
10+
- Changed enum command parameter to support namespaces to distinguish between enums with same names.
1011
- Fixed strings being displayed incorrectly by cs_evaluate.
1112
- Fixed issue with cached enum types.
1213

Runtime/DevConsoleMono.cs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,37 +1512,66 @@ private void InitBuiltInCommands()
15121512
// Check if the enum type was cached
15131513
Type enumType = _cacheEnumTypes.FirstOrDefault(t => t.Name.Equals(s));
15141514

1515+
#if INPUT_SYSTEM_INSTALLED
1516+
// Special case for Key
1517+
if (s.Equals("Key"))
1518+
{
1519+
enumType = typeof(Key);
1520+
}
1521+
#endif
1522+
15151523
if (enumType == null)
15161524
{
1525+
List<Type> options = new List<Type>();
1526+
15171527
// Search all loaded assemblies for the enum
15181528
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
15191529
{
15201530
enumType = assembly.GetTypes()
15211531
.SelectMany(t => t.GetMembers())
15221532
.Union(assembly.GetTypes())
1523-
.FirstOrDefault(t => t.ReflectedType != null && t.ReflectedType.IsEnum && t.ReflectedType.Name.Equals(s))
1533+
.FirstOrDefault(t => t.ReflectedType != null && t.ReflectedType.IsEnum && (t.ReflectedType.Name.Equals(s) || s.Equals($"{t.ReflectedType.Namespace}.{t.ReflectedType.Name}")))
15241534
?.ReflectedType;
15251535

15261536
if (enumType != null)
15271537
{
1528-
// Cache the type
1529-
_cacheEnumTypes.Add(enumType);
1530-
if (_cacheEnumTypes.Count > MaxCachedEnumTypes)
1538+
if (s.Equals($"{enumType.Namespace}.{enumType.Name}"))
15311539
{
1532-
_cacheEnumTypes.RemoveAt(0);
1540+
options = new List<Type>() { enumType };
1541+
break;
15331542
}
1534-
break;
1543+
1544+
options.Add(enumType);
15351545
}
15361546
}
1537-
}
15381547

1539-
if (enumType == null)
1540-
{
1541-
LogError($"Could not find enum type with the specified name: \"{s}\"");
1542-
return;
1548+
if (options.Count > 1)
1549+
{
1550+
LogError($"Multiple results found: {string.Join(", ", options.Select(x => $"{x.Namespace}.{x.Name}"))}.");
1551+
return;
1552+
}
1553+
1554+
else if (options.Count == 1)
1555+
{
1556+
// Select the first type
1557+
enumType = options.FirstOrDefault();
1558+
1559+
// Cache the type
1560+
_cacheEnumTypes.Add(enumType);
1561+
if (_cacheEnumTypes.Count > MaxCachedEnumTypes)
1562+
{
1563+
_cacheEnumTypes.RemoveAt(0);
1564+
}
1565+
}
1566+
1567+
else
1568+
{
1569+
LogError($"Could not find enum type with the specified name: \"{s}\"");
1570+
return;
1571+
}
15431572
}
15441573

1545-
LogSeperator($"{enumType.Name} ({enumType.GetEnumUnderlyingType().Name}){(enumType.GetCustomAttribute(typeof(FlagsAttribute)) == null ? "" : " [Flags]")}");
1574+
LogSeperator($"{enumType.Namespace}.{enumType.Name} ({enumType.GetEnumUnderlyingType().Name}){(enumType.GetCustomAttribute(typeof(FlagsAttribute)) == null ? "" : " [Flags]")}");
15461575

15471576
FieldInfo[] values = enumType.GetFields();
15481577
string formattedValues = string.Empty;

0 commit comments

Comments
 (0)