Skip to content

Commit

Permalink
Attempt to fix #217
Browse files Browse the repository at this point in the history
  • Loading branch information
pamidur committed Jun 9, 2023
1 parent 36423da commit cbafc5a
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AspectInjector.Core.Advice.Effects;
using AspectInjector.Core.Advice.Effects;
using AspectInjector.Core.Extensions;
using AspectInjector.Core.Models;
using FluentIL;
Expand Down Expand Up @@ -82,7 +82,7 @@ private void RedirectPreviousWrapper(MethodDefinition prev, MethodDefinition nex
{
var unwrapper = GetOrCreateUnwrapper();

var instructions = prev.Body.Instructions.Where(i => i.Operand is MethodReference && ((MethodReference)i.Operand).Resolve() == unwrapper).ToList();
var instructions = prev.Body.Instructions.Where(i => i.Operand is MethodReference reference && reference.Resolve() == unwrapper).ToList();

var nextRef = CreateRef(next, prev);

Expand Down Expand Up @@ -238,7 +238,9 @@ private void MoveBody(MethodDefinition from, MethodDefinition to)
var fbProc = from.Body.GetILProcessor();
var fdbg = from.DebugInformation;
var fsp = from.DebugInformation.SequencePoints;
var tsp = to.DebugInformation.SequencePoints;
var tsp = to.DebugInformation.SequencePoints;

to.HasThis = from.HasThis;

var codeStart = from.Body.GetUserCodeStart();
var init = codeStart == null ? 0 : frb.IndexOf(codeStart);
Expand All @@ -261,18 +263,34 @@ private void MoveBody(MethodDefinition from, MethodDefinition to)
}
}

to.DebugInformation.Scope = new ScopeDebugInformation(to.Body.Instructions.First(), to.Body.Instructions.Last());

if (from.DebugInformation.Scope != null)
to.DebugInformation.Scope.Import = from.DebugInformation.Scope.Import;
if (from.DebugInformation.Scope != null)
{
to.DebugInformation.Scope = from.DebugInformation.Scope;
to.DebugInformation.Scope.Start = new InstructionOffset(to.Body.Instructions[0].Offset);
to.DebugInformation.Scope.End = new InstructionOffset(to.Body.Instructions[to.Body.Instructions.Count - 1].Offset);
from.DebugInformation.Scope = null;
}


//to.DebugInformation.StateMachineKickOffMethod = from;

if (from.DebugInformation.StateMachineKickOffMethod != null)
{
to.DebugInformation.StateMachineKickOffMethod = from.DebugInformation.StateMachineKickOffMethod;
from.DebugInformation.StateMachineKickOffMethod = null;
}

if (from.DebugInformation.HasCustomDebugInformations)
foreach (var cdi in from.DebugInformation.CustomDebugInformations.ToArray())
{
from.DebugInformation.CustomDebugInformations.Remove(cdi);
to.DebugInformation.CustomDebugInformations.Add(cdi);
}

}

//var cstm = new StateMachineScopeDebugInformation();
//cstm.Scopes.Add(new StateMachineScope(to.Body.Instructions[0], to.Body.Instructions[to.Body.Instructions.Count - 1]));
//to.CustomDebugInformations.Add(cstm);

var to_vars = to.Body.Variables;
foreach (var var in from.Body.Variables)
to_vars.Add(new VariableDefinition(to.Module.ImportReference(var.VariableType)));
Expand Down Expand Up @@ -323,4 +341,4 @@ private static string GetAroundMethodPrefix(MethodDefinition target)
return $"{Constants.Prefix}around_{target.Name}_{target.MetadataToken.ToUInt32()}_";
}
}
}
}
2 changes: 1 addition & 1 deletion src/AspectInjector.Core/AspectInjector.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Mono.Cecil" Version="0.11.4" />
<PackageReference Include="Mono.Cecil" Version="0.11.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FluentIL\FluentIL.csproj" />
Expand Down
4 changes: 2 additions & 2 deletions src/AspectInjector/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand All @@ -18,7 +18,7 @@ private static int Main(string[] args)

var optimize = false;
var verbose = false;
List<string> references = new List<string>();
List<string> references = new();

for (int i = 0; i < args.Length; i++)
{
Expand Down
2 changes: 1 addition & 1 deletion src/FluentIL/FluentIL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Mono.Cecil" Version="0.11.4" />
<PackageReference Include="Mono.Cecil" Version="0.11.5" />
</ItemGroup>

<ItemGroup>
Expand Down
13 changes: 9 additions & 4 deletions src/FluentIL/MethodEditor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FluentIL.Extensions;
using FluentIL.Extensions;
using Mono.Cecil;
using Mono.Cecil.Cil;
using System;
Expand Down Expand Up @@ -64,15 +64,20 @@ public static void Instead(this MethodBody body, PointCut action)
.Here(action);
}

public static void Mark(this MethodDefinition method, TypeReference attribute)
public static void Mark(this MethodDefinition method, TypeReference attribute, params CustomAttributeArgument[] args)
{
if (method.CustomAttributes.Any(ca => ca.AttributeType.FullName == attribute.FullName))
return;

var constructor = method.Module.ImportReference(attribute).Resolve()
.Methods.First(m => m.IsConstructor && !m.IsStatic);

method.CustomAttributes.Add(new CustomAttribute(method.Module.ImportReference(constructor)));
var attr = new CustomAttribute(method.Module.ImportReference(constructor));

foreach (var arg in args)
attr.ConstructorArguments.Add(arg);

method.CustomAttributes.Add(attr);
}

public static Instruction GetCodeStart(this MethodBody body)
Expand Down Expand Up @@ -146,4 +151,4 @@ public static void OnEveryOccasionOf(this MethodBody body, Func<Instruction, boo
new Cut(body, curi).Here(pc);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.10.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
<TargetFrameworks>net6.0</TargetFrameworks>
<LangVersion>7.1</LangVersion>
<Configurations>Debug;Release;DebugTests</Configurations>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

<ItemGroup>
<PackageReference Include="Unofficial.CoreRT.ILVerify" Version="0.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net6.0;net471;net461</TargetFrameworks>
<TargetFrameworks>net6.0;net462;net470;net480</TargetFrameworks>
<LangVersion>7.3</LangVersion>
<Configurations>Debug;Release;DebugTests</Configurations>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.2">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configurations>Debug;Release;DebugTests</Configurations>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net6.0;net471;net461</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net6.0;net462;net470;net480</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.2">
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net6.0;net471;net461</TargetFrameworks>
<TargetFrameworks>net6.0;net462;net470;net480</TargetFrameworks>
<Configurations>Debug;Release;DebugTests</Configurations>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.2">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit cbafc5a

Please sign in to comment.