using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using AsmResolver; using AsmResolver.DotNet; using AsmResolver.DotNet.Code.Cil; using AsmResolver.DotNet.Collections; using AsmResolver.DotNet.Signatures; using AsmResolver.PE.DotNet.Cil; using AsmResolver.PE.DotNet.Metadata.Tables; using Microsoft.CodeAnalysis; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")] [assembly: AssemblyMetadata("IsTrimmable", "True")] [assembly: AssemblyCompany("AssetRipper")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyCopyright("Copyright (c) 2024 ds5678")] [assembly: AssemblyDescription("A library for modifying CIL bytecode.")] [assembly: AssemblyFileVersion("1.1.0")] [assembly: AssemblyInformationalVersion("1.1.0+58e24a2bb2113cf5de19fb64002899b85fcedc65")] [assembly: AssemblyProduct("AssetRipper.CIL")] [assembly: AssemblyTitle("AssetRipper.CIL")] [assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/AssetRipper/AssetRipper.CIL")] [assembly: AssemblyVersion("1.1.0.0")] [assembly: TypeForwardedTo(typeof(IsExternalInit))] [module: RefSafetyRules(11)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)] internal sealed class NullableAttribute : Attribute { public readonly byte[] NullableFlags; public NullableAttribute(byte P_0) { NullableFlags = new byte[1] { P_0 }; } public NullableAttribute(byte[] P_0) { NullableFlags = P_0; } } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)] internal sealed class NullableContextAttribute : Attribute { public readonly byte Flag; public NullableContextAttribute(byte P_0) { Flag = P_0; } } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)] internal sealed class RefSafetyRulesAttribute : Attribute { public readonly int Version; public RefSafetyRulesAttribute(int P_0) { Version = P_0; } } } namespace System.Runtime.Versioning { [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Module | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface, AllowMultiple = true, Inherited = false)] [ExcludeFromCodeCoverage] [Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")] internal sealed class ObsoletedOSPlatformAttribute : Attribute { public string? Message { get; } public string? Url { get; set; } public ObsoletedOSPlatformAttribute(string platformName) { } public ObsoletedOSPlatformAttribute(string platformName, string? message) { Message = message; } } } namespace System.Runtime.CompilerServices2 { [AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] [ExcludeFromCodeCoverage] [Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")] internal sealed class InlineArrayAttribute : Attribute { public int Length { get; } public InlineArrayAttribute(int length) { Length = length; } } } namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface, Inherited = false)] [ExcludeFromCodeCoverage] internal sealed class CollectionBuilderAttribute : Attribute { public Type BuilderType { get; } public string MethodName { get; } public CollectionBuilderAttribute(Type builderType, string methodName) { BuilderType = builderType; MethodName = methodName; } } [AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)] [ExcludeFromCodeCoverage] internal sealed class CompilerFeatureRequiredAttribute : Attribute { public const string RefStructs = "RefStructs"; public const string RequiredMembers = "RequiredMembers"; public string FeatureName { get; } public bool IsOptional { get; set; } public CompilerFeatureRequiredAttribute(string featureName) { FeatureName = featureName; } } [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = false)] [ExcludeFromCodeCoverage] internal sealed class RequiredMemberAttribute : Attribute { } [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] [EditorBrowsable(EditorBrowsableState.Never)] [ExcludeFromCodeCoverage] internal sealed class RequiresLocationAttribute : Attribute { } [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] [ExcludeFromCodeCoverage] [Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")] internal sealed class DisableRuntimeMarshallingAttribute : Attribute { } [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] [ExcludeFromCodeCoverage] [Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")] internal sealed class UnsafeAccessorAttribute : Attribute { public UnsafeAccessorKind Kind { get; } public string? Name { get; set; } public UnsafeAccessorAttribute(UnsafeAccessorKind kind) { Kind = kind; } } internal enum UnsafeAccessorKind { Constructor, Method, StaticMethod, Field, StaticField } } namespace System.Diagnostics.CodeAnalysis { [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Module | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Delegate, Inherited = false)] [ExcludeFromCodeCoverage] internal sealed class ExperimentalAttribute : Attribute { public string DiagnosticId { get; } public string? UrlFormat { get; set; } public ExperimentalAttribute(string diagnosticId) { DiagnosticId = diagnosticId; } } [AttributeUsage(AttributeTargets.Constructor, AllowMultiple = false, Inherited = false)] [ExcludeFromCodeCoverage] internal sealed class SetsRequiredMembersAttribute : Attribute { } [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)] [ExcludeFromCodeCoverage] internal sealed class StringSyntaxAttribute : Attribute { public const string CompositeFormat = "CompositeFormat"; public const string DateOnlyFormat = "DateOnlyFormat"; public const string DateTimeFormat = "DateTimeFormat"; public const string EnumFormat = "EnumFormat"; public const string GuidFormat = "GuidFormat"; public const string Json = "Json"; public const string NumericFormat = "NumericFormat"; public const string Regex = "Regex"; public const string TimeOnlyFormat = "TimeOnlyFormat"; public const string TimeSpanFormat = "TimeSpanFormat"; public const string Uri = "Uri"; public const string Xml = "Xml"; public string Syntax { get; } public object?[] Arguments { get; } public StringSyntaxAttribute(string syntax) { Syntax = syntax; Arguments = new object[0]; } public StringSyntaxAttribute(string syntax, params object?[] arguments) { Syntax = syntax; Arguments = arguments; } } [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)] [ExcludeFromCodeCoverage] internal sealed class UnscopedRefAttribute : Attribute { } [AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Method, Inherited = false)] [ExcludeFromCodeCoverage] [Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")] internal sealed class RequiresDynamicCodeAttribute : Attribute { public string Message { get; } public string? Url { get; set; } public RequiresDynamicCodeAttribute(string message) { Message = message; } } } namespace AssetRipper.CIL { public static class CilInstructionCollectionExtensions { public static CilLocalVariable AddLocalVariable(this CilInstructionCollection instructions, TypeSignature variableType) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Expected O, but got Unknown CilLocalVariable val = new CilLocalVariable(variableType); ((Collection)(object)instructions.Owner.LocalVariables).Add(val); return val; } public static void AddDefaultValue(this CilInstructionCollection instructions, TypeSignature type) { //IL_003a: Unknown result type (might be due to invalid IL or missing references) CorLibTypeSignature val = (CorLibTypeSignature)(object)((type is CorLibTypeSignature) ? type : null); if (val != null && ((TypeSignature)val).IsValueType) { instructions.AddDefaultPrimitiveValue(val); } else if (type is ByReferenceTypeSignature) { instructions.AddNullRef(); } else if (type.IsValueTypeOrGenericParameter()) { instructions.AddDefaultValueForUnknownType(type); } else { instructions.Add(CilOpCodes.Ldnull); } } public static void Pop(this CilInstructionCollection instructions) { instructions.RemoveAt(checked(instructions.Count - 1)); } public static CilInstruction AddLoadElement(this CilInstructionCollection instructions, TypeSignature elementType) { //IL_001b: Unknown result type (might be due to invalid IL or missing references) //IL_0020: Unknown result type (might be due to invalid IL or missing references) //IL_0021: Unknown result type (might be due to invalid IL or missing references) //IL_0023: Unknown result type (might be due to invalid IL or missing references) //IL_0059: Expected I4, but got Unknown //IL_005f: Unknown result type (might be due to invalid IL or missing references) //IL_0070: Unknown result type (might be due to invalid IL or missing references) //IL_0081: Unknown result type (might be due to invalid IL or missing references) //IL_0092: Unknown result type (might be due to invalid IL or missing references) //IL_00a3: Unknown result type (might be due to invalid IL or missing references) //IL_00b1: Unknown result type (might be due to invalid IL or missing references) //IL_00bf: Unknown result type (might be due to invalid IL or missing references) //IL_00cd: Unknown result type (might be due to invalid IL or missing references) //IL_00db: Unknown result type (might be due to invalid IL or missing references) //IL_00e9: Unknown result type (might be due to invalid IL or missing references) //IL_00f7: Unknown result type (might be due to invalid IL or missing references) //IL_012a: Unknown result type (might be due to invalid IL or missing references) //IL_011a: Unknown result type (might be due to invalid IL or missing references) //IL_010d: Unknown result type (might be due to invalid IL or missing references) //IL_014d: Unknown result type (might be due to invalid IL or missing references) //IL_0140: Unknown result type (might be due to invalid IL or missing references) CorLibTypeSignature val = (CorLibTypeSignature)(object)((elementType is CorLibTypeSignature) ? elementType : null); if (val == null) { if (elementType is GenericParameterSignature) { return instructions.Add(CilOpCodes.Ldelem); } return elementType.IsValueType ? instructions.Add(CilOpCodes.Ldelem) : instructions.Add(CilOpCodes.Ldelem_Ref); } ElementType elementType2 = ((TypeSignature)val).ElementType; return (CilInstruction)((elementType2 - 2) switch { 0 => instructions.Add(CilOpCodes.Ldelem_U1), 2 => instructions.Add(CilOpCodes.Ldelem_I1), 3 => instructions.Add(CilOpCodes.Ldelem_U1), 4 => instructions.Add(CilOpCodes.Ldelem_I2), 5 => instructions.Add(CilOpCodes.Ldelem_U2), 6 => instructions.Add(CilOpCodes.Ldelem_I4), 7 => instructions.Add(CilOpCodes.Ldelem_U4), 8 => instructions.Add(CilOpCodes.Ldelem_I8), 9 => instructions.Add(CilOpCodes.Ldelem_I8), 10 => instructions.Add(CilOpCodes.Ldelem_R4), 11 => instructions.Add(CilOpCodes.Ldelem_R8), _ => elementType.IsValueType ? instructions.Add(CilOpCodes.Ldelem) : instructions.Add(CilOpCodes.Ldelem_Ref), }); } public static CilInstruction AddStoreElement(this CilInstructionCollection instructions, TypeSignature elementType) { //IL_001b: Unknown result type (might be due to invalid IL or missing references) //IL_0020: Unknown result type (might be due to invalid IL or missing references) //IL_0021: Unknown result type (might be due to invalid IL or missing references) //IL_0023: Unknown result type (might be due to invalid IL or missing references) //IL_0059: Expected I4, but got Unknown //IL_005f: Unknown result type (might be due to invalid IL or missing references) //IL_00ff: Unknown result type (might be due to invalid IL or missing references) //IL_0070: Unknown result type (might be due to invalid IL or missing references) //IL_0081: Unknown result type (might be due to invalid IL or missing references) //IL_008f: Unknown result type (might be due to invalid IL or missing references) //IL_009d: Unknown result type (might be due to invalid IL or missing references) //IL_00ab: Unknown result type (might be due to invalid IL or missing references) //IL_00b9: Unknown result type (might be due to invalid IL or missing references) //IL_00c7: Unknown result type (might be due to invalid IL or missing references) //IL_00d5: Unknown result type (might be due to invalid IL or missing references) //IL_00e3: Unknown result type (might be due to invalid IL or missing references) //IL_00f1: Unknown result type (might be due to invalid IL or missing references) //IL_010f: Unknown result type (might be due to invalid IL or missing references) //IL_0132: Unknown result type (might be due to invalid IL or missing references) //IL_0125: Unknown result type (might be due to invalid IL or missing references) CorLibTypeSignature val = (CorLibTypeSignature)(object)((elementType is CorLibTypeSignature) ? elementType : null); if (val == null) { if (elementType is GenericParameterSignature) { return instructions.Add(CilOpCodes.Stelem); } return elementType.IsValueType ? instructions.Add(CilOpCodes.Stelem) : instructions.Add(CilOpCodes.Stelem_Ref); } ElementType elementType2 = ((TypeSignature)val).ElementType; return (CilInstruction)((elementType2 - 2) switch { 0 => instructions.Add(CilOpCodes.Stelem_I1), 2 => instructions.Add(CilOpCodes.Stelem_I1), 3 => instructions.Add(CilOpCodes.Stelem_I1), 4 => instructions.Add(CilOpCodes.Stelem_I2), 5 => instructions.Add(CilOpCodes.Stelem_I2), 6 => instructions.Add(CilOpCodes.Stelem_I4), 7 => instructions.Add(CilOpCodes.Stelem_I4), 8 => instructions.Add(CilOpCodes.Stelem_I8), 9 => instructions.Add(CilOpCodes.Stelem_I8), 10 => instructions.Add(CilOpCodes.Stelem_R4), 11 => instructions.Add(CilOpCodes.Stelem_R8), _ => instructions.Add(CilOpCodes.Stelem_Ref), }); } public static void AddNullRef(this CilInstructionCollection instructions) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_000d: Unknown result type (might be due to invalid IL or missing references) instructions.Add(CilOpCodes.Ldc_I4_0); instructions.Add(CilOpCodes.Conv_U); } private static void AddDefaultValueForUnknownType(this CilInstructionCollection instructions, TypeSignature type) { //IL_0009: Unknown result type (might be due to invalid IL or missing references) //IL_0016: Unknown result type (might be due to invalid IL or missing references) //IL_0028: Unknown result type (might be due to invalid IL or missing references) CilLocalVariable val = instructions.AddLocalVariable(type); instructions.Add(CilOpCodes.Ldloca, val); instructions.Add(CilOpCodes.Initobj, type.ToTypeDefOrRef()); instructions.Add(CilOpCodes.Ldloc, val); } private static void AddDefaultPrimitiveValue(this CilInstructionCollection instructions, CorLibTypeSignature type) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_0009: Unknown result type (might be due to invalid IL or missing references) //IL_007f: Expected I4, but got Unknown //IL_0085: Unknown result type (might be due to invalid IL or missing references) //IL_0092: Unknown result type (might be due to invalid IL or missing references) //IL_009e: Unknown result type (might be due to invalid IL or missing references) //IL_00ab: Unknown result type (might be due to invalid IL or missing references) //IL_00b7: Unknown result type (might be due to invalid IL or missing references) //IL_00f6: Unknown result type (might be due to invalid IL or missing references) //IL_0108: Unknown result type (might be due to invalid IL or missing references) //IL_011e: Unknown result type (might be due to invalid IL or missing references) //IL_00c4: Unknown result type (might be due to invalid IL or missing references) //IL_00d0: Unknown result type (might be due to invalid IL or missing references) //IL_00dd: Unknown result type (might be due to invalid IL or missing references) //IL_00e9: Unknown result type (might be due to invalid IL or missing references) ElementType elementType = ((TypeSignature)type).ElementType; switch (elementType - 1) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: instructions.Add(CilOpCodes.Ldc_I4_0); break; case 9: instructions.Add(CilOpCodes.Ldc_I4_0); instructions.Add(CilOpCodes.Conv_I8); break; case 10: instructions.Add(CilOpCodes.Ldc_I4_0); instructions.Add(CilOpCodes.Conv_U8); break; case 23: instructions.Add(CilOpCodes.Ldc_I4_0); instructions.Add(CilOpCodes.Conv_I); break; case 24: instructions.Add(CilOpCodes.Ldc_I4_0); instructions.Add(CilOpCodes.Conv_U); break; case 11: instructions.Add(CilOpCodes.Ldc_R4, 0f); break; case 12: instructions.Add(CilOpCodes.Ldc_R8, 0.0); break; case 13: case 27: instructions.Add(CilOpCodes.Ldnull); break; case 21: instructions.AddDefaultValueForUnknownType((TypeSignature)(object)type); break; default: throw new ArgumentOutOfRangeException(null); case 0: break; } } } public static class MethodDefinitionExtensions { public static void ReplaceMethodBodyWithMinimalImplementation(this MethodDefinition methodDefinition) { MethodStubber.FillMethodBodyWithStub(methodDefinition); } public static bool IsInstanceConstructor(this MethodDefinition methodDefinition) { if (methodDefinition.IsConstructor) { return !methodDefinition.IsStatic; } return false; } internal static bool ShouldCallBaseConstructor(this MethodDefinition methodDefinition) { if (methodDefinition.IsInstanceConstructor()) { return !methodDefinition.IsDeclaredByValueType(); } return false; } public static bool IsDeclaredByValueType(this MethodDefinition methodDefinition) { TypeDefinition declaringType = methodDefinition.DeclaringType; return ((declaringType != null) ? new bool?(declaringType.IsValueType) : null) ?? false; } internal static bool ShouldInitializeFields(this MethodDefinition methodDefinition) { if (methodDefinition.IsInstanceConstructor()) { return methodDefinition.IsDeclaredByValueType(); } return false; } public static bool IsManagedMethodWithBody(this MethodDefinition managedMethod) { if (managedMethod.Managed && !managedMethod.IsAbstract && !managedMethod.IsPInvokeImpl && !managedMethod.IsInternalCall && !managedMethod.IsNative) { return !managedMethod.IsRuntime; } return false; } public static bool IsAutoPropertyGetMethod(this MethodDefinition method, [NotNullWhen(true)] out FieldDefinition? backingField) { if (!method.IsGetMethod || method.DeclaringType == null || method.Parameters.Count != 0 || method.Name == null || !method.Name.Value.StartsWith("get_", StringComparison.Ordinal)) { backingField = null; return false; } string value = method.Name.Value; string backingFieldName = "<" + value.Substring(4, value.Length - 4) + ">k__BackingField"; FieldDefinition val = ((IEnumerable)method.DeclaringType.Fields).FirstOrDefault((Func)((FieldDefinition f) => f.Name == backingFieldName)); if (val != null) { SignatureComparer @default = SignatureComparer.Default; FieldSignature signature = val.Signature; TypeSignature obj = ((signature != null) ? signature.FieldType : null); MethodSignature signature2 = method.Signature; if (@default.Equals(obj, (signature2 != null) ? ((MethodSignatureBase)signature2).ReturnType : null) && val.IsStatic == method.IsStatic) { backingField = val; return true; } } backingField = null; return false; } public static bool IsAutoPropertySetMethod(this MethodDefinition method, [NotNullWhen(true)] out FieldDefinition? backingField) { //IL_0032: Unknown result type (might be due to invalid IL or missing references) //IL_0038: Invalid comparison between Unknown and I4 if (method.IsSetMethod && method.DeclaringType != null) { MethodSignature signature = method.Signature; TypeSignature obj = ((signature != null) ? ((MethodSignatureBase)signature).ReturnType : null); CorLibTypeSignature val = (CorLibTypeSignature)(object)((obj is CorLibTypeSignature) ? obj : null); if (val != null && (int)((TypeSignature)val).ElementType == 1 && method.Parameters.Count == 1 && method.Name != null && method.Name.Value.StartsWith("set_", StringComparison.Ordinal)) { string value = method.Name.Value; string backingFieldName = "<" + value.Substring(4, value.Length - 4) + ">k__BackingField"; FieldDefinition val2 = ((IEnumerable)method.DeclaringType.Fields).FirstOrDefault((Func)((FieldDefinition f) => f.Name == backingFieldName)); if (val2 != null) { SignatureComparer @default = SignatureComparer.Default; FieldSignature signature2 = val2.Signature; if (@default.Equals((signature2 != null) ? signature2.FieldType : null, method.Parameters[0].ParameterType) && val2.IsStatic == method.IsStatic) { backingField = val2; return true; } } backingField = null; return false; } } backingField = null; return false; } public static Parameter AddParameter(this MethodDefinition method, TypeSignature parameterSignature, string parameterName) { ParameterDefinition parameterDefinition; return method.AddParameter(parameterSignature, parameterName, out parameterDefinition); } public static Parameter AddParameter(this MethodDefinition method, TypeSignature parameterSignature, string parameterName, out ParameterDefinition parameterDefinition) { //IL_0034: Unknown result type (might be due to invalid IL or missing references) //IL_003a: Expected O, but got Unknown string parameterName2 = parameterName; TypeSignature parameterSignature2 = parameterSignature; parameterDefinition = new ParameterDefinition(checked((ushort)(((MethodSignatureBase)method.Signature).ParameterTypes.Count + 1)), Utf8String.op_Implicit(parameterName2), (ParameterAttributes)0); ((MethodSignatureBase)method.Signature).ParameterTypes.Add(parameterSignature2); method.ParameterDefinitions.Add(parameterDefinition); method.Parameters.PullUpdatesFromMethodSignature(); return ((IEnumerable)method.Parameters).Single((Parameter parameter) => parameter.Name == parameterName2 && parameter.ParameterType == parameterSignature2); } public static Parameter AddParameter(this MethodDefinition method, TypeSignature parameterSignature) { ((MethodSignatureBase)method.Signature).ParameterTypes.Add(parameterSignature); method.Parameters.PullUpdatesFromMethodSignature(); ParameterCollection parameters = method.Parameters; return parameters[parameters.Count - 1]; } } internal static class MethodStubber { public static void FillMethodBodyWithStub(MethodDefinition methodDefinition) { //IL_000b: Unknown result type (might be due to invalid IL or missing references) //IL_0015: Expected O, but got Unknown //IL_0043: Unknown result type (might be due to invalid IL or missing references) //IL_004f: Unknown result type (might be due to invalid IL or missing references) //IL_0034: Unknown result type (might be due to invalid IL or missing references) //IL_0096: Unknown result type (might be due to invalid IL or missing references) //IL_00a2: Unknown result type (might be due to invalid IL or missing references) //IL_00ae: Unknown result type (might be due to invalid IL or missing references) //IL_007b: Unknown result type (might be due to invalid IL or missing references) //IL_0087: Unknown result type (might be due to invalid IL or missing references) //IL_005c: Unknown result type (might be due to invalid IL or missing references) //IL_00bb: Unknown result type (might be due to invalid IL or missing references) //IL_015b: Unknown result type (might be due to invalid IL or missing references) //IL_0249: Unknown result type (might be due to invalid IL or missing references) //IL_00fd: Unknown result type (might be due to invalid IL or missing references) //IL_011b: Unknown result type (might be due to invalid IL or missing references) //IL_01fb: Unknown result type (might be due to invalid IL or missing references) //IL_0209: Unknown result type (might be due to invalid IL or missing references) //IL_0215: Unknown result type (might be due to invalid IL or missing references) //IL_01d8: Unknown result type (might be due to invalid IL or missing references) //IL_01e6: Unknown result type (might be due to invalid IL or missing references) //IL_019f: Unknown result type (might be due to invalid IL or missing references) if (!methodDefinition.IsManagedMethodWithBody()) { return; } methodDefinition.CilMethodBody = new CilMethodBody(methodDefinition); CilInstructionCollection instructions = methodDefinition.CilMethodBody.Instructions; if (methodDefinition.IsAutoPropertyGetMethod(out FieldDefinition backingField)) { if (methodDefinition.IsStatic) { instructions.Add(CilOpCodes.Ldsfld, (IFieldDescriptor)(object)backingField); } else { instructions.Add(CilOpCodes.Ldarg_0); instructions.Add(CilOpCodes.Ldfld, (IFieldDescriptor)(object)backingField); } instructions.Add(CilOpCodes.Ret); return; } if (methodDefinition.IsAutoPropertySetMethod(out backingField)) { if (methodDefinition.IsStatic) { instructions.Add(CilOpCodes.Ldarg_0); instructions.Add(CilOpCodes.Stsfld, (IFieldDescriptor)(object)backingField); } else { instructions.Add(CilOpCodes.Ldarg_0); instructions.Add(CilOpCodes.Ldarg_1); instructions.Add(CilOpCodes.Stfld, (IFieldDescriptor)(object)backingField); } instructions.Add(CilOpCodes.Ret); return; } if (methodDefinition.ShouldInitializeFields()) { foreach (FieldDefinition field in methodDefinition.DeclaringType.Fields) { if (!field.IsStatic && field.Signature != null) { instructions.Add(CilOpCodes.Ldarg_0); instructions.AddDefaultValue(field.Signature.FieldType); instructions.Add(CilOpCodes.Stfld, (IFieldDescriptor)(object)field); } } } if (methodDefinition.ShouldCallBaseConstructor() && TryGetBaseConstructor(methodDefinition, out IMethodDescriptor baseConstructor)) { MethodSignature signature = baseConstructor.Signature; if (signature != null) { instructions.Add(CilOpCodes.Ldarg_0); foreach (TypeSignature parameterType2 in ((MethodSignatureBase)signature).ParameterTypes) { instructions.AddDefaultValue(parameterType2); } instructions.Add(CilOpCodes.Call, baseConstructor); } } foreach (Parameter parameter in methodDefinition.Parameters) { if (parameter.IsOutParameter(out TypeSignature parameterType)) { if (parameterType.IsValueTypeOrGenericParameter()) { instructions.Add(CilOpCodes.Ldarg, parameter); instructions.Add(CilOpCodes.Initobj, parameterType.ToTypeDefOrRef()); } else { instructions.Add(CilOpCodes.Ldarg, parameter); instructions.Add(CilOpCodes.Ldnull); instructions.Add(CilOpCodes.Stind_Ref); } } } instructions.AddDefaultValue(((MethodSignatureBase)methodDefinition.Signature).ReturnType); instructions.Add(CilOpCodes.Ret); instructions.OptimizeMacros(); } private static IMethodDescriptor? TryGetBaseConstructor(MethodDefinition methodDefinition) { //IL_00ff: Unknown result type (might be due to invalid IL or missing references) //IL_0105: Expected O, but got Unknown if (methodDefinition.DeclaringType == null) { return null; } ITypeDefOrRef baseType2 = methodDefinition.DeclaringType.BaseType; if (baseType2 == null) { return null; } TypeDefinition val = (TypeDefinition)(object)((baseType2 is TypeDefinition) ? baseType2 : null); if (val != null) { return (IMethodDescriptor?)(object)GetFirstCompatibleConstructor(methodDefinition.DeclaringType, val); } TypeReference val2 = (TypeReference)(object)((baseType2 is TypeReference) ? baseType2 : null); if (val2 != null) { TypeDefinition val3 = val2.Resolve(); if (val3 == null) { return null; } MethodDefinition val4 = GetFirstCompatibleConstructor(methodDefinition.DeclaringType, val3); if (val4 == null || methodDefinition.DeclaringType.Module == null) { return null; } return (IMethodDescriptor?)(object)methodDefinition.DeclaringType.Module.DefaultImporter.ImportMethod((IMethodDefOrRef)(object)val4); } TypeSpecification val5 = (TypeSpecification)(object)((baseType2 is TypeSpecification) ? baseType2 : null); if (val5 != null) { TypeSignature signature = val5.Signature; GenericInstanceTypeSignature val6 = (GenericInstanceTypeSignature)(object)((signature is GenericInstanceTypeSignature) ? signature : null); if (val6 == null) { return null; } TypeDefinition val7 = ((ITypeDescriptor)val6.GenericType).Resolve(); if (val7 == null) { return null; } MethodDefinition val8 = GetFirstCompatibleConstructor(methodDefinition.DeclaringType, val7); if (val8 == null || methodDefinition.DeclaringType.Module == null) { return null; } IMethodDefOrRef val9 = methodDefinition.DeclaringType.Module.DefaultImporter.ImportMethod((IMethodDefOrRef)(object)val8); return (IMethodDescriptor?)new MemberReference((IMemberRefParent)(object)baseType2, ((IMethodDescriptor)val9).Name, (MemberSignature)(object)((IMethodDescriptor)val9).Signature); } return null; static MethodDefinition? GetFirstCompatibleConstructor(TypeDefinition declaringType, TypeDefinition baseType) { if (declaringType.Module == baseType.Module) { return ((IEnumerable)baseType.Methods).FirstOrDefault((Func)((MethodDefinition m) => m.IsInstanceConstructor() && !m.IsPrivate)); } return ((IEnumerable)baseType.Methods).FirstOrDefault((Func)((MethodDefinition m) => m.IsInstanceConstructor() && (m.IsFamily || m.IsPublic))); } } private static bool TryGetBaseConstructor(MethodDefinition methodDefinition, [NotNullWhen(true)] out IMethodDescriptor? baseConstructor) { baseConstructor = TryGetBaseConstructor(methodDefinition); return baseConstructor != null; } } internal static class ParameterExtensions { public static bool IsOutParameter(this Parameter parameter, [NotNullWhen(true)] out TypeSignature? parameterType) { ParameterDefinition definition = parameter.Definition; if (definition != null && definition.IsOut) { TypeSignature parameterType2 = parameter.ParameterType; ByReferenceTypeSignature val = (ByReferenceTypeSignature)(object)((parameterType2 is ByReferenceTypeSignature) ? parameterType2 : null); if (val != null) { parameterType = ((TypeSpecificationSignature)val).BaseType; return true; } } parameterType = null; return false; } } public static class TypeDefinitionExtensions { public static MethodDefinition AddMethod(this TypeDefinition type, string methodName, MethodAttributes methodAttributes, TypeSignature returnType) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) MethodDefinition val = CreateMethod(methodName, methodAttributes, returnType); type.Methods.Add(val); return val; } private static MethodDefinition CreateMethod(string methodName, MethodAttributes methodAttributes, TypeSignature returnType) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0003: Unknown result type (might be due to invalid IL or missing references) //IL_0005: Invalid comparison between Unknown and I4 //IL_001e: Unknown result type (might be due to invalid IL or missing references) //IL_0020: Unknown result type (might be due to invalid IL or missing references) //IL_0025: Unknown result type (might be due to invalid IL or missing references) //IL_0026: Unknown result type (might be due to invalid IL or missing references) //IL_002c: Expected O, but got Unknown //IL_0027: Unknown result type (might be due to invalid IL or missing references) //IL_0031: Expected O, but got Unknown //IL_0032: Expected O, but got Unknown MethodSignature val = (((methodAttributes & 0x10) > 0) ? MethodSignature.CreateStatic(returnType) : MethodSignature.CreateInstance(returnType)); MethodDefinition val2 = new MethodDefinition(Utf8String.op_Implicit(methodName), methodAttributes, val); val2.CilMethodBody = new CilMethodBody(val2); return val2; } } internal static class TypeSignatureExtensions { public static bool IsValueTypeOrGenericParameter(this TypeSignature type) { if (type != null && (type.IsValueType || type is GenericParameterSignature)) { return true; } return false; } } }