mirror of
https://github.com/AssetRipper/AssetRipper.CIL.git
synced 2026-11-13 12:41:25 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e871881517 | ||
|
|
c1eceeda51 | ||
|
|
44996e27ff | ||
|
|
0039dc95b3 | ||
|
|
0953640bf1 | ||
|
|
c1e8657d67 | ||
|
|
09db4f64ae | ||
|
|
0af62d2590 | ||
|
|
cc71118bfb | ||
|
|
21d8f70781 | ||
|
|
75a48b3ce0 | ||
|
|
c164acde5f | ||
|
|
8935137a6f | ||
|
|
7e3e7bfa8c | ||
|
|
777fb5ea1c | ||
|
|
be8f11f6b5 | ||
|
|
1338ca2f95 | ||
|
|
e95ff3bae6 |
No files matched your search
@@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\AssetRipper.CIL\AssetRipper.CIL.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,18 @@
|
||||
using AsmResolver.DotNet;
|
||||
|
||||
namespace AssetRipper.CIL.Stubber
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
ModuleDefinition module = ModuleDefinition.FromFile(args[0]);
|
||||
foreach (MethodDefinition method in module.GetAllTypes().SelectMany(t => t.Methods))
|
||||
{
|
||||
method.ReplaceMethodBodyWithMinimalImplementation();
|
||||
}
|
||||
module.Write(args[1]);
|
||||
Console.WriteLine("Done!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageReference Include="NUnit" Version="4.4.0" />
|
||||
<PackageReference Include="NUnit.Analyzers" Version="4.10.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="5.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\AssetRipper.CIL\AssetRipper.CIL.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="NUnit.Framework" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,113 @@
|
||||
using AsmResolver.DotNet;
|
||||
using AsmResolver.DotNet.Code.Cil;
|
||||
using AsmResolver.DotNet.Signatures;
|
||||
using AsmResolver.PE.DotNet.Cil;
|
||||
using AsmResolver.PE.DotNet.Metadata.Tables;
|
||||
|
||||
namespace AssetRipper.CIL.Tests;
|
||||
|
||||
public class LoadIndirectTests
|
||||
{
|
||||
[TestCase(ElementType.I1, CilCode.Ldind_I1)]
|
||||
[TestCase(ElementType.I2, CilCode.Ldind_I2)]
|
||||
[TestCase(ElementType.I4, CilCode.Ldind_I4)]
|
||||
[TestCase(ElementType.I8, CilCode.Ldind_I8)]
|
||||
[TestCase(ElementType.U1, CilCode.Ldind_U1)]
|
||||
[TestCase(ElementType.U2, CilCode.Ldind_U2)]
|
||||
[TestCase(ElementType.U4, CilCode.Ldind_U4)]
|
||||
[TestCase(ElementType.U8, CilCode.Ldind_I8)]
|
||||
[TestCase(ElementType.I, CilCode.Ldind_I)]
|
||||
[TestCase(ElementType.U, CilCode.Ldind_I)]
|
||||
[TestCase(ElementType.R4, CilCode.Ldind_R4)]
|
||||
[TestCase(ElementType.R8, CilCode.Ldind_R8)]
|
||||
[TestCase(ElementType.Boolean, CilCode.Ldind_U1)]
|
||||
[TestCase(ElementType.Char, CilCode.Ldind_U2)]
|
||||
[TestCase(ElementType.Object, CilCode.Ldind_Ref)]
|
||||
[TestCase(ElementType.String, CilCode.Ldind_Ref)]
|
||||
[TestCase(ElementType.TypedByRef, CilCode.Ldobj)]
|
||||
public void CorlibLoadIndirect(ElementType type, CilCode code)
|
||||
{
|
||||
ModuleDefinition module = new ModuleDefinition("TestModule");
|
||||
CorLibTypeSignature? typeToLoad = module.CorLibTypeFactory.FromElementType(type);
|
||||
Assert.That(typeToLoad, Is.Not.Null);
|
||||
AssertCorrectLoadIndirect(typeToLoad, code);
|
||||
}
|
||||
|
||||
[TestCase(ElementType.I1, CilCode.Ldind_I1)]
|
||||
[TestCase(ElementType.I2, CilCode.Ldind_I2)]
|
||||
[TestCase(ElementType.I4, CilCode.Ldind_I4)]
|
||||
[TestCase(ElementType.I8, CilCode.Ldind_I8)]
|
||||
[TestCase(ElementType.U1, CilCode.Ldind_U1)]
|
||||
[TestCase(ElementType.U2, CilCode.Ldind_U2)]
|
||||
[TestCase(ElementType.U4, CilCode.Ldind_U4)]
|
||||
[TestCase(ElementType.U8, CilCode.Ldind_I8)]
|
||||
public void EnumLoadIndirect(ElementType type, CilCode code)
|
||||
{
|
||||
ModuleDefinition module = new ModuleDefinition("TestModule");
|
||||
TypeSignature? typeToLoad = CreateEnum(module, type).ToTypeSignature();
|
||||
Assert.That(typeToLoad, Is.Not.Null);
|
||||
AssertCorrectLoadIndirect(typeToLoad, code);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PointerLoadIndirect()
|
||||
{
|
||||
ModuleDefinition module = new ModuleDefinition("TestModule");
|
||||
PointerTypeSignature typeToLoad = module.CorLibTypeFactory.Void.MakePointerType();
|
||||
AssertCorrectLoadIndirect(typeToLoad, CilCode.Ldind_I);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenericParameterLoadIndirect()
|
||||
{
|
||||
ModuleDefinition module = new ModuleDefinition("TestModule");
|
||||
GenericParameterSignature typeToLoad = new GenericParameterSignature(module, GenericParameterType.Type, 0);
|
||||
AssertCorrectLoadIndirect(typeToLoad, CilCode.Ldobj);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SzArrayLoadIndirect()
|
||||
{
|
||||
ModuleDefinition module = new ModuleDefinition("TestModule");
|
||||
TypeSignature typeToLoad = module.CorLibTypeFactory.Int32.MakeSzArrayType();
|
||||
AssertCorrectLoadIndirect(typeToLoad, CilCode.Ldind_Ref);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ClassLoadIndirect()
|
||||
{
|
||||
ModuleDefinition module = new ModuleDefinition("TestModule");
|
||||
TypeDefinition type = new TypeDefinition("TestNamespace", "TestName", TypeAttributes.Public, module.CorLibTypeFactory.Object.ToTypeDefOrRef());
|
||||
module.TopLevelTypes.Add(type);
|
||||
AssertCorrectLoadIndirect(type.ToTypeSignature(), CilCode.Ldind_Ref);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StructLoadIndirect()
|
||||
{
|
||||
ModuleDefinition module = new ModuleDefinition("TestModule");
|
||||
TypeDefinition type = new TypeDefinition("TestNamespace", "TestName", TypeAttributes.Public | TypeAttributes.SequentialLayout | TypeAttributes.Sealed, module.DefaultImporter.ImportType(typeof(ValueType)));
|
||||
module.TopLevelTypes.Add(type);
|
||||
AssertCorrectLoadIndirect(type.ToTypeSignature(), CilCode.Ldobj);
|
||||
}
|
||||
|
||||
private static void AssertCorrectLoadIndirect(TypeSignature typeToLoad, CilCode expectedOpCode)
|
||||
{
|
||||
MethodDefinition method = new MethodDefinition("Test", MethodAttributes.Public | MethodAttributes.Static, null);
|
||||
method.CilMethodBody = new();
|
||||
CilInstructionCollection instructions = method.CilMethodBody.Instructions;
|
||||
instructions.AddLoadIndirect(typeToLoad);
|
||||
Assert.That(instructions, Has.Count.EqualTo(1));
|
||||
Assert.That(instructions[0].OpCode.Code, Is.EqualTo(expectedOpCode));
|
||||
}
|
||||
|
||||
private static TypeDefinition CreateEnum(ModuleDefinition module, ElementType underlyingType)
|
||||
{
|
||||
TypeDefinition enumType = new TypeDefinition("TestNamespace", "TestEnum", TypeAttributes.Public, module.DefaultImporter.ImportType(typeof(Enum)));
|
||||
module.TopLevelTypes.Add(enumType);
|
||||
CorLibTypeSignature underlyingTypeSignature = module.CorLibTypeFactory.FromElementType(underlyingType) ?? throw new NullReferenceException();
|
||||
FieldDefinition valueField = new FieldDefinition("value__", FieldAttributes.Public | FieldAttributes.SpecialName | FieldAttributes.RuntimeSpecialName, underlyingTypeSignature);
|
||||
enumType.Fields.Add(valueField);
|
||||
return enumType;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
using AsmResolver.DotNet;
|
||||
using AsmResolver.DotNet.Code.Cil;
|
||||
using AsmResolver.DotNet.Signatures;
|
||||
using AsmResolver.PE.DotNet.Cil;
|
||||
using AsmResolver.PE.DotNet.Metadata.Tables;
|
||||
|
||||
namespace AssetRipper.CIL.Tests;
|
||||
|
||||
public class StoreIndirectTests
|
||||
{
|
||||
[TestCase(ElementType.I1, CilCode.Stind_I1)]
|
||||
[TestCase(ElementType.I2, CilCode.Stind_I2)]
|
||||
[TestCase(ElementType.I4, CilCode.Stind_I4)]
|
||||
[TestCase(ElementType.I8, CilCode.Stind_I8)]
|
||||
[TestCase(ElementType.U1, CilCode.Stind_I1)]
|
||||
[TestCase(ElementType.U2, CilCode.Stind_I2)]
|
||||
[TestCase(ElementType.U4, CilCode.Stind_I4)]
|
||||
[TestCase(ElementType.U8, CilCode.Stind_I8)]
|
||||
[TestCase(ElementType.I, CilCode.Stind_I)]
|
||||
[TestCase(ElementType.U, CilCode.Stind_I)]
|
||||
[TestCase(ElementType.R4, CilCode.Stind_R4)]
|
||||
[TestCase(ElementType.R8, CilCode.Stind_R8)]
|
||||
[TestCase(ElementType.Boolean, CilCode.Stind_I1)]
|
||||
[TestCase(ElementType.Char, CilCode.Stind_I2)]
|
||||
[TestCase(ElementType.Object, CilCode.Stind_Ref)]
|
||||
[TestCase(ElementType.String, CilCode.Stind_Ref)]
|
||||
[TestCase(ElementType.TypedByRef, CilCode.Stobj)]
|
||||
public void CorlibStoreIndirect(ElementType type, CilCode code)
|
||||
{
|
||||
ModuleDefinition module = new ModuleDefinition("TestModule");
|
||||
CorLibTypeSignature? typeToStore = module.CorLibTypeFactory.FromElementType(type);
|
||||
Assert.That(typeToStore, Is.Not.Null);
|
||||
AssertCorrectStoreIndirect(typeToStore, code);
|
||||
}
|
||||
|
||||
[TestCase(ElementType.I1, CilCode.Stind_I1)]
|
||||
[TestCase(ElementType.I2, CilCode.Stind_I2)]
|
||||
[TestCase(ElementType.I4, CilCode.Stind_I4)]
|
||||
[TestCase(ElementType.I8, CilCode.Stind_I8)]
|
||||
[TestCase(ElementType.U1, CilCode.Stind_I1)]
|
||||
[TestCase(ElementType.U2, CilCode.Stind_I2)]
|
||||
[TestCase(ElementType.U4, CilCode.Stind_I4)]
|
||||
[TestCase(ElementType.U8, CilCode.Stind_I8)]
|
||||
public void EnumStoreIndirect(ElementType type, CilCode code)
|
||||
{
|
||||
ModuleDefinition module = new ModuleDefinition("TestModule");
|
||||
TypeSignature? typeToStore = CreateEnum(module, type).ToTypeSignature();
|
||||
Assert.That(typeToStore, Is.Not.Null);
|
||||
AssertCorrectStoreIndirect(typeToStore, code);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PointerStoreIndirect()
|
||||
{
|
||||
ModuleDefinition module = new ModuleDefinition("TestModule");
|
||||
PointerTypeSignature typeToStore = module.CorLibTypeFactory.Void.MakePointerType();
|
||||
AssertCorrectStoreIndirect(typeToStore, CilCode.Stind_I);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenericParameterStoreIndirect()
|
||||
{
|
||||
ModuleDefinition module = new ModuleDefinition("TestModule");
|
||||
GenericParameterSignature typeToStore = new GenericParameterSignature(module, GenericParameterType.Type, 0);
|
||||
AssertCorrectStoreIndirect(typeToStore, CilCode.Stobj);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SzArrayStoreIndirect()
|
||||
{
|
||||
ModuleDefinition module = new ModuleDefinition("TestModule");
|
||||
TypeSignature typeToStore = module.CorLibTypeFactory.Int32.MakeSzArrayType();
|
||||
AssertCorrectStoreIndirect(typeToStore, CilCode.Stind_Ref);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ClassStoreIndirect()
|
||||
{
|
||||
ModuleDefinition module = new ModuleDefinition("TestModule");
|
||||
TypeDefinition type = new TypeDefinition("TestNamespace", "TestName", TypeAttributes.Public, module.CorLibTypeFactory.Object.ToTypeDefOrRef());
|
||||
module.TopLevelTypes.Add(type);
|
||||
AssertCorrectStoreIndirect(type.ToTypeSignature(), CilCode.Stind_Ref);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StructStoreIndirect()
|
||||
{
|
||||
ModuleDefinition module = new ModuleDefinition("TestModule");
|
||||
TypeDefinition type = new TypeDefinition("TestNamespace", "TestName", TypeAttributes.Public | TypeAttributes.SequentialLayout | TypeAttributes.Sealed, module.DefaultImporter.ImportType(typeof(ValueType)));
|
||||
module.TopLevelTypes.Add(type);
|
||||
AssertCorrectStoreIndirect(type.ToTypeSignature(), CilCode.Stobj);
|
||||
}
|
||||
|
||||
private static void AssertCorrectStoreIndirect(TypeSignature typeToStore, CilCode expectedOpCode)
|
||||
{
|
||||
MethodDefinition method = new MethodDefinition("Test", MethodAttributes.Public | MethodAttributes.Static, null);
|
||||
method.CilMethodBody = new();
|
||||
CilInstructionCollection instructions = method.CilMethodBody.Instructions;
|
||||
instructions.AddStoreIndirect(typeToStore);
|
||||
Assert.That(instructions, Has.Count.EqualTo(1));
|
||||
Assert.That(instructions[0].OpCode.Code, Is.EqualTo(expectedOpCode));
|
||||
}
|
||||
|
||||
private static TypeDefinition CreateEnum(ModuleDefinition module, ElementType underlyingType)
|
||||
{
|
||||
TypeDefinition enumType = new TypeDefinition("TestNamespace", "TestEnum", TypeAttributes.Public, module.DefaultImporter.ImportType(typeof(Enum)));
|
||||
module.TopLevelTypes.Add(enumType);
|
||||
CorLibTypeSignature underlyingTypeSignature = module.CorLibTypeFactory.FromElementType(underlyingType) ?? throw new NullReferenceException();
|
||||
FieldDefinition valueField = new FieldDefinition("value__", FieldAttributes.Public | FieldAttributes.SpecialName | FieldAttributes.RuntimeSpecialName, underlyingTypeSignature);
|
||||
enumType.Fields.Add(valueField);
|
||||
return enumType;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
@@ -18,6 +18,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AssetRipper.CIL.Validator",
|
||||
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
|
||||
@@ -36,6 +40,14 @@ Global
|
||||
{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
|
||||
|
||||
@@ -15,8 +15,9 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AsmResolver.DotNet" Version="6.0.0-beta.1" />
|
||||
<PackageReference Include="AsmResolver.DotNet" Version="6.0.0-beta.4" />
|
||||
<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.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -22,7 +22,7 @@ public static class CilInstructionCollectionExtensions
|
||||
{
|
||||
instructions.AddDefaultPrimitiveValue(corLibTypeSignature);
|
||||
}
|
||||
else if (type is ByReferenceTypeSignature)
|
||||
else if (type is ByReferenceTypeSignature or PointerTypeSignature)
|
||||
{
|
||||
instructions.AddNullRef();
|
||||
}
|
||||
@@ -43,7 +43,7 @@ public static class CilInstructionCollectionExtensions
|
||||
{
|
||||
instructions.InitializeDefaultPrimitiveValue(corLibTypeSignature, localVariable);
|
||||
}
|
||||
else if (type is ByReferenceTypeSignature)
|
||||
else if (type is ByReferenceTypeSignature or PointerTypeSignature)
|
||||
{
|
||||
instructions.AddNullRef();
|
||||
instructions.Add(CilOpCodes.Stloc, localVariable);
|
||||
@@ -129,17 +129,36 @@ public static class CilInstructionCollectionExtensions
|
||||
ElementType.I2 => instructions.Add(CilOpCodes.Ldind_I2),
|
||||
ElementType.I4 => instructions.Add(CilOpCodes.Ldind_I4),
|
||||
ElementType.I8 or ElementType.U8 => instructions.Add(CilOpCodes.Ldind_I8),
|
||||
ElementType.U1 => instructions.Add(CilOpCodes.Ldind_U1),
|
||||
ElementType.U2 => instructions.Add(CilOpCodes.Ldind_U2),
|
||||
ElementType.U1 or ElementType.Boolean => instructions.Add(CilOpCodes.Ldind_U1),
|
||||
ElementType.U2 or ElementType.Char => instructions.Add(CilOpCodes.Ldind_U2),
|
||||
ElementType.U4 => instructions.Add(CilOpCodes.Ldind_U4),
|
||||
ElementType.R4 => instructions.Add(CilOpCodes.Ldind_R4),
|
||||
ElementType.R8 => instructions.Add(CilOpCodes.Ldind_R8),
|
||||
ElementType.I or ElementType.U => instructions.Add(CilOpCodes.Ldind_I),
|
||||
ElementType.Object or ElementType.String => instructions.Add(CilOpCodes.Ldind_Ref),
|
||||
_ => instructions.Add(CilOpCodes.Ldobj, type.ToTypeDefOrRef()),
|
||||
},
|
||||
PointerTypeSignature => instructions.Add(CilOpCodes.Ldind_I),
|
||||
_ => instructions.Add(CilOpCodes.Ldobj, type.ToTypeDefOrRef()),
|
||||
GenericParameterSignature => instructions.Add(CilOpCodes.Ldobj, type.ToTypeDefOrRef()),
|
||||
TypeDefOrRefSignature t => AddTypeDefOrRef(instructions, t),
|
||||
_ => AddUnknown(instructions, type),
|
||||
};
|
||||
|
||||
static CilInstruction AddUnknown(CilInstructionCollection instructions, TypeSignature type)
|
||||
{
|
||||
return type.IsValueType
|
||||
? 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)
|
||||
@@ -152,17 +171,36 @@ public static class CilInstructionCollectionExtensions
|
||||
ElementType.I2 => instructions.Add(CilOpCodes.Stind_I2),
|
||||
ElementType.I4 => instructions.Add(CilOpCodes.Stind_I4),
|
||||
ElementType.I8 or ElementType.U8 => instructions.Add(CilOpCodes.Stind_I8),
|
||||
ElementType.U1 => instructions.Add(CilOpCodes.Stind_I1),
|
||||
ElementType.U2 => instructions.Add(CilOpCodes.Stind_I2),
|
||||
ElementType.U1 or ElementType.Boolean => instructions.Add(CilOpCodes.Stind_I1),
|
||||
ElementType.U2 or ElementType.Char => instructions.Add(CilOpCodes.Stind_I2),
|
||||
ElementType.U4 => instructions.Add(CilOpCodes.Stind_I4),
|
||||
ElementType.R4 => instructions.Add(CilOpCodes.Stind_R4),
|
||||
ElementType.R8 => instructions.Add(CilOpCodes.Stind_R8),
|
||||
ElementType.I or ElementType.U => instructions.Add(CilOpCodes.Stind_I),
|
||||
ElementType.Object or ElementType.String => instructions.Add(CilOpCodes.Stind_Ref),
|
||||
_ => instructions.Add(CilOpCodes.Stobj, type.ToTypeDefOrRef()),
|
||||
},
|
||||
PointerTypeSignature => instructions.Add(CilOpCodes.Stind_I),
|
||||
_ => instructions.Add(CilOpCodes.Stobj, type.ToTypeDefOrRef()),
|
||||
GenericParameterSignature => instructions.Add(CilOpCodes.Stobj, type.ToTypeDefOrRef()),
|
||||
TypeDefOrRefSignature t => AddTypeDefOrRef(instructions, t),
|
||||
_ => AddUnknown(instructions, type),
|
||||
};
|
||||
|
||||
static CilInstruction AddUnknown(CilInstructionCollection instructions, TypeSignature type)
|
||||
{
|
||||
return type.IsValueType
|
||||
? 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>
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
using AsmResolver.DotNet.Signatures;
|
||||
|
||||
namespace AssetRipper.CIL;
|
||||
|
||||
internal static class CorLibTypeFactoryExtensions
|
||||
{
|
||||
public static CorLibTypeSignature? FromType(this CorLibTypeFactory factory, Type type)
|
||||
{
|
||||
if (type == typeof(bool))
|
||||
{
|
||||
return factory.Boolean;
|
||||
}
|
||||
if (type == typeof(char))
|
||||
{
|
||||
return factory.Char;
|
||||
}
|
||||
if (type == typeof(byte))
|
||||
{
|
||||
return factory.Byte;
|
||||
}
|
||||
if (type == typeof(sbyte))
|
||||
{
|
||||
return factory.SByte;
|
||||
}
|
||||
if (type == typeof(short))
|
||||
{
|
||||
return factory.Int16;
|
||||
}
|
||||
if (type == typeof(ushort))
|
||||
{
|
||||
return factory.UInt16;
|
||||
}
|
||||
if (type == typeof(int))
|
||||
{
|
||||
return factory.Int32;
|
||||
}
|
||||
if (type == typeof(uint))
|
||||
{
|
||||
return factory.UInt32;
|
||||
}
|
||||
if (type == typeof(long))
|
||||
{
|
||||
return factory.Int64;
|
||||
}
|
||||
if (type == typeof(ulong))
|
||||
{
|
||||
return factory.UInt64;
|
||||
}
|
||||
if (type == typeof(float))
|
||||
{
|
||||
return factory.Single;
|
||||
}
|
||||
if (type == typeof(double))
|
||||
{
|
||||
return factory.Double;
|
||||
}
|
||||
if (type == typeof(string))
|
||||
{
|
||||
return factory.String;
|
||||
}
|
||||
if (type == typeof(IntPtr))
|
||||
{
|
||||
return factory.IntPtr;
|
||||
}
|
||||
if (type == typeof(UIntPtr))
|
||||
{
|
||||
return factory.UIntPtr;
|
||||
}
|
||||
if (type == typeof(object))
|
||||
{
|
||||
return factory.Object;
|
||||
}
|
||||
if (type == typeof(void))
|
||||
{
|
||||
return factory.Void;
|
||||
}
|
||||
if (type == typeof(TypedReference))
|
||||
{
|
||||
return factory.TypedReference;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using AsmResolver.DotNet;
|
||||
using AsmResolver.DotNet.Signatures;
|
||||
|
||||
namespace AssetRipper.CIL;
|
||||
|
||||
public static class CustomAttributeExtensions
|
||||
{
|
||||
public static CustomAttributeArgument AddFixedArgument(this CustomAttribute attribute, TypeSignature paramType, object paramValue)
|
||||
{
|
||||
attribute.Signature ??= new();
|
||||
CustomAttributeArgument argument = new(paramType, paramValue);
|
||||
attribute.Signature.FixedArguments.Add(argument);
|
||||
return argument;
|
||||
}
|
||||
}
|
||||
@@ -57,4 +57,9 @@ public static class FieldDefinitionExtensions
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsFixedBuffer(this FieldDefinition field)
|
||||
{
|
||||
return field.HasCustomAttribute("System.Runtime.CompilerServices", "FixedBufferAttribute");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
using AsmResolver.DotNet;
|
||||
using AsmResolver.DotNet.Signatures;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace AssetRipper.CIL;
|
||||
|
||||
public static class IHasCustomAttributeExtensions
|
||||
{
|
||||
public static CustomAttribute AddCustomAttribute(this IHasCustomAttribute _this, IMethodDefOrRef constructor)
|
||||
{
|
||||
if (constructor is ICustomAttributeType usableConstructor)
|
||||
{
|
||||
CustomAttribute attribute = new(usableConstructor);
|
||||
_this.CustomAttributes.Add(attribute);
|
||||
return attribute;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Constructor is not ICustomAttributeType", nameof(constructor));
|
||||
}
|
||||
}
|
||||
|
||||
public static CustomAttribute AddCustomAttribute(this IHasCustomAttribute _this, IMethodDefOrRef constructor, TypeSignature paramType, object paramValue)
|
||||
{
|
||||
CustomAttribute attribute = _this.AddCustomAttribute(constructor);
|
||||
attribute.AddFixedArgument(paramType, paramValue);
|
||||
return attribute;
|
||||
}
|
||||
|
||||
internal static ModuleDefinition GetModule(this IHasCustomAttribute _this)
|
||||
{
|
||||
return (_this as IModuleProvider)?.ContextModule ?? throw new ArgumentException("Entity does not have a module", nameof(_this));
|
||||
}
|
||||
|
||||
public static CustomAttribute AddFlagsAttribute(this IHasCustomAttribute _this)
|
||||
{
|
||||
ModuleDefinition module = _this.GetModule();
|
||||
|
||||
if (module.TryGetTopLevelType("System", nameof(FlagsAttribute), out TypeDefinition? flagsType))
|
||||
{
|
||||
return _this.AddCustomAttribute(flagsType.GetDefaultConstructor());
|
||||
}
|
||||
else
|
||||
{
|
||||
TypeReference flagsTypeRef = new(module.CorLibTypeFactory.CorLibScope, "System", nameof(FlagsAttribute));
|
||||
MemberReference constructor = new(flagsTypeRef, ".ctor", MethodSignature.CreateInstance(module.CorLibTypeFactory.Void));
|
||||
return _this.AddCustomAttribute(module.DefaultImporter.ImportMethod(constructor));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies the <see cref="CompilerGeneratedAttribute"/>
|
||||
/// </summary>
|
||||
/// <param name="_this">The entity on which to apply the attribute</param>
|
||||
/// <returns>The resulting custom attribute</returns>
|
||||
public static CustomAttribute AddCompilerGeneratedAttribute(this IHasCustomAttribute _this)
|
||||
{
|
||||
ModuleDefinition module = _this.GetModule();
|
||||
|
||||
if (module.TryGetTopLevelType("System.Runtime.CompilerServices", nameof(CompilerGeneratedAttribute), out TypeDefinition? compilerGeneratedType))
|
||||
{
|
||||
return _this.AddCustomAttribute(compilerGeneratedType.GetDefaultConstructor());
|
||||
}
|
||||
else
|
||||
{
|
||||
AssemblyReference compilerServicesAssembly = GetOrAddReferenceToSystemRuntimeCompilerServices(module);
|
||||
|
||||
TypeReference compilerGeneratedTypeRef = new(compilerServicesAssembly, "System.Runtime.CompilerServices", nameof(CompilerGeneratedAttribute));
|
||||
|
||||
MemberReference constructor = new(compilerGeneratedTypeRef, ".ctor", MethodSignature.CreateInstance(module.CorLibTypeFactory.Void));
|
||||
|
||||
return _this.AddCustomAttribute(module.DefaultImporter.ImportMethod(constructor));
|
||||
}
|
||||
|
||||
static AssemblyReference GetOrAddReferenceToSystemRuntimeCompilerServices(ModuleDefinition module)
|
||||
{
|
||||
AssemblyReference? compilerServicesAssembly = module.AssemblyReferences.FirstOrDefault(a => a.Name == "System.Runtime.CompilerServices");
|
||||
if (compilerServicesAssembly is null)
|
||||
{
|
||||
Version version = module.CorLibTypeFactory.CorLibScope.GetAssembly()?.Version ?? new Version(0, 0, 0, 0);
|
||||
compilerServicesAssembly = new("System.Runtime.CompilerServices", version);
|
||||
module.AssemblyReferences.Add(compilerServicesAssembly);
|
||||
}
|
||||
return compilerServicesAssembly;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
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 System.Diagnostics.CodeAnalysis;
|
||||
|
||||
@@ -23,6 +25,25 @@ 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.Add(CilOpCodes.Ldnull);
|
||||
methodInstructions.Add(CilOpCodes.Throw);
|
||||
}
|
||||
|
||||
public static bool IsInstance(this MethodDefinition methodDefinition)
|
||||
{
|
||||
return !methodDefinition.IsStatic;
|
||||
|
||||
@@ -16,19 +16,19 @@ internal static class MethodStubber
|
||||
return;
|
||||
}
|
||||
|
||||
methodDefinition.CilMethodBody = new(methodDefinition);
|
||||
methodDefinition.CilMethodBody = new();
|
||||
CilInstructionCollection methodInstructions = methodDefinition.CilMethodBody.Instructions;
|
||||
|
||||
if (methodDefinition.IsAutoPropertyGetMethod(out FieldDefinition? backingField))
|
||||
{
|
||||
if (methodDefinition.IsStatic)
|
||||
{
|
||||
methodInstructions.Add(CilOpCodes.Ldsfld, backingField);
|
||||
methodInstructions.Add(CilOpCodes.Ldsfld, backingField.MakeGenericIfNecessary());
|
||||
}
|
||||
else
|
||||
{
|
||||
methodInstructions.Add(CilOpCodes.Ldarg_0);
|
||||
methodInstructions.Add(CilOpCodes.Ldfld, backingField);
|
||||
methodInstructions.Add(CilOpCodes.Ldfld, backingField.MakeGenericIfNecessary());
|
||||
}
|
||||
methodInstructions.Add(CilOpCodes.Ret);
|
||||
return;
|
||||
@@ -38,13 +38,13 @@ internal static class MethodStubber
|
||||
if (methodDefinition.IsStatic)
|
||||
{
|
||||
methodInstructions.Add(CilOpCodes.Ldarg_0);
|
||||
methodInstructions.Add(CilOpCodes.Stsfld, backingField);
|
||||
methodInstructions.Add(CilOpCodes.Stsfld, backingField.MakeGenericIfNecessary());
|
||||
}
|
||||
else
|
||||
{
|
||||
methodInstructions.Add(CilOpCodes.Ldarg_0);
|
||||
methodInstructions.Add(CilOpCodes.Ldarg_1);
|
||||
methodInstructions.Add(CilOpCodes.Stfld, backingField);
|
||||
methodInstructions.Add(CilOpCodes.Stfld, backingField.MakeGenericIfNecessary());
|
||||
}
|
||||
methodInstructions.Add(CilOpCodes.Ret);
|
||||
return;
|
||||
@@ -55,14 +55,14 @@ internal static class MethodStubber
|
||||
TypeDefinition declaringType = methodDefinition.DeclaringType!;
|
||||
foreach (FieldDefinition field in declaringType.Fields)
|
||||
{
|
||||
if (field.IsStatic || field.Signature is null)
|
||||
if (field.IsStatic || field.Signature is null || field.IsFixedBuffer())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
methodInstructions.Add(CilOpCodes.Ldarg_0);
|
||||
methodInstructions.AddDefaultValue(field.Signature.FieldType);
|
||||
methodInstructions.Add(CilOpCodes.Stfld, field);
|
||||
methodInstructions.Add(CilOpCodes.Stfld, field.MakeGenericIfNecessary());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,13 +131,13 @@ internal static class MethodStubber
|
||||
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)
|
||||
@@ -153,11 +153,11 @@ internal static class MethodStubber
|
||||
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 +165,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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,4 +174,18 @@ internal static class MethodStubber
|
||||
baseConstructor = TryGetBaseConstructor(methodDefinition);
|
||||
return baseConstructor is not null;
|
||||
}
|
||||
|
||||
private static IFieldDescriptor MakeGenericIfNecessary(this FieldDefinition field)
|
||||
{
|
||||
if (field.DeclaringType is null or { GenericParameters.Count: 0 })
|
||||
{
|
||||
return field;
|
||||
}
|
||||
|
||||
TypeSignature[] typeArguments = Enumerable.Range(0, field.DeclaringType.GenericParameters.Count)
|
||||
.Select(i => new GenericParameterSignature(field.DeclaringModule, GenericParameterType.Type, i))
|
||||
.ToArray();
|
||||
|
||||
return new MemberReference(field.DeclaringType.MakeGenericInstanceType(typeArguments).ToTypeDefOrRef(), field.Name, field.Signature);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using AsmResolver.DotNet;
|
||||
using AsmResolver.DotNet.Signatures;
|
||||
using AsmResolver.PE.DotNet.Metadata.Tables;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace AssetRipper.CIL;
|
||||
|
||||
public static class ModuleDefinitionExtensions
|
||||
{
|
||||
public static bool TryGetTopLevelType(this ModuleDefinition module, string? @namespace, string? name, [NotNullWhen(true)] out TypeDefinition? type)
|
||||
{
|
||||
type = module.TopLevelTypes.FirstOrDefault(t => t.Namespace == @namespace && t.Name == name);
|
||||
return type != null;
|
||||
}
|
||||
|
||||
public static bool HasTopLevelType(this ModuleDefinition module, string? @namespace, string? name)
|
||||
{
|
||||
return module.TryGetTopLevelType(@namespace, name, out _);
|
||||
}
|
||||
|
||||
public static TypeDefinition AddEmptyEnum(this ModuleDefinition module, string? @namespace, string? name, Visibility visibility, ElementType underlyingType)
|
||||
{
|
||||
CorLibTypeSignature underlyingTypeSignature = module.CorLibTypeFactory.FromElementType(underlyingType)
|
||||
?? throw new ArgumentOutOfRangeException(nameof(underlyingType), underlyingType, null);
|
||||
|
||||
TypeReference baseType = new(module.CorLibTypeFactory.CorLibScope, "System", "Enum");
|
||||
TypeDefinition type = new(@namespace, name, visibility.ToTypeAttributes() | TypeAttributes.Sealed, module.DefaultImporter.ImportType(baseType));
|
||||
module.TopLevelTypes.Add(type);
|
||||
AddEnumValue(type, underlyingTypeSignature);
|
||||
return type;
|
||||
|
||||
static void AddEnumValue(TypeDefinition type, TypeSignature underlyingType)
|
||||
{
|
||||
FieldSignature fieldSignature = new FieldSignature(underlyingType);
|
||||
FieldDefinition fieldDef = new FieldDefinition("value__", FieldAttributes.Public | FieldAttributes.SpecialName | FieldAttributes.RuntimeSpecialName, fieldSignature);
|
||||
type.Fields.Add(fieldDef);
|
||||
}
|
||||
}
|
||||
|
||||
public static TypeDefinition AddExistingEnum<T>(this ModuleDefinition module) where T : struct, Enum
|
||||
{
|
||||
Type underlyingType = Enum.GetUnderlyingType(typeof(T));
|
||||
bool isSigned = underlyingType == typeof(sbyte) || underlyingType == typeof(short) || underlyingType == typeof(int) || underlyingType == typeof(long);
|
||||
CorLibTypeSignature underlyingTypeSignature = module.CorLibTypeFactory.FromType(underlyingType)
|
||||
?? throw new InvalidOperationException($"Failed to get underlying type for {typeof(T)}");
|
||||
|
||||
TypeDefinition type = module.AddEmptyEnum(typeof(T).Namespace, typeof(T).Name, VisibilityExtensions.GetVisibility(typeof(T)), underlyingTypeSignature.ElementType);
|
||||
foreach (T value in GetValues())
|
||||
{
|
||||
type.AddEnumField(value.ToString(), ToInt64(value));
|
||||
}
|
||||
|
||||
if (typeof(T).CustomAttributes.Any(a => a.AttributeType is { Namespace: "System", Name: nameof(FlagsAttribute) }))
|
||||
{
|
||||
type.AddFlagsAttribute();
|
||||
}
|
||||
|
||||
return type;
|
||||
|
||||
static IEnumerable<T> GetValues()
|
||||
{
|
||||
#if NETSTANDARD
|
||||
return Enum.GetValues(typeof(T)).Cast<T>();
|
||||
#else
|
||||
return Enum.GetValues<T>();
|
||||
#endif
|
||||
}
|
||||
|
||||
long ToInt64(T value)
|
||||
{
|
||||
if (Unsafe.SizeOf<T>() == sizeof(long))
|
||||
{
|
||||
return Unsafe.As<T, long>(ref value);
|
||||
}
|
||||
else if (Unsafe.SizeOf<T>() == sizeof(int))
|
||||
{
|
||||
return isSigned ? Unsafe.As<T, int>(ref value) : Unsafe.As<T, uint>(ref value);
|
||||
}
|
||||
else if (Unsafe.SizeOf<T>() == sizeof(short))
|
||||
{
|
||||
return isSigned ? Unsafe.As<T, short>(ref value) : Unsafe.As<T, ushort>(ref value);
|
||||
}
|
||||
else if (Unsafe.SizeOf<T>() == sizeof(byte))
|
||||
{
|
||||
return isSigned ? Unsafe.As<T, sbyte>(ref value) : Unsafe.As<T, byte>(ref value);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException($"Unsupported enum underlying type {typeof(T)}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,33 @@ 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)
|
||||
{
|
||||
FieldAttributes fieldAttributes = visibility.ToFieldAttributes() | (isStatic ? FieldAttributes.Static : default);
|
||||
FieldDefinition field = new FieldDefinition(fieldName, fieldAttributes, fieldType);
|
||||
type.Fields.Add(field);
|
||||
return field;
|
||||
}
|
||||
|
||||
public static FieldDefinition AddConstantField(
|
||||
this TypeDefinition type,
|
||||
TypeSignature fieldType,
|
||||
string fieldName,
|
||||
Constant constant,
|
||||
Visibility visibility = Visibility.Public)
|
||||
{
|
||||
FieldDefinition field = new FieldDefinition(
|
||||
fieldName,
|
||||
FieldAttributes.Static |
|
||||
FieldAttributes.Literal |
|
||||
FieldAttributes.HasDefault |
|
||||
visibility.ToFieldAttributes(),
|
||||
new FieldSignature(fieldType));
|
||||
field.Constant = constant;
|
||||
type.Fields.Add(field);
|
||||
return field;
|
||||
}
|
||||
|
||||
public static MethodDefinition AddMethod(this TypeDefinition type, string methodName, MethodAttributes methodAttributes, TypeSignature returnType)
|
||||
{
|
||||
MethodDefinition method = CreateMethod(methodName, methodAttributes, returnType);
|
||||
@@ -14,6 +41,32 @@ public static class TypeDefinitionExtensions
|
||||
return method;
|
||||
}
|
||||
|
||||
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.DeclaringModule!.CorLibTypeFactory.Void);
|
||||
}
|
||||
|
||||
public static FieldDefinition AddEnumField(this TypeDefinition type, string name, long value)
|
||||
{
|
||||
TypeSignature underlyingType = type.GetEnumUnderlyingType() ?? throw new ArgumentException("Type is not an enum", nameof(type));
|
||||
|
||||
return type.AddConstantField(type.ToTypeSignature(), name, FromValue(value, underlyingType.ElementType));
|
||||
|
||||
static Constant FromValue(long value, ElementType elementType) => elementType switch
|
||||
{
|
||||
ElementType.I1 => Constant.FromValue((sbyte)value),
|
||||
ElementType.U1 => Constant.FromValue((byte)value),
|
||||
ElementType.I2 => Constant.FromValue((short)value),
|
||||
ElementType.U2 => Constant.FromValue((ushort)value),
|
||||
ElementType.I4 => Constant.FromValue((int)value),
|
||||
ElementType.U4 => Constant.FromValue((uint)value),
|
||||
ElementType.I8 => Constant.FromValue(value),
|
||||
ElementType.U8 => Constant.FromValue(unchecked((ulong)value)),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(elementType)),
|
||||
};
|
||||
}
|
||||
|
||||
private static MethodDefinition CreateMethod(string methodName, MethodAttributes methodAttributes, TypeSignature returnType)
|
||||
{
|
||||
bool isStatic = (methodAttributes & MethodAttributes.Static) != 0;
|
||||
@@ -22,11 +75,16 @@ 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;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
return type.IsSealed && type.IsAbstract;
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
using AsmResolver.PE.DotNet.Metadata.Tables;
|
||||
|
||||
namespace AssetRipper.CIL;
|
||||
|
||||
public enum Visibility
|
||||
{
|
||||
Public,
|
||||
Protected,
|
||||
Internal,
|
||||
ProtectedInternal,
|
||||
Private,
|
||||
PrivateProtected
|
||||
}
|
||||
public static class VisibilityExtensions
|
||||
{
|
||||
public static MethodAttributes ToMethodAttributes(this Visibility visibility) => visibility switch
|
||||
{
|
||||
Visibility.Public => MethodAttributes.Public,
|
||||
Visibility.Protected => MethodAttributes.Family,
|
||||
Visibility.Internal => MethodAttributes.Assembly,
|
||||
Visibility.ProtectedInternal => MethodAttributes.FamilyOrAssembly,
|
||||
Visibility.Private => MethodAttributes.Private,
|
||||
Visibility.PrivateProtected => MethodAttributes.FamilyAndAssembly,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(visibility), visibility, null),
|
||||
};
|
||||
|
||||
public static FieldAttributes ToFieldAttributes(this Visibility visibility) => visibility switch
|
||||
{
|
||||
Visibility.Public => FieldAttributes.Public,
|
||||
Visibility.Protected => FieldAttributes.Family,
|
||||
Visibility.Internal => FieldAttributes.Assembly,
|
||||
Visibility.ProtectedInternal => FieldAttributes.FamilyOrAssembly,
|
||||
Visibility.Private => FieldAttributes.Private,
|
||||
Visibility.PrivateProtected => FieldAttributes.FamilyAndAssembly,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(visibility), visibility, null),
|
||||
};
|
||||
|
||||
public static TypeAttributes ToTypeAttributes(this Visibility visibility, bool nested = false) => nested
|
||||
? visibility switch
|
||||
{
|
||||
Visibility.Public => TypeAttributes.NestedPublic,
|
||||
Visibility.Protected => TypeAttributes.NestedFamily,
|
||||
Visibility.Internal => TypeAttributes.NestedAssembly,
|
||||
Visibility.ProtectedInternal => TypeAttributes.NestedFamilyOrAssembly,
|
||||
Visibility.Private => TypeAttributes.NestedPrivate,
|
||||
Visibility.PrivateProtected => TypeAttributes.NestedFamilyAndAssembly,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(visibility), visibility, null),
|
||||
}
|
||||
: visibility switch
|
||||
{
|
||||
Visibility.Public => TypeAttributes.Public,
|
||||
Visibility.Internal => TypeAttributes.NotPublic,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(visibility), visibility, null),
|
||||
};
|
||||
|
||||
internal static Visibility GetVisibility(Type type)
|
||||
{
|
||||
if (type.IsPublic || type.IsNestedPublic)
|
||||
{
|
||||
return Visibility.Public;
|
||||
}
|
||||
if (type.IsNestedFamily)
|
||||
{
|
||||
return Visibility.Protected;
|
||||
}
|
||||
if (type.IsNotPublic || type.IsNestedAssembly)
|
||||
{
|
||||
return Visibility.Internal;
|
||||
}
|
||||
if (type.IsNestedFamORAssem)
|
||||
{
|
||||
return Visibility.ProtectedInternal;
|
||||
}
|
||||
if (type.IsNestedPrivate)
|
||||
{
|
||||
return Visibility.Private;
|
||||
}
|
||||
if (type.IsNestedFamANDAssem)
|
||||
{
|
||||
return Visibility.PrivateProtected;
|
||||
}
|
||||
throw new ArgumentException($"Unknown visibility for type {type}", nameof(type));
|
||||
}
|
||||
}
|
||||
@@ -2,13 +2,12 @@
|
||||
<PropertyGroup>
|
||||
<LangVersion>Latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsAotCompatible>true</IsAotCompatible>
|
||||
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
|
||||
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>
|
||||
|
||||
<Version>1.1.3</Version>
|
||||
<Version>1.2.0</Version>
|
||||
<AssemblyVersion>$(Version)</AssemblyVersion>
|
||||
<Copyright>Copyright (c) 2024 ds5678</Copyright>
|
||||
<Copyright>Copyright (c) 2025 ds5678</Copyright>
|
||||
<Authors>ds5678</Authors>
|
||||
<Company>AssetRipper</Company>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
|
||||
Reference in new issue
Block a user