C++: Handle C and C++ versions of enum type definitions correctly (#62)
This commit is contained in:
@@ -407,11 +407,15 @@ namespace Il2CppInspector.Cpp
|
||||
// Return the type as a field
|
||||
public override string ToFieldString(string fieldName) => "enum " + Name + " " + fieldName;
|
||||
|
||||
// Format specifier: 'c' = don't output C++-style enum with base type, use C-compatible code only
|
||||
public override string ToString(string format = "") {
|
||||
var sb = new StringBuilder();
|
||||
|
||||
// Don't output " : {underlyingType.Name}" because it breaks C
|
||||
sb.Append($"enum {Name} {{");
|
||||
if (format.Contains('c'))
|
||||
sb.Append($"enum {Name} {{");
|
||||
else
|
||||
sb.Append($"enum {Name} : {UnderlyingType.Name} {{");
|
||||
|
||||
foreach (var field in Fields.Values.SelectMany(f => f))
|
||||
sb.Append("\n " + string.Join("\n ", field.ToString(format).Split('\n')) + ",");
|
||||
|
||||
@@ -49,6 +49,8 @@ typedef __int64 int64_t;
|
||||
typedef __int{model.Package.BinaryImage.Bits} size_t;
|
||||
typedef size_t intptr_t;
|
||||
typedef size_t uintptr_t;
|
||||
#else
|
||||
#define _CPLUSPLUS_
|
||||
#endif
|
||||
");
|
||||
|
||||
@@ -247,7 +249,16 @@ typedef size_t uintptr_t;
|
||||
private void writeTypesForGroup(string header, string group) {
|
||||
writeSectionHeader(header);
|
||||
foreach (var cppType in model.GetDependencyOrderedCppTypeGroup(group))
|
||||
writeCode(cppType.ToString());
|
||||
if (cppType is CppEnumType) {
|
||||
// Ghidra can't process C++ enum base types
|
||||
writeCode("#if defined(_CPLUSPLUS_)");
|
||||
writeCode(cppType.ToString());
|
||||
writeCode("#else");
|
||||
writeCode(cppType.ToString("c"));
|
||||
writeCode("#endif");
|
||||
} else {
|
||||
writeCode(cppType.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
private void writeCode(string text) {
|
||||
|
||||
Reference in New Issue
Block a user