Output: Annotate unmanaged type usages with 'unsafe' keyword

This commit is contained in:
Katy Coe
2019-11-10 18:53:07 +01:00
parent 54d03b9f0f
commit 5bf6e2c7c3
5 changed files with 13 additions and 0 deletions

View File

@@ -84,6 +84,8 @@ namespace Il2CppInspector
var del = type.GetMethod("Invoke");
// IL2CPP doesn't seem to retain return type attributes
//writer.Write(del.ReturnType.CustomAttributes.ToString(prefix, "return: "));
if (del.RequiresUnsafeContext)
writer.Write("unsafe ");
writer.Write($"delegate {del.ReturnType.CSharpName} {type.CSharpTypeDeclarationName}(");
writer.Write(del.GetParametersString());
writer.Write($"); // TypeDefIndex: {type.Index}; {del.VirtualAddress.ToAddressString()}\n");

View File

@@ -128,6 +128,8 @@ namespace Il2CppInspector.Reflection {
public string GetModifierString() {
var modifiers = new StringBuilder(GetAccessModifierString());
if (FieldType.RequiresUnsafeContext || GetCustomAttributes("System.Runtime.CompilerServices.FixedBufferAttribute").Any())
modifiers.Append("unsafe ");
if (IsLiteral)
modifiers.Append("const ");
// All const fields are also static by implication

View File

@@ -47,6 +47,8 @@ namespace Il2CppInspector.Reflection
public bool IsStatic => (Attributes & MethodAttributes.Static) == MethodAttributes.Static;
public bool IsVirtual => (Attributes & MethodAttributes.Virtual) == MethodAttributes.Virtual;
public virtual bool RequiresUnsafeContext => DeclaredParameters.Any(p => p.ParameterType.RequiresUnsafeContext);
// TODO: GetMethodBody()
public string CSharpName =>
@@ -132,6 +134,8 @@ namespace Il2CppInspector.Reflection
var modifiers = new StringBuilder(GetAccessModifierString());
if (RequiresUnsafeContext)
modifiers.Append("unsafe ");
if (IsAbstract)
modifiers.Append("abstract ");
// Methods that implement interfaces are IsVirtual && IsFinal with MethodAttributes.NewSlot (don't show 'virtual sealed' for these)

View File

@@ -20,6 +20,8 @@ namespace Il2CppInspector.Reflection
private readonly int returnTypeUsage;
public TypeInfo ReturnType => Assembly.Model.GetTypeFromUsage(returnTypeUsage, MemberTypes.TypeInfo);
public override bool RequiresUnsafeContext => base.RequiresUnsafeContext || ReturnType.RequiresUnsafeContext;
// IL2CPP doesn't seem to retain return type custom attributes
public MethodInfo(Il2CppInspector pkg, int methodIndex, TypeInfo declaringType) : base(pkg, methodIndex, declaringType) {

View File

@@ -164,6 +164,9 @@ namespace Il2CppInspector.Reflection {
public bool IsSpecialName => (Attributes & TypeAttributes.SpecialName) == TypeAttributes.SpecialName;
public bool IsValueType => BaseType?.FullName == "System.ValueType";
// Helper function for determining if using this type as a field, parameter etc. requires that field or method to be declared as unsafe
public bool RequiresUnsafeContext => IsPointer || (HasElementType && ElementType.RequiresUnsafeContext);
// May get overridden by Il2CppType-based constructor below
public override MemberTypes MemberType { get; } = MemberTypes.TypeInfo;