Model: UnmangledBaseName / CSharpSafeName refactoring (#70)

This commit is contained in:
Katy Coe
2020-09-13 14:51:20 +02:00
parent 8e00e47ccd
commit e511b99dec
5 changed files with 14 additions and 14 deletions

View File

@@ -385,11 +385,11 @@ namespace Il2CppInspector.Outputs
if (field.GetCustomAttributes(FBAttribute).Any()) { if (field.GetCustomAttributes(FBAttribute).Any()) {
if (!SuppressMetadata) if (!SuppressMetadata)
sb.Append($"/* {field.GetCustomAttributes(FBAttribute)[0].VirtualAddress.ToAddressString()} */ "); sb.Append($"/* {field.GetCustomAttributes(FBAttribute)[0].VirtualAddress.ToAddressString()} */ ");
sb.Append($"{field.FieldType.DeclaredFields[0].FieldType.GetScopedCSharpName(scope)} {field.CSharpSafeName}[0]"); // FixedElementField sb.Append($"{field.FieldType.DeclaredFields[0].FieldType.GetScopedCSharpName(scope)} {field.CSharpName}[0]"); // FixedElementField
} }
// Regular fields // Regular fields
else else
sb.Append($"{field.FieldType.GetScopedCSharpName(scope)} {field.CSharpSafeName}"); sb.Append($"{field.FieldType.GetScopedCSharpName(scope)} {field.CSharpName}");
if (field.HasDefaultValue) if (field.HasDefaultValue)
sb.Append($" = {field.GetDefaultValueString(scope)}"); sb.Append($" = {field.GetDefaultValueString(scope)}");
sb.Append(";"); sb.Append(";");
@@ -477,7 +477,7 @@ namespace Il2CppInspector.Outputs
.ToString(scope, prefix + "\t", emitPointer: !SuppressMetadata, mustCompile: MustCompile)); .ToString(scope, prefix + "\t", emitPointer: !SuppressMetadata, mustCompile: MustCompile));
string modifiers = evt.AddMethod?.GetModifierString(); string modifiers = evt.AddMethod?.GetModifierString();
sb.Append($"{prefix}\t{modifiers}event {evt.EventHandlerType.GetScopedCSharpName(scope)} {evt.CSharpSafeName}"); sb.Append($"{prefix}\t{modifiers}event {evt.EventHandlerType.GetScopedCSharpName(scope)} {evt.CSharpName}");
if (!MustCompile) { if (!MustCompile) {
sb.Append(" {\n"); sb.Append(" {\n");
@@ -507,14 +507,14 @@ namespace Il2CppInspector.Outputs
// Crete a parameterless constructor for every relevant type when making code that compiles to mitigate CS1729 and CS7036 // Crete a parameterless constructor for every relevant type when making code that compiles to mitigate CS1729 and CS7036
if (MustCompile && !type.IsInterface && !(type.IsAbstract && type.IsSealed) && !type.IsValueType if (MustCompile && !type.IsInterface && !(type.IsAbstract && type.IsSealed) && !type.IsValueType
&& type.DeclaredConstructors.All(c => c.IsStatic || c.DeclaredParameters.Any())) && type.DeclaredConstructors.All(c => c.IsStatic || c.DeclaredParameters.Any()))
sb.Append($"{prefix}\t{(type.IsAbstract? "protected" : "public")} {type.UnmangledBaseName}() {{}} // Dummy constructor\n"); sb.Append($"{prefix}\t{(type.IsAbstract? "protected" : "public")} {type.CSharpBaseName}() {{}} // Dummy constructor\n");
foreach (var method in type.DeclaredConstructors) { foreach (var method in type.DeclaredConstructors) {
// Attributes // Attributes
sb.Append(method.CustomAttributes.OrderBy(a => a.AttributeType.Name) sb.Append(method.CustomAttributes.OrderBy(a => a.AttributeType.Name)
.ToString(scope, prefix + "\t", emitPointer: !SuppressMetadata, mustCompile: MustCompile)); .ToString(scope, prefix + "\t", emitPointer: !SuppressMetadata, mustCompile: MustCompile));
sb.Append($"{prefix}\t{method.GetModifierString()}{method.DeclaringType.UnmangledBaseName}{method.GetTypeParametersString(scope)}"); sb.Append($"{prefix}\t{method.GetModifierString()}{method.DeclaringType.CSharpBaseName}{method.GetTypeParametersString(scope)}");
sb.Append($"({method.GetParametersString(scope, !SuppressMetadata)})"); sb.Append($"({method.GetParametersString(scope, !SuppressMetadata)})");
if (MustCompile) { if (MustCompile) {
@@ -640,7 +640,7 @@ namespace Il2CppInspector.Outputs
// Finalizers become destructors // Finalizers become destructors
if (method.Name == "Finalize" && method.IsVirtual && method.ReturnType.FullName == "System.Void" && method.IsFamily) if (method.Name == "Finalize" && method.IsVirtual && method.ReturnType.FullName == "System.Void" && method.IsFamily)
writer.Append("~" + method.DeclaringType.UnmangledBaseName); writer.Append("~" + method.DeclaringType.CSharpBaseName);
// Regular method or operator overload // Regular method or operator overload
else if (method.Name != "op_Implicit" && method.Name != "op_Explicit") else if (method.Name != "op_Implicit" && method.Name != "op_Explicit")

View File

@@ -29,7 +29,7 @@ namespace Il2CppInspector.Reflection {
public virtual string Name { get; protected set; } public virtual string Name { get; protected set; }
// Name of the member with @ prepended if the name is a C# reserved keyword // Name of the member with @ prepended if the name is a C# reserved keyword
public string CSharpSafeName => Constants.Keywords.Contains(Name) ? "@" + Name : Name; public virtual string CSharpName => Constants.Keywords.Contains(Name) ? "@" + Name : Name;
// For top-level members in an assembly (ie. non-nested types) // For top-level members in an assembly (ie. non-nested types)
protected MemberInfo(Assembly asm) => Assembly = asm; protected MemberInfo(Assembly asm) => Assembly = asm;

View File

@@ -92,7 +92,7 @@ namespace Il2CppInspector.Reflection
return image.ReadMappedBytes(VirtualAddress.Value.Start, (int) (VirtualAddress.Value.End - VirtualAddress.Value.Start)); return image.ReadMappedBytes(VirtualAddress.Value.Start, (int) (VirtualAddress.Value.End - VirtualAddress.Value.Start));
} }
public string CSharpName => public override string CSharpName =>
// Operator overload or user-defined conversion operator // Operator overload or user-defined conversion operator
OperatorMethodNames.ContainsKey(Name)? "operator " + OperatorMethodNames[Name] OperatorMethodNames.ContainsKey(Name)? "operator " + OperatorMethodNames[Name]

View File

@@ -31,14 +31,14 @@ namespace Il2CppInspector.Reflection {
public override string Name { get; protected set; } public override string Name { get; protected set; }
public string CSharpName { public override string CSharpName {
get { get {
// Explicit interface implementation // Explicit interface implementation
if (DeclaringType.ImplementedInterfaces if (DeclaringType.ImplementedInterfaces
.FirstOrDefault(i => CSharpSafeName.IndexOf("." + i.CSharpName, StringComparison.Ordinal) != -1) is TypeInfo @interface) .FirstOrDefault(i => Name.IndexOf("." + i.CSharpName, StringComparison.Ordinal) != -1) is TypeInfo @interface)
return CSharpSafeName.Substring(CSharpSafeName.IndexOf("." + @interface.CSharpName, StringComparison.Ordinal) + 1); return Name.Substring(Name.IndexOf("." + @interface.CSharpName, StringComparison.Ordinal) + 1);
// Regular method // Regular property
return Name; return Name;
} }
} }

View File

@@ -298,10 +298,10 @@ namespace Il2CppInspector.Reflection
} }
// Get rid of generic backticks // Get rid of generic backticks
public string UnmangledBaseName => unmangleName(base.Name); public string CSharpBaseName => unmangleName(base.Name);
// C# colloquial name of the type (if available) // C# colloquial name of the type (if available)
public string CSharpName { public override string CSharpName {
get { get {
if (HasElementType) { if (HasElementType) {
var n = ElementType.CSharpName; var n = ElementType.CSharpName;