24 Commits
Author SHA1 Message Date
ds5678 aaa9f870f3 Bump version to 1.3.1 in Directory.Build.props
Updated the VersionPrefix property from 1.3.0 to 1.3.1 to prepare for a new patch release. No other changes were made.
2026-03-06 17:19:08 -08:00
ds5678 46a18efa27 Update NUnit to version 4.5.1 in test project
Upgraded the NUnit package in AssetRipper.CIL.Tests.csproj from version 4.5.0 to 4.5.1 to ensure the latest fixes and improvements are included in the test suite. No other dependencies were changed.
2026-03-06 17:18:29 -08:00
ds5678 61c354bd4f Update AsmResolver.DotNet to 6.0.0-rc.1
Upgraded the AsmResolver.DotNet NuGet package from version 6.0.0-beta.6 to 6.0.0-rc.1 to incorporate the latest features and improvements.
2026-03-06 17:16:59 -08:00
ds5678 227524817f Replace .sln with .slnx solution file format
Removed the legacy Visual Studio .sln file and added a new XML-based .slnx solution file. The new format preserves the existing solution structure and items, offering improved readability and tooling compatibility.
2026-03-06 17:09:47 -08:00
ds5678 444e6164f6 Bump version to 1.3.0 in Directory.Build.props
Updated the VersionPrefix from 1.2.2 to 1.3.0 to prepare for a new minor release. No other changes were made.
2026-02-27 21:35:10 -08:00
ds5678 5df204203b Formatting 2026-02-27 21:30:41 -08:00
ds5678 9fa935cb0f Use the underlying types for enums in CilInstructionCollectionExtensions
This affects:
* AddDefaultValue
* InitializeDefaultValue
* AddLoadElement
* AddStoreElement

The code for AddLoadIndirect and AddStoreIndirect was also refactored, but functionality did not change.
2026-02-27 21:27:41 -08:00
ds5678 f10f09581d Fix output for AddLoadElement and AddStoreElement with pointer-sized types 2026-02-27 21:21:40 -08:00
ds5678 672c585e52 Fix stelem and ldelem missing the type 2026-02-27 21:12:08 -08:00
ds5678 c27bbc1164 Refactor generic param array creation to use for loop
Replaced LINQ-based creation of the typeArguments array in MethodStubber with a manual for loop. This improves readability and may enhance performance by eliminating LINQ overhead.
2026-02-27 11:54:11 -08:00
ds5678 eda49e0ff3 Upgrade from AsmResolver beta 5 to beta 6 2026-02-27 11:47:52 -08:00
ds5678 ae137f5afd Update test dependencies and remove coverlet.collector
Removed coverlet.collector package and updated test-related NuGet dependencies: Microsoft.NET.Test.Sdk to 18.3.0, NUnit to 4.5.0, and NUnit3TestAdapter to 6.1.0. No changes to NUnit.Analyzers or project references.
2026-02-27 11:23:10 -08:00
ds5678 b73fe6f510 Bump version to 1.2.2 2025-11-25 01:08:37 -08:00
ds5678 06216e6982 Support .NET 10 2025-11-25 01:07:10 -08:00
ds5678 90ebeb49a1 Update dependencies in test and main projects
Updated the following dependencies to their latest versions:
- `Microsoft.NET.Test.Sdk` to `18.0.1` in `AssetRipper.CIL.Tests.csproj`
- `NUnit.Analyzers` to `4.11.2` in `AssetRipper.CIL.Tests.csproj`
- `NUnit3TestAdapter` to `5.2.0` in `AssetRipper.CIL.Tests.csproj`
- `AsmResolver.DotNet` to `6.0.0-beta.5` in `AssetRipper.CIL.csproj`

These updates include improvements, fixes, and compatibility updates.
2025-11-25 01:05:08 -08:00
ds5678 2d68e67393 Bump version to 1.2.1
Updated the version number in the project properties from 1.2.0 to 1.2.1.
2025-09-15 15:47:05 -07:00
ds5678 d8230781d9 AddThrowNull 2025-09-15 14:43:00 -07:00
ds5678 2990c647c5 Add extensions from AssemblyDumper 2025-09-15 14:41:33 -07:00
ds5678 e871881517 Update project version to 1.2.0
Bump the version number in the project properties from 1.1.6 to 1.2.0.
2025-09-08 14:30:19 -07:00
ds5678 c1eceeda51 AsmResolver 6 beta 4
Resolves #2
2025-09-08 14:25:12 -07:00
ds5678 44996e27ff Update NuGet packages in project files
Updated several NuGet packages in `AssetRipper.CIL.Tests.csproj`:
- Microsoft.NET.Test.Sdk: 17.13.0 -> 17.14.1
- NUnit: 4.3.2 -> 4.4.0
- NUnit.Analyzers: 4.6.0 -> 4.10.0
- NUnit3TestAdapter: 5.0.0 -> 5.1.0

