diff --git a/Cpp2IL.Core/Attributes/RegisterCpp2IlPluginAttribute.cs b/Cpp2IL.Core/Attributes/RegisterCpp2IlPluginAttribute.cs index 37abf67d..d333aba9 100644 --- a/Cpp2IL.Core/Attributes/RegisterCpp2IlPluginAttribute.cs +++ b/Cpp2IL.Core/Attributes/RegisterCpp2IlPluginAttribute.cs @@ -7,12 +7,10 @@ namespace Cpp2IL.Core.Attributes; [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] public class RegisterCpp2IlPluginAttribute : Attribute { -#if NET6_0 [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] -#endif public Type PluginType { get; } - public RegisterCpp2IlPluginAttribute(Type pluginType) + public RegisterCpp2IlPluginAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type pluginType) { if (!typeof(Cpp2IlPlugin).IsAssignableFrom(pluginType)) throw new ArgumentException("Plugin type to register must extend Cpp2IlPlugin", nameof(pluginType)); @@ -22,4 +20,4 @@ public RegisterCpp2IlPluginAttribute(Type pluginType) PluginType = pluginType; } -} \ No newline at end of file +} diff --git a/Cpp2IL.Core/Cpp2IL.Core.csproj b/Cpp2IL.Core/Cpp2IL.Core.csproj index 8c67f80d..a208c322 100644 --- a/Cpp2IL.Core/Cpp2IL.Core.csproj +++ b/Cpp2IL.Core/Cpp2IL.Core.csproj @@ -22,6 +22,7 @@ true + true @@ -40,7 +41,7 @@ - + diff --git a/Cpp2IL.Core/Utils/MiscUtils.cs b/Cpp2IL.Core/Utils/MiscUtils.cs index e861ab31..b2beb0a4 100644 --- a/Cpp2IL.Core/Utils/MiscUtils.cs +++ b/Cpp2IL.Core/Utils/MiscUtils.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text; using Cpp2IL.Core.Extensions; @@ -153,7 +154,7 @@ public static int GetPointerSizeBytes() return LibCpp2IlMain.Binary!.is32Bit ? 4 : 8; } - public static IConvertible ReinterpretBytes(IConvertible original, Type desired) + public static IConvertible ReinterpretBytes(IConvertible original, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] Type desired) { if (desired is null) throw new ArgumentNullException(nameof(desired), "Destination type is null"); diff --git a/LibCpp2IL/LibCpp2IL.csproj b/LibCpp2IL/LibCpp2IL.csproj index 7365d8cc..6f1d44ec 100644 --- a/LibCpp2IL/LibCpp2IL.csproj +++ b/LibCpp2IL/LibCpp2IL.csproj @@ -18,6 +18,7 @@ Library for interacting with IL2CPP metadata and binaries Debug;Release net7.0;net6.0;netstandard2.0 + true @@ -26,12 +27,12 @@ + - diff --git a/LibCpp2IL/LibCpp2IlUtils.cs b/LibCpp2IL/LibCpp2IlUtils.cs index b86ef56f..b8a07060 100644 --- a/LibCpp2IL/LibCpp2IlUtils.cs +++ b/LibCpp2IL/LibCpp2IlUtils.cs @@ -35,22 +35,22 @@ public static class LibCpp2ILUtils {28, "object"} }; - private static readonly Dictionary PrimitiveSizes = new() + private static readonly Dictionary PrimitiveSizes = new() { - {"Byte", 1}, - {"SByte", 1}, - {"Boolean", 1}, - {"Int16", 2}, - {"UInt16", 2}, - {"Char", 2}, - {"Int32", 4}, - {"UInt32", 4}, - {"Single", 4}, - {"Int64", 8}, - {"UInt64", 8}, - {"Double", 8}, - {"IntPtr", 8}, - {"UIntPtr", 8}, + {typeof(byte), 1}, + {typeof(sbyte), 1}, + {typeof(bool), 1}, + {typeof(short), 2}, + {typeof(ushort), 2}, + {typeof(char), 2}, + {typeof(int), 4}, + {typeof(uint), 4}, + {typeof(float), 4}, + {typeof(long), 8}, + {typeof(ulong), 8}, + {typeof(double), 8}, + {typeof(IntPtr), 8}, + {typeof(UIntPtr), 8}, }; private static Dictionary _cachedVersionAttributes = new(); @@ -393,18 +393,17 @@ public static Il2CppTypeReflectionData GetTypeReflectionData(Il2CppType forWhat) throw new ArgumentException($"Unknown type {forWhat.Type}"); } -#pragma warning disable IL2070 //'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields' public static int VersionAwareSizeOf( - Type type, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] Type type, bool dontCheckVersionAttributes = false, bool downsize = true ) { if (type.IsEnum) - type = type.GetEnumUnderlyingType(); + return PrimitiveSizes[type.GetEnumUnderlyingType()]; if (type.IsPrimitive) - return (int) PrimitiveSizes[type.Name]; + return PrimitiveSizes[type]; var shouldDownsize = downsize && LibCpp2IlMain.Binary!.is32Bit; @@ -418,36 +417,32 @@ public static int VersionAwareSizeOf( continue; } - switch (field.FieldType.Name) - { - case "Int64": - case "UInt64": - size += shouldDownsize ? 4 : 8; - break; - case "Int32": - case "UInt32": - size += 4; - break; - case "Int16": - case "UInt16": - size += 2; - break; - case "Byte": - case "SByte": - size += 1; - break; - default: - if (field.FieldType == type) - throw new Exception($"Infinite recursion is not allowed. Field {field} of type {type} has the same type as its parent."); - - size += VersionAwareSizeOf(field.FieldType, dontCheckVersionAttributes, downsize); - break; - } + if (field.FieldType == typeof(long) || field.FieldType == typeof(ulong)) + size += shouldDownsize ? 4 : 8; + else if (field.FieldType == typeof(int) || field.FieldType == typeof(uint)) + size += 4; + else if (field.FieldType == typeof(short) || field.FieldType == typeof(ushort)) + size += 2; + else if (field.FieldType == typeof(byte) || field.FieldType == typeof(sbyte)) + size += 1; + else if (field.FieldType.IsEnum) + size += PrimitiveSizes[field.FieldType.GetEnumUnderlyingType()]; + else if (field.FieldType.IsPrimitive) + size += PrimitiveSizes[field.FieldType]; + else if (field.FieldType == type) + throw new Exception($"Infinite recursion is not allowed. Field {field} of type {type} has the same type as its parent."); + else if (field.FieldType == typeof(Il2CppGenericContext)) + size += VersionAwareSizeOf(typeof(Il2CppGenericContext), dontCheckVersionAttributes, downsize); + else if (field.FieldType == typeof(Il2CppGenericMethodIndices)) + size += VersionAwareSizeOf(typeof(Il2CppGenericMethodIndices), dontCheckVersionAttributes, downsize); + else if (field.FieldType == typeof(Il2CppAssemblyNameDefinition)) + size += VersionAwareSizeOf(typeof(Il2CppAssemblyNameDefinition), dontCheckVersionAttributes, downsize); + else + throw new Exception($"Custom field type '{field.FieldType}' has no special case."); } return size; } -#pragma warning restore IL2070 internal static IEnumerable Range(int start, int count) { diff --git a/LibCpp2IL/Metadata/Il2CppMetadata.cs b/LibCpp2IL/Metadata/Il2CppMetadata.cs index 0cbda72f..8e4b411a 100644 --- a/LibCpp2IL/Metadata/Il2CppMetadata.cs +++ b/LibCpp2IL/Metadata/Il2CppMetadata.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Reflection; @@ -286,7 +287,7 @@ private Il2CppMetadata(MemoryStream stream) : base(stream) } #pragma warning restore 8618 - private T[] ReadMetadataClassArray(int offset, int length) where T : ReadableClass, new() + private T[] ReadMetadataClassArray<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>(int offset, int length) where T : ReadableClass, new() { return ReadReadableArrayAtRawAddr(offset, length / LibCpp2ILUtils.VersionAwareSizeOf(typeof(T), downsize: false)); }