From a85e1a96360fbeb02c6501f0eb542bfe360faa99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9C=A8=E6=A3=AE=20=C2=B7=20=E4=BD=9C=E9=9C=96?= <16236903+NMSAzulX@users.noreply.github.com> Date: Sun, 24 Dec 2023 18:58:18 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=90=88=E5=B9=B6=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 增加实现程序集的元数据解析。 2. 增加方法使用率扩展。 3. 区分智能与简洁模式。 4. 优化 API。 --- Natasha.sln | 7 + .../HotReloadPlugin/HotReloadPlugin.csproj | 2 +- samples/HotReloadSample/Program.cs | 8 +- samples/ReferenceSample/Program.cs | 159 ++++++----- .../ReferenceSample/ReferenceSample.csproj | 6 +- src/Directory.Build.props | 3 - .../Extension/AssemblyExtension.cs | 19 ++ .../AssenblyCSharpBuilderExtension.cs | 51 ++++ .../Natasha.CSharp.Codecov.csproj | 14 + .../Utils/CodecovMonitor.cs | 66 +++++ .../Utils/CodecovRecorder.cs | 50 ++++ .../Utils/MethodDefinedFilter.cs | 29 ++ .../AssemblyCSharpBuilder.Compile.cs | 68 ++++- .../AssemblyCSharpBuilder.Domain.cs | 68 +---- .../CompileUnit/AssemblyCSharpBuilder.cs | 49 ++-- .../Configuration/DomainConfiguration.cs | 52 ++++ .../Component/Domain/NatashaReferenceCache.cs | 54 ++-- .../Domain/NatashaReferenceDomain.cs | 14 +- .../Component/Using/GlobalUsingHelper.cs | 31 +-- .../NatashaAssemblyBuilderMultiExtension.cs | 57 ++-- .../MultiDomain/NatashaInitializer.cs | 176 +++++++----- .../MultiDomain/NatashaManagement.cs | 41 ++- .../Natasha.CSharp.Compiler.csproj | 1 - .../AssemblyCSharpBuilder.Compile.cs | 15 +- .../AssemblyCSharpBuilder.CompileOption.cs | 1 + .../Configuration/DebugConfiguration.cs | 0 .../Model/CombineReferenceBehavior.cs | 5 +- .../Component/Metadata/MetadataHelper.cs | 252 ++++++++++++++++++ .../Extension/NatashaAssemblyExtension.cs | 45 +++- .../Public/Extension/NatashaTypeExtension.cs | 15 +- .../Public/NatashaManagement.cs | 6 +- .../Public/NatashaReferencePathsHelper.cs | 23 +- .../AssemblyCSharpBuilder.Ouput.Single.cs | 2 +- .../CompileUnit/AssemblyCSharpBuilder.cs | 7 +- .../Public/API/Level2/Deconstruct/DString.cs | 4 + .../Core/Model/PluginLoadBehavior.cs | 3 +- .../Core/NatashaDomain.Default.cs | 30 +-- src/Natasha.Domain/Core/NatashaDomain.cs | 2 +- src/Natasha.Domain/Natasha.Domain.csproj | 1 - test/ut/NatashaFunctionUT/Class1.cs | 10 - .../Compile/CompileInSameDomainTest.cs | 2 + .../Compile/CompileLogTest.cs | 2 + .../Compile/CompileSemanticTest.cs | 1 + .../Compile/CompileUnitTest.cs | 3 + test/ut/NatashaFunctionUT/DomainPrepare.cs | 8 +- .../NatashaFunctionUT.csproj | 3 +- .../Special/DeconstructTest.cs | 1 + .../Special/Private/PrivateTest.cs | 35 ++- .../Special/Readonly/ReadonlyTest.cs | 4 +- .../Template/Compile/ClassBuilderTest.cs | 6 +- .../Template/Compile/EnumBuilderTest.cs | 4 +- .../Template/Compile/FakeOperatorTest.cs | 8 +- .../Template/Compile/FastOperatorTest.cs | 9 +- .../Template/Compile/InterfaceBuilderTest.cs | 2 +- .../Template/Compile/NDelegateTest.cs | 13 +- .../Template/Compile/StructBuilderTest.cs | 4 +- .../Compile/CompilePrepare.cs | 2 +- 57 files changed, 1153 insertions(+), 400 deletions(-) create mode 100644 src/Natasha.CSharp/Extension/Natasha.CSharp.Codecov/Extension/AssemblyExtension.cs create mode 100644 src/Natasha.CSharp/Extension/Natasha.CSharp.Codecov/Extension/AssenblyCSharpBuilderExtension.cs create mode 100644 src/Natasha.CSharp/Extension/Natasha.CSharp.Codecov/Natasha.CSharp.Codecov.csproj create mode 100644 src/Natasha.CSharp/Extension/Natasha.CSharp.Codecov/Utils/CodecovMonitor.cs create mode 100644 src/Natasha.CSharp/Extension/Natasha.CSharp.Codecov/Utils/CodecovRecorder.cs create mode 100644 src/Natasha.CSharp/Extension/Natasha.CSharp.Codecov/Utils/MethodDefinedFilter.cs create mode 100644 src/Natasha.CSharp/Natasha.CSharp.Compiler/MultiDomain/Component/Configuration/DomainConfiguration.cs rename src/Natasha.CSharp/Natasha.CSharp.Compiler/Public/Component/Compiler/{Model => }/Configuration/DebugConfiguration.cs (100%) create mode 100644 src/Natasha.CSharp/Natasha.CSharp.Compiler/Public/Component/Metadata/MetadataHelper.cs delete mode 100644 test/ut/NatashaFunctionUT/Class1.cs diff --git a/Natasha.sln b/Natasha.sln index 1a6dc0c0..ff9ca19c 100644 --- a/Natasha.sln +++ b/Natasha.sln @@ -201,6 +201,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotReloadSample", "samples\ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotReloadPlugin", "samples\HotReloadPlugin\HotReloadPlugin.csproj", "{89A2EC20-B432-4C6A-B740-78BBD1BF80EC}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Natasha.CSharp.Codecov", "src\Natasha.CSharp\Extension\Natasha.CSharp.Codecov\Natasha.CSharp.Codecov.csproj", "{63C14375-8D73-43C8-BD7A-4BEC53456E4C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -367,6 +369,10 @@ Global {89A2EC20-B432-4C6A-B740-78BBD1BF80EC}.Debug|Any CPU.Build.0 = Debug|Any CPU {89A2EC20-B432-4C6A-B740-78BBD1BF80EC}.Release|Any CPU.ActiveCfg = Release|Any CPU {89A2EC20-B432-4C6A-B740-78BBD1BF80EC}.Release|Any CPU.Build.0 = Release|Any CPU + {63C14375-8D73-43C8-BD7A-4BEC53456E4C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {63C14375-8D73-43C8-BD7A-4BEC53456E4C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {63C14375-8D73-43C8-BD7A-4BEC53456E4C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {63C14375-8D73-43C8-BD7A-4BEC53456E4C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -436,6 +442,7 @@ Global {716B9CEC-8CA2-433F-9E9A-974E536C4099} = {F4622BCB-C287-4AFE-916B-8C138BD15A3D} {7CD6F3A9-85F0-4F01-8B68-7FCC527768E6} = {B4487ABB-27A2-4353-AB16-1A94B31E0210} {89A2EC20-B432-4C6A-B740-78BBD1BF80EC} = {B4487ABB-27A2-4353-AB16-1A94B31E0210} + {63C14375-8D73-43C8-BD7A-4BEC53456E4C} = {0CB3AF52-50F3-4728-BAC6-389F2E995111} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3004E730-B231-40FA-B75C-58D7DDE17679} diff --git a/samples/HotReloadPlugin/HotReloadPlugin.csproj b/samples/HotReloadPlugin/HotReloadPlugin.csproj index fa71b7ae..90c02b08 100644 --- a/samples/HotReloadPlugin/HotReloadPlugin.csproj +++ b/samples/HotReloadPlugin/HotReloadPlugin.csproj @@ -1,7 +1,7 @@  - net8.0 + net6.0; enable enable diff --git a/samples/HotReloadSample/Program.cs b/samples/HotReloadSample/Program.cs index aef7d96f..e7fb352e 100644 --- a/samples/HotReloadSample/Program.cs +++ b/samples/HotReloadSample/Program.cs @@ -107,7 +107,7 @@ public unsafe static void Test(Assembly assembly) foreach (var handle in asmDefinition.NamespaceDefinitions) { var nameDefinition = metaReader.GetNamespaceDefinition(handle); - Console.WriteLine($"{nameDefinition.Name.IsNil} : {metaReader.GetString(handle)} : {nameDefinition.TypeDefinitions.Count()}"); + Console.WriteLine($"{nameDefinition.Name.IsNil} : {metaReader.GetString(handle)} : {nameDefinition.TypeDefinitions.Length}"); } } } @@ -118,8 +118,10 @@ private static void HotReloadService_UpdateApplicationEvent(Type[]? obj) public static Assembly OldAssembly(NatashaReferenceDomain domain) { - AssemblyCSharpBuilder builder = new AssemblyCSharpBuilder(); - builder.Domain = domain; + AssemblyCSharpBuilder builder = new AssemblyCSharpBuilder + { + Domain = domain + }; builder.Add("public class A{ public int Code = 1; public void Show(){ Console.WriteLine(Code); } }", UsingLoadBehavior.WithDefault); return builder.GetAssembly(); } diff --git a/samples/ReferenceSample/Program.cs b/samples/ReferenceSample/Program.cs index 0d7719cd..53127b26 100644 --- a/samples/ReferenceSample/Program.cs +++ b/samples/ReferenceSample/Program.cs @@ -1,16 +1,10 @@ -using Microsoft.CodeAnalysis; -using System.Reflection; -using System.Security.Cryptography.X509Certificates; -using System.Text.RegularExpressions; -using Internal; -using System.Runtime.CompilerServices; -using static System.Diagnostics.DebuggableAttribute; -using System.Diagnostics; -using System.Diagnostics.SymbolStore; -using HarmonyLib; +using HotReloadPlugin; +using Microsoft.CodeAnalysis; +using Natasha.CSharp.Codecov.Utils; using System.Diagnostics.CodeAnalysis; +using System.Reflection; using System.Reflection.PortableExecutable; -using static System.Reflection.Metadata.BlobBuilder; +using System.Runtime.CompilerServices; namespace ReferenceSample { @@ -29,12 +23,14 @@ static void Main(string[] args) //var p = typeof(C).GetProperty("ReferenceSample.Program.B.Name", BindingFlags.NonPublic | BindingFlags.Instance); //var p1 = typeof(C).GetProperties(BindingFlags.NonPublic | BindingFlags.Instance); //Console.WriteLine(p.Name); - TestMini(); - //NatashaManagement.Preheating(false, false); + //TestMini(); + //var domain = NatashaManagement.CreateRandomDomain(); //var asm = domain.LoadPluginUseDefaultDependency("I:\\OpenSource\\Natasha\\samples\\ReferenceSample\\bin\\Debug\\net8.0\\DynamicLibraryFolders\\Nc0e9a864079d427680ea239b5a9e525e\\a69937be3d244336a20c46843d51d19b.dll"); - - //TestMini(); + //var a = typeof(CodecovMonitor); + //var b = typeof(A); + //NatashaManagement.Preheating(true, true); + TestMini(); //var a = Math.Min(1, args.Length); //NatashaManagement.Preheating(false, false); //Console.WriteLine("============================="); @@ -85,11 +81,16 @@ public static void TestMini1() AssemblyCSharpBuilder builder = new(); builder .UseRandomDomain() - .WithDebugCompile(item => item.WriteToAssembly()) - .AddReferenceAndUsingCode(typeof(object).Assembly) - .AddReferenceAndUsingCode(typeof(Math).Assembly) - .AddReferenceAndUsingCode(typeof(MathF).Assembly) - .AddReferenceAndUsingCode(typeof(SuppressMessageAttribute)); + .WithCombineUsingCode(UsingLoadBehavior.WithDefault) + .UseSmartMode() + //.UseSimpleMode() + ////.WithOutsideReferences([MetadataReference.CreateFromFile("a")]) + //.WithDebugCompile(item => item.WriteToAssembly()) + //.AddReferenceAndUsingCode(typeof(object).Assembly) + //.AddReferenceAndUsingCode(typeof(Math).Assembly) + //.AddReferenceAndUsingCode(typeof(MathF).Assembly) + //.AddReferenceAndUsingCode(typeof(SuppressMessageAttribute)) + ; builder.Add(@" namespace MyNamespace{ @@ -117,21 +118,24 @@ public static object Invoke(){ } public static void TestMini() { - AssemblyCSharpBuilder builder = new(); - builder.WithAnalysisAccessibility() + + AssemblyCSharpBuilder builder = new(); + builder .UseRandomDomain() - .WithFileOutput() - .WithDebugCompile(item=>item.WriteToFile()) - .AddReferenceAndUsingCode(typeof(HarmonyPatch)) - .AddReferenceAndUsingCode(typeof(DebuggableAttribute)) - .AddReferenceAndUsingCode(typeof(object).Assembly) + .UseSmartMode() + .ConfigCompilerOption(item => item.WithLowerVersionsAssembly().WithDiagnosticLevel(ReportDiagnostic.Warn)) + //.WithFileOutput() + .WithDebugCompile(item => item.WriteToAssembly()) + .UseSimpleMode() .AddReferenceAndUsingCode(typeof(Math).Assembly) .AddReferenceAndUsingCode(typeof(MathF).Assembly) + .AddReferenceAndUsingCode(typeof(A)).AddDependencyReferences(typeof(A)) + .AddReferenceAndUsingCode(typeof(CodecovMonitor)).AddDependencyReferences(typeof(CodecovMonitor)) .AddReferenceAndUsingCode(typeof(SuppressMessageAttribute)); + builder.Add(@" -[assembly: TargetFramework("".NETCoreApp,Version=v8.0"", FrameworkDisplayName = "".NET 8.0"")] public class A{ public static int N1 = 10; public static float N2 = 1.2F; @@ -148,12 +152,17 @@ public static object Invoke2(){ /// 我的动态方法,返回科学计算结果。 /// public static object Invoke(){ - var type = typeof(HarmonyPatch); - var type2 = typeof(HarmonyPrefix); - int[] a = [1,2,3]; - return N1 + MathF.Log10((float)Math.Sqrt(MathF.Sqrt(N2) + Math.Tan(N3))); + if(N1 == 10){ + HotReloadPlugin.A asdasd = new(); + asdasd.Show(); + int[] a = [1,2,3]; + Console.WriteLine(a); + return N1 + MathF.Log10((float)Math.Sqrt(MathF.Sqrt(N2) + Math.Tan(N3))); + } + else{ + return 0; + } } - [SuppressMessage(""Microsoft.Performance"", ""CA1801:ReviewUnusedParameters"", MessageId = ""isChecked"")] public object Test(){ return N1; } @@ -170,49 +179,59 @@ public object Invoke2(){ return new T(); } }"); - builder.Add(@" -namespace Microsoft.CodeAnalysis.Runtime -{ - public static class Instrumentation + + //a09e6bef-ff64-4b5f-8bb8-fc495ebb50ba + DebugDirectoryBuilder debug = new(); + debug.AddReproducibleEntry(); + debug.AddReproducibleEntry(); + + + + builder.WithCodecov(); + var asm = builder.GetAssembly(); + + //执行A.Invoke + var funcA = asm.GetDelegateFromShortName>("A", "Invoke"); + funcA(); + + //执行B.Invoke2 + var typeB = asm.GetType("B"); + var funcB = typeB.GetMethod("Invoke2"); + funcB.Invoke(Activator.CreateInstance(typeB), null); + var list = asm.GetCodecovCollection(); + Show(list); + + + //重置 + Console.WriteLine("===================Reset============="); + asm.ResetCodecov(); + //执行B.Invoke2 + funcB.Invoke(Activator.CreateInstance(typeB), null); + list = asm.GetCodecovCollection(); + Show(list); + //Console.WriteLine(result); + } + public static void Show(List<(string MethodName, bool[] Usage)> list) { - public static bool[] CreatePayload(System.Guid mvid, int methodToken, int fileIndex, ref bool[] payload, int payloadLength) - { - Console.WriteLine(mvid.ToString()); - if (payload == null) + for (int i = 0; i < list!.Count; i++) + { + if (list[i].Usage != null) { - payload = new bool[payloadLength]; + if (list[i].Usage.Length == 0) + { + Console.WriteLine($"{list[i].MethodName} 执行:100%"); + } + else + { + var executeCount = list[i].Usage.Count(item => item); + Console.WriteLine($"{list[i].MethodName} 执行:{((double)executeCount / list[i].Usage.Length).ToString("P")}"); + } } - return payload; - } - - public static bool[] CreatePayload(System.Guid mvid, int methodToken, int[] fileIndices, ref bool[] payload, int payloadLength) - { - if (payload == null) + else { - payload = new bool[payloadLength]; + Console.WriteLine($"{list[i].MethodName} 未执行!"); } - return payload; } - - public static void FlushPayload() - { - - } - } -}"); - //a09e6bef-ff64-4b5f-8bb8-fc495ebb50ba - DebugDirectoryBuilder debug = new(); - debug.AddReproducibleEntry(); - debug.AddReproducibleEntry(); - var asm = builder.GetAssembly(); - var type = asm.GetType("A"); - var type1 = asm.GetType(""); - var method = type.GetMethod("Invoke"); - var result = method.Invoke(null, null); - //var method2 = type.GetMethod("Invoke2"); - //var result2 = method2.Invoke(Activator.CreateInstance(type), null); - result = method.Invoke(null, null); - Console.WriteLine(result); } [MethodImpl(MethodImplOptions.NoInlining)] diff --git a/samples/ReferenceSample/ReferenceSample.csproj b/samples/ReferenceSample/ReferenceSample.csproj index ccea1438..64599343 100644 --- a/samples/ReferenceSample/ReferenceSample.csproj +++ b/samples/ReferenceSample/ReferenceSample.csproj @@ -5,6 +5,7 @@ net8.0 enable enable + preview zh-Hans @@ -33,11 +34,12 @@ - + - + +