Also updated `System.Runtime.CompilerServices.Unsafe` in `AssetRipper.CIL.csproj` from version 6.1.1 to 6.1.2.
2025-09-08 14:20:20 -07:00
ds5678 0039dc95b3 Use IsAccessibleFromType when searching for a base constructor
* Related: #2
2025-04-08 15:06:57 -07:00
ds5678 0953640bf1 Add ReplaceMethodBodyWithThrowNull method
Introduces a new method `ReplaceMethodBodyWithThrowNull` in the `MethodDefinitionExtensions` class. This method modifies the body of a managed method to throw a `null` exception, with appropriate XML documentation. It includes checks to ensure the method is managed and has a body before making changes.
2025-03-23 20:20:02 -07:00
ds5678 c1e8657d67 Ensure references are imported 2025-03-21 19:23:54 -07:00
22 changed files with 211 additions and 147 deletions

No files matched your search

@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
@@ -9,17 +9,13 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
<PackageReference Include="NUnit" Version="4.5.1" />
<PackageReference Include="NUnit.Analyzers" Version="4.11.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="NUnit" Version="4.3.2" />
<PackageReference Include="NUnit.Analyzers" Version="4.6.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" Version="5.0.0" />
<PackageReference Include="NUnit3TestAdapter" Version="6.1.0" />
</ItemGroup>
<ItemGroup>
+1 -1
View File
@@ -94,7 +94,7 @@ public class LoadIndirectTests
private static void AssertCorrectLoadIndirect(TypeSignature typeToLoad, CilCode expectedOpCode)
{
MethodDefinition method = new MethodDefinition("Test", MethodAttributes.Public | MethodAttributes.Static, null);
method.CilMethodBody = new(method);
method.CilMethodBody = new();
CilInstructionCollection instructions = method.CilMethodBody.Instructions;
instructions.AddLoadIndirect(typeToLoad);
Assert.That(instructions, Has.Count.EqualTo(1));
+1 -1
View File
@@ -94,7 +94,7 @@ public class StoreIndirectTests
private static void AssertCorrectStoreIndirect(TypeSignature typeToStore, CilCode expectedOpCode)
{
MethodDefinition method = new MethodDefinition("Test", MethodAttributes.Public | MethodAttributes.Static, null);
method.CilMethodBody = new(method);
method.CilMethodBody = new();
CilInstructionCollection instructions = method.CilMethodBody.Instructions;
instructions.AddStoreIndirect(typeToStore);
Assert.That(instructions, Has.Count.EqualTo(1));
@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
@@ -37,7 +37,7 @@ public static class CilInstructionCollectionEquality
}
// Instructions
return Equals(left.Instructions, right.Instructions);
return Equals(left.Instructions, right.Instructions);
}
public static bool Equals(CilExceptionHandler left, CilExceptionHandler right)
-58
View File
@@ -1,58 +0,0 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34321.82
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AssetRipper.CIL", "AssetRipper.CIL\AssetRipper.CIL.csproj", "{3F385513-F04F-4282-9B91-2693CA93ED1C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{88415A5B-37C1-4CDF-93E0-2682460B6390}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.gitignore = .gitignore
Directory.Build.props = Directory.Build.props
LICENSE = LICENSE
README.md = README.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AssetRipper.CIL.Validator", "AssetRipper.CIL.Validator\AssetRipper.CIL.Validator.csproj", "{0E177F49-031D-486A-ADD2-6823263D1923}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssetRipper.CIL.Standardizer", "AssetRipper.CIL.Standardizer\AssetRipper.CIL.Standardizer.csproj", "{9E6D8580-5C3C-4AA8-89C9-34F475B79B24}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssetRipper.CIL.Stubber", "AssetRipper.CIL.Stubber\AssetRipper.CIL.Stubber.csproj", "{A35A3452-2211-4DF5-A5A5-406FDF1E1A76}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssetRipper.CIL.Tests", "AssetRipper.CIL.Tests\AssetRipper.CIL.Tests.csproj", "{FC03589F-7457-45E7-9628-F9E003F30BAB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3F385513-F04F-4282-9B91-2693CA93ED1C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3F385513-F04F-4282-9B91-2693CA93ED1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3F385513-F04F-4282-9B91-2693CA93ED1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3F385513-F04F-4282-9B91-2693CA93ED1C}.Release|Any CPU.Build.0 = Release|Any CPU
{0E177F49-031D-486A-ADD2-6823263D1923}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0E177F49-031D-486A-ADD2-6823263D1923}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0E177F49-031D-486A-ADD2-6823263D1923}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0E177F49-031D-486A-ADD2-6823263D1923}.Release|Any CPU.Build.0 = Release|Any CPU
{9E6D8580-5C3C-4AA8-89C9-34F475B79B24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9E6D8580-5C3C-4AA8-89C9-34F475B79B24}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9E6D8580-5C3C-4AA8-89C9-34F475B79B24}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9E6D8580-5C3C-4AA8-89C9-34F475B79B24}.Release|Any CPU.Build.0 = Release|Any CPU
{A35A3452-2211-4DF5-A5A5-406FDF1E1A76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A35A3452-2211-4DF5-A5A5-406FDF1E1A76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A35A3452-2211-4DF5-A5A5-406FDF1E1A76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A35A3452-2211-4DF5-A5A5-406FDF1E1A76}.Release|Any CPU.Build.0 = Release|Any CPU
{FC03589F-7457-45E7-9628-F9E003F30BAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FC03589F-7457-45E7-9628-F9E003F30BAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC03589F-7457-45E7-9628-F9E003F30BAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FC03589F-7457-45E7-9628-F9E003F30BAB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FEAD0DEB-0A49-4BC9-A468-3D1E9D52EF2C}
EndGlobalSection
EndGlobal
+14
View File
@@ -0,0 +1,14 @@
<Solution>
<Folder Name="/Solution Items/">
<File Path=".editorconfig" />
<File Path=".gitignore" />
<File Path="Directory.Build.props" />
<File Path="LICENSE" />
<File Path="README.md" />
</Folder>
<Project Path="AssetRipper.CIL.Standardizer/AssetRipper.CIL.Standardizer.csproj" />
<Project Path="AssetRipper.CIL.Stubber/AssetRipper.CIL.Stubber.csproj" />
<Project Path="AssetRipper.CIL.Tests/AssetRipper.CIL.Tests.csproj" />
<Project Path="AssetRipper.CIL.Validator/AssetRipper.CIL.Validator.csproj" />
<Project Path="AssetRipper.CIL/AssetRipper.CIL.csproj" />
</Solution>
+3 -3
View File
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0;net9.0;net10.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<PackageId>AssetRipper.CIL</PackageId>
@@ -15,9 +15,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AsmResolver.DotNet" Version="6.0.0-beta.3" />
<PackageReference Include="AsmResolver.DotNet" Version="6.0.0-rc.1" />
<PackageReference Include="PolySharp" Version="1.15.0" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.1" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.2" />
</ItemGroup>
<ItemGroup>
@@ -3,6 +3,7 @@ using AsmResolver.DotNet.Signatures;
using AsmResolver.PE.DotNet.Cil;
using AsmResolver.PE.DotNet.Metadata.Tables;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace AssetRipper.CIL;
@@ -26,6 +27,10 @@ public static class CilInstructionCollectionExtensions
{
instructions.AddNullRef();
}
else if (type is TypeDefOrRefSignature t && TryGetEnumUnderlyingType(instructions, t, out CorLibTypeSignature? underlyingType))
{
instructions.AddDefaultPrimitiveValue(underlyingType);
}
else if (type.IsValueTypeOrGenericParameter())
{
instructions.AddDefaultValueForUnknownType(type);
@@ -48,6 +53,10 @@ public static class CilInstructionCollectionExtensions
instructions.AddNullRef();
instructions.Add(CilOpCodes.Stloc, localVariable);
}
else if (type is TypeDefOrRefSignature t && TryGetEnumUnderlyingType(instructions, t, out CorLibTypeSignature? underlyingType))
{
instructions.InitializeDefaultPrimitiveValue(underlyingType, localVariable);
}
else if (type.IsValueTypeOrGenericParameter())
{
instructions.InitializeDefaultValueForUnknownType(localVariable);
@@ -71,6 +80,12 @@ public static class CilInstructionCollectionExtensions
instructions.Add(CilOpCodes.Ceq);
}
public static void AddThrowNull(this CilInstructionCollection instructions)
{
instructions.Add(CilOpCodes.Ldnull);
instructions.Add(CilOpCodes.Throw);
}
public static CilInstruction AddLoadElement(this CilInstructionCollection instructions, TypeSignature elementType)
{
return elementType switch
@@ -88,11 +103,24 @@ public static class CilInstructionCollectionExtensions
ElementType.U8 => instructions.Add(CilOpCodes.Ldelem_I8),
ElementType.R4 => instructions.Add(CilOpCodes.Ldelem_R4),
ElementType.R8 => instructions.Add(CilOpCodes.Ldelem_R8),
_ => elementType.IsValueType ? instructions.Add(CilOpCodes.Ldelem) : instructions.Add(CilOpCodes.Ldelem_Ref)
ElementType.I or ElementType.U => instructions.Add(CilOpCodes.Ldelem_I),
ElementType.Object or ElementType.String => instructions.Add(CilOpCodes.Ldelem_Ref),
_ => instructions.Add(CilOpCodes.Ldelem, elementType.ToTypeDefOrRef()),
},
GenericParameterSignature => instructions.Add(CilOpCodes.Ldelem),
_ => elementType.IsValueType ? instructions.Add(CilOpCodes.Ldelem) : instructions.Add(CilOpCodes.Ldelem_Ref)
GenericParameterSignature => instructions.Add(CilOpCodes.Ldelem, elementType.ToTypeDefOrRef()),
PointerTypeSignature => instructions.Add(CilOpCodes.Ldelem_I),
TypeDefOrRefSignature t => TryGetEnumUnderlyingType(instructions, t, out CorLibTypeSignature? underlyingType)
? instructions.AddLoadElement(underlyingType)
: AddUnknown(instructions, t),
_ => AddUnknown(instructions, elementType)
};
static CilInstruction AddUnknown(CilInstructionCollection instructions, TypeSignature elementType)
{
return elementType.IsValueType
? instructions.Add(CilOpCodes.Ldelem, elementType.ToTypeDefOrRef())
: instructions.Add(CilOpCodes.Ldelem_Ref);
}
}
public static CilInstruction AddStoreElement(this CilInstructionCollection instructions, TypeSignature elementType)
@@ -112,11 +140,24 @@ public static class CilInstructionCollectionExtensions
ElementType.U8 => instructions.Add(CilOpCodes.Stelem_I8),
ElementType.R4 => instructions.Add(CilOpCodes.Stelem_R4),
ElementType.R8 => instructions.Add(CilOpCodes.Stelem_R8),
_ => instructions.Add(CilOpCodes.Stelem_Ref),
ElementType.I or ElementType.U => instructions.Add(CilOpCodes.Stelem_I),
ElementType.Object or ElementType.String => instructions.Add(CilOpCodes.Stelem_Ref),
_ => instructions.Add(CilOpCodes.Stelem, elementType.ToTypeDefOrRef()),
},
GenericParameterSignature => instructions.Add(CilOpCodes.Stelem),
_ => elementType.IsValueType ? instructions.Add(CilOpCodes.Stelem) : instructions.Add(CilOpCodes.Stelem_Ref)
GenericParameterSignature => instructions.Add(CilOpCodes.Stelem, elementType.ToTypeDefOrRef()),
PointerTypeSignature => instructions.Add(CilOpCodes.Stelem_I),
TypeDefOrRefSignature t => TryGetEnumUnderlyingType(instructions, t, out CorLibTypeSignature? underlyingType)
? instructions.AddStoreElement(underlyingType)
: AddUnknown(instructions, t),
_ => AddUnknown(instructions, elementType)
};
static CilInstruction AddUnknown(CilInstructionCollection instructions, TypeSignature elementType)
{
return elementType.IsValueType
? instructions.Add(CilOpCodes.Stelem, elementType.ToTypeDefOrRef())
: instructions.Add(CilOpCodes.Stelem_Ref);
}
}
public static CilInstruction AddLoadIndirect(this CilInstructionCollection instructions, TypeSignature type)
@@ -140,7 +181,9 @@ public static class CilInstructionCollectionExtensions
},
PointerTypeSignature => instructions.Add(CilOpCodes.Ldind_I),
GenericParameterSignature => instructions.Add(CilOpCodes.Ldobj, type.ToTypeDefOrRef()),
TypeDefOrRefSignature t => AddTypeDefOrRef(instructions, t),
TypeDefOrRefSignature t => TryGetEnumUnderlyingType(instructions, t, out CorLibTypeSignature? underlyingType)
? instructions.AddLoadIndirect(underlyingType)
: AddUnknown(instructions, type),
_ => AddUnknown(instructions, type),
};
@@ -150,15 +193,6 @@ public static class CilInstructionCollectionExtensions
? instructions.Add(CilOpCodes.Ldobj, type.ToTypeDefOrRef())
: instructions.Add(CilOpCodes.Ldind_Ref);
}
static CilInstruction AddTypeDefOrRef(CilInstructionCollection instructions, TypeDefOrRefSignature type)
{
// Check if the type is an enum
TypeSignature underlyingType = type.GetUnderlyingType();
return underlyingType is CorLibTypeSignature
? instructions.AddLoadIndirect(underlyingType)
: AddUnknown(instructions, underlyingType);
}
}
public static CilInstruction AddStoreIndirect(this CilInstructionCollection instructions, TypeSignature type)
@@ -182,7 +216,9 @@ public static class CilInstructionCollectionExtensions
},
PointerTypeSignature => instructions.Add(CilOpCodes.Stind_I),
GenericParameterSignature => instructions.Add(CilOpCodes.Stobj, type.ToTypeDefOrRef()),
TypeDefOrRefSignature t => AddTypeDefOrRef(instructions, t),
TypeDefOrRefSignature t => TryGetEnumUnderlyingType(instructions, t, out CorLibTypeSignature? underlyingType)
? instructions.AddStoreIndirect(underlyingType)
: AddUnknown(instructions, t),
_ => AddUnknown(instructions, type),
};
@@ -192,15 +228,6 @@ public static class CilInstructionCollectionExtensions
? instructions.Add(CilOpCodes.Stobj, type.ToTypeDefOrRef())
: instructions.Add(CilOpCodes.Stind_Ref);
}
static CilInstruction AddTypeDefOrRef(CilInstructionCollection instructions, TypeDefOrRefSignature type)
{
// Check if the type is an enum
TypeSignature underlyingType = type.GetUnderlyingType();
return underlyingType is CorLibTypeSignature
? instructions.AddStoreIndirect(underlyingType)
: AddUnknown(instructions, underlyingType);
}
}
/// <summary>
@@ -331,4 +358,10 @@ public static class CilInstructionCollectionExtensions
break;
}
}
private static bool TryGetEnumUnderlyingType(CilInstructionCollection instructions, TypeDefOrRefSignature type, [NotNullWhen(true)] out CorLibTypeSignature? underlyingType)
{
underlyingType = type.GetUnderlyingType(instructions.Owner.Owner?.DeclaringModule?.RuntimeContext) as CorLibTypeSignature;
return underlyingType is not null;
}
}
@@ -12,4 +12,19 @@ public static class CustomAttributeExtensions
attribute.Signature.FixedArguments.Add(argument);
return argument;
}
public static CustomAttributeArgument AddFixedArgument<T>(this CustomAttribute attribute, TypeSignature paramType, T[] paramValue)
{
CustomAttributeArgument argument = new CustomAttributeArgument(paramType, paramValue.Select(e => (object?)e));
attribute.Signature!.FixedArguments.Add(argument);
return argument;
}
public static CustomAttributeNamedArgument AddNamedArgument(this CustomAttribute attribute, TypeSignature memberType, string memberName, TypeSignature paramType, object paramValue, CustomAttributeArgumentMemberType memberKind)
{
CustomAttributeArgument argument = new CustomAttributeArgument(paramType, paramValue);
CustomAttributeNamedArgument namedArgument = new CustomAttributeNamedArgument(memberKind, memberName, memberType, argument);
attribute.Signature!.NamedArguments.Add(namedArgument);
return namedArgument;
}
}
+9 -2
View File
@@ -35,12 +35,19 @@ public static class EventDefinitionExtensions
public static bool HasBackingField(this EventDefinition eventDefinition, [NotNullWhen(true)] out FieldDefinition? backingField)
{
TypeSignature? eventType = eventDefinition.EventType?.ToTypeSignature();
RuntimeContext? runtimeContext = eventDefinition.DeclaringModule?.RuntimeContext;
if (runtimeContext is null)
{
backingField = null;
return false;
}
TypeSignature? eventType = eventDefinition.EventType?.ToTypeSignature(runtimeContext);
if (eventType is not null)
{
foreach (FieldDefinition field in eventDefinition.DeclaringType?.Fields ?? [])
{
if (field.IsPrivate && field.Name == eventDefinition.Name && SignatureComparer.Default.Equals(field.Signature?.FieldType, eventType))
if (field.IsPrivate && field.Name == eventDefinition.Name && runtimeContext.SignatureComparer.Equals(field.Signature?.FieldType, eventType))
{
backingField = field;
return true;
+4 -3
View File
@@ -1,5 +1,4 @@
using AsmResolver.DotNet;
using AsmResolver.DotNet.Signatures;
using System.Diagnostics.CodeAnalysis;
namespace AssetRipper.CIL;
@@ -33,7 +32,8 @@ public static class FieldDefinitionExtensions
property = field.DeclaringType?.Properties.FirstOrDefault(p => p.Name == propertyName);
return property is not null
&& field.IsStatic == property.IsStatic()
&& SignatureComparer.Default.Equals(field.Signature?.FieldType, property.Signature?.ReturnType);
&& field.DeclaringModule?.RuntimeContext is { } runtimeContext
&& runtimeContext.SignatureComparer.Equals(field.Signature?.FieldType, property.Signature?.ReturnType);
}
else
{
@@ -49,7 +49,8 @@ public static class FieldDefinitionExtensions
@event = field.DeclaringType?.Events.FirstOrDefault(e => e.Name == field.Name);
return @event is not null
&& field.IsStatic == @event.IsStatic()
&& SignatureComparer.Default.Equals(@event.EventType?.ToTypeSignature(), field.Signature?.FieldType);
&& @event.DeclaringModule?.RuntimeContext is { } runtimeContext
&& runtimeContext.SignatureComparer.Equals(@event.EventType?.ToTypeSignature(runtimeContext), field.Signature?.FieldType);
}
else
{
@@ -27,9 +27,26 @@ public static class IHasCustomAttributeExtensions
return attribute;
}
public static CustomAttribute AddCustomAttribute<T>(this IHasCustomAttribute _this, IMethodDefOrRef constructor, SzArrayTypeSignature paramType, T[] paramValue)
{
CustomAttribute attribute = _this.AddCustomAttribute(constructor);
attribute.AddFixedArgument(paramType, paramValue);
return attribute;
}
public static CustomAttribute AddCustomAttribute(this IHasCustomAttribute _this, IMethodDefOrRef constructor, params (TypeSignature, object)[] parameters)
{
CustomAttribute attribute = _this.AddCustomAttribute(constructor);
for (int i = 0; i < parameters.Length; i++)
{
attribute.AddFixedArgument(parameters[i].Item1, parameters[i].Item2);
}
return attribute;
}
internal static ModuleDefinition GetModule(this IHasCustomAttribute _this)
{
return (_this as IModuleProvider)?.Module ?? throw new ArgumentException("Entity does not have a module", nameof(_this));
return (_this as IModuleProvider)?.ContextModule ?? throw new ArgumentException("Entity does not have a module", nameof(_this));
}
public static CustomAttribute AddFlagsAttribute(this IHasCustomAttribute _this)
@@ -44,7 +61,7 @@ public static class IHasCustomAttributeExtensions
{
TypeReference flagsTypeRef = new(module.CorLibTypeFactory.CorLibScope, "System", nameof(FlagsAttribute));
MemberReference constructor = new(flagsTypeRef, ".ctor", MethodSignature.CreateInstance(module.CorLibTypeFactory.Void));
return _this.AddCustomAttribute(constructor);
return _this.AddCustomAttribute(module.DefaultImporter.ImportMethod(constructor));
}
}
@@ -69,7 +86,7 @@ public static class IHasCustomAttributeExtensions
MemberReference constructor = new(compilerGeneratedTypeRef, ".ctor", MethodSignature.CreateInstance(module.CorLibTypeFactory.Void));
return _this.AddCustomAttribute(constructor);
return _this.AddCustomAttribute(module.DefaultImporter.ImportMethod(constructor));
}
static AssemblyReference GetOrAddReferenceToSystemRuntimeCompilerServices(ModuleDefinition module)
@@ -11,7 +11,7 @@ public sealed class SwitchRemover : ICilManipulator
{
public void Execute(CilManipulationContext context)
{
ModuleDefinition? module = context.Instructions.Owner.Owner.Module;
ModuleDefinition? module = context.Instructions.Owner.Owner?.DeclaringModule;
if (module is null)
{
return;
+30 -4
View File
@@ -1,4 +1,5 @@
using AsmResolver.DotNet;
using AsmResolver.DotNet.Code.Cil;
using AsmResolver.DotNet.Collections;
using AsmResolver.DotNet.Signatures;
using AsmResolver.PE.DotNet.Metadata.Tables;
@@ -23,6 +24,24 @@ public static class MethodDefinitionExtensions
MethodStubber.FillMethodBodyWithStub(methodDefinition);
}
/// <summary>
/// Replaces the method body with: <code>throw null;</code>
/// </summary>
/// <remarks>
/// If the method is not managed or should not have a body, this method does nothing.
/// </remarks>
/// <param name="methodDefinition">The method to modify.</param>
public static void ReplaceMethodBodyWithThrowNull(this MethodDefinition methodDefinition)
{
if (!methodDefinition.IsManagedMethodWithBody())
{
return;
}
methodDefinition.CilMethodBody = new();
CilInstructionCollection methodInstructions = methodDefinition.CilMethodBody.Instructions;
methodInstructions.AddThrowNull();
}
public static bool IsInstance(this MethodDefinition methodDefinition)
{
return !methodDefinition.IsStatic;
@@ -72,6 +91,11 @@ public static class MethodDefinitionExtensions
&& !managedMethod.IsRuntime;
}
public static CilInstructionCollection GetInstructions(this MethodDefinition method)
{
return method.CilMethodBody?.Instructions ?? throw new ArgumentException("CilMethodBody was null", nameof(method));
}
public static bool IsAutoPropertyGetMethod(this MethodDefinition method, [NotNullWhen(true)] out FieldDefinition? backingField)
{
if (!method.IsGetMethod
@@ -89,7 +113,8 @@ public static class MethodDefinitionExtensions
if (field is null
|| !field.IsPrivate
|| field.IsStatic != method.IsStatic
|| !SignatureComparer.Default.Equals(field.Signature?.FieldType, method.Signature?.ReturnType))
|| method.DeclaringModule?.RuntimeContext is not { } runtimeContext
|| !runtimeContext.SignatureComparer.Equals(field.Signature?.FieldType, method.Signature?.ReturnType))
{
backingField = null;
return false;
@@ -119,7 +144,8 @@ public static class MethodDefinitionExtensions
if (field is null
|| !field.IsPrivate
|| field.IsStatic != method.IsStatic
|| !SignatureComparer.Default.Equals(field.Signature?.FieldType, method.Parameters[0].ParameterType))
|| method.DeclaringModule?.RuntimeContext is not { } runtimeContext
|| !runtimeContext.SignatureComparer.Equals(field.Signature?.FieldType, method.Parameters[0].ParameterType))
{
backingField = null;
return false;
@@ -131,12 +157,12 @@ public static class MethodDefinitionExtensions
}
}
public static Parameter AddParameter(this MethodDefinition method, TypeSignature parameterSignature, string parameterName)
public static Parameter AddParameter(this MethodDefinition method, TypeSignature parameterSignature, string? parameterName)
{
return method.AddParameter(parameterSignature, parameterName, out _);
}
public static Parameter AddParameter(this MethodDefinition method, TypeSignature parameterSignature, string parameterName, out ParameterDefinition parameterDefinition)
public static Parameter AddParameter(this MethodDefinition method, TypeSignature parameterSignature, string? parameterName, out ParameterDefinition parameterDefinition)
{
parameterDefinition = new ParameterDefinition((ushort)(method.Signature!.ParameterTypes.Count + 1), parameterName, default);
method.Signature.ParameterTypes.Add(parameterSignature);
+22 -21
View File
@@ -16,7 +16,7 @@ internal static class MethodStubber
return;
}
methodDefinition.CilMethodBody = new(methodDefinition);
methodDefinition.CilMethodBody = new();
CilInstructionCollection methodInstructions = methodDefinition.CilMethodBody.Instructions;
if (methodDefinition.IsAutoPropertyGetMethod(out FieldDefinition? backingField))
@@ -118,6 +118,11 @@ internal static class MethodStubber
{
return null;
}
RuntimeContext? runtimeContext = methodDefinition.DeclaringModule?.RuntimeContext;
if (runtimeContext is null)
{
return null;
}
if (baseType is TypeDefinition baseTypeDef)
{
@@ -125,19 +130,19 @@ internal static class MethodStubber
}
else if (baseType is TypeReference baseTypeRef)
{
TypeDefinition? baseTypeResolved = baseTypeRef.Resolve();
if (baseTypeResolved is null)
ResolutionStatus resolutionStatus = baseTypeRef.Resolve(runtimeContext, out TypeDefinition? baseTypeResolved);
if (baseTypeResolved is null || resolutionStatus is not ResolutionStatus.Success)
{
return null;
}
MethodDefinition? baseConstructor = GetFirstCompatibleConstructor(methodDefinition.DeclaringType, baseTypeResolved);
if (baseConstructor is null || methodDefinition.DeclaringType.Module is null)
if (baseConstructor is null || methodDefinition.DeclaringType.DeclaringModule is null)
{
return null;
}
else
{
return methodDefinition.DeclaringType.Module.DefaultImporter.ImportMethod(baseConstructor);
return methodDefinition.DeclaringType.DeclaringModule.DefaultImporter.ImportMethod(baseConstructor);
}
}
else if (baseType is TypeSpecification baseTypeSpec)
@@ -147,17 +152,17 @@ internal static class MethodStubber
{
return null;
}
TypeDefinition? baseTypeResolved = genericInstanceTypeSignature.GenericType.Resolve();
if (baseTypeResolved is null)
ResolutionStatus resolutionStatus = genericInstanceTypeSignature.GenericType.Resolve(runtimeContext, out TypeDefinition? baseTypeResolved);
if (baseTypeResolved is null || resolutionStatus is not ResolutionStatus.Success)
{
return null;
}
MethodDefinition? baseConstructor = GetFirstCompatibleConstructor(methodDefinition.DeclaringType, baseTypeResolved);
if (baseConstructor is null || methodDefinition.DeclaringType.Module is null)
if (baseConstructor is null || methodDefinition.DeclaringType.DeclaringModule is null)
{
return null;
}
IMethodDefOrRef baseConstructorImported = methodDefinition.DeclaringType.Module.DefaultImporter.ImportMethod(baseConstructor);
IMethodDefOrRef baseConstructorImported = methodDefinition.DeclaringType.DeclaringModule.DefaultImporter.ImportMethod(baseConstructor);
return new MemberReference(baseType, baseConstructorImported.Name, baseConstructorImported.Signature);
}
@@ -165,14 +170,7 @@ internal static class MethodStubber
static MethodDefinition? GetFirstCompatibleConstructor(TypeDefinition declaringType, TypeDefinition baseType)
{
if (declaringType.Module == baseType.Module)
{
return baseType.Methods.FirstOrDefault(m => m.IsInstanceConstructor() && !m.IsPrivate);
}
else
{
return baseType.Methods.FirstOrDefault(m => m.IsInstanceConstructor() && (m.IsFamily || m.IsPublic));
}
return baseType.Methods.FirstOrDefault(m => m.IsInstanceConstructor() && m.IsAccessibleFromType(declaringType, m.DeclaringModule?.RuntimeContext));
}
}
@@ -189,10 +187,13 @@ internal static class MethodStubber
return field;
}
TypeSignature[] typeArguments = Enumerable.Range(0, field.DeclaringType.GenericParameters.Count)
.Select(i => new GenericParameterSignature(field.Module, GenericParameterType.Type, i))
.ToArray();
int count = field.DeclaringType.GenericParameters.Count;
TypeSignature[] typeArguments = new TypeSignature[count];
for (int i = 0; i < count; i++)
{
typeArguments[i] = new GenericParameterSignature(field.DeclaringModule, GenericParameterType.Type, i);
}
return new MemberReference(field.DeclaringType.MakeGenericInstanceType(typeArguments).ToTypeDefOrRef(), field.Name, field.Signature);
return new MemberReference(field.DeclaringType.MakeGenericInstanceType(field.DeclaringModule?.RuntimeContext, typeArguments).ToTypeDefOrRef(), field.Name, field.Signature);
}
}
@@ -25,7 +25,7 @@ public static class ModuleDefinitionExtensions
?? throw new ArgumentOutOfRangeException(nameof(underlyingType), underlyingType, null);
TypeReference baseType = new(module.CorLibTypeFactory.CorLibScope, "System", "Enum");
TypeDefinition type = new(@namespace, name, visibility.ToTypeAttributes() | TypeAttributes.Sealed, baseType);
TypeDefinition type = new(@namespace, name, visibility.ToTypeAttributes() | TypeAttributes.Sealed, module.DefaultImporter.ImportType(baseType));
module.TopLevelTypes.Add(type);
AddEnumValue(type, underlyingTypeSignature);
return type;
@@ -1,5 +1,4 @@
using AsmResolver.DotNet;
using AsmResolver.DotNet.Signatures;
using System.Diagnostics.CodeAnalysis;
namespace AssetRipper.CIL;
@@ -13,7 +12,8 @@ public static class PropertyDefinitionExtensions
if (backingField is null
|| !backingField.IsPrivate
|| backingField.IsStatic != property.IsStatic()
|| !SignatureComparer.Default.Equals(backingField.Signature?.FieldType, property.Signature?.ReturnType))
|| backingField.DeclaringModule?.RuntimeContext is not { } runtimeContext
|| !runtimeContext.SignatureComparer.Equals(backingField.Signature?.FieldType, property.Signature?.ReturnType))
{
backingField = null;
return false;
+19 -6
View File
@@ -7,7 +7,7 @@ namespace AssetRipper.CIL;
public static class TypeDefinitionExtensions
{
public static FieldDefinition AddField(this TypeDefinition type, string fieldName, TypeSignature fieldType, bool isStatic = false, Visibility visibility = Visibility.Public)
public static FieldDefinition AddField(this TypeDefinition type, string? fieldName, TypeSignature fieldType, bool isStatic = false, Visibility visibility = Visibility.Public)
{
FieldAttributes fieldAttributes = visibility.ToFieldAttributes() | (isStatic ? FieldAttributes.Static : default);
FieldDefinition field = new FieldDefinition(fieldName, fieldAttributes, fieldType);
@@ -34,7 +34,7 @@ public static class TypeDefinitionExtensions
return field;
}
public static MethodDefinition AddMethod(this TypeDefinition type, string methodName, MethodAttributes methodAttributes, TypeSignature returnType)
public static MethodDefinition AddMethod(this TypeDefinition type, string? methodName, MethodAttributes methodAttributes, TypeSignature returnType)
{
MethodDefinition method = CreateMethod(methodName, methodAttributes, returnType);
type.Methods.Add(method);
@@ -44,7 +44,7 @@ public static class TypeDefinitionExtensions
public static MethodDefinition AddConstructor(this TypeDefinition type, Visibility visibility = Visibility.Public)
{
MethodAttributes methodAttributes = visibility.ToMethodAttributes() | MethodAttributes.SpecialName | MethodAttributes.RuntimeSpecialName;
return type.AddMethod(".ctor", methodAttributes, type.Module!.CorLibTypeFactory.Void);
return type.AddMethod(".ctor", methodAttributes, type.DeclaringModule!.CorLibTypeFactory.Void);
}
public static FieldDefinition AddEnumField(this TypeDefinition type, string name, long value)
@@ -67,7 +67,7 @@ public static class TypeDefinitionExtensions
};
}
private static MethodDefinition CreateMethod(string methodName, MethodAttributes methodAttributes, TypeSignature returnType)
private static MethodDefinition CreateMethod(string? methodName, MethodAttributes methodAttributes, TypeSignature returnType)
{
bool isStatic = (methodAttributes & MethodAttributes.Static) != 0;
MethodSignature methodSignature = isStatic
@@ -75,17 +75,30 @@ public static class TypeDefinitionExtensions
: MethodSignature.CreateInstance(returnType);
MethodDefinition result = new MethodDefinition(methodName, methodAttributes, methodSignature);
result.CilMethodBody = new CilMethodBody(result);
result.CilMethodBody = new CilMethodBody();
return result;
}
/// <summary>
/// Gets the default constructor for a <see cref="TypeDefinition"/>. Throws an exception if one doesn't exist.
/// </summary>
public static MethodDefinition GetDefaultConstructor(this TypeDefinition type)
{
return type.Methods.First(m => m.IsInstanceConstructor() && m.Parameters.Count == 0);
}
public static bool IsStaticClass(this TypeDefinition type)
/// <summary>
/// Get all the instance constructors for a type
/// </summary>
/// <param name="type">The definition for the type</param>
/// <returns>All the instance constructors</returns>
public static IEnumerable<MethodDefinition> GetInstanceConstructors(this TypeDefinition type)
{
return type.Methods.Where(m => !m.IsStatic && m.IsConstructor);
}
public static bool IsStatic(this TypeDefinition type)
{
return type.IsSealed && type.IsAbstract;
}
+1 -2
View File
@@ -5,8 +5,7 @@
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>
<Version>1.1.6</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<VersionPrefix>1.3.1</VersionPrefix>
<Copyright>Copyright (c) 2025 ds5678</Copyright>
<Authors>ds5678</Authors>
<Company>AssetRipper</Company>