Output: Show enum usages as their corresponding member names (CS0266, CS1750)
This commit is contained in:
@@ -63,7 +63,7 @@ namespace Il2CppInspector.Reflection
|
||||
};
|
||||
|
||||
// Output a value in C#-friendly syntax
|
||||
public static string ToCSharpValue(this object value) {
|
||||
public static string ToCSharpValue(this object value, TypeInfo type, Scope usingScope = null) {
|
||||
if (value is bool)
|
||||
return (bool) value ? "true" : "false";
|
||||
if (value is float)
|
||||
@@ -79,12 +79,27 @@ namespace Il2CppInspector.Reflection
|
||||
: str[i].ToString());
|
||||
return $"\"{s}\"";
|
||||
}
|
||||
if (!(value is char))
|
||||
return (value?.ToString() ?? "null");
|
||||
var cValue = (int) (char) value;
|
||||
if (cValue < 32 || cValue > 126)
|
||||
return $"'\\x{cValue:x4}'";
|
||||
return $"'{value}'";
|
||||
if (value is char) {
|
||||
var cValue = (int) (char) value;
|
||||
if (cValue < 32 || cValue > 126)
|
||||
return $"'\\x{cValue:x4}'";
|
||||
return $"'{value}'";
|
||||
}
|
||||
if (type.IsEnum) {
|
||||
var flags = type.GetCustomAttributes("System.FlagsAttribute").Any();
|
||||
var values = type.GetEnumNames().Zip(type.GetEnumValues().OfType<object>(), (k, v) => new {k, v}).ToDictionary(x => x.k, x => x.v);
|
||||
var typePrefix = type.GetScopedCSharpName(usingScope) + ".";
|
||||
|
||||
// We don't know what type the enumeration or value is, so we use Object.Equals() to do content-based equality testing
|
||||
if (!flags)
|
||||
return typePrefix + values.First(v => v.Value.Equals(value)).Key;
|
||||
|
||||
// Logical OR a series of flags together
|
||||
var flagValue = Convert.ToInt64(value);
|
||||
var setFlags = values.Where(x => (Convert.ToInt64(x.Value) & flagValue) == Convert.ToInt64(x.Value)).Select(x => typePrefix + x.Key);
|
||||
return string.Join(" | ", setFlags);
|
||||
}
|
||||
return (value?.ToString() ?? "null");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user