mirror of
https://github.com/AssetRipper/AssetRipper.CIL.git
synced 2026-11-13 04:26:24 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
44cf58eef6 | ||
|
|
cb89ec09b2 | ||
|
|
677f5349b3 | ||
|
|
7fd99d382d | ||
|
|
2ad3472b3e | ||
|
|
c8e1b8809c | ||
|
|
a0c48e0216 | ||
|
|
1df36de7df | ||
|
|
89d8b4a7cd |
No files matched your search
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.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.Standardizer
|
||||
{
|
||||
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.CilMethodBody?.Instructions.StandardizeMacros();
|
||||
}
|
||||
module.Write(args[1]);
|
||||
Console.WriteLine("Done!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ public static class CilInstructionCollectionEquality
|
||||
{
|
||||
public static bool Equals(CilMethodBody left, CilMethodBody right)
|
||||
{
|
||||
// Local Variables
|
||||
if (left.LocalVariables.Count != right.LocalVariables.Count)
|
||||
{
|
||||
return false;
|
||||
@@ -22,9 +23,34 @@ public static class CilInstructionCollectionEquality
|
||||
}
|
||||
}
|
||||
|
||||
// Exception Handlers
|
||||
if (left.ExceptionHandlers.Count != right.ExceptionHandlers.Count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < left.ExceptionHandlers.Count; i++)
|
||||
{
|
||||
if (!Equals(left.ExceptionHandlers[i], right.ExceptionHandlers[i]))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Instructions
|
||||
return Equals(left.Instructions, right.Instructions);
|
||||
}
|
||||
|
||||
public static bool Equals(CilExceptionHandler left, CilExceptionHandler right)
|
||||
{
|
||||
return left.HandlerType == right.HandlerType
|
||||
&& left.TryStart?.Offset == right.TryStart?.Offset
|
||||
&& left.TryEnd?.Offset == right.TryEnd?.Offset
|
||||
&& left.HandlerStart?.Offset == right.HandlerStart?.Offset
|
||||
&& left.HandlerEnd?.Offset == right.HandlerEnd?.Offset
|
||||
&& left.FilterStart?.Offset == right.FilterStart?.Offset
|
||||
&& SignatureComparer.Default.Equals(left.ExceptionType, right.ExceptionType);
|
||||
}
|
||||
|
||||
public static bool Equals(CilInstructionCollection left, CilInstructionCollection right)
|
||||
{
|
||||
if (left.Count != right.Count)
|
||||
|
||||
+7
-1
@@ -14,7 +14,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
|
||||
README.md = README.md
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssetRipper.CIL.Validator", "AssetRipper.CIL.Validator\AssetRipper.CIL.Validator.csproj", "{0E177F49-031D-486A-ADD2-6823263D1923}"
|
||||
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
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@@ -30,6 +32,10 @@ Global
|
||||
{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
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
|
||||
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
|
||||
<PackageId>AssetRipper.CIL</PackageId>
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AsmResolver.DotNet" Version="6.0.0-beta.1" />
|
||||
<PackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
|
||||
<PackageReference Include="PolySharp" Version="1.15.0" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
using AsmResolver.DotNet;
|
||||
using AsmResolver.DotNet.Signatures;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace AssetRipper.CIL;
|
||||
|
||||
public static class EventDefinitionExtensions
|
||||
{
|
||||
public static bool IsInstance(this EventDefinition eventDefinition)
|
||||
{
|
||||
return (eventDefinition.AddMethod?.IsInstance() ?? false)
|
||||
|| (eventDefinition.RemoveMethod?.IsInstance() ?? false);
|
||||
}
|
||||
|
||||
public static bool IsStatic(this EventDefinition eventDefinition)
|
||||
{
|
||||
return (eventDefinition.AddMethod?.IsStatic ?? false)
|
||||
|| (eventDefinition.RemoveMethod?.IsStatic ?? false);
|
||||
}
|
||||
|
||||
public static bool HasAddMethod(this EventDefinition eventDefinition)
|
||||
{
|
||||
return eventDefinition.AddMethod is not null;
|
||||
}
|
||||
|
||||
public static bool HasRemoveMethod(this EventDefinition eventDefinition)
|
||||
{
|
||||
return eventDefinition.RemoveMethod is not null;
|
||||
}
|
||||
|
||||
public static bool HasFireMethod(this EventDefinition eventDefinition)
|
||||
{
|
||||
return eventDefinition.FireMethod is not null;
|
||||
}
|
||||
|
||||
public static bool HasBackingField(this EventDefinition eventDefinition, [NotNullWhen(true)] out FieldDefinition? backingField)
|
||||
{
|
||||
TypeSignature? eventType = eventDefinition.EventType?.ToTypeSignature();
|
||||
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))
|
||||
{
|
||||
backingField = field;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
backingField = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using AsmResolver.DotNet;
|
||||
using AsmResolver.DotNet.Signatures;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace AssetRipper.CIL;
|
||||
|
||||
public static class FieldDefinitionExtensions
|
||||
{
|
||||
public static bool IsInstance(this FieldDefinition field)
|
||||
{
|
||||
return !field.IsStatic;
|
||||
}
|
||||
|
||||
private static bool IsPropertyBackingField(this FieldDefinition field, [NotNullWhen(true)] out string? propertyName)
|
||||
{
|
||||
string? name = field.Name;
|
||||
if (name is ['<', ..] && name.EndsWith(">k__BackingField", StringComparison.Ordinal))
|
||||
{
|
||||
propertyName = name[1..^16];
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
propertyName = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsPropertyBackingField(this FieldDefinition field, [NotNullWhen(true)] out PropertyDefinition? property)
|
||||
{
|
||||
if (field.IsPropertyBackingField(out string? propertyName))
|
||||
{
|
||||
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);
|
||||
}
|
||||
else
|
||||
{
|
||||
property = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsEventBackingField(this FieldDefinition field, [NotNullWhen(true)] out EventDefinition? @event)
|
||||
{
|
||||
if (field.IsPrivate)
|
||||
{
|
||||
@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);
|
||||
}
|
||||
else
|
||||
{
|
||||
@event = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,68 +1,20 @@
|
||||
using AsmResolver.DotNet.Code.Cil;
|
||||
using AsmResolver.PE.DotNet.Cil;
|
||||
using AssetRipper.CIL.Manipulation;
|
||||
|
||||
namespace AssetRipper.CIL;
|
||||
|
||||
public static class MacroStandardization
|
||||
{
|
||||
/// <summary>
|
||||
/// This method expands all macros in the collection and applies some standardizations, such as removing nop instructions.
|
||||
/// This method expands all macros in the collection and applies some standardizations, such as removing nop and switch instructions.
|
||||
/// </summary>
|
||||
/// <param name="instructions">The collection to modify.</param>
|
||||
public static void StandardizeMacros(this CilInstructionCollection instructions)
|
||||
{
|
||||
instructions.ExpandMacros();
|
||||
instructions.RemoveNopInstructions();
|
||||
}
|
||||
|
||||
private static void RemoveNopInstructions(this CilInstructionCollection instructions)
|
||||
{
|
||||
// Find labels that reference nop instructions.
|
||||
HashSet<CilInstructionLabel> nopLabels = new();
|
||||
foreach (CilInstruction instruction in instructions)
|
||||
{
|
||||
if (instruction.Operand is CilInstructionLabel { Instruction: not null } label && label.Instruction.OpCode == CilOpCodes.Nop)
|
||||
{
|
||||
nopLabels.Add(label);
|
||||
}
|
||||
else if (instruction.Operand is CilInstructionLabel[] labels) // switch
|
||||
{
|
||||
foreach (CilInstructionLabel l in labels)
|
||||
{
|
||||
if (l.Instruction is not null && l.Instruction.OpCode == CilOpCodes.Nop)
|
||||
{
|
||||
nopLabels.Add(l);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Reassign labels to the next instruction, which is not a nop.
|
||||
foreach (CilInstructionLabel label in nopLabels)
|
||||
{
|
||||
int index = instructions.IndexOf(label.Instruction!);
|
||||
if (index < 0)
|
||||
{
|
||||
throw new InvalidOperationException("Label instruction not found");
|
||||
}
|
||||
|
||||
for (int i = index + 1; i < instructions.Count; i++)
|
||||
{
|
||||
if (instructions[i].OpCode != CilOpCodes.Nop)
|
||||
{
|
||||
label.Instruction = instructions[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove nop instructions
|
||||
for (int i = instructions.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (instructions[i].OpCode == CilOpCodes.Nop)
|
||||
{
|
||||
instructions.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
new CilManipulationContext(instructions)
|
||||
.Execute<MacroExpander>()
|
||||
.Execute<NopRemover>()
|
||||
.Execute<SwitchRemover>()
|
||||
.Execute<RedundantConversionRemover>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
using AsmResolver.DotNet.Code.Cil;
|
||||
using AsmResolver.PE.DotNet.Cil;
|
||||
|
||||
namespace AssetRipper.CIL.Manipulation;
|
||||
|
||||
public sealed class CilManipulationContext
|
||||
{
|
||||
private readonly Dictionary<CilInstruction, HashSet<CilInstructionLabel>> instructionReferences = new();
|
||||
public CilInstructionCollection Instructions { get; }
|
||||
|
||||
public CilManipulationContext(CilInstructionCollection instructions)
|
||||
{
|
||||
Instructions = instructions;
|
||||
|
||||
foreach (CilExceptionHandler handler in instructions.Owner.ExceptionHandlers)
|
||||
{
|
||||
AddReference(handler.TryStart as CilInstructionLabel);
|
||||
AddReference(handler.TryEnd as CilInstructionLabel);
|
||||
AddReference(handler.HandlerStart as CilInstructionLabel);
|
||||
AddReference(handler.HandlerEnd as CilInstructionLabel);
|
||||
AddReference(handler.FilterStart as CilInstructionLabel);
|
||||
}
|
||||
|
||||
foreach (CilInstruction instruction in instructions)
|
||||
{
|
||||
if (instruction.Operand is CilInstructionLabel label)
|
||||
{
|
||||
AddReference(label);
|
||||
}
|
||||
else if (instruction.Operand is IReadOnlyList<ICilLabel> labels) // switch
|
||||
{
|
||||
foreach (ICilLabel l in labels)
|
||||
{
|
||||
AddReference(l as CilInstructionLabel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool CanRemove(CilInstruction instruction) => !instructionReferences.ContainsKey(instruction);
|
||||
|
||||
public void Remove(CilInstruction instruction)
|
||||
{
|
||||
if (!CanRemove(instruction))
|
||||
{
|
||||
throw new InvalidOperationException("Cannot remove instruction with references");
|
||||
}
|
||||
Instructions.Remove(instruction);
|
||||
}
|
||||
|
||||
public bool TryRemove(CilInstruction instruction)
|
||||
{
|
||||
return CanRemove(instruction) && Instructions.Remove(instruction);
|
||||
}
|
||||
|
||||
public void Replace(CilInstruction oldInstruction, CilInstruction newInstruction)
|
||||
{
|
||||
if (instructionReferences.TryGetValue(oldInstruction, out HashSet<CilInstructionLabel>? references))
|
||||
{
|
||||
instructionReferences.Remove(oldInstruction);
|
||||
instructionReferences.Add(newInstruction, references);
|
||||
foreach (CilInstructionLabel label in references)
|
||||
{
|
||||
label.Instruction = newInstruction;
|
||||
}
|
||||
}
|
||||
Instructions[Instructions.IndexOf(oldInstruction)] = newInstruction;
|
||||
}
|
||||
|
||||
public void ReassignReferences(CilInstruction oldInstruction, CilInstruction newInstruction)
|
||||
{
|
||||
if (instructionReferences.TryGetValue(oldInstruction, out HashSet<CilInstructionLabel>? oldReferences))
|
||||
{
|
||||
instructionReferences.Remove(oldInstruction);
|
||||
foreach (CilInstructionLabel label in oldReferences)
|
||||
{
|
||||
label.Instruction = newInstruction;
|
||||
}
|
||||
|
||||
if (instructionReferences.TryGetValue(newInstruction, out HashSet<CilInstructionLabel>? newReferences))
|
||||
{
|
||||
newReferences.UnionWith(oldReferences);
|
||||
}
|
||||
else
|
||||
{
|
||||
instructionReferences.Add(newInstruction, oldReferences);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CilManipulationContext Execute<T>() where T : ICilManipulator, new()
|
||||
{
|
||||
SingletonCache<T>.Instance.Execute(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
private void AddReference(CilInstructionLabel? label)
|
||||
{
|
||||
CilInstruction? instruction = label?.Instruction;
|
||||
if (instruction is not null)
|
||||
{
|
||||
instructionReferences.GetOrCreate(instruction).Add(label!);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace AssetRipper.CIL.Manipulation;
|
||||
|
||||
internal static class DictionaryExtensions
|
||||
{
|
||||
public static TValue GetOrCreate<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key)
|
||||
where TKey : notnull
|
||||
where TValue : new()
|
||||
{
|
||||
if (!dictionary.TryGetValue(key, out TValue? value))
|
||||
{
|
||||
value = new TValue();
|
||||
dictionary[key] = value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace AssetRipper.CIL.Manipulation;
|
||||
|
||||
/// <summary>
|
||||
/// An interface for manipulating CIL instructions.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Derived classes are expected to be stateless and have a parameterless constructor.
|
||||
/// </remarks>
|
||||
public interface ICilManipulator
|
||||
{
|
||||
void Execute(CilManipulationContext context);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace AssetRipper.CIL.Manipulation;
|
||||
|
||||
public sealed class MacroExpander : ICilManipulator
|
||||
{
|
||||
public void Execute(CilManipulationContext context) => context.Instructions.ExpandMacros();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace AssetRipper.CIL.Manipulation;
|
||||
|
||||
public sealed class MacroOptimizer : ICilManipulator
|
||||
{
|
||||
public void Execute(CilManipulationContext context) => context.Instructions.OptimizeMacros();
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using AsmResolver.PE.DotNet.Cil;
|
||||
|
||||
namespace AssetRipper.CIL.Manipulation;
|
||||
|
||||
/// <summary>
|
||||
/// This removes all nop instructions.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This is a stronger version of <see cref="UnreferencedNopRemover"/>.
|
||||
/// </remarks>
|
||||
public sealed class NopRemover : ICilManipulator
|
||||
{
|
||||
public void Execute(CilManipulationContext context)
|
||||
{
|
||||
int i = 1;
|
||||
while (i < context.Instructions.Count)
|
||||
{
|
||||
CilInstruction previousInstruction = context.Instructions[i - 1];
|
||||
if (previousInstruction.OpCode == CilOpCodes.Nop)
|
||||
{
|
||||
CilInstruction currentInstruction = context.Instructions[i];
|
||||
context.ReassignReferences(previousInstruction, currentInstruction);
|
||||
context.Remove(previousInstruction);
|
||||
}
|
||||
else
|
||||
{
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using AsmResolver.PE.DotNet.Cil;
|
||||
|
||||
namespace AssetRipper.CIL.Manipulation;
|
||||
|
||||
public sealed class RedundantConversionRemover : ICilManipulator
|
||||
{
|
||||
public void Execute(CilManipulationContext context)
|
||||
{
|
||||
int i = 1;
|
||||
while (i < context.Instructions.Count)
|
||||
{
|
||||
CilInstruction previousInstruction = context.Instructions[i - 1];
|
||||
CilInstruction instruction = context.Instructions[i];
|
||||
if (previousInstruction.OpCode == instruction.OpCode
|
||||
&& instruction.OpCode.Code
|
||||
is CilCode.Conv_I1 or CilCode.Conv_I2 or CilCode.Conv_I4 or CilCode.Conv_I8
|
||||
or CilCode.Conv_U1 or CilCode.Conv_U2 or CilCode.Conv_U4 or CilCode.Conv_U8
|
||||
or CilCode.Conv_R4 or CilCode.Conv_R8
|
||||
or CilCode.Conv_I or CilCode.Conv_U
|
||||
&& (context.TryRemove(instruction) || context.TryRemove(previousInstruction)))
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace AssetRipper.CIL.Manipulation;
|
||||
|
||||
internal static class SingletonCache<T> where T : new()
|
||||
{
|
||||
public static T Instance { get; } = new T();
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using AsmResolver.DotNet;
|
||||
using AsmResolver.DotNet.Code.Cil;
|
||||
using AsmResolver.PE.DotNet.Cil;
|
||||
|
||||
namespace AssetRipper.CIL.Manipulation;
|
||||
|
||||
/// <summary>
|
||||
/// This manipulator removes switch instructions by converting them to a series of conditional branches.
|
||||
/// </summary>
|
||||
public sealed class SwitchRemover : ICilManipulator
|
||||
{
|
||||
public void Execute(CilManipulationContext context)
|
||||
{
|
||||
ModuleDefinition? module = context.Instructions.Owner.Owner.Module;
|
||||
if (module is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < context.Instructions.Count; i++)
|
||||
{
|
||||
CilInstruction instruction = context.Instructions[i];
|
||||
if (instruction.OpCode == CilOpCodes.Switch && instruction.Operand is IReadOnlyList<ICilLabel> labels)
|
||||
{
|
||||
CilLocalVariable switchValue = context.Instructions.AddLocalVariable(module.CorLibTypeFactory.UInt32);
|
||||
|
||||
context.Replace(instruction, new CilInstruction(CilOpCodes.Conv_U4));
|
||||
|
||||
context.Instructions.Insert(i + 1, new CilInstruction(CilOpCodes.Stloc, switchValue));
|
||||
i++; // No need to process the newly added instruction
|
||||
|
||||
for (int j = 0; j < labels.Count; j++)
|
||||
{
|
||||
ICilLabel label = labels[j];
|
||||
context.Instructions.Insert(i + 1, new CilInstruction(CilOpCodes.Ldloc, switchValue));
|
||||
context.Instructions.Insert(i + 2, new CilInstruction(CilOpCodes.Ldc_I4, j));
|
||||
context.Instructions.Insert(i + 3, new CilInstruction(CilOpCodes.Ceq));
|
||||
context.Instructions.Insert(i + 4, new CilInstruction(CilOpCodes.Brtrue, label));
|
||||
i += 4; // No need to process the newly added instructions
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using AsmResolver.PE.DotNet.Cil;
|
||||
|
||||
namespace AssetRipper.CIL.Manipulation;
|
||||
|
||||
/// <summary>
|
||||
/// This removes unreferenced nop instructions.
|
||||
/// </summary>
|
||||
public sealed class UnreferencedNopRemover : ICilManipulator
|
||||
{
|
||||
public void Execute(CilManipulationContext context)
|
||||
{
|
||||
int i = 0;
|
||||
while (i < context.Instructions.Count)
|
||||
{
|
||||
CilInstruction instruction = context.Instructions[i];
|
||||
if (instruction.OpCode == CilOpCodes.Nop && context.TryRemove(instruction))
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -87,8 +87,9 @@ public static class MethodDefinitionExtensions
|
||||
string backingFieldName = $"<{method.Name.Value[4..]}>k__BackingField";
|
||||
FieldDefinition? field = method.DeclaringType.Fields.FirstOrDefault(f => f.Name == backingFieldName);
|
||||
if (field is null
|
||||
|| !SignatureComparer.Default.Equals(field.Signature?.FieldType, method.Signature?.ReturnType)
|
||||
|| field.IsStatic != method.IsStatic)
|
||||
|| !field.IsPrivate
|
||||
|| field.IsStatic != method.IsStatic
|
||||
|| !SignatureComparer.Default.Equals(field.Signature?.FieldType, method.Signature?.ReturnType))
|
||||
{
|
||||
backingField = null;
|
||||
return false;
|
||||
@@ -116,8 +117,9 @@ public static class MethodDefinitionExtensions
|
||||
string backingFieldName = $"<{method.Name.Value[4..]}>k__BackingField";
|
||||
FieldDefinition? field = method.DeclaringType.Fields.FirstOrDefault(f => f.Name == backingFieldName);
|
||||
if (field is null
|
||||
|| !SignatureComparer.Default.Equals(field.Signature?.FieldType, method.Parameters[0].ParameterType)
|
||||
|| field.IsStatic != method.IsStatic)
|
||||
|| !field.IsPrivate
|
||||
|| field.IsStatic != method.IsStatic
|
||||
|| !SignatureComparer.Default.Equals(field.Signature?.FieldType, method.Parameters[0].ParameterType))
|
||||
{
|
||||
backingField = null;
|
||||
return false;
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
using AsmResolver.DotNet;
|
||||
using AsmResolver.DotNet.Signatures;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace AssetRipper.CIL;
|
||||
|
||||
public static class PropertyDefinitionExtensions
|
||||
{
|
||||
public static bool HasBackingField(this PropertyDefinition property, [NotNullWhen(true)] out FieldDefinition? backingField)
|
||||
{
|
||||
string backingFieldName = $"<{property.Name}>k__BackingField";
|
||||
backingField = property.DeclaringType?.Fields.FirstOrDefault(f => f.Name == backingFieldName);
|
||||
if (backingField is null
|
||||
|| !backingField.IsPrivate
|
||||
|| backingField.IsStatic != property.IsStatic()
|
||||
|| !SignatureComparer.Default.Equals(backingField.Signature?.FieldType, property.Signature?.ReturnType))
|
||||
{
|
||||
backingField = null;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool HasGetMethod(this PropertyDefinition property)
|
||||
{
|
||||
return property.GetMethod is not null;
|
||||
}
|
||||
|
||||
public static bool HasSetMethod(this PropertyDefinition property)
|
||||
{
|
||||
return property.SetMethod is not null;
|
||||
}
|
||||
|
||||
public static bool HasBothAccessors(this PropertyDefinition property)
|
||||
{
|
||||
return property.HasGetMethod() && property.HasSetMethod();
|
||||
}
|
||||
|
||||
public static bool IsInstance(this PropertyDefinition property) => property.Signature?.HasThis ?? false;
|
||||
|
||||
public static bool IsStatic(this PropertyDefinition property) => !property.Signature?.HasThis ?? false;
|
||||
}
|
||||
+26
-26
@@ -1,27 +1,27 @@
|
||||
<Project>
|
||||
<PropertyGroup>
|
||||
<LangVersion>Latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsAotCompatible>true</IsAotCompatible>
|
||||
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
|
||||
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>
|
||||
|
||||
<Version>1.1.1</Version>
|
||||
<AssemblyVersion>$(Version)</AssemblyVersion>
|
||||
<Copyright>Copyright (c) 2024 ds5678</Copyright>
|
||||
<Authors>ds5678</Authors>
|
||||
<Company>AssetRipper</Company>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<RepositoryUrl>https://github.com/AssetRipper/AssetRipper.CIL</RepositoryUrl>
|
||||
<PackageProjectUrl>$(RepositoryUrl)</PackageProjectUrl>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">
|
||||
<IsTrimmable>true</IsTrimmable>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
|
||||
<IsAotCompatible>true</IsAotCompatible>
|
||||
</PropertyGroup>
|
||||
<Project>
|
||||
<PropertyGroup>
|
||||
<LangVersion>Latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsAotCompatible>true</IsAotCompatible>
|
||||
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
|
||||
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>
|
||||
|
||||
<Version>1.1.3</Version>
|
||||
<AssemblyVersion>$(Version)</AssemblyVersion>
|
||||
<Copyright>Copyright (c) 2024 ds5678</Copyright>
|
||||
<Authors>ds5678</Authors>
|
||||
<Company>AssetRipper</Company>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<RepositoryUrl>https://github.com/AssetRipper/AssetRipper.CIL</RepositoryUrl>
|
||||
<PackageProjectUrl>$(RepositoryUrl)</PackageProjectUrl>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">
|
||||
<IsTrimmable>true</IsTrimmable>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
|
||||
<IsAotCompatible>true</IsAotCompatible>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
Reference in new issue
Block a user