From cbafc5afc23ca6f49863042cafb3c9ba91b0ca0a Mon Sep 17 00:00:00 2001 From: Oleksandr HULYI Date: Fri, 9 Jun 2023 22:41:03 +1200 Subject: [PATCH] Attempt to fix #217 --- .../Weavers/Processes/AdviceAroundProcess.cs | 38 ++++++++++++++----- .../AspectInjector.Core.csproj | 2 +- src/AspectInjector/Program.cs | 4 +- src/FluentIL/FluentIL.csproj | 2 +- src/FluentIL/MethodEditor.cs | 13 +++++-- .../AspectInjector.Analyzer.Tests.csproj | 6 +-- .../AspectInjector.Tests.Generics.csproj | 4 +- .../AspectInjector.Tests.Integrity.csproj | 6 +-- .../AspectInjector.Tests.Runtime.csproj | 12 +++--- .../AspectInjector.Tests.RuntimeAssets.csproj | 6 +-- .../AspectInjector.Tests.VBRuntime.vbproj | 12 +++--- 11 files changed, 64 insertions(+), 41 deletions(-) diff --git a/src/AspectInjector.Core.Advice/Weavers/Processes/AdviceAroundProcess.cs b/src/AspectInjector.Core.Advice/Weavers/Processes/AdviceAroundProcess.cs index a735dd21..f065ddfe 100644 --- a/src/AspectInjector.Core.Advice/Weavers/Processes/AdviceAroundProcess.cs +++ b/src/AspectInjector.Core.Advice/Weavers/Processes/AdviceAroundProcess.cs @@ -1,4 +1,4 @@ -using AspectInjector.Core.Advice.Effects; +using AspectInjector.Core.Advice.Effects; using AspectInjector.Core.Extensions; using AspectInjector.Core.Models; using FluentIL; @@ -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); @@ -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); @@ -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))); @@ -323,4 +341,4 @@ private static string GetAroundMethodPrefix(MethodDefinition target) return $"{Constants.Prefix}around_{target.Name}_{target.MetadataToken.ToUInt32()}_"; } } -} \ No newline at end of file +} diff --git a/src/AspectInjector.Core/AspectInjector.Core.csproj b/src/AspectInjector.Core/AspectInjector.Core.csproj index 5ee09647..ba676855 100755 --- a/src/AspectInjector.Core/AspectInjector.Core.csproj +++ b/src/AspectInjector.Core/AspectInjector.Core.csproj @@ -6,7 +6,7 @@ - + diff --git a/src/AspectInjector/Program.cs b/src/AspectInjector/Program.cs index ab405281..8959e93b 100644 --- a/src/AspectInjector/Program.cs +++ b/src/AspectInjector/Program.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; @@ -18,7 +18,7 @@ private static int Main(string[] args) var optimize = false; var verbose = false; - List references = new List(); + List references = new(); for (int i = 0; i < args.Length; i++) { diff --git a/src/FluentIL/FluentIL.csproj b/src/FluentIL/FluentIL.csproj index d8e84a5a..6a63ae6d 100644 --- a/src/FluentIL/FluentIL.csproj +++ b/src/FluentIL/FluentIL.csproj @@ -7,7 +7,7 @@ - + diff --git a/src/FluentIL/MethodEditor.cs b/src/FluentIL/MethodEditor.cs index cfee377c..6620494b 100644 --- a/src/FluentIL/MethodEditor.cs +++ b/src/FluentIL/MethodEditor.cs @@ -1,4 +1,4 @@ -using FluentIL.Extensions; +using FluentIL.Extensions; using Mono.Cecil; using Mono.Cecil.Cil; using System; @@ -64,7 +64,7 @@ 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; @@ -72,7 +72,12 @@ public static void Mark(this MethodDefinition method, TypeReference attribute) 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) @@ -146,4 +151,4 @@ public static void OnEveryOccasionOf(this MethodBody body, Func - + all runtime; build; native; contentfiles; analyzers - - + + all runtime; build; native; contentfiles; analyzers diff --git a/tests/AspectInjector.Tests.Generics/AspectInjector.Tests.Generics.csproj b/tests/AspectInjector.Tests.Generics/AspectInjector.Tests.Generics.csproj index eace375e..d2604515 100755 --- a/tests/AspectInjector.Tests.Generics/AspectInjector.Tests.Generics.csproj +++ b/tests/AspectInjector.Tests.Generics/AspectInjector.Tests.Generics.csproj @@ -1,7 +1,7 @@ - + - netcoreapp3.1;net6.0 + net6.0 7.1 Debug;Release;DebugTests diff --git a/tests/AspectInjector.Tests.Integrity/AspectInjector.Tests.Integrity.csproj b/tests/AspectInjector.Tests.Integrity/AspectInjector.Tests.Integrity.csproj index 5ec56499..bf781759 100755 --- a/tests/AspectInjector.Tests.Integrity/AspectInjector.Tests.Integrity.csproj +++ b/tests/AspectInjector.Tests.Integrity/AspectInjector.Tests.Integrity.csproj @@ -7,9 +7,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers diff --git a/tests/AspectInjector.Tests.Runtime/AspectInjector.Tests.Runtime.csproj b/tests/AspectInjector.Tests.Runtime/AspectInjector.Tests.Runtime.csproj index 4b31277f..6851d6ee 100755 --- a/tests/AspectInjector.Tests.Runtime/AspectInjector.Tests.Runtime.csproj +++ b/tests/AspectInjector.Tests.Runtime/AspectInjector.Tests.Runtime.csproj @@ -1,17 +1,17 @@ - + - netcoreapp3.1;net6.0;net471;net461 + net6.0;net462;net470;net480 7.3 Debug;Release;DebugTests - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + all runtime; build; native; contentfiles; analyzers diff --git a/tests/AspectInjector.Tests.RuntimeAssets/AspectInjector.Tests.RuntimeAssets.csproj b/tests/AspectInjector.Tests.RuntimeAssets/AspectInjector.Tests.RuntimeAssets.csproj index 4da34867..11d0d380 100755 --- a/tests/AspectInjector.Tests.RuntimeAssets/AspectInjector.Tests.RuntimeAssets.csproj +++ b/tests/AspectInjector.Tests.RuntimeAssets/AspectInjector.Tests.RuntimeAssets.csproj @@ -1,10 +1,10 @@ - + Debug;Release;DebugTests - netstandard2.0;netcoreapp3.1;net6.0;net471;net461 + netstandard2.0;net6.0;net462;net470;net480 - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/tests/AspectInjector.Tests.VBRuntime/AspectInjector.Tests.VBRuntime.vbproj b/tests/AspectInjector.Tests.VBRuntime/AspectInjector.Tests.VBRuntime.vbproj index cabf38d8..8cba79dc 100755 --- a/tests/AspectInjector.Tests.VBRuntime/AspectInjector.Tests.VBRuntime.vbproj +++ b/tests/AspectInjector.Tests.VBRuntime/AspectInjector.Tests.VBRuntime.vbproj @@ -1,16 +1,16 @@ - + - netcoreapp3.1;net6.0;net471;net461 + net6.0;net462;net470;net480 Debug;Release;DebugTests - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + all runtime; build; native; contentfiles; analyzers