Output: Annotate unmanaged type usages with 'unsafe' keyword
This commit is contained in:
@@ -84,6 +84,8 @@ namespace Il2CppInspector
|
|||||||
var del = type.GetMethod("Invoke");
|
var del = type.GetMethod("Invoke");
|
||||||
// IL2CPP doesn't seem to retain return type attributes
|
// IL2CPP doesn't seem to retain return type attributes
|
||||||
//writer.Write(del.ReturnType.CustomAttributes.ToString(prefix, "return: "));
|
//writer.Write(del.ReturnType.CustomAttributes.ToString(prefix, "return: "));
|
||||||
|
if (del.RequiresUnsafeContext)
|
||||||
|
writer.Write("unsafe ");
|
||||||
writer.Write($"delegate {del.ReturnType.CSharpName} {type.CSharpTypeDeclarationName}(");
|
writer.Write($"delegate {del.ReturnType.CSharpName} {type.CSharpTypeDeclarationName}(");
|
||||||
writer.Write(del.GetParametersString());
|
writer.Write(del.GetParametersString());
|
||||||
writer.Write($"); // TypeDefIndex: {type.Index}; {del.VirtualAddress.ToAddressString()}\n");
|
writer.Write($"); // TypeDefIndex: {type.Index}; {del.VirtualAddress.ToAddressString()}\n");
|
||||||
|
|||||||
@@ -128,6 +128,8 @@ namespace Il2CppInspector.Reflection {
|
|||||||
public string GetModifierString() {
|
public string GetModifierString() {
|
||||||
var modifiers = new StringBuilder(GetAccessModifierString());
|
var modifiers = new StringBuilder(GetAccessModifierString());
|
||||||
|
|
||||||
|
if (FieldType.RequiresUnsafeContext || GetCustomAttributes("System.Runtime.CompilerServices.FixedBufferAttribute").Any())
|
||||||
|
modifiers.Append("unsafe ");
|
||||||
if (IsLiteral)
|
if (IsLiteral)
|
||||||
modifiers.Append("const ");
|
modifiers.Append("const ");
|
||||||
// All const fields are also static by implication
|
// All const fields are also static by implication
|
||||||
|
|||||||
@@ -47,6 +47,8 @@ namespace Il2CppInspector.Reflection
|
|||||||
public bool IsStatic => (Attributes & MethodAttributes.Static) == MethodAttributes.Static;
|
public bool IsStatic => (Attributes & MethodAttributes.Static) == MethodAttributes.Static;
|
||||||
public bool IsVirtual => (Attributes & MethodAttributes.Virtual) == MethodAttributes.Virtual;
|
public bool IsVirtual => (Attributes & MethodAttributes.Virtual) == MethodAttributes.Virtual;
|
||||||
|
|
||||||
|
public virtual bool RequiresUnsafeContext => DeclaredParameters.Any(p => p.ParameterType.RequiresUnsafeContext);
|
||||||
|
|
||||||
// TODO: GetMethodBody()
|
// TODO: GetMethodBody()
|
||||||
|
|
||||||
public string CSharpName =>
|
public string CSharpName =>
|
||||||
@@ -132,6 +134,8 @@ namespace Il2CppInspector.Reflection
|
|||||||
|
|
||||||
var modifiers = new StringBuilder(GetAccessModifierString());
|
var modifiers = new StringBuilder(GetAccessModifierString());
|
||||||
|
|
||||||
|
if (RequiresUnsafeContext)
|
||||||
|
modifiers.Append("unsafe ");
|
||||||
if (IsAbstract)
|
if (IsAbstract)
|
||||||
modifiers.Append("abstract ");
|
modifiers.Append("abstract ");
|
||||||
// Methods that implement interfaces are IsVirtual && IsFinal with MethodAttributes.NewSlot (don't show 'virtual sealed' for these)
|
// Methods that implement interfaces are IsVirtual && IsFinal with MethodAttributes.NewSlot (don't show 'virtual sealed' for these)
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ namespace Il2CppInspector.Reflection
|
|||||||
private readonly int returnTypeUsage;
|
private readonly int returnTypeUsage;
|
||||||
public TypeInfo ReturnType => Assembly.Model.GetTypeFromUsage(returnTypeUsage, MemberTypes.TypeInfo);
|
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
|
// IL2CPP doesn't seem to retain return type custom attributes
|
||||||
|
|
||||||
public MethodInfo(Il2CppInspector pkg, int methodIndex, TypeInfo declaringType) : base(pkg, methodIndex, declaringType) {
|
public MethodInfo(Il2CppInspector pkg, int methodIndex, TypeInfo declaringType) : base(pkg, methodIndex, declaringType) {
|
||||||
|
|||||||
@@ -164,6 +164,9 @@ namespace Il2CppInspector.Reflection {
|
|||||||
public bool IsSpecialName => (Attributes & TypeAttributes.SpecialName) == TypeAttributes.SpecialName;
|
public bool IsSpecialName => (Attributes & TypeAttributes.SpecialName) == TypeAttributes.SpecialName;
|
||||||
public bool IsValueType => BaseType?.FullName == "System.ValueType";
|
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
|
// May get overridden by Il2CppType-based constructor below
|
||||||
public override MemberTypes MemberType { get; } = MemberTypes.TypeInfo;
|
public override MemberTypes MemberType { get; } = MemberTypes.TypeInfo;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